Buckets:
hf-doc-build/doc / diffusers /main /en /_app /pages /using-diffusers /write_own_pipeline.mdx-hf-doc-builder.js
| import{S as Vo,i as Wo,s as No,e as a,k as h,w as u,t as p,M as Ro,c as n,d as s,m as d,a as o,x as f,h as r,b as m,N as Go,G as t,g as i,y as b,q as y,o as g,B as M,v as xo,L as So}from"../../chunks/vendor-hf-doc-builder.js";import{T as ei}from"../../chunks/Tip-hf-doc-builder.js";import{I as ue}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as J}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Bo}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Ao(H){let c,$,w,U,T;return{c(){c=a("p"),$=p("\u{1F4A1} Read the "),w=a("a"),U=p("How does Stable Diffusion work?"),T=p(" blog for more details about how the VAE, UNet, and text encoder models."),this.h()},l(v){c=n(v,"P",{});var j=o(c);$=r(j,"\u{1F4A1} Read the "),w=n(j,"A",{href:!0,rel:!0});var E=o(w);U=r(E,"How does Stable Diffusion work?"),E.forEach(s),T=r(j," blog for more details about how the VAE, UNet, and text encoder models."),j.forEach(s),this.h()},h(){m(w,"href","https://huggingface.co/blog/stable_diffusion#how-does-stable-diffusion-work"),m(w,"rel","nofollow")},m(v,j){i(v,c,j),t(c,$),t(c,w),t(w,U),t(c,T)},d(v){v&&s(c)}}}function Xo(H){let c,$,w,U,T;return{c(){c=a("p"),$=p("\u{1F4A1} The "),w=a("code"),U=p("guidance_scale"),T=p(" parameter determines how much weight should be given to the prompt when generating an image.")},l(v){c=n(v,"P",{});var j=o(c);$=r(j,"\u{1F4A1} The "),w=n(j,"CODE",{});var E=o(w);U=r(E,"guidance_scale"),E.forEach(s),T=r(j," parameter determines how much weight should be given to the prompt when generating an image."),j.forEach(s)},m(v,j){i(v,c,j),t(c,$),t(c,w),t(w,U),t(c,T)},d(v){v&&s(c)}}}function Do(H){let c,$,w,U,T,v,j,E;return j=new J({props:{code:"MiUyMCoqJTIwKGxlbih2YWUuY29uZmlnLmJsb2NrX291dF9jaGFubmVscyklMjAtJTIwMSklMjAlM0QlM0QlMjA4",highlighted:'<span class="hljs-number">2</span> ** (<span class="hljs-built_in">len</span>(vae.config.block_out_channels) - <span class="hljs-number">1</span>) == <span class="hljs-number">8</span>'}}),{c(){c=a("p"),$=p("\u{1F4A1} The height and width are divided by 8 because the "),w=a("code"),U=p("vae"),T=p(" model has 3 down-sampling layers. You can check by running the following:"),v=h(),u(j.$$.fragment)},l(_){c=n(_,"P",{});var I=o(c);$=r(I,"\u{1F4A1} The height and width are divided by 8 because the "),w=n(I,"CODE",{});var V=o(w);U=r(V,"vae"),V.forEach(s),T=r(I," model has 3 down-sampling layers. You can check by running the following:"),I.forEach(s),v=d(_),f(j.$$.fragment,_)},m(_,I){i(_,c,I),t(c,$),t(c,w),t(w,U),t(c,T),i(_,v,I),b(j,_,I),E=!0},p:So,i(_){E||(y(j.$$.fragment,_),E=!0)},o(_){g(j.$$.fragment,_),E=!1},d(_){_&&s(c),_&&s(v),M(j,_)}}}function Fo(H){let c,$,w,U,T,v,j,E,_,I,V,wt,L,Pl,Ke,Hl,Ll,jt,Oe,ql,vt,X,q,Is,fe,Kl,Cs,Ol,Ut,es,ea,Jt,be,_t,ye,ss,si,Tt,ts,sa,$t,C,ta,ls,la,aa,as,na,ia,Gs,oa,pa,Et,ns,ra,Zt,is,Vs,ha,kt,ge,It,Me,Ws,da,Ct,we,Gt,je,Ns,ma,Vt,ve,Wt,Ue,Rs,ca,Nt,Je,Rt,_e,G,ua,os,fa,ba,ps,ya,ga,xs,Ma,wa,xt,Te,St,rs,ja,Bt,$e,Ss,va,At,Ee,Xt,Z,Ua,Bs,Ja,_a,As,Ta,$a,Xs,Ea,Za,Ds,ka,Ia,Dt,hs,Ca,Ft,D,K,Fs,Ze,Ga,zs,Va,zt,O,Wa,Qs,Na,Ra,Qt,ds,xa,Yt,ee,Pt,W,Sa,ms,Ba,Aa,ke,Ys,Xa,Da,Ht,Ie,Lt,N,Fa,cs,za,Qa,us,Ya,Pa,qt,Ce,Kt,fs,Ha,Ot,Ge,el,F,se,Ps,Ve,La,Hs,qa,sl,bs,Ka,tl,te,ll,ys,Oa,al,We,nl,gs,en,il,Ne,ol,k,sn,Ls,tn,ln,qs,an,nn,Ks,on,pn,Os,rn,hn,pl,Re,rl,Ms,dn,hl,xe,dl,z,le,et,Se,mn,st,cn,ml,ae,un,tt,fn,bn,cl,ne,ul,Be,fl,Q,ie,lt,Ae,yn,at,gn,bl,R,Mn,nt,wn,jn,ws,vn,Un,yl,Xe,gl,oe,Jn,it,_n,Tn,Ml,x,ot,$n,En,pt,Zn,kn,rt,In,wl,De,jl,Y,pe,ht,Fe,Cn,dt,Gn,vl,S,Vn,mt,Wn,Nn,ct,Rn,xn,Ul,ze,Jl,re,Sn,ut,Bn,An,_l,Qe,Tl,Ye,ft,ti,$l,P,he,bt,Pe,Xn,yt,Dn,El,js,Fn,Zl,vs,zn,kl,Us,Qn,Il,de,He,Yn,Js,Pn,Hn,Ln,Le,qn,_s,Kn,On,Cl;return v=new ue({}),V=new Bo({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/write_own_pipeline.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/write_own_pipeline.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/write_own_pipeline.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/write_own_pipeline.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/write_own_pipeline.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/write_own_pipeline.ipynb"}]}}),fe=new ue({}),be=new J({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEREUE1QaXBlbGluZSUwQSUwQWRkcG0lMjAlM0QlMjBERFBNUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmRkcG0tY2F0LTI1NiUyMiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUpLnRvKCUyMmN1ZGElMjIpJTBBaW1hZ2UlMjAlM0QlMjBkZHBtKG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QyNSkuaW1hZ2VzJTVCMCU1RCUwQWltYWdl",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDPMPipeline | |
| <span class="hljs-meta">>>> </span>ddpm = DDPMPipeline.from_pretrained(<span class="hljs-string">"google/ddpm-cat-256"</span>, use_safetensors=<span class="hljs-literal">True</span>).to(<span class="hljs-string">"cuda"</span>) | |
| <span class="hljs-meta">>>> </span>image = ddpm(num_inference_steps=<span class="hljs-number">25</span>).images[<span class="hljs-number">0</span>] | |
| <span class="hljs-meta">>>> </span>image`}}),ge=new J({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEREUE1TY2hlZHVsZXIlMkMlMjBVTmV0MkRNb2RlbCUwQSUwQXNjaGVkdWxlciUyMCUzRCUyMEREUE1TY2hlZHVsZXIuZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmRkcG0tY2F0LTI1NiUyMiklMEFtb2RlbCUyMCUzRCUyMFVOZXQyRE1vZGVsLmZyb21fcHJldHJhaW5lZCglMjJnb29nbGUlMkZkZHBtLWNhdC0yNTYlMjIlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlKS50byglMjJjdWRhJTIyKQ==",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DDPMScheduler, UNet2DModel | |
| <span class="hljs-meta">>>> </span>scheduler = DDPMScheduler.from_pretrained(<span class="hljs-string">"google/ddpm-cat-256"</span>) | |
| <span class="hljs-meta">>>> </span>model = UNet2DModel.from_pretrained(<span class="hljs-string">"google/ddpm-cat-256"</span>, use_safetensors=<span class="hljs-literal">True</span>).to(<span class="hljs-string">"cuda"</span>)`}}),we=new J({props:{code:"c2NoZWR1bGVyLnNldF90aW1lc3RlcHMoNTAp",highlighted:'<span class="hljs-meta">>>> </span>scheduler.set_timesteps(<span class="hljs-number">50</span>)'}}),ve=new J({props:{code:"c2NoZWR1bGVyLnRpbWVzdGVwcw==",highlighted:`<span class="hljs-meta">>>> </span>scheduler.timesteps | |
| tensor([<span class="hljs-number">980</span>, <span class="hljs-number">960</span>, <span class="hljs-number">940</span>, <span class="hljs-number">920</span>, <span class="hljs-number">900</span>, <span class="hljs-number">880</span>, <span class="hljs-number">860</span>, <span class="hljs-number">840</span>, <span class="hljs-number">820</span>, <span class="hljs-number">800</span>, <span class="hljs-number">780</span>, <span class="hljs-number">760</span>, <span class="hljs-number">740</span>, <span class="hljs-number">720</span>, | |
| <span class="hljs-number">700</span>, <span class="hljs-number">680</span>, <span class="hljs-number">660</span>, <span class="hljs-number">640</span>, <span class="hljs-number">620</span>, <span class="hljs-number">600</span>, <span class="hljs-number">580</span>, <span class="hljs-number">560</span>, <span class="hljs-number">540</span>, <span class="hljs-number">520</span>, <span class="hljs-number">500</span>, <span class="hljs-number">480</span>, <span class="hljs-number">460</span>, <span class="hljs-number">440</span>, | |
| <span class="hljs-number">420</span>, <span class="hljs-number">400</span>, <span class="hljs-number">380</span>, <span class="hljs-number">360</span>, <span class="hljs-number">340</span>, <span class="hljs-number">320</span>, <span class="hljs-number">300</span>, <span class="hljs-number">280</span>, <span class="hljs-number">260</span>, <span class="hljs-number">240</span>, <span class="hljs-number">220</span>, <span class="hljs-number">200</span>, <span class="hljs-number">180</span>, <span class="hljs-number">160</span>, | |
| <span class="hljs-number">140</span>, <span class="hljs-number">120</span>, <span class="hljs-number">100</span>, <span class="hljs-number">80</span>, <span class="hljs-number">60</span>, <span class="hljs-number">40</span>, <span class="hljs-number">20</span>, <span class="hljs-number">0</span>])`}}),Je=new J({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEFzYW1wbGVfc2l6ZSUyMCUzRCUyMG1vZGVsLmNvbmZpZy5zYW1wbGVfc2l6ZSUwQW5vaXNlJTIwJTNEJTIwdG9yY2gucmFuZG4oKDElMkMlMjAzJTJDJTIwc2FtcGxlX3NpemUlMkMlMjBzYW1wbGVfc2l6ZSkpLnRvKCUyMmN1ZGElMjIp",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-meta">>>> </span>sample_size = model.config.sample_size | |
| <span class="hljs-meta">>>> </span>noise = torch.randn((<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, sample_size, sample_size)).to(<span class="hljs-string">"cuda"</span>)`}}),Te=new J({props:{code:"aW5wdXQlMjAlM0QlMjBub2lzZSUwQSUwQWZvciUyMHQlMjBpbiUyMHNjaGVkdWxlci50aW1lc3RlcHMlM0ElMEElMjAlMjAlMjAlMjB3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbm9pc3lfcmVzaWR1YWwlMjAlM0QlMjBtb2RlbChpbnB1dCUyQyUyMHQpLnNhbXBsZSUwQSUyMCUyMCUyMCUyMHByZXZpb3VzX25vaXN5X3NhbXBsZSUyMCUzRCUyMHNjaGVkdWxlci5zdGVwKG5vaXN5X3Jlc2lkdWFsJTJDJTIwdCUyQyUyMGlucHV0KS5wcmV2X3NhbXBsZSUwQSUyMCUyMCUyMCUyMGlucHV0JTIwJTNEJTIwcHJldmlvdXNfbm9pc3lfc2FtcGxl",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-built_in">input</span> = noise | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> scheduler.timesteps: | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">with</span> torch.no_grad(): | |
| <span class="hljs-meta">... </span> noisy_residual = model(<span class="hljs-built_in">input</span>, t).sample | |
| <span class="hljs-meta">... </span> previous_noisy_sample = scheduler.step(noisy_residual, t, <span class="hljs-built_in">input</span>).prev_sample | |
| <span class="hljs-meta">... </span> <span class="hljs-built_in">input</span> = previous_noisy_sample`}}),Ee=new J({props:{code:"ZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBaW1hZ2UlMjAlM0QlMjAoaW5wdXQlMjAlMkYlMjAyJTIwJTJCJTIwMC41KS5jbGFtcCgwJTJDJTIwMSkuc3F1ZWV6ZSgpJTBBaW1hZ2UlMjAlM0QlMjAoaW1hZ2UucGVybXV0ZSgxJTJDJTIwMiUyQyUyMDApJTIwKiUyMDI1NSkucm91bmQoKS50byh0b3JjaC51aW50OCkuY3B1KCkubnVtcHkoKSUwQWltYWdlJTIwJTNEJTIwSW1hZ2UuZnJvbWFycmF5KGltYWdlKSUwQWltYWdl",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np | |
| <span class="hljs-meta">>>> </span>image = (<span class="hljs-built_in">input</span> / <span class="hljs-number">2</span> + <span class="hljs-number">0.5</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>).squeeze() | |
| <span class="hljs-meta">>>> </span>image = (image.permute(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>) * <span class="hljs-number">255</span>).<span class="hljs-built_in">round</span>().to(torch.uint8).cpu().numpy() | |
| <span class="hljs-meta">>>> </span>image = Image.fromarray(image) | |
| <span class="hljs-meta">>>> </span>image`}}),Ze=new ue({}),ee=new ei({props:{$$slots:{default:[Ao]},$$scope:{ctx:H}}}),Ie=new J({props:{code:"ZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQ0xJUFRleHRNb2RlbCUyQyUyMENMSVBUb2tlbml6ZXIlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b2VuY29kZXJLTCUyQyUyMFVOZXQyRENvbmRpdGlvbk1vZGVsJTJDJTIwUE5ETVNjaGVkdWxlciUwQSUwQXZhZSUyMCUzRCUyMEF1dG9lbmNvZGVyS0wuZnJvbV9wcmV0cmFpbmVkKCUyMkNvbXBWaXMlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTQlMjIlMkMlMjBzdWJmb2xkZXIlM0QlMjJ2YWUlMjIlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlKSUwQXRva2VuaXplciUyMCUzRCUyMENMSVBUb2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMkNvbXBWaXMlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTQlMjIlMkMlMjBzdWJmb2xkZXIlM0QlMjJ0b2tlbml6ZXIlMjIpJTBBdGV4dF9lbmNvZGVyJTIwJTNEJTIwQ0xJUFRleHRNb2RlbC5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyQ29tcFZpcyUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNCUyMiUyQyUyMHN1YmZvbGRlciUzRCUyMnRleHRfZW5jb2RlciUyMiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMEEpJTBBdW5ldCUyMCUzRCUyMFVOZXQyRENvbmRpdGlvbk1vZGVsLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJDb21wVmlzJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS00JTIyJTJDJTIwc3ViZm9sZGVyJTNEJTIydW5ldCUyMiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMEEp",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> CLIPTextModel, CLIPTokenizer | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoencoderKL, UNet2DConditionModel, PNDMScheduler | |
| <span class="hljs-meta">>>> </span>vae = AutoencoderKL.from_pretrained(<span class="hljs-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"vae"</span>, use_safetensors=<span class="hljs-literal">True</span>) | |
| <span class="hljs-meta">>>> </span>tokenizer = CLIPTokenizer.from_pretrained(<span class="hljs-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"tokenizer"</span>) | |
| <span class="hljs-meta">>>> </span>text_encoder = CLIPTextModel.from_pretrained( | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"text_encoder"</span>, use_safetensors=<span class="hljs-literal">True</span> | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>unet = UNet2DConditionModel.from_pretrained( | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"unet"</span>, use_safetensors=<span class="hljs-literal">True</span> | |
| <span class="hljs-meta">... </span>)`}}),Ce=new J({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFVuaVBDTXVsdGlzdGVwU2NoZWR1bGVyJTBBJTBBc2NoZWR1bGVyJTIwJTNEJTIwVW5pUENNdWx0aXN0ZXBTY2hlZHVsZXIuZnJvbV9wcmV0cmFpbmVkKCUyMkNvbXBWaXMlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTQlMjIlMkMlMjBzdWJmb2xkZXIlM0QlMjJzY2hlZHVsZXIlMjIp",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> UniPCMultistepScheduler | |
| <span class="hljs-meta">>>> </span>scheduler = UniPCMultistepScheduler.from_pretrained(<span class="hljs-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"scheduler"</span>)`}}),Ge=new J({props:{code:"dG9yY2hfZGV2aWNlJTIwJTNEJTIwJTIyY3VkYSUyMiUwQXZhZS50byh0b3JjaF9kZXZpY2UpJTBBdGV4dF9lbmNvZGVyLnRvKHRvcmNoX2RldmljZSklMEF1bmV0LnRvKHRvcmNoX2RldmljZSk=",highlighted:`<span class="hljs-meta">>>> </span>torch_device = <span class="hljs-string">"cuda"</span> | |
| <span class="hljs-meta">>>> </span>vae.to(torch_device) | |
| <span class="hljs-meta">>>> </span>text_encoder.to(torch_device) | |
| <span class="hljs-meta">>>> </span>unet.to(torch_device)`}}),Ve=new ue({}),te=new ei({props:{$$slots:{default:[Xo]},$$scope:{ctx:H}}}),We=new J({props:{code:"cHJvbXB0JTIwJTNEJTIwJTVCJTIyYSUyMHBob3RvZ3JhcGglMjBvZiUyMGFuJTIwYXN0cm9uYXV0JTIwcmlkaW5nJTIwYSUyMGhvcnNlJTIyJTVEJTBBaGVpZ2h0JTIwJTNEJTIwNTEyJTIwJTIwJTIzJTIwZGVmYXVsdCUyMGhlaWdodCUyMG9mJTIwU3RhYmxlJTIwRGlmZnVzaW9uJTBBd2lkdGglMjAlM0QlMjA1MTIlMjAlMjAlMjMlMjBkZWZhdWx0JTIwd2lkdGglMjBvZiUyMFN0YWJsZSUyMERpZmZ1c2lvbiUwQW51bV9pbmZlcmVuY2Vfc3RlcHMlMjAlM0QlMjAyNSUyMCUyMCUyMyUyME51bWJlciUyMG9mJTIwZGVub2lzaW5nJTIwc3RlcHMlMEFndWlkYW5jZV9zY2FsZSUyMCUzRCUyMDcuNSUyMCUyMCUyMyUyMFNjYWxlJTIwZm9yJTIwY2xhc3NpZmllci1mcmVlJTIwZ3VpZGFuY2UlMEFnZW5lcmF0b3IlMjAlM0QlMjB0b3JjaC5tYW51YWxfc2VlZCgwKSUyMCUyMCUyMyUyMFNlZWQlMjBnZW5lcmF0b3IlMjB0byUyMGNyZWF0ZSUyMHRoZSUyMGluaXRhbCUyMGxhdGVudCUyMG5vaXNlJTBBYmF0Y2hfc2l6ZSUyMCUzRCUyMGxlbihwcm9tcHQp",highlighted:`<span class="hljs-meta">>>> </span>prompt = [<span class="hljs-string">"a photograph of an astronaut riding a horse"</span>] | |
| <span class="hljs-meta">>>> </span>height = <span class="hljs-number">512</span> <span class="hljs-comment"># default height of Stable Diffusion</span> | |
| <span class="hljs-meta">>>> </span>width = <span class="hljs-number">512</span> <span class="hljs-comment"># default width of Stable Diffusion</span> | |
| <span class="hljs-meta">>>> </span>num_inference_steps = <span class="hljs-number">25</span> <span class="hljs-comment"># Number of denoising steps</span> | |
| <span class="hljs-meta">>>> </span>guidance_scale = <span class="hljs-number">7.5</span> <span class="hljs-comment"># Scale for classifier-free guidance</span> | |
| <span class="hljs-meta">>>> </span>generator = torch.manual_seed(<span class="hljs-number">0</span>) <span class="hljs-comment"># Seed generator to create the inital latent noise</span> | |
| <span class="hljs-meta">>>> </span>batch_size = <span class="hljs-built_in">len</span>(prompt)`}}),Ne=new J({props:{code:"dGV4dF9pbnB1dCUyMCUzRCUyMHRva2VuaXplciglMEElMjAlMjAlMjAlMjBwcm9tcHQlMkMlMjBwYWRkaW5nJTNEJTIybWF4X2xlbmd0aCUyMiUyQyUyMG1heF9sZW5ndGglM0R0b2tlbml6ZXIubW9kZWxfbWF4X2xlbmd0aCUyQyUyMHRydW5jYXRpb24lM0RUcnVlJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMiUwQSklMEElMEF3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwdGV4dF9lbWJlZGRpbmdzJTIwJTNEJTIwdGV4dF9lbmNvZGVyKHRleHRfaW5wdXQuaW5wdXRfaWRzLnRvKHRvcmNoX2RldmljZSkpJTVCMCU1RA==",highlighted:`<span class="hljs-meta">>>> </span>text_input = tokenizer( | |
| <span class="hljs-meta">... </span> prompt, padding=<span class="hljs-string">"max_length"</span>, max_length=tokenizer.model_max_length, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">"pt"</span> | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">with</span> torch.no_grad(): | |
| <span class="hljs-meta">... </span> text_embeddings = text_encoder(text_input.input_ids.to(torch_device))[<span class="hljs-number">0</span>]`}}),Re=new J({props:{code:"bWF4X2xlbmd0aCUyMCUzRCUyMHRleHRfaW5wdXQuaW5wdXRfaWRzLnNoYXBlJTVCLTElNUQlMEF1bmNvbmRfaW5wdXQlMjAlM0QlMjB0b2tlbml6ZXIoJTVCJTIyJTIyJTVEJTIwKiUyMGJhdGNoX3NpemUlMkMlMjBwYWRkaW5nJTNEJTIybWF4X2xlbmd0aCUyMiUyQyUyMG1heF9sZW5ndGglM0RtYXhfbGVuZ3RoJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMiklMEF1bmNvbmRfZW1iZWRkaW5ncyUyMCUzRCUyMHRleHRfZW5jb2Rlcih1bmNvbmRfaW5wdXQuaW5wdXRfaWRzLnRvKHRvcmNoX2RldmljZSkpJTVCMCU1RA==",highlighted:`<span class="hljs-meta">>>> </span>max_length = text_input.input_ids.shape[-<span class="hljs-number">1</span>] | |
| <span class="hljs-meta">>>> </span>uncond_input = tokenizer([<span class="hljs-string">""</span>] * batch_size, padding=<span class="hljs-string">"max_length"</span>, max_length=max_length, return_tensors=<span class="hljs-string">"pt"</span>) | |
| <span class="hljs-meta">>>> </span>uncond_embeddings = text_encoder(uncond_input.input_ids.to(torch_device))[<span class="hljs-number">0</span>]`}}),xe=new J({props:{code:"dGV4dF9lbWJlZGRpbmdzJTIwJTNEJTIwdG9yY2guY2F0KCU1QnVuY29uZF9lbWJlZGRpbmdzJTJDJTIwdGV4dF9lbWJlZGRpbmdzJTVEKQ==",highlighted:'<span class="hljs-meta">>>> </span>text_embeddings = torch.cat([uncond_embeddings, text_embeddings])'}}),Se=new ue({}),ne=new ei({props:{$$slots:{default:[Do]},$$scope:{ctx:H}}}),Be=new J({props:{code:"bGF0ZW50cyUyMCUzRCUyMHRvcmNoLnJhbmRuKCUwQSUyMCUyMCUyMCUyMChiYXRjaF9zaXplJTJDJTIwdW5ldC5pbl9jaGFubmVscyUyQyUyMGhlaWdodCUyMCUyRiUyRiUyMDglMkMlMjB3aWR0aCUyMCUyRiUyRiUyMDgpJTJDJTBBJTIwJTIwJTIwJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yJTJDJTBBKSUwQWxhdGVudHMlMjAlM0QlMjBsYXRlbnRzLnRvKHRvcmNoX2RldmljZSk=",highlighted:`<span class="hljs-meta">>>> </span>latents = torch.randn( | |
| <span class="hljs-meta">... </span> (batch_size, unet.in_channels, height // <span class="hljs-number">8</span>, width // <span class="hljs-number">8</span>), | |
| <span class="hljs-meta">... </span> generator=generator, | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>latents = latents.to(torch_device)`}}),Ae=new ue({}),Xe=new J({props:{code:"bGF0ZW50cyUyMCUzRCUyMGxhdGVudHMlMjAqJTIwc2NoZWR1bGVyLmluaXRfbm9pc2Vfc2lnbWE=",highlighted:'<span class="hljs-meta">>>> </span>latents = latents * scheduler.init_noise_sigma'}}),De=new J({props:{code:"ZnJvbSUyMHRxZG0uYXV0byUyMGltcG9ydCUyMHRxZG0lMEElMEFzY2hlZHVsZXIuc2V0X3RpbWVzdGVwcyhudW1faW5mZXJlbmNlX3N0ZXBzKSUwQSUwQWZvciUyMHQlMjBpbiUyMHRxZG0oc2NoZWR1bGVyLnRpbWVzdGVwcyklM0ElMEElMjAlMjAlMjAlMjAlMjMlMjBleHBhbmQlMjB0aGUlMjBsYXRlbnRzJTIwaWYlMjB3ZSUyMGFyZSUyMGRvaW5nJTIwY2xhc3NpZmllci1mcmVlJTIwZ3VpZGFuY2UlMjB0byUyMGF2b2lkJTIwZG9pbmclMjB0d28lMjBmb3J3YXJkJTIwcGFzc2VzLiUwQSUyMCUyMCUyMCUyMGxhdGVudF9tb2RlbF9pbnB1dCUyMCUzRCUyMHRvcmNoLmNhdCglNUJsYXRlbnRzJTVEJTIwKiUyMDIpJTBBJTBBJTIwJTIwJTIwJTIwbGF0ZW50X21vZGVsX2lucHV0JTIwJTNEJTIwc2NoZWR1bGVyLnNjYWxlX21vZGVsX2lucHV0KGxhdGVudF9tb2RlbF9pbnB1dCUyQyUyMHRpbWVzdGVwJTNEdCklMEElMEElMjAlMjAlMjAlMjAlMjMlMjBwcmVkaWN0JTIwdGhlJTIwbm9pc2UlMjByZXNpZHVhbCUwQSUyMCUyMCUyMCUyMHdpdGglMjB0b3JjaC5ub19ncmFkKCklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBub2lzZV9wcmVkJTIwJTNEJTIwdW5ldChsYXRlbnRfbW9kZWxfaW5wdXQlMkMlMjB0JTJDJTIwZW5jb2Rlcl9oaWRkZW5fc3RhdGVzJTNEdGV4dF9lbWJlZGRpbmdzKS5zYW1wbGUlMEElMEElMjAlMjAlMjAlMjAlMjMlMjBwZXJmb3JtJTIwZ3VpZGFuY2UlMEElMjAlMjAlMjAlMjBub2lzZV9wcmVkX3VuY29uZCUyQyUyMG5vaXNlX3ByZWRfdGV4dCUyMCUzRCUyMG5vaXNlX3ByZWQuY2h1bmsoMiklMEElMjAlMjAlMjAlMjBub2lzZV9wcmVkJTIwJTNEJTIwbm9pc2VfcHJlZF91bmNvbmQlMjAlMkIlMjBndWlkYW5jZV9zY2FsZSUyMColMjAobm9pc2VfcHJlZF90ZXh0JTIwLSUyMG5vaXNlX3ByZWRfdW5jb25kKSUwQSUwQSUyMCUyMCUyMCUyMCUyMyUyMGNvbXB1dGUlMjB0aGUlMjBwcmV2aW91cyUyMG5vaXN5JTIwc2FtcGxlJTIweF90JTIwLSUzRSUyMHhfdC0xJTBBJTIwJTIwJTIwJTIwbGF0ZW50cyUyMCUzRCUyMHNjaGVkdWxlci5zdGVwKG5vaXNlX3ByZWQlMkMlMjB0JTJDJTIwbGF0ZW50cykucHJldl9zYW1wbGU=",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm | |
| <span class="hljs-meta">>>> </span>scheduler.set_timesteps(num_inference_steps) | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> tqdm(scheduler.timesteps): | |
| <span class="hljs-meta">... </span> <span class="hljs-comment"># expand the latents if we are doing classifier-free guidance to avoid doing two forward passes.</span> | |
| <span class="hljs-meta">... </span> latent_model_input = torch.cat([latents] * <span class="hljs-number">2</span>) | |
| <span class="hljs-meta">... </span> latent_model_input = scheduler.scale_model_input(latent_model_input, timestep=t) | |
| <span class="hljs-meta">... </span> <span class="hljs-comment"># predict the noise residual</span> | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">with</span> torch.no_grad(): | |
| <span class="hljs-meta">... </span> noise_pred = unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample | |
| <span class="hljs-meta">... </span> <span class="hljs-comment"># perform guidance</span> | |
| <span class="hljs-meta">... </span> noise_pred_uncond, noise_pred_text = noise_pred.chunk(<span class="hljs-number">2</span>) | |
| <span class="hljs-meta">... </span> noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) | |
| <span class="hljs-meta">... </span> <span class="hljs-comment"># compute the previous noisy sample x_t -> x_t-1</span> | |
| <span class="hljs-meta">... </span> latents = scheduler.step(noise_pred, t, latents).prev_sample`}}),Fe=new ue({}),ze=new J({props:{code:"JTIzJTIwc2NhbGUlMjBhbmQlMjBkZWNvZGUlMjB0aGUlMjBpbWFnZSUyMGxhdGVudHMlMjB3aXRoJTIwdmFlJTBBbGF0ZW50cyUyMCUzRCUyMDElMjAlMkYlMjAwLjE4MjE1JTIwKiUyMGxhdGVudHMlMEF3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwaW1hZ2UlMjAlM0QlMjB2YWUuZGVjb2RlKGxhdGVudHMpLnNhbXBsZQ==",highlighted:`<span class="hljs-comment"># scale and decode the image latents with vae</span> | |
| latents = <span class="hljs-number">1</span> / <span class="hljs-number">0.18215</span> * latents | |
| <span class="hljs-keyword">with</span> torch.no_grad(): | |
| image = vae.decode(latents).sample`}}),Qe=new J({props:{code:"aW1hZ2UlMjAlM0QlMjAoaW1hZ2UlMjAlMkYlMjAyJTIwJTJCJTIwMC41KS5jbGFtcCgwJTJDJTIwMSkuc3F1ZWV6ZSgpJTBBaW1hZ2UlMjAlM0QlMjAoaW1hZ2UucGVybXV0ZSgxJTJDJTIwMiUyQyUyMDApJTIwKiUyMDI1NSkudG8odG9yY2gudWludDgpLmNwdSgpLm51bXB5KCklMEFpbWFnZXMlMjAlM0QlMjAoaW1hZ2UlMjAqJTIwMjU1KS5yb3VuZCgpLmFzdHlwZSglMjJ1aW50OCUyMiklMEFpbWFnZSUyMCUzRCUyMEltYWdlLmZyb21hcnJheShpbWFnZSklMEFpbWFnZQ==",highlighted:`<span class="hljs-meta">>>> </span>image = (image / <span class="hljs-number">2</span> + <span class="hljs-number">0.5</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>).squeeze() | |
| <span class="hljs-meta">>>> </span>image = (image.permute(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>) * <span class="hljs-number">255</span>).to(torch.uint8).cpu().numpy() | |
| <span class="hljs-meta">>>> </span>images = (image * <span class="hljs-number">255</span>).<span class="hljs-built_in">round</span>().astype(<span class="hljs-string">"uint8"</span>) | |
| <span class="hljs-meta">>>> </span>image = Image.fromarray(image) | |
| <span class="hljs-meta">>>> </span>image`}}),Pe=new ue({}),{c(){c=a("meta"),$=h(),w=a("h1"),U=a("a"),T=a("span"),u(v.$$.fragment),j=h(),E=a("span"),_=p("Understanding pipelines, models and schedulers"),I=h(),u(V.$$.fragment),wt=h(),L=a("p"),Pl=p("\u{1F9E8} Diffusers is designed to be a user-friendly and flexible toolbox for building diffusion systems tailored to your use-case. At the core of the toolbox are models and schedulers. While the "),Ke=a("a"),Hl=p("DiffusionPipeline"),Ll=p(" bundles these components together for convenience, you can also unbundle the pipeline and use the models and schedulers separately to create new diffusion systems."),jt=h(),Oe=a("p"),ql=p("In this tutorial, you\u2019ll learn how to use models and schedulers to assemble a diffusion system for inference, starting with a basic pipeline and then progressing to the Stable Diffusion pipeline."),vt=h(),X=a("h2"),q=a("a"),Is=a("span"),u(fe.$$.fragment),Kl=h(),Cs=a("span"),Ol=p("Deconstruct a basic pipeline"),Ut=h(),es=a("p"),ea=p("A pipeline is a quick and easy way to run a model for inference, requiring no more than four lines of code to generate an image:"),Jt=h(),u(be.$$.fragment),_t=h(),ye=a("div"),ss=a("img"),Tt=h(),ts=a("p"),sa=p("That was super easy, but how did the pipeline do that? Let\u2019s breakdown the pipeline and take a look at what\u2019s happening under the hood."),$t=h(),C=a("p"),ta=p("In the example above, the pipeline contains a "),ls=a("a"),la=p("UNet2DModel"),aa=p(" model and a "),as=a("a"),na=p("DDPMScheduler"),ia=p(". The pipeline denoises an image by taking random noise the size of the desired output and passing it through the model several times. At each timestep, the model predicts the "),Gs=a("em"),oa=p("noise residual"),pa=p(" and the scheduler uses it to predict a less noisy image. The pipeline repeats this process until it reaches the end of the specified number of inference steps."),Et=h(),ns=a("p"),ra=p("To recreate the pipeline with the model and scheduler separately, let\u2019s write our own denoising process."),Zt=h(),is=a("ol"),Vs=a("li"),ha=p("Load the model and scheduler:"),kt=h(),u(ge.$$.fragment),It=h(),Me=a("ol"),Ws=a("li"),da=p("Set the number of timesteps to run the denoising process for:"),Ct=h(),u(we.$$.fragment),Gt=h(),je=a("ol"),Ns=a("li"),ma=p("Setting the scheduler timesteps creates a tensor with evenly spaced elements in it, 50 in this example. Each element corresponds to a timestep at which the model denoises an image. When you create the denoising loop later, you\u2019ll iterate over this tensor to denoise an image:"),Vt=h(),u(ve.$$.fragment),Wt=h(),Ue=a("ol"),Rs=a("li"),ca=p("Create some random noise with the same shape as the desired output:"),Nt=h(),u(Je.$$.fragment),Rt=h(),_e=a("ol"),G=a("li"),ua=p("Now write a loop to iterate over the timesteps. At each timestep, the model does a "),os=a("a"),fa=p("UNet2DModel.forward()"),ba=p(" pass and returns the noisy residual. The scheduler\u2019s "),ps=a("a"),ya=p("step()"),ga=p(" method takes the noisy residual, timestep, and input and it predicts the image at the previous timestep. This output becomes the next input to the model in the denoising loop, and it\u2019ll repeat until it reaches the end of the "),xs=a("code"),Ma=p("timesteps"),wa=p(" array."),xt=h(),u(Te.$$.fragment),St=h(),rs=a("p"),ja=p("This is the entire denoising process, and you can use this same pattern to write any diffusion system."),Bt=h(),$e=a("ol"),Ss=a("li"),va=p("The last step is to convert the denoised output into an image:"),At=h(),u(Ee.$$.fragment),Xt=h(),Z=a("p"),Ua=p("In the next section, you\u2019ll put your skills to the test and breakdown the more complex Stable Diffusion pipeline. The steps are more or less the same. You\u2019ll initialize the necessary components, and set the number of timesteps to create a "),Bs=a("code"),Ja=p("timestep"),_a=p(" array. The "),As=a("code"),Ta=p("timestep"),$a=p(" array is used in the denoising loop, and for each element in this array, the model predicts a less noisy image. The denoising loop iterates over the "),Xs=a("code"),Ea=p("timestep"),Za=p("\u2019s, and at each timestep, it outputs a noisy residual and the scheduler uses it to predict a less noisy image at the previous timestep. This process is repeated until you reach the end of the "),Ds=a("code"),ka=p("timestep"),Ia=p(" array."),Dt=h(),hs=a("p"),Ca=p("Let\u2019s try it out!"),Ft=h(),D=a("h2"),K=a("a"),Fs=a("span"),u(Ze.$$.fragment),Ga=h(),zs=a("span"),Va=p("Deconstruct the Stable Diffusion pipeline"),zt=h(),O=a("p"),Wa=p("Stable Diffusion is a text-to-image "),Qs=a("em"),Na=p("latent diffusion"),Ra=p(" model. It is called a latent diffusion model because it works with a lower-dimensional representation of the image instead of the actual pixel space, which makes it more memory efficient. The encoder compresses the image into a smaller representation, and a decoder to convert the compressed representation back into an image. For text-to-image models, you\u2019ll need a tokenizer and an encoder to generate text embeddings. From the previous example, you already know you need a UNet model and a scheduler."),Qt=h(),ds=a("p"),xa=p("As you can see, this is already more complex than the DDPM pipeline which only contains a UNet model. The Stable Diffusion model has three separate pretrained models."),Yt=h(),u(ee.$$.fragment),Pt=h(),W=a("p"),Sa=p("Now that you know what you need for the Stable Diffusion pipeline, load all these components with the "),ms=a("a"),Ba=p("from_pretrained()"),Aa=p(" method. You can find them in the pretrained "),ke=a("a"),Ys=a("code"),Xa=p("runwayml/stable-diffusion-v1-5"),Da=p(" checkpoint, and each component is stored in a separate subfolder:"),Ht=h(),u(Ie.$$.fragment),Lt=h(),N=a("p"),Fa=p("Instead of the default "),cs=a("a"),za=p("PNDMScheduler"),Qa=p(", exchange it for the "),us=a("a"),Ya=p("UniPCMultistepScheduler"),Pa=p(" to see how easy it is to plug a different scheduler in:"),qt=h(),u(Ce.$$.fragment),Kt=h(),fs=a("p"),Ha=p("To speed up inference, move the models to a GPU since, unlike the scheduler, they have trainable weights:"),Ot=h(),u(Ge.$$.fragment),el=h(),F=a("h3"),se=a("a"),Ps=a("span"),u(Ve.$$.fragment),La=h(),Hs=a("span"),qa=p("Create text embeddings"),sl=h(),bs=a("p"),Ka=p("The next step is to tokenize the text to generate embeddings. The text is used to condition the UNet model and steer the diffusion process towards something that resembles the input prompt."),tl=h(),u(te.$$.fragment),ll=h(),ys=a("p"),Oa=p("Feel free to choose any prompt you like if you want to generate something else!"),al=h(),u(We.$$.fragment),nl=h(),gs=a("p"),en=p("Tokenize the text and generate the embeddings from the prompt:"),il=h(),u(Ne.$$.fragment),ol=h(),k=a("p"),sn=p("You\u2019ll also need to generate the "),Ls=a("em"),tn=p("unconditional text embeddings"),ln=p(" which are the embeddings for the padding token. These need to have the same shape ("),qs=a("code"),an=p("batch_size"),nn=p(" and "),Ks=a("code"),on=p("seq_length"),pn=p(") as the conditional "),Os=a("code"),rn=p("text_embeddings"),hn=p(":"),pl=h(),u(Re.$$.fragment),rl=h(),Ms=a("p"),dn=p("Let\u2019s concatenate the conditional and unconditional embeddings into a batch to avoid doing two forward passes:"),hl=h(),u(xe.$$.fragment),dl=h(),z=a("h3"),le=a("a"),et=a("span"),u(Se.$$.fragment),mn=h(),st=a("span"),cn=p("Create random noise"),ml=h(),ae=a("p"),un=p("Next, generate some initial random noise as a starting point for the diffusion process. This is the latent representation of the image, and it\u2019ll be gradually denoised. At this point, the "),tt=a("code"),fn=p("latent"),bn=p(" image is smaller than the final image size but that\u2019s okay though because the model will transform it into the final 512x512 image dimensions later."),cl=h(),u(ne.$$.fragment),ul=h(),u(Be.$$.fragment),fl=h(),Q=a("h3"),ie=a("a"),lt=a("span"),u(Ae.$$.fragment),yn=h(),at=a("span"),gn=p("Denoise the image"),bl=h(),R=a("p"),Mn=p("Start by scaling the input with the initial noise distribution, "),nt=a("em"),wn=p("sigma"),jn=p(", the noise scale value, which is required for improved schedulers like "),ws=a("a"),vn=p("UniPCMultistepScheduler"),Un=p(":"),yl=h(),u(Xe.$$.fragment),gl=h(),oe=a("p"),Jn=p("The last step is to create the denoising loop that\u2019ll progressively transform the pure noise in "),it=a("code"),_n=p("latents"),Tn=p(" to an image described by your prompt. Remember, the denoising loop needs to do three things:"),Ml=h(),x=a("ol"),ot=a("li"),$n=p("Set the scheduler\u2019s timesteps to use during denoising."),En=h(),pt=a("li"),Zn=p("Iterate over the timesteps."),kn=h(),rt=a("li"),In=p("At each timestep, call the UNet model to predict the noise residual and pass it to the scheduler to compute the previous noisy sample."),wl=h(),u(De.$$.fragment),jl=h(),Y=a("h3"),pe=a("a"),ht=a("span"),u(Fe.$$.fragment),Cn=h(),dt=a("span"),Gn=p("Decode the image"),vl=h(),S=a("p"),Vn=p("The final step is to use the "),mt=a("code"),Wn=p("vae"),Nn=p(" to decode the latent representation into an image and get the decoded output with "),ct=a("code"),Rn=p("sample"),xn=p(":"),Ul=h(),u(ze.$$.fragment),Jl=h(),re=a("p"),Sn=p("Lastly, convert the image to a "),ut=a("code"),Bn=p("PIL.Image"),An=p(" to see your generated image!"),_l=h(),u(Qe.$$.fragment),Tl=h(),Ye=a("div"),ft=a("img"),$l=h(),P=a("h2"),he=a("a"),bt=a("span"),u(Pe.$$.fragment),Xn=h(),yt=a("span"),Dn=p("Next steps"),El=h(),js=a("p"),Fn=p("From basic to complex pipelines, you\u2019ve seen that all you really need to write your own diffusion system is a denoising loop. The loop should set the scheduler\u2019s timesteps, iterate over them, and alternate between calling the UNet model to predict the noise residual and passing it to the scheduler to compute the previous noisy sample."),Zl=h(),vs=a("p"),zn=p("This is really what \u{1F9E8} Diffusers is designed for: to make it intuitive and easy to write your own diffusion system using models and schedulers."),kl=h(),Us=a("p"),Qn=p("For your next steps, feel free to:"),Il=h(),de=a("ul"),He=a("li"),Yn=p("Learn how to "),Js=a("a"),Pn=p("build and contribute a pipeline"),Hn=p(" to \u{1F9E8} Diffusers. We can\u2019t wait and see what you\u2019ll come up with!"),Ln=h(),Le=a("li"),qn=p("Explore "),_s=a("a"),Kn=p("existing pipelines"),On=p(" in the library, and see if you can deconstruct and build a pipeline from scratch using the models and schedulers separately."),this.h()},l(e){const l=Ro('[data-svelte="svelte-1phssyn"]',document.head);c=n(l,"META",{name:!0,content:!0}),l.forEach(s),$=d(e),w=n(e,"H1",{class:!0});var qe=o(w);U=n(qe,"A",{id:!0,class:!0,href:!0});var gt=o(U);T=n(gt,"SPAN",{});var Mt=o(T);f(v.$$.fragment,Mt),Mt.forEach(s),gt.forEach(s),j=d(qe),E=n(qe,"SPAN",{});var li=o(E);_=r(li,"Understanding pipelines, models and schedulers"),li.forEach(s),qe.forEach(s),I=d(e),f(V.$$.fragment,e),wt=d(e),L=n(e,"P",{});var Gl=o(L);Pl=r(Gl,"\u{1F9E8} Diffusers is designed to be a user-friendly and flexible toolbox for building diffusion systems tailored to your use-case. At the core of the toolbox are models and schedulers. While the "),Ke=n(Gl,"A",{href:!0});var ai=o(Ke);Hl=r(ai,"DiffusionPipeline"),ai.forEach(s),Ll=r(Gl," bundles these components together for convenience, you can also unbundle the pipeline and use the models and schedulers separately to create new diffusion systems."),Gl.forEach(s),jt=d(e),Oe=n(e,"P",{});var ni=o(Oe);ql=r(ni,"In this tutorial, you\u2019ll learn how to use models and schedulers to assemble a diffusion system for inference, starting with a basic pipeline and then progressing to the Stable Diffusion pipeline."),ni.forEach(s),vt=d(e),X=n(e,"H2",{class:!0});var Vl=o(X);q=n(Vl,"A",{id:!0,class:!0,href:!0});var ii=o(q);Is=n(ii,"SPAN",{});var oi=o(Is);f(fe.$$.fragment,oi),oi.forEach(s),ii.forEach(s),Kl=d(Vl),Cs=n(Vl,"SPAN",{});var pi=o(Cs);Ol=r(pi,"Deconstruct a basic pipeline"),pi.forEach(s),Vl.forEach(s),Ut=d(e),es=n(e,"P",{});var ri=o(es);ea=r(ri,"A pipeline is a quick and easy way to run a model for inference, requiring no more than four lines of code to generate an image:"),ri.forEach(s),Jt=d(e),f(be.$$.fragment,e),_t=d(e),ye=n(e,"DIV",{class:!0});var hi=o(ye);ss=n(hi,"IMG",{src:!0,alt:!0}),hi.forEach(s),Tt=d(e),ts=n(e,"P",{});var di=o(ts);sa=r(di,"That was super easy, but how did the pipeline do that? Let\u2019s breakdown the pipeline and take a look at what\u2019s happening under the hood."),di.forEach(s),$t=d(e),C=n(e,"P",{});var me=o(C);ta=r(me,"In the example above, the pipeline contains a "),ls=n(me,"A",{href:!0});var mi=o(ls);la=r(mi,"UNet2DModel"),mi.forEach(s),aa=r(me," model and a "),as=n(me,"A",{href:!0});var ci=o(as);na=r(ci,"DDPMScheduler"),ci.forEach(s),ia=r(me,". The pipeline denoises an image by taking random noise the size of the desired output and passing it through the model several times. At each timestep, the model predicts the "),Gs=n(me,"EM",{});var ui=o(Gs);oa=r(ui,"noise residual"),ui.forEach(s),pa=r(me," and the scheduler uses it to predict a less noisy image. The pipeline repeats this process until it reaches the end of the specified number of inference steps."),me.forEach(s),Et=d(e),ns=n(e,"P",{});var fi=o(ns);ra=r(fi,"To recreate the pipeline with the model and scheduler separately, let\u2019s write our own denoising process."),fi.forEach(s),Zt=d(e),is=n(e,"OL",{});var bi=o(is);Vs=n(bi,"LI",{});var yi=o(Vs);ha=r(yi,"Load the model and scheduler:"),yi.forEach(s),bi.forEach(s),kt=d(e),f(ge.$$.fragment,e),It=d(e),Me=n(e,"OL",{start:!0});var gi=o(Me);Ws=n(gi,"LI",{});var Mi=o(Ws);da=r(Mi,"Set the number of timesteps to run the denoising process for:"),Mi.forEach(s),gi.forEach(s),Ct=d(e),f(we.$$.fragment,e),Gt=d(e),je=n(e,"OL",{start:!0});var wi=o(je);Ns=n(wi,"LI",{});var ji=o(Ns);ma=r(ji,"Setting the scheduler timesteps creates a tensor with evenly spaced elements in it, 50 in this example. Each element corresponds to a timestep at which the model denoises an image. When you create the denoising loop later, you\u2019ll iterate over this tensor to denoise an image:"),ji.forEach(s),wi.forEach(s),Vt=d(e),f(ve.$$.fragment,e),Wt=d(e),Ue=n(e,"OL",{start:!0});var vi=o(Ue);Rs=n(vi,"LI",{});var Ui=o(Rs);ca=r(Ui,"Create some random noise with the same shape as the desired output:"),Ui.forEach(s),vi.forEach(s),Nt=d(e),f(Je.$$.fragment,e),Rt=d(e),_e=n(e,"OL",{start:!0});var Ji=o(_e);G=n(Ji,"LI",{});var ce=o(G);ua=r(ce,"Now write a loop to iterate over the timesteps. At each timestep, the model does a "),os=n(ce,"A",{href:!0});var _i=o(os);fa=r(_i,"UNet2DModel.forward()"),_i.forEach(s),ba=r(ce," pass and returns the noisy residual. The scheduler\u2019s "),ps=n(ce,"A",{href:!0});var Ti=o(ps);ya=r(Ti,"step()"),Ti.forEach(s),ga=r(ce," method takes the noisy residual, timestep, and input and it predicts the image at the previous timestep. This output becomes the next input to the model in the denoising loop, and it\u2019ll repeat until it reaches the end of the "),xs=n(ce,"CODE",{});var $i=o(xs);Ma=r($i,"timesteps"),$i.forEach(s),wa=r(ce," array."),ce.forEach(s),Ji.forEach(s),xt=d(e),f(Te.$$.fragment,e),St=d(e),rs=n(e,"P",{});var Ei=o(rs);ja=r(Ei,"This is the entire denoising process, and you can use this same pattern to write any diffusion system."),Ei.forEach(s),Bt=d(e),$e=n(e,"OL",{start:!0});var Zi=o($e);Ss=n(Zi,"LI",{});var ki=o(Ss);va=r(ki,"The last step is to convert the denoised output into an image:"),ki.forEach(s),Zi.forEach(s),At=d(e),f(Ee.$$.fragment,e),Xt=d(e),Z=n(e,"P",{});var B=o(Z);Ua=r(B,"In the next section, you\u2019ll put your skills to the test and breakdown the more complex Stable Diffusion pipeline. The steps are more or less the same. You\u2019ll initialize the necessary components, and set the number of timesteps to create a "),Bs=n(B,"CODE",{});var Ii=o(Bs);Ja=r(Ii,"timestep"),Ii.forEach(s),_a=r(B," array. The "),As=n(B,"CODE",{});var Ci=o(As);Ta=r(Ci,"timestep"),Ci.forEach(s),$a=r(B," array is used in the denoising loop, and for each element in this array, the model predicts a less noisy image. The denoising loop iterates over the "),Xs=n(B,"CODE",{});var Gi=o(Xs);Ea=r(Gi,"timestep"),Gi.forEach(s),Za=r(B,"\u2019s, and at each timestep, it outputs a noisy residual and the scheduler uses it to predict a less noisy image at the previous timestep. This process is repeated until you reach the end of the "),Ds=n(B,"CODE",{});var Vi=o(Ds);ka=r(Vi,"timestep"),Vi.forEach(s),Ia=r(B," array."),B.forEach(s),Dt=d(e),hs=n(e,"P",{});var Wi=o(hs);Ca=r(Wi,"Let\u2019s try it out!"),Wi.forEach(s),Ft=d(e),D=n(e,"H2",{class:!0});var Wl=o(D);K=n(Wl,"A",{id:!0,class:!0,href:!0});var Ni=o(K);Fs=n(Ni,"SPAN",{});var Ri=o(Fs);f(Ze.$$.fragment,Ri),Ri.forEach(s),Ni.forEach(s),Ga=d(Wl),zs=n(Wl,"SPAN",{});var xi=o(zs);Va=r(xi,"Deconstruct the Stable Diffusion pipeline"),xi.forEach(s),Wl.forEach(s),zt=d(e),O=n(e,"P",{});var Nl=o(O);Wa=r(Nl,"Stable Diffusion is a text-to-image "),Qs=n(Nl,"EM",{});var Si=o(Qs);Na=r(Si,"latent diffusion"),Si.forEach(s),Ra=r(Nl," model. It is called a latent diffusion model because it works with a lower-dimensional representation of the image instead of the actual pixel space, which makes it more memory efficient. The encoder compresses the image into a smaller representation, and a decoder to convert the compressed representation back into an image. For text-to-image models, you\u2019ll need a tokenizer and an encoder to generate text embeddings. From the previous example, you already know you need a UNet model and a scheduler."),Nl.forEach(s),Qt=d(e),ds=n(e,"P",{});var Bi=o(ds);xa=r(Bi,"As you can see, this is already more complex than the DDPM pipeline which only contains a UNet model. The Stable Diffusion model has three separate pretrained models."),Bi.forEach(s),Yt=d(e),f(ee.$$.fragment,e),Pt=d(e),W=n(e,"P",{});var Ts=o(W);Sa=r(Ts,"Now that you know what you need for the Stable Diffusion pipeline, load all these components with the "),ms=n(Ts,"A",{href:!0});var Ai=o(ms);Ba=r(Ai,"from_pretrained()"),Ai.forEach(s),Aa=r(Ts," method. You can find them in the pretrained "),ke=n(Ts,"A",{href:!0,rel:!0});var Xi=o(ke);Ys=n(Xi,"CODE",{});var Di=o(Ys);Xa=r(Di,"runwayml/stable-diffusion-v1-5"),Di.forEach(s),Xi.forEach(s),Da=r(Ts," checkpoint, and each component is stored in a separate subfolder:"),Ts.forEach(s),Ht=d(e),f(Ie.$$.fragment,e),Lt=d(e),N=n(e,"P",{});var $s=o(N);Fa=r($s,"Instead of the default "),cs=n($s,"A",{href:!0});var Fi=o(cs);za=r(Fi,"PNDMScheduler"),Fi.forEach(s),Qa=r($s,", exchange it for the "),us=n($s,"A",{href:!0});var zi=o(us);Ya=r(zi,"UniPCMultistepScheduler"),zi.forEach(s),Pa=r($s," to see how easy it is to plug a different scheduler in:"),$s.forEach(s),qt=d(e),f(Ce.$$.fragment,e),Kt=d(e),fs=n(e,"P",{});var Qi=o(fs);Ha=r(Qi,"To speed up inference, move the models to a GPU since, unlike the scheduler, they have trainable weights:"),Qi.forEach(s),Ot=d(e),f(Ge.$$.fragment,e),el=d(e),F=n(e,"H3",{class:!0});var Rl=o(F);se=n(Rl,"A",{id:!0,class:!0,href:!0});var Yi=o(se);Ps=n(Yi,"SPAN",{});var Pi=o(Ps);f(Ve.$$.fragment,Pi),Pi.forEach(s),Yi.forEach(s),La=d(Rl),Hs=n(Rl,"SPAN",{});var Hi=o(Hs);qa=r(Hi,"Create text embeddings"),Hi.forEach(s),Rl.forEach(s),sl=d(e),bs=n(e,"P",{});var Li=o(bs);Ka=r(Li,"The next step is to tokenize the text to generate embeddings. The text is used to condition the UNet model and steer the diffusion process towards something that resembles the input prompt."),Li.forEach(s),tl=d(e),f(te.$$.fragment,e),ll=d(e),ys=n(e,"P",{});var qi=o(ys);Oa=r(qi,"Feel free to choose any prompt you like if you want to generate something else!"),qi.forEach(s),al=d(e),f(We.$$.fragment,e),nl=d(e),gs=n(e,"P",{});var Ki=o(gs);en=r(Ki,"Tokenize the text and generate the embeddings from the prompt:"),Ki.forEach(s),il=d(e),f(Ne.$$.fragment,e),ol=d(e),k=n(e,"P",{});var A=o(k);sn=r(A,"You\u2019ll also need to generate the "),Ls=n(A,"EM",{});var Oi=o(Ls);tn=r(Oi,"unconditional text embeddings"),Oi.forEach(s),ln=r(A," which are the embeddings for the padding token. These need to have the same shape ("),qs=n(A,"CODE",{});var eo=o(qs);an=r(eo,"batch_size"),eo.forEach(s),nn=r(A," and "),Ks=n(A,"CODE",{});var so=o(Ks);on=r(so,"seq_length"),so.forEach(s),pn=r(A,") as the conditional "),Os=n(A,"CODE",{});var to=o(Os);rn=r(to,"text_embeddings"),to.forEach(s),hn=r(A,":"),A.forEach(s),pl=d(e),f(Re.$$.fragment,e),rl=d(e),Ms=n(e,"P",{});var lo=o(Ms);dn=r(lo,"Let\u2019s concatenate the conditional and unconditional embeddings into a batch to avoid doing two forward passes:"),lo.forEach(s),hl=d(e),f(xe.$$.fragment,e),dl=d(e),z=n(e,"H3",{class:!0});var xl=o(z);le=n(xl,"A",{id:!0,class:!0,href:!0});var ao=o(le);et=n(ao,"SPAN",{});var no=o(et);f(Se.$$.fragment,no),no.forEach(s),ao.forEach(s),mn=d(xl),st=n(xl,"SPAN",{});var io=o(st);cn=r(io,"Create random noise"),io.forEach(s),xl.forEach(s),ml=d(e),ae=n(e,"P",{});var Sl=o(ae);un=r(Sl,"Next, generate some initial random noise as a starting point for the diffusion process. This is the latent representation of the image, and it\u2019ll be gradually denoised. At this point, the "),tt=n(Sl,"CODE",{});var oo=o(tt);fn=r(oo,"latent"),oo.forEach(s),bn=r(Sl," image is smaller than the final image size but that\u2019s okay though because the model will transform it into the final 512x512 image dimensions later."),Sl.forEach(s),cl=d(e),f(ne.$$.fragment,e),ul=d(e),f(Be.$$.fragment,e),fl=d(e),Q=n(e,"H3",{class:!0});var Bl=o(Q);ie=n(Bl,"A",{id:!0,class:!0,href:!0});var po=o(ie);lt=n(po,"SPAN",{});var ro=o(lt);f(Ae.$$.fragment,ro),ro.forEach(s),po.forEach(s),yn=d(Bl),at=n(Bl,"SPAN",{});var ho=o(at);gn=r(ho,"Denoise the image"),ho.forEach(s),Bl.forEach(s),bl=d(e),R=n(e,"P",{});var Es=o(R);Mn=r(Es,"Start by scaling the input with the initial noise distribution, "),nt=n(Es,"EM",{});var mo=o(nt);wn=r(mo,"sigma"),mo.forEach(s),jn=r(Es,", the noise scale value, which is required for improved schedulers like "),ws=n(Es,"A",{href:!0});var co=o(ws);vn=r(co,"UniPCMultistepScheduler"),co.forEach(s),Un=r(Es,":"),Es.forEach(s),yl=d(e),f(Xe.$$.fragment,e),gl=d(e),oe=n(e,"P",{});var Al=o(oe);Jn=r(Al,"The last step is to create the denoising loop that\u2019ll progressively transform the pure noise in "),it=n(Al,"CODE",{});var uo=o(it);_n=r(uo,"latents"),uo.forEach(s),Tn=r(Al," to an image described by your prompt. Remember, the denoising loop needs to do three things:"),Al.forEach(s),Ml=d(e),x=n(e,"OL",{});var Zs=o(x);ot=n(Zs,"LI",{});var fo=o(ot);$n=r(fo,"Set the scheduler\u2019s timesteps to use during denoising."),fo.forEach(s),En=d(Zs),pt=n(Zs,"LI",{});var bo=o(pt);Zn=r(bo,"Iterate over the timesteps."),bo.forEach(s),kn=d(Zs),rt=n(Zs,"LI",{});var yo=o(rt);In=r(yo,"At each timestep, call the UNet model to predict the noise residual and pass it to the scheduler to compute the previous noisy sample."),yo.forEach(s),Zs.forEach(s),wl=d(e),f(De.$$.fragment,e),jl=d(e),Y=n(e,"H3",{class:!0});var Xl=o(Y);pe=n(Xl,"A",{id:!0,class:!0,href:!0});var go=o(pe);ht=n(go,"SPAN",{});var Mo=o(ht);f(Fe.$$.fragment,Mo),Mo.forEach(s),go.forEach(s),Cn=d(Xl),dt=n(Xl,"SPAN",{});var wo=o(dt);Gn=r(wo,"Decode the image"),wo.forEach(s),Xl.forEach(s),vl=d(e),S=n(e,"P",{});var ks=o(S);Vn=r(ks,"The final step is to use the "),mt=n(ks,"CODE",{});var jo=o(mt);Wn=r(jo,"vae"),jo.forEach(s),Nn=r(ks," to decode the latent representation into an image and get the decoded output with "),ct=n(ks,"CODE",{});var vo=o(ct);Rn=r(vo,"sample"),vo.forEach(s),xn=r(ks,":"),ks.forEach(s),Ul=d(e),f(ze.$$.fragment,e),Jl=d(e),re=n(e,"P",{});var Dl=o(re);Sn=r(Dl,"Lastly, convert the image to a "),ut=n(Dl,"CODE",{});var Uo=o(ut);Bn=r(Uo,"PIL.Image"),Uo.forEach(s),An=r(Dl," to see your generated image!"),Dl.forEach(s),_l=d(e),f(Qe.$$.fragment,e),Tl=d(e),Ye=n(e,"DIV",{class:!0});var Jo=o(Ye);ft=n(Jo,"IMG",{src:!0}),Jo.forEach(s),$l=d(e),P=n(e,"H2",{class:!0});var Fl=o(P);he=n(Fl,"A",{id:!0,class:!0,href:!0});var _o=o(he);bt=n(_o,"SPAN",{});var To=o(bt);f(Pe.$$.fragment,To),To.forEach(s),_o.forEach(s),Xn=d(Fl),yt=n(Fl,"SPAN",{});var $o=o(yt);Dn=r($o,"Next steps"),$o.forEach(s),Fl.forEach(s),El=d(e),js=n(e,"P",{});var Eo=o(js);Fn=r(Eo,"From basic to complex pipelines, you\u2019ve seen that all you really need to write your own diffusion system is a denoising loop. The loop should set the scheduler\u2019s timesteps, iterate over them, and alternate between calling the UNet model to predict the noise residual and passing it to the scheduler to compute the previous noisy sample."),Eo.forEach(s),Zl=d(e),vs=n(e,"P",{});var Zo=o(vs);zn=r(Zo,"This is really what \u{1F9E8} Diffusers is designed for: to make it intuitive and easy to write your own diffusion system using models and schedulers."),Zo.forEach(s),kl=d(e),Us=n(e,"P",{});var ko=o(Us);Qn=r(ko,"For your next steps, feel free to:"),ko.forEach(s),Il=d(e),de=n(e,"UL",{});var zl=o(de);He=n(zl,"LI",{});var Ql=o(He);Yn=r(Ql,"Learn how to "),Js=n(Ql,"A",{href:!0});var Io=o(Js);Pn=r(Io,"build and contribute a pipeline"),Io.forEach(s),Hn=r(Ql," to \u{1F9E8} Diffusers. We can\u2019t wait and see what you\u2019ll come up with!"),Ql.forEach(s),Ln=d(zl),Le=n(zl,"LI",{});var Yl=o(Le);qn=r(Yl,"Explore "),_s=n(Yl,"A",{href:!0});var Co=o(_s);Kn=r(Co,"existing pipelines"),Co.forEach(s),On=r(Yl," in the library, and see if you can deconstruct and build a pipeline from scratch using the models and schedulers separately."),Yl.forEach(s),zl.forEach(s),this.h()},h(){m(c,"name","hf:doc:metadata"),m(c,"content",JSON.stringify(zo)),m(U,"id","understanding-pipelines-models-and-schedulers"),m(U,"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(U,"href","#understanding-pipelines-models-and-schedulers"),m(w,"class","relative group"),m(Ke,"href","/docs/diffusers/main/en/api/pipelines/overview#diffusers.DiffusionPipeline"),m(q,"id","deconstruct-a-basic-pipeline"),m(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"),m(q,"href","#deconstruct-a-basic-pipeline"),m(X,"class","relative group"),Go(ss.src,si="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ddpm-cat.png")||m(ss,"src",si),m(ss,"alt","Image of cat created from DDPMPipeline"),m(ye,"class","flex justify-center"),m(ls,"href","/docs/diffusers/main/en/api/models/unet2d#diffusers.UNet2DModel"),m(as,"href","/docs/diffusers/main/en/api/schedulers/ddpm#diffusers.DDPMScheduler"),m(Me,"start","2"),m(je,"start","3"),m(Ue,"start","4"),m(os,"href","/docs/diffusers/main/en/api/models/unet2d#diffusers.UNet2DModel.forward"),m(ps,"href","/docs/diffusers/main/en/api/schedulers/ddpm#diffusers.DDPMScheduler.step"),m(_e,"start","5"),m($e,"start","6"),m(K,"id","deconstruct-the-stable-diffusion-pipeline"),m(K,"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(K,"href","#deconstruct-the-stable-diffusion-pipeline"),m(D,"class","relative group"),m(ms,"href","/docs/diffusers/main/en/api/models/overview#diffusers.ModelMixin.from_pretrained"),m(ke,"href","https://huggingface.co/runwayml/stable-diffusion-v1-5"),m(ke,"rel","nofollow"),m(cs,"href","/docs/diffusers/main/en/api/schedulers/pndm#diffusers.PNDMScheduler"),m(us,"href","/docs/diffusers/main/en/api/schedulers/unipc#diffusers.UniPCMultistepScheduler"),m(se,"id","create-text-embeddings"),m(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"),m(se,"href","#create-text-embeddings"),m(F,"class","relative group"),m(le,"id","create-random-noise"),m(le,"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(le,"href","#create-random-noise"),m(z,"class","relative group"),m(ie,"id","denoise-the-image"),m(ie,"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(ie,"href","#denoise-the-image"),m(Q,"class","relative group"),m(ws,"href","/docs/diffusers/main/en/api/schedulers/unipc#diffusers.UniPCMultistepScheduler"),m(pe,"id","decode-the-image"),m(pe,"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(pe,"href","#decode-the-image"),m(Y,"class","relative group"),Go(ft.src,ti="https://huggingface.co/blog/assets/98_stable_diffusion/stable_diffusion_k_lms.png")||m(ft,"src",ti),m(Ye,"class","flex justify-center"),m(he,"id","next-steps"),m(he,"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(he,"href","#next-steps"),m(P,"class","relative group"),m(Js,"href","contribute_pipeline"),m(_s,"href","../api/pipelines/overview")},m(e,l){t(document.head,c),i(e,$,l),i(e,w,l),t(w,U),t(U,T),b(v,T,null),t(w,j),t(w,E),t(E,_),i(e,I,l),b(V,e,l),i(e,wt,l),i(e,L,l),t(L,Pl),t(L,Ke),t(Ke,Hl),t(L,Ll),i(e,jt,l),i(e,Oe,l),t(Oe,ql),i(e,vt,l),i(e,X,l),t(X,q),t(q,Is),b(fe,Is,null),t(X,Kl),t(X,Cs),t(Cs,Ol),i(e,Ut,l),i(e,es,l),t(es,ea),i(e,Jt,l),b(be,e,l),i(e,_t,l),i(e,ye,l),t(ye,ss),i(e,Tt,l),i(e,ts,l),t(ts,sa),i(e,$t,l),i(e,C,l),t(C,ta),t(C,ls),t(ls,la),t(C,aa),t(C,as),t(as,na),t(C,ia),t(C,Gs),t(Gs,oa),t(C,pa),i(e,Et,l),i(e,ns,l),t(ns,ra),i(e,Zt,l),i(e,is,l),t(is,Vs),t(Vs,ha),i(e,kt,l),b(ge,e,l),i(e,It,l),i(e,Me,l),t(Me,Ws),t(Ws,da),i(e,Ct,l),b(we,e,l),i(e,Gt,l),i(e,je,l),t(je,Ns),t(Ns,ma),i(e,Vt,l),b(ve,e,l),i(e,Wt,l),i(e,Ue,l),t(Ue,Rs),t(Rs,ca),i(e,Nt,l),b(Je,e,l),i(e,Rt,l),i(e,_e,l),t(_e,G),t(G,ua),t(G,os),t(os,fa),t(G,ba),t(G,ps),t(ps,ya),t(G,ga),t(G,xs),t(xs,Ma),t(G,wa),i(e,xt,l),b(Te,e,l),i(e,St,l),i(e,rs,l),t(rs,ja),i(e,Bt,l),i(e,$e,l),t($e,Ss),t(Ss,va),i(e,At,l),b(Ee,e,l),i(e,Xt,l),i(e,Z,l),t(Z,Ua),t(Z,Bs),t(Bs,Ja),t(Z,_a),t(Z,As),t(As,Ta),t(Z,$a),t(Z,Xs),t(Xs,Ea),t(Z,Za),t(Z,Ds),t(Ds,ka),t(Z,Ia),i(e,Dt,l),i(e,hs,l),t(hs,Ca),i(e,Ft,l),i(e,D,l),t(D,K),t(K,Fs),b(Ze,Fs,null),t(D,Ga),t(D,zs),t(zs,Va),i(e,zt,l),i(e,O,l),t(O,Wa),t(O,Qs),t(Qs,Na),t(O,Ra),i(e,Qt,l),i(e,ds,l),t(ds,xa),i(e,Yt,l),b(ee,e,l),i(e,Pt,l),i(e,W,l),t(W,Sa),t(W,ms),t(ms,Ba),t(W,Aa),t(W,ke),t(ke,Ys),t(Ys,Xa),t(W,Da),i(e,Ht,l),b(Ie,e,l),i(e,Lt,l),i(e,N,l),t(N,Fa),t(N,cs),t(cs,za),t(N,Qa),t(N,us),t(us,Ya),t(N,Pa),i(e,qt,l),b(Ce,e,l),i(e,Kt,l),i(e,fs,l),t(fs,Ha),i(e,Ot,l),b(Ge,e,l),i(e,el,l),i(e,F,l),t(F,se),t(se,Ps),b(Ve,Ps,null),t(F,La),t(F,Hs),t(Hs,qa),i(e,sl,l),i(e,bs,l),t(bs,Ka),i(e,tl,l),b(te,e,l),i(e,ll,l),i(e,ys,l),t(ys,Oa),i(e,al,l),b(We,e,l),i(e,nl,l),i(e,gs,l),t(gs,en),i(e,il,l),b(Ne,e,l),i(e,ol,l),i(e,k,l),t(k,sn),t(k,Ls),t(Ls,tn),t(k,ln),t(k,qs),t(qs,an),t(k,nn),t(k,Ks),t(Ks,on),t(k,pn),t(k,Os),t(Os,rn),t(k,hn),i(e,pl,l),b(Re,e,l),i(e,rl,l),i(e,Ms,l),t(Ms,dn),i(e,hl,l),b(xe,e,l),i(e,dl,l),i(e,z,l),t(z,le),t(le,et),b(Se,et,null),t(z,mn),t(z,st),t(st,cn),i(e,ml,l),i(e,ae,l),t(ae,un),t(ae,tt),t(tt,fn),t(ae,bn),i(e,cl,l),b(ne,e,l),i(e,ul,l),b(Be,e,l),i(e,fl,l),i(e,Q,l),t(Q,ie),t(ie,lt),b(Ae,lt,null),t(Q,yn),t(Q,at),t(at,gn),i(e,bl,l),i(e,R,l),t(R,Mn),t(R,nt),t(nt,wn),t(R,jn),t(R,ws),t(ws,vn),t(R,Un),i(e,yl,l),b(Xe,e,l),i(e,gl,l),i(e,oe,l),t(oe,Jn),t(oe,it),t(it,_n),t(oe,Tn),i(e,Ml,l),i(e,x,l),t(x,ot),t(ot,$n),t(x,En),t(x,pt),t(pt,Zn),t(x,kn),t(x,rt),t(rt,In),i(e,wl,l),b(De,e,l),i(e,jl,l),i(e,Y,l),t(Y,pe),t(pe,ht),b(Fe,ht,null),t(Y,Cn),t(Y,dt),t(dt,Gn),i(e,vl,l),i(e,S,l),t(S,Vn),t(S,mt),t(mt,Wn),t(S,Nn),t(S,ct),t(ct,Rn),t(S,xn),i(e,Ul,l),b(ze,e,l),i(e,Jl,l),i(e,re,l),t(re,Sn),t(re,ut),t(ut,Bn),t(re,An),i(e,_l,l),b(Qe,e,l),i(e,Tl,l),i(e,Ye,l),t(Ye,ft),i(e,$l,l),i(e,P,l),t(P,he),t(he,bt),b(Pe,bt,null),t(P,Xn),t(P,yt),t(yt,Dn),i(e,El,l),i(e,js,l),t(js,Fn),i(e,Zl,l),i(e,vs,l),t(vs,zn),i(e,kl,l),i(e,Us,l),t(Us,Qn),i(e,Il,l),i(e,de,l),t(de,He),t(He,Yn),t(He,Js),t(Js,Pn),t(He,Hn),t(de,Ln),t(de,Le),t(Le,qn),t(Le,_s),t(_s,Kn),t(Le,On),Cl=!0},p(e,[l]){const qe={};l&2&&(qe.$$scope={dirty:l,ctx:e}),ee.$set(qe);const gt={};l&2&&(gt.$$scope={dirty:l,ctx:e}),te.$set(gt);const Mt={};l&2&&(Mt.$$scope={dirty:l,ctx:e}),ne.$set(Mt)},i(e){Cl||(y(v.$$.fragment,e),y(V.$$.fragment,e),y(fe.$$.fragment,e),y(be.$$.fragment,e),y(ge.$$.fragment,e),y(we.$$.fragment,e),y(ve.$$.fragment,e),y(Je.$$.fragment,e),y(Te.$$.fragment,e),y(Ee.$$.fragment,e),y(Ze.$$.fragment,e),y(ee.$$.fragment,e),y(Ie.$$.fragment,e),y(Ce.$$.fragment,e),y(Ge.$$.fragment,e),y(Ve.$$.fragment,e),y(te.$$.fragment,e),y(We.$$.fragment,e),y(Ne.$$.fragment,e),y(Re.$$.fragment,e),y(xe.$$.fragment,e),y(Se.$$.fragment,e),y(ne.$$.fragment,e),y(Be.$$.fragment,e),y(Ae.$$.fragment,e),y(Xe.$$.fragment,e),y(De.$$.fragment,e),y(Fe.$$.fragment,e),y(ze.$$.fragment,e),y(Qe.$$.fragment,e),y(Pe.$$.fragment,e),Cl=!0)},o(e){g(v.$$.fragment,e),g(V.$$.fragment,e),g(fe.$$.fragment,e),g(be.$$.fragment,e),g(ge.$$.fragment,e),g(we.$$.fragment,e),g(ve.$$.fragment,e),g(Je.$$.fragment,e),g(Te.$$.fragment,e),g(Ee.$$.fragment,e),g(Ze.$$.fragment,e),g(ee.$$.fragment,e),g(Ie.$$.fragment,e),g(Ce.$$.fragment,e),g(Ge.$$.fragment,e),g(Ve.$$.fragment,e),g(te.$$.fragment,e),g(We.$$.fragment,e),g(Ne.$$.fragment,e),g(Re.$$.fragment,e),g(xe.$$.fragment,e),g(Se.$$.fragment,e),g(ne.$$.fragment,e),g(Be.$$.fragment,e),g(Ae.$$.fragment,e),g(Xe.$$.fragment,e),g(De.$$.fragment,e),g(Fe.$$.fragment,e),g(ze.$$.fragment,e),g(Qe.$$.fragment,e),g(Pe.$$.fragment,e),Cl=!1},d(e){s(c),e&&s($),e&&s(w),M(v),e&&s(I),M(V,e),e&&s(wt),e&&s(L),e&&s(jt),e&&s(Oe),e&&s(vt),e&&s(X),M(fe),e&&s(Ut),e&&s(es),e&&s(Jt),M(be,e),e&&s(_t),e&&s(ye),e&&s(Tt),e&&s(ts),e&&s($t),e&&s(C),e&&s(Et),e&&s(ns),e&&s(Zt),e&&s(is),e&&s(kt),M(ge,e),e&&s(It),e&&s(Me),e&&s(Ct),M(we,e),e&&s(Gt),e&&s(je),e&&s(Vt),M(ve,e),e&&s(Wt),e&&s(Ue),e&&s(Nt),M(Je,e),e&&s(Rt),e&&s(_e),e&&s(xt),M(Te,e),e&&s(St),e&&s(rs),e&&s(Bt),e&&s($e),e&&s(At),M(Ee,e),e&&s(Xt),e&&s(Z),e&&s(Dt),e&&s(hs),e&&s(Ft),e&&s(D),M(Ze),e&&s(zt),e&&s(O),e&&s(Qt),e&&s(ds),e&&s(Yt),M(ee,e),e&&s(Pt),e&&s(W),e&&s(Ht),M(Ie,e),e&&s(Lt),e&&s(N),e&&s(qt),M(Ce,e),e&&s(Kt),e&&s(fs),e&&s(Ot),M(Ge,e),e&&s(el),e&&s(F),M(Ve),e&&s(sl),e&&s(bs),e&&s(tl),M(te,e),e&&s(ll),e&&s(ys),e&&s(al),M(We,e),e&&s(nl),e&&s(gs),e&&s(il),M(Ne,e),e&&s(ol),e&&s(k),e&&s(pl),M(Re,e),e&&s(rl),e&&s(Ms),e&&s(hl),M(xe,e),e&&s(dl),e&&s(z),M(Se),e&&s(ml),e&&s(ae),e&&s(cl),M(ne,e),e&&s(ul),M(Be,e),e&&s(fl),e&&s(Q),M(Ae),e&&s(bl),e&&s(R),e&&s(yl),M(Xe,e),e&&s(gl),e&&s(oe),e&&s(Ml),e&&s(x),e&&s(wl),M(De,e),e&&s(jl),e&&s(Y),M(Fe),e&&s(vl),e&&s(S),e&&s(Ul),M(ze,e),e&&s(Jl),e&&s(re),e&&s(_l),M(Qe,e),e&&s(Tl),e&&s(Ye),e&&s($l),e&&s(P),M(Pe),e&&s(El),e&&s(js),e&&s(Zl),e&&s(vs),e&&s(kl),e&&s(Us),e&&s(Il),e&&s(de)}}}const zo={local:"understanding-pipelines-models-and-schedulers",sections:[{local:"deconstruct-a-basic-pipeline",title:"Deconstruct a basic pipeline"},{local:"deconstruct-the-stable-diffusion-pipeline",sections:[{local:"create-text-embeddings",title:"Create text embeddings"},{local:"create-random-noise",title:"Create random noise"},{local:"denoise-the-image",title:"Denoise the image"},{local:"decode-the-image",title:"Decode the image"}],title:"Deconstruct the Stable Diffusion pipeline"},{local:"next-steps",title:"Next steps"}],title:"Understanding pipelines, models and schedulers"};function Qo(H){return xo(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ko extends Vo{constructor(c){super();Wo(this,c,Qo,Fo,No,{})}}export{Ko as default,zo as metadata}; | |
Xet Storage Details
- Size:
- 62.1 kB
- Xet hash:
- 857cedf47193c6279cdac705d9d14b5b845d32e834d773383002d91a6b2b7d37
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.