import{s as Bt,o as Ht,n as Rt}from"../chunks/scheduler.b9285784.js";import{S as Yt,i as Vt,e as m,s as r,c as h,h as xt,a as f,d as a,b as c,f as Wt,g as y,j as b,k as $e,l as zt,m as l,n as M,t as w,o as $,p as g}from"../chunks/index.26bc89a1.js";import{T as Qt}from"../chunks/Tip.e4eba3d6.js";import{C as Ft,H as k,E as Lt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.7242ff19.js";import{C as j}from"../chunks/CodeBlock.c8ee1711.js";import{H as St,a as Xt}from"../chunks/HfOption.76c7ca3e.js";function Nt(C){let i,U='For models from the <a href="https://hf.co/docs/transformers/index" rel="nofollow">Transformers</a> library, save the model with the <a href="https://huggingface.co/docs/transformers/main/en/main_classes/model#transformers.PreTrainedModel.save_pretrained" rel="nofollow">save_pretrained</a> method so that it can be reloaded with the <a href="https://huggingface.co/docs/transformers/main/en/main_classes/model#transformers.PreTrainedModel.from_pretrained" rel="nofollow">from_pretrained</a> method.',p,u,o;return u=new j({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(){i=m("p"),i.innerHTML=U,p=r(),h(u.$$.fragment)},l(s){i=f(s,"P",{"data-svelte-h":!0}),b(i)!=="svelte-y3y63b"&&(i.innerHTML=U),p=c(s),y(u.$$.fragment,s)},m(s,T){l(s,i,T),l(s,p,T),M(u,s,T),o=!0},p:Rt,i(s){o||(w(u.$$.fragment,s),o=!0)},o(s){$(u.$$.fragment,s),o=!1},d(s){s&&(a(i),a(p)),g(u,s)}}}function Et(C){let i,U,p,u,o,s='To load your weights, use the <a href="/docs/accelerate/main/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>.',T,v,_;return i=new j({props:{code:"YWNjZWxlcmF0b3Iud2FpdF9mb3JfZXZlcnlvbmUoKSUwQWFjY2VsZXJhdG9yLnNhdmVfbW9kZWwobW9kZWwlMkMlMjBzYXZlX2RpcmVjdG9yeSk=",highlighted:`accelerator.wait_for_everyone()
accelerator.save_model(model, save_directory)`,wrap:!1}}),p=new Qt({props:{$$slots:{default:[Nt]},$$scope:{ctx:C}}}),v=new j({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(){h(i.$$.fragment),U=r(),h(p.$$.fragment),u=r(),o=m("p"),o.innerHTML=s,T=r(),h(v.$$.fragment)},l(d){y(i.$$.fragment,d),U=c(d),y(p.$$.fragment,d),u=c(d),o=f(d,"P",{"data-svelte-h":!0}),b(o)!=="svelte-js2qkb"&&(o.innerHTML=s),T=c(d),y(v.$$.fragment,d)},m(d,n){M(i,d,n),l(d,U,n),M(p,d,n),l(d,u,n),l(d,o,n),l(d,T,n),M(v,d,n),_=!0},p(d,n){const J={};n&2&&(J.$$scope={dirty:n,ctx:d}),p.$set(J)},i(d){_||(w(i.$$.fragment,d),w(p.$$.fragment,d),w(v.$$.fragment,d),_=!0)},o(d){$(i.$$.fragment,d),$(p.$$.fragment,d),$(v.$$.fragment,d),_=!1},d(d){d&&(a(U),a(u),a(o),a(T)),g(i,d),g(p,d),g(v,d)}}}function Pt(C){let i,U="Set <code>safe_serialization=True</code> to save the model in the safetensor format.",p,u,o,s,T='To load a sharded checkpoint or a safetensor formatted checkpoint, use the <a href="/docs/accelerate/main/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.',v,_,d;return u=new j({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}}),_=new j({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(){i=m("p"),i.innerHTML=U,p=r(),h(u.$$.fragment),o=r(),s=m("p"),s.innerHTML=T,v=r(),h(_.$$.fragment)},l(n){i=f(n,"P",{"data-svelte-h":!0}),b(i)!=="svelte-jahj75"&&(i.innerHTML=U),p=c(n),y(u.$$.fragment,n),o=c(n),s=f(n,"P",{"data-svelte-h":!0}),b(s)!=="svelte-70qodr"&&(s.innerHTML=T),v=c(n),y(_.$$.fragment,n)},m(n,J){l(n,i,J),l(n,p,J),M(u,n,J),l(n,o,J),l(n,s,J),l(n,v,J),M(_,n,J),d=!0},p:Rt,i(n){d||(w(u.$$.fragment,n),w(_.$$.fragment,n),d=!0)},o(n){$(u.$$.fragment,n),$(_.$$.fragment,n),d=!1},d(n){n&&(a(i),a(p),a(o),a(s),a(v)),g(u,n),g(_,n)}}}function Kt(C){let i,U,p,u;return i=new Xt({props:{id:"save",option:"single checkpoint",$$slots:{default:[Et]},$$scope:{ctx:C}}}),p=new Xt({props:{id:"save",option:"sharded checkpoint",$$slots:{default:[Pt]},$$scope:{ctx:C}}}),{c(){h(i.$$.fragment),U=r(),h(p.$$.fragment)},l(o){y(i.$$.fragment,o),U=c(o),y(p.$$.fragment,o)},m(o,s){M(i,o,s),l(o,U,s),M(p,o,s),u=!0},p(o,s){const T={};s&2&&(T.$$scope={dirty:s,ctx:o}),i.$set(T);const v={};s&2&&(v.$$scope={dirty:s,ctx:o}),p.$set(v)},i(o){u||(w(i.$$.fragment,o),w(p.$$.fragment,o),u=!0)},o(o){$(i.$$.fragment,o),$(p.$$.fragment,o),u=!1},d(o){o&&a(U),g(i,o),g(p,o)}}}function qt(C){let i,U,p,u,o,s,T,v,_,d="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.",n,J,dt="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.",ge,W,be,X,Te,R,pt='The <a href="/docs/accelerate/main/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.',Ue,B,mt='That’s why you should always start by importing and creating an <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a> instance in your script.',ve,H,_e,Y,ft='The <a href="/docs/accelerate/main/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.',Je,V,je,x,ke,z,ut='Next, you need to prepare your PyTorch objects (model, optimizer, scheduler, etc.) for distributed training. The <a href="/docs/accelerate/main/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/main/en/package_reference/torch_wrappers#accelerate.optimizer.AcceleratedOptimizer">AcceleratedOptimizer</a> and <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.scheduler.AcceleratedScheduler">AcceleratedScheduler</a>, and creating a new dataloader that can be sharded across processes.',Ce,G,ht="<p>Accelerate only prepares objects that inherit from their respective PyTorch classes such as <code>torch.optim.Optimizer</code>.</p>",Ge,Q,yt="The PyTorch objects are returned in the same order they’re sent.",Ze,F,Ie,L,Ae,S,Mt='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/main/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).',We,N,Xe,E,wt="Put everything together and your new Accelerate training loop should now look like this!",Re,P,Be,K,He,q,$t="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,gt='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/main/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a> class and add the <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.accumulate">accumulate()</a> context manager to your script.',xe,ee,ze,te,Qe,ae,bt="Gradient clipping is a technique to prevent “exploding gradients”, and Accelerate offers:",Fe,le,Tt='<li><a href="/docs/accelerate/main/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/main/en/package_reference/accelerator#accelerate.Accelerator.clip_grad_norm_">clip<em>grad_norm</em>()</a> for normalizing gradients to a certain value</li>',Le,re,Se,ce,Ut="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,vt='Set the mixed precision type to use in the <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a>, and then use the <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.autocast">autocast()</a> context manager to automatically cast the values to the specified data type.',Ee,Z,_t='<p>Accelerate enables automatic mixed precision, so <a href="/docs/accelerate/main/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/main/en/package_reference/accelerator#accelerate.Accelerator.backward">backward()</a> which already handles the scaling.</p>',Pe,ie,Ke,ne,qe,se,Jt="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.",De,de,Oe,pe,jt='Once all processes are complete, unwrap the model with the <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.unwrap_model">unwrap_model()</a> method before saving it because the <a href="/docs/accelerate/main/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.',et,me,kt='You should use the <a href="/docs/accelerate/main/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.',tt,I,at,fe,lt,ue,Ct='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/main/en/package_reference/accelerator#accelerate.Accelerator.save_state">save_state()</a> and <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.load_state">load_state()</a> methods to your script to save and load states.',rt,he,Gt='To further customize where and how states are saved through <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.save_state">save_state()</a>, use the <a href="/docs/accelerate/main/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>.',ct,ye,Zt='Any other stateful items to be stored should be registered with the <a href="/docs/accelerate/main/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.',ot,A,It='<p>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/main/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.</p>',it,Me,nt,we,st;return o=new Ft({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),T=new k({props:{title:"Add Accelerate to your code",local:"add-accelerate-to-your-code",headingTag:"h1"}}),W=new j({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"}}),H=new j({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator

accelerator = Accelerator()`,wrap:!1}}),V=new j({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}}),x=new k({props:{title:"Prepare PyTorch objects",local:"prepare-pytorch-objects",headingTag:"h2"}}),F=new j({props:{code:"bW9kZWwlMkMlMjBvcHRpbWl6ZXIlMkMlMjB0cmFpbmluZ19kYXRhbG9hZGVyJTJDJTIwc2NoZWR1bGVyJTIwJTNEJTIwYWNjZWxlcmF0b3IucHJlcGFyZSglMEElMjAlMjAlMjAlMjBtb2RlbCUyQyUyMG9wdGltaXplciUyQyUyMHRyYWluaW5nX2RhdGFsb2FkZXIlMkMlMjBzY2hlZHVsZXIlMEEp",highlighted:`model, optimizer, training_dataloader, scheduler = accelerator.prepare(
    model, optimizer, training_dataloader, scheduler
)`,wrap:!1}}),L=new k({props:{title:"Training loop",local:"training-loop",headingTag:"h2"}}),N=new j({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 j({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 j({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"}}),ie=new j({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}}),ne=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 St({props:{id:"save",options:["single checkpoint","sharded checkpoint"],$$slots:{default:[Kt]},$$scope:{ctx:C}}}),fe=new k({props:{title:"State",local:"state",headingTag:"h3"}}),Me=new Lt({props:{source:"https://github.com/huggingface/accelerate/blob/main/docs/source/basic_tutorials/migration.md"}}),{c(){i=m("meta"),U=r(),p=m("p"),u=r(),h(o.$$.fragment),s=r(),h(T.$$.fragment),v=r(),_=m("p"),_.textContent=d,n=r(),J=m("p"),J.innerHTML=dt,ge=r(),h(W.$$.fragment),be=r(),h(X.$$.fragment),Te=r(),R=m("p"),R.innerHTML=pt,Ue=r(),B=m("p"),B.innerHTML=mt,ve=r(),h(H.$$.fragment),_e=r(),Y=m("p"),Y.innerHTML=ft,Je=r(),h(V.$$.fragment),je=r(),h(x.$$.fragment),ke=r(),z=m("p"),z.innerHTML=ut,Ce=r(),G=m("blockquote"),G.innerHTML=ht,Ge=r(),Q=m("p"),Q.textContent=yt,Ze=r(),h(F.$$.fragment),Ie=r(),h(L.$$.fragment),Ae=r(),S=m("p"),S.innerHTML=Mt,We=r(),h(N.$$.fragment),Xe=r(),E=m("p"),E.textContent=wt,Re=r(),h(P.$$.fragment),Be=r(),h(K.$$.fragment),He=r(),q=m("p"),q.textContent=$t,Ye=r(),h(D.$$.fragment),Ve=r(),O=m("p"),O.innerHTML=gt,xe=r(),h(ee.$$.fragment),ze=r(),h(te.$$.fragment),Qe=r(),ae=m("p"),ae.textContent=bt,Fe=r(),le=m("ul"),le.innerHTML=Tt,Le=r(),h(re.$$.fragment),Se=r(),ce=m("p"),ce.textContent=Ut,Ne=r(),oe=m("p"),oe.innerHTML=vt,Ee=r(),Z=m("blockquote"),Z.innerHTML=_t,Pe=r(),h(ie.$$.fragment),Ke=r(),h(ne.$$.fragment),qe=r(),se=m("p"),se.innerHTML=Jt,De=r(),h(de.$$.fragment),Oe=r(),pe=m("p"),pe.innerHTML=jt,et=r(),me=m("p"),me.innerHTML=kt,tt=r(),h(I.$$.fragment),at=r(),h(fe.$$.fragment),lt=r(),ue=m("p"),ue.innerHTML=Ct,rt=r(),he=m("p"),he.innerHTML=Gt,ct=r(),ye=m("p"),ye.innerHTML=Zt,ot=r(),A=m("blockquote"),A.innerHTML=It,it=r(),h(Me.$$.fragment),nt=r(),we=m("p"),this.h()},l(e){const t=xt("svelte-u9bgzb",document.head);i=f(t,"META",{name:!0,content:!0}),t.forEach(a),U=c(e),p=f(e,"P",{}),Wt(p).forEach(a),u=c(e),y(o.$$.fragment,e),s=c(e),y(T.$$.fragment,e),v=c(e),_=f(e,"P",{"data-svelte-h":!0}),b(_)!=="svelte-19zbp5z"&&(_.textContent=d),n=c(e),J=f(e,"P",{"data-svelte-h":!0}),b(J)!=="svelte-1lmjen8"&&(J.innerHTML=dt),ge=c(e),y(W.$$.fragment,e),be=c(e),y(X.$$.fragment,e),Te=c(e),R=f(e,"P",{"data-svelte-h":!0}),b(R)!=="svelte-1dn1lk2"&&(R.innerHTML=pt),Ue=c(e),B=f(e,"P",{"data-svelte-h":!0}),b(B)!=="svelte-zcqlut"&&(B.innerHTML=mt),ve=c(e),y(H.$$.fragment,e),_e=c(e),Y=f(e,"P",{"data-svelte-h":!0}),b(Y)!=="svelte-1csx4md"&&(Y.innerHTML=ft),Je=c(e),y(V.$$.fragment,e),je=c(e),y(x.$$.fragment,e),ke=c(e),z=f(e,"P",{"data-svelte-h":!0}),b(z)!=="svelte-wqk9uz"&&(z.innerHTML=ut),Ce=c(e),G=f(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),b(G)!=="svelte-v2v6f9"&&(G.innerHTML=ht),Ge=c(e),Q=f(e,"P",{"data-svelte-h":!0}),b(Q)!=="svelte-l5v2wx"&&(Q.textContent=yt),Ze=c(e),y(F.$$.fragment,e),Ie=c(e),y(L.$$.fragment,e),Ae=c(e),S=f(e,"P",{"data-svelte-h":!0}),b(S)!=="svelte-i5xqdp"&&(S.innerHTML=Mt),We=c(e),y(N.$$.fragment,e),Xe=c(e),E=f(e,"P",{"data-svelte-h":!0}),b(E)!=="svelte-1b6tm11"&&(E.textContent=wt),Re=c(e),y(P.$$.fragment,e),Be=c(e),y(K.$$.fragment,e),He=c(e),q=f(e,"P",{"data-svelte-h":!0}),b(q)!=="svelte-1ef6475"&&(q.textContent=$t),Ye=c(e),y(D.$$.fragment,e),Ve=c(e),O=f(e,"P",{"data-svelte-h":!0}),b(O)!=="svelte-1nruvdz"&&(O.innerHTML=gt),xe=c(e),y(ee.$$.fragment,e),ze=c(e),y(te.$$.fragment,e),Qe=c(e),ae=f(e,"P",{"data-svelte-h":!0}),b(ae)!=="svelte-bg95gl"&&(ae.textContent=bt),Fe=c(e),le=f(e,"UL",{"data-svelte-h":!0}),b(le)!=="svelte-3kii7l"&&(le.innerHTML=Tt),Le=c(e),y(re.$$.fragment,e),Se=c(e),ce=f(e,"P",{"data-svelte-h":!0}),b(ce)!=="svelte-81s14c"&&(ce.textContent=Ut),Ne=c(e),oe=f(e,"P",{"data-svelte-h":!0}),b(oe)!=="svelte-mmotdg"&&(oe.innerHTML=vt),Ee=c(e),Z=f(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),b(Z)!=="svelte-1wnxfgp"&&(Z.innerHTML=_t),Pe=c(e),y(ie.$$.fragment,e),Ke=c(e),y(ne.$$.fragment,e),qe=c(e),se=f(e,"P",{"data-svelte-h":!0}),b(se)!=="svelte-dqdivd"&&(se.innerHTML=Jt),De=c(e),y(de.$$.fragment,e),Oe=c(e),pe=f(e,"P",{"data-svelte-h":!0}),b(pe)!=="svelte-nvfiz9"&&(pe.innerHTML=jt),et=c(e),me=f(e,"P",{"data-svelte-h":!0}),b(me)!=="svelte-56aoja"&&(me.innerHTML=kt),tt=c(e),y(I.$$.fragment,e),at=c(e),y(fe.$$.fragment,e),lt=c(e),ue=f(e,"P",{"data-svelte-h":!0}),b(ue)!=="svelte-qzsquh"&&(ue.innerHTML=Ct),rt=c(e),he=f(e,"P",{"data-svelte-h":!0}),b(he)!=="svelte-cf1uwy"&&(he.innerHTML=Gt),ct=c(e),ye=f(e,"P",{"data-svelte-h":!0}),b(ye)!=="svelte-uuw2iq"&&(ye.innerHTML=Zt),ot=c(e),A=f(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),b(A)!=="svelte-npxcpt"&&(A.innerHTML=It),it=c(e),y(Me.$$.fragment,e),nt=c(e),we=f(e,"P",{}),Wt(we).forEach(a),this.h()},h(){$e(i,"name","hf:doc:metadata"),$e(i,"content",Dt),$e(G,"class","tip"),$e(Z,"class","warning"),$e(A,"class","tip")},m(e,t){zt(document.head,i),l(e,U,t),l(e,p,t),l(e,u,t),M(o,e,t),l(e,s,t),M(T,e,t),l(e,v,t),l(e,_,t),l(e,n,t),l(e,J,t),l(e,ge,t),M(W,e,t),l(e,be,t),M(X,e,t),l(e,Te,t),l(e,R,t),l(e,Ue,t),l(e,B,t),l(e,ve,t),M(H,e,t),l(e,_e,t),l(e,Y,t),l(e,Je,t),M(V,e,t),l(e,je,t),M(x,e,t),l(e,ke,t),l(e,z,t),l(e,Ce,t),l(e,G,t),l(e,Ge,t),l(e,Q,t),l(e,Ze,t),M(F,e,t),l(e,Ie,t),M(L,e,t),l(e,Ae,t),l(e,S,t),l(e,We,t),M(N,e,t),l(e,Xe,t),l(e,E,t),l(e,Re,t),M(P,e,t),l(e,Be,t),M(K,e,t),l(e,He,t),l(e,q,t),l(e,Ye,t),M(D,e,t),l(e,Ve,t),l(e,O,t),l(e,xe,t),M(ee,e,t),l(e,ze,t),M(te,e,t),l(e,Qe,t),l(e,ae,t),l(e,Fe,t),l(e,le,t),l(e,Le,t),M(re,e,t),l(e,Se,t),l(e,ce,t),l(e,Ne,t),l(e,oe,t),l(e,Ee,t),l(e,Z,t),l(e,Pe,t),M(ie,e,t),l(e,Ke,t),M(ne,e,t),l(e,qe,t),l(e,se,t),l(e,De,t),M(de,e,t),l(e,Oe,t),l(e,pe,t),l(e,et,t),l(e,me,t),l(e,tt,t),M(I,e,t),l(e,at,t),M(fe,e,t),l(e,lt,t),l(e,ue,t),l(e,rt,t),l(e,he,t),l(e,ct,t),l(e,ye,t),l(e,ot,t),l(e,A,t),l(e,it,t),M(Me,e,t),l(e,nt,t),l(e,we,t),st=!0},p(e,[t]){const At={};t&2&&(At.$$scope={dirty:t,ctx:e}),I.$set(At)},i(e){st||(w(o.$$.fragment,e),w(T.$$.fragment,e),w(W.$$.fragment,e),w(X.$$.fragment,e),w(H.$$.fragment,e),w(V.$$.fragment,e),w(x.$$.fragment,e),w(F.$$.fragment,e),w(L.$$.fragment,e),w(N.$$.fragment,e),w(P.$$.fragment,e),w(K.$$.fragment,e),w(D.$$.fragment,e),w(ee.$$.fragment,e),w(te.$$.fragment,e),w(re.$$.fragment,e),w(ie.$$.fragment,e),w(ne.$$.fragment,e),w(de.$$.fragment,e),w(I.$$.fragment,e),w(fe.$$.fragment,e),w(Me.$$.fragment,e),st=!0)},o(e){$(o.$$.fragment,e),$(T.$$.fragment,e),$(W.$$.fragment,e),$(X.$$.fragment,e),$(H.$$.fragment,e),$(V.$$.fragment,e),$(x.$$.fragment,e),$(F.$$.fragment,e),$(L.$$.fragment,e),$(N.$$.fragment,e),$(P.$$.fragment,e),$(K.$$.fragment,e),$(D.$$.fragment,e),$(ee.$$.fragment,e),$(te.$$.fragment,e),$(re.$$.fragment,e),$(ie.$$.fragment,e),$(ne.$$.fragment,e),$(de.$$.fragment,e),$(I.$$.fragment,e),$(fe.$$.fragment,e),$(Me.$$.fragment,e),st=!1},d(e){e&&(a(U),a(p),a(u),a(s),a(v),a(_),a(n),a(J),a(ge),a(be),a(Te),a(R),a(Ue),a(B),a(ve),a(_e),a(Y),a(Je),a(je),a(ke),a(z),a(Ce),a(G),a(Ge),a(Q),a(Ze),a(Ie),a(Ae),a(S),a(We),a(Xe),a(E),a(Re),a(Be),a(He),a(q),a(Ye),a(Ve),a(O),a(xe),a(ze),a(Qe),a(ae),a(Fe),a(le),a(Le),a(Se),a(ce),a(Ne),a(oe),a(Ee),a(Z),a(Pe),a(Ke),a(qe),a(se),a(De),a(Oe),a(pe),a(et),a(me),a(tt),a(at),a(lt),a(ue),a(rt),a(he),a(ct),a(ye),a(ot),a(A),a(it),a(nt),a(we)),a(i),g(o,e),g(T,e),g(W,e),g(X,e),g(H,e),g(V,e),g(x,e),g(F,e),g(L,e),g(N,e),g(P,e),g(K,e),g(D,e),g(ee,e),g(te,e),g(re,e),g(ie,e),g(ne,e),g(de,e),g(I,e),g(fe,e),g(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(C){return Ht(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class oa extends Yt{constructor(i){super(),Vt(this,i,Ot,qt,Bt,{})}}export{oa as component};
