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