import{s as ve,n as ke,o as _e}from"../chunks/scheduler.56730f09.js";import{S as xe,i as Ne,g as w,s,r as p,A as Ve,h as o,f as l,c as n,j as Ge,u as M,x as y,k as Ye,y as Re,a,v as i,d as c,t as r,w as m}from"../chunks/index.1f144517.js";import{C as j}from"../chunks/CodeBlock.738eeccb.js";import{H as E}from"../chunks/Heading.57d46534.js";function Qe(he){let J,S,F,z,T,K,d,be='모델이 커지면서 병렬 처리는 제한된 하드웨어에서 더 큰 모델을 훈련하고 훈련 속도를 몇 배로 가속화하기 위한 전략으로 등장했습니다. Hugging Face에서는 사용자가 하나의 머신에 여러 개의 GPU를 사용하든 여러 머신에 여러 개의 GPU를 사용하든 모든 유형의 분산 설정에서 🤗 Transformers 모델을 쉽게 훈련할 수 있도록 돕기 위해 <a href="https://huggingface.co/docs/accelerate" rel="nofollow">🤗 Accelerate</a> 라이브러리를 만들었습니다. 이 튜토리얼에서는 분산 환경에서 훈련할 수 있도록 기본 PyTorch 훈련 루프를 커스터마이즈하는 방법을 알아봅시다.',L,U,P,f,$e="🤗 Accelerate 설치 시작하기:",q,h,D,b,Ie="그 다음, <code>Accelerator</code> 객체를 불러오고 생성합니다. <code>Accelerator</code>는 자동으로 분산 설정 유형을 감지하고 훈련에 필요한 모든 구성 요소를 초기화합니다. 장치에 모델을 명시적으로 배치할 필요는 없습니다.",O,$,ee,I,te,u,ue="다음 단계는 관련된 모든 훈련 객체를 <code>prepare</code> 메소드에 전달하는 것입니다. 여기에는 훈련 및 평가 데이터로더, 모델 및 옵티마이저가 포함됩니다:",le,C,ae,B,se,Z,Ce="마지막으로 훈련 루프의 일반적인 <code>loss.backward()</code>를 🤗 Accelerate의 <code>backward</code> 메소드로 대체하기만 하면 됩니다:",ne,g,pe,A,Be="다음 코드에서 볼 수 있듯이, 훈련 루프에 코드 네 줄만 추가하면 분산 학습을 활성화할 수 있습니다!",Me,W,ie,X,ce,G,Ze="관련 코드를 추가한 후에는 스크립트나 Colaboratory와 같은 노트북에서 훈련을 시작하세요.",re,Y,me,v,ge="스크립트에서 훈련을 실행하는 경우, 다음 명령을 실행하여 구성 파일을 생성하고 저장합니다:",we,k,oe,_,Ae="Then launch your training with:",ye,x,Je,N,je,V,We="Collaboratory의 TPU를 사용하려는 경우, 노트북에서도 🤗 Accelerate를 실행할 수 있습니다. 훈련을 담당하는 모든 코드를 함수로 감싸서 <code>notebook_launcher</code>에 전달하세요:",Te,R,de,Q,Xe='🤗 Accelerate 및 다양한 기능에 대한 자세한 내용은 <a href="https://huggingface.co/docs/accelerate" rel="nofollow">documentation</a>를 참조하세요.',Ue,H,fe;return T=new E({props:{title:"🤗 Accelerate를 활용한 분산 학습",local:"distributed-training-with-accelerate",headingTag:"h1"}}),U=new E({props:{title:"설정",local:"setup",headingTag:"h2"}}),h=new j({props:{code:"cGlwJTIwaW5zdGFsbCUyMGFjY2VsZXJhdGU=",highlighted:"pip install accelerate",wrap:!1}}),$=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 E({props:{title:"가속화를 위한 준비",local:"prepare-to-accelerate",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 E({props:{title:"백워드(Backward)",local:"backward",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}}),W=new j({props:{code:"JTJCJTIwZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUyMCUyMGZyb20lMjB0cmFuc2Zvcm1lcnMlMjBpbXBvcnQlMjBBZGFtVyUyQyUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMkMlMjBnZXRfc2NoZWR1bGVyJTBBJTBBJTJCJTIwYWNjZWxlcmF0b3IlMjAlM0QlMjBBY2NlbGVyYXRvcigpJTBBJTBBJTIwJTIwbW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZChjaGVja3BvaW50JTJDJTIwbnVtX2xhYmVscyUzRDIpJTBBJTIwJTIwb3B0aW1pemVyJTIwJTNEJTIwQWRhbVcobW9kZWwucGFyYW1ldGVycygpJTJDJTIwbHIlM0QzZS01KSUwQSUwQS0lMjBkZXZpY2UlMjAlM0QlMjB0b3JjaC5kZXZpY2UoJTIyY3VkYSUyMiklMjBpZiUyMHRvcmNoLmN1ZGEuaXNfYXZhaWxhYmxlKCklMjBlbHNlJTIwdG9yY2guZGV2aWNlKCUyMmNwdSUyMiklMEEtJTIwbW9kZWwudG8oZGV2aWNlKSUwQSUwQSUyQiUyMHRyYWluX2RhdGFsb2FkZXIlMkMlMjBldmFsX2RhdGFsb2FkZXIlMkMlMjBtb2RlbCUyQyUyMG9wdGltaXplciUyMCUzRCUyMGFjY2VsZXJhdG9yLnByZXBhcmUoJTBBJTJCJTIwJTIwJTIwJTIwJTIwdHJhaW5fZGF0YWxvYWRlciUyQyUyMGV2YWxfZGF0YWxvYWRlciUyQyUyMG1vZGVsJTJDJTIwb3B0aW1pemVyJTBBJTJCJTIwKSUwQSUwQSUyMCUyMG51bV9lcG9jaHMl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wQSUyQiUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGFjY2VsZXJhdG9yLmJhY2t3YXJkKGxvc3MpJTBBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3B0aW1pemVyLnN0ZXAoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxyX3NjaGVkdWxlci5zdGVwKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvcHRpbWl6ZXIuemVyb19ncmFkKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9ncmVzc19iYXIudXBkYXRlKDEp",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 E({props:{title:"학습",local:"train",headingTag:"h2"}}),Y=new E({props:{title:"스크립트로 학습하기",local:"train-with-a-script",headingTag:"h3"}}),k=new j({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZw==",highlighted:"accelerate config",wrap:!1}}),x=new j({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluLnB5",highlighted:"accelerate launch train.py",wrap:!1}}),N=new E({props:{title:"노트북으로 학습하기",local:"train-with-a-notebook",headingTag:"h3"}}),R=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}}),{c(){J=w("meta"),S=s(),F=w("p"),z=s(),p(T.$$.fragment),K=s(),d=w("p"),d.innerHTML=be,L=s(),p(U.$$.fragment),P=s(),f=w("p"),f.textContent=$e,q=s(),p(h.$$.fragment),D=s(),b=w("p"),b.innerHTML=Ie,O=s(),p($.$$.fragment),ee=s(),p(I.$$.fragment),te=s(),u=w("p"),u.innerHTML=ue,le=s(),p(C.$$.fragment),ae=s(),p(B.$$.fragment),se=s(),Z=w("p"),Z.innerHTML=Ce,ne=s(),p(g.$$.fragment),pe=s(),A=w("p"),A.textContent=Be,Me=s(),p(W.$$.fragment),ie=s(),p(X.$$.fragment),ce=s(),G=w("p"),G.textContent=Ze,re=s(),p(Y.$$.fragment),me=s(),v=w("p"),v.textContent=ge,we=s(),p(k.$$.fragment),oe=s(),_=w("p"),_.textContent=Ae,ye=s(),p(x.$$.fragment),Je=s(),p(N.$$.fragment),je=s(),V=w("p"),V.innerHTML=We,Te=s(),p(R.$$.fragment),de=s(),Q=w("p"),Q.innerHTML=Xe,Ue=s(),H=w("p"),this.h()},l(e){const t=Ve("svelte-u9bgzb",document.head);J=o(t,"META",{name:!0,content:!0}),t.forEach(l),S=n(e),F=o(e,"P",{}),Ge(F).forEach(l),z=n(e),M(T.$$.fragment,e),K=n(e),d=o(e,"P",{"data-svelte-h":!0}),y(d)!=="svelte-1l3vzsc"&&(d.innerHTML=be),L=n(e),M(U.$$.fragment,e),P=n(e),f=o(e,"P",{"data-svelte-h":!0}),y(f)!=="svelte-1vp3931"&&(f.textContent=$e),q=n(e),M(h.$$.fragment,e),D=n(e),b=o(e,"P",{"data-svelte-h":!0}),y(b)!=="svelte-14p69wx"&&(b.innerHTML=Ie),O=n(e),M($.$$.fragment,e),ee=n(e),M(I.$$.fragment,e),te=n(e),u=o(e,"P",{"data-svelte-h":!0}),y(u)!=="svelte-ca2x7w"&&(u.innerHTML=ue),le=n(e),M(C.$$.fragment,e),ae=n(e),M(B.$$.fragment,e),se=n(e),Z=o(e,"P",{"data-svelte-h":!0}),y(Z)!=="svelte-1cm2xwv"&&(Z.innerHTML=Ce),ne=n(e),M(g.$$.fragment,e),pe=n(e),A=o(e,"P",{"data-svelte-h":!0}),y(A)!=="svelte-4baxra"&&(A.textContent=Be),Me=n(e),M(W.$$.fragment,e),ie=n(e),M(X.$$.fragment,e),ce=n(e),G=o(e,"P",{"data-svelte-h":!0}),y(G)!=="svelte-1l6nwy7"&&(G.textContent=Ze),re=n(e),M(Y.$$.fragment,e),me=n(e),v=o(e,"P",{"data-svelte-h":!0}),y(v)!=="svelte-7jp99x"&&(v.textContent=ge),we=n(e),M(k.$$.fragment,e),oe=n(e),_=o(e,"P",{"data-svelte-h":!0}),y(_)!=="svelte-xc3fgn"&&(_.textContent=Ae),ye=n(e),M(x.$$.fragment,e),Je=n(e),M(N.$$.fragment,e),je=n(e),V=o(e,"P",{"data-svelte-h":!0}),y(V)!=="svelte-11gpxs1"&&(V.innerHTML=We),Te=n(e),M(R.$$.fragment,e),de=n(e),Q=o(e,"P",{"data-svelte-h":!0}),y(Q)!=="svelte-1ufkf96"&&(Q.innerHTML=Xe),Ue=n(e),H=o(e,"P",{}),Ge(H).forEach(l),this.h()},h(){Ye(J,"name","hf:doc:metadata"),Ye(J,"content",Ee)},m(e,t){Re(document.head,J),a(e,S,t),a(e,F,t),a(e,z,t),i(T,e,t),a(e,K,t),a(e,d,t),a(e,L,t),i(U,e,t),a(e,P,t),a(e,f,t),a(e,q,t),i(h,e,t),a(e,D,t),a(e,b,t),a(e,O,t),i($,e,t),a(e,ee,t),i(I,e,t),a(e,te,t),a(e,u,t),a(e,le,t),i(C,e,t),a(e,ae,t),i(B,e,t),a(e,se,t),a(e,Z,t),a(e,ne,t),i(g,e,t),a(e,pe,t),a(e,A,t),a(e,Me,t),i(W,e,t),a(e,ie,t),i(X,e,t),a(e,ce,t),a(e,G,t),a(e,re,t),i(Y,e,t),a(e,me,t),a(e,v,t),a(e,we,t),i(k,e,t),a(e,oe,t),a(e,_,t),a(e,ye,t),i(x,e,t),a(e,Je,t),i(N,e,t),a(e,je,t),a(e,V,t),a(e,Te,t),i(R,e,t),a(e,de,t),a(e,Q,t),a(e,Ue,t),a(e,H,t),fe=!0},p:ke,i(e){fe||(c(T.$$.fragment,e),c(U.$$.fragment,e),c(h.$$.fragment,e),c($.$$.fragment,e),c(I.$$.fragment,e),c(C.$$.fragment,e),c(B.$$.fragment,e),c(g.$$.fragment,e),c(W.$$.fragment,e),c(X.$$.fragment,e),c(Y.$$.fragment,e),c(k.$$.fragment,e),c(x.$$.fragment,e),c(N.$$.fragment,e),c(R.$$.fragment,e),fe=!0)},o(e){r(T.$$.fragment,e),r(U.$$.fragment,e),r(h.$$.fragment,e),r($.$$.fragment,e),r(I.$$.fragment,e),r(C.$$.fragment,e),r(B.$$.fragment,e),r(g.$$.fragment,e),r(W.$$.fragment,e),r(X.$$.fragment,e),r(Y.$$.fragment,e),r(k.$$.fragment,e),r(x.$$.fragment,e),r(N.$$.fragment,e),r(R.$$.fragment,e),fe=!1},d(e){e&&(l(S),l(F),l(z),l(K),l(d),l(L),l(P),l(f),l(q),l(D),l(b),l(O),l(ee),l(te),l(u),l(le),l(ae),l(se),l(Z),l(ne),l(pe),l(A),l(Me),l(ie),l(ce),l(G),l(re),l(me),l(v),l(we),l(oe),l(_),l(ye),l(Je),l(je),l(V),l(Te),l(de),l(Q),l(Ue),l(H)),l(J),m(T,e),m(U,e),m(h,e),m($,e),m(I,e),m(C,e),m(B,e),m(g,e),m(W,e),m(X,e),m(Y,e),m(k,e),m(x,e),m(N,e),m(R,e)}}}const Ee='{"title":"🤗 Accelerate를 활용한 분산 학습","local":"distributed-training-with-accelerate","sections":[{"title":"설정","local":"setup","sections":[],"depth":2},{"title":"가속화를 위한 준비","local":"prepare-to-accelerate","sections":[],"depth":2},{"title":"백워드(Backward)","local":"backward","sections":[],"depth":2},{"title":"학습","local":"train","sections":[{"title":"스크립트로 학습하기","local":"train-with-a-script","sections":[],"depth":3},{"title":"노트북으로 학습하기","local":"train-with-a-notebook","sections":[],"depth":3}],"depth":2}],"depth":1}';function Fe(he){return _e(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Le extends xe{constructor(J){super(),Ne(this,J,Fe,Qe,ve,{})}}export{Le as component};
