Buckets:
hf-doc-build/doc / diffusers /v0.17.0 /en /_app /pages /using-diffusers /write_own_pipeline.mdx-hf-doc-builder.js
| import{S as Wo,i as Ro,s as xo,e as l,k as h,w as u,t as o,M as So,c as n,d as s,m as d,a as i,x as f,h as p,b as m,N as No,G as t,g as r,y as b,q as g,o as y,B as M,v as Xo,L as Bo}from"../../chunks/vendor-hf-doc-builder.js";import{T as pi}from"../../chunks/Tip-hf-doc-builder.js";import{I as ge}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as _}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Ao}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Do(K){let c,$,w,U,T;return{c(){c=l("p"),$=o("\u{1F4A1} Read the "),w=l("a"),U=o("How does Stable Diffusion work?"),T=o(" blog for more details about how the VAE, UNet, and text encoder models."),this.h()},l(v){c=n(v,"P",{});var j=i(c);$=p(j,"\u{1F4A1} Read the "),w=n(j,"A",{href:!0,rel:!0});var E=i(w);U=p(E,"How does Stable Diffusion work?"),E.forEach(s),T=p(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){r(v,c,j),t(c,$),t(c,w),t(w,U),t(c,T)},d(v){v&&s(c)}}}function Fo(K){let c,$,w,U,T;return{c(){c=l("p"),$=o("\u{1F4A1} The "),w=l("code"),U=o("guidance_scale"),T=o(" parameter determines how much weight should be given to the prompt when generating an image.")},l(v){c=n(v,"P",{});var j=i(c);$=p(j,"\u{1F4A1} The "),w=n(j,"CODE",{});var E=i(w);U=p(E,"guidance_scale"),E.forEach(s),T=p(j," parameter determines how much weight should be given to the prompt when generating an image."),j.forEach(s)},m(v,j){r(v,c,j),t(c,$),t(c,w),t(w,U),t(c,T)},d(v){v&&s(c)}}}function zo(K){let c,$,w,U,T,v,j,E;return j=new _({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=l("p"),$=o("\u{1F4A1} The height and width are divided by 8 because the "),w=l("code"),U=o("vae"),T=o(" model has 3 down-sampling layers. You can check by running the following:"),v=h(),u(j.$$.fragment)},l(J){c=n(J,"P",{});var C=i(c);$=p(C,"\u{1F4A1} The height and width are divided by 8 because the "),w=n(C,"CODE",{});var W=i(w);U=p(W,"vae"),W.forEach(s),T=p(C," model has 3 down-sampling layers. You can check by running the following:"),C.forEach(s),v=d(J),f(j.$$.fragment,J)},m(J,C){r(J,c,C),t(c,$),t(c,w),t(w,U),t(c,T),r(J,v,C),b(j,J,C),E=!0},p:Bo,i(J){E||(g(j.$$.fragment,J),E=!0)},o(J){y(j.$$.fragment,J),E=!1},d(J){J&&s(c),J&&s(v),M(j,J)}}}function Yo(K){let c,$,w,U,T,v,j,E,J,C,W,Ut,O,Fa,ss,za,Ya,_t,ts,Qa,Jt,F,ee,Gs,ye,Pa,Vs,La,Tt,as,Ha,$t,Me,Et,we,ls,ri,kt,ns,qa,Zt,G,Ka,is,Oa,el,os,sl,tl,Ns,al,ll,It,ps,nl,Ct,k,je,Ws,il,ol,ve,pl,Ue,Rs,rl,hl,_e,dl,Je,xs,ml,cl,Te,ul,$e,Ss,fl,bl,Ee,gl,z,N,yl,rs,Ml,wl,hs,jl,vl,Xs,Ul,_l,Jl,ke,Tl,Bs,$l,El,Ze,As,kl,Zl,Ie,Gt,Z,Il,Ds,Cl,Gl,Fs,Vl,Nl,zs,Wl,Rl,Ys,xl,Sl,Vt,ds,Xl,Nt,Y,se,Qs,Ce,Bl,Ps,Al,Wt,te,Dl,Ls,Fl,zl,Rt,ms,Yl,xt,ae,St,R,Ql,cs,Pl,Ll,Ge,Hs,Hl,ql,Xt,Ve,Bt,x,Kl,us,Ol,en,fs,sn,tn,At,Ne,Dt,bs,an,Ft,We,zt,Q,le,qs,Re,ln,Ks,nn,Yt,gs,on,Qt,ne,Pt,ys,pn,Lt,xe,Ht,Ms,rn,qt,Se,Kt,I,hn,Os,dn,mn,et,cn,un,st,fn,bn,tt,gn,yn,Ot,Xe,ea,ws,Mn,sa,Be,ta,P,ie,at,Ae,wn,lt,jn,aa,oe,vn,nt,Un,_n,la,pe,na,De,ia,L,re,it,Fe,Jn,ot,Tn,oa,S,$n,pt,En,kn,js,Zn,In,pa,ze,ra,he,Cn,rt,Gn,Vn,ha,X,ht,Nn,Wn,dt,Rn,xn,mt,Sn,da,Ye,ma,H,de,ct,Qe,Xn,ut,Bn,ca,B,An,ft,Dn,Fn,bt,zn,Yn,ua,Pe,fa,me,Qn,gt,Pn,Ln,ba,Le,ga,He,yt,hi,ya,q,ce,Mt,qe,Hn,wt,qn,Ma,vs,Kn,wa,Us,On,ja,_s,ei,va,ue,Ke,si,Js,ti,ai,li,Oe,ni,Ts,ii,oi,Ua;return v=new ge({}),W=new Ao({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"}]}}),ye=new ge({}),Me=new _({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEREUE1QaXBlbGluZSUwQSUwQWRkcG0lMjAlM0QlMjBERFBNUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmRkcG0tY2F0LTI1NiUyMikudG8oJTIyY3VkYSUyMiklMEFpbWFnZSUyMCUzRCUyMGRkcG0obnVtX2luZmVyZW5jZV9zdGVwcyUzRDI1KS5pbWFnZXMlNUIwJTVEJTBBaW1hZ2U=",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>).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`}}),ve=new _({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEREUE1TY2hlZHVsZXIlMkMlMjBVTmV0MkRNb2RlbCUwQSUwQXNjaGVkdWxlciUyMCUzRCUyMEREUE1TY2hlZHVsZXIuZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmRkcG0tY2F0LTI1NiUyMiklMEFtb2RlbCUyMCUzRCUyMFVOZXQyRE1vZGVsLmZyb21fcHJldHJhaW5lZCglMjJnb29nbGUlMkZkZHBtLWNhdC0yNTYlMjIpLnRvKCUyMmN1ZGElMjIp",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>).to(<span class="hljs-string">"cuda"</span>)`}}),_e=new _({props:{code:"c2NoZWR1bGVyLnNldF90aW1lc3RlcHMoNTAp",highlighted:'<span class="hljs-meta">>>> </span>scheduler.set_timesteps(<span class="hljs-number">50</span>)'}}),Te=new _({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>])`}}),Ee=new _({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>)`}}),ke=new _({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`}}),Ie=new _({props:{code:"ZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBJTBBaW1hZ2UlMjAlM0QlMjAoaW5wdXQlMjAlMkYlMjAyJTIwJTJCJTIwMC41KS5jbGFtcCgwJTJDJTIwMSklMEFpbWFnZSUyMCUzRCUyMGltYWdlLmNwdSgpLnBlcm11dGUoMCUyQyUyMDIlMkMlMjAzJTJDJTIwMSkubnVtcHkoKSU1QjAlNUQlMEFpbWFnZSUyMCUzRCUyMEltYWdlLmZyb21hcnJheSgoaW1hZ2UlMjAqJTIwMjU1KS5yb3VuZCgpLmFzdHlwZSglMjJ1aW50OCUyMikpJTBBaW1hZ2U=",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>) | |
| <span class="hljs-meta">>>> </span>image = image.cpu().permute(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>).numpy()[<span class="hljs-number">0</span>] | |
| <span class="hljs-meta">>>> </span>image = Image.fromarray((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`}}),Ce=new ge({}),ae=new pi({props:{$$slots:{default:[Do]},$$scope:{ctx:K}}}),Ve=new _({props:{code:"ZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQ0xJUFRleHRNb2RlbCUyQyUyMENMSVBUb2tlbml6ZXIlMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b2VuY29kZXJLTCUyQyUyMFVOZXQyRENvbmRpdGlvbk1vZGVsJTJDJTIwUE5ETVNjaGVkdWxlciUwQSUwQXZhZSUyMCUzRCUyMEF1dG9lbmNvZGVyS0wuZnJvbV9wcmV0cmFpbmVkKCUyMkNvbXBWaXMlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTQlMjIlMkMlMjBzdWJmb2xkZXIlM0QlMjJ2YWUlMjIpJTBBdG9rZW5pemVyJTIwJTNEJTIwQ0xJUFRva2VuaXplci5mcm9tX3ByZXRyYWluZWQoJTIyQ29tcFZpcyUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNCUyMiUyQyUyMHN1YmZvbGRlciUzRCUyMnRva2VuaXplciUyMiklMEF0ZXh0X2VuY29kZXIlMjAlM0QlMjBDTElQVGV4dE1vZGVsLmZyb21fcHJldHJhaW5lZCglMjJDb21wVmlzJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS00JTIyJTJDJTIwc3ViZm9sZGVyJTNEJTIydGV4dF9lbmNvZGVyJTIyKSUwQXVuZXQlMjAlM0QlMjBVTmV0MkRDb25kaXRpb25Nb2RlbC5mcm9tX3ByZXRyYWluZWQoJTIyQ29tcFZpcyUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNCUyMiUyQyUyMHN1YmZvbGRlciUzRCUyMnVuZXQlMjIp",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>) | |
| <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-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"text_encoder"</span>) | |
| <span class="hljs-meta">>>> </span>unet = UNet2DConditionModel.from_pretrained(<span class="hljs-string">"CompVis/stable-diffusion-v1-4"</span>, subfolder=<span class="hljs-string">"unet"</span>)`}}),Ne=new _({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>)`}}),We=new _({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)`}}),Re=new ge({}),ne=new pi({props:{$$slots:{default:[Fo]},$$scope:{ctx:K}}}),xe=new _({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)`}}),Se=new _({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>]`}}),Xe=new _({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>]`}}),Be=new _({props:{code:"dGV4dF9lbWJlZGRpbmdzJTIwJTNEJTIwdG9yY2guY2F0KCU1QnVuY29uZF9lbWJlZGRpbmdzJTJDJTIwdGV4dF9lbWJlZGRpbmdzJTVEKQ==",highlighted:'<span class="hljs-meta">>>> </span>text_embeddings = torch.cat([uncond_embeddings, text_embeddings])'}}),Ae=new ge({}),pe=new pi({props:{$$slots:{default:[zo]},$$scope:{ctx:K}}}),De=new _({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)`}}),Fe=new ge({}),ze=new _({props:{code:"bGF0ZW50cyUyMCUzRCUyMGxhdGVudHMlMjAqJTIwc2NoZWR1bGVyLmluaXRfbm9pc2Vfc2lnbWE=",highlighted:'<span class="hljs-meta">>>> </span>latents = latents * scheduler.init_noise_sigma'}}),Ye=new _({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`}}),Qe=new ge({}),Pe=new _({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`}}),Le=new _({props:{code:"aW1hZ2UlMjAlM0QlMjAoaW1hZ2UlMjAlMkYlMjAyJTIwJTJCJTIwMC41KS5jbGFtcCgwJTJDJTIwMSklMEFpbWFnZSUyMCUzRCUyMGltYWdlLmRldGFjaCgpLmNwdSgpLnBlcm11dGUoMCUyQyUyMDIlMkMlMjAzJTJDJTIwMSkubnVtcHkoKSUwQWltYWdlcyUyMCUzRCUyMChpbWFnZSUyMColMjAyNTUpLnJvdW5kKCkuYXN0eXBlKCUyMnVpbnQ4JTIyKSUwQXBpbF9pbWFnZXMlMjAlM0QlMjAlNUJJbWFnZS5mcm9tYXJyYXkoaW1hZ2UpJTIwZm9yJTIwaW1hZ2UlMjBpbiUyMGltYWdlcyU1RCUwQXBpbF9pbWFnZXMlNUIwJTVE",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>) | |
| <span class="hljs-meta">>>> </span>image = image.detach().cpu().permute(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>).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>pil_images = [Image.fromarray(image) <span class="hljs-keyword">for</span> image <span class="hljs-keyword">in</span> images] | |
| <span class="hljs-meta">>>> </span>pil_images[<span class="hljs-number">0</span>]`}}),qe=new ge({}),{c(){c=l("meta"),$=h(),w=l("h1"),U=l("a"),T=l("span"),u(v.$$.fragment),j=h(),E=l("span"),J=o("Understanding pipelines, models and schedulers"),C=h(),u(W.$$.fragment),Ut=h(),O=l("p"),Fa=o("\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 "),ss=l("a"),za=o("DiffusionPipeline"),Ya=o(" bundles these components together for convenience, you can also unbundle the pipeline and use the models and schedulers separately to create new diffusion systems."),_t=h(),ts=l("p"),Qa=o("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."),Jt=h(),F=l("h2"),ee=l("a"),Gs=l("span"),u(ye.$$.fragment),Pa=h(),Vs=l("span"),La=o("Deconstruct a basic pipeline"),Tt=h(),as=l("p"),Ha=o("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:"),$t=h(),u(Me.$$.fragment),Et=h(),we=l("div"),ls=l("img"),kt=h(),ns=l("p"),qa=o("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."),Zt=h(),G=l("p"),Ka=o("In the example above, the pipeline contains a "),is=l("a"),Oa=o("UNet2DModel"),el=o(" model and a "),os=l("a"),sl=o("DDPMScheduler"),tl=o(". 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 "),Ns=l("em"),al=o("noise residual"),ll=o(" 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."),It=h(),ps=l("p"),nl=o("To recreate the pipeline with the model and scheduler separately, let\u2019s write our own denoising process."),Ct=h(),k=l("ol"),je=l("li"),Ws=l("p"),il=o("Load the model and scheduler:"),ol=h(),u(ve.$$.fragment),pl=h(),Ue=l("li"),Rs=l("p"),rl=o("Set the number of timesteps to run the denoising process for:"),hl=h(),u(_e.$$.fragment),dl=h(),Je=l("li"),xs=l("p"),ml=o("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:"),cl=h(),u(Te.$$.fragment),ul=h(),$e=l("li"),Ss=l("p"),fl=o("Create some random noise with the same shape as the desired output:"),bl=h(),u(Ee.$$.fragment),gl=h(),z=l("li"),N=l("p"),yl=o("Now write a loop to iterate over the timesteps. At each timestep, the model does a "),rs=l("a"),Ml=o("UNet2DModel.forward()"),wl=o(" pass and returns the noisy residual. The scheduler\u2019s "),hs=l("a"),jl=o("step()"),vl=o(" 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=l("code"),Ul=o("timesteps"),_l=o(" array."),Jl=h(),u(ke.$$.fragment),Tl=h(),Bs=l("p"),$l=o("This is the entire denoising process, and you can use this same pattern to write any diffusion system."),El=h(),Ze=l("li"),As=l("p"),kl=o("The last step is to convert the denoised output into an image:"),Zl=h(),u(Ie.$$.fragment),Gt=h(),Z=l("p"),Il=o("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 "),Ds=l("code"),Cl=o("timestep"),Gl=o(" array. The "),Fs=l("code"),Vl=o("timestep"),Nl=o(" 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 "),zs=l("code"),Wl=o("timestep"),Rl=o("\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 "),Ys=l("code"),xl=o("timestep"),Sl=o(" array."),Vt=h(),ds=l("p"),Xl=o("Let\u2019s try it out!"),Nt=h(),Y=l("h2"),se=l("a"),Qs=l("span"),u(Ce.$$.fragment),Bl=h(),Ps=l("span"),Al=o("Deconstruct the Stable Diffusion pipeline"),Wt=h(),te=l("p"),Dl=o("Stable Diffusion is a text-to-image "),Ls=l("em"),Fl=o("latent diffusion"),zl=o(" 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."),Rt=h(),ms=l("p"),Yl=o("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."),xt=h(),u(ae.$$.fragment),St=h(),R=l("p"),Ql=o("Now that you know what you need for the Stable Diffusion pipeline, load all these components with the "),cs=l("a"),Pl=o("from_pretrained()"),Ll=o(" method. You can find them in the pretrained "),Ge=l("a"),Hs=l("code"),Hl=o("runwayml/stable-diffusion-v1-5"),ql=o(" checkpoint, and each component is stored in a separate subfolder:"),Xt=h(),u(Ve.$$.fragment),Bt=h(),x=l("p"),Kl=o("Instead of the default "),us=l("a"),Ol=o("PNDMScheduler"),en=o(", exchange it for the "),fs=l("a"),sn=o("UniPCMultistepScheduler"),tn=o(" to see how easy it is to plug a different scheduler in:"),At=h(),u(Ne.$$.fragment),Dt=h(),bs=l("p"),an=o("To speed up inference, move the models to a GPU since, unlike the scheduler, they have trainable weights:"),Ft=h(),u(We.$$.fragment),zt=h(),Q=l("h3"),le=l("a"),qs=l("span"),u(Re.$$.fragment),ln=h(),Ks=l("span"),nn=o("Create text embeddings"),Yt=h(),gs=l("p"),on=o("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."),Qt=h(),u(ne.$$.fragment),Pt=h(),ys=l("p"),pn=o("Feel free to choose any prompt you like if you want to generate something else!"),Lt=h(),u(xe.$$.fragment),Ht=h(),Ms=l("p"),rn=o("Tokenize the text and generate the embeddings from the prompt:"),qt=h(),u(Se.$$.fragment),Kt=h(),I=l("p"),hn=o("You\u2019ll also need to generate the "),Os=l("em"),dn=o("unconditional text embeddings"),mn=o(" which are the embeddings for the padding token. These need to have the same shape ("),et=l("code"),cn=o("batch_size"),un=o(" and "),st=l("code"),fn=o("seq_length"),bn=o(") as the conditional "),tt=l("code"),gn=o("text_embeddings"),yn=o(":"),Ot=h(),u(Xe.$$.fragment),ea=h(),ws=l("p"),Mn=o("Let\u2019s concatenate the conditional and unconditional embeddings into a batch to avoid doing two forward passes:"),sa=h(),u(Be.$$.fragment),ta=h(),P=l("h3"),ie=l("a"),at=l("span"),u(Ae.$$.fragment),wn=h(),lt=l("span"),jn=o("Create random noise"),aa=h(),oe=l("p"),vn=o("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 "),nt=l("code"),Un=o("latent"),_n=o(" 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."),la=h(),u(pe.$$.fragment),na=h(),u(De.$$.fragment),ia=h(),L=l("h3"),re=l("a"),it=l("span"),u(Fe.$$.fragment),Jn=h(),ot=l("span"),Tn=o("Denoise the image"),oa=h(),S=l("p"),$n=o("Start by scaling the input with the initial noise distribution, "),pt=l("em"),En=o("sigma"),kn=o(", the noise scale value, which is required for improved schedulers like "),js=l("a"),Zn=o("UniPCMultistepScheduler"),In=o(":"),pa=h(),u(ze.$$.fragment),ra=h(),he=l("p"),Cn=o("The last step is to create the denoising loop that\u2019ll progressively transform the pure noise in "),rt=l("code"),Gn=o("latents"),Vn=o(" to an image described by your prompt. Remember, the denoising loop needs to do three things:"),ha=h(),X=l("ol"),ht=l("li"),Nn=o("Set the scheduler\u2019s timesteps to use during denoising."),Wn=h(),dt=l("li"),Rn=o("Iterate over the timesteps."),xn=h(),mt=l("li"),Sn=o("At each timestep, call the UNet model to predict the noise residual and pass it to the scheduler to compute the previous noisy sample."),da=h(),u(Ye.$$.fragment),ma=h(),H=l("h3"),de=l("a"),ct=l("span"),u(Qe.$$.fragment),Xn=h(),ut=l("span"),Bn=o("Decode the image"),ca=h(),B=l("p"),An=o("The final step is to use the "),ft=l("code"),Dn=o("vae"),Fn=o(" to decode the latent representation into an image and get the decoded output with "),bt=l("code"),zn=o("sample"),Yn=o(":"),ua=h(),u(Pe.$$.fragment),fa=h(),me=l("p"),Qn=o("Lastly, convert the image to a "),gt=l("code"),Pn=o("PIL.Image"),Ln=o(" to see your generated image!"),ba=h(),u(Le.$$.fragment),ga=h(),He=l("div"),yt=l("img"),ya=h(),q=l("h2"),ce=l("a"),Mt=l("span"),u(qe.$$.fragment),Hn=h(),wt=l("span"),qn=o("Next steps"),Ma=h(),vs=l("p"),Kn=o("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."),wa=h(),Us=l("p"),On=o("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."),ja=h(),_s=l("p"),ei=o("For your next steps, feel free to:"),va=h(),ue=l("ul"),Ke=l("li"),si=o("Learn how to "),Js=l("a"),ti=o("build and contribute a pipeline"),ai=o(" to \u{1F9E8} Diffusers. We can\u2019t wait and see what you\u2019ll come up with!"),li=h(),Oe=l("li"),ni=o("Explore "),Ts=l("a"),ii=o("existing pipelines"),oi=o(" 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 a=So('[data-svelte="svelte-1phssyn"]',document.head);c=n(a,"META",{name:!0,content:!0}),a.forEach(s),$=d(e),w=n(e,"H1",{class:!0});var es=i(w);U=n(es,"A",{id:!0,class:!0,href:!0});var jt=i(U);T=n(jt,"SPAN",{});var vt=i(T);f(v.$$.fragment,vt),vt.forEach(s),jt.forEach(s),j=d(es),E=n(es,"SPAN",{});var di=i(E);J=p(di,"Understanding pipelines, models and schedulers"),di.forEach(s),es.forEach(s),C=d(e),f(W.$$.fragment,e),Ut=d(e),O=n(e,"P",{});var _a=i(O);Fa=p(_a,"\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 "),ss=n(_a,"A",{href:!0});var mi=i(ss);za=p(mi,"DiffusionPipeline"),mi.forEach(s),Ya=p(_a," bundles these components together for convenience, you can also unbundle the pipeline and use the models and schedulers separately to create new diffusion systems."),_a.forEach(s),_t=d(e),ts=n(e,"P",{});var ci=i(ts);Qa=p(ci,"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."),ci.forEach(s),Jt=d(e),F=n(e,"H2",{class:!0});var Ja=i(F);ee=n(Ja,"A",{id:!0,class:!0,href:!0});var ui=i(ee);Gs=n(ui,"SPAN",{});var fi=i(Gs);f(ye.$$.fragment,fi),fi.forEach(s),ui.forEach(s),Pa=d(Ja),Vs=n(Ja,"SPAN",{});var bi=i(Vs);La=p(bi,"Deconstruct a basic pipeline"),bi.forEach(s),Ja.forEach(s),Tt=d(e),as=n(e,"P",{});var gi=i(as);Ha=p(gi,"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:"),gi.forEach(s),$t=d(e),f(Me.$$.fragment,e),Et=d(e),we=n(e,"DIV",{class:!0});var yi=i(we);ls=n(yi,"IMG",{src:!0,alt:!0}),yi.forEach(s),kt=d(e),ns=n(e,"P",{});var Mi=i(ns);qa=p(Mi,"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."),Mi.forEach(s),Zt=d(e),G=n(e,"P",{});var fe=i(G);Ka=p(fe,"In the example above, the pipeline contains a "),is=n(fe,"A",{href:!0});var wi=i(is);Oa=p(wi,"UNet2DModel"),wi.forEach(s),el=p(fe," model and a "),os=n(fe,"A",{href:!0});var ji=i(os);sl=p(ji,"DDPMScheduler"),ji.forEach(s),tl=p(fe,". 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 "),Ns=n(fe,"EM",{});var vi=i(Ns);al=p(vi,"noise residual"),vi.forEach(s),ll=p(fe," 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."),fe.forEach(s),It=d(e),ps=n(e,"P",{});var Ui=i(ps);nl=p(Ui,"To recreate the pipeline with the model and scheduler separately, let\u2019s write our own denoising process."),Ui.forEach(s),Ct=d(e),k=n(e,"OL",{});var V=i(k);je=n(V,"LI",{});var Ta=i(je);Ws=n(Ta,"P",{});var _i=i(Ws);il=p(_i,"Load the model and scheduler:"),_i.forEach(s),ol=d(Ta),f(ve.$$.fragment,Ta),Ta.forEach(s),pl=d(V),Ue=n(V,"LI",{});var $a=i(Ue);Rs=n($a,"P",{});var Ji=i(Rs);rl=p(Ji,"Set the number of timesteps to run the denoising process for:"),Ji.forEach(s),hl=d($a),f(_e.$$.fragment,$a),$a.forEach(s),dl=d(V),Je=n(V,"LI",{});var Ea=i(Je);xs=n(Ea,"P",{});var Ti=i(xs);ml=p(Ti,"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:"),Ti.forEach(s),cl=d(Ea),f(Te.$$.fragment,Ea),Ea.forEach(s),ul=d(V),$e=n(V,"LI",{});var ka=i($e);Ss=n(ka,"P",{});var $i=i(Ss);fl=p($i,"Create some random noise with the same shape as the desired output:"),$i.forEach(s),bl=d(ka),f(Ee.$$.fragment,ka),ka.forEach(s),gl=d(V),z=n(V,"LI",{});var $s=i(z);N=n($s,"P",{});var be=i(N);yl=p(be,"Now write a loop to iterate over the timesteps. At each timestep, the model does a "),rs=n(be,"A",{href:!0});var Ei=i(rs);Ml=p(Ei,"UNet2DModel.forward()"),Ei.forEach(s),wl=p(be," pass and returns the noisy residual. The scheduler\u2019s "),hs=n(be,"A",{href:!0});var ki=i(hs);jl=p(ki,"step()"),ki.forEach(s),vl=p(be," 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(be,"CODE",{});var Zi=i(Xs);Ul=p(Zi,"timesteps"),Zi.forEach(s),_l=p(be," array."),be.forEach(s),Jl=d($s),f(ke.$$.fragment,$s),Tl=d($s),Bs=n($s,"P",{});var Ii=i(Bs);$l=p(Ii,"This is the entire denoising process, and you can use this same pattern to write any diffusion system."),Ii.forEach(s),$s.forEach(s),El=d(V),Ze=n(V,"LI",{});var Za=i(Ze);As=n(Za,"P",{});var Ci=i(As);kl=p(Ci,"The last step is to convert the denoised output into an image:"),Ci.forEach(s),Zl=d(Za),f(Ie.$$.fragment,Za),Za.forEach(s),V.forEach(s),Gt=d(e),Z=n(e,"P",{});var A=i(Z);Il=p(A,"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 "),Ds=n(A,"CODE",{});var Gi=i(Ds);Cl=p(Gi,"timestep"),Gi.forEach(s),Gl=p(A," array. The "),Fs=n(A,"CODE",{});var Vi=i(Fs);Vl=p(Vi,"timestep"),Vi.forEach(s),Nl=p(A," 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 "),zs=n(A,"CODE",{});var Ni=i(zs);Wl=p(Ni,"timestep"),Ni.forEach(s),Rl=p(A,"\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 "),Ys=n(A,"CODE",{});var Wi=i(Ys);xl=p(Wi,"timestep"),Wi.forEach(s),Sl=p(A," array."),A.forEach(s),Vt=d(e),ds=n(e,"P",{});var Ri=i(ds);Xl=p(Ri,"Let\u2019s try it out!"),Ri.forEach(s),Nt=d(e),Y=n(e,"H2",{class:!0});var Ia=i(Y);se=n(Ia,"A",{id:!0,class:!0,href:!0});var xi=i(se);Qs=n(xi,"SPAN",{});var Si=i(Qs);f(Ce.$$.fragment,Si),Si.forEach(s),xi.forEach(s),Bl=d(Ia),Ps=n(Ia,"SPAN",{});var Xi=i(Ps);Al=p(Xi,"Deconstruct the Stable Diffusion pipeline"),Xi.forEach(s),Ia.forEach(s),Wt=d(e),te=n(e,"P",{});var Ca=i(te);Dl=p(Ca,"Stable Diffusion is a text-to-image "),Ls=n(Ca,"EM",{});var Bi=i(Ls);Fl=p(Bi,"latent diffusion"),Bi.forEach(s),zl=p(Ca," 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."),Ca.forEach(s),Rt=d(e),ms=n(e,"P",{});var Ai=i(ms);Yl=p(Ai,"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."),Ai.forEach(s),xt=d(e),f(ae.$$.fragment,e),St=d(e),R=n(e,"P",{});var Es=i(R);Ql=p(Es,"Now that you know what you need for the Stable Diffusion pipeline, load all these components with the "),cs=n(Es,"A",{href:!0});var Di=i(cs);Pl=p(Di,"from_pretrained()"),Di.forEach(s),Ll=p(Es," method. You can find them in the pretrained "),Ge=n(Es,"A",{href:!0,rel:!0});var Fi=i(Ge);Hs=n(Fi,"CODE",{});var zi=i(Hs);Hl=p(zi,"runwayml/stable-diffusion-v1-5"),zi.forEach(s),Fi.forEach(s),ql=p(Es," checkpoint, and each component is stored in a separate subfolder:"),Es.forEach(s),Xt=d(e),f(Ve.$$.fragment,e),Bt=d(e),x=n(e,"P",{});var ks=i(x);Kl=p(ks,"Instead of the default "),us=n(ks,"A",{href:!0});var Yi=i(us);Ol=p(Yi,"PNDMScheduler"),Yi.forEach(s),en=p(ks,", exchange it for the "),fs=n(ks,"A",{href:!0});var Qi=i(fs);sn=p(Qi,"UniPCMultistepScheduler"),Qi.forEach(s),tn=p(ks," to see how easy it is to plug a different scheduler in:"),ks.forEach(s),At=d(e),f(Ne.$$.fragment,e),Dt=d(e),bs=n(e,"P",{});var Pi=i(bs);an=p(Pi,"To speed up inference, move the models to a GPU since, unlike the scheduler, they have trainable weights:"),Pi.forEach(s),Ft=d(e),f(We.$$.fragment,e),zt=d(e),Q=n(e,"H3",{class:!0});var Ga=i(Q);le=n(Ga,"A",{id:!0,class:!0,href:!0});var Li=i(le);qs=n(Li,"SPAN",{});var Hi=i(qs);f(Re.$$.fragment,Hi),Hi.forEach(s),Li.forEach(s),ln=d(Ga),Ks=n(Ga,"SPAN",{});var qi=i(Ks);nn=p(qi,"Create text embeddings"),qi.forEach(s),Ga.forEach(s),Yt=d(e),gs=n(e,"P",{});var Ki=i(gs);on=p(Ki,"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."),Ki.forEach(s),Qt=d(e),f(ne.$$.fragment,e),Pt=d(e),ys=n(e,"P",{});var Oi=i(ys);pn=p(Oi,"Feel free to choose any prompt you like if you want to generate something else!"),Oi.forEach(s),Lt=d(e),f(xe.$$.fragment,e),Ht=d(e),Ms=n(e,"P",{});var eo=i(Ms);rn=p(eo,"Tokenize the text and generate the embeddings from the prompt:"),eo.forEach(s),qt=d(e),f(Se.$$.fragment,e),Kt=d(e),I=n(e,"P",{});var D=i(I);hn=p(D,"You\u2019ll also need to generate the "),Os=n(D,"EM",{});var so=i(Os);dn=p(so,"unconditional text embeddings"),so.forEach(s),mn=p(D," which are the embeddings for the padding token. These need to have the same shape ("),et=n(D,"CODE",{});var to=i(et);cn=p(to,"batch_size"),to.forEach(s),un=p(D," and "),st=n(D,"CODE",{});var ao=i(st);fn=p(ao,"seq_length"),ao.forEach(s),bn=p(D,") as the conditional "),tt=n(D,"CODE",{});var lo=i(tt);gn=p(lo,"text_embeddings"),lo.forEach(s),yn=p(D,":"),D.forEach(s),Ot=d(e),f(Xe.$$.fragment,e),ea=d(e),ws=n(e,"P",{});var no=i(ws);Mn=p(no,"Let\u2019s concatenate the conditional and unconditional embeddings into a batch to avoid doing two forward passes:"),no.forEach(s),sa=d(e),f(Be.$$.fragment,e),ta=d(e),P=n(e,"H3",{class:!0});var Va=i(P);ie=n(Va,"A",{id:!0,class:!0,href:!0});var io=i(ie);at=n(io,"SPAN",{});var oo=i(at);f(Ae.$$.fragment,oo),oo.forEach(s),io.forEach(s),wn=d(Va),lt=n(Va,"SPAN",{});var po=i(lt);jn=p(po,"Create random noise"),po.forEach(s),Va.forEach(s),aa=d(e),oe=n(e,"P",{});var Na=i(oe);vn=p(Na,"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 "),nt=n(Na,"CODE",{});var ro=i(nt);Un=p(ro,"latent"),ro.forEach(s),_n=p(Na," 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."),Na.forEach(s),la=d(e),f(pe.$$.fragment,e),na=d(e),f(De.$$.fragment,e),ia=d(e),L=n(e,"H3",{class:!0});var Wa=i(L);re=n(Wa,"A",{id:!0,class:!0,href:!0});var ho=i(re);it=n(ho,"SPAN",{});var mo=i(it);f(Fe.$$.fragment,mo),mo.forEach(s),ho.forEach(s),Jn=d(Wa),ot=n(Wa,"SPAN",{});var co=i(ot);Tn=p(co,"Denoise the image"),co.forEach(s),Wa.forEach(s),oa=d(e),S=n(e,"P",{});var Zs=i(S);$n=p(Zs,"Start by scaling the input with the initial noise distribution, "),pt=n(Zs,"EM",{});var uo=i(pt);En=p(uo,"sigma"),uo.forEach(s),kn=p(Zs,", the noise scale value, which is required for improved schedulers like "),js=n(Zs,"A",{href:!0});var fo=i(js);Zn=p(fo,"UniPCMultistepScheduler"),fo.forEach(s),In=p(Zs,":"),Zs.forEach(s),pa=d(e),f(ze.$$.fragment,e),ra=d(e),he=n(e,"P",{});var Ra=i(he);Cn=p(Ra,"The last step is to create the denoising loop that\u2019ll progressively transform the pure noise in "),rt=n(Ra,"CODE",{});var bo=i(rt);Gn=p(bo,"latents"),bo.forEach(s),Vn=p(Ra," to an image described by your prompt. Remember, the denoising loop needs to do three things:"),Ra.forEach(s),ha=d(e),X=n(e,"OL",{});var Is=i(X);ht=n(Is,"LI",{});var go=i(ht);Nn=p(go,"Set the scheduler\u2019s timesteps to use during denoising."),go.forEach(s),Wn=d(Is),dt=n(Is,"LI",{});var yo=i(dt);Rn=p(yo,"Iterate over the timesteps."),yo.forEach(s),xn=d(Is),mt=n(Is,"LI",{});var Mo=i(mt);Sn=p(Mo,"At each timestep, call the UNet model to predict the noise residual and pass it to the scheduler to compute the previous noisy sample."),Mo.forEach(s),Is.forEach(s),da=d(e),f(Ye.$$.fragment,e),ma=d(e),H=n(e,"H3",{class:!0});var xa=i(H);de=n(xa,"A",{id:!0,class:!0,href:!0});var wo=i(de);ct=n(wo,"SPAN",{});var jo=i(ct);f(Qe.$$.fragment,jo),jo.forEach(s),wo.forEach(s),Xn=d(xa),ut=n(xa,"SPAN",{});var vo=i(ut);Bn=p(vo,"Decode the image"),vo.forEach(s),xa.forEach(s),ca=d(e),B=n(e,"P",{});var Cs=i(B);An=p(Cs,"The final step is to use the "),ft=n(Cs,"CODE",{});var Uo=i(ft);Dn=p(Uo,"vae"),Uo.forEach(s),Fn=p(Cs," to decode the latent representation into an image and get the decoded output with "),bt=n(Cs,"CODE",{});var _o=i(bt);zn=p(_o,"sample"),_o.forEach(s),Yn=p(Cs,":"),Cs.forEach(s),ua=d(e),f(Pe.$$.fragment,e),fa=d(e),me=n(e,"P",{});var Sa=i(me);Qn=p(Sa,"Lastly, convert the image to a "),gt=n(Sa,"CODE",{});var Jo=i(gt);Pn=p(Jo,"PIL.Image"),Jo.forEach(s),Ln=p(Sa," to see your generated image!"),Sa.forEach(s),ba=d(e),f(Le.$$.fragment,e),ga=d(e),He=n(e,"DIV",{class:!0});var To=i(He);yt=n(To,"IMG",{src:!0}),To.forEach(s),ya=d(e),q=n(e,"H2",{class:!0});var Xa=i(q);ce=n(Xa,"A",{id:!0,class:!0,href:!0});var $o=i(ce);Mt=n($o,"SPAN",{});var Eo=i(Mt);f(qe.$$.fragment,Eo),Eo.forEach(s),$o.forEach(s),Hn=d(Xa),wt=n(Xa,"SPAN",{});var ko=i(wt);qn=p(ko,"Next steps"),ko.forEach(s),Xa.forEach(s),Ma=d(e),vs=n(e,"P",{});var Zo=i(vs);Kn=p(Zo,"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."),Zo.forEach(s),wa=d(e),Us=n(e,"P",{});var Io=i(Us);On=p(Io,"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."),Io.forEach(s),ja=d(e),_s=n(e,"P",{});var Co=i(_s);ei=p(Co,"For your next steps, feel free to:"),Co.forEach(s),va=d(e),ue=n(e,"UL",{});var Ba=i(ue);Ke=n(Ba,"LI",{});var Aa=i(Ke);si=p(Aa,"Learn how to "),Js=n(Aa,"A",{href:!0});var Go=i(Js);ti=p(Go,"build and contribute a pipeline"),Go.forEach(s),ai=p(Aa," to \u{1F9E8} Diffusers. We can\u2019t wait and see what you\u2019ll come up with!"),Aa.forEach(s),li=d(Ba),Oe=n(Ba,"LI",{});var Da=i(Oe);ni=p(Da,"Explore "),Ts=n(Da,"A",{href:!0});var Vo=i(Ts);ii=p(Vo,"existing pipelines"),Vo.forEach(s),oi=p(Da," in the library, and see if you can deconstruct and build a pipeline from scratch using the models and schedulers separately."),Da.forEach(s),Ba.forEach(s),this.h()},h(){m(c,"name","hf:doc:metadata"),m(c,"content",JSON.stringify(Qo)),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(ss,"href","/docs/diffusers/v0.17.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),m(ee,"id","deconstruct-a-basic-pipeline"),m(ee,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),m(ee,"href","#deconstruct-a-basic-pipeline"),m(F,"class","relative group"),No(ls.src,ri="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ddpm-cat.png")||m(ls,"src",ri),m(ls,"alt","Image of cat created from DDPMPipeline"),m(we,"class","flex justify-center"),m(is,"href","/docs/diffusers/v0.17.0/en/api/models#diffusers.UNet2DModel"),m(os,"href","/docs/diffusers/v0.17.0/en/api/schedulers/ddpm#diffusers.DDPMScheduler"),m(rs,"href","/docs/diffusers/v0.17.0/en/api/models#diffusers.UNet2DModel.forward"),m(hs,"href","/docs/diffusers/v0.17.0/en/api/schedulers/ddpm#diffusers.DDPMScheduler.step"),m(se,"id","deconstruct-the-stable-diffusion-pipeline"),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","#deconstruct-the-stable-diffusion-pipeline"),m(Y,"class","relative group"),m(cs,"href","/docs/diffusers/v0.17.0/en/api/models#diffusers.ModelMixin.from_pretrained"),m(Ge,"href","https://huggingface.co/runwayml/stable-diffusion-v1-5"),m(Ge,"rel","nofollow"),m(us,"href","/docs/diffusers/v0.17.0/en/api/schedulers/pndm#diffusers.PNDMScheduler"),m(fs,"href","/docs/diffusers/v0.17.0/en/api/schedulers/unipc#diffusers.UniPCMultistepScheduler"),m(le,"id","create-text-embeddings"),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-text-embeddings"),m(Q,"class","relative group"),m(ie,"id","create-random-noise"),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","#create-random-noise"),m(P,"class","relative group"),m(re,"id","denoise-the-image"),m(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"),m(re,"href","#denoise-the-image"),m(L,"class","relative group"),m(js,"href","/docs/diffusers/v0.17.0/en/api/schedulers/unipc#diffusers.UniPCMultistepScheduler"),m(de,"id","decode-the-image"),m(de,"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(de,"href","#decode-the-image"),m(H,"class","relative group"),No(yt.src,hi="https://huggingface.co/blog/assets/98_stable_diffusion/stable_diffusion_k_lms.png")||m(yt,"src",hi),m(He,"class","flex justify-center"),m(ce,"id","next-steps"),m(ce,"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(ce,"href","#next-steps"),m(q,"class","relative group"),m(Js,"href","using-diffusers/#contribute_pipeline"),m(Ts,"href","./api/pipelines/overview")},m(e,a){t(document.head,c),r(e,$,a),r(e,w,a),t(w,U),t(U,T),b(v,T,null),t(w,j),t(w,E),t(E,J),r(e,C,a),b(W,e,a),r(e,Ut,a),r(e,O,a),t(O,Fa),t(O,ss),t(ss,za),t(O,Ya),r(e,_t,a),r(e,ts,a),t(ts,Qa),r(e,Jt,a),r(e,F,a),t(F,ee),t(ee,Gs),b(ye,Gs,null),t(F,Pa),t(F,Vs),t(Vs,La),r(e,Tt,a),r(e,as,a),t(as,Ha),r(e,$t,a),b(Me,e,a),r(e,Et,a),r(e,we,a),t(we,ls),r(e,kt,a),r(e,ns,a),t(ns,qa),r(e,Zt,a),r(e,G,a),t(G,Ka),t(G,is),t(is,Oa),t(G,el),t(G,os),t(os,sl),t(G,tl),t(G,Ns),t(Ns,al),t(G,ll),r(e,It,a),r(e,ps,a),t(ps,nl),r(e,Ct,a),r(e,k,a),t(k,je),t(je,Ws),t(Ws,il),t(je,ol),b(ve,je,null),t(k,pl),t(k,Ue),t(Ue,Rs),t(Rs,rl),t(Ue,hl),b(_e,Ue,null),t(k,dl),t(k,Je),t(Je,xs),t(xs,ml),t(Je,cl),b(Te,Je,null),t(k,ul),t(k,$e),t($e,Ss),t(Ss,fl),t($e,bl),b(Ee,$e,null),t(k,gl),t(k,z),t(z,N),t(N,yl),t(N,rs),t(rs,Ml),t(N,wl),t(N,hs),t(hs,jl),t(N,vl),t(N,Xs),t(Xs,Ul),t(N,_l),t(z,Jl),b(ke,z,null),t(z,Tl),t(z,Bs),t(Bs,$l),t(k,El),t(k,Ze),t(Ze,As),t(As,kl),t(Ze,Zl),b(Ie,Ze,null),r(e,Gt,a),r(e,Z,a),t(Z,Il),t(Z,Ds),t(Ds,Cl),t(Z,Gl),t(Z,Fs),t(Fs,Vl),t(Z,Nl),t(Z,zs),t(zs,Wl),t(Z,Rl),t(Z,Ys),t(Ys,xl),t(Z,Sl),r(e,Vt,a),r(e,ds,a),t(ds,Xl),r(e,Nt,a),r(e,Y,a),t(Y,se),t(se,Qs),b(Ce,Qs,null),t(Y,Bl),t(Y,Ps),t(Ps,Al),r(e,Wt,a),r(e,te,a),t(te,Dl),t(te,Ls),t(Ls,Fl),t(te,zl),r(e,Rt,a),r(e,ms,a),t(ms,Yl),r(e,xt,a),b(ae,e,a),r(e,St,a),r(e,R,a),t(R,Ql),t(R,cs),t(cs,Pl),t(R,Ll),t(R,Ge),t(Ge,Hs),t(Hs,Hl),t(R,ql),r(e,Xt,a),b(Ve,e,a),r(e,Bt,a),r(e,x,a),t(x,Kl),t(x,us),t(us,Ol),t(x,en),t(x,fs),t(fs,sn),t(x,tn),r(e,At,a),b(Ne,e,a),r(e,Dt,a),r(e,bs,a),t(bs,an),r(e,Ft,a),b(We,e,a),r(e,zt,a),r(e,Q,a),t(Q,le),t(le,qs),b(Re,qs,null),t(Q,ln),t(Q,Ks),t(Ks,nn),r(e,Yt,a),r(e,gs,a),t(gs,on),r(e,Qt,a),b(ne,e,a),r(e,Pt,a),r(e,ys,a),t(ys,pn),r(e,Lt,a),b(xe,e,a),r(e,Ht,a),r(e,Ms,a),t(Ms,rn),r(e,qt,a),b(Se,e,a),r(e,Kt,a),r(e,I,a),t(I,hn),t(I,Os),t(Os,dn),t(I,mn),t(I,et),t(et,cn),t(I,un),t(I,st),t(st,fn),t(I,bn),t(I,tt),t(tt,gn),t(I,yn),r(e,Ot,a),b(Xe,e,a),r(e,ea,a),r(e,ws,a),t(ws,Mn),r(e,sa,a),b(Be,e,a),r(e,ta,a),r(e,P,a),t(P,ie),t(ie,at),b(Ae,at,null),t(P,wn),t(P,lt),t(lt,jn),r(e,aa,a),r(e,oe,a),t(oe,vn),t(oe,nt),t(nt,Un),t(oe,_n),r(e,la,a),b(pe,e,a),r(e,na,a),b(De,e,a),r(e,ia,a),r(e,L,a),t(L,re),t(re,it),b(Fe,it,null),t(L,Jn),t(L,ot),t(ot,Tn),r(e,oa,a),r(e,S,a),t(S,$n),t(S,pt),t(pt,En),t(S,kn),t(S,js),t(js,Zn),t(S,In),r(e,pa,a),b(ze,e,a),r(e,ra,a),r(e,he,a),t(he,Cn),t(he,rt),t(rt,Gn),t(he,Vn),r(e,ha,a),r(e,X,a),t(X,ht),t(ht,Nn),t(X,Wn),t(X,dt),t(dt,Rn),t(X,xn),t(X,mt),t(mt,Sn),r(e,da,a),b(Ye,e,a),r(e,ma,a),r(e,H,a),t(H,de),t(de,ct),b(Qe,ct,null),t(H,Xn),t(H,ut),t(ut,Bn),r(e,ca,a),r(e,B,a),t(B,An),t(B,ft),t(ft,Dn),t(B,Fn),t(B,bt),t(bt,zn),t(B,Yn),r(e,ua,a),b(Pe,e,a),r(e,fa,a),r(e,me,a),t(me,Qn),t(me,gt),t(gt,Pn),t(me,Ln),r(e,ba,a),b(Le,e,a),r(e,ga,a),r(e,He,a),t(He,yt),r(e,ya,a),r(e,q,a),t(q,ce),t(ce,Mt),b(qe,Mt,null),t(q,Hn),t(q,wt),t(wt,qn),r(e,Ma,a),r(e,vs,a),t(vs,Kn),r(e,wa,a),r(e,Us,a),t(Us,On),r(e,ja,a),r(e,_s,a),t(_s,ei),r(e,va,a),r(e,ue,a),t(ue,Ke),t(Ke,si),t(Ke,Js),t(Js,ti),t(Ke,ai),t(ue,li),t(ue,Oe),t(Oe,ni),t(Oe,Ts),t(Ts,ii),t(Oe,oi),Ua=!0},p(e,[a]){const es={};a&2&&(es.$$scope={dirty:a,ctx:e}),ae.$set(es);const jt={};a&2&&(jt.$$scope={dirty:a,ctx:e}),ne.$set(jt);const vt={};a&2&&(vt.$$scope={dirty:a,ctx:e}),pe.$set(vt)},i(e){Ua||(g(v.$$.fragment,e),g(W.$$.fragment,e),g(ye.$$.fragment,e),g(Me.$$.fragment,e),g(ve.$$.fragment,e),g(_e.$$.fragment,e),g(Te.$$.fragment,e),g(Ee.$$.fragment,e),g(ke.$$.fragment,e),g(Ie.$$.fragment,e),g(Ce.$$.fragment,e),g(ae.$$.fragment,e),g(Ve.$$.fragment,e),g(Ne.$$.fragment,e),g(We.$$.fragment,e),g(Re.$$.fragment,e),g(ne.$$.fragment,e),g(xe.$$.fragment,e),g(Se.$$.fragment,e),g(Xe.$$.fragment,e),g(Be.$$.fragment,e),g(Ae.$$.fragment,e),g(pe.$$.fragment,e),g(De.$$.fragment,e),g(Fe.$$.fragment,e),g(ze.$$.fragment,e),g(Ye.$$.fragment,e),g(Qe.$$.fragment,e),g(Pe.$$.fragment,e),g(Le.$$.fragment,e),g(qe.$$.fragment,e),Ua=!0)},o(e){y(v.$$.fragment,e),y(W.$$.fragment,e),y(ye.$$.fragment,e),y(Me.$$.fragment,e),y(ve.$$.fragment,e),y(_e.$$.fragment,e),y(Te.$$.fragment,e),y(Ee.$$.fragment,e),y(ke.$$.fragment,e),y(Ie.$$.fragment,e),y(Ce.$$.fragment,e),y(ae.$$.fragment,e),y(Ve.$$.fragment,e),y(Ne.$$.fragment,e),y(We.$$.fragment,e),y(Re.$$.fragment,e),y(ne.$$.fragment,e),y(xe.$$.fragment,e),y(Se.$$.fragment,e),y(Xe.$$.fragment,e),y(Be.$$.fragment,e),y(Ae.$$.fragment,e),y(pe.$$.fragment,e),y(De.$$.fragment,e),y(Fe.$$.fragment,e),y(ze.$$.fragment,e),y(Ye.$$.fragment,e),y(Qe.$$.fragment,e),y(Pe.$$.fragment,e),y(Le.$$.fragment,e),y(qe.$$.fragment,e),Ua=!1},d(e){s(c),e&&s($),e&&s(w),M(v),e&&s(C),M(W,e),e&&s(Ut),e&&s(O),e&&s(_t),e&&s(ts),e&&s(Jt),e&&s(F),M(ye),e&&s(Tt),e&&s(as),e&&s($t),M(Me,e),e&&s(Et),e&&s(we),e&&s(kt),e&&s(ns),e&&s(Zt),e&&s(G),e&&s(It),e&&s(ps),e&&s(Ct),e&&s(k),M(ve),M(_e),M(Te),M(Ee),M(ke),M(Ie),e&&s(Gt),e&&s(Z),e&&s(Vt),e&&s(ds),e&&s(Nt),e&&s(Y),M(Ce),e&&s(Wt),e&&s(te),e&&s(Rt),e&&s(ms),e&&s(xt),M(ae,e),e&&s(St),e&&s(R),e&&s(Xt),M(Ve,e),e&&s(Bt),e&&s(x),e&&s(At),M(Ne,e),e&&s(Dt),e&&s(bs),e&&s(Ft),M(We,e),e&&s(zt),e&&s(Q),M(Re),e&&s(Yt),e&&s(gs),e&&s(Qt),M(ne,e),e&&s(Pt),e&&s(ys),e&&s(Lt),M(xe,e),e&&s(Ht),e&&s(Ms),e&&s(qt),M(Se,e),e&&s(Kt),e&&s(I),e&&s(Ot),M(Xe,e),e&&s(ea),e&&s(ws),e&&s(sa),M(Be,e),e&&s(ta),e&&s(P),M(Ae),e&&s(aa),e&&s(oe),e&&s(la),M(pe,e),e&&s(na),M(De,e),e&&s(ia),e&&s(L),M(Fe),e&&s(oa),e&&s(S),e&&s(pa),M(ze,e),e&&s(ra),e&&s(he),e&&s(ha),e&&s(X),e&&s(da),M(Ye,e),e&&s(ma),e&&s(H),M(Qe),e&&s(ca),e&&s(B),e&&s(ua),M(Pe,e),e&&s(fa),e&&s(me),e&&s(ba),M(Le,e),e&&s(ga),e&&s(He),e&&s(ya),e&&s(q),M(qe),e&&s(Ma),e&&s(vs),e&&s(wa),e&&s(Us),e&&s(ja),e&&s(_s),e&&s(va),e&&s(ue)}}}const Qo={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 Po(K){return Xo(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ep extends Wo{constructor(c){super();Ro(this,c,Po,Yo,xo,{})}}export{ep as default,Qo as metadata}; | |
Xet Storage Details
- Size:
- 61.4 kB
- Xet hash:
- a4e78fc7686935bb1b1c2e654f3f4aa256e04d542b1f5e65646345d27dad1cb9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.