import{s as _e,n as xe,o as Ne}from"../chunks/scheduler.25b97de1.js";import{S as Ve,i as Re,g as m,s as n,r,A as Qe,h as w,f as a,c as s,j as ve,u as i,x as y,k as Ye,y as Ee,a as l,v as c,d as o,t as p,w as M}from"../chunks/index.d9030fc9.js";import{C as h}from"../chunks/CodeBlock.e6cd0d95.js";import{H as F,E as Fe}from"../chunks/EditOnGithub.91d95064.js";function He(Ue){let d,z,H,K,J,L,T,ge='As models get bigger, parallelism has emerged as a strategy for training larger models on limited hardware and accelerating training speed by several orders of magnitude. At Hugging Face, we created the <a href="https://huggingface.co/docs/accelerate" rel="nofollow">🤗 Accelerate</a> library to help users easily train a 🤗 Transformers model on any type of distributed setup, whether it is multiple GPU’s on one machine or multiple GPU’s across several machines. In this tutorial, learn how to customize your native PyTorch training loop to enable training in a distributed environment.',P,f,D,j,$e="Get started by installing 🤗 Accelerate:",q,u,O,b,Ie='Then import and create an <a href="https://huggingface.co/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator" rel="nofollow">Accelerator</a> object. The <a href="https://huggingface.co/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator" rel="nofollow">Accelerator</a> will automatically detect your type of distributed setup and initialize all the necessary components for training. You don’t need to explicitly place your model on a device.',ee,U,te,g,ae,$,Ce='The next step is to pass all the relevant training objects to the <a href="https://huggingface.co/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.prepare" rel="nofollow">prepare</a> method. This includes your training and evaluation DataLoaders, a model and an optimizer:',le,I,ne,C,se,B,Be='The last addition is to replace the typical <code>loss.backward()</code> in your training loop with 🤗 Accelerate’s <a href="https://huggingface.co/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.backward" rel="nofollow">backward</a> method:',re,A,ie,Z,Ae="As you can see in the following code, you only need to add four additional lines of code to your training loop to enable distributed training!",ce,W,oe,G,pe,X,Ze="Once you’ve added the relevant lines of code, launch your training in a script or a notebook like Colaboratory.",Me,k,me,v,We="If you are running your training from a script, run the following command to create and save a configuration file:",we,Y,ye,_,Ge="Then launch your training with:",de,x,he,N,Je,V,Xe='🤗 Accelerate can also run in a notebook if you’re planning on using Colaboratory’s TPUs. Wrap all the code responsible for training in a function, and pass it to <a href="https://huggingface.co/docs/accelerate/main/en/package_reference/launchers#accelerate.notebook_launcher" rel="nofollow">notebook_launcher</a>:',Te,R,fe,Q,ke='For more information about 🤗 Accelerate and its rich features, refer to the <a href="https://huggingface.co/docs/accelerate" rel="nofollow">documentation</a>.',je,E,ue,S,be;return J=new F({props:{title:"Distributed training with 🤗 Accelerate",local:"distributed-training-with--accelerate",headingTag:"h1"}}),f=new F({props:{title:"Setup",local:"setup",headingTag:"h2"}}),u=new h({props:{code:"cGlwJTIwaW5zdGFsbCUyMGFjY2VsZXJhdGU=",highlighted:"pip install accelerate",wrap:!1}}),U=new h({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWFjY2VsZXJhdG9yJTIwJTNEJTIwQWNjZWxlcmF0b3IoKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator

<span class="hljs-meta">&gt;&gt;&gt; </span>accelerator = Accelerator()`,wrap:!1}}),g=new F({props:{title:"Prepare to accelerate",local:"prepare-to-accelerate",headingTag:"h2"}}),I=new h({props:{code:"dHJhaW5fZGF0YWxvYWRlciUyQyUyMGV2YWxfZGF0YWxvYWRlciUyQyUyMG1vZGVsJTJDJTIwb3B0aW1pemVyJTIwJTNEJTIwYWNjZWxlcmF0b3IucHJlcGFyZSglMEElMjAlMjAlMjAlMjB0cmFpbl9kYXRhbG9hZGVyJTJDJTIwZXZhbF9kYXRhbG9hZGVyJTJDJTIwbW9kZWwlMkMlMjBvcHRpbWl6ZXIlMEEp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(
<span class="hljs-meta">... </span>    train_dataloader, eval_dataloader, model, optimizer
<span class="hljs-meta">... </span>)`,wrap:!1}}),C=new F({props:{title:"Backward",local:"backward",headingTag:"h2"}}),A=new h({props:{code:"Zm9yJTIwZXBvY2glMjBpbiUyMHJhbmdlKG51bV9lcG9jaHMpJTNBJTBBJTIwJTIwJTIwJTIwZm9yJTIwYmF0Y2glMjBpbiUyMHRyYWluX2RhdGFsb2FkZXIlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRzJTIwJTNEJTIwbW9kZWwoKipiYXRjaCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBsb3NzJTIwJTNEJTIwb3V0cHV0cy5sb3NzJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYWNjZWxlcmF0b3IuYmFja3dhcmQobG9zcyklMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuc3RlcCgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbHJfc2NoZWR1bGVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG9wdGltaXplci56ZXJvX2dyYWQoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByb2dyZXNzX2Jhci51cGRhdGUoMSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
<span class="hljs-meta">... </span>        outputs = model(**batch)
<span class="hljs-meta">... </span>        loss = outputs.loss
<span class="hljs-meta">... </span>        accelerator.backward(loss)

<span class="hljs-meta">... </span>        optimizer.step()
<span class="hljs-meta">... </span>        lr_scheduler.step()
<span class="hljs-meta">... </span>        optimizer.zero_grad()
<span class="hljs-meta">... </span>        progress_bar.update(<span class="hljs-number">1</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9yLmJhY2t3YXJkKGxvc3MpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3B0aW1pemVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxyX3NjaGVkdWxlci5zdGVwKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuemVyb19ncmFkKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9ncmVzc19iYXIudXBkYXRlKDEp",highlighted:`<span class="hljs-addition">+ from accelerate import Accelerator</span>
  from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

<span class="hljs-addition">+ accelerator = Accelerator()</span>

  model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
  optimizer = AdamW(model.parameters(), lr=3e-5)

<span class="hljs-deletion">- device = torch.device(&quot;cuda&quot;) if torch.cuda.is_available() else torch.device(&quot;cpu&quot;)</span>
<span class="hljs-deletion">- model.to(device)</span>

<span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(</span>
<span class="hljs-addition">+     train_dataloader, eval_dataloader, model, optimizer</span>
<span class="hljs-addition">+ )</span>

  num_epochs = 3
  num_training_steps = num_epochs * len(train_dataloader)
  lr_scheduler = get_scheduler(
      &quot;linear&quot;,
      optimizer=optimizer,
      num_warmup_steps=0,
      num_training_steps=num_training_steps
  )

  progress_bar = tqdm(range(num_training_steps))

  model.train()
  for epoch in range(num_epochs):
      for batch in train_dataloader:
<span class="hljs-deletion">-         batch = {k: v.to(device) for k, v in batch.items()}</span>
          outputs = model(**batch)
          loss = outputs.loss
<span class="hljs-deletion">-         loss.backward()</span>
<span class="hljs-addition">+         accelerator.backward(loss)</span>

          optimizer.step()
          lr_scheduler.step()
          optimizer.zero_grad()
          progress_bar.update(1)`,wrap:!1}}),G=new F({props:{title:"Train",local:"train",headingTag:"h2"}}),k=new F({props:{title:"Train with a script",local:"train-with-a-script",headingTag:"h3"}}),Y=new h({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZw==",highlighted:"accelerate config",wrap:!1}}),x=new h({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluLnB5",highlighted:"accelerate launch train.py",wrap:!1}}),N=new F({props:{title:"Train with a notebook",local:"train-with-a-notebook",headingTag:"h3"}}),R=new h({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBub3RlYm9va19sYXVuY2hlciUwQSUwQW5vdGVib29rX2xhdW5jaGVyKHRyYWluaW5nX2Z1bmN0aW9uKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> notebook_launcher

<span class="hljs-meta">&gt;&gt;&gt; </span>notebook_launcher(training_function)`,wrap:!1}}),E=new Fe({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/en/accelerate.md"}}),{c(){d=m("meta"),z=n(),H=m("p"),K=n(),r(J.$$.fragment),L=n(),T=m("p"),T.innerHTML=ge,P=n(),r(f.$$.fragment),D=n(),j=m("p"),j.textContent=$e,q=n(),r(u.$$.fragment),O=n(),b=m("p"),b.innerHTML=Ie,ee=n(),r(U.$$.fragment),te=n(),r(g.$$.fragment),ae=n(),$=m("p"),$.innerHTML=Ce,le=n(),r(I.$$.fragment),ne=n(),r(C.$$.fragment),se=n(),B=m("p"),B.innerHTML=Be,re=n(),r(A.$$.fragment),ie=n(),Z=m("p"),Z.textContent=Ae,ce=n(),r(W.$$.fragment),oe=n(),r(G.$$.fragment),pe=n(),X=m("p"),X.textContent=Ze,Me=n(),r(k.$$.fragment),me=n(),v=m("p"),v.textContent=We,we=n(),r(Y.$$.fragment),ye=n(),_=m("p"),_.textContent=Ge,de=n(),r(x.$$.fragment),he=n(),r(N.$$.fragment),Je=n(),V=m("p"),V.innerHTML=Xe,Te=n(),r(R.$$.fragment),fe=n(),Q=m("p"),Q.innerHTML=ke,je=n(),r(E.$$.fragment),ue=n(),S=m("p"),this.h()},l(e){const t=Qe("svelte-u9bgzb",document.head);d=w(t,"META",{name:!0,content:!0}),t.forEach(a),z=s(e),H=w(e,"P",{}),ve(H).forEach(a),K=s(e),i(J.$$.fragment,e),L=s(e),T=w(e,"P",{"data-svelte-h":!0}),y(T)!=="svelte-11mv690"&&(T.innerHTML=ge),P=s(e),i(f.$$.fragment,e),D=s(e),j=w(e,"P",{"data-svelte-h":!0}),y(j)!=="svelte-1wyzwc5"&&(j.textContent=$e),q=s(e),i(u.$$.fragment,e),O=s(e),b=w(e,"P",{"data-svelte-h":!0}),y(b)!=="svelte-1j0db2t"&&(b.innerHTML=Ie),ee=s(e),i(U.$$.fragment,e),te=s(e),i(g.$$.fragment,e),ae=s(e),$=w(e,"P",{"data-svelte-h":!0}),y($)!=="svelte-6emsq4"&&($.innerHTML=Ce),le=s(e),i(I.$$.fragment,e),ne=s(e),i(C.$$.fragment,e),se=s(e),B=w(e,"P",{"data-svelte-h":!0}),y(B)!=="svelte-1jccf24"&&(B.innerHTML=Be),re=s(e),i(A.$$.fragment,e),ie=s(e),Z=w(e,"P",{"data-svelte-h":!0}),y(Z)!=="svelte-tbmaal"&&(Z.textContent=Ae),ce=s(e),i(W.$$.fragment,e),oe=s(e),i(G.$$.fragment,e),pe=s(e),X=w(e,"P",{"data-svelte-h":!0}),y(X)!=="svelte-1bmx0kl"&&(X.textContent=Ze),Me=s(e),i(k.$$.fragment,e),me=s(e),v=w(e,"P",{"data-svelte-h":!0}),y(v)!=="svelte-d65x2y"&&(v.textContent=We),we=s(e),i(Y.$$.fragment,e),ye=s(e),_=w(e,"P",{"data-svelte-h":!0}),y(_)!=="svelte-xc3fgn"&&(_.textContent=Ge),de=s(e),i(x.$$.fragment,e),he=s(e),i(N.$$.fragment,e),Je=s(e),V=w(e,"P",{"data-svelte-h":!0}),y(V)!=="svelte-6n473t"&&(V.innerHTML=Xe),Te=s(e),i(R.$$.fragment,e),fe=s(e),Q=w(e,"P",{"data-svelte-h":!0}),y(Q)!=="svelte-5m2lvn"&&(Q.innerHTML=ke),je=s(e),i(E.$$.fragment,e),ue=s(e),S=w(e,"P",{}),ve(S).forEach(a),this.h()},h(){Ye(d,"name","hf:doc:metadata"),Ye(d,"content",Se)},m(e,t){Ee(document.head,d),l(e,z,t),l(e,H,t),l(e,K,t),c(J,e,t),l(e,L,t),l(e,T,t),l(e,P,t),c(f,e,t),l(e,D,t),l(e,j,t),l(e,q,t),c(u,e,t),l(e,O,t),l(e,b,t),l(e,ee,t),c(U,e,t),l(e,te,t),c(g,e,t),l(e,ae,t),l(e,$,t),l(e,le,t),c(I,e,t),l(e,ne,t),c(C,e,t),l(e,se,t),l(e,B,t),l(e,re,t),c(A,e,t),l(e,ie,t),l(e,Z,t),l(e,ce,t),c(W,e,t),l(e,oe,t),c(G,e,t),l(e,pe,t),l(e,X,t),l(e,Me,t),c(k,e,t),l(e,me,t),l(e,v,t),l(e,we,t),c(Y,e,t),l(e,ye,t),l(e,_,t),l(e,de,t),c(x,e,t),l(e,he,t),c(N,e,t),l(e,Je,t),l(e,V,t),l(e,Te,t),c(R,e,t),l(e,fe,t),l(e,Q,t),l(e,je,t),c(E,e,t),l(e,ue,t),l(e,S,t),be=!0},p:xe,i(e){be||(o(J.$$.fragment,e),o(f.$$.fragment,e),o(u.$$.fragment,e),o(U.$$.fragment,e),o(g.$$.fragment,e),o(I.$$.fragment,e),o(C.$$.fragment,e),o(A.$$.fragment,e),o(W.$$.fragment,e),o(G.$$.fragment,e),o(k.$$.fragment,e),o(Y.$$.fragment,e),o(x.$$.fragment,e),o(N.$$.fragment,e),o(R.$$.fragment,e),o(E.$$.fragment,e),be=!0)},o(e){p(J.$$.fragment,e),p(f.$$.fragment,e),p(u.$$.fragment,e),p(U.$$.fragment,e),p(g.$$.fragment,e),p(I.$$.fragment,e),p(C.$$.fragment,e),p(A.$$.fragment,e),p(W.$$.fragment,e),p(G.$$.fragment,e),p(k.$$.fragment,e),p(Y.$$.fragment,e),p(x.$$.fragment,e),p(N.$$.fragment,e),p(R.$$.fragment,e),p(E.$$.fragment,e),be=!1},d(e){e&&(a(z),a(H),a(K),a(L),a(T),a(P),a(D),a(j),a(q),a(O),a(b),a(ee),a(te),a(ae),a($),a(le),a(ne),a(se),a(B),a(re),a(ie),a(Z),a(ce),a(oe),a(pe),a(X),a(Me),a(me),a(v),a(we),a(ye),a(_),a(de),a(he),a(Je),a(V),a(Te),a(fe),a(Q),a(je),a(ue),a(S)),a(d),M(J,e),M(f,e),M(u,e),M(U,e),M(g,e),M(I,e),M(C,e),M(A,e),M(W,e),M(G,e),M(k,e),M(Y,e),M(x,e),M(N,e),M(R,e),M(E,e)}}}const Se='{"title":"Distributed training with 🤗 Accelerate","local":"distributed-training-with--accelerate","sections":[{"title":"Setup","local":"setup","sections":[],"depth":2},{"title":"Prepare to accelerate","local":"prepare-to-accelerate","sections":[],"depth":2},{"title":"Backward","local":"backward","sections":[],"depth":2},{"title":"Train","local":"train","sections":[{"title":"Train with a script","local":"train-with-a-script","sections":[],"depth":3},{"title":"Train with a notebook","local":"train-with-a-notebook","sections":[],"depth":3}],"depth":2}],"depth":1}';function ze(Ue){return Ne(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class qe extends Ve{constructor(d){super(),Re(this,d,ze,He,_e,{})}}export{qe as component};
