import{s as Le,o as Te,n as $e}from"../chunks/scheduler.defa9a21.js";import{S as be,i as we,g as s,s as i,r as N,A as He,h as c,f as a,c as l,j as ye,u as Q,x as d,k as ge,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 ve}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=s("p"),n.textContent=h},l(o){n=c(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=s("p"),n.innerHTML=h},l(o){n=c(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 Ae(k){let n,h,o,p,u,D,_,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>.`,S,y,re=`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)`,z,g,ne='Then, when calling <a href="/docs/accelerate/main/en/package_reference/accelerator#accelerate.Accelerator.prepare">prepare()</a>, the library:',j,v,ie='<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>',q,$,le=`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).`,E,L,se='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:',I,T,ce=`<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>`,U,b,oe='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.',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/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.`,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/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>.',Y,P,fe='For more details about the internals, see the <a href="../package_reference/torch_wrappers">Internals page</a>.',B,C,J,A,K;return u=new xe({props:{title:"Accelerate’s internal mechanisms",local:"accelerates-internal-mechanisms",headingTag:"h1"}}),m=new ve({props:{warning:!0,$$slots:{default:[Ce]},$$scope:{ctx:k}}}),f=new ve({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=s("meta"),h=i(),o=s("p"),p=i(),N(u.$$.fragment),D=i(),_=s("p"),_.innerHTML=ae,S=i(),y=s("p"),y.innerHTML=re,z=i(),g=s("p"),g.innerHTML=ne,j=i(),v=s("ul"),v.innerHTML=ie,q=i(),$=s("p"),$.innerHTML=le,E=i(),L=s("p"),L.innerHTML=se,I=i(),T=s("ul"),T.innerHTML=ce,U=i(),b=s("p"),b.innerHTML=oe,O=i(),w=s("p"),w.textContent=de,R=i(),H=s("ul"),H.innerHTML=he,G=i(),M=s("p"),M.innerHTML=pe,F=i(),N(m.$$.fragment),W=i(),N(f.$$.fragment),X=i(),x=s("p"),x.innerHTML=me,Y=i(),P=s("p"),P.innerHTML=fe,B=i(),N(C.$$.fragment),J=i(),A=s("p"),this.h()},l(e){const t=He("svelte-u9bgzb",document.head);n=c(t,"META",{name:!0,content:!0}),t.forEach(a),h=l(e),o=c(e,"P",{}),ye(o).forEach(a),p=l(e),Q(u.$$.fragment,e),D=l(e),_=c(e,"P",{"data-svelte-h":!0}),d(_)!=="svelte-1i5bxcr"&&(_.innerHTML=ae),S=l(e),y=c(e,"P",{"data-svelte-h":!0}),d(y)!=="svelte-1hypqer"&&(y.innerHTML=re),z=l(e),g=c(e,"P",{"data-svelte-h":!0}),d(g)!=="svelte-6pjg03"&&(g.innerHTML=ne),j=l(e),v=c(e,"UL",{"data-svelte-h":!0}),d(v)!=="svelte-rii4xv"&&(v.innerHTML=ie),q=l(e),$=c(e,"P",{"data-svelte-h":!0}),d($)!=="svelte-1xlmls8"&&($.innerHTML=le),E=l(e),L=c(e,"P",{"data-svelte-h":!0}),d(L)!=="svelte-w1g3aa"&&(L.innerHTML=se),I=l(e),T=c(e,"UL",{"data-svelte-h":!0}),d(T)!=="svelte-zg63ut"&&(T.innerHTML=ce),U=l(e),b=c(e,"P",{"data-svelte-h":!0}),d(b)!=="svelte-jz6m0n"&&(b.innerHTML=oe),O=l(e),w=c(e,"P",{"data-svelte-h":!0}),d(w)!=="svelte-rp8jqi"&&(w.textContent=de),R=l(e),H=c(e,"UL",{"data-svelte-h":!0}),d(H)!=="svelte-1nv6po7"&&(H.innerHTML=he),G=l(e),M=c(e,"P",{"data-svelte-h":!0}),d(M)!=="svelte-v2oynf"&&(M.innerHTML=pe),F=l(e),Q(m.$$.fragment,e),W=l(e),Q(f.$$.fragment,e),X=l(e),x=c(e,"P",{"data-svelte-h":!0}),d(x)!=="svelte-ps1yqq"&&(x.innerHTML=me),Y=l(e),P=c(e,"P",{"data-svelte-h":!0}),d(P)!=="svelte-12lucyn"&&(P.innerHTML=fe),B=l(e),Q(C.$$.fragment,e),J=l(e),A=c(e,"P",{}),ye(A).forEach(a),this.h()},h(){ge(n,"name","hf:doc:metadata"),ge(n,"content",De)},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,D,t),r(e,_,t),r(e,S,t),r(e,y,t),r(e,z,t),r(e,g,t),r(e,j,t),r(e,v,t),r(e,q,t),r(e,$,t),r(e,E,t),r(e,L,t),r(e,I,t),r(e,T,t),r(e,U,t),r(e,b,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,A,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(D),a(_),a(S),a(y),a(z),a(g),a(j),a(v),a(q),a($),a(E),a(L),a(I),a(T),a(U),a(b),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(A)),a(n),te(u,e),te(m,e),te(f,e),te(C,e)}}}const De='{"title":"Accelerate’s internal mechanisms","local":"accelerates-internal-mechanisms","sections":[],"depth":1}';function Se(k){return Te(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ie extends be{constructor(n){super(),we(this,n,Se,Ae,Le,{})}}export{Ie as component};
