import{S as $l,i as kl,s as Pl,e as a,k as c,w as T,t as i,M as Cl,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 j,q as U,o as _,B as E,v as Rl}from"../../chunks/vendor-hf-doc-builder.js";import{T as Ft}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";import{D as Nl}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Vl(H){let d,v,h,y,I,M,Z,J,W;return{c(){d=a("p"),v=i("\u{1F4A1} We strongly recommend reading PyTorch\u2019s "),h=a("a"),y=i("statement about reproducibility"),I=i(":"),M=c(),Z=a("blockquote"),J=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 w=r(d);v=o(w,"\u{1F4A1} We strongly recommend reading PyTorch\u2019s "),h=n(w,"A",{href:!0,rel:!0});var B=r(h);y=o(B,"statement about reproducibility"),B.forEach(s),I=o(w,":"),w.forEach(s),M=u(b),Z=n(b,"BLOCKQUOTE",{});var f=r(Z);J=n(f,"P",{});var R=r(J);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),f.forEach(s),this.h()},h(){m(h,"href","https://pytorch.org/docs/stable/notes/randomness.html"),m(h,"rel","nofollow")},m(b,w){p(b,d,w),t(d,v),t(d,h),t(h,y),t(d,I),p(b,M,w),p(b,Z,w),t(Z,J),t(J,W)},d(b){b&&s(d),b&&s(M),b&&s(Z)}}}function Xl(H){let d,v,h,y,I,M,Z,J,W,b,w;return{c(){d=a("p"),v=i("\u{1F4A1} It might be a bit unintuitive at first to pass "),h=a("code"),y=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"),Z=i("Generator"),J=i("\u2019s are "),W=a("em"),b=i("random states"),w=i(` that can be
passed to multiple pipelines in a sequence.`)},l(B){d=n(B,"P",{});var f=r(d);v=o(f,"\u{1F4A1} It might be a bit unintuitive at first to pass "),h=n(f,"CODE",{});var R=r(h);y=o(R,"Generator"),R.forEach(s),I=o(f,` 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(f,"CODE",{});var N=r(M);Z=o(N,"Generator"),N.forEach(s),J=o(f,"\u2019s are "),W=n(f,"EM",{});var ye=r(W);b=o(ye,"random states"),ye.forEach(s),w=o(f,` that can be
passed to multiple pipelines in a sequence.`),f.forEach(s)},m(B,f){p(B,d,f),t(d,v),t(d,h),t(h,y),t(d,I),t(d,M),t(M,Z),t(d,J),t(d,W),t(W,b),t(d,w)},d(B){B&&s(d)}}}function Sl(H){let d,v;return{c(){d=a("p"),v=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 y=r(d);v=o(y,`\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.`),y.forEach(s)},m(h,y){p(h,d,y),t(d,v)},d(h){h&&s(d)}}}function Ql(H){let d,v,h,y,I,M,Z,J,W,b,w,B,f,R,N,ye,Hs,ts,be,Fs,ls,F,as,V,D,ke,se,Ds,Pe,qs,ns,we,xs,rs,q,As,Me,Ys,zs,is,te,os,ve,Ls,ps,x,Ks,le,Ce,Os,et,cs,Je,st,us,X,A,Re,ae,tt,Ne,lt,ds,Y,at,ne,Ve,nt,rt,ms,re,hs,$,it,Xe,ot,pt,Se,ct,ut,fs,Te,dt,ys,z,bs,S,L,Qe,ie,mt,He,ht,ws,ge,ft,Ms,oe,vs,je,yt,Js,G,bt,Ue,wt,Mt,Fe,vt,Jt,De,Tt,gt,qe,jt,Ut,Ts,_e,_t,gs,pe,js,K,Us,O,Et,Ee,Zt,Gt,_s,Q,ee,xe,ce,It,Ae,Wt,Es,Ze,Bt,Zs,k,$t,ue,Ye,kt,Pt,ze,Ct,Rt,Gs,P,Nt,Le,Vt,Xt,de,Ke,St,Qt,Is,me,Ws,Ge,Ht,Bs,he,$s;return M=new ss({}),w=new Nl({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/reproducibility.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/reproducibility.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/reproducibility.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/reproducibility.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/reproducibility.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/reproducibility.ipynb"}]}}),F=new Ft({props:{$$slots:{default:[Vl]},$$scope:{ctx:H}}}),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>())`}}),z=new Ft({props:{$$slots:{default:[Xl]},$$scope:{ctx:H}}}),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>())`}}),K=new Ft({props:{$$slots:{default:[Sl]},$$scope:{ctx:H}}}),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"),v=c(),h=a("h1"),y=a("a"),I=a("span"),T(M.$$.fragment),Z=c(),J=a("span"),W=i("Create reproducible pipelines"),b=c(),T(w.$$.fragment),B=c(),f=a("p"),R=i("Reproducibility is important for testing, replicating results, and can even be used to "),N=a("a"),ye=i("improve image quality"),Hs=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."),ts=c(),be=a("p"),Fs=i("This is why it\u2019s important to understand how to control sources of randomness in diffusion models or use deterministic algorithms."),ls=c(),T(F.$$.fragment),as=c(),V=a("h2"),D=a("a"),ke=a("span"),T(se.$$.fragment),Ds=c(),Pe=a("span"),qs=i("Control randomness"),ns=c(),we=a("p"),xs=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.`),rs=c(),q=a("p"),As=i("Take a look at the tensor values in the "),Me=a("a"),Ys=i("DDIMPipeline"),zs=i(" after two inference steps:"),is=c(),T(te.$$.fragment),os=c(),ve=a("p"),Ls=i("Running the code above prints one value, but if you run it again you get a different value. What is going on here?"),ps=c(),x=a("p"),Ks=i("Every time the pipeline is run, "),le=a("a"),Ce=a("code"),Os=i("torch.randn"),et=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."),cs=c(),Je=a("p"),st=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."),us=c(),X=a("h3"),A=a("a"),Re=a("span"),T(ae.$$.fragment),tt=c(),Ne=a("span"),lt=i("CPU"),ds=c(),Y=a("p"),at=i("To generate reproducible results on a CPU, you\u2019ll need to use a PyTorch "),ne=a("a"),Ve=a("code"),nt=i("Generator"),rt=i(" and set a seed:"),ms=c(),T(re.$$.fragment),hs=c(),$=a("p"),it=i("Now when you run the code above, it always prints a value of "),Xe=a("code"),ot=i("1491.1711"),pt=i(" no matter what because the "),Se=a("code"),ct=i("Generator"),ut=i(" object with the seed is passed to all the random functions of the pipeline."),fs=c(),Te=a("p"),dt=i("If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result."),ys=c(),T(z.$$.fragment),bs=c(),S=a("h3"),L=a("a"),Qe=a("span"),T(ie.$$.fragment),mt=c(),He=a("span"),ht=i("GPU"),ws=c(),ge=a("p"),ft=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:"),Ms=c(),T(oe.$$.fragment),vs=c(),je=a("p"),yt=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(),G=a("p"),bt=i("To circumvent this problem, \u{1F9E8} Diffusers has a "),Ue=a("a"),wt=i("randn_tensor()"),Mt=i(" function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The "),Fe=a("code"),vt=i("randn_tensor"),Jt=i(" function is used everywhere inside the pipeline, allowing the user to "),De=a("strong"),Tt=i("always"),gt=i(" pass a CPU "),qe=a("code"),jt=i("Generator"),Ut=i(" even if the pipeline is run on a GPU."),Ts=c(),_e=a("p"),_t=i("You\u2019ll see the results are much closer now!"),gs=c(),T(pe.$$.fragment),js=c(),T(K.$$.fragment),Us=c(),O=a("p"),Et=i("Finally, for more complex pipelines such as "),Ee=a("a"),Zt=i("UnCLIPPipeline"),Gt=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.`),_s=c(),Q=a("h2"),ee=a("a"),xe=a("span"),T(ce.$$.fragment),It=c(),Ae=a("span"),Wt=i("Deterministic algorithms"),Es=c(),Ze=a("p"),Bt=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!"),Zs=c(),k=a("p"),$t=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"),kt=i("CUBLAS_WORKSPACE_CONFIG"),Pt=i(" to "),ze=a("code"),Ct=i(":16:8"),Rt=i(" to only use one buffer size during runtime."),Gs=c(),P=a("p"),Nt=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"),Vt=i("True"),Xt=i(" to "),de=a("a"),Ke=a("code"),St=i("torch.use_deterministic_algorithms"),Qt=i(" to enable deterministic algorithms."),Is=c(),T(me.$$.fragment),Ws=c(),Ge=a("p"),Ht=i("Now when you run the same pipeline twice, you\u2019ll get identical results."),Bs=c(),T(he.$$.fragment),this.h()},l(e){const l=Cl('[data-svelte="svelte-1phssyn"]',document.head);d=n(l,"META",{name:!0,content:!0}),l.forEach(s),v=u(e),h=n(e,"H1",{class:!0});var fe=r(h);y=n(fe,"A",{id:!0,class:!0,href:!0});var Oe=r(y);I=n(Oe,"SPAN",{});var es=r(I);g(M.$$.fragment,es),es.forEach(s),Oe.forEach(s),Z=u(fe),J=n(fe,"SPAN",{});var Dt=r(J);W=o(Dt,"Create reproducible pipelines"),Dt.forEach(s),fe.forEach(s),b=u(e),g(w.$$.fragment,e),B=u(e),f=n(e,"P",{});var ks=r(f);R=o(ks,"Reproducibility is important for testing, replicating results, and can even be used to "),N=n(ks,"A",{href:!0});var qt=r(N);ye=o(qt,"improve image quality"),qt.forEach(s),Hs=o(ks,". 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."),ks.forEach(s),ts=u(e),be=n(e,"P",{});var xt=r(be);Fs=o(xt,"This is why it\u2019s important to understand how to control sources of randomness in diffusion models or use deterministic algorithms."),xt.forEach(s),ls=u(e),g(F.$$.fragment,e),as=u(e),V=n(e,"H2",{class:!0});var Ps=r(V);D=n(Ps,"A",{id:!0,class:!0,href:!0});var At=r(D);ke=n(At,"SPAN",{});var Yt=r(ke);g(se.$$.fragment,Yt),Yt.forEach(s),At.forEach(s),Ds=u(Ps),Pe=n(Ps,"SPAN",{});var zt=r(Pe);qs=o(zt,"Control randomness"),zt.forEach(s),Ps.forEach(s),ns=u(e),we=n(e,"P",{});var Lt=r(we);xs=o(Lt,`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.`),Lt.forEach(s),rs=u(e),q=n(e,"P",{});var Cs=r(q);As=o(Cs,"Take a look at the tensor values in the "),Me=n(Cs,"A",{href:!0});var Kt=r(Me);Ys=o(Kt,"DDIMPipeline"),Kt.forEach(s),zs=o(Cs," after two inference steps:"),Cs.forEach(s),is=u(e),g(te.$$.fragment,e),os=u(e),ve=n(e,"P",{});var Ot=r(ve);Ls=o(Ot,"Running the code above prints one value, but if you run it again you get a different value. What is going on here?"),Ot.forEach(s),ps=u(e),x=n(e,"P",{});var Rs=r(x);Ks=o(Rs,"Every time the pipeline is run, "),le=n(Rs,"A",{href:!0,rel:!0});var el=r(le);Ce=n(el,"CODE",{});var sl=r(Ce);Os=o(sl,"torch.randn"),sl.forEach(s),el.forEach(s),et=o(Rs," 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."),Rs.forEach(s),cs=u(e),Je=n(e,"P",{});var tl=r(Je);st=o(tl,"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."),tl.forEach(s),us=u(e),X=n(e,"H3",{class:!0});var Ns=r(X);A=n(Ns,"A",{id:!0,class:!0,href:!0});var ll=r(A);Re=n(ll,"SPAN",{});var al=r(Re);g(ae.$$.fragment,al),al.forEach(s),ll.forEach(s),tt=u(Ns),Ne=n(Ns,"SPAN",{});var nl=r(Ne);lt=o(nl,"CPU"),nl.forEach(s),Ns.forEach(s),ds=u(e),Y=n(e,"P",{});var Vs=r(Y);at=o(Vs,"To generate reproducible results on a CPU, you\u2019ll need to use a PyTorch "),ne=n(Vs,"A",{href:!0,rel:!0});var rl=r(ne);Ve=n(rl,"CODE",{});var il=r(Ve);nt=o(il,"Generator"),il.forEach(s),rl.forEach(s),rt=o(Vs," and set a seed:"),Vs.forEach(s),ms=u(e),g(re.$$.fragment,e),hs=u(e),$=n(e,"P",{});var Ie=r($);it=o(Ie,"Now when you run the code above, it always prints a value of "),Xe=n(Ie,"CODE",{});var ol=r(Xe);ot=o(ol,"1491.1711"),ol.forEach(s),pt=o(Ie," no matter what because the "),Se=n(Ie,"CODE",{});var pl=r(Se);ct=o(pl,"Generator"),pl.forEach(s),ut=o(Ie," object with the seed is passed to all the random functions of the pipeline."),Ie.forEach(s),fs=u(e),Te=n(e,"P",{});var cl=r(Te);dt=o(cl,"If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result."),cl.forEach(s),ys=u(e),g(z.$$.fragment,e),bs=u(e),S=n(e,"H3",{class:!0});var Xs=r(S);L=n(Xs,"A",{id:!0,class:!0,href:!0});var ul=r(L);Qe=n(ul,"SPAN",{});var dl=r(Qe);g(ie.$$.fragment,dl),dl.forEach(s),ul.forEach(s),mt=u(Xs),He=n(Xs,"SPAN",{});var ml=r(He);ht=o(ml,"GPU"),ml.forEach(s),Xs.forEach(s),ws=u(e),ge=n(e,"P",{});var hl=r(ge);ft=o(hl,"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:"),hl.forEach(s),Ms=u(e),g(oe.$$.fragment,e),vs=u(e),je=n(e,"P",{});var fl=r(je);yt=o(fl,"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."),fl.forEach(s),Js=u(e),G=n(e,"P",{});var C=r(G);bt=o(C,"To circumvent this problem, \u{1F9E8} Diffusers has a "),Ue=n(C,"A",{href:!0});var yl=r(Ue);wt=o(yl,"randn_tensor()"),yl.forEach(s),Mt=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 bl=r(Fe);vt=o(bl,"randn_tensor"),bl.forEach(s),Jt=o(C," function is used everywhere inside the pipeline, allowing the user to "),De=n(C,"STRONG",{});var wl=r(De);Tt=o(wl,"always"),wl.forEach(s),gt=o(C," pass a CPU "),qe=n(C,"CODE",{});var Ml=r(qe);jt=o(Ml,"Generator"),Ml.forEach(s),Ut=o(C," even if the pipeline is run on a GPU."),C.forEach(s),Ts=u(e),_e=n(e,"P",{});var vl=r(_e);_t=o(vl,"You\u2019ll see the results are much closer now!"),vl.forEach(s),gs=u(e),g(pe.$$.fragment,e),js=u(e),g(K.$$.fragment,e),Us=u(e),O=n(e,"P",{});var Ss=r(O);Et=o(Ss,"Finally, for more complex pipelines such as "),Ee=n(Ss,"A",{href:!0});var Jl=r(Ee);Zt=o(Jl,"UnCLIPPipeline"),Jl.forEach(s),Gt=o(Ss,`, 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.`),Ss.forEach(s),_s=u(e),Q=n(e,"H2",{class:!0});var Qs=r(Q);ee=n(Qs,"A",{id:!0,class:!0,href:!0});var Tl=r(ee);xe=n(Tl,"SPAN",{});var gl=r(xe);g(ce.$$.fragment,gl),gl.forEach(s),Tl.forEach(s),It=u(Qs),Ae=n(Qs,"SPAN",{});var jl=r(Ae);Wt=o(jl,"Deterministic algorithms"),jl.forEach(s),Qs.forEach(s),Es=u(e),Ze=n(e,"P",{});var Ul=r(Ze);Bt=o(Ul,"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!"),Ul.forEach(s),Zs=u(e),k=n(e,"P",{});var We=r(k);$t=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 _l=r(ue);Ye=n(_l,"CODE",{});var El=r(Ye);kt=o(El,"CUBLAS_WORKSPACE_CONFIG"),El.forEach(s),_l.forEach(s),Pt=o(We," to "),ze=n(We,"CODE",{});var Zl=r(ze);Ct=o(Zl,":16:8"),Zl.forEach(s),Rt=o(We," to only use one buffer size during runtime."),We.forEach(s),Gs=u(e),P=n(e,"P",{});var Be=r(P);Nt=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 Gl=r(Le);Vt=o(Gl,"True"),Gl.forEach(s),Xt=o(Be," to "),de=n(Be,"A",{href:!0,rel:!0});var Il=r(de);Ke=n(Il,"CODE",{});var Wl=r(Ke);St=o(Wl,"torch.use_deterministic_algorithms"),Wl.forEach(s),Il.forEach(s),Qt=o(Be," to enable deterministic algorithms."),Be.forEach(s),Is=u(e),g(me.$$.fragment,e),Ws=u(e),Ge=n(e,"P",{});var Bl=r(Ge);Ht=o(Bl,"Now when you run the same pipeline twice, you\u2019ll get identical results."),Bl.forEach(s),Bs=u(e),g(he.$$.fragment,e),this.h()},h(){m(d,"name","hf:doc:metadata"),m(d,"content",JSON.stringify(Hl)),m(y,"id","create-reproducible-pipelines"),m(y,"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(y,"href","#create-reproducible-pipelines"),m(h,"class","relative group"),m(N,"href","reusing_seeds"),m(D,"id","control-randomness"),m(D,"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(D,"href","#control-randomness"),m(V,"class","relative group"),m(Me,"href","/docs/diffusers/v0.19.0/en/api/pipelines/ddim#diffusers.DDIMPipeline"),m(le,"href","https://pytorch.org/docs/stable/generated/torch.randn.html"),m(le,"rel","nofollow"),m(A,"id","cpu"),m(A,"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(A,"href","#cpu"),m(X,"class","relative group"),m(ne,"href","https://pytorch.org/docs/stable/generated/torch.randn.html"),m(ne,"rel","nofollow"),m(L,"id","gpu"),m(L,"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(L,"href","#gpu"),m(S,"class","relative group"),m(Ue,"href","/docs/diffusers/v0.19.0/en/api/utilities#diffusers.utils.randn_tensor"),m(Ee,"href","/docs/diffusers/v0.19.0/en/api/pipelines/unclip#diffusers.UnCLIPPipeline"),m(ee,"id","deterministic-algorithms"),m(ee,"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(ee,"href","#deterministic-algorithms"),m(Q,"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,v,l),p(e,h,l),t(h,y),t(y,I),j(M,I,null),t(h,Z),t(h,J),t(J,W),p(e,b,l),j(w,e,l),p(e,B,l),p(e,f,l),t(f,R),t(f,N),t(N,ye),t(f,Hs),p(e,ts,l),p(e,be,l),t(be,Fs),p(e,ls,l),j(F,e,l),p(e,as,l),p(e,V,l),t(V,D),t(D,ke),j(se,ke,null),t(V,Ds),t(V,Pe),t(Pe,qs),p(e,ns,l),p(e,we,l),t(we,xs),p(e,rs,l),p(e,q,l),t(q,As),t(q,Me),t(Me,Ys),t(q,zs),p(e,is,l),j(te,e,l),p(e,os,l),p(e,ve,l),t(ve,Ls),p(e,ps,l),p(e,x,l),t(x,Ks),t(x,le),t(le,Ce),t(Ce,Os),t(x,et),p(e,cs,l),p(e,Je,l),t(Je,st),p(e,us,l),p(e,X,l),t(X,A),t(A,Re),j(ae,Re,null),t(X,tt),t(X,Ne),t(Ne,lt),p(e,ds,l),p(e,Y,l),t(Y,at),t(Y,ne),t(ne,Ve),t(Ve,nt),t(Y,rt),p(e,ms,l),j(re,e,l),p(e,hs,l),p(e,$,l),t($,it),t($,Xe),t(Xe,ot),t($,pt),t($,Se),t(Se,ct),t($,ut),p(e,fs,l),p(e,Te,l),t(Te,dt),p(e,ys,l),j(z,e,l),p(e,bs,l),p(e,S,l),t(S,L),t(L,Qe),j(ie,Qe,null),t(S,mt),t(S,He),t(He,ht),p(e,ws,l),p(e,ge,l),t(ge,ft),p(e,Ms,l),j(oe,e,l),p(e,vs,l),p(e,je,l),t(je,yt),p(e,Js,l),p(e,G,l),t(G,bt),t(G,Ue),t(Ue,wt),t(G,Mt),t(G,Fe),t(Fe,vt),t(G,Jt),t(G,De),t(De,Tt),t(G,gt),t(G,qe),t(qe,jt),t(G,Ut),p(e,Ts,l),p(e,_e,l),t(_e,_t),p(e,gs,l),j(pe,e,l),p(e,js,l),j(K,e,l),p(e,Us,l),p(e,O,l),t(O,Et),t(O,Ee),t(Ee,Zt),t(O,Gt),p(e,_s,l),p(e,Q,l),t(Q,ee),t(ee,xe),j(ce,xe,null),t(Q,It),t(Q,Ae),t(Ae,Wt),p(e,Es,l),p(e,Ze,l),t(Ze,Bt),p(e,Zs,l),p(e,k,l),t(k,$t),t(k,ue),t(ue,Ye),t(Ye,kt),t(k,Pt),t(k,ze),t(ze,Ct),t(k,Rt),p(e,Gs,l),p(e,P,l),t(P,Nt),t(P,Le),t(Le,Vt),t(P,Xt),t(P,de),t(de,Ke),t(Ke,St),t(P,Qt),p(e,Is,l),j(me,e,l),p(e,Ws,l),p(e,Ge,l),t(Ge,Ht),p(e,Bs,l),j(he,e,l),$s=!0},p(e,[l]){const fe={};l&2&&(fe.$$scope={dirty:l,ctx:e}),F.$set(fe);const Oe={};l&2&&(Oe.$$scope={dirty:l,ctx:e}),z.$set(Oe);const es={};l&2&&(es.$$scope={dirty:l,ctx:e}),K.$set(es)},i(e){$s||(U(M.$$.fragment,e),U(w.$$.fragment,e),U(F.$$.fragment,e),U(se.$$.fragment,e),U(te.$$.fragment,e),U(ae.$$.fragment,e),U(re.$$.fragment,e),U(z.$$.fragment,e),U(ie.$$.fragment,e),U(oe.$$.fragment,e),U(pe.$$.fragment,e),U(K.$$.fragment,e),U(ce.$$.fragment,e),U(me.$$.fragment,e),U(he.$$.fragment,e),$s=!0)},o(e){_(M.$$.fragment,e),_(w.$$.fragment,e),_(F.$$.fragment,e),_(se.$$.fragment,e),_(te.$$.fragment,e),_(ae.$$.fragment,e),_(re.$$.fragment,e),_(z.$$.fragment,e),_(ie.$$.fragment,e),_(oe.$$.fragment,e),_(pe.$$.fragment,e),_(K.$$.fragment,e),_(ce.$$.fragment,e),_(me.$$.fragment,e),_(he.$$.fragment,e),$s=!1},d(e){s(d),e&&s(v),e&&s(h),E(M),e&&s(b),E(w,e),e&&s(B),e&&s(f),e&&s(ts),e&&s(be),e&&s(ls),E(F,e),e&&s(as),e&&s(V),E(se),e&&s(ns),e&&s(we),e&&s(rs),e&&s(q),e&&s(is),E(te,e),e&&s(os),e&&s(ve),e&&s(ps),e&&s(x),e&&s(cs),e&&s(Je),e&&s(us),e&&s(X),E(ae),e&&s(ds),e&&s(Y),e&&s(ms),E(re,e),e&&s(hs),e&&s($),e&&s(fs),e&&s(Te),e&&s(ys),E(z,e),e&&s(bs),e&&s(S),E(ie),e&&s(ws),e&&s(ge),e&&s(Ms),E(oe,e),e&&s(vs),e&&s(je),e&&s(Js),e&&s(G),e&&s(Ts),e&&s(_e),e&&s(gs),E(pe,e),e&&s(js),E(K,e),e&&s(Us),e&&s(O),e&&s(_s),e&&s(Q),E(ce),e&&s(Es),e&&s(Ze),e&&s(Zs),e&&s(k),e&&s(Gs),e&&s(P),e&&s(Is),E(me,e),e&&s(Ws),e&&s(Ge),e&&s(Bs),E(he,e)}}}const Hl={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 Fl(H){return Rl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class zl extends $l{constructor(d){super();kl(this,d,Fl,Ql,Pl,{})}}export{zl as default,Hl as metadata};
