import{s as be,o as we,n as Te}from"../chunks/scheduler.b9285784.js";import{S as xe,i as Me,e as s,s as i,c as z,h as He,a as o,d as a,b as l,f as ge,g as A,j as d,k as ve,l as Pe,m as r,n as j,t as q,o as E,p as I}from"../chunks/index.26bc89a1.js";import{T as Le}from"../chunks/Tip.e4eba3d6.js";import{C as Ce,H as Se,E as ke}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.7242ff19.js";function De(k){let n,p=`Synchronization of the main torch (or CUDA or XLA) random number generator will affect any other potential random
artifacts you could have in your dataset (like random data augmentation) in the sense that all processes will get
the same random numbers from the torch random modules (so will apply the same random data augmentation if it’s
controlled by torch).`;return{c(){n=s("p"),n.textContent=p},l(c){n=o(c,"P",{"data-svelte-h":!0}),d(n)!=="svelte-1pu9s3f"&&(n.textContent=p)},m(c,h){r(c,n,h)},p:Te,d(c){c&&a(n)}}}function ze(k){let n,p=`The randomization part of your custom sampler, batch sampler or iterable dataset should be done using a local
<code>torch.Generator</code> object (in PyTorch &gt;= 1.6), see the traditional <code>RandomSampler</code>, as an example.`;return{c(){n=s("p"),n.innerHTML=p},l(c){n=o(c,"P",{"data-svelte-h":!0}),d(n)!=="svelte-1ffnyf3"&&(n.innerHTML=p)},m(c,h){r(c,n,h)},p:Te,d(c){c&&a(n)}}}function Ae(k){let n,p,c,h,u,U,_,O,y,ne=`Internally, Accelerate works by first analyzing the environment in which the script is launched to determine which
kind of distributed setup is used, how many different processes there are and which one the current script is in. All
that information is stored in the <code>~AcceleratorState</code>.`,R,$,ie=`This class is initialized the first time you instantiate an <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator">~Accelerator</a> as well as performing any
specific initialization your distributed setup needs. Its state is then uniquely shared through all instances of
<a href="/docs/accelerate/main/en/package_reference/state#accelerate.state.AcceleratorState">AcceleratorState</a>. (The same can also be done with the <a href="/docs/accelerate/main/en/package_reference/state#accelerate.PartialState">PartialState</a>, a more barebones version it inherits)`,G,g,le='Then, when calling <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.prepare">prepare()</a>, the library:',F,v,se='<li>wraps your model(s) in the container adapted for the distributed setup,</li> <li>wraps your optimizer(s) in an <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.optimizer.AcceleratedOptimizer">AcceleratedOptimizer</a>,</li> <li>wraps your scheduler(s) in an <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.scheduler.AcceleratedScheduler">AcceleratedScheduler</a></li> <li>creates a new version of your dataloader(s) in a <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderShard">DataLoaderShard</a> or <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderDispatcher">DataLoaderDispatcher</a></li>',W,L,oe=`While the model(s), optimizer(s), and scheduler(s) are just put in simple wrappers, the dataloader(s) are re-created. This is mostly
because PyTorch does not let the user change the <code>batch_sampler</code> of a dataloader once it’s been created and the
library handles the sharding of your data between processes by changing that <code>batch_sampler</code> to yield every other
<code>num_processes</code> batches (if enabled).`,X,T,ce='The <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderShard">DataLoaderShard</a> subclasses <code>DataLoader</code> to add the following functionality:',Y,b,de=`<li>it synchronizes the appropriate random number generator of all processes at each new iteration, to ensure any
randomization (like shuffling) is done the exact same way across processes.</li> <li>it puts the batches on the proper device before yielding them (unless you have opted out of
<code>device_placement=True</code>).</li>`,B,w,pe='The <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderDispatcher">DataLoaderDispatcher</a> subclasses differs from the <a href="/docs/accelerate/main/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderShard">DataLoaderShard</a> in that when iterating through the <code>DataLoader</code>, the data is all starting from process 0 and <em>then</em> split and sent off to each process rather than it happening at the dataset level.',J,x,he="The random number generator synchronization will by default synchronize:",K,M,me="<li>the <code>generator</code> attribute of a given sampler (like the PyTorch <code>RandomSampler</code>) for PyTorch &gt;= 1.6</li> <li>the main random number generator in PyTorch &lt;=1.5.1</li>",N,H,fe=`You can choose which random number generator(s) to synchronize with the <code>rng_types</code> argument of the main
<a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator">Accelerator</a>. In PyTorch &gt;= 1.6, it is recommended to rely on a local <code>generator</code> to avoid
setting the same seed in the main random number generator in all processes.`,Q,m,V,f,Z,P,ue='If you have <a href="https://github.com/pytorch/data/tree/main" rel="nofollow"><code>torchdata&gt;=0.8.0</code></a> installed, and you have passed <code>use_stateful_dataloader=True</code> into your <a href="/docs/accelerate/main/en/package_reference/utilities#accelerate.DataLoaderConfiguration">DataLoaderConfiguration</a>, these classes will directly inherit from <code>StatefulDataLoader</code> instead, and maintain a <code>state_dict</code>.',ee,C,_e='For more details about the internals, see the <a href="../package_reference/torch_wrappers">Internals page</a>.',te,S,ae,D,re;return u=new Ce({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),_=new Se({props:{title:"Accelerate’s internal mechanisms",local:"accelerates-internal-mechanisms",headingTag:"h1"}}),m=new Le({props:{warning:!0,$$slots:{default:[De]},$$scope:{ctx:k}}}),f=new Le({props:{$$slots:{default:[ze]},$$scope:{ctx:k}}}),S=new ke({props:{source:"https://github.com/huggingface/accelerate/blob/main/docs/source/concept_guides/internal_mechanism.md"}}),{c(){n=s("meta"),p=i(),c=s("p"),h=i(),z(u.$$.fragment),U=i(),z(_.$$.fragment),O=i(),y=s("p"),y.innerHTML=ne,R=i(),$=s("p"),$.innerHTML=ie,G=i(),g=s("p"),g.innerHTML=le,F=i(),v=s("ul"),v.innerHTML=se,W=i(),L=s("p"),L.innerHTML=oe,X=i(),T=s("p"),T.innerHTML=ce,Y=i(),b=s("ul"),b.innerHTML=de,B=i(),w=s("p"),w.innerHTML=pe,J=i(),x=s("p"),x.textContent=he,K=i(),M=s("ul"),M.innerHTML=me,N=i(),H=s("p"),H.innerHTML=fe,Q=i(),z(m.$$.fragment),V=i(),z(f.$$.fragment),Z=i(),P=s("p"),P.innerHTML=ue,ee=i(),C=s("p"),C.innerHTML=_e,te=i(),z(S.$$.fragment),ae=i(),D=s("p"),this.h()},l(e){const t=He("svelte-u9bgzb",document.head);n=o(t,"META",{name:!0,content:!0}),t.forEach(a),p=l(e),c=o(e,"P",{}),ge(c).forEach(a),h=l(e),A(u.$$.fragment,e),U=l(e),A(_.$$.fragment,e),O=l(e),y=o(e,"P",{"data-svelte-h":!0}),d(y)!=="svelte-1i5bxcr"&&(y.innerHTML=ne),R=l(e),$=o(e,"P",{"data-svelte-h":!0}),d($)!=="svelte-1hypqer"&&($.innerHTML=ie),G=l(e),g=o(e,"P",{"data-svelte-h":!0}),d(g)!=="svelte-6pjg03"&&(g.innerHTML=le),F=l(e),v=o(e,"UL",{"data-svelte-h":!0}),d(v)!=="svelte-rii4xv"&&(v.innerHTML=se),W=l(e),L=o(e,"P",{"data-svelte-h":!0}),d(L)!=="svelte-1xlmls8"&&(L.innerHTML=oe),X=l(e),T=o(e,"P",{"data-svelte-h":!0}),d(T)!=="svelte-w1g3aa"&&(T.innerHTML=ce),Y=l(e),b=o(e,"UL",{"data-svelte-h":!0}),d(b)!=="svelte-zg63ut"&&(b.innerHTML=de),B=l(e),w=o(e,"P",{"data-svelte-h":!0}),d(w)!=="svelte-jz6m0n"&&(w.innerHTML=pe),J=l(e),x=o(e,"P",{"data-svelte-h":!0}),d(x)!=="svelte-rp8jqi"&&(x.textContent=he),K=l(e),M=o(e,"UL",{"data-svelte-h":!0}),d(M)!=="svelte-1nv6po7"&&(M.innerHTML=me),N=l(e),H=o(e,"P",{"data-svelte-h":!0}),d(H)!=="svelte-v2oynf"&&(H.innerHTML=fe),Q=l(e),A(m.$$.fragment,e),V=l(e),A(f.$$.fragment,e),Z=l(e),P=o(e,"P",{"data-svelte-h":!0}),d(P)!=="svelte-ps1yqq"&&(P.innerHTML=ue),ee=l(e),C=o(e,"P",{"data-svelte-h":!0}),d(C)!=="svelte-12lucyn"&&(C.innerHTML=_e),te=l(e),A(S.$$.fragment,e),ae=l(e),D=o(e,"P",{}),ge(D).forEach(a),this.h()},h(){ve(n,"name","hf:doc:metadata"),ve(n,"content",je)},m(e,t){Pe(document.head,n),r(e,p,t),r(e,c,t),r(e,h,t),j(u,e,t),r(e,U,t),j(_,e,t),r(e,O,t),r(e,y,t),r(e,R,t),r(e,$,t),r(e,G,t),r(e,g,t),r(e,F,t),r(e,v,t),r(e,W,t),r(e,L,t),r(e,X,t),r(e,T,t),r(e,Y,t),r(e,b,t),r(e,B,t),r(e,w,t),r(e,J,t),r(e,x,t),r(e,K,t),r(e,M,t),r(e,N,t),r(e,H,t),r(e,Q,t),j(m,e,t),r(e,V,t),j(f,e,t),r(e,Z,t),r(e,P,t),r(e,ee,t),r(e,C,t),r(e,te,t),j(S,e,t),r(e,ae,t),r(e,D,t),re=!0},p(e,[t]){const ye={};t&2&&(ye.$$scope={dirty:t,ctx:e}),m.$set(ye);const $e={};t&2&&($e.$$scope={dirty:t,ctx:e}),f.$set($e)},i(e){re||(q(u.$$.fragment,e),q(_.$$.fragment,e),q(m.$$.fragment,e),q(f.$$.fragment,e),q(S.$$.fragment,e),re=!0)},o(e){E(u.$$.fragment,e),E(_.$$.fragment,e),E(m.$$.fragment,e),E(f.$$.fragment,e),E(S.$$.fragment,e),re=!1},d(e){e&&(a(p),a(c),a(h),a(U),a(O),a(y),a(R),a($),a(G),a(g),a(F),a(v),a(W),a(L),a(X),a(T),a(Y),a(b),a(B),a(w),a(J),a(x),a(K),a(M),a(N),a(H),a(Q),a(V),a(Z),a(P),a(ee),a(C),a(te),a(ae),a(D)),a(n),I(u,e),I(_,e),I(m,e),I(f,e),I(S,e)}}}const je='{"title":"Accelerate’s internal mechanisms","local":"accelerates-internal-mechanisms","sections":[],"depth":1}';function qe(k){return we(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Re extends xe{constructor(n){super(),Me(this,n,qe,Ae,be,{})}}export{Re as component};
