import{s as kt,o as Rt,n as Wt}from"../chunks/scheduler.d627b047.js";import{S as Zt,i as jt,g as r,s as n,r as u,m as Gt,H as Et,A as Nt,h as m,f as a,c as i,j as dt,u as d,x as c,n as zt,B as Yt,k as Ht,y as Ut,a as s,v as f,d as g,t as h,w as $}from"../chunks/index.73c51727.js";import{T as Lt}from"../chunks/Tip.a82942ec.js";import{C as ft}from"../chunks/CodeBlock.5f78c87f.js";import{H as gt,E as At}from"../chunks/index.8df1db42.js";function Bt(E){let l;return{c(){l=Gt("Section under construction. Feel free to contribute!")},l(p){l=zt(p,"Section under construction. Feel free to contribute!")},m(p,o){s(p,l,o)},d(p){p&&a(l)}}}function Ft(E){let l,p='Having one model per GPU can lead to high memory usage, which may not be feasible for large models or low-memory GPUs. In such cases, you can leverage <a href="https://github.com/deepspeedai/DeepSpeed" rel="nofollow">DeepSpeed</a>, which provides optimizations like model sharding, Zero Redundancy Optimizer, mixed precision training, and offloading to CPU or NVMe. Check out our <a href="deepspeed_integration.md">DeepSpeed Integration</a> guide for more details.';return{c(){l=r("p"),l.innerHTML=p},l(o){l=m(o,"P",{"data-svelte-h":!0}),c(l)!=="svelte-1wlllm2"&&(l.innerHTML=p)},m(o,N){s(o,l,N)},p:Wt,d(o){o&&a(l)}}}function Jt(E){let l,p,o,N,x,B,_,F,w,J,v,ht='The trainers in TRL use <a href="https://github.com/huggingface/accelerate" rel="nofollow">🤗 Accelerate</a> to enable distributed training across multiple GPUs or nodes. To do so, first create an <a href="https://github.com/huggingface/accelerate" rel="nofollow">🤗 Accelerate</a> config file by running',q,y,D,T,$t="and answering the questions according to your multi-GPU / multi-node setup. You can then launch distributed training by running:",I,M,V,P,_t='We also provide config files in the <a href="https://github.com/huggingface/trl/tree/main/examples/accelerate_configs" rel="nofollow">examples folder</a> that can be used as templates. To use these templates, simply pass the path to the config file when launching a job, e.g.:',Q,C,X,H,bt="This automatically distributes the workload across all available GPUs.",O,U,xt='Under the hood, <a href="https://github.com/huggingface/accelerate" rel="nofollow">🤗 Accelerate</a> creates one model per GPU. Each process:',K,L,wt="<li>Processes its own batch of data</li> <li>Computes the loss and gradients for that batch</li> <li>Shares gradient updates across all GPUs</li>",tt,G,vt='<img src="https://huggingface.co/datasets/trl-lib/documentation-images/resolve/main/multi_gpu.png"/>',et,z,ut,at,St=`<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>Batch Size</mtext><mo>=</mo><mtext>per_device_train_batch_size</mtext><mo>×</mo><mtext>num_devices</mtext><mo>×</mo><mtext>gradient_accumulation_steps</mtext></mrow><annotation encoding="application/x-tex">
\\text{Batch Size} = \\text{per\\_device\\_train\\_batch\\_size} \\times \\text{num\\_devices} \\times \\text{gradient\\_accumulation\\_steps}
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord text"><span class="mord">Batch Size</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0044em;vertical-align:-0.31em;"></span><span class="mord text"><span class="mord">per_device_train_batch_size</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.0044em;vertical-align:-0.31em;"></span><span class="mord text"><span class="mord">num_devices</span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1.0044em;vertical-align:-0.31em;"></span><span class="mord text"><span class="mord">gradient_accumulation_steps</span></span></span></span></span></span>`,st,S,yt="To maintain a consistent batch size when scaling to multiple GPUs, make sure to update <code>per_device_train_batch_size</code> and <code>gradient_accumulation_steps</code> accordingly.",nt,k,Tt="Example, these configurations are equivalent, and should yield the same results:",it,R,Mt="<thead><tr><th>Number of GPUs</th> <th>Per device batch size</th> <th>Gradient accumulation steps</th> <th>Comments</th></tr></thead> <tbody><tr><td>1</td> <td>32</td> <td>1</td> <td>Possibly high memory usage, but faster training</td></tr> <tr><td>1</td> <td>4</td> <td>8</td> <td>Lower memory usage, slower training</td></tr> <tr><td>8</td> <td>4</td> <td>1</td> <td>Multi-GPU to get the best of both worlds</td></tr></tbody>",lt,b,rt,W,mt,Z,Pt="We’re working on a guide for multi-node training. Stay tuned! 🚀",pt,j,ot,A,ct;return x=new gt({props:{title:"Distributing Training",local:"distributing-training",headingTag:"h1"}}),_=new Lt({props:{warning:!0,$$slots:{default:[Bt]},$$scope:{ctx:E}}}),w=new gt({props:{title:"Multi-GPU Training with TRL",local:"multi-gpu-training-with-trl",headingTag:"h2"}}),y=new ft({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZw==",highlighted:"accelerate config",wrap:!1}}),M=new ft({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluLnB5",highlighted:"accelerate launch train.py",wrap:!1}}),C=new ft({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMC0tY29uZmlnX2ZpbGUlMjBleGFtcGxlcyUyRmFjY2VsZXJhdGVfY29uZmlncyUyRm11bHRpX2dwdS55YW1sJTIwdHJhaW4ucHklMjAlM0NTQ1JJUFRfQVJHUyUzRQ==",highlighted:"accelerate launch --config_file examples/accelerate_configs/multi_gpu.yaml train.py &lt;SCRIPT_ARGS&gt;",wrap:!1}}),b=new Lt({props:{$$slots:{default:[Ft]},$$scope:{ctx:E}}}),W=new gt({props:{title:"Multi-Nodes Training",local:"multi-nodes-training",headingTag:"h2"}}),j=new At({props:{source:"https://github.com/huggingface/trl/blob/main/docs/source/distributing_training.md"}}),{c(){l=r("meta"),p=n(),o=r("p"),N=n(),u(x.$$.fragment),B=n(),u(_.$$.fragment),F=n(),u(w.$$.fragment),J=n(),v=r("p"),v.innerHTML=ht,q=n(),u(y.$$.fragment),D=n(),T=r("p"),T.textContent=$t,I=n(),u(M.$$.fragment),V=n(),P=r("p"),P.innerHTML=_t,Q=n(),u(C.$$.fragment),X=n(),H=r("p"),H.textContent=bt,O=n(),U=r("p"),U.innerHTML=xt,K=n(),L=r("ul"),L.innerHTML=wt,tt=n(),G=r("p"),G.innerHTML=vt,et=n(),z=r("p"),ut=Gt(`The effective batch size is calculated as:
`),at=new Et(!1),st=n(),S=r("p"),S.innerHTML=yt,nt=n(),k=r("p"),k.textContent=Tt,it=n(),R=r("table"),R.innerHTML=Mt,lt=n(),u(b.$$.fragment),rt=n(),u(W.$$.fragment),mt=n(),Z=r("p"),Z.textContent=Pt,pt=n(),u(j.$$.fragment),ot=n(),A=r("p"),this.h()},l(t){const e=Nt("svelte-u9bgzb",document.head);l=m(e,"META",{name:!0,content:!0}),e.forEach(a),p=i(t),o=m(t,"P",{}),dt(o).forEach(a),N=i(t),d(x.$$.fragment,t),B=i(t),d(_.$$.fragment,t),F=i(t),d(w.$$.fragment,t),J=i(t),v=m(t,"P",{"data-svelte-h":!0}),c(v)!=="svelte-1awbvnz"&&(v.innerHTML=ht),q=i(t),d(y.$$.fragment,t),D=i(t),T=m(t,"P",{"data-svelte-h":!0}),c(T)!=="svelte-n2kdl3"&&(T.textContent=$t),I=i(t),d(M.$$.fragment,t),V=i(t),P=m(t,"P",{"data-svelte-h":!0}),c(P)!=="svelte-15pg0q3"&&(P.innerHTML=_t),Q=i(t),d(C.$$.fragment,t),X=i(t),H=m(t,"P",{"data-svelte-h":!0}),c(H)!=="svelte-1n2xjt"&&(H.textContent=bt),O=i(t),U=m(t,"P",{"data-svelte-h":!0}),c(U)!=="svelte-zfygwf"&&(U.innerHTML=xt),K=i(t),L=m(t,"UL",{"data-svelte-h":!0}),c(L)!=="svelte-3s441m"&&(L.innerHTML=wt),tt=i(t),G=m(t,"P",{"data-svelte-h":!0}),c(G)!=="svelte-qt5st7"&&(G.innerHTML=vt),et=i(t),z=m(t,"P",{});var Y=dt(z);ut=zt(Y,`The effective batch size is calculated as:
`),at=Yt(Y,!1),Y.forEach(a),st=i(t),S=m(t,"P",{"data-svelte-h":!0}),c(S)!=="svelte-19d9mn0"&&(S.innerHTML=yt),nt=i(t),k=m(t,"P",{"data-svelte-h":!0}),c(k)!=="svelte-1npf4a2"&&(k.textContent=Tt),it=i(t),R=m(t,"TABLE",{"data-svelte-h":!0}),c(R)!=="svelte-nztwgr"&&(R.innerHTML=Mt),lt=i(t),d(b.$$.fragment,t),rt=i(t),d(W.$$.fragment,t),mt=i(t),Z=m(t,"P",{"data-svelte-h":!0}),c(Z)!=="svelte-cn0u2z"&&(Z.textContent=Pt),pt=i(t),d(j.$$.fragment,t),ot=i(t),A=m(t,"P",{}),dt(A).forEach(a),this.h()},h(){Ht(l,"name","hf:doc:metadata"),Ht(l,"content",qt),at.a=null},m(t,e){Ut(document.head,l),s(t,p,e),s(t,o,e),s(t,N,e),f(x,t,e),s(t,B,e),f(_,t,e),s(t,F,e),f(w,t,e),s(t,J,e),s(t,v,e),s(t,q,e),f(y,t,e),s(t,D,e),s(t,T,e),s(t,I,e),f(M,t,e),s(t,V,e),s(t,P,e),s(t,Q,e),f(C,t,e),s(t,X,e),s(t,H,e),s(t,O,e),s(t,U,e),s(t,K,e),s(t,L,e),s(t,tt,e),s(t,G,e),s(t,et,e),s(t,z,e),Ut(z,ut),at.m(St,z),s(t,st,e),s(t,S,e),s(t,nt,e),s(t,k,e),s(t,it,e),s(t,R,e),s(t,lt,e),f(b,t,e),s(t,rt,e),f(W,t,e),s(t,mt,e),s(t,Z,e),s(t,pt,e),f(j,t,e),s(t,ot,e),s(t,A,e),ct=!0},p(t,[e]){const Y={};e&2&&(Y.$$scope={dirty:e,ctx:t}),_.$set(Y);const Ct={};e&2&&(Ct.$$scope={dirty:e,ctx:t}),b.$set(Ct)},i(t){ct||(g(x.$$.fragment,t),g(_.$$.fragment,t),g(w.$$.fragment,t),g(y.$$.fragment,t),g(M.$$.fragment,t),g(C.$$.fragment,t),g(b.$$.fragment,t),g(W.$$.fragment,t),g(j.$$.fragment,t),ct=!0)},o(t){h(x.$$.fragment,t),h(_.$$.fragment,t),h(w.$$.fragment,t),h(y.$$.fragment,t),h(M.$$.fragment,t),h(C.$$.fragment,t),h(b.$$.fragment,t),h(W.$$.fragment,t),h(j.$$.fragment,t),ct=!1},d(t){t&&(a(p),a(o),a(N),a(B),a(F),a(J),a(v),a(q),a(D),a(T),a(I),a(V),a(P),a(Q),a(X),a(H),a(O),a(U),a(K),a(L),a(tt),a(G),a(et),a(z),a(st),a(S),a(nt),a(k),a(it),a(R),a(lt),a(rt),a(mt),a(Z),a(pt),a(ot),a(A)),a(l),$(x,t),$(_,t),$(w,t),$(y,t),$(M,t),$(C,t),$(b,t),$(W,t),$(j,t)}}}const qt='{"title":"Distributing Training","local":"distributing-training","sections":[{"title":"Multi-GPU Training with TRL","local":"multi-gpu-training-with-trl","sections":[],"depth":2},{"title":"Multi-Nodes Training","local":"multi-nodes-training","sections":[],"depth":2}],"depth":1}';function Dt(E){return Rt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Kt extends Zt{constructor(l){super(),jt(this,l,Dt,Jt,kt,{})}}export{Kt as component};
