import{s as Le,o as We,n as Ye}from"../chunks/scheduler.defa9a21.js";import{S as Be,i as Se,g as r,s as n,r as c,A as He,h as o,f as l,c as a,j as Ze,u as d,x as m,k as Je,y as Xe,a as s,v as h,d as u,t as f,w as $}from"../chunks/index.fe795e71.js";import{T as Ae}from"../chunks/Tip.179eb360.js";import{C as ge}from"../chunks/CodeBlock.204b6c34.js";import{H as V,E as Ee}from"../chunks/getInferenceSnippets.2234a8dd.js";function Ie(E){let i,g="In this example, there are two GPUs for “Multi-GPU” and a TPU pod with 8 workers";return{c(){i=r("p"),i.textContent=g},l(p){i=o(p,"P",{"data-svelte-h":!0}),m(i)!=="svelte-k9rq2f"&&(i.textContent=g)},m(p,b){s(p,i,b)},p:Ye,d(p){p&&l(i)}}}function Ve(E){let i,g=`Since users can have their own learning rate schedulers defined, we leave this up to the user to decide if they wish to scale their
learning rate or not.`;return{c(){i=r("p"),i.textContent=g},l(p){i=o(p,"P",{"data-svelte-h":!0}),m(i)!=="svelte-1birp9c"&&(i.textContent=g)},m(p,b){s(p,i,b)},p:Ye,d(p){p&&l(i)}}}function Re(E){let i,g,p,b,v,R,x,be=`Evaluating and comparing the performance from different setups can be quite tricky if you don’t know what to look for.
For example, you cannot run the same script with the same batch size across TPU, multi-GPU, and single-GPU with Accelerate
and expect your results to line up.`,F,T,we="But why?",N,_,Me="There are three reasons for this that this tutorial will cover:",Q,C,ve="<li><strong>Setting the right seeds</strong></li> <li><strong>Observed Batch Sizes</strong></li> <li><strong>Learning Rates</strong></li>",q,j,K,y,xe='While this issue has not come up as much, make sure to use <a href="/docs/accelerate/v1.10.1/en/package_reference/utilities#accelerate.utils.set_seed">utils.set_seed()</a> to fully set the seed in all distributed cases so training will be reproducible:',O,U,D,P,Te="Why is this important? Under the hood this will set <strong>5</strong> different seed settings:",ee,z,te,G,_e="The random state, numpy’s state, torch, torch’s device state, and if TPUs are available torch_xla’s cuda state.",le,k,se,Z,Ce=`When training with Accelerate, the batch size passed to the dataloader is the <strong>batch size per GPU</strong>. What this entails is
a batch size of 64 on two GPUs is truly a batch size of 128. As a result, when testing on a single GPU this needs to be accounted for,
as well as similarly for TPUs.`,ne,J,je="The below table can be used as a quick reference to try out different batch sizes:",ae,w,ie,A,ye="<thead><tr><th>Single GPU Batch Size</th> <th>Multi-GPU Equivalent Batch Size</th> <th>TPU Equivalent Batch Size</th></tr></thead> <tbody><tr><td>256</td> <td>128</td> <td>32</td></tr> <tr><td>128</td> <td>64</td> <td>16</td></tr> <tr><td>64</td> <td>32</td> <td>8</td></tr> <tr><td>32</td> <td>16</td> <td>4</td></tr></tbody>",re,Y,oe,L,Ue=`As noted in multiple sources[<a href="https://aws.amazon.com/blogs/machine-learning/scalable-multi-node-deep-learning-training-using-gpus-in-the-aws-cloud/" rel="nofollow">1</a>][<a href="https://docs.nvidia.com/clara/clara-train-sdk/pt/model.html#classification-models-multi-gpu-training" rel="nofollow">2</a>], the learning rate should be scaled <em>linearly</em> based on the number of devices present. The below
snippet shows doing so with Accelerate:`,pe,M,me,W,ce,B,Pe=`You will also find that <code>accelerate</code> will step the learning rate based on the number of processes being trained on. This is because
of the observed batch size noted earlier. So in the case of 2 GPUs, the learning rate will be stepped twice as often as a single GPU
to account for the batch size being twice as large (if no changes to the batch size on the single GPU instance are made).`,de,S,he,H,ze=`When using gradient accumulation and mixed precision, due to how gradient averaging works (accumulation) and the precision loss (mixed precision),
some degradation in performance is expected. This will be explicitly seen when comparing the batch-wise loss between different compute
setups. However, the overall loss, metric, and general performance at the end of training should be <em>roughly</em> the same.`,ue,X,fe,I,$e;return v=new V({props:{title:"Comparing performance across distributed setups",local:"comparing-performance-across-distributed-setups",headingTag:"h1"}}),j=new V({props:{title:"Setting the Seed",local:"setting-the-seed",headingTag:"h2"}}),U=new ge({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUudXRpbHMlMjBpbXBvcnQlMjBzZXRfc2VlZCUwQSUwQXNldF9zZWVkKDQyKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate.utils <span class="hljs-keyword">import</span> set_seed

set_seed(<span class="hljs-number">42</span>)`,wrap:!1}}),z=new ge({props:{code:"JTIwJTIwJTIwJTIwcmFuZG9tLnNlZWQoc2VlZCklMEElMjAlMjAlMjAlMjBucC5yYW5kb20uc2VlZChzZWVkKSUwQSUyMCUyMCUyMCUyMHRvcmNoLm1hbnVhbF9zZWVkKHNlZWQpJTBBJTIwJTIwJTIwJTIwdG9yY2guY3VkYS5tYW51YWxfc2VlZF9hbGwoc2VlZCklMjAlMjMlMjBvciUyMHRvcmNoLnhwdS5tYW51YWxfc2VlZF9hbGwlMkMlMjBldGMlMEElMjAlMjAlMjAlMjAlMjMlMjAlNUUlNUUlMjBzYWZlJTIwdG8lMjBjYWxsJTIwdGhpcyUyMGZ1bmN0aW9uJTIwZXZlbiUyMGlmJTIwY3VkYSUyMGlzJTIwbm90JTIwYXZhaWxhYmxlJTBBJTIwJTIwJTIwJTIwaWYlMjBpc190b3JjaF94bGFfYXZhaWxhYmxlKCklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB4bS5zZXRfcm5nX3N0YXRlKHNlZWQp",highlighted:`    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed) <span class="hljs-comment"># or torch.xpu.manual_seed_all, etc</span>
    <span class="hljs-comment"># ^^ safe to call this function even if cuda is not available</span>
    <span class="hljs-keyword">if</span> is_torch_xla_available():
        xm.set_rng_state(seed)`,wrap:!1}}),k=new V({props:{title:"Observed Batch Sizes",local:"observed-batch-sizes",headingTag:"h2"}}),w=new Ae({props:{$$slots:{default:[Ie]},$$scope:{ctx:E}}}),Y=new V({props:{title:"Learning Rates",local:"learning-rates",headingTag:"h2"}}),M=new Ae({props:{$$slots:{default:[Ve]},$$scope:{ctx:E}}}),W=new ge({props:{code:"bGVhcm5pbmdfcmF0ZSUyMCUzRCUyMDFlLTMlMEFhY2NlbGVyYXRvciUyMCUzRCUyMEFjY2VsZXJhdG9yKCklMEFsZWFybmluZ19yYXRlJTIwKiUzRCUyMGFjY2VsZXJhdG9yLm51bV9wcm9jZXNzZXMlMEElMEFvcHRpbWl6ZXIlMjAlM0QlMjBBZGFtVyhwYXJhbXMlM0Rtb2RlbC5wYXJhbWV0ZXJzKCklMkMlMjBsciUzRGxlYXJuaW5nX3JhdGUp",highlighted:`learning_rate = <span class="hljs-number">1e-3</span>
accelerator = Accelerator()
learning_rate *= accelerator.num_processes

optimizer = AdamW(params=model.parameters(), lr=learning_rate)`,wrap:!1}}),S=new V({props:{title:"Gradient Accumulation and Mixed Precision",local:"gradient-accumulation-and-mixed-precision",headingTag:"h2"}}),X=new Ee({props:{source:"https://github.com/huggingface/accelerate/blob/main/docs/source/concept_guides/performance.md"}}),{c(){i=r("meta"),g=n(),p=r("p"),b=n(),c(v.$$.fragment),R=n(),x=r("p"),x.textContent=be,F=n(),T=r("p"),T.textContent=we,N=n(),_=r("p"),_.textContent=Me,Q=n(),C=r("ol"),C.innerHTML=ve,q=n(),c(j.$$.fragment),K=n(),y=r("p"),y.innerHTML=xe,O=n(),c(U.$$.fragment),D=n(),P=r("p"),P.innerHTML=Te,ee=n(),c(z.$$.fragment),te=n(),G=r("p"),G.textContent=_e,le=n(),c(k.$$.fragment),se=n(),Z=r("p"),Z.innerHTML=Ce,ne=n(),J=r("p"),J.textContent=je,ae=n(),c(w.$$.fragment),ie=n(),A=r("table"),A.innerHTML=ye,re=n(),c(Y.$$.fragment),oe=n(),L=r("p"),L.innerHTML=Ue,pe=n(),c(M.$$.fragment),me=n(),c(W.$$.fragment),ce=n(),B=r("p"),B.innerHTML=Pe,de=n(),c(S.$$.fragment),he=n(),H=r("p"),H.innerHTML=ze,ue=n(),c(X.$$.fragment),fe=n(),I=r("p"),this.h()},l(e){const t=He("svelte-u9bgzb",document.head);i=o(t,"META",{name:!0,content:!0}),t.forEach(l),g=a(e),p=o(e,"P",{}),Ze(p).forEach(l),b=a(e),d(v.$$.fragment,e),R=a(e),x=o(e,"P",{"data-svelte-h":!0}),m(x)!=="svelte-1u8i7ng"&&(x.textContent=be),F=a(e),T=o(e,"P",{"data-svelte-h":!0}),m(T)!=="svelte-1pzk3n6"&&(T.textContent=we),N=a(e),_=o(e,"P",{"data-svelte-h":!0}),m(_)!=="svelte-1px1vea"&&(_.textContent=Me),Q=a(e),C=o(e,"OL",{"data-svelte-h":!0}),m(C)!=="svelte-1ezajja"&&(C.innerHTML=ve),q=a(e),d(j.$$.fragment,e),K=a(e),y=o(e,"P",{"data-svelte-h":!0}),m(y)!=="svelte-y8zb2u"&&(y.innerHTML=xe),O=a(e),d(U.$$.fragment,e),D=a(e),P=o(e,"P",{"data-svelte-h":!0}),m(P)!=="svelte-bvqjor"&&(P.innerHTML=Te),ee=a(e),d(z.$$.fragment,e),te=a(e),G=o(e,"P",{"data-svelte-h":!0}),m(G)!=="svelte-19oel81"&&(G.textContent=_e),le=a(e),d(k.$$.fragment,e),se=a(e),Z=o(e,"P",{"data-svelte-h":!0}),m(Z)!=="svelte-y1nchk"&&(Z.innerHTML=Ce),ne=a(e),J=o(e,"P",{"data-svelte-h":!0}),m(J)!=="svelte-1pogoqw"&&(J.textContent=je),ae=a(e),d(w.$$.fragment,e),ie=a(e),A=o(e,"TABLE",{"data-svelte-h":!0}),m(A)!=="svelte-4k3weo"&&(A.innerHTML=ye),re=a(e),d(Y.$$.fragment,e),oe=a(e),L=o(e,"P",{"data-svelte-h":!0}),m(L)!=="svelte-1xakrqo"&&(L.innerHTML=Ue),pe=a(e),d(M.$$.fragment,e),me=a(e),d(W.$$.fragment,e),ce=a(e),B=o(e,"P",{"data-svelte-h":!0}),m(B)!=="svelte-1z0alx"&&(B.innerHTML=Pe),de=a(e),d(S.$$.fragment,e),he=a(e),H=o(e,"P",{"data-svelte-h":!0}),m(H)!=="svelte-1oucnsg"&&(H.innerHTML=ze),ue=a(e),d(X.$$.fragment,e),fe=a(e),I=o(e,"P",{}),Ze(I).forEach(l),this.h()},h(){Je(i,"name","hf:doc:metadata"),Je(i,"content",Fe)},m(e,t){Xe(document.head,i),s(e,g,t),s(e,p,t),s(e,b,t),h(v,e,t),s(e,R,t),s(e,x,t),s(e,F,t),s(e,T,t),s(e,N,t),s(e,_,t),s(e,Q,t),s(e,C,t),s(e,q,t),h(j,e,t),s(e,K,t),s(e,y,t),s(e,O,t),h(U,e,t),s(e,D,t),s(e,P,t),s(e,ee,t),h(z,e,t),s(e,te,t),s(e,G,t),s(e,le,t),h(k,e,t),s(e,se,t),s(e,Z,t),s(e,ne,t),s(e,J,t),s(e,ae,t),h(w,e,t),s(e,ie,t),s(e,A,t),s(e,re,t),h(Y,e,t),s(e,oe,t),s(e,L,t),s(e,pe,t),h(M,e,t),s(e,me,t),h(W,e,t),s(e,ce,t),s(e,B,t),s(e,de,t),h(S,e,t),s(e,he,t),s(e,H,t),s(e,ue,t),h(X,e,t),s(e,fe,t),s(e,I,t),$e=!0},p(e,[t]){const Ge={};t&2&&(Ge.$$scope={dirty:t,ctx:e}),w.$set(Ge);const ke={};t&2&&(ke.$$scope={dirty:t,ctx:e}),M.$set(ke)},i(e){$e||(u(v.$$.fragment,e),u(j.$$.fragment,e),u(U.$$.fragment,e),u(z.$$.fragment,e),u(k.$$.fragment,e),u(w.$$.fragment,e),u(Y.$$.fragment,e),u(M.$$.fragment,e),u(W.$$.fragment,e),u(S.$$.fragment,e),u(X.$$.fragment,e),$e=!0)},o(e){f(v.$$.fragment,e),f(j.$$.fragment,e),f(U.$$.fragment,e),f(z.$$.fragment,e),f(k.$$.fragment,e),f(w.$$.fragment,e),f(Y.$$.fragment,e),f(M.$$.fragment,e),f(W.$$.fragment,e),f(S.$$.fragment,e),f(X.$$.fragment,e),$e=!1},d(e){e&&(l(g),l(p),l(b),l(R),l(x),l(F),l(T),l(N),l(_),l(Q),l(C),l(q),l(K),l(y),l(O),l(D),l(P),l(ee),l(te),l(G),l(le),l(se),l(Z),l(ne),l(J),l(ae),l(ie),l(A),l(re),l(oe),l(L),l(pe),l(me),l(ce),l(B),l(de),l(he),l(H),l(ue),l(fe),l(I)),l(i),$(v,e),$(j,e),$(U,e),$(z,e),$(k,e),$(w,e),$(Y,e),$(M,e),$(W,e),$(S,e),$(X,e)}}}const Fe='{"title":"Comparing performance across distributed setups","local":"comparing-performance-across-distributed-setups","sections":[{"title":"Setting the Seed","local":"setting-the-seed","sections":[],"depth":2},{"title":"Observed Batch Sizes","local":"observed-batch-sizes","sections":[],"depth":2},{"title":"Learning Rates","local":"learning-rates","sections":[],"depth":2},{"title":"Gradient Accumulation and Mixed Precision","local":"gradient-accumulation-and-mixed-precision","sections":[],"depth":2}],"depth":1}';function Ne(E){return We(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class et extends Be{constructor(i){super(),Se(this,i,Ne,Re,Le,{})}}export{et as component};
