import{s as be,o as Le,n as $e}from"../chunks/scheduler.defa9a21.js";import{S as Te,i as we,g as c,s as l,r as N,A as He,h as s,f as a,c as i,j as ve,u as Q,x as d,k as ye,y as Me,a as r,v as V,d as Z,t as ee,w as te}from"../chunks/index.fe795e71.js";import{T as ge}from"../chunks/Tip.179eb360.js";import{H as xe,E as Pe}from"../chunks/getInferenceSnippets.2234a8dd.js";function Ce(k){let n,h=`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=c("p"),n.textContent=h},l(o){n=s(o,"P",{"data-svelte-h":!0}),d(n)!=="svelte-1pu9s3f"&&(n.textContent=h)},m(o,p){r(o,n,p)},p:$e,d(o){o&&a(n)}}}function ke(k){let n,h=`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=c("p"),n.innerHTML=h},l(o){n=s(o,"P",{"data-svelte-h":!0}),d(n)!=="svelte-1ffnyf3"&&(n.innerHTML=h)},m(o,p){r(o,n,p)},p:$e,d(o){o&&a(n)}}}function ze(k){let n,h,o,p,u,A,_,ae=`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>.`,D,v,re=`This class is initialized the first time you instantiate an <a href="/docs/accelerate/v1.10.1/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/v1.10.1/en/package_reference/state#accelerate.state.AcceleratorState">AcceleratorState</a>. (The same can also be done with the <a href="/docs/accelerate/v1.10.1/en/package_reference/state#accelerate.PartialState">PartialState</a>, a more barebones version it inherits)`,S,y,ne='Then, when calling <a href="/docs/accelerate/v1.10.1/en/package_reference/accelerator#accelerate.Accelerator.prepare">prepare()</a>, the library:',E,g,le='<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/v1.10.1/en/package_reference/torch_wrappers#accelerate.optimizer.AcceleratedOptimizer">AcceleratedOptimizer</a>,</li> <li>wraps your scheduler(s) in an <a href="/docs/accelerate/v1.10.1/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/v1.10.1/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderShard">DataLoaderShard</a> or <a href="/docs/accelerate/v1.10.1/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderDispatcher">DataLoaderDispatcher</a></li>',I,$,ie=`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).`,U,b,ce='The <a href="/docs/accelerate/v1.10.1/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderShard">DataLoaderShard</a> subclasses <code>DataLoader</code> to add the following functionality:',j,L,se=`<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>`,q,T,oe='The <a href="/docs/accelerate/v1.10.1/en/package_reference/torch_wrappers#accelerate.data_loader.DataLoaderDispatcher">DataLoaderDispatcher</a> subclasses differs from the <a href="/docs/accelerate/v1.10.1/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.',O,w,de="The random number generator synchronization will by default synchronize:",R,H,he="<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>",G,M,pe=`You can choose which random number generator(s) to synchronize with the <code>rng_types</code> argument of the main
<a href="/docs/accelerate/v1.10.1/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.`,F,m,W,f,X,x,me='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/v1.10.1/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>.',Y,P,fe='For more details about the internals, see the <a href="../package_reference/torch_wrappers">Internals page</a>.',B,C,J,z,K;return u=new xe({props:{title:"Accelerate’s internal mechanisms",local:"accelerates-internal-mechanisms",headingTag:"h1"}}),m=new ge({props:{warning:!0,$$slots:{default:[Ce]},$$scope:{ctx:k}}}),f=new ge({props:{$$slots:{default:[ke]},$$scope:{ctx:k}}}),C=new Pe({props:{source:"https://github.com/huggingface/accelerate/blob/main/docs/source/concept_guides/internal_mechanism.md"}}),{c(){n=c("meta"),h=l(),o=c("p"),p=l(),N(u.$$.fragment),A=l(),_=c("p"),_.innerHTML=ae,D=l(),v=c("p"),v.innerHTML=re,S=l(),y=c("p"),y.innerHTML=ne,E=l(),g=c("ul"),g.innerHTML=le,I=l(),$=c("p"),$.innerHTML=ie,U=l(),b=c("p"),b.innerHTML=ce,j=l(),L=c("ul"),L.innerHTML=se,q=l(),T=c("p"),T.innerHTML=oe,O=l(),w=c("p"),w.textContent=de,R=l(),H=c("ul"),H.innerHTML=he,G=l(),M=c("p"),M.innerHTML=pe,F=l(),N(m.$$.fragment),W=l(),N(f.$$.fragment),X=l(),x=c("p"),x.innerHTML=me,Y=l(),P=c("p"),P.innerHTML=fe,B=l(),N(C.$$.fragment),J=l(),z=c("p"),this.h()},l(e){const t=He("svelte-u9bgzb",document.head);n=s(t,"META",{name:!0,content:!0}),t.forEach(a),h=i(e),o=s(e,"P",{}),ve(o).forEach(a),p=i(e),Q(u.$$.fragment,e),A=i(e),_=s(e,"P",{"data-svelte-h":!0}),d(_)!=="svelte-1i5bxcr"&&(_.innerHTML=ae),D=i(e),v=s(e,"P",{"data-svelte-h":!0}),d(v)!=="svelte-1865z5v"&&(v.innerHTML=re),S=i(e),y=s(e,"P",{"data-svelte-h":!0}),d(y)!=="svelte-1e7x4bn"&&(y.innerHTML=ne),E=i(e),g=s(e,"UL",{"data-svelte-h":!0}),d(g)!=="svelte-m04cbz"&&(g.innerHTML=le),I=i(e),$=s(e,"P",{"data-svelte-h":!0}),d($)!=="svelte-1xlmls8"&&($.innerHTML=ie),U=i(e),b=s(e,"P",{"data-svelte-h":!0}),d(b)!=="svelte-1fohplc"&&(b.innerHTML=ce),j=i(e),L=s(e,"UL",{"data-svelte-h":!0}),d(L)!=="svelte-zg63ut"&&(L.innerHTML=se),q=i(e),T=s(e,"P",{"data-svelte-h":!0}),d(T)!=="svelte-6pkzaz"&&(T.innerHTML=oe),O=i(e),w=s(e,"P",{"data-svelte-h":!0}),d(w)!=="svelte-rp8jqi"&&(w.textContent=de),R=i(e),H=s(e,"UL",{"data-svelte-h":!0}),d(H)!=="svelte-1nv6po7"&&(H.innerHTML=he),G=i(e),M=s(e,"P",{"data-svelte-h":!0}),d(M)!=="svelte-nrfeu9"&&(M.innerHTML=pe),F=i(e),Q(m.$$.fragment,e),W=i(e),Q(f.$$.fragment,e),X=i(e),x=s(e,"P",{"data-svelte-h":!0}),d(x)!=="svelte-1h8i06c"&&(x.innerHTML=me),Y=i(e),P=s(e,"P",{"data-svelte-h":!0}),d(P)!=="svelte-12lucyn"&&(P.innerHTML=fe),B=i(e),Q(C.$$.fragment,e),J=i(e),z=s(e,"P",{}),ve(z).forEach(a),this.h()},h(){ye(n,"name","hf:doc:metadata"),ye(n,"content",Ae)},m(e,t){Me(document.head,n),r(e,h,t),r(e,o,t),r(e,p,t),V(u,e,t),r(e,A,t),r(e,_,t),r(e,D,t),r(e,v,t),r(e,S,t),r(e,y,t),r(e,E,t),r(e,g,t),r(e,I,t),r(e,$,t),r(e,U,t),r(e,b,t),r(e,j,t),r(e,L,t),r(e,q,t),r(e,T,t),r(e,O,t),r(e,w,t),r(e,R,t),r(e,H,t),r(e,G,t),r(e,M,t),r(e,F,t),V(m,e,t),r(e,W,t),V(f,e,t),r(e,X,t),r(e,x,t),r(e,Y,t),r(e,P,t),r(e,B,t),V(C,e,t),r(e,J,t),r(e,z,t),K=!0},p(e,[t]){const ue={};t&2&&(ue.$$scope={dirty:t,ctx:e}),m.$set(ue);const _e={};t&2&&(_e.$$scope={dirty:t,ctx:e}),f.$set(_e)},i(e){K||(Z(u.$$.fragment,e),Z(m.$$.fragment,e),Z(f.$$.fragment,e),Z(C.$$.fragment,e),K=!0)},o(e){ee(u.$$.fragment,e),ee(m.$$.fragment,e),ee(f.$$.fragment,e),ee(C.$$.fragment,e),K=!1},d(e){e&&(a(h),a(o),a(p),a(A),a(_),a(D),a(v),a(S),a(y),a(E),a(g),a(I),a($),a(U),a(b),a(j),a(L),a(q),a(T),a(O),a(w),a(R),a(H),a(G),a(M),a(F),a(W),a(X),a(x),a(Y),a(P),a(B),a(J),a(z)),a(n),te(u,e),te(m,e),te(f,e),te(C,e)}}}const Ae='{"title":"Accelerate’s internal mechanisms","local":"accelerates-internal-mechanisms","sections":[],"depth":1}';function De(k){return Le(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class je extends Te{constructor(n){super(),we(this,n,De,ze,be,{})}}export{je as component};
