import{s as _e,o as ve,n as Te}from"../chunks/scheduler.53228c21.js";import{S as Je,i as je,e as d,s as p,c as g,h as Ue,a as f,d as s,b as m,f as we,g as $,j as b,k as pe,l as Ze,m as n,n as y,t as w,o as M,p as T}from"../chunks/index.100fac89.js";import{C as Ge}from"../chunks/CopyLLMTxtMenu.2cdbb51f.js";import{C as oe}from"../chunks/CodeBlock.d30a6509.js";import{H as re,E as Ce}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.3fcbbc76.js";import{H as ke,a as Me}from"../chunks/HfOption.fad27e59.js";function We(v){let a,h='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.',c,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,_;return u=new oe({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(){a=d("p"),a.innerHTML=h,c=p(),o=d("p"),o.innerHTML=l,i=p(),g(u.$$.fragment)},l(r){a=f(r,"P",{"data-svelte-h":!0}),b(a)!=="svelte-1nlmyz0"&&(a.innerHTML=h),c=m(r),o=f(r,"P",{"data-svelte-h":!0}),b(o)!=="svelte-195eoue"&&(o.innerHTML=l),i=m(r),$(u.$$.fragment,r)},m(r,J){n(r,a,J),n(r,c,J),n(r,o,J),n(r,i,J),y(u,r,J),_=!0},p:Te,i(r){_||(w(u.$$.fragment,r),_=!0)},o(r){M(u.$$.fragment,r),_=!1},d(r){r&&(s(a),s(c),s(o),s(i)),T(u,r)}}}function Re(v){let a,h='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.',c,o,l;return o=new oe({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(){a=d("p"),a.innerHTML=h,c=p(),g(o.$$.fragment)},l(i){a=f(i,"P",{"data-svelte-h":!0}),b(a)!=="svelte-i5qv65"&&(a.innerHTML=h),c=m(i),$(o.$$.fragment,i)},m(i,u){n(i,a,u),n(i,c,u),y(o,i,u),l=!0},p:Te,i(i){l||(w(o.$$.fragment,i),l=!0)},o(i){M(o.$$.fragment,i),l=!1},d(i){i&&(s(a),s(c)),T(o,i)}}}function Ve(v){let a,h,c,o;return a=new Me({props:{id:"generator",option:"GPU",$$slots:{default:[We]},$$scope:{ctx:v}}}),c=new Me({props:{id:"generator",option:"CPU",$$slots:{default:[Re]},$$scope:{ctx:v}}}),{c(){g(a.$$.fragment),h=p(),g(c.$$.fragment)},l(l){$(a.$$.fragment,l),h=m(l),$(c.$$.fragment,l)},m(l,i){y(a,l,i),n(l,h,i),y(c,l,i),o=!0},p(l,i){const u={};i&2&&(u.$$scope={dirty:i,ctx:l}),a.$set(u);const _={};i&2&&(_.$$scope={dirty:i,ctx:l}),c.$set(_)},i(l){o||(w(a.$$.fragment,l),w(c.$$.fragment,l),o=!0)},o(l){M(a.$$.fragment,l),M(c.$$.fragment,l),o=!1},d(l){l&&s(h),T(a,l),T(c,l)}}}function He(v){let a,h,c,o,l,i,u,_,r,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.",S,Z,me="This guide will show you how to control sources of randomness and enable deterministic algorithms.",x,G,F,C,ce='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.',D,j,ue="<p>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.</p>",Q,U,Y,k,de="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.",q,W,A,R,K,V,fe='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.',N,H,he='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.',O,I,ee,B,be="Under the hood, <code>enable_full_determinism</code> works by:",te,P,ge='<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>',se,E,ne,L,$e='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.',le,X,ae,z,ie;return l=new Ge({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),u=new re({props:{title:"Reproducibility",local:"reproducibility",headingTag:"h1"}}),G=new re({props:{title:"Generator",local:"generator",headingTag:"h2"}}),U=new ke({props:{id:"generator",options:["GPU","CPU"],$$slots:{default:[Ve]},$$scope:{ctx:v}}}),W=new oe({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}}),R=new re({props:{title:"Deterministic algorithms",local:"deterministic-algorithms",headingTag:"h2"}}),I=new oe({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}}),E=new re({props:{title:"Resources",local:"resources",headingTag:"h2"}}),X=new Ce({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/reusing_seeds.md"}}),{c(){a=d("meta"),h=p(),c=d("p"),o=p(),g(l.$$.fragment),i=p(),g(u.$$.fragment),_=p(),r=d("p"),r.textContent=J,S=p(),Z=d("p"),Z.textContent=me,x=p(),g(G.$$.fragment),F=p(),C=d("p"),C.innerHTML=ce,D=p(),j=d("blockquote"),j.innerHTML=ue,Q=p(),g(U.$$.fragment),Y=p(),k=d("p"),k.innerHTML=de,q=p(),g(W.$$.fragment),A=p(),g(R.$$.fragment),K=p(),V=d("p"),V.innerHTML=fe,N=p(),H=d("p"),H.innerHTML=he,O=p(),g(I.$$.fragment),ee=p(),B=d("p"),B.innerHTML=be,te=p(),P=d("ul"),P.innerHTML=ge,se=p(),g(E.$$.fragment),ne=p(),L=d("p"),L.innerHTML=$e,le=p(),g(X.$$.fragment),ae=p(),z=d("p"),this.h()},l(e){const t=Ue("svelte-u9bgzb",document.head);a=f(t,"META",{name:!0,content:!0}),t.forEach(s),h=m(e),c=f(e,"P",{}),we(c).forEach(s),o=m(e),$(l.$$.fragment,e),i=m(e),$(u.$$.fragment,e),_=m(e),r=f(e,"P",{"data-svelte-h":!0}),b(r)!=="svelte-urbwv"&&(r.textContent=J),S=m(e),Z=f(e,"P",{"data-svelte-h":!0}),b(Z)!=="svelte-j5curv"&&(Z.textContent=me),x=m(e),$(G.$$.fragment,e),F=m(e),C=f(e,"P",{"data-svelte-h":!0}),b(C)!=="svelte-p45uo9"&&(C.innerHTML=ce),D=m(e),j=f(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),b(j)!=="svelte-avo6dt"&&(j.innerHTML=ue),Q=m(e),$(U.$$.fragment,e),Y=m(e),k=f(e,"P",{"data-svelte-h":!0}),b(k)!=="svelte-1q1k51l"&&(k.innerHTML=de),q=m(e),$(W.$$.fragment,e),A=m(e),$(R.$$.fragment,e),K=m(e),V=f(e,"P",{"data-svelte-h":!0}),b(V)!=="svelte-174lkkp"&&(V.innerHTML=fe),N=m(e),H=f(e,"P",{"data-svelte-h":!0}),b(H)!=="svelte-1hzqg3c"&&(H.innerHTML=he),O=m(e),$(I.$$.fragment,e),ee=m(e),B=f(e,"P",{"data-svelte-h":!0}),b(B)!=="svelte-k420hc"&&(B.innerHTML=be),te=m(e),P=f(e,"UL",{"data-svelte-h":!0}),b(P)!=="svelte-9fj8sk"&&(P.innerHTML=ge),se=m(e),$(E.$$.fragment,e),ne=m(e),L=f(e,"P",{"data-svelte-h":!0}),b(L)!=="svelte-1flhclo"&&(L.innerHTML=$e),le=m(e),$(X.$$.fragment,e),ae=m(e),z=f(e,"P",{}),we(z).forEach(s),this.h()},h(){pe(a,"name","hf:doc:metadata"),pe(a,"content",Ie),pe(j,"class","tip")},m(e,t){Ze(document.head,a),n(e,h,t),n(e,c,t),n(e,o,t),y(l,e,t),n(e,i,t),y(u,e,t),n(e,_,t),n(e,r,t),n(e,S,t),n(e,Z,t),n(e,x,t),y(G,e,t),n(e,F,t),n(e,C,t),n(e,D,t),n(e,j,t),n(e,Q,t),y(U,e,t),n(e,Y,t),n(e,k,t),n(e,q,t),y(W,e,t),n(e,A,t),y(R,e,t),n(e,K,t),n(e,V,t),n(e,N,t),n(e,H,t),n(e,O,t),y(I,e,t),n(e,ee,t),n(e,B,t),n(e,te,t),n(e,P,t),n(e,se,t),y(E,e,t),n(e,ne,t),n(e,L,t),n(e,le,t),y(X,e,t),n(e,ae,t),n(e,z,t),ie=!0},p(e,[t]){const ye={};t&2&&(ye.$$scope={dirty:t,ctx:e}),U.$set(ye)},i(e){ie||(w(l.$$.fragment,e),w(u.$$.fragment,e),w(G.$$.fragment,e),w(U.$$.fragment,e),w(W.$$.fragment,e),w(R.$$.fragment,e),w(I.$$.fragment,e),w(E.$$.fragment,e),w(X.$$.fragment,e),ie=!0)},o(e){M(l.$$.fragment,e),M(u.$$.fragment,e),M(G.$$.fragment,e),M(U.$$.fragment,e),M(W.$$.fragment,e),M(R.$$.fragment,e),M(I.$$.fragment,e),M(E.$$.fragment,e),M(X.$$.fragment,e),ie=!1},d(e){e&&(s(h),s(c),s(o),s(i),s(_),s(r),s(S),s(Z),s(x),s(F),s(C),s(D),s(j),s(Q),s(Y),s(k),s(q),s(A),s(K),s(V),s(N),s(H),s(O),s(ee),s(B),s(te),s(P),s(se),s(ne),s(L),s(le),s(ae),s(z)),s(a),T(l,e),T(u,e),T(G,e),T(U,e),T(W,e),T(R,e),T(I,e),T(E,e),T(X,e)}}}const Ie='{"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 Be(v){return ve(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class xe extends Je{constructor(a){super(),je(this,a,Be,He,_e,{})}}export{xe as component};
