import{s as Rt,o as Ht,n as $e}from"../chunks/scheduler.defa9a21.js";import{S as Yt,i as Vt,g as $,s as c,r as f,A as Bt,h as g,f as a,c as o,j as At,u,x as T,k as Wt,y as zt,a as l,v as h,d as y,t as M,w}from"../chunks/index.fe795e71.js";import{T as st}from"../chunks/Tip.179eb360.js";import{C}from"../chunks/CodeBlock.204b6c34.js";import{H as k,E as xt}from"../chunks/getInferenceSnippets.2234a8dd.js";import{H as Ft,a as Xt}from"../chunks/HfOption.3c290b0f.js";function Qt(J){let r,b="Accelerate only prepares objects that inherit from their respective PyTorch classes such as <code>torch.optim.Optimizer</code>.";return{c(){r=$("p"),r.innerHTML=b},l(n){r=g(n,"P",{"data-svelte-h":!0}),T(r)!=="svelte-1oh2ah6"&&(r.innerHTML=b)},m(n,m){l(n,r,m)},p:$e,d(n){n&&a(r)}}}function St(J){let r,b='Accelerate enables automatic mixed precision, so <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.autocast">autocast()</a> is only needed if there are other mixed precision operations besides those performed on loss by <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.backward">backward()</a> which already handles the scaling.';return{c(){r=$("p"),r.innerHTML=b},l(n){r=g(n,"P",{"data-svelte-h":!0}),T(r)!=="svelte-108vbdz"&&(r.innerHTML=b)},m(n,m){l(n,r,m)},p:$e,d(n){n&&a(r)}}}function Nt(J){let r,b='For models from the <a href="https://hf.co/docs/transformers/index" rel="nofollow">Transformers</a> library, save the model with the <code>save_pretrained</code> method so that it can be reloaded with the <code>from_pretrained</code> method.',n,m,i;return m=new C({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbCUwQSUwQXVud3JhcHBlZF9tb2RlbCUyMCUzRCUyMGFjY2VsZXJhdG9yLnVud3JhcF9tb2RlbChtb2RlbCklMEF1bndyYXBwZWRfbW9kZWwuc2F2ZV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnBhdGglMkZ0byUyRm15X21vZGVsX2RpcmVjdG9yeSUyMiUyQyUwQSUyMCUyMCUyMCUyMGlzX21haW5fcHJvY2VzcyUzRGFjY2VsZXJhdG9yLmlzX21haW5fcHJvY2VzcyUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfZnVuY3Rpb24lM0RhY2NlbGVyYXRvci5zYXZlJTJDJTBBKSUwQSUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsLmZyb21fcHJldHJhaW5lZCglMjJwYXRoJTJGdG8lMkZteV9tb2RlbF9kaXJlY3RvcnklMjIp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModel

unwrapped_model = accelerator.unwrap_model(model)
unwrapped_model.save_pretrained(
    <span class="hljs-string">&quot;path/to/my_model_directory&quot;</span>,
    is_main_process=accelerator.is_main_process,
    save_function=accelerator.save,
)

model = AutoModel.from_pretrained(<span class="hljs-string">&quot;path/to/my_model_directory&quot;</span>)`,wrap:!1}}),{c(){r=$("p"),r.innerHTML=b,n=c(),f(m.$$.fragment)},l(d){r=g(d,"P",{"data-svelte-h":!0}),T(r)!=="svelte-1uyjbz2"&&(r.innerHTML=b),n=o(d),u(m.$$.fragment,d)},m(d,v){l(d,r,v),l(d,n,v),h(m,d,v),i=!0},p:$e,i(d){i||(y(m.$$.fragment,d),i=!0)},o(d){M(m.$$.fragment,d),i=!1},d(d){d&&(a(r),a(n)),w(m,d)}}}function Lt(J){let r,b,n,m,i,d='To load your weights, use the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.unwrap_model">unwrap_model()</a> method to unwrap the model first before loading the weights. All model parameters are references to tensors, so this loads your weights inside <code>model</code>.',v,_,U;return r=new C({props:{code:"YWNjZWxlcmF0b3Iud2FpdF9mb3JfZXZlcnlvbmUoKSUwQWFjY2VsZXJhdG9yLnNhdmVfbW9kZWwobW9kZWwlMkMlMjBzYXZlX2RpcmVjdG9yeSk=",highlighted:`accelerator.wait_for_everyone()
accelerator.save_model(model, save_directory)`,wrap:!1}}),n=new st({props:{$$slots:{default:[Nt]},$$scope:{ctx:J}}}),_=new C({props:{code:"dW53cmFwcGVkX21vZGVsJTIwJTNEJTIwYWNjZWxlcmF0b3IudW53cmFwX21vZGVsKG1vZGVsKSUwQXBhdGhfdG9fY2hlY2twb2ludCUyMCUzRCUyMG9zLnBhdGguam9pbihzYXZlX2RpcmVjdG9yeSUyQyUyMnB5dG9yY2hfbW9kZWwuYmluJTIyKSUwQXVud3JhcHBlZF9tb2RlbC5sb2FkX3N0YXRlX2RpY3QodG9yY2gubG9hZChwYXRoX3RvX2NoZWNrcG9pbnQpKQ==",highlighted:`unwrapped_model = accelerator.unwrap_model(model)
path_to_checkpoint = os.path.join(save_directory,<span class="hljs-string">&quot;pytorch_model.bin&quot;</span>)
unwrapped_model.load_state_dict(torch.load(path_to_checkpoint))`,wrap:!1}}),{c(){f(r.$$.fragment),b=c(),f(n.$$.fragment),m=c(),i=$("p"),i.innerHTML=d,v=c(),f(_.$$.fragment)},l(s){u(r.$$.fragment,s),b=o(s),u(n.$$.fragment,s),m=o(s),i=g(s,"P",{"data-svelte-h":!0}),T(i)!=="svelte-67migr"&&(i.innerHTML=d),v=o(s),u(_.$$.fragment,s)},m(s,p){h(r,s,p),l(s,b,p),h(n,s,p),l(s,m,p),l(s,i,p),l(s,v,p),h(_,s,p),U=!0},p(s,p){const j={};p&2&&(j.$$scope={dirty:p,ctx:s}),n.$set(j)},i(s){U||(y(r.$$.fragment,s),y(n.$$.fragment,s),y(_.$$.fragment,s),U=!0)},o(s){M(r.$$.fragment,s),M(n.$$.fragment,s),M(_.$$.fragment,s),U=!1},d(s){s&&(a(b),a(m),a(i),a(v)),w(r,s),w(n,s),w(_,s)}}}function Et(J){let r,b="Set <code>safe_serialization=True</code> to save the model in the safetensor format.",n,m,i,d,v='To load a sharded checkpoint or a safetensor formatted checkpoint, use the <a href="/docs/accelerate/v1.10.1/en/package_reference/big_modeling#accelerate.load_checkpoint_in_model">load_checkpoint_in_model()</a> method. This method allows you to load a checkpoint onto a specific device.',_,U,s;return m=new C({props:{code:"YWNjZWxlcmF0b3Iud2FpdF9mb3JfZXZlcnlvbmUoKSUwQWFjY2VsZXJhdG9yLnNhdmVfbW9kZWwobW9kZWwlMkMlMjBzYXZlX2RpcmVjdG9yeSUyQyUyMG1heF9zaGFyZF9zaXplJTNEJTIyMUdCJTIyJTJDJTIwc2FmZV9zZXJpYWxpemF0aW9uJTNEVHJ1ZSk=",highlighted:`accelerator.wait_for_everyone()
accelerator.save_model(model, save_directory, max_shard_size=<span class="hljs-string">&quot;1GB&quot;</span>, safe_serialization=<span class="hljs-literal">True</span>)`,wrap:!1}}),U=new C({props:{code:"bG9hZF9jaGVja3BvaW50X2luX21vZGVsKHVud3JhcHBlZF9tb2RlbCUyQyUyMHNhdmVfZGlyZWN0b3J5JTJDJTIwZGV2aWNlX21hcCUzRCU3QiUyMiUyMiUzQWRldmljZSU3RCk=",highlighted:'load_checkpoint_in_model(unwrapped_model, save_directory, device_map={<span class="hljs-string">&quot;&quot;</span>:device})',wrap:!1}}),{c(){r=$("p"),r.innerHTML=b,n=c(),f(m.$$.fragment),i=c(),d=$("p"),d.innerHTML=v,_=c(),f(U.$$.fragment)},l(p){r=g(p,"P",{"data-svelte-h":!0}),T(r)!=="svelte-jahj75"&&(r.innerHTML=b),n=o(p),u(m.$$.fragment,p),i=o(p),d=g(p,"P",{"data-svelte-h":!0}),T(d)!=="svelte-blvb8z"&&(d.innerHTML=v),_=o(p),u(U.$$.fragment,p)},m(p,j){l(p,r,j),l(p,n,j),h(m,p,j),l(p,i,j),l(p,d,j),l(p,_,j),h(U,p,j),s=!0},p:$e,i(p){s||(y(m.$$.fragment,p),y(U.$$.fragment,p),s=!0)},o(p){M(m.$$.fragment,p),M(U.$$.fragment,p),s=!1},d(p){p&&(a(r),a(n),a(i),a(d),a(_)),w(m,p),w(U,p)}}}function Pt(J){let r,b,n,m;return r=new Xt({props:{id:"save",option:"single checkpoint",$$slots:{default:[Lt]},$$scope:{ctx:J}}}),n=new Xt({props:{id:"save",option:"sharded checkpoint",$$slots:{default:[Et]},$$scope:{ctx:J}}}),{c(){f(r.$$.fragment),b=c(),f(n.$$.fragment)},l(i){u(r.$$.fragment,i),b=o(i),u(n.$$.fragment,i)},m(i,d){h(r,i,d),l(i,b,d),h(n,i,d),m=!0},p(i,d){const v={};d&2&&(v.$$scope={dirty:d,ctx:i}),r.$set(v);const _={};d&2&&(_.$$scope={dirty:d,ctx:i}),n.$set(_)},i(i){m||(y(r.$$.fragment,i),y(n.$$.fragment,i),m=!0)},o(i){M(r.$$.fragment,i),M(n.$$.fragment,i),m=!1},d(i){i&&a(b),w(r,i),w(n,i)}}}function Kt(J){let r,b='If you have <a href="https://github.com/pytorch/data/tree/main" rel="nofollow"><code>torchdata&gt;=0.8.0</code></a> installed, you can additionally pass <code>use_stateful_dataloader=True</code> into your <a href="/docs/accelerate/v1.10.1/en/package_reference/utilities#accelerate.DataLoaderConfiguration">DataLoaderConfiguration</a>. This extends Accelerate’s DataLoader classes with a <code>load_state_dict</code> and <code>state_dict</code> function, and makes it so <code>Accelerator.save_state</code> and <code>Accelerator.load_state</code> also track how far into the training dataset it has read when persisting the model.';return{c(){r=$("p"),r.innerHTML=b},l(n){r=g(n,"P",{"data-svelte-h":!0}),T(r)!=="svelte-1s6bre4"&&(r.innerHTML=b)},m(n,m){l(n,r,m)},p:$e,d(n){n&&a(r)}}}function qt(J){let r,b,n,m,i,d,v,_="Each distributed training framework has their own way of doing things which can require writing a lot of custom code to adapt it to your PyTorch training code and training environment. Accelerate offers a friendly way to interface with these distributed training frameworks without having to learn the specific details of each one. Accelerate takes care of those details for you, so you can focus on the training code and scale it to any distributed training environment.",U,s,p="In this tutorial, you’ll learn how to adapt your existing PyTorch code with Accelerate and get you on your way toward training on distributed systems with ease! You’ll start with a basic PyTorch training loop (it assumes all the training objects like <code>model</code> and <code>optimizer</code> have been setup already) and progressively integrate Accelerate into it.",j,W,ge,X,be,R,dt='The <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a> is the main class for adapting your code to work with Accelerate. It knows about the distributed setup you’re using such as the number of different processes and your hardware type. This class also provides access to many of the necessary methods for enabling your PyTorch code to work in any distributed training environment and for managing and executing processes across devices.',Te,H,pt='That’s why you should always start by importing and creating an <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a> instance in your script.',ve,Y,_e,V,mt='The <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a> also knows which device to move your PyTorch objects to, so it is recommended to let Accelerate handle this for you.',Ue,B,Je,z,je,x,ft='Next, you need to prepare your PyTorch objects (model, optimizer, scheduler, etc.) for distributed training. The <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.prepare">prepare()</a> method takes care of placing your model in the appropriate container (like single GPU or multi-GPU) for your training setup, adapting the optimizer and scheduler to use Accelerate’s <a href="/docs/accelerate/v1.10.1/en/package_reference/torch_wrappers#accelerate.optimizer.AcceleratedOptimizer">AcceleratedOptimizer</a> and <a href="/docs/accelerate/v1.10.1/en/package_reference/torch_wrappers#accelerate.scheduler.AcceleratedScheduler">AcceleratedScheduler</a>, and creating a new dataloader that can be sharded across processes.',Ce,G,ke,F,ut="The PyTorch objects are returned in the same order they’re sent.",Ge,Q,Ze,S,Ie,N,ht='Finally, remove the <code>to(device)</code> calls to the inputs and targets in the training loop because Accelerate’s DataLoader classes automatically places them on the right device. You should also replace the usual <code>backward()</code> pass with Accelerate’s <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.backward">backward()</a> method which scales the gradients for you and uses the appropriate <code>backward()</code> method depending on your distributed setup (for example, DeepSpeed or Megatron).',Ae,L,We,E,yt="Put everything together and your new Accelerate training loop should now look like this!",Xe,P,Re,K,He,q,Mt="Accelerate offers additional features - like gradient accumulation, gradient clipping, mixed precision training and more - you can add to your script to improve your training run. Let’s explore these three features.",Ye,D,Ve,O,wt='Gradient accumulation enables you to train on larger batch sizes by accumulating the gradients over multiple batches before updating the weights. This can be useful for getting around memory limitations. To enable this feature in Accelerate, specify the <code>gradient_accumulation_steps</code> parameter in the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a> class and add the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.accumulate">accumulate()</a> context manager to your script.',Be,ee,ze,te,xe,ae,$t="Gradient clipping is a technique to prevent “exploding gradients”, and Accelerate offers:",Fe,le,gt='<li><a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.clip_grad_value_">clip<em>grad_value</em>()</a> to clip gradients to a minimum and maximum value</li> <li><a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.clip_grad_norm_">clip<em>grad_norm</em>()</a> for normalizing gradients to a certain value</li>',Qe,re,Se,ce,bt="Mixed precision accelerates training by using a lower precision data type like fp16 (half-precision) to calculate the gradients. For the best performance with Accelerate, the loss should be computed inside your model (like in Transformers models) because computations outside of the model are computed in full precision.",Ne,oe,Tt='Set the mixed precision type to use in the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a>, and then use the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.autocast">autocast()</a> context manager to automatically cast the values to the specified data type.',Le,Z,Ee,ne,Pe,ie,Ke,se,vt="Accelerate can also save and load a <em>model</em> once training is complete or you can also save the model and optimizer <em>state</em> which could be useful for resuming training.",qe,de,De,pe,_t='Once all processes are complete, unwrap the model with the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.unwrap_model">unwrap_model()</a> method before saving it because the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.prepare">prepare()</a> method wrapped your model into the proper interface for distributed training. If you don’t unwrap the model, saving the model state dictionary also saves any potential extra layers from the larger model and you won’t be able to load the weights back into your base model.',Oe,me,Ut='You should use the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.save_model">save_model()</a> method to unwrap and save the model state dictionary. This method can also save a model into sharded checkpoints or into the <a href="https://hf.co/docs/safetensors/index" rel="nofollow">safetensors</a> format.',et,I,tt,fe,at,ue,Jt='During training, you may want to save the current state of the model, optimizer, random generators, and potentially learning rate schedulers so they can be restored in the <em>same script</em>. You should add the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.save_state">save_state()</a> and <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.load_state">load_state()</a> methods to your script to save and load states.',lt,he,jt='To further customize where and how states are saved through <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.save_state">save_state()</a>, use the <a href="/docs/accelerate/v1.10.1/en/package_reference/utilities#accelerate.utils.ProjectConfiguration">ProjectConfiguration</a> class. For example, if <code>automatic_checkpoint_naming</code> is enabled, each saved checkpoint is stored at <code>Accelerator.project_dir/checkpoints/checkpoint_{checkpoint_number}</code>.',rt,ye,Ct='Any other stateful items to be stored should be registered with the <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.register_for_checkpointing">register_for_checkpointing()</a> method so they can be saved and loaded. Every object passed to this method to be stored must have a <code>load_state_dict</code> and <code>state_dict</code> function.',ct,A,ot,Me,nt,we,it;return i=new k({props:{title:"Add Accelerate to your code",local:"add-accelerate-to-your-code",headingTag:"h1"}}),W=new C({props:{code:"ZGV2aWNlJTIwJTNEJTIwJTIyY3VkYSUyMiUwQW1vZGVsLnRvKGRldmljZSklMEElMEFmb3IlMjBiYXRjaCUyMGluJTIwdHJhaW5pbmdfZGF0YWxvYWRlciUzQSUwQSUyMCUyMCUyMCUyMG9wdGltaXplci56ZXJvX2dyYWQoKSUwQSUyMCUyMCUyMCUyMGlucHV0cyUyQyUyMHRhcmdldHMlMjAlM0QlMjBiYXRjaCUwQSUyMCUyMCUyMCUyMGlucHV0cyUyMCUzRCUyMGlucHV0cy50byhkZXZpY2UpJTBBJTIwJTIwJTIwJTIwdGFyZ2V0cyUyMCUzRCUyMHRhcmdldHMudG8oZGV2aWNlKSUwQSUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBtb2RlbChpbnB1dHMpJTBBJTIwJTIwJTIwJTIwbG9zcyUyMCUzRCUyMGxvc3NfZnVuY3Rpb24ob3V0cHV0cyUyQyUyMHRhcmdldHMpJTBBJTIwJTIwJTIwJTIwbG9zcy5iYWNrd2FyZCgpJTBBJTIwJTIwJTIwJTIwb3B0aW1pemVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMHNjaGVkdWxlci5zdGVwKCk=",highlighted:`device = <span class="hljs-string">&quot;cuda&quot;</span>
model.to(device)

<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> training_dataloader:
    optimizer.zero_grad()
    inputs, targets = batch
    inputs = inputs.to(device)
    targets = targets.to(device)
    outputs = model(inputs)
    loss = loss_function(outputs, targets)
    loss.backward()
    optimizer.step()
    scheduler.step()`,wrap:!1}}),X=new k({props:{title:"Accelerator",local:"accelerator",headingTag:"h2"}}),Y=new C({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator

accelerator = Accelerator()`,wrap:!1}}),B=new C({props:{code:"LSUyMGRldmljZSUyMCUzRCUyMCUyMmN1ZGElMjIlMEElMkIlMjBkZXZpY2UlMjAlM0QlMjBhY2NlbGVyYXRvci5kZXZpY2UlMEElMjAlMjBtb2RlbC50byhkZXZpY2Up",highlighted:`<span class="hljs-deletion">- device = &quot;cuda&quot;</span>
<span class="hljs-addition">+ device = accelerator.device</span>
  model.to(device)`,wrap:!1}}),z=new k({props:{title:"Prepare PyTorch objects",local:"prepare-pytorch-objects",headingTag:"h2"}}),G=new st({props:{warning:!1,$$slots:{default:[Qt]},$$scope:{ctx:J}}}),Q=new C({props:{code:"bW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbmluZ19kYXRhbG9hZGVyJTJDJTIwc2NoZWR1bGVyJTIwJTNEJTIwYWNjZWxlcmF0b3IucHJlcGFyZSglMEElMjAlMjAlMjAlMjBtb2RlbCUyQyUyMG9wdGltaXplciUyQyUyMHRyYWluaW5nX2RhdGFsb2FkZXIlMkMlMjBzY2hlZHVsZXIlMEEp",highlighted:`model, optimizer, training_dataloader, scheduler = accelerator.prepare(
    model, optimizer, training_dataloader, scheduler
)`,wrap:!1}}),S=new k({props:{title:"Training loop",local:"training-loop",headingTag:"h2"}}),L=new C({props:{code:"LSUyMCUyMCUyMGlucHV0cyUyMCUzRCUyMGlucHV0cy50byhkZXZpY2UpJTBBLSUyMCUyMCUyMHRhcmdldHMlMjAlM0QlMjB0YXJnZXRzLnRvKGRldmljZSklMEElMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoaW5wdXRzKSUwQSUyMCUyMCUyMCUyMGxvc3MlMjAlM0QlMjBsb3NzX2Z1bmN0aW9uKG91dHB1dHMlMkMlMjB0YXJnZXRzKSUwQS0lMjAlMjAlMjBsb3NzLmJhY2t3YXJkKCklMEElMkIlMjAlMjAlMjBhY2NlbGVyYXRvci5iYWNrd2FyZChsb3NzKQ==",highlighted:`<span class="hljs-deletion">-   inputs = inputs.to(device)</span>
<span class="hljs-deletion">-   targets = targets.to(device)</span>
    outputs = model(inputs)
    loss = loss_function(outputs, targets)
<span class="hljs-deletion">-   loss.backward()</span>
<span class="hljs-addition">+   accelerator.backward(loss)</span>`,wrap:!1}}),P=new C({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoKSUwQSUwQWRldmljZSUyMCUzRCUyMGFjY2VsZXJhdG9yLmRldmljZSUwQW1vZGVsJTJDJTIwb3B0aW1pemVyJTJDJTIwdHJhaW5pbmdfZGF0YWxvYWRlciUyQyUyMHNjaGVkdWxlciUyMCUzRCUyMGFjY2VsZXJhdG9yLnByZXBhcmUoJTBBJTIwJTIwJTIwJTIwbW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbmluZ19kYXRhbG9hZGVyJTJDJTIwc2NoZWR1bGVyJTBBKSUwQSUwQWZvciUyMGJhdGNoJTIwaW4lMjB0cmFpbmluZ19kYXRhbG9hZGVyJTNBJTBBJTIwJTIwJTIwJTIwb3B0aW1pemVyLnplcm9fZ3JhZCgpJTBBJTIwJTIwJTIwJTIwaW5wdXRzJTJDJTIwdGFyZ2V0cyUyMCUzRCUyMGJhdGNoJTBBJTIwJTIwJTIwJTIwb3V0cHV0cyUyMCUzRCUyMG1vZGVsKGlucHV0cyklMEElMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwbG9zc19mdW5jdGlvbihvdXRwdXRzJTJDJTIwdGFyZ2V0cyklMEElMjAlMjAlMjAlMjBhY2NlbGVyYXRvci5iYWNrd2FyZChsb3NzKSUwQSUyMCUyMCUyMCUyMG9wdGltaXplci5zdGVwKCklMEElMjAlMjAlMjAlMjBzY2hlZHVsZXIuc3RlcCgp",highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator
accelerator = Accelerator()

device = accelerator.device
model, optimizer, training_dataloader, scheduler = accelerator.prepare(
    model, optimizer, training_dataloader, scheduler
)

<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> training_dataloader:
    optimizer.zero_grad()
    inputs, targets = batch
    outputs = model(inputs)
    loss = loss_function(outputs, targets)
    accelerator.backward(loss)
    optimizer.step()
    scheduler.step()`,wrap:!1}}),K=new k({props:{title:"Training features",local:"training-features",headingTag:"h2"}}),D=new k({props:{title:"Gradient accumulation",local:"gradient-accumulation",headingTag:"h3"}}),ee=new C({props:{code:"JTJCJTIwYWNjZWxlcmF0b3IlMjAlM0QlMjBBY2NlbGVyYXRvcihncmFkaWVudF9hY2N1bXVsYXRpb25fc3RlcHMlM0QyKSUwQSUyMCUyMG1vZGVsJTJDJTIwb3B0aW1pemVyJTJDJTIwdHJhaW5pbmdfZGF0YWxvYWRlciUyMCUzRCUyMGFjY2VsZXJhdG9yLnByZXBhcmUobW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbmluZ19kYXRhbG9hZGVyKSUwQSUwQSUyMCUyMGZvciUyMGlucHV0JTJDJTIwbGFiZWwlMjBpbiUyMHRyYWluaW5nX2RhdGFsb2FkZXIlM0ElMEElMkIlMjAlMjAlMjAlMjAlMjB3aXRoJTIwYWNjZWxlcmF0b3IuYWNjdW11bGF0ZShtb2RlbCklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcmVkaWN0aW9ucyUyMCUzRCUyMG1vZGVsKGlucHV0KSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxvc3MlMjAlM0QlMjBsb3NzX2Z1bmN0aW9uKHByZWRpY3Rpb25zJTJDJTIwbGFiZWwpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IuYmFja3dhcmQobG9zcyklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuc3RlcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2NoZWR1bGVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG9wdGltaXplci56ZXJvX2dyYWQoKQ==",highlighted:`<span class="hljs-addition">+ accelerator = Accelerator(gradient_accumulation_steps=2)</span>
  model, optimizer, training_dataloader = accelerator.prepare(model, optimizer, training_dataloader)

  for input, label in training_dataloader:
<span class="hljs-addition">+     with accelerator.accumulate(model):</span>
          predictions = model(input)
          loss = loss_function(predictions, label)
          accelerator.backward(loss)
          optimizer.step()
          scheduler.step()
          optimizer.zero_grad()`,wrap:!1}}),te=new k({props:{title:"Gradient clipping",local:"gradient-clipping",headingTag:"h3"}}),re=new k({props:{title:"Mixed precision",local:"mixed-precision",headingTag:"h3"}}),Z=new st({props:{warning:!0,$$slots:{default:[St]},$$scope:{ctx:J}}}),ne=new C({props:{code:"JTJCJTIwYWNjZWxlcmF0b3IlMjAlM0QlMjBBY2NlbGVyYXRvcihtaXhlZF9wcmVjaXNpb24lM0QlMjJmcDE2JTIyKSUwQSUyQiUyMHdpdGglMjBhY2NlbGVyYXRvci5hdXRvY2FzdCgpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwbG9zcyUyMCUzRCUyMGNvbXBsZXhfbG9zc19mdW5jdGlvbihvdXRwdXRzJTJDJTIwdGFyZ2V0KQ==",highlighted:`<span class="hljs-addition">+ accelerator = Accelerator(mixed_precision=&quot;fp16&quot;)</span>
<span class="hljs-addition">+ with accelerator.autocast():</span>
      loss = complex_loss_function(outputs, target)`,wrap:!1}}),ie=new k({props:{title:"Save and load",local:"save-and-load",headingTag:"h2"}}),de=new k({props:{title:"Model",local:"model",headingTag:"h3"}}),I=new Ft({props:{id:"save",options:["single checkpoint","sharded checkpoint"],$$slots:{default:[Pt]},$$scope:{ctx:J}}}),fe=new k({props:{title:"State",local:"state",headingTag:"h3"}}),A=new st({props:{warning:!1,$$slots:{default:[Kt]},$$scope:{ctx:J}}}),Me=new xt({props:{source:"https://github.com/huggingface/accelerate/blob/main/docs/source/basic_tutorials/migration.md"}}),{c(){r=$("meta"),b=c(),n=$("p"),m=c(),f(i.$$.fragment),d=c(),v=$("p"),v.textContent=_,U=c(),s=$("p"),s.innerHTML=p,j=c(),f(W.$$.fragment),ge=c(),f(X.$$.fragment),be=c(),R=$("p"),R.innerHTML=dt,Te=c(),H=$("p"),H.innerHTML=pt,ve=c(),f(Y.$$.fragment),_e=c(),V=$("p"),V.innerHTML=mt,Ue=c(),f(B.$$.fragment),Je=c(),f(z.$$.fragment),je=c(),x=$("p"),x.innerHTML=ft,Ce=c(),f(G.$$.fragment),ke=c(),F=$("p"),F.textContent=ut,Ge=c(),f(Q.$$.fragment),Ze=c(),f(S.$$.fragment),Ie=c(),N=$("p"),N.innerHTML=ht,Ae=c(),f(L.$$.fragment),We=c(),E=$("p"),E.textContent=yt,Xe=c(),f(P.$$.fragment),Re=c(),f(K.$$.fragment),He=c(),q=$("p"),q.textContent=Mt,Ye=c(),f(D.$$.fragment),Ve=c(),O=$("p"),O.innerHTML=wt,Be=c(),f(ee.$$.fragment),ze=c(),f(te.$$.fragment),xe=c(),ae=$("p"),ae.textContent=$t,Fe=c(),le=$("ul"),le.innerHTML=gt,Qe=c(),f(re.$$.fragment),Se=c(),ce=$("p"),ce.textContent=bt,Ne=c(),oe=$("p"),oe.innerHTML=Tt,Le=c(),f(Z.$$.fragment),Ee=c(),f(ne.$$.fragment),Pe=c(),f(ie.$$.fragment),Ke=c(),se=$("p"),se.innerHTML=vt,qe=c(),f(de.$$.fragment),De=c(),pe=$("p"),pe.innerHTML=_t,Oe=c(),me=$("p"),me.innerHTML=Ut,et=c(),f(I.$$.fragment),tt=c(),f(fe.$$.fragment),at=c(),ue=$("p"),ue.innerHTML=Jt,lt=c(),he=$("p"),he.innerHTML=jt,rt=c(),ye=$("p"),ye.innerHTML=Ct,ct=c(),f(A.$$.fragment),ot=c(),f(Me.$$.fragment),nt=c(),we=$("p"),this.h()},l(e){const t=Bt("svelte-u9bgzb",document.head);r=g(t,"META",{name:!0,content:!0}),t.forEach(a),b=o(e),n=g(e,"P",{}),At(n).forEach(a),m=o(e),u(i.$$.fragment,e),d=o(e),v=g(e,"P",{"data-svelte-h":!0}),T(v)!=="svelte-19zbp5z"&&(v.textContent=_),U=o(e),s=g(e,"P",{"data-svelte-h":!0}),T(s)!=="svelte-1lmjen8"&&(s.innerHTML=p),j=o(e),u(W.$$.fragment,e),ge=o(e),u(X.$$.fragment,e),be=o(e),R=g(e,"P",{"data-svelte-h":!0}),T(R)!=="svelte-1ny3jio"&&(R.innerHTML=dt),Te=o(e),H=g(e,"P",{"data-svelte-h":!0}),T(H)!=="svelte-w6842p"&&(H.innerHTML=pt),ve=o(e),u(Y.$$.fragment,e),_e=o(e),V=g(e,"P",{"data-svelte-h":!0}),T(V)!=="svelte-dnh6ed"&&(V.innerHTML=mt),Ue=o(e),u(B.$$.fragment,e),Je=o(e),u(z.$$.fragment,e),je=o(e),x=g(e,"P",{"data-svelte-h":!0}),T(x)!=="svelte-10r1kdn"&&(x.innerHTML=ft),Ce=o(e),u(G.$$.fragment,e),ke=o(e),F=g(e,"P",{"data-svelte-h":!0}),T(F)!=="svelte-l5v2wx"&&(F.textContent=ut),Ge=o(e),u(Q.$$.fragment,e),Ze=o(e),u(S.$$.fragment,e),Ie=o(e),N=g(e,"P",{"data-svelte-h":!0}),T(N)!=="svelte-1jtginz"&&(N.innerHTML=ht),Ae=o(e),u(L.$$.fragment,e),We=o(e),E=g(e,"P",{"data-svelte-h":!0}),T(E)!=="svelte-1b6tm11"&&(E.textContent=yt),Xe=o(e),u(P.$$.fragment,e),Re=o(e),u(K.$$.fragment,e),He=o(e),q=g(e,"P",{"data-svelte-h":!0}),T(q)!=="svelte-1ef6475"&&(q.textContent=Mt),Ye=o(e),u(D.$$.fragment,e),Ve=o(e),O=g(e,"P",{"data-svelte-h":!0}),T(O)!=="svelte-1bdygfx"&&(O.innerHTML=wt),Be=o(e),u(ee.$$.fragment,e),ze=o(e),u(te.$$.fragment,e),xe=o(e),ae=g(e,"P",{"data-svelte-h":!0}),T(ae)!=="svelte-bg95gl"&&(ae.textContent=$t),Fe=o(e),le=g(e,"UL",{"data-svelte-h":!0}),T(le)!=="svelte-1ex1elp"&&(le.innerHTML=gt),Qe=o(e),u(re.$$.fragment,e),Se=o(e),ce=g(e,"P",{"data-svelte-h":!0}),T(ce)!=="svelte-81s14c"&&(ce.textContent=bt),Ne=o(e),oe=g(e,"P",{"data-svelte-h":!0}),T(oe)!=="svelte-1ufrago"&&(oe.innerHTML=Tt),Le=o(e),u(Z.$$.fragment,e),Ee=o(e),u(ne.$$.fragment,e),Pe=o(e),u(ie.$$.fragment,e),Ke=o(e),se=g(e,"P",{"data-svelte-h":!0}),T(se)!=="svelte-dqdivd"&&(se.innerHTML=vt),qe=o(e),u(de.$$.fragment,e),De=o(e),pe=g(e,"P",{"data-svelte-h":!0}),T(pe)!=="svelte-qsv94x"&&(pe.innerHTML=_t),Oe=o(e),me=g(e,"P",{"data-svelte-h":!0}),T(me)!=="svelte-1wzau76"&&(me.innerHTML=Ut),et=o(e),u(I.$$.fragment,e),tt=o(e),u(fe.$$.fragment,e),at=o(e),ue=g(e,"P",{"data-svelte-h":!0}),T(ue)!=="svelte-4nk607"&&(ue.innerHTML=Jt),lt=o(e),he=g(e,"P",{"data-svelte-h":!0}),T(he)!=="svelte-1gqticu"&&(he.innerHTML=jt),rt=o(e),ye=g(e,"P",{"data-svelte-h":!0}),T(ye)!=="svelte-160x3a6"&&(ye.innerHTML=Ct),ct=o(e),u(A.$$.fragment,e),ot=o(e),u(Me.$$.fragment,e),nt=o(e),we=g(e,"P",{}),At(we).forEach(a),this.h()},h(){Wt(r,"name","hf:doc:metadata"),Wt(r,"content",Dt)},m(e,t){zt(document.head,r),l(e,b,t),l(e,n,t),l(e,m,t),h(i,e,t),l(e,d,t),l(e,v,t),l(e,U,t),l(e,s,t),l(e,j,t),h(W,e,t),l(e,ge,t),h(X,e,t),l(e,be,t),l(e,R,t),l(e,Te,t),l(e,H,t),l(e,ve,t),h(Y,e,t),l(e,_e,t),l(e,V,t),l(e,Ue,t),h(B,e,t),l(e,Je,t),h(z,e,t),l(e,je,t),l(e,x,t),l(e,Ce,t),h(G,e,t),l(e,ke,t),l(e,F,t),l(e,Ge,t),h(Q,e,t),l(e,Ze,t),h(S,e,t),l(e,Ie,t),l(e,N,t),l(e,Ae,t),h(L,e,t),l(e,We,t),l(e,E,t),l(e,Xe,t),h(P,e,t),l(e,Re,t),h(K,e,t),l(e,He,t),l(e,q,t),l(e,Ye,t),h(D,e,t),l(e,Ve,t),l(e,O,t),l(e,Be,t),h(ee,e,t),l(e,ze,t),h(te,e,t),l(e,xe,t),l(e,ae,t),l(e,Fe,t),l(e,le,t),l(e,Qe,t),h(re,e,t),l(e,Se,t),l(e,ce,t),l(e,Ne,t),l(e,oe,t),l(e,Le,t),h(Z,e,t),l(e,Ee,t),h(ne,e,t),l(e,Pe,t),h(ie,e,t),l(e,Ke,t),l(e,se,t),l(e,qe,t),h(de,e,t),l(e,De,t),l(e,pe,t),l(e,Oe,t),l(e,me,t),l(e,et,t),h(I,e,t),l(e,tt,t),h(fe,e,t),l(e,at,t),l(e,ue,t),l(e,lt,t),l(e,he,t),l(e,rt,t),l(e,ye,t),l(e,ct,t),h(A,e,t),l(e,ot,t),h(Me,e,t),l(e,nt,t),l(e,we,t),it=!0},p(e,[t]){const kt={};t&2&&(kt.$$scope={dirty:t,ctx:e}),G.$set(kt);const Gt={};t&2&&(Gt.$$scope={dirty:t,ctx:e}),Z.$set(Gt);const Zt={};t&2&&(Zt.$$scope={dirty:t,ctx:e}),I.$set(Zt);const It={};t&2&&(It.$$scope={dirty:t,ctx:e}),A.$set(It)},i(e){it||(y(i.$$.fragment,e),y(W.$$.fragment,e),y(X.$$.fragment,e),y(Y.$$.fragment,e),y(B.$$.fragment,e),y(z.$$.fragment,e),y(G.$$.fragment,e),y(Q.$$.fragment,e),y(S.$$.fragment,e),y(L.$$.fragment,e),y(P.$$.fragment,e),y(K.$$.fragment,e),y(D.$$.fragment,e),y(ee.$$.fragment,e),y(te.$$.fragment,e),y(re.$$.fragment,e),y(Z.$$.fragment,e),y(ne.$$.fragment,e),y(ie.$$.fragment,e),y(de.$$.fragment,e),y(I.$$.fragment,e),y(fe.$$.fragment,e),y(A.$$.fragment,e),y(Me.$$.fragment,e),it=!0)},o(e){M(i.$$.fragment,e),M(W.$$.fragment,e),M(X.$$.fragment,e),M(Y.$$.fragment,e),M(B.$$.fragment,e),M(z.$$.fragment,e),M(G.$$.fragment,e),M(Q.$$.fragment,e),M(S.$$.fragment,e),M(L.$$.fragment,e),M(P.$$.fragment,e),M(K.$$.fragment,e),M(D.$$.fragment,e),M(ee.$$.fragment,e),M(te.$$.fragment,e),M(re.$$.fragment,e),M(Z.$$.fragment,e),M(ne.$$.fragment,e),M(ie.$$.fragment,e),M(de.$$.fragment,e),M(I.$$.fragment,e),M(fe.$$.fragment,e),M(A.$$.fragment,e),M(Me.$$.fragment,e),it=!1},d(e){e&&(a(b),a(n),a(m),a(d),a(v),a(U),a(s),a(j),a(ge),a(be),a(R),a(Te),a(H),a(ve),a(_e),a(V),a(Ue),a(Je),a(je),a(x),a(Ce),a(ke),a(F),a(Ge),a(Ze),a(Ie),a(N),a(Ae),a(We),a(E),a(Xe),a(Re),a(He),a(q),a(Ye),a(Ve),a(O),a(Be),a(ze),a(xe),a(ae),a(Fe),a(le),a(Qe),a(Se),a(ce),a(Ne),a(oe),a(Le),a(Ee),a(Pe),a(Ke),a(se),a(qe),a(De),a(pe),a(Oe),a(me),a(et),a(tt),a(at),a(ue),a(lt),a(he),a(rt),a(ye),a(ct),a(ot),a(nt),a(we)),a(r),w(i,e),w(W,e),w(X,e),w(Y,e),w(B,e),w(z,e),w(G,e),w(Q,e),w(S,e),w(L,e),w(P,e),w(K,e),w(D,e),w(ee,e),w(te,e),w(re,e),w(Z,e),w(ne,e),w(ie,e),w(de,e),w(I,e),w(fe,e),w(A,e),w(Me,e)}}}const Dt='{"title":"Add Accelerate to your code","local":"add-accelerate-to-your-code","sections":[{"title":"Accelerator","local":"accelerator","sections":[],"depth":2},{"title":"Prepare PyTorch objects","local":"prepare-pytorch-objects","sections":[],"depth":2},{"title":"Training loop","local":"training-loop","sections":[],"depth":2},{"title":"Training features","local":"training-features","sections":[{"title":"Gradient accumulation","local":"gradient-accumulation","sections":[],"depth":3},{"title":"Gradient clipping","local":"gradient-clipping","sections":[],"depth":3},{"title":"Mixed precision","local":"mixed-precision","sections":[],"depth":3}],"depth":2},{"title":"Save and load","local":"save-and-load","sections":[{"title":"Model","local":"model","sections":[],"depth":3},{"title":"State","local":"state","sections":[],"depth":3}],"depth":2}],"depth":1}';function Ot(J){return Ht(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class oa extends Yt{constructor(r){super(),Vt(this,r,Ot,qt,Rt,{})}}export{oa as component};
