import{s as Me,o as Te,n as re}from"../chunks/scheduler.8c3d61f6.js";import{S as _e,i as ve,g as f,s as p,r as b,A as Je,h,f as s,c as m,j as ge,u as g,x as y,k as ye,y as je,a,v as w,d as M,t as T,w as _}from"../chunks/index.da70eac4.js";import{T as Ue}from"../chunks/Tip.1d9b8c37.js";import{C as ie}from"../chunks/CodeBlock.a9c4becf.js";import{H as le,E as Ze}from"../chunks/getInferenceSnippets.cd165132.js";import{H as Ge,a as we}from"../chunks/HfOption.6c3b4e77.js";function Ce(v){let n,d="If reproducibility is important to your use case, we recommend always using a CPU <code>Generator</code>. The performance loss is often negligible and you’ll generate more similar values.";return{c(){n=f("p"),n.innerHTML=d},l(r){n=h(r,"P",{"data-svelte-h":!0}),y(n)!=="svelte-14ecxc2"&&(n.innerHTML=d)},m(r,o){a(r,n,o)},p:re,d(r){r&&s(n)}}}function ke(v){let n,d='The GPU uses a different random number generator than the CPU. Diffusers solves this issue with the <a href="/docs/diffusers/main/en/api/utilities#diffusers.utils.torch_utils.randn_tensor">randn_tensor()</a> function to create the random tensor on a CPU and then moving it to the GPU. This function is used everywhere inside the pipeline and you don’t need to explicitly call it.',r,o,l='Use <a href="https://docs.pytorch.org/docs/stable/generated/torch.manual_seed.html" rel="nofollow">manual_seed</a> as shown below to set a seed.',i,u,J;return u=new ie({props:{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBJTBBZGRpbSUyMCUzRCUyMERESU1QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyZ29vZ2xlJTJGZGRwbS1jaWZhcjEwLTMyJTIyJTJDJTIwZGV2aWNlX21hcCUzRCUyMmN1ZGElMjIpJTBBZ2VuZXJhdG9yJTIwJTNEJTIwdG9yY2gubWFudWFsX3NlZWQoMCklMEFpbWFnZSUyMCUzRCUyMGRkaW0obnVtX2luZmVyZW5jZV9zdGVwcyUzRDIlMkMlMjBvdXRwdXRfdHlwZSUzRCUyMm5wJTIyJTJDJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yKS5pbWFnZXMlMEFwcmludChucC5hYnMoaW1hZ2UpLnN1bSgpKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline

ddim = DDIMPipeline.from_pretrained(<span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>, device_map=<span class="hljs-string">&quot;cuda&quot;</span>)
generator = torch.manual_seed(<span class="hljs-number">0</span>)
image = ddim(num_inference_steps=<span class="hljs-number">2</span>, output_type=<span class="hljs-string">&quot;np&quot;</span>, generator=generator).images
<span class="hljs-built_in">print</span>(np.<span class="hljs-built_in">abs</span>(image).<span class="hljs-built_in">sum</span>())`,wrap:!1}}),{c(){n=f("p"),n.innerHTML=d,r=p(),o=f("p"),o.innerHTML=l,i=p(),b(u.$$.fragment)},l(c){n=h(c,"P",{"data-svelte-h":!0}),y(n)!=="svelte-1nlmyz0"&&(n.innerHTML=d),r=m(c),o=h(c,"P",{"data-svelte-h":!0}),y(o)!=="svelte-195eoue"&&(o.innerHTML=l),i=m(c),g(u.$$.fragment,c)},m(c,$){a(c,n,$),a(c,r,$),a(c,o,$),a(c,i,$),w(u,c,$),J=!0},p:re,i(c){J||(M(u.$$.fragment,c),J=!0)},o(c){T(u.$$.fragment,c),J=!1},d(c){c&&(s(n),s(r),s(o),s(i)),_(u,c)}}}function We(v){let n,d='Set <code>device=&quot;cpu&quot;</code> in the <code>Generator</code> and use <a href="https://docs.pytorch.org/docs/stable/generated/torch.manual_seed.html" rel="nofollow">manual_seed</a> to set a seed for generating random numbers.',r,o,l;return o=new ie({props:{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBJTBBZGRpbSUyMCUzRCUyMERESU1QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyZ29vZ2xlJTJGZGRwbS1jaWZhcjEwLTMyJTIyKSUwQWdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjcHUlMjIpLm1hbnVhbF9zZWVkKDApJTBBaW1hZ2UlMjAlM0QlMjBkZGltKG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QyJTJDJTIwb3V0cHV0X3R5cGUlM0QlMjJucCUyMiUyQyUyMGdlbmVyYXRvciUzRGdlbmVyYXRvcikuaW1hZ2VzJTBBcHJpbnQobnAuYWJzKGltYWdlKS5zdW0oKSk=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline

ddim = DDIMPipeline.from_pretrained(<span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>)
generator = torch.Generator(device=<span class="hljs-string">&quot;cpu&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
image = ddim(num_inference_steps=<span class="hljs-number">2</span>, output_type=<span class="hljs-string">&quot;np&quot;</span>, generator=generator).images
<span class="hljs-built_in">print</span>(np.<span class="hljs-built_in">abs</span>(image).<span class="hljs-built_in">sum</span>())`,wrap:!1}}),{c(){n=f("p"),n.innerHTML=d,r=p(),b(o.$$.fragment)},l(i){n=h(i,"P",{"data-svelte-h":!0}),y(n)!=="svelte-i5qv65"&&(n.innerHTML=d),r=m(i),g(o.$$.fragment,i)},m(i,u){a(i,n,u),a(i,r,u),w(o,i,u),l=!0},p:re,i(i){l||(M(o.$$.fragment,i),l=!0)},o(i){T(o.$$.fragment,i),l=!1},d(i){i&&(s(n),s(r)),_(o,i)}}}function Re(v){let n,d,r,o;return n=new we({props:{id:"generator",option:"GPU",$$slots:{default:[ke]},$$scope:{ctx:v}}}),r=new we({props:{id:"generator",option:"CPU",$$slots:{default:[We]},$$scope:{ctx:v}}}),{c(){b(n.$$.fragment),d=p(),b(r.$$.fragment)},l(l){g(n.$$.fragment,l),d=m(l),g(r.$$.fragment,l)},m(l,i){w(n,l,i),a(l,d,i),w(r,l,i),o=!0},p(l,i){const u={};i&2&&(u.$$scope={dirty:i,ctx:l}),n.$set(u);const J={};i&2&&(J.$$scope={dirty:i,ctx:l}),r.$set(J)},i(l){o||(M(n.$$.fragment,l),M(r.$$.fragment,l),o=!0)},o(l){T(n.$$.fragment,l),T(r.$$.fragment,l),o=!1},d(l){l&&s(d),_(n,l),_(r,l)}}}function Ve(v){let n,d,r,o,l,i,u,J="Diffusion is a random process that generates a different output every time. For certain situations like testing and replicating results, you want to generate the same result each time, across releases and platforms within a certain tolerance range.",c,$,oe="This guide will show you how to control sources of randomness and enable deterministic algorithms.",L,Z,S,G,pe='Pipelines rely on <a href="https://pytorch.org/docs/stable/generated/torch.randn.html" rel="nofollow">torch.randn</a>, which uses a different random seed each time, to create the initial noisy tensors. To generate the same output on a CPU or GPU, use a <a href="https://docs.pytorch.org/docs/stable/generated/torch.Generator.html" rel="nofollow">Generator</a> to manage how random values are generated.',F,j,x,U,D,C,me="The <code>Generator</code> object should be passed to the pipeline instead of an integer seed. <code>Generator</code> maintains a <em>random state</em> that is consumed and modified when used. Once consumed, the same <code>Generator</code> object produces different results in subsequent calls, even across different pipelines, because it’s <em>state</em> has changed.",Y,k,Q,W,q,R,ce='PyTorch supports <a href="https://docs.pytorch.org/docs/stable/notes/randomness.html#avoiding-nondeterministic-algorithms" rel="nofollow">deterministic algorithms</a> - where available - for certain operations so they produce the same results. Deterministic algorithms may be slower and decrease performance.',A,V,ue='Use Diffusers’ <a href="https://github.com/huggingface/diffusers/blob/142f353e1c638ff1d20bd798402b68f72c1ebbdd/src/diffusers/utils/testing_utils.py#L861" rel="nofollow">enable_full_determinism</a> function to enable deterministic algorithms.',K,H,N,I,de="Under the hood, <code>enable_full_determinism</code> works by:",O,P,fe='<li>Setting the environment variable <a href="https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility" rel="nofollow">CUBLAS_WORKSPACE_CONFIG</a> to <code>:16:8</code> to only use one buffer size during rntime. Non-deterministic behavior occurs when operations are used in more than one CUDA stream.</li> <li>Disabling benchmarking to find the fastest convolution operation by setting <code>torch.backends.cudnn.benchmark=False</code>. Non-deterministic behavior occurs because the benchmark may select different algorithms each time depending on hardware or benchmarking noise.</li> <li>Disabling TensorFloat32 (TF32) operations in favor of more precise and consistent full-precision operations.</li>',ee,B,te,X,he='We strongly recommend reading PyTorch’s developer notes about <a href="https://docs.pytorch.org/docs/stable/notes/randomness.html" rel="nofollow">Reproducibility</a>. You can try to limit randomness, but it is not <em>guaranteed</em> even with an identical seed.',se,E,ne,z,ae;return l=new le({props:{title:"Reproducibility",local:"reproducibility",headingTag:"h1"}}),Z=new le({props:{title:"Generator",local:"generator",headingTag:"h2"}}),j=new Ue({props:{warning:!1,$$slots:{default:[Ce]},$$scope:{ctx:v}}}),U=new Ge({props:{id:"generator",options:["GPU","CPU"],$$slots:{default:[Re]},$$scope:{ctx:v}}}),k=new ie({props:{code:"Z2VuZXJhdG9yJTIwJTNEJTIwdG9yY2gubWFudWFsX3NlZWQoMCklMEElMEFmb3IlMjBfJTIwaW4lMjByYW5nZSg1KSUzQSUwQS0lMjAlMjAlMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGVsaW5lKHByb21wdCUyQyUyMGdlbmVyYXRvciUzRGdlbmVyYXRvciklMEElMkIlMjAlMjAlMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGVsaW5lKHByb21wdCUyQyUyMGdlbmVyYXRvciUzRHRvcmNoLm1hbnVhbF9zZWVkKDApKQ==",highlighted:`generator = torch.manual_seed(<span class="hljs-number">0</span>)

<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):
-    image = pipeline(prompt, generator=generator)
+    image = pipeline(prompt, generator=torch.manual_seed(<span class="hljs-number">0</span>))`,wrap:!1}}),W=new le({props:{title:"Deterministic algorithms",local:"deterministic-algorithms",headingTag:"h2"}}),H=new ie({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzX3V0aWxzJTIwaW1wb3J0JTIwZW5hYmxlX2Z1bGxfZGV0ZXJtaW5pc20lMEElMEFlbmFibGVfZnVsbF9kZXRlcm1pbmlzbSgp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers_utils <span class="hljs-keyword">import</span> enable_full_determinism

enable_full_determinism()`,wrap:!1}}),B=new le({props:{title:"Resources",local:"resources",headingTag:"h2"}}),E=new Ze({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/reusing_seeds.md"}}),{c(){n=f("meta"),d=p(),r=f("p"),o=p(),b(l.$$.fragment),i=p(),u=f("p"),u.textContent=J,c=p(),$=f("p"),$.textContent=oe,L=p(),b(Z.$$.fragment),S=p(),G=f("p"),G.innerHTML=pe,F=p(),b(j.$$.fragment),x=p(),b(U.$$.fragment),D=p(),C=f("p"),C.innerHTML=me,Y=p(),b(k.$$.fragment),Q=p(),b(W.$$.fragment),q=p(),R=f("p"),R.innerHTML=ce,A=p(),V=f("p"),V.innerHTML=ue,K=p(),b(H.$$.fragment),N=p(),I=f("p"),I.innerHTML=de,O=p(),P=f("ul"),P.innerHTML=fe,ee=p(),b(B.$$.fragment),te=p(),X=f("p"),X.innerHTML=he,se=p(),b(E.$$.fragment),ne=p(),z=f("p"),this.h()},l(e){const t=Je("svelte-u9bgzb",document.head);n=h(t,"META",{name:!0,content:!0}),t.forEach(s),d=m(e),r=h(e,"P",{}),ge(r).forEach(s),o=m(e),g(l.$$.fragment,e),i=m(e),u=h(e,"P",{"data-svelte-h":!0}),y(u)!=="svelte-urbwv"&&(u.textContent=J),c=m(e),$=h(e,"P",{"data-svelte-h":!0}),y($)!=="svelte-j5curv"&&($.textContent=oe),L=m(e),g(Z.$$.fragment,e),S=m(e),G=h(e,"P",{"data-svelte-h":!0}),y(G)!=="svelte-p45uo9"&&(G.innerHTML=pe),F=m(e),g(j.$$.fragment,e),x=m(e),g(U.$$.fragment,e),D=m(e),C=h(e,"P",{"data-svelte-h":!0}),y(C)!=="svelte-1q1k51l"&&(C.innerHTML=me),Y=m(e),g(k.$$.fragment,e),Q=m(e),g(W.$$.fragment,e),q=m(e),R=h(e,"P",{"data-svelte-h":!0}),y(R)!=="svelte-174lkkp"&&(R.innerHTML=ce),A=m(e),V=h(e,"P",{"data-svelte-h":!0}),y(V)!=="svelte-1hzqg3c"&&(V.innerHTML=ue),K=m(e),g(H.$$.fragment,e),N=m(e),I=h(e,"P",{"data-svelte-h":!0}),y(I)!=="svelte-k420hc"&&(I.innerHTML=de),O=m(e),P=h(e,"UL",{"data-svelte-h":!0}),y(P)!=="svelte-9fj8sk"&&(P.innerHTML=fe),ee=m(e),g(B.$$.fragment,e),te=m(e),X=h(e,"P",{"data-svelte-h":!0}),y(X)!=="svelte-1flhclo"&&(X.innerHTML=he),se=m(e),g(E.$$.fragment,e),ne=m(e),z=h(e,"P",{}),ge(z).forEach(s),this.h()},h(){ye(n,"name","hf:doc:metadata"),ye(n,"content",He)},m(e,t){je(document.head,n),a(e,d,t),a(e,r,t),a(e,o,t),w(l,e,t),a(e,i,t),a(e,u,t),a(e,c,t),a(e,$,t),a(e,L,t),w(Z,e,t),a(e,S,t),a(e,G,t),a(e,F,t),w(j,e,t),a(e,x,t),w(U,e,t),a(e,D,t),a(e,C,t),a(e,Y,t),w(k,e,t),a(e,Q,t),w(W,e,t),a(e,q,t),a(e,R,t),a(e,A,t),a(e,V,t),a(e,K,t),w(H,e,t),a(e,N,t),a(e,I,t),a(e,O,t),a(e,P,t),a(e,ee,t),w(B,e,t),a(e,te,t),a(e,X,t),a(e,se,t),w(E,e,t),a(e,ne,t),a(e,z,t),ae=!0},p(e,[t]){const $e={};t&2&&($e.$$scope={dirty:t,ctx:e}),j.$set($e);const be={};t&2&&(be.$$scope={dirty:t,ctx:e}),U.$set(be)},i(e){ae||(M(l.$$.fragment,e),M(Z.$$.fragment,e),M(j.$$.fragment,e),M(U.$$.fragment,e),M(k.$$.fragment,e),M(W.$$.fragment,e),M(H.$$.fragment,e),M(B.$$.fragment,e),M(E.$$.fragment,e),ae=!0)},o(e){T(l.$$.fragment,e),T(Z.$$.fragment,e),T(j.$$.fragment,e),T(U.$$.fragment,e),T(k.$$.fragment,e),T(W.$$.fragment,e),T(H.$$.fragment,e),T(B.$$.fragment,e),T(E.$$.fragment,e),ae=!1},d(e){e&&(s(d),s(r),s(o),s(i),s(u),s(c),s($),s(L),s(S),s(G),s(F),s(x),s(D),s(C),s(Y),s(Q),s(q),s(R),s(A),s(V),s(K),s(N),s(I),s(O),s(P),s(ee),s(te),s(X),s(se),s(ne),s(z)),s(n),_(l,e),_(Z,e),_(j,e),_(U,e),_(k,e),_(W,e),_(H,e),_(B,e),_(E,e)}}}const He='{"title":"Reproducibility","local":"reproducibility","sections":[{"title":"Generator","local":"generator","sections":[],"depth":2},{"title":"Deterministic algorithms","local":"deterministic-algorithms","sections":[],"depth":2},{"title":"Resources","local":"resources","sections":[],"depth":2}],"depth":1}';function Ie(v){return Te(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Se extends _e{constructor(n){super(),ve(this,n,Ie,Ve,Me,{})}}export{Se as component};
