import{s as ke,n as Ne,o as Ve}from"../chunks/scheduler.9991993c.js";import{S as Re,i as Qe,g as o,s,r as p,A as xe,h as y,f as t,c as n,j as ve,u as M,x as w,k as _e,y as Ee,a,v as i,d as m,t as r,w as c}from"../chunks/index.7fc9a5e7.js";import{C as j}from"../chunks/CodeBlock.e11cba92.js";import{H as F,E as Fe}from"../chunks/EditOnGithub.84ab7f0e.js";function He(he){let J,z,H,K,T,L,d,Ie='随着模型变得越来越大，并行性已经成为在有限硬件上训练更大模型和加速训练速度的策略，增加了数个数量级。在Hugging Face，我们创建了<a href="https://huggingface.co/docs/accelerate" rel="nofollow">🤗 加速</a>库，以帮助用户在任何类型的分布式设置上轻松训练🤗 Transformers模型，无论是在一台机器上的多个GPU还是在多个机器上的多个GPU。在本教程中，了解如何自定义您的原生PyTorch训练循环，以启用分布式环境中的训练。',P,U,q,f,ue="通过安装🤗 加速开始:",D,$,O,b,Ce="然后导入并创建<code>Accelerator</code>对象。<code>Accelerator</code>将自动检测您的分布式设置类型，并初始化所有必要的训练组件。您不需要显式地将模型放在设备上。",ee,h,le,I,te,u,Be="下一步是将所有相关的训练对象传递给<code>prepare</code>方法。这包括您的训练和评估DataLoader、一个模型和一个优化器:",ae,C,se,B,ne,Z,Ze="最后一步是用🤗 加速的<code>backward</code>方法替换训练循环中的典型<code>loss.backward()</code>:",pe,g,Me,W,ge="如您在下面的代码中所见，您只需要添加四行额外的代码到您的训练循环中即可启用分布式训练！",ie,G,me,X,re,A,We="在添加了相关代码行后，可以在脚本或笔记本（如Colaboratory）中启动训练。",ce,Y,oe,v,Ge="如果您从脚本中运行训练，请运行以下命令以创建和保存配置文件:",ye,_,we,k,Xe="然后使用以下命令启动训练:",Je,N,je,V,Te,R,Ae="🤗 加速还可以在笔记本中运行，如果您计划使用Colaboratory的TPU，则可在其中运行。将负责训练的所有代码包装在一个函数中，并将其传递给<code>notebook_launcher</code>:",de,Q,Ue,x,Ye='有关🤗 加速及其丰富功能的更多信息，请参阅<a href="https://huggingface.co/docs/accelerate" rel="nofollow">文档</a>。',fe,E,$e,S,be;return T=new F({props:{title:"🤗 加速分布式训练",local:"-加速分布式训练",headingTag:"h1"}}),U=new F({props:{title:"设置",local:"设置",headingTag:"h2"}}),$=new j({props:{code:"cGlwJTIwaW5zdGFsbCUyMGFjY2VsZXJhdGU=",highlighted:"pip install accelerate",wrap:!1}}),h=new j({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}}),I=new F({props:{title:"准备加速",local:"准备加速",headingTag:"h2"}}),C=new j({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}}),B=new F({props:{title:"反向传播",local:"反向传播",headingTag:"h2"}}),g=new j({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</span>)`,wrap:!1}}),G=new j({props:{code:"JTJCJTIwZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUyMCUyMGZyb20lMjB0cmFuc2Zvcm1lcnMlMjBpbXBvcnQlMjBBZGFtVyUyQyUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMkMlMjBnZXRfc2NoZWR1bGVyJTBBJTBBJTJCJTIwY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MjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9ncmVzc19iYXIudXBkYXRlKDEp",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}}),X=new F({props:{title:"训练",local:"训练",headingTag:"h2"}}),Y=new F({props:{title:"用脚本训练",local:"用脚本训练",headingTag:"h3"}}),_=new j({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZw==",highlighted:"accelerate config",wrap:!1}}),N=new j({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluLnB5",highlighted:"accelerate launch train.py",wrap:!1}}),V=new F({props:{title:"用笔记本训练",local:"用笔记本训练",headingTag:"h3"}}),Q=new j({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/zh/accelerate.md"}}),{c(){J=o("meta"),z=s(),H=o("p"),K=s(),p(T.$$.fragment),L=s(),d=o("p"),d.innerHTML=Ie,P=s(),p(U.$$.fragment),q=s(),f=o("p"),f.textContent=ue,D=s(),p($.$$.fragment),O=s(),b=o("p"),b.innerHTML=Ce,ee=s(),p(h.$$.fragment),le=s(),p(I.$$.fragment),te=s(),u=o("p"),u.innerHTML=Be,ae=s(),p(C.$$.fragment),se=s(),p(B.$$.fragment),ne=s(),Z=o("p"),Z.innerHTML=Ze,pe=s(),p(g.$$.fragment),Me=s(),W=o("p"),W.textContent=ge,ie=s(),p(G.$$.fragment),me=s(),p(X.$$.fragment),re=s(),A=o("p"),A.textContent=We,ce=s(),p(Y.$$.fragment),oe=s(),v=o("p"),v.textContent=Ge,ye=s(),p(_.$$.fragment),we=s(),k=o("p"),k.textContent=Xe,Je=s(),p(N.$$.fragment),je=s(),p(V.$$.fragment),Te=s(),R=o("p"),R.innerHTML=Ae,de=s(),p(Q.$$.fragment),Ue=s(),x=o("p"),x.innerHTML=Ye,fe=s(),p(E.$$.fragment),$e=s(),S=o("p"),this.h()},l(e){const l=xe("svelte-u9bgzb",document.head);J=y(l,"META",{name:!0,content:!0}),l.forEach(t),z=n(e),H=y(e,"P",{}),ve(H).forEach(t),K=n(e),M(T.$$.fragment,e),L=n(e),d=y(e,"P",{"data-svelte-h":!0}),w(d)!=="svelte-101ov1a"&&(d.innerHTML=Ie),P=n(e),M(U.$$.fragment,e),q=n(e),f=y(e,"P",{"data-svelte-h":!0}),w(f)!=="svelte-tw1ztk"&&(f.textContent=ue),D=n(e),M($.$$.fragment,e),O=n(e),b=y(e,"P",{"data-svelte-h":!0}),w(b)!=="svelte-fuwluk"&&(b.innerHTML=Ce),ee=n(e),M(h.$$.fragment,e),le=n(e),M(I.$$.fragment,e),te=n(e),u=y(e,"P",{"data-svelte-h":!0}),w(u)!=="svelte-rz36qo"&&(u.innerHTML=Be),ae=n(e),M(C.$$.fragment,e),se=n(e),M(B.$$.fragment,e),ne=n(e),Z=y(e,"P",{"data-svelte-h":!0}),w(Z)!=="svelte-1gfcp9e"&&(Z.innerHTML=Ze),pe=n(e),M(g.$$.fragment,e),Me=n(e),W=y(e,"P",{"data-svelte-h":!0}),w(W)!=="svelte-13i7565"&&(W.textContent=ge),ie=n(e),M(G.$$.fragment,e),me=n(e),M(X.$$.fragment,e),re=n(e),A=y(e,"P",{"data-svelte-h":!0}),w(A)!=="svelte-1b13pra"&&(A.textContent=We),ce=n(e),M(Y.$$.fragment,e),oe=n(e),v=y(e,"P",{"data-svelte-h":!0}),w(v)!=="svelte-1wt7rny"&&(v.textContent=Ge),ye=n(e),M(_.$$.fragment,e),we=n(e),k=y(e,"P",{"data-svelte-h":!0}),w(k)!=="svelte-1jwcwo5"&&(k.textContent=Xe),Je=n(e),M(N.$$.fragment,e),je=n(e),M(V.$$.fragment,e),Te=n(e),R=y(e,"P",{"data-svelte-h":!0}),w(R)!=="svelte-1a5o0v"&&(R.innerHTML=Ae),de=n(e),M(Q.$$.fragment,e),Ue=n(e),x=y(e,"P",{"data-svelte-h":!0}),w(x)!=="svelte-1mt0oay"&&(x.innerHTML=Ye),fe=n(e),M(E.$$.fragment,e),$e=n(e),S=y(e,"P",{}),ve(S).forEach(t),this.h()},h(){_e(J,"name","hf:doc:metadata"),_e(J,"content",Se)},m(e,l){Ee(document.head,J),a(e,z,l),a(e,H,l),a(e,K,l),i(T,e,l),a(e,L,l),a(e,d,l),a(e,P,l),i(U,e,l),a(e,q,l),a(e,f,l),a(e,D,l),i($,e,l),a(e,O,l),a(e,b,l),a(e,ee,l),i(h,e,l),a(e,le,l),i(I,e,l),a(e,te,l),a(e,u,l),a(e,ae,l),i(C,e,l),a(e,se,l),i(B,e,l),a(e,ne,l),a(e,Z,l),a(e,pe,l),i(g,e,l),a(e,Me,l),a(e,W,l),a(e,ie,l),i(G,e,l),a(e,me,l),i(X,e,l),a(e,re,l),a(e,A,l),a(e,ce,l),i(Y,e,l),a(e,oe,l),a(e,v,l),a(e,ye,l),i(_,e,l),a(e,we,l),a(e,k,l),a(e,Je,l),i(N,e,l),a(e,je,l),i(V,e,l),a(e,Te,l),a(e,R,l),a(e,de,l),i(Q,e,l),a(e,Ue,l),a(e,x,l),a(e,fe,l),i(E,e,l),a(e,$e,l),a(e,S,l),be=!0},p:Ne,i(e){be||(m(T.$$.fragment,e),m(U.$$.fragment,e),m($.$$.fragment,e),m(h.$$.fragment,e),m(I.$$.fragment,e),m(C.$$.fragment,e),m(B.$$.fragment,e),m(g.$$.fragment,e),m(G.$$.fragment,e),m(X.$$.fragment,e),m(Y.$$.fragment,e),m(_.$$.fragment,e),m(N.$$.fragment,e),m(V.$$.fragment,e),m(Q.$$.fragment,e),m(E.$$.fragment,e),be=!0)},o(e){r(T.$$.fragment,e),r(U.$$.fragment,e),r($.$$.fragment,e),r(h.$$.fragment,e),r(I.$$.fragment,e),r(C.$$.fragment,e),r(B.$$.fragment,e),r(g.$$.fragment,e),r(G.$$.fragment,e),r(X.$$.fragment,e),r(Y.$$.fragment,e),r(_.$$.fragment,e),r(N.$$.fragment,e),r(V.$$.fragment,e),r(Q.$$.fragment,e),r(E.$$.fragment,e),be=!1},d(e){e&&(t(z),t(H),t(K),t(L),t(d),t(P),t(q),t(f),t(D),t(O),t(b),t(ee),t(le),t(te),t(u),t(ae),t(se),t(ne),t(Z),t(pe),t(Me),t(W),t(ie),t(me),t(re),t(A),t(ce),t(oe),t(v),t(ye),t(we),t(k),t(Je),t(je),t(Te),t(R),t(de),t(Ue),t(x),t(fe),t($e),t(S)),t(J),c(T,e),c(U,e),c($,e),c(h,e),c(I,e),c(C,e),c(B,e),c(g,e),c(G,e),c(X,e),c(Y,e),c(_,e),c(N,e),c(V,e),c(Q,e),c(E,e)}}}const Se='{"title":"🤗 加速分布式训练","local":"-加速分布式训练","sections":[{"title":"设置","local":"设置","sections":[],"depth":2},{"title":"准备加速","local":"准备加速","sections":[],"depth":2},{"title":"反向传播","local":"反向传播","sections":[],"depth":2},{"title":"训练","local":"训练","sections":[{"title":"用脚本训练","local":"用脚本训练","sections":[],"depth":3},{"title":"用笔记本训练","local":"用笔记本训练","sections":[],"depth":3}],"depth":2}],"depth":1}';function ze(he){return Ve(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class De extends Re{constructor(J){super(),Qe(this,J,ze,He,ke,{})}}export{De as component};
