Buckets:

hf-doc-build/doc / diffusers /v0.16.0 /en /_app /pages /using-diffusers /reproducibility.mdx-hf-doc-builder.js
rtrm's picture
download
raw
34.5 kB
import{S as ln,i as pn,s as un,e as n,k as u,w,t as o,M as dn,c as a,d as t,m as d,a as i,x as $,h as l,b as c,G as s,g as p,y as P,q as E,o as j,B as D,v as cn}from"../../chunks/vendor-hf-doc-builder.js";import{T as cr}from"../../chunks/Tip-hf-doc-builder.js";import{D as mn}from"../../chunks/Docstring-hf-doc-builder.js";import{C as Le}from"../../chunks/CodeBlock-hf-doc-builder.js";import{I as Fe}from"../../chunks/IconCopyLink-hf-doc-builder.js";function hn(H){let m,b,h,f,q,v,C,k,G;return{c(){m=n("p"),b=o("\u{1F4A1} We strongly recommend reading PyTorch\u2019s "),h=n("a"),f=o("statement about reproducibility"),q=o(":"),v=u(),C=n("blockquote"),k=n("p"),G=o("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(g){m=a(g,"P",{});var y=i(m);b=l(y,"\u{1F4A1} We strongly recommend reading PyTorch\u2019s "),h=a(y,"A",{href:!0,rel:!0});var T=i(h);f=l(T,"statement about reproducibility"),T.forEach(t),q=l(y,":"),y.forEach(t),v=d(g),C=a(g,"BLOCKQUOTE",{});var _=i(C);k=a(_,"P",{});var O=i(k);G=l(O,"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."),O.forEach(t),_.forEach(t),this.h()},h(){c(h,"href","https://pytorch.org/docs/stable/notes/randomness.html"),c(h,"rel","nofollow")},m(g,y){p(g,m,y),s(m,b),s(m,h),s(h,f),s(m,q),p(g,v,y),p(g,C,y),s(C,k),s(k,G)},d(g){g&&t(m),g&&t(v),g&&t(C)}}}function fn(H){let m,b,h,f,q,v,C,k,G,g,y;return{c(){m=n("p"),b=o("\u{1F4A1} It might be a bit unintuitive at first to pass "),h=n("code"),f=o("Generator"),q=o(` 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 `),v=n("code"),C=o("Generator"),k=o("\u2019s are "),G=n("em"),g=o("random states"),y=o(` that can be
passed to multiple pipelines in a sequence.`)},l(T){m=a(T,"P",{});var _=i(m);b=l(_,"\u{1F4A1} It might be a bit unintuitive at first to pass "),h=a(_,"CODE",{});var O=i(h);f=l(O,"Generator"),O.forEach(t),q=l(_,` 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 `),v=a(_,"CODE",{});var $e=i(v);C=l($e,"Generator"),$e.forEach(t),k=l(_,"\u2019s are "),G=a(_,"EM",{});var ne=i(G);g=l(ne,"random states"),ne.forEach(t),y=l(_,` that can be
passed to multiple pipelines in a sequence.`),_.forEach(t)},m(T,_){p(T,m,_),s(m,b),s(m,h),s(h,f),s(m,q),s(m,v),s(v,C),s(m,k),s(m,G),s(G,g),s(m,y)},d(T){T&&t(m)}}}function yn(H){let m,b;return{c(){m=n("p"),b=o(`\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){m=a(h,"P",{});var f=i(m);b=l(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(t)},m(h,f){p(h,m,f),s(m,b)},d(h){h&&t(m)}}}function gn(H){let m,b,h,f,q,v,C,k,G,g,y,T,_,O,$e,ne,Pe,ns,ht,K,ft,N,z,Re,ae,as,We,is,yt,Ee,os,gt,Y,ls,je,ps,us,_t,ie,vt,De,ds,bt,J,cs,oe,Be,ms,hs,wt,ke,fs,$t,M,Q,He,le,ys,Ke,gs,Pt,V,_s,pe,ze,vs,bs,Et,ue,jt,I,ws,Ye,$s,Ps,Je,Es,js,Dt,Ce,Ds,kt,X,Ct,L,Z,Qe,de,ks,Ve,Cs,Ut,Ue,Us,qt,ce,Gt,qe,qs,Tt,U,Gs,Ge,Xe,Ts,Is,Ze,xs,As,et,Ss,Os,tt,Ns,Ms,It,Te,Ls,xt,me,At,ee,St,te,Fs,Ie,Rs,Ws,Ot,F,se,st,he,Bs,rt,Hs,Nt,R,fe,Ks,W,zs,nt,Ys,Js,at,Qs,Vs,Mt,B,re,it,ye,Xs,ot,Zs,Lt,xe,er,Ft,x,tr,ge,lt,sr,rr,pt,nr,ar,Rt,A,ir,ut,or,lr,_e,dt,pr,ur,Wt,ve,Bt,Ae,dr,Ht,be,Kt;return v=new Fe({}),K=new cr({props:{$$slots:{default:[hn]},$$scope:{ctx:H}}}),ae=new Fe({}),ie=new Le({props:{code:`from diffusers import DDIMPipeline
import numpy as np
model_id = "google/ddpm-cifar10-32"
# load model and scheduler
ddim = DDIMPipeline.from_pretrained(model_id)
# run pipeline for just two steps and return numpy tensor
image = ddim(num_inference_steps=2, output_type="np").images
print(np.abs(image).sum())`,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>())`}}),le=new Fe({}),ue=new Le({props:{code:`import torch
from diffusers import DDIMPipeline
import numpy as np
model_id = "google/ddpm-cifar10-32"
# load model and scheduler
ddim = DDIMPipeline.from_pretrained(model_id)
# create a generator for reproducibility
generator = torch.Generator(device="cpu").manual_seed(0)
# run pipeline for just two steps and return numpy tensor
image = ddim(num_inference_steps=2, output_type="np", generator=generator).images
print(np.abs(image).sum())`,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>())`}}),X=new cr({props:{$$slots:{default:[fn]},$$scope:{ctx:H}}}),de=new Fe({}),ce=new Le({props:{code:`import torch
from diffusers import DDIMPipeline
import numpy as np
model_id = "google/ddpm-cifar10-32"
# load model and scheduler
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to("cuda")
# create a generator for reproducibility
generator = torch.Generator(device="cuda").manual_seed(0)
# run pipeline for just two steps and return numpy tensor
image = ddim(num_inference_steps=2, output_type="np", generator=generator).images
print(np.abs(image).sum())`,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>())`}}),me=new Le({props:{code:`import torch
from diffusers import DDIMPipeline
import numpy as np
model_id = "google/ddpm-cifar10-32"
# load model and scheduler
ddim = DDIMPipeline.from_pretrained(model_id)
ddim.to("cuda")
# create a generator for reproducibility; notice you don't place it on the GPU!
generator = torch.manual_seed(0)
# run pipeline for just two steps and return numpy tensor
image = ddim(num_inference_steps=2, output_type="np", generator=generator).images
print(np.abs(image).sum())`,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>())`}}),ee=new cr({props:{$$slots:{default:[yn]},$$scope:{ctx:H}}}),he=new Fe({}),fe=new mn({props:{name:"diffusers.utils.randn_tensor",anchor:"diffusers.utils.randn_tensor",parameters:[{name:"shape",val:": typing.Union[typing.Tuple, typing.List]"},{name:"generator",val:": typing.Union[typing.List[ForwardRef('torch.Generator')], ForwardRef('torch.Generator'), NoneType] = None"},{name:"device",val:": typing.Optional[ForwardRef('torch.device')] = None"},{name:"dtype",val:": typing.Optional[ForwardRef('torch.dtype')] = None"},{name:"layout",val:": typing.Optional[ForwardRef('torch.layout')] = None"}],source:"https://github.com/huggingface/diffusers/blob/v0.16.0/src/diffusers/utils/torch_utils.py#L29"}}),ye=new Fe({}),ve=new Le({props:{code:`import os
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"
torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True)`,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>)`}}),be=new Le({props:{code:`import torch
from diffusers import DDIMScheduler, StableDiffusionPipeline
import numpy as np
model_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id).to("cuda")
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
g = torch.Generator(device="cuda")
prompt = "A bear is playing a guitar on Times Square"
g.manual_seed(0)
result1 = pipe(prompt=prompt, num_inference_steps=50, generator=g, output_type="latent").images
g.manual_seed(0)
result2 = pipe(prompt=prompt, num_inference_steps=50, generator=g, output_type="latent").images
print("L_inf dist = ", abs(result1 - result2).max())
"L_inf dist = tensor(0., device='cuda:0')"`,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(){m=n("meta"),b=u(),h=n("h1"),f=n("a"),q=n("span"),w(v.$$.fragment),C=u(),k=n("span"),G=o("Create reproducible pipelines"),g=u(),y=n("p"),T=o("Reproducibility is important for testing, replicating results, and can even be used to "),_=n("a"),O=o("improve image quality"),$e=o(". 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."),ne=u(),Pe=n("p"),ns=o("This is why it\u2019s important to understand how to control sources of randomness in diffusion models or use deterministic algorithms."),ht=u(),w(K.$$.fragment),ft=u(),N=n("h2"),z=n("a"),Re=n("span"),w(ae.$$.fragment),as=u(),We=n("span"),is=o("Control randomness"),yt=u(),Ee=n("p"),os=o(`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.`),gt=u(),Y=n("p"),ls=o("Take a look at the tensor values in the "),je=n("a"),ps=o("DDIMPipeline"),us=o(" after two inference steps:"),_t=u(),w(ie.$$.fragment),vt=u(),De=n("p"),ds=o("Running the code above prints one value, but if you run it again you get a different value. What is going on here?"),bt=u(),J=n("p"),cs=o("Every time the pipeline is run, "),oe=n("a"),Be=n("code"),ms=o("torch.randn"),hs=o(" 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."),wt=u(),ke=n("p"),fs=o("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."),$t=u(),M=n("h3"),Q=n("a"),He=n("span"),w(le.$$.fragment),ys=u(),Ke=n("span"),gs=o("CPU"),Pt=u(),V=n("p"),_s=o("To generate reproducible results on a CPU, you\u2019ll need to use a PyTorch "),pe=n("a"),ze=n("code"),vs=o("Generator"),bs=o(" and set a seed:"),Et=u(),w(ue.$$.fragment),jt=u(),I=n("p"),ws=o("Now when you run the code above, it always prints a value of "),Ye=n("code"),$s=o("1491.1711"),Ps=o(" no matter what because the "),Je=n("code"),Es=o("Generator"),js=o(" object with the seed is passed to all the random functions of the pipeline."),Dt=u(),Ce=n("p"),Ds=o("If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result."),kt=u(),w(X.$$.fragment),Ct=u(),L=n("h3"),Z=n("a"),Qe=n("span"),w(de.$$.fragment),ks=u(),Ve=n("span"),Cs=o("GPU"),Ut=u(),Ue=n("p"),Us=o("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:"),qt=u(),w(ce.$$.fragment),Gt=u(),qe=n("p"),qs=o("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."),Tt=u(),U=n("p"),Gs=o("To circumvent this problem, \u{1F9E8} Diffusers has a "),Ge=n("a"),Xe=n("code"),Ts=o("randn_tensor"),Is=o(" function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The "),Ze=n("code"),xs=o("randn_tensor"),As=o(" function is used everywhere inside the pipeline, allowing the user to "),et=n("strong"),Ss=o("always"),Os=o(" pass a CPU "),tt=n("code"),Ns=o("Generator"),Ms=o(" even if the pipeline is run on a GPU."),It=u(),Te=n("p"),Ls=o("You\u2019ll see the results are much closer now!"),xt=u(),w(me.$$.fragment),At=u(),w(ee.$$.fragment),St=u(),te=n("p"),Fs=o("Finally, for more complex pipelines such as "),Ie=n("a"),Rs=o("UnCLIPPipeline"),Ws=o(`, 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.`),Ot=u(),F=n("h3"),se=n("a"),st=n("span"),w(he.$$.fragment),Bs=u(),rt=n("span"),Hs=o("randn_tensor"),Nt=u(),R=n("div"),w(fe.$$.fragment),Ks=u(),W=n("p"),zs=o("This is a helper function that allows to create random tensors on the desired "),nt=n("code"),Ys=o("device"),Js=o(" with the desired "),at=n("code"),Qs=o("dtype"),Vs=o(`. When
passing a list of generators one can seed each batched size individually. If CPU generators are passed the tensor
will always be created on CPU.`),Mt=u(),B=n("h2"),re=n("a"),it=n("span"),w(ye.$$.fragment),Xs=u(),ot=n("span"),Zs=o("Deterministic algorithms"),Lt=u(),xe=n("p"),er=o("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!"),Ft=u(),x=n("p"),tr=o("Nondeterministic behavior occurs when operations are launched in more than one CUDA stream. To avoid this, set the environment varibale "),ge=n("a"),lt=n("code"),sr=o("CUBLAS_WORKSPACE_CONFIG"),rr=o(" to "),pt=n("code"),nr=o(":16:8"),ar=o(" to only use one buffer size during runtime."),Rt=u(),A=n("p"),ir=o("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 "),ut=n("code"),or=o("True"),lr=o(" to "),_e=n("a"),dt=n("code"),pr=o("torch.use_deterministic_algorithms"),ur=o(" to enable deterministic algorithms."),Wt=u(),w(ve.$$.fragment),Bt=u(),Ae=n("p"),dr=o("Now when you run the same pipeline twice, you\u2019ll get identical results."),Ht=u(),w(be.$$.fragment),this.h()},l(e){const r=dn('[data-svelte="svelte-1phssyn"]',document.head);m=a(r,"META",{name:!0,content:!0}),r.forEach(t),b=d(e),h=a(e,"H1",{class:!0});var we=i(h);f=a(we,"A",{id:!0,class:!0,href:!0});var ct=i(f);q=a(ct,"SPAN",{});var mt=i(q);$(v.$$.fragment,mt),mt.forEach(t),ct.forEach(t),C=d(we),k=a(we,"SPAN",{});var mr=i(k);G=l(mr,"Create reproducible pipelines"),mr.forEach(t),we.forEach(t),g=d(e),y=a(e,"P",{});var zt=i(y);T=l(zt,"Reproducibility is important for testing, replicating results, and can even be used to "),_=a(zt,"A",{href:!0});var hr=i(_);O=l(hr,"improve image quality"),hr.forEach(t),$e=l(zt,". 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."),zt.forEach(t),ne=d(e),Pe=a(e,"P",{});var fr=i(Pe);ns=l(fr,"This is why it\u2019s important to understand how to control sources of randomness in diffusion models or use deterministic algorithms."),fr.forEach(t),ht=d(e),$(K.$$.fragment,e),ft=d(e),N=a(e,"H2",{class:!0});var Yt=i(N);z=a(Yt,"A",{id:!0,class:!0,href:!0});var yr=i(z);Re=a(yr,"SPAN",{});var gr=i(Re);$(ae.$$.fragment,gr),gr.forEach(t),yr.forEach(t),as=d(Yt),We=a(Yt,"SPAN",{});var _r=i(We);is=l(_r,"Control randomness"),_r.forEach(t),Yt.forEach(t),yt=d(e),Ee=a(e,"P",{});var vr=i(Ee);os=l(vr,`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.`),vr.forEach(t),gt=d(e),Y=a(e,"P",{});var Jt=i(Y);ls=l(Jt,"Take a look at the tensor values in the "),je=a(Jt,"A",{href:!0});var br=i(je);ps=l(br,"DDIMPipeline"),br.forEach(t),us=l(Jt," after two inference steps:"),Jt.forEach(t),_t=d(e),$(ie.$$.fragment,e),vt=d(e),De=a(e,"P",{});var wr=i(De);ds=l(wr,"Running the code above prints one value, but if you run it again you get a different value. What is going on here?"),wr.forEach(t),bt=d(e),J=a(e,"P",{});var Qt=i(J);cs=l(Qt,"Every time the pipeline is run, "),oe=a(Qt,"A",{href:!0,rel:!0});var $r=i(oe);Be=a($r,"CODE",{});var Pr=i(Be);ms=l(Pr,"torch.randn"),Pr.forEach(t),$r.forEach(t),hs=l(Qt," 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."),Qt.forEach(t),wt=d(e),ke=a(e,"P",{});var Er=i(ke);fs=l(Er,"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."),Er.forEach(t),$t=d(e),M=a(e,"H3",{class:!0});var Vt=i(M);Q=a(Vt,"A",{id:!0,class:!0,href:!0});var jr=i(Q);He=a(jr,"SPAN",{});var Dr=i(He);$(le.$$.fragment,Dr),Dr.forEach(t),jr.forEach(t),ys=d(Vt),Ke=a(Vt,"SPAN",{});var kr=i(Ke);gs=l(kr,"CPU"),kr.forEach(t),Vt.forEach(t),Pt=d(e),V=a(e,"P",{});var Xt=i(V);_s=l(Xt,"To generate reproducible results on a CPU, you\u2019ll need to use a PyTorch "),pe=a(Xt,"A",{href:!0,rel:!0});var Cr=i(pe);ze=a(Cr,"CODE",{});var Ur=i(ze);vs=l(Ur,"Generator"),Ur.forEach(t),Cr.forEach(t),bs=l(Xt," and set a seed:"),Xt.forEach(t),Et=d(e),$(ue.$$.fragment,e),jt=d(e),I=a(e,"P",{});var Se=i(I);ws=l(Se,"Now when you run the code above, it always prints a value of "),Ye=a(Se,"CODE",{});var qr=i(Ye);$s=l(qr,"1491.1711"),qr.forEach(t),Ps=l(Se," no matter what because the "),Je=a(Se,"CODE",{});var Gr=i(Je);Es=l(Gr,"Generator"),Gr.forEach(t),js=l(Se," object with the seed is passed to all the random functions of the pipeline."),Se.forEach(t),Dt=d(e),Ce=a(e,"P",{});var Tr=i(Ce);Ds=l(Tr,"If you run this code example on your specific hardware and PyTorch version, you should get a similar, if not the same, result."),Tr.forEach(t),kt=d(e),$(X.$$.fragment,e),Ct=d(e),L=a(e,"H3",{class:!0});var Zt=i(L);Z=a(Zt,"A",{id:!0,class:!0,href:!0});var Ir=i(Z);Qe=a(Ir,"SPAN",{});var xr=i(Qe);$(de.$$.fragment,xr),xr.forEach(t),Ir.forEach(t),ks=d(Zt),Ve=a(Zt,"SPAN",{});var Ar=i(Ve);Cs=l(Ar,"GPU"),Ar.forEach(t),Zt.forEach(t),Ut=d(e),Ue=a(e,"P",{});var Sr=i(Ue);Us=l(Sr,"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:"),Sr.forEach(t),qt=d(e),$(ce.$$.fragment,e),Gt=d(e),qe=a(e,"P",{});var Or=i(qe);qs=l(Or,"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."),Or.forEach(t),Tt=d(e),U=a(e,"P",{});var S=i(U);Gs=l(S,"To circumvent this problem, \u{1F9E8} Diffusers has a "),Ge=a(S,"A",{href:!0});var Nr=i(Ge);Xe=a(Nr,"CODE",{});var Mr=i(Xe);Ts=l(Mr,"randn_tensor"),Mr.forEach(t),Nr.forEach(t),Is=l(S," function for creating random noise on the CPU, and then moving the tensor to a GPU if necessary. The "),Ze=a(S,"CODE",{});var Lr=i(Ze);xs=l(Lr,"randn_tensor"),Lr.forEach(t),As=l(S," function is used everywhere inside the pipeline, allowing the user to "),et=a(S,"STRONG",{});var Fr=i(et);Ss=l(Fr,"always"),Fr.forEach(t),Os=l(S," pass a CPU "),tt=a(S,"CODE",{});var Rr=i(tt);Ns=l(Rr,"Generator"),Rr.forEach(t),Ms=l(S," even if the pipeline is run on a GPU."),S.forEach(t),It=d(e),Te=a(e,"P",{});var Wr=i(Te);Ls=l(Wr,"You\u2019ll see the results are much closer now!"),Wr.forEach(t),xt=d(e),$(me.$$.fragment,e),At=d(e),$(ee.$$.fragment,e),St=d(e),te=a(e,"P",{});var es=i(te);Fs=l(es,"Finally, for more complex pipelines such as "),Ie=a(es,"A",{href:!0});var Br=i(Ie);Rs=l(Br,"UnCLIPPipeline"),Br.forEach(t),Ws=l(es,`, 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.`),es.forEach(t),Ot=d(e),F=a(e,"H3",{class:!0});var ts=i(F);se=a(ts,"A",{id:!0,class:!0,href:!0});var Hr=i(se);st=a(Hr,"SPAN",{});var Kr=i(st);$(he.$$.fragment,Kr),Kr.forEach(t),Hr.forEach(t),Bs=d(ts),rt=a(ts,"SPAN",{});var zr=i(rt);Hs=l(zr,"randn_tensor"),zr.forEach(t),ts.forEach(t),Nt=d(e),R=a(e,"DIV",{class:!0});var ss=i(R);$(fe.$$.fragment,ss),Ks=d(ss),W=a(ss,"P",{});var Oe=i(W);zs=l(Oe,"This is a helper function that allows to create random tensors on the desired "),nt=a(Oe,"CODE",{});var Yr=i(nt);Ys=l(Yr,"device"),Yr.forEach(t),Js=l(Oe," with the desired "),at=a(Oe,"CODE",{});var Jr=i(at);Qs=l(Jr,"dtype"),Jr.forEach(t),Vs=l(Oe,`. When
passing a list of generators one can seed each batched size individually. If CPU generators are passed the tensor
will always be created on CPU.`),Oe.forEach(t),ss.forEach(t),Mt=d(e),B=a(e,"H2",{class:!0});var rs=i(B);re=a(rs,"A",{id:!0,class:!0,href:!0});var Qr=i(re);it=a(Qr,"SPAN",{});var Vr=i(it);$(ye.$$.fragment,Vr),Vr.forEach(t),Qr.forEach(t),Xs=d(rs),ot=a(rs,"SPAN",{});var Xr=i(ot);Zs=l(Xr,"Deterministic algorithms"),Xr.forEach(t),rs.forEach(t),Lt=d(e),xe=a(e,"P",{});var Zr=i(xe);er=l(Zr,"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!"),Zr.forEach(t),Ft=d(e),x=a(e,"P",{});var Ne=i(x);tr=l(Ne,"Nondeterministic behavior occurs when operations are launched in more than one CUDA stream. To avoid this, set the environment varibale "),ge=a(Ne,"A",{href:!0,rel:!0});var en=i(ge);lt=a(en,"CODE",{});var tn=i(lt);sr=l(tn,"CUBLAS_WORKSPACE_CONFIG"),tn.forEach(t),en.forEach(t),rr=l(Ne," to "),pt=a(Ne,"CODE",{});var sn=i(pt);nr=l(sn,":16:8"),sn.forEach(t),ar=l(Ne," to only use one buffer size during runtime."),Ne.forEach(t),Rt=d(e),A=a(e,"P",{});var Me=i(A);ir=l(Me,"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 "),ut=a(Me,"CODE",{});var rn=i(ut);or=l(rn,"True"),rn.forEach(t),lr=l(Me," to "),_e=a(Me,"A",{href:!0,rel:!0});var nn=i(_e);dt=a(nn,"CODE",{});var an=i(dt);pr=l(an,"torch.use_deterministic_algorithms"),an.forEach(t),nn.forEach(t),ur=l(Me," to enable deterministic algorithms."),Me.forEach(t),Wt=d(e),$(ve.$$.fragment,e),Bt=d(e),Ae=a(e,"P",{});var on=i(Ae);dr=l(on,"Now when you run the same pipeline twice, you\u2019ll get identical results."),on.forEach(t),Ht=d(e),$(be.$$.fragment,e),this.h()},h(){c(m,"name","hf:doc:metadata"),c(m,"content",JSON.stringify(_n)),c(f,"id","create-reproducible-pipelines"),c(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"),c(f,"href","#create-reproducible-pipelines"),c(h,"class","relative group"),c(_,"href","reusing_seeds"),c(z,"id","control-randomness"),c(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"),c(z,"href","#control-randomness"),c(N,"class","relative group"),c(je,"href","/docs/diffusers/v0.16.0/en/api/pipelines/ddim#diffusers.DDIMPipeline"),c(oe,"href","https://pytorch.org/docs/stable/generated/torch.randn.html"),c(oe,"rel","nofollow"),c(Q,"id","cpu"),c(Q,"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"),c(Q,"href","#cpu"),c(M,"class","relative group"),c(pe,"href","https://pytorch.org/docs/stable/generated/torch.randn.html"),c(pe,"rel","nofollow"),c(Z,"id","gpu"),c(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"),c(Z,"href","#gpu"),c(L,"class","relative group"),c(Ge,"href","#diffusers.utils.randn_tensor"),c(Ie,"href","/docs/diffusers/v0.16.0/en/api/pipelines/unclip#diffusers.UnCLIPPipeline"),c(se,"id","diffusers.utils.randn_tensor"),c(se,"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"),c(se,"href","#diffusers.utils.randn_tensor"),c(F,"class","relative group"),c(R,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),c(re,"id","deterministic-algorithms"),c(re,"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"),c(re,"href","#deterministic-algorithms"),c(B,"class","relative group"),c(ge,"href","https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility"),c(ge,"rel","nofollow"),c(_e,"href","https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html"),c(_e,"rel","nofollow")},m(e,r){s(document.head,m),p(e,b,r),p(e,h,r),s(h,f),s(f,q),P(v,q,null),s(h,C),s(h,k),s(k,G),p(e,g,r),p(e,y,r),s(y,T),s(y,_),s(_,O),s(y,$e),p(e,ne,r),p(e,Pe,r),s(Pe,ns),p(e,ht,r),P(K,e,r),p(e,ft,r),p(e,N,r),s(N,z),s(z,Re),P(ae,Re,null),s(N,as),s(N,We),s(We,is),p(e,yt,r),p(e,Ee,r),s(Ee,os),p(e,gt,r),p(e,Y,r),s(Y,ls),s(Y,je),s(je,ps),s(Y,us),p(e,_t,r),P(ie,e,r),p(e,vt,r),p(e,De,r),s(De,ds),p(e,bt,r),p(e,J,r),s(J,cs),s(J,oe),s(oe,Be),s(Be,ms),s(J,hs),p(e,wt,r),p(e,ke,r),s(ke,fs),p(e,$t,r),p(e,M,r),s(M,Q),s(Q,He),P(le,He,null),s(M,ys),s(M,Ke),s(Ke,gs),p(e,Pt,r),p(e,V,r),s(V,_s),s(V,pe),s(pe,ze),s(ze,vs),s(V,bs),p(e,Et,r),P(ue,e,r),p(e,jt,r),p(e,I,r),s(I,ws),s(I,Ye),s(Ye,$s),s(I,Ps),s(I,Je),s(Je,Es),s(I,js),p(e,Dt,r),p(e,Ce,r),s(Ce,Ds),p(e,kt,r),P(X,e,r),p(e,Ct,r),p(e,L,r),s(L,Z),s(Z,Qe),P(de,Qe,null),s(L,ks),s(L,Ve),s(Ve,Cs),p(e,Ut,r),p(e,Ue,r),s(Ue,Us),p(e,qt,r),P(ce,e,r),p(e,Gt,r),p(e,qe,r),s(qe,qs),p(e,Tt,r),p(e,U,r),s(U,Gs),s(U,Ge),s(Ge,Xe),s(Xe,Ts),s(U,Is),s(U,Ze),s(Ze,xs),s(U,As),s(U,et),s(et,Ss),s(U,Os),s(U,tt),s(tt,Ns),s(U,Ms),p(e,It,r),p(e,Te,r),s(Te,Ls),p(e,xt,r),P(me,e,r),p(e,At,r),P(ee,e,r),p(e,St,r),p(e,te,r),s(te,Fs),s(te,Ie),s(Ie,Rs),s(te,Ws),p(e,Ot,r),p(e,F,r),s(F,se),s(se,st),P(he,st,null),s(F,Bs),s(F,rt),s(rt,Hs),p(e,Nt,r),p(e,R,r),P(fe,R,null),s(R,Ks),s(R,W),s(W,zs),s(W,nt),s(nt,Ys),s(W,Js),s(W,at),s(at,Qs),s(W,Vs),p(e,Mt,r),p(e,B,r),s(B,re),s(re,it),P(ye,it,null),s(B,Xs),s(B,ot),s(ot,Zs),p(e,Lt,r),p(e,xe,r),s(xe,er),p(e,Ft,r),p(e,x,r),s(x,tr),s(x,ge),s(ge,lt),s(lt,sr),s(x,rr),s(x,pt),s(pt,nr),s(x,ar),p(e,Rt,r),p(e,A,r),s(A,ir),s(A,ut),s(ut,or),s(A,lr),s(A,_e),s(_e,dt),s(dt,pr),s(A,ur),p(e,Wt,r),P(ve,e,r),p(e,Bt,r),p(e,Ae,r),s(Ae,dr),p(e,Ht,r),P(be,e,r),Kt=!0},p(e,[r]){const we={};r&2&&(we.$$scope={dirty:r,ctx:e}),K.$set(we);const ct={};r&2&&(ct.$$scope={dirty:r,ctx:e}),X.$set(ct);const mt={};r&2&&(mt.$$scope={dirty:r,ctx:e}),ee.$set(mt)},i(e){Kt||(E(v.$$.fragment,e),E(K.$$.fragment,e),E(ae.$$.fragment,e),E(ie.$$.fragment,e),E(le.$$.fragment,e),E(ue.$$.fragment,e),E(X.$$.fragment,e),E(de.$$.fragment,e),E(ce.$$.fragment,e),E(me.$$.fragment,e),E(ee.$$.fragment,e),E(he.$$.fragment,e),E(fe.$$.fragment,e),E(ye.$$.fragment,e),E(ve.$$.fragment,e),E(be.$$.fragment,e),Kt=!0)},o(e){j(v.$$.fragment,e),j(K.$$.fragment,e),j(ae.$$.fragment,e),j(ie.$$.fragment,e),j(le.$$.fragment,e),j(ue.$$.fragment,e),j(X.$$.fragment,e),j(de.$$.fragment,e),j(ce.$$.fragment,e),j(me.$$.fragment,e),j(ee.$$.fragment,e),j(he.$$.fragment,e),j(fe.$$.fragment,e),j(ye.$$.fragment,e),j(ve.$$.fragment,e),j(be.$$.fragment,e),Kt=!1},d(e){t(m),e&&t(b),e&&t(h),D(v),e&&t(g),e&&t(y),e&&t(ne),e&&t(Pe),e&&t(ht),D(K,e),e&&t(ft),e&&t(N),D(ae),e&&t(yt),e&&t(Ee),e&&t(gt),e&&t(Y),e&&t(_t),D(ie,e),e&&t(vt),e&&t(De),e&&t(bt),e&&t(J),e&&t(wt),e&&t(ke),e&&t($t),e&&t(M),D(le),e&&t(Pt),e&&t(V),e&&t(Et),D(ue,e),e&&t(jt),e&&t(I),e&&t(Dt),e&&t(Ce),e&&t(kt),D(X,e),e&&t(Ct),e&&t(L),D(de),e&&t(Ut),e&&t(Ue),e&&t(qt),D(ce,e),e&&t(Gt),e&&t(qe),e&&t(Tt),e&&t(U),e&&t(It),e&&t(Te),e&&t(xt),D(me,e),e&&t(At),D(ee,e),e&&t(St),e&&t(te),e&&t(Ot),e&&t(F),D(he),e&&t(Nt),e&&t(R),D(fe),e&&t(Mt),e&&t(B),D(ye),e&&t(Lt),e&&t(xe),e&&t(Ft),e&&t(x),e&&t(Rt),e&&t(A),e&&t(Wt),D(ve,e),e&&t(Bt),e&&t(Ae),e&&t(Ht),D(be,e)}}}const _n={local:"create-reproducible-pipelines",sections:[{local:"control-randomness",sections:[{local:"cpu",title:"CPU"},{local:"gpu",title:"GPU"},{local:"diffusers.utils.randn_tensor",title:"randn_tensor"}],title:"Control randomness"},{local:"deterministic-algorithms",title:"Deterministic algorithms"}],title:"Create reproducible pipelines"};function vn(H){return cn(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class jn extends ln{constructor(m){super();pn(this,m,vn,gn,un,{})}}export{jn as default,_n as metadata};

Xet Storage Details

Size:
34.5 kB
·
Xet hash:
77a19dc1b94ef8263c3431b72b05755a473710a9128972ccfe54600afff9a0b7

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.