import{S as Wl,i as Bl,s as $l,e as a,k as c,w as U,t as i,M as kl,c as n,d as s,m as u,a as r,x as g,h as o,b as m,G as t,g as p,y as E,q as _,o as Z,B as G,v as Pl}from"../../chunks/vendor-hf-doc-builder.js";import{T as Qt}from"../../chunks/Tip-hf-doc-builder.js";import{I as ss}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as $e}from"../../chunks/CodeBlock-hf-doc-builder.js";function Cl(Q){let d,J,h,f,I,M,T,v,W;return{c(){d=a("p"),J=i("\u{1F4A1} We strongly recommend reading PyTorch\u2019s "),h=a("a"),f=i("statement about reproducibility"),I=i(":"),M=c(),T=a("blockquote"),v=a("p"),W=i("Completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms. Furthermore, results may not be reproducible between CPU and GPU executions, even when using identical seeds."),this.h()},l(b){d=n(b,"P",{});var y=r(d);J=o(y,"\u{1F4A1} We strongly recommend reading PyTorch\u2019s "),h=n(y,"A",{href:!0,rel:!0});var B=r(h);f=o(B,"statement about reproducibility"),B.forEach(s),I=o(y,":"),y.forEach(s),M=u(b),T=n(b,"BLOCKQUOTE",{});var w=r(T);v=n(w,"P",{});var R=r(v);W=o(R,"Completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms. Furthermore, results may not be reproducible between CPU and GPU executions, even when using identical seeds."),R.forEach(s),w.forEach(s),this.h()},h(){m(h,"href","https://pytorch.org/docs/stable/notes/randomness.html"),m(h,"rel","nofollow")},m(b,y){p(b,d,y),t(d,J),t(d,h),t(h,f),t(d,I),p(b,M,y),p(b,T,y),t(T,v),t(v,W)},d(b){b&&s(d),b&&s(M),b&&s(T)}}}function Rl(Q){let d,J,h,f,I,M,T,v,W,b,y;return{c(){d=a("p"),J=i("\u{1F4A1} It might be a bit unintuitive at first to pass "),h=a("code"),f=i("Generator"),I=i(` objects to the pipeline instead of
just integer values representing the seed, but this is the recommended design when dealing with
probabilistic models in PyTorch as `),M=a("code"),T=i("Generator"),v=i("\u2019s are "),W=a("em"),b=i("random states"),y=i(` that can be
passed to multiple pipelines in a sequence.`)},l(B){d=n(B,"P",{});var w=r(d);J=o(w,"\u{1F4A1} It might be a bit unintuitive at first to pass "),h=n(w,"CODE",{});var R=r(h);f=o(R,"Generator"),R.forEach(s),I=o(w,` objects to the pipeline instead of
just integer values representing the seed, but this is the recommended design when dealing with
probabilistic models in PyTorch as `),M=n(w,"CODE",{});var ye=r(M);T=o(ye,"Generator"),ye.forEach(s),v=o(w,"\u2019s are "),W=n(w,"EM",{});var ee=r(W);b=o(ee,"random states"),ee.forEach(s),y=o(w,` that can be
passed to multiple pipelines in a sequence.`),w.forEach(s)},m(B,w){p(B,d,w),t(d,J),t(d,h),t(h,f),t(d,I),t(d,M),t(M,T),t(d,v),t(d,W),t(W,b),t(d,y)},d(B){B&&s(d)}}}function Nl(Q){let d,J;return{c(){d=a("p"),J=i(`\u{1F4A1} If reproducibility is important, we recommend always passing a CPU generator.
The performance loss is often neglectable, and you\u2019ll generate much more similar
values than if the pipeline had been run on a GPU.`)},l(h){d=n(h,"P",{});var f=r(d);J=o(f,`\u{1F4A1} If reproducibility is important, we recommend always passing a CPU generator.
The performance loss is often neglectable, and you\u2019ll generate much more similar
values than if the pipeline had been run on a GPU.`),f.forEach(s)},m(h,f){p(h,d,f),t(d,J)},d(h){h&&s(d)}}}function Vl(Q){let d,J,h,f,I,M,T,v,W,b,y,B,w,R,ye,ee,be,Qs,ts,H,ls,N,F,ke,se,Hs,Pe,Fs,as,we,Ds,ns,D,qs,Me,xs,As,rs,te,is,Je,Ys,os,q,zs,le,Ce,Ls,Ks,ps,ve,Os,cs,V,x,Re,ae,et,Ne,st,us,A,tt,ne,Ve,lt,at,ds,re,ms,$,nt,Xe,rt,it,Se,ot,pt,hs,Te,ct,fs,Y,ys,X,z,Qe,ie,ut,He,dt,bs,je,mt,ws,oe,Ms,Ue,ht,Js,j,ft,ge,yt,bt,Fe,wt,Mt,De,Jt,vt,qe,Tt,jt,vs,Ee,Ut,Ts,pe,js,L,Us,K,gt,_e,Et,_t,gs,S,O,xe,ce,Zt,Ae,Gt,Es,Ze,It,_s,k,Wt,ue,Ye,Bt,$t,ze,kt,Pt,Zs,P,Ct,Le,Rt,Nt,de,Ke,Vt,Xt,Gs,me,Is,Ge,St,Ws,he,Bs;return M=new ss({}),H=new Qt({props:{$$slots:{default:[Cl]},$$scope:{ctx:Q}}}),se=new ss({}),te=new $e({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERESU1QaXBlbGluZSUwQWltcG9ydCUyMG51bXB5JTIwYXMlMjBucCUwQSUwQW1vZGVsX2lkJTIwJTNEJTIwJTIyZ29vZ2xlJTJGZGRwbS1jaWZhcjEwLTMyJTIyJTBBJTBBJTIzJTIwbG9hZCUyMG1vZGVsJTIwYW5kJTIwc2NoZWR1bGVyJTBBZGRpbSUyMCUzRCUyMERESU1QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQobW9kZWxfaWQpJTBBJTBBJTIzJTIwcnVuJTIwcGlwZWxpbmUlMjBmb3IlMjBqdXN0JTIwdHdvJTIwc3RlcHMlMjBhbmQlMjByZXR1cm4lMjBudW1weSUyMHRlbnNvciUwQWltYWdlJTIwJTNEJTIwZGRpbShudW1faW5mZXJlbmNlX3N0ZXBzJTNEMiUyQyUyMG91dHB1dF90eXBlJTNEJTIybnAlMjIpLmltYWdlcyUwQXByaW50KG5wLmFicyhpbWFnZSkuc3VtKCkp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>

<span class="hljs-comment"># load model and scheduler</span>
ddim = DDIMPipeline.from_pretrained(model_id)

<span class="hljs-comment"># run pipeline for just two steps and return numpy tensor</span>
image = ddim(num_inference_steps=<span class="hljs-number">2</span>, output_type=<span class="hljs-string">&quot;np&quot;</span>).images
<span class="hljs-built_in">print</span>(np.<span class="hljs-built_in">abs</span>(image).<span class="hljs-built_in">sum</span>())`}}),ae=new ss({}),re=new $e({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJnb29nbGUlMkZkZHBtLWNpZmFyMTAtMzIlMjIlMEElMEElMjMlMjBsb2FkJTIwbW9kZWwlMjBhbmQlMjBzY2hlZHVsZXIlMEFkZGltJTIwJTNEJTIwRERJTVBpcGVsaW5lLmZyb21fcHJldHJhaW5lZChtb2RlbF9pZCklMEElMEElMjMlMjBjcmVhdGUlMjBhJTIwZ2VuZXJhdG9yJTIwZm9yJTIwcmVwcm9kdWNpYmlsaXR5JTBBZ2VuZXJhdG9yJTIwJTNEJTIwdG9yY2guR2VuZXJhdG9yKGRldmljZSUzRCUyMmNwdSUyMikubWFudWFsX3NlZWQoMCklMEElMEElMjMlMjBydW4lMjBwaXBlbGluZSUyMGZvciUyMGp1c3QlMjB0d28lMjBzdGVwcyUyMGFuZCUyMHJldHVybiUyMG51bXB5JTIwdGVuc29yJTBBaW1hZ2UlMjAlM0QlMjBkZGltKG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QyJTJDJTIwb3V0cHV0X3R5cGUlM0QlMjJucCUyMiUyQyUyMGdlbmVyYXRvciUzRGdlbmVyYXRvcikuaW1hZ2VzJTBBcHJpbnQobnAuYWJzKGltYWdlKS5zdW0oKSk=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>

<span class="hljs-comment"># load model and scheduler</span>
ddim = DDIMPipeline.from_pretrained(model_id)

<span class="hljs-comment"># create a generator for reproducibility</span>
generator = torch.Generator(device=<span class="hljs-string">&quot;cpu&quot;</span>).manual_seed(<span class="hljs-number">0</span>)

<span class="hljs-comment"># run pipeline for just two steps and return numpy tensor</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>())`}}),Y=new Qt({props:{$$slots:{default:[Rl]},$$scope:{ctx:Q}}}),ie=new ss({}),oe=new $e({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJnb29nbGUlMkZkZHBtLWNpZmFyMTAtMzIlMjIlMEElMEElMjMlMjBsb2FkJTIwbW9kZWwlMjBhbmQlMjBzY2hlZHVsZXIlMEFkZGltJTIwJTNEJTIwRERJTVBpcGVsaW5lLmZyb21fcHJldHJhaW5lZChtb2RlbF9pZCklMEFkZGltLnRvKCUyMmN1ZGElMjIpJTBBJTBBJTIzJTIwY3JlYXRlJTIwYSUyMGdlbmVyYXRvciUyMGZvciUyMHJlcHJvZHVjaWJpbGl0eSUwQWdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLkdlbmVyYXRvcihkZXZpY2UlM0QlMjJjdWRhJTIyKS5tYW51YWxfc2VlZCgwKSUwQSUwQSUyMyUyMHJ1biUyMHBpcGVsaW5lJTIwZm9yJTIwanVzdCUyMHR3byUyMHN0ZXBzJTIwYW5kJTIwcmV0dXJuJTIwbnVtcHklMjB0ZW5zb3IlMEFpbWFnZSUyMCUzRCUyMGRkaW0obnVtX2luZmVyZW5jZV9zdGVwcyUzRDIlMkMlMjBvdXRwdXRfdHlwZSUzRCUyMm5wJTIyJTJDJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yKS5pbWFnZXMlMEFwcmludChucC5hYnMoaW1hZ2UpLnN1bSgpKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>

<span class="hljs-comment"># load model and scheduler</span>
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-comment"># create a generator for reproducibility</span>
generator = torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>)

<span class="hljs-comment"># run pipeline for just two steps and return numpy tensor</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>())`}}),pe=new $e({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVBpcGVsaW5lJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJnb29nbGUlMkZkZHBtLWNpZmFyMTAtMzIlMjIlMEElMEElMjMlMjBsb2FkJTIwbW9kZWwlMjBhbmQlMjBzY2hlZHVsZXIlMEFkZGltJTIwJTNEJTIwRERJTVBpcGVsaW5lLmZyb21fcHJldHJhaW5lZChtb2RlbF9pZCklMEFkZGltLnRvKCUyMmN1ZGElMjIpJTBBJTBBJTIzJTIwY3JlYXRlJTIwYSUyMGdlbmVyYXRvciUyMGZvciUyMHJlcHJvZHVjaWJpbGl0eSUzQiUyMG5vdGljZSUyMHlvdSUyMGRvbid0JTIwcGxhY2UlMjBpdCUyMG9uJTIwdGhlJTIwR1BVISUwQWdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLm1hbnVhbF9zZWVkKDApJTBBJTBBJTIzJTIwcnVuJTIwcGlwZWxpbmUlMjBmb3IlMjBqdXN0JTIwdHdvJTIwc3RlcHMlMjBhbmQlMjByZXR1cm4lMjBudW1weSUyMHRlbnNvciUwQWltYWdlJTIwJTNEJTIwZGRpbShudW1faW5mZXJlbmNlX3N0ZXBzJTNEMiUyQyUyMG91dHB1dF90eXBlJTNEJTIybnAlMjIlMkMlMjBnZW5lcmF0b3IlM0RnZW5lcmF0b3IpLmltYWdlcyUwQXByaW50KG5wLmFicyhpbWFnZSkuc3VtKCkp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

model_id = <span class="hljs-string">&quot;google/ddpm-cifar10-32&quot;</span>

<span class="hljs-comment"># load model and scheduler</span>
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-comment"># create a generator for reproducibility; notice you don&#x27;t place it on the GPU!</span>
generator = torch.manual_seed(<span class="hljs-number">0</span>)

<span class="hljs-comment"># run pipeline for just two steps and return numpy tensor</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>())`}}),L=new Qt({props:{$$slots:{default:[Nl]},$$scope:{ctx:Q}}}),ce=new ss({}),me=new $e({props:{code:"aW1wb3J0JTIwb3MlMEElMEFvcy5lbnZpcm9uJTVCJTIyQ1VCTEFTX1dPUktTUEFDRV9DT05GSUclMjIlNUQlMjAlM0QlMjAlMjIlM0ExNiUzQTglMjIlMEElMEF0b3JjaC5iYWNrZW5kcy5jdWRubi5iZW5jaG1hcmslMjAlM0QlMjBGYWxzZSUwQXRvcmNoLnVzZV9kZXRlcm1pbmlzdGljX2FsZ29yaXRobXMoVHJ1ZSk=",highlighted:`<span class="hljs-keyword">import</span> os

os.environ[<span class="hljs-string">&quot;CUBLAS_WORKSPACE_CONFIG&quot;</span>] = <span class="hljs-string">&quot;:16:8&quot;</span>

torch.backends.cudnn.benchmark = <span class="hljs-literal">False</span>
torch.use_deterministic_algorithms(<span class="hljs-literal">True</span>)`}}),he=new $e({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRERJTVNjaGVkdWxlciUyQyUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUwQXBpcGUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQobW9kZWxfaWQpLnRvKCUyMmN1ZGElMjIpJTBBcGlwZS5zY2hlZHVsZXIlMjAlM0QlMjBERElNU2NoZWR1bGVyLmZyb21fY29uZmlnKHBpcGUuc2NoZWR1bGVyLmNvbmZpZyklMEFnJTIwJTNEJTIwdG9yY2guR2VuZXJhdG9yKGRldmljZSUzRCUyMmN1ZGElMjIpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyQSUyMGJlYXIlMjBpcyUyMHBsYXlpbmclMjBhJTIwZ3VpdGFyJTIwb24lMjBUaW1lcyUyMFNxdWFyZSUyMiUwQSUwQWcubWFudWFsX3NlZWQoMCklMEFyZXN1bHQxJTIwJTNEJTIwcGlwZShwcm9tcHQlM0Rwcm9tcHQlMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTAlMkMlMjBnZW5lcmF0b3IlM0RnJTJDJTIwb3V0cHV0X3R5cGUlM0QlMjJsYXRlbnQlMjIpLmltYWdlcyUwQSUwQWcubWFudWFsX3NlZWQoMCklMEFyZXN1bHQyJTIwJTNEJTIwcGlwZShwcm9tcHQlM0Rwcm9tcHQlMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTAlMkMlMjBnZW5lcmF0b3IlM0RnJTJDJTIwb3V0cHV0X3R5cGUlM0QlMjJsYXRlbnQlMjIpLmltYWdlcyUwQSUwQXByaW50KCUyMkxfaW5mJTIwZGlzdCUyMCUzRCUyMCUyMiUyQyUyMGFicyhyZXN1bHQxJTIwLSUyMHJlc3VsdDIpLm1heCgpKSUwQSUyMkxfaW5mJTIwZGlzdCUyMCUzRCUyMCUyMHRlbnNvcigwLiUyQyUyMGRldmljZSUzRCdjdWRhJTNBMCcpJTIy",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDIMScheduler, StableDiffusionPipeline
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

model_id = <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
pipe = StableDiffusionPipeline.from_pretrained(model_id).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
g = torch.Generator(device=<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;A bear is playing a guitar on Times Square&quot;</span>

g.manual_seed(<span class="hljs-number">0</span>)
result1 = pipe(prompt=prompt, num_inference_steps=<span class="hljs-number">50</span>, generator=g, output_type=<span class="hljs-string">&quot;latent&quot;</span>).images

g.manual_seed(<span class="hljs-number">0</span>)
result2 = pipe(prompt=prompt, num_inference_steps=<span class="hljs-number">50</span>, generator=g, output_type=<span class="hljs-string">&quot;latent&quot;</span>).images

<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;L_inf dist = &quot;</span>, <span class="hljs-built_in">abs</span>(result1 - result2).<span class="hljs-built_in">max</span>())
<span class="hljs-string">&quot;L_inf dist =  tensor(0., device=&#x27;cuda:0&#x27;)&quot;</span>`}}),{c(){d=a("meta"),J=c(),h=a("h1"),f=a("a"),I=a("span"),U(M.$$.fragment),T=c(),v=a("span"),W=i("Create reproducible pipelines"),b=c(),y=a("p"),B=i("Reproducibility is important for testing, replicating results, and can even be used to "),w=a("a"),R=i("improve image quality"),ye=i(". However, the randomness in diffusion models is a desired property because it allows the pipeline to generate different images every time it is run. While you can\u2019t expect to get the exact same results across platforms, you can expect results to be reproducible across releases and platforms within a certain tolerance range. Even then, tolerance varies depending on the diffusion pipeline and checkpoint."),ee=c(),be=a("p"),Qs=i("This is why it\u2019s important to understand how to control sources of randomness in diffusion models or use deterministic algorithms."),ts=c(),U(H.$$.fragment),ls=c(),N=a("h2"),F=a("a"),ke=a("span"),U(se.$$.fragment),Hs=c(),Pe=a("span"),Fs=i("Control randomness"),as=c(),we=a("p"),Ds=i(`During inference, pipelines rely heavily on random sampling operations which include creating the
Gaussian noise tensors to denoise and adding noise to the scheduling step.`),ns=c(),D=a("p"),qs=i("Take a look at the tensor values in the "),Me=a("a"),xs=i("DDIMPipeline"),As=i(" after two inference steps:"),rs=c(),U(te.$$.fragment),is=c(),Je=a("p"),Ys=i("Running the code above prints one value, but if you run it again you get a different value. What is going on here?"),os=c(),q=a("p"),zs=i("Every time the pipeline is run, "),le=a("a"),Ce=a("code"),Ls=i("torch.randn"),Ks=i(" uses a different random seed to create Gaussian noise which is denoised stepwise. This leads to a different result each time it is run, which is great for diffusion pipelines since it generates a different random image each time."),ps=c(),ve=a("p"),Os=i("But if you need to reliably generate the same image, that\u2019ll depend on whether you\u2019re running the pipeline on a CPU or GPU."),cs=c(),V=a("h3"),x=a("a"),Re=a("span"),U(ae.$$.fragment),et=c(),Ne=a("span"),st=i("CPU"),us=c(),A=a("p"),tt=i("To generate reproducible results on a CPU, you\u2019ll need to use a PyTorch "),ne=a("a"),Ve=a("code"),lt=i("Generator"),at=i(" and set a seed:"),ds=c(),U(re.$$.fragment),ms=c(),$=a("p"),nt=i("Now when you run the code above, it always prints a value of "),Xe=a("code"),rt=i("1491.1711"),it=i(" no matter what because the "),Se=a("code"),ot=i("Generator"),pt=i(" object with the seed is passed to all the random functions of the pipeline."),hs=c(),Te=a("p"),ct=i("If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result."),fs=c(),U(Y.$$.fragment),ys=c(),X=a("h3"),z=a("a"),Qe=a("span"),U(ie.$$.fragment),ut=c(),He=a("span"),dt=i("GPU"),bs=c(),je=a("p"),mt=i("Writing a reproducible pipeline on a GPU is a bit trickier, and full reproducibility across different hardware is not guaranteed because matrix multiplication - which diffusion pipelines require a lot of - is less deterministic on a GPU than a CPU. For example, if you run the same code example above on a GPU:"),ws=c(),U(oe.$$.fragment),Ms=c(),Ue=a("p"),ht=i("The result is not the same even though you\u2019re using an identical seed because the GPU uses a different random number generator than the CPU."),Js=c(),j=a("p"),ft=i("To circumvent this problem, \u{1F9E8} Diffusers has a "),ge=a("a"),yt=i("randn_tensor()"),bt=i(" function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The "),Fe=a("code"),wt=i("randn_tensor"),Mt=i(" function is used everywhere inside the pipeline, allowing the user to "),De=a("strong"),Jt=i("always"),vt=i(" pass a CPU "),qe=a("code"),Tt=i("Generator"),jt=i(" even if the pipeline is run on a GPU."),vs=c(),Ee=a("p"),Ut=i("You\u2019ll see the results are much closer now!"),Ts=c(),U(pe.$$.fragment),js=c(),U(L.$$.fragment),Us=c(),K=a("p"),gt=i("Finally, for more complex pipelines such as "),_e=a("a"),Et=i("UnCLIPPipeline"),_t=i(`, these are often extremely
susceptible to precision error propagation. Don\u2019t expect similar results across
different GPU hardware or PyTorch versions. In this case, you\u2019ll need to run
exactly the same hardware and PyTorch version for full reproducibility.`),gs=c(),S=a("h2"),O=a("a"),xe=a("span"),U(ce.$$.fragment),Zt=c(),Ae=a("span"),Gt=i("Deterministic algorithms"),Es=c(),Ze=a("p"),It=i("You can also configure PyTorch to use deterministic algorithms to create a reproducible pipeline. However, you should be aware that deterministic algorithms may be slower than nondeterministic ones and you may observe a decrease in performance. But if reproducibility is important to you, then this is the way to go!"),_s=c(),k=a("p"),Wt=i("Nondeterministic behavior occurs when operations are launched in more than one CUDA stream. To avoid this, set the environment varibale "),ue=a("a"),Ye=a("code"),Bt=i("CUBLAS_WORKSPACE_CONFIG"),$t=i(" to "),ze=a("code"),kt=i(":16:8"),Pt=i(" to only use one buffer size during runtime."),Zs=c(),P=a("p"),Ct=i("PyTorch typically benchmarks multiple algorithms to select the fastest one, but if you want reproducibility, you should disable this feature because the benchmark may select different algorithms each time. Lastly, pass "),Le=a("code"),Rt=i("True"),Nt=i(" to "),de=a("a"),Ke=a("code"),Vt=i("torch.use_deterministic_algorithms"),Xt=i(" to enable deterministic algorithms."),Gs=c(),U(me.$$.fragment),Is=c(),Ge=a("p"),St=i("Now when you run the same pipeline twice, you\u2019ll get identical results."),Ws=c(),U(he.$$.fragment),this.h()},l(e){const l=kl('[data-svelte="svelte-1phssyn"]',document.head);d=n(l,"META",{name:!0,content:!0}),l.forEach(s),J=u(e),h=n(e,"H1",{class:!0});var fe=r(h);f=n(fe,"A",{id:!0,class:!0,href:!0});var Oe=r(f);I=n(Oe,"SPAN",{});var es=r(I);g(M.$$.fragment,es),es.forEach(s),Oe.forEach(s),T=u(fe),v=n(fe,"SPAN",{});var Ht=r(v);W=o(Ht,"Create reproducible pipelines"),Ht.forEach(s),fe.forEach(s),b=u(e),y=n(e,"P",{});var $s=r(y);B=o($s,"Reproducibility is important for testing, replicating results, and can even be used to "),w=n($s,"A",{href:!0});var Ft=r(w);R=o(Ft,"improve image quality"),Ft.forEach(s),ye=o($s,". However, the randomness in diffusion models is a desired property because it allows the pipeline to generate different images every time it is run. While you can\u2019t expect to get the exact same results across platforms, you can expect results to be reproducible across releases and platforms within a certain tolerance range. Even then, tolerance varies depending on the diffusion pipeline and checkpoint."),$s.forEach(s),ee=u(e),be=n(e,"P",{});var Dt=r(be);Qs=o(Dt,"This is why it\u2019s important to understand how to control sources of randomness in diffusion models or use deterministic algorithms."),Dt.forEach(s),ts=u(e),g(H.$$.fragment,e),ls=u(e),N=n(e,"H2",{class:!0});var ks=r(N);F=n(ks,"A",{id:!0,class:!0,href:!0});var qt=r(F);ke=n(qt,"SPAN",{});var xt=r(ke);g(se.$$.fragment,xt),xt.forEach(s),qt.forEach(s),Hs=u(ks),Pe=n(ks,"SPAN",{});var At=r(Pe);Fs=o(At,"Control randomness"),At.forEach(s),ks.forEach(s),as=u(e),we=n(e,"P",{});var Yt=r(we);Ds=o(Yt,`During inference, pipelines rely heavily on random sampling operations which include creating the
Gaussian noise tensors to denoise and adding noise to the scheduling step.`),Yt.forEach(s),ns=u(e),D=n(e,"P",{});var Ps=r(D);qs=o(Ps,"Take a look at the tensor values in the "),Me=n(Ps,"A",{href:!0});var zt=r(Me);xs=o(zt,"DDIMPipeline"),zt.forEach(s),As=o(Ps," after two inference steps:"),Ps.forEach(s),rs=u(e),g(te.$$.fragment,e),is=u(e),Je=n(e,"P",{});var Lt=r(Je);Ys=o(Lt,"Running the code above prints one value, but if you run it again you get a different value. What is going on here?"),Lt.forEach(s),os=u(e),q=n(e,"P",{});var Cs=r(q);zs=o(Cs,"Every time the pipeline is run, "),le=n(Cs,"A",{href:!0,rel:!0});var Kt=r(le);Ce=n(Kt,"CODE",{});var Ot=r(Ce);Ls=o(Ot,"torch.randn"),Ot.forEach(s),Kt.forEach(s),Ks=o(Cs," uses a different random seed to create Gaussian noise which is denoised stepwise. This leads to a different result each time it is run, which is great for diffusion pipelines since it generates a different random image each time."),Cs.forEach(s),ps=u(e),ve=n(e,"P",{});var el=r(ve);Os=o(el,"But if you need to reliably generate the same image, that\u2019ll depend on whether you\u2019re running the pipeline on a CPU or GPU."),el.forEach(s),cs=u(e),V=n(e,"H3",{class:!0});var Rs=r(V);x=n(Rs,"A",{id:!0,class:!0,href:!0});var sl=r(x);Re=n(sl,"SPAN",{});var tl=r(Re);g(ae.$$.fragment,tl),tl.forEach(s),sl.forEach(s),et=u(Rs),Ne=n(Rs,"SPAN",{});var ll=r(Ne);st=o(ll,"CPU"),ll.forEach(s),Rs.forEach(s),us=u(e),A=n(e,"P",{});var Ns=r(A);tt=o(Ns,"To generate reproducible results on a CPU, you\u2019ll need to use a PyTorch "),ne=n(Ns,"A",{href:!0,rel:!0});var al=r(ne);Ve=n(al,"CODE",{});var nl=r(Ve);lt=o(nl,"Generator"),nl.forEach(s),al.forEach(s),at=o(Ns," and set a seed:"),Ns.forEach(s),ds=u(e),g(re.$$.fragment,e),ms=u(e),$=n(e,"P",{});var Ie=r($);nt=o(Ie,"Now when you run the code above, it always prints a value of "),Xe=n(Ie,"CODE",{});var rl=r(Xe);rt=o(rl,"1491.1711"),rl.forEach(s),it=o(Ie," no matter what because the "),Se=n(Ie,"CODE",{});var il=r(Se);ot=o(il,"Generator"),il.forEach(s),pt=o(Ie," object with the seed is passed to all the random functions of the pipeline."),Ie.forEach(s),hs=u(e),Te=n(e,"P",{});var ol=r(Te);ct=o(ol,"If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result."),ol.forEach(s),fs=u(e),g(Y.$$.fragment,e),ys=u(e),X=n(e,"H3",{class:!0});var Vs=r(X);z=n(Vs,"A",{id:!0,class:!0,href:!0});var pl=r(z);Qe=n(pl,"SPAN",{});var cl=r(Qe);g(ie.$$.fragment,cl),cl.forEach(s),pl.forEach(s),ut=u(Vs),He=n(Vs,"SPAN",{});var ul=r(He);dt=o(ul,"GPU"),ul.forEach(s),Vs.forEach(s),bs=u(e),je=n(e,"P",{});var dl=r(je);mt=o(dl,"Writing a reproducible pipeline on a GPU is a bit trickier, and full reproducibility across different hardware is not guaranteed because matrix multiplication - which diffusion pipelines require a lot of - is less deterministic on a GPU than a CPU. For example, if you run the same code example above on a GPU:"),dl.forEach(s),ws=u(e),g(oe.$$.fragment,e),Ms=u(e),Ue=n(e,"P",{});var ml=r(Ue);ht=o(ml,"The result is not the same even though you\u2019re using an identical seed because the GPU uses a different random number generator than the CPU."),ml.forEach(s),Js=u(e),j=n(e,"P",{});var C=r(j);ft=o(C,"To circumvent this problem, \u{1F9E8} Diffusers has a "),ge=n(C,"A",{href:!0});var hl=r(ge);yt=o(hl,"randn_tensor()"),hl.forEach(s),bt=o(C," function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The "),Fe=n(C,"CODE",{});var fl=r(Fe);wt=o(fl,"randn_tensor"),fl.forEach(s),Mt=o(C," function is used everywhere inside the pipeline, allowing the user to "),De=n(C,"STRONG",{});var yl=r(De);Jt=o(yl,"always"),yl.forEach(s),vt=o(C," pass a CPU "),qe=n(C,"CODE",{});var bl=r(qe);Tt=o(bl,"Generator"),bl.forEach(s),jt=o(C," even if the pipeline is run on a GPU."),C.forEach(s),vs=u(e),Ee=n(e,"P",{});var wl=r(Ee);Ut=o(wl,"You\u2019ll see the results are much closer now!"),wl.forEach(s),Ts=u(e),g(pe.$$.fragment,e),js=u(e),g(L.$$.fragment,e),Us=u(e),K=n(e,"P",{});var Xs=r(K);gt=o(Xs,"Finally, for more complex pipelines such as "),_e=n(Xs,"A",{href:!0});var Ml=r(_e);Et=o(Ml,"UnCLIPPipeline"),Ml.forEach(s),_t=o(Xs,`, these are often extremely
susceptible to precision error propagation. Don\u2019t expect similar results across
different GPU hardware or PyTorch versions. In this case, you\u2019ll need to run
exactly the same hardware and PyTorch version for full reproducibility.`),Xs.forEach(s),gs=u(e),S=n(e,"H2",{class:!0});var Ss=r(S);O=n(Ss,"A",{id:!0,class:!0,href:!0});var Jl=r(O);xe=n(Jl,"SPAN",{});var vl=r(xe);g(ce.$$.fragment,vl),vl.forEach(s),Jl.forEach(s),Zt=u(Ss),Ae=n(Ss,"SPAN",{});var Tl=r(Ae);Gt=o(Tl,"Deterministic algorithms"),Tl.forEach(s),Ss.forEach(s),Es=u(e),Ze=n(e,"P",{});var jl=r(Ze);It=o(jl,"You can also configure PyTorch to use deterministic algorithms to create a reproducible pipeline. However, you should be aware that deterministic algorithms may be slower than nondeterministic ones and you may observe a decrease in performance. But if reproducibility is important to you, then this is the way to go!"),jl.forEach(s),_s=u(e),k=n(e,"P",{});var We=r(k);Wt=o(We,"Nondeterministic behavior occurs when operations are launched in more than one CUDA stream. To avoid this, set the environment varibale "),ue=n(We,"A",{href:!0,rel:!0});var Ul=r(ue);Ye=n(Ul,"CODE",{});var gl=r(Ye);Bt=o(gl,"CUBLAS_WORKSPACE_CONFIG"),gl.forEach(s),Ul.forEach(s),$t=o(We," to "),ze=n(We,"CODE",{});var El=r(ze);kt=o(El,":16:8"),El.forEach(s),Pt=o(We," to only use one buffer size during runtime."),We.forEach(s),Zs=u(e),P=n(e,"P",{});var Be=r(P);Ct=o(Be,"PyTorch typically benchmarks multiple algorithms to select the fastest one, but if you want reproducibility, you should disable this feature because the benchmark may select different algorithms each time. Lastly, pass "),Le=n(Be,"CODE",{});var _l=r(Le);Rt=o(_l,"True"),_l.forEach(s),Nt=o(Be," to "),de=n(Be,"A",{href:!0,rel:!0});var Zl=r(de);Ke=n(Zl,"CODE",{});var Gl=r(Ke);Vt=o(Gl,"torch.use_deterministic_algorithms"),Gl.forEach(s),Zl.forEach(s),Xt=o(Be," to enable deterministic algorithms."),Be.forEach(s),Gs=u(e),g(me.$$.fragment,e),Is=u(e),Ge=n(e,"P",{});var Il=r(Ge);St=o(Il,"Now when you run the same pipeline twice, you\u2019ll get identical results."),Il.forEach(s),Ws=u(e),g(he.$$.fragment,e),this.h()},h(){m(d,"name","hf:doc:metadata"),m(d,"content",JSON.stringify(Xl)),m(f,"id","create-reproducible-pipelines"),m(f,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),m(f,"href","#create-reproducible-pipelines"),m(h,"class","relative group"),m(w,"href","reusing_seeds"),m(F,"id","control-randomness"),m(F,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),m(F,"href","#control-randomness"),m(N,"class","relative group"),m(Me,"href","/docs/diffusers/v0.17.0/en/api/pipelines/ddim#diffusers.DDIMPipeline"),m(le,"href","https://pytorch.org/docs/stable/generated/torch.randn.html"),m(le,"rel","nofollow"),m(x,"id","cpu"),m(x,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),m(x,"href","#cpu"),m(V,"class","relative group"),m(ne,"href","https://pytorch.org/docs/stable/generated/torch.randn.html"),m(ne,"rel","nofollow"),m(z,"id","gpu"),m(z,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),m(z,"href","#gpu"),m(X,"class","relative group"),m(ge,"href","/docs/diffusers/v0.17.0/en/api/utilities#diffusers.utils.randn_tensor"),m(_e,"href","/docs/diffusers/v0.17.0/en/api/pipelines/unclip#diffusers.UnCLIPPipeline"),m(O,"id","deterministic-algorithms"),m(O,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),m(O,"href","#deterministic-algorithms"),m(S,"class","relative group"),m(ue,"href","https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility"),m(ue,"rel","nofollow"),m(de,"href","https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html"),m(de,"rel","nofollow")},m(e,l){t(document.head,d),p(e,J,l),p(e,h,l),t(h,f),t(f,I),E(M,I,null),t(h,T),t(h,v),t(v,W),p(e,b,l),p(e,y,l),t(y,B),t(y,w),t(w,R),t(y,ye),p(e,ee,l),p(e,be,l),t(be,Qs),p(e,ts,l),E(H,e,l),p(e,ls,l),p(e,N,l),t(N,F),t(F,ke),E(se,ke,null),t(N,Hs),t(N,Pe),t(Pe,Fs),p(e,as,l),p(e,we,l),t(we,Ds),p(e,ns,l),p(e,D,l),t(D,qs),t(D,Me),t(Me,xs),t(D,As),p(e,rs,l),E(te,e,l),p(e,is,l),p(e,Je,l),t(Je,Ys),p(e,os,l),p(e,q,l),t(q,zs),t(q,le),t(le,Ce),t(Ce,Ls),t(q,Ks),p(e,ps,l),p(e,ve,l),t(ve,Os),p(e,cs,l),p(e,V,l),t(V,x),t(x,Re),E(ae,Re,null),t(V,et),t(V,Ne),t(Ne,st),p(e,us,l),p(e,A,l),t(A,tt),t(A,ne),t(ne,Ve),t(Ve,lt),t(A,at),p(e,ds,l),E(re,e,l),p(e,ms,l),p(e,$,l),t($,nt),t($,Xe),t(Xe,rt),t($,it),t($,Se),t(Se,ot),t($,pt),p(e,hs,l),p(e,Te,l),t(Te,ct),p(e,fs,l),E(Y,e,l),p(e,ys,l),p(e,X,l),t(X,z),t(z,Qe),E(ie,Qe,null),t(X,ut),t(X,He),t(He,dt),p(e,bs,l),p(e,je,l),t(je,mt),p(e,ws,l),E(oe,e,l),p(e,Ms,l),p(e,Ue,l),t(Ue,ht),p(e,Js,l),p(e,j,l),t(j,ft),t(j,ge),t(ge,yt),t(j,bt),t(j,Fe),t(Fe,wt),t(j,Mt),t(j,De),t(De,Jt),t(j,vt),t(j,qe),t(qe,Tt),t(j,jt),p(e,vs,l),p(e,Ee,l),t(Ee,Ut),p(e,Ts,l),E(pe,e,l),p(e,js,l),E(L,e,l),p(e,Us,l),p(e,K,l),t(K,gt),t(K,_e),t(_e,Et),t(K,_t),p(e,gs,l),p(e,S,l),t(S,O),t(O,xe),E(ce,xe,null),t(S,Zt),t(S,Ae),t(Ae,Gt),p(e,Es,l),p(e,Ze,l),t(Ze,It),p(e,_s,l),p(e,k,l),t(k,Wt),t(k,ue),t(ue,Ye),t(Ye,Bt),t(k,$t),t(k,ze),t(ze,kt),t(k,Pt),p(e,Zs,l),p(e,P,l),t(P,Ct),t(P,Le),t(Le,Rt),t(P,Nt),t(P,de),t(de,Ke),t(Ke,Vt),t(P,Xt),p(e,Gs,l),E(me,e,l),p(e,Is,l),p(e,Ge,l),t(Ge,St),p(e,Ws,l),E(he,e,l),Bs=!0},p(e,[l]){const fe={};l&2&&(fe.$$scope={dirty:l,ctx:e}),H.$set(fe);const Oe={};l&2&&(Oe.$$scope={dirty:l,ctx:e}),Y.$set(Oe);const es={};l&2&&(es.$$scope={dirty:l,ctx:e}),L.$set(es)},i(e){Bs||(_(M.$$.fragment,e),_(H.$$.fragment,e),_(se.$$.fragment,e),_(te.$$.fragment,e),_(ae.$$.fragment,e),_(re.$$.fragment,e),_(Y.$$.fragment,e),_(ie.$$.fragment,e),_(oe.$$.fragment,e),_(pe.$$.fragment,e),_(L.$$.fragment,e),_(ce.$$.fragment,e),_(me.$$.fragment,e),_(he.$$.fragment,e),Bs=!0)},o(e){Z(M.$$.fragment,e),Z(H.$$.fragment,e),Z(se.$$.fragment,e),Z(te.$$.fragment,e),Z(ae.$$.fragment,e),Z(re.$$.fragment,e),Z(Y.$$.fragment,e),Z(ie.$$.fragment,e),Z(oe.$$.fragment,e),Z(pe.$$.fragment,e),Z(L.$$.fragment,e),Z(ce.$$.fragment,e),Z(me.$$.fragment,e),Z(he.$$.fragment,e),Bs=!1},d(e){s(d),e&&s(J),e&&s(h),G(M),e&&s(b),e&&s(y),e&&s(ee),e&&s(be),e&&s(ts),G(H,e),e&&s(ls),e&&s(N),G(se),e&&s(as),e&&s(we),e&&s(ns),e&&s(D),e&&s(rs),G(te,e),e&&s(is),e&&s(Je),e&&s(os),e&&s(q),e&&s(ps),e&&s(ve),e&&s(cs),e&&s(V),G(ae),e&&s(us),e&&s(A),e&&s(ds),G(re,e),e&&s(ms),e&&s($),e&&s(hs),e&&s(Te),e&&s(fs),G(Y,e),e&&s(ys),e&&s(X),G(ie),e&&s(bs),e&&s(je),e&&s(ws),G(oe,e),e&&s(Ms),e&&s(Ue),e&&s(Js),e&&s(j),e&&s(vs),e&&s(Ee),e&&s(Ts),G(pe,e),e&&s(js),G(L,e),e&&s(Us),e&&s(K),e&&s(gs),e&&s(S),G(ce),e&&s(Es),e&&s(Ze),e&&s(_s),e&&s(k),e&&s(Zs),e&&s(P),e&&s(Gs),G(me,e),e&&s(Is),e&&s(Ge),e&&s(Ws),G(he,e)}}}const Xl={local:"create-reproducible-pipelines",sections:[{local:"control-randomness",sections:[{local:"cpu",title:"CPU"},{local:"gpu",title:"GPU"}],title:"Control randomness"},{local:"deterministic-algorithms",title:"Deterministic algorithms"}],title:"Create reproducible pipelines"};function Sl(Q){return Pl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ql extends Wl{constructor(d){super();Bl(this,d,Sl,Vl,$l,{})}}export{ql as default,Xl as metadata};
