import{S as wn,i as $n,s as En,e as r,k as f,w as c,t as a,M as kn,c as o,d as t,m as u,a as n,x as m,h as l,b as h,N as lt,G as s,g as p,y as d,q as g,o as _,B as y,v as Pn}from"../chunks/vendor-hf-doc-builder.js";import{T as bn}from"../chunks/Tip-hf-doc-builder.js";import{I as _e}from"../chunks/IconCopyLink-hf-doc-builder.js";import{C as $}from"../chunks/CodeBlock-hf-doc-builder.js";import{D as Dn}from"../chunks/DocNotebookDropdown-hf-doc-builder.js";function Sn(nt){let v,D,b,k,P;return{c(){v=r("p"),D=a("\u{1F4A1} If you don\u2019t have access to a GPU, you can use one for free from a GPU provider like "),b=r("a"),k=a("Colab"),P=a("!"),this.h()},l(w){v=o(w,"P",{});var E=n(v);D=l(E,"\u{1F4A1} If you don\u2019t have access to a GPU, you can use one for free from a GPU provider like "),b=o(E,"A",{href:!0,rel:!0});var M=n(b);k=l(M,"Colab"),M.forEach(t),P=l(E,"!"),E.forEach(t),this.h()},h(){h(b,"href","https://colab.research.google.com/"),h(b,"rel","nofollow")},m(w,E){p(w,v,E),s(v,D),s(v,b),s(b,k),s(v,P)},d(w){w&&t(v)}}}function jn(nt){let v,D,b,k,P;return{c(){v=r("p"),D=a("\u{1F4A1} We strongly suggest always running your pipelines in "),b=r("code"),k=a("float16"),P=a(", and so far, we\u2019ve rarely seen any degradation in output quality.")},l(w){v=o(w,"P",{});var E=n(v);D=l(E,"\u{1F4A1} We strongly suggest always running your pipelines in "),b=o(E,"CODE",{});var M=n(b);k=l(M,"float16"),M.forEach(t),P=l(E,", and so far, we\u2019ve rarely seen any degradation in output quality."),E.forEach(t)},m(w,E){p(w,v,E),s(v,D),s(v,b),s(b,k),s(v,P)},d(w){w&&t(v)}}}function Mn(nt){let v,D,b,k,P,w,E,M,dr,Gs,ye,xs,q,gr,pt,_r,yr,ft,vr,br,Is,A,wr,Bt,$r,Er,Ht,kr,Pr,Ts,R,Dr,ut,Sr,jr,zs,V,Mr,ve,Yt,qr,Ar,Ns,be,Os,ht,Gr,Cs,we,Ls,C,F,Kt,$e,xr,Wt,Ir,Us,Q,Bs,ct,Tr,Hs,Ee,Ys,G,zr,ke,Rt,Nr,Or,mt,Cr,Lr,Ks,Pe,Ws,dt,Ur,Rs,De,Vs,Se,Vt,Oa,Fs,S,Br,gt,Hr,Yr,Ft,Kr,Wr,Qt,Rr,Vr,Qs,J,Fr,Jt,Qr,Jr,Js,je,Xs,Me,Xt,Ca,Zs,_t,Xr,ei,X,ti,x,Zr,yt,eo,to,Zt,so,io,si,qe,ii,j,ro,vt,oo,ao,bt,lo,no,wt,po,fo,ri,Ae,oi,Z,uo,es,ho,co,ai,Ge,li,xe,ts,La,ni,$t,mo,pi,L,ee,ss,Ie,go,is,_o,fi,te,yo,rs,vo,bo,ui,I,wo,os,$o,Eo,as,ko,Po,hi,Te,ci,Et,Do,mi,ze,di,se,So,ls,jo,Mo,gi,Ne,_i,T,qo,ns,Ao,Go,kt,xo,Io,yi,Oe,vi,ie,To,ps,zo,No,bi,Ce,wi,Le,fs,Ua,$i,Pt,Oo,Ei,U,re,us,Ue,Co,hs,Lo,ki,oe,Uo,cs,Bo,Ho,Pi,B,ae,ms,Be,Yo,ds,Ko,Di,le,Wo,He,Ro,Vo,Si,z,Fo,Ye,Qo,Jo,Ke,Xo,Zo,ji,H,ne,gs,We,ea,_s,ta,Mi,pe,sa,Re,ia,ra,qi,Ve,Ai,Fe,ys,Ba,Gi,Y,fe,vs,Qe,oa,bs,aa,xi,ue,la,ws,na,pa,Ii,he,$s,fa,ua,Es,ha,Ti,Dt,ca,zi,Je,Ni,St,ma,Oi,Xe,Ci,Ze,ks,Ha,Li,N,da,Ps,ga,_a,Ds,ya,va,Ui,et,Bi,tt,Ss,Ya,Hi,K,ce,js,st,ba,Ms,wa,Yi,me,$a,jt,Ea,ka,Ki,O,it,Pa,Mt,Da,Sa,ja,W,Ma,qt,qa,Aa,rt,qs,Ga,xa,Ia,ot,Ta,At,za,Na,Wi;return w=new _e({}),ye=new Dn({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/stable_diffusion.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/stable_diffusion.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/stable_diffusion.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/stable_diffusion.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/stable_diffusion.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/stable_diffusion.ipynb"}]}}),be=new $({props:{code:`from diffusers import DiffusionPipeline

model_id = "runwayml/stable-diffusion-v1-5"
pipeline = DiffusionPipeline.from_pretrained(model_id)`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

model_id = <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
pipeline = DiffusionPipeline.from_pretrained(model_id)`}}),we=new $({props:{code:'prompt = "portrait photo of a old warrior chief"',highlighted:'prompt = <span class="hljs-string">&quot;portrait photo of a old warrior chief&quot;</span>'}}),$e=new _e({}),Q=new bn({props:{$$slots:{default:[Sn]},$$scope:{ctx:nt}}}),Ee=new $({props:{code:'pipeline = pipeline.to("cuda")',highlighted:'pipeline = pipeline.to(<span class="hljs-string">&quot;cuda&quot;</span>)'}}),Pe=new $({props:{code:'generator = torch.Generator("cuda").manual_seed(0)',highlighted:'generator = torch.Generator(<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>)'}}),De=new $({props:{code:`image = pipeline(prompt, generator=generator).images[0]
image`,highlighted:`image = pipeline(prompt, generator=generator).images[<span class="hljs-number">0</span>]
image`}}),je=new $({props:{code:`import torch

pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")
generator = torch.Generator("cuda").manual_seed(0)
image = pipeline(prompt, generator=generator).images[0]
image`,highlighted:`<span class="hljs-keyword">import</span> torch

pipeline = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to(<span class="hljs-string">&quot;cuda&quot;</span>)
generator = torch.Generator(<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
image = pipeline(prompt, generator=generator).images[<span class="hljs-number">0</span>]
image`}}),X=new bn({props:{$$slots:{default:[jn]},$$scope:{ctx:nt}}}),qe=new $({props:{code:`pipeline.scheduler.compatibles
[
    diffusers.schedulers.scheduling_lms_discrete.LMSDiscreteScheduler,
    diffusers.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler,
    diffusers.schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteScheduler,
    diffusers.schedulers.scheduling_deis_multistep.DEISMultistepScheduler,
    diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler,
    diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler,
    diffusers.schedulers.scheduling_ddpm.DDPMScheduler,
    diffusers.schedulers.scheduling_dpmsolver_singlestep.DPMSolverSinglestepScheduler,
    diffusers.schedulers.scheduling_k_dpm_2_ancestral_discrete.KDPM2AncestralDiscreteScheduler,
    diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler,
    diffusers.schedulers.scheduling_pndm.PNDMScheduler,
    diffusers.schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteScheduler,
    diffusers.schedulers.scheduling_ddim.DDIMScheduler,
]`,highlighted:`pipeline.scheduler.compatibles
[
    diffusers.schedulers.scheduling_lms_discrete.LMSDiscreteScheduler,
    diffusers.schedulers.scheduling_unipc_multistep.UniPCMultistepScheduler,
    diffusers.schedulers.scheduling_k_dpm_2_discrete.KDPM2DiscreteScheduler,
    diffusers.schedulers.scheduling_deis_multistep.DEISMultistepScheduler,
    diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler,
    diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler,
    diffusers.schedulers.scheduling_ddpm.DDPMScheduler,
    diffusers.schedulers.scheduling_dpmsolver_singlestep.DPMSolverSinglestepScheduler,
    diffusers.schedulers.scheduling_k_dpm_2_ancestral_discrete.KDPM2AncestralDiscreteScheduler,
    diffusers.schedulers.scheduling_heun_discrete.HeunDiscreteScheduler,
    diffusers.schedulers.scheduling_pndm.PNDMScheduler,
    diffusers.schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteScheduler,
    diffusers.schedulers.scheduling_ddim.DDIMScheduler,
]`}}),Ae=new $({props:{code:`from diffusers import DPMSolverMultistepScheduler

pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DPMSolverMultistepScheduler

pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)`}}),Ge=new $({props:{code:`generator = torch.Generator("cuda").manual_seed(0)
image = pipeline(prompt, generator=generator, num_inference_steps=20).images[0]
image`,highlighted:`generator = torch.Generator(<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">0</span>)
image = pipeline(prompt, generator=generator, num_inference_steps=<span class="hljs-number">20</span>).images[<span class="hljs-number">0</span>]
image`}}),Ie=new _e({}),Te=new $({props:{code:`def get_inputs(batch_size=1):
    generator = [torch.Generator("cuda").manual_seed(i) for i in range(batch_size)]
    prompts = batch_size * [prompt]
    num_inference_steps = 20

    return {"prompt": prompts, "generator": generator, "num_inference_steps": num_inference_steps}`,highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_inputs</span>(<span class="hljs-params">batch_size=<span class="hljs-number">1</span></span>):
    generator = [torch.Generator(<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(batch_size)]
    prompts = batch_size * [prompt]
    num_inference_steps = <span class="hljs-number">20</span>

    <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;prompt&quot;</span>: prompts, <span class="hljs-string">&quot;generator&quot;</span>: generator, <span class="hljs-string">&quot;num_inference_steps&quot;</span>: num_inference_steps}`}}),ze=new $({props:{code:`from PIL import image


def image_grid(imgs, rows=2, cols=2):
    w, h = imgs[0].size
    grid = Image.new("RGB", size=(cols * w, rows * h))

    for i, img in enumerate(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    return grid`,highlighted:`<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> image


<span class="hljs-keyword">def</span> <span class="hljs-title function_">image_grid</span>(<span class="hljs-params">imgs, rows=<span class="hljs-number">2</span>, cols=<span class="hljs-number">2</span></span>):
    w, h = imgs[<span class="hljs-number">0</span>].size
    grid = Image.new(<span class="hljs-string">&quot;RGB&quot;</span>, size=(cols * w, rows * h))

    <span class="hljs-keyword">for</span> i, img <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(imgs):
        grid.paste(img, box=(i % cols * w, i // cols * h))
    <span class="hljs-keyword">return</span> grid`}}),Ne=new $({props:{code:`images = pipeline(**get_inputs(batch_size=4)).images
image_grid(images)`,highlighted:`images = pipeline(**get_inputs(batch_size=<span class="hljs-number">4</span>)).images
image_grid(images)`}}),Oe=new $({props:{code:"pipeline.enable_attention_slicing()",highlighted:"pipeline.enable_attention_slicing()"}}),Ce=new $({props:{code:`images = pipeline(**get_inputs(batch_size=8)).images
image_grid(images, rows=2, cols=4)`,highlighted:`images = pipeline(**get_inputs(batch_size=<span class="hljs-number">8</span>)).images
image_grid(images, rows=<span class="hljs-number">2</span>, cols=<span class="hljs-number">4</span>)`}}),Ue=new _e({}),Be=new _e({}),We=new _e({}),Ve=new $({props:{code:`from diffusers import AutoencoderKL

vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse", torch_dtype=torch.float16).to("cuda")
pipeline.vae = vae
images = pipeline(**get_inputs(batch_size=8)).images
image_grid(images, rows=2, cols=4)`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoencoderKL

vae = AutoencoderKL.from_pretrained(<span class="hljs-string">&quot;stabilityai/sd-vae-ft-mse&quot;</span>, torch_dtype=torch.float16).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.vae = vae
images = pipeline(**get_inputs(batch_size=<span class="hljs-number">8</span>)).images
image_grid(images, rows=<span class="hljs-number">2</span>, cols=<span class="hljs-number">4</span>)`}}),Qe=new _e({}),Je=new $({props:{code:`prompt += ", tribal panther make up, blue on red, side profile, looking away, serious eyes"
prompt += " 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta"`,highlighted:`prompt += <span class="hljs-string">&quot;, tribal panther make up, blue on red, side profile, looking away, serious eyes&quot;</span>
prompt += <span class="hljs-string">&quot; 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta&quot;</span>`}}),Xe=new $({props:{code:`images = pipeline(**get_inputs(batch_size=8)).images
image_grid(images, rows=2, cols=4)`,highlighted:`images = pipeline(**get_inputs(batch_size=<span class="hljs-number">8</span>)).images
image_grid(images, rows=<span class="hljs-number">2</span>, cols=<span class="hljs-number">4</span>)`}}),et=new $({props:{code:`prommpts = [
    "portrait photo of the oldest warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
    "portrait photo of a old warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
    "portrait photo of a warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
    "portrait photo of a young warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta",
]

generator = [torch.Generator("cuda").manual_seed(1) for _ in range(len(prompts))]
images = pipeline(prompt=prompts, generator=generator, num_inference_steps=25).images
image_grid(images)`,highlighted:`prommpts = [
    <span class="hljs-string">&quot;portrait photo of the oldest warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta&quot;</span>,
    <span class="hljs-string">&quot;portrait photo of a old warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta&quot;</span>,
    <span class="hljs-string">&quot;portrait photo of a warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta&quot;</span>,
    <span class="hljs-string">&quot;portrait photo of a young warrior chief, tribal panther make up, blue on red, side profile, looking away, serious eyes 50mm portrait photography, hard rim lighting photography--beta --ar 2:3  --beta --upbeta&quot;</span>,
]

generator = [torch.Generator(<span class="hljs-string">&quot;cuda&quot;</span>).manual_seed(<span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(prompts))]
images = pipeline(prompt=prompts, generator=generator, num_inference_steps=<span class="hljs-number">25</span>).images
image_grid(images)`}}),st=new _e({}),{c(){v=r("meta"),D=f(),b=r("h1"),k=r("a"),P=r("span"),c(w.$$.fragment),E=f(),M=r("span"),dr=a("Effective and efficient diffusion"),Gs=f(),c(ye.$$.fragment),xs=f(),q=r("p"),gr=a("Getting the "),pt=r("a"),_r=a("DiffusionPipeline"),yr=a(" to generate images in a certain style or include what you want can be tricky. Often times, you have to run the "),ft=r("a"),vr=a("DiffusionPipeline"),br=a(" several times before you end up with an image you\u2019re happy with. But generating something out of nothing is a computationally intensive process, especially if you\u2019re running inference over and over again."),Is=f(),A=r("p"),wr=a("This is why it\u2019s important to get the most "),Bt=r("em"),$r=a("computational"),Er=a(" (speed) and "),Ht=r("em"),kr=a("memory"),Pr=a(" (GPU RAM) efficiency from the pipeline to reduce the time between inference cycles so you can iterate faster."),Ts=f(),R=r("p"),Dr=a("This tutorial walks you through how to generate faster and better with the "),ut=r("a"),Sr=a("DiffusionPipeline"),jr=a("."),zs=f(),V=r("p"),Mr=a("Begin by loading the "),ve=r("a"),Yt=r("code"),qr=a("runwayml/stable-diffusion-v1-5"),Ar=a(" model:"),Ns=f(),c(be.$$.fragment),Os=f(),ht=r("p"),Gr=a("The example prompt you\u2019ll use is a portrait of an old warrior chief, but feel free to use your own prompt:"),Cs=f(),c(we.$$.fragment),Ls=f(),C=r("h2"),F=r("a"),Kt=r("span"),c($e.$$.fragment),xr=f(),Wt=r("span"),Ir=a("Speed"),Us=f(),c(Q.$$.fragment),Bs=f(),ct=r("p"),Tr=a("One of the simplest ways to speed up inference is to place the pipeline on a GPU the same way you would with any PyTorch module:"),Hs=f(),c(Ee.$$.fragment),Ys=f(),G=r("p"),zr=a("To make sure you can use the same image and improve on it, use a "),ke=r("a"),Rt=r("code"),Nr=a("Generator"),Or=a(" and set a seed for "),mt=r("a"),Cr=a("reproducibility"),Lr=a(":"),Ks=f(),c(Pe.$$.fragment),Ws=f(),dt=r("p"),Ur=a("Now you can generate an image:"),Rs=f(),c(De.$$.fragment),Vs=f(),Se=r("div"),Vt=r("img"),Fs=f(),S=r("p"),Br=a("This process took ~30 seconds on a T4 GPU (it might be faster if your allocated GPU is better than a T4). By default, the "),gt=r("a"),Hr=a("DiffusionPipeline"),Yr=a(" runs inference with full "),Ft=r("code"),Kr=a("float32"),Wr=a(" precision for 50 inference steps. You can speed this up by switching to a lower precision like "),Qt=r("code"),Rr=a("float16"),Vr=a(" or running fewer inference steps."),Qs=f(),J=r("p"),Fr=a("Let\u2019s start by loading the model in "),Jt=r("code"),Qr=a("float16"),Jr=a(" and generate an image:"),Js=f(),c(je.$$.fragment),Xs=f(),Me=r("div"),Xt=r("img"),Zs=f(),_t=r("p"),Xr=a("This time, it only took ~11 seconds to generate the image, which is almost 3x faster than before!"),ei=f(),c(X.$$.fragment),ti=f(),x=r("p"),Zr=a("Another option is to reduce the number of inference steps. Choosing a more efficient scheduler could help decrease the number of steps without sacrificing output quality. You can find which schedulers are compatible with the current model in the "),yt=r("a"),eo=a("DiffusionPipeline"),to=a(" by calling the "),Zt=r("code"),so=a("compatibles"),io=a(" method:"),si=f(),c(qe.$$.fragment),ii=f(),j=r("p"),ro=a("The Stable Diffusion model uses the "),vt=r("a"),oo=a("PNDMScheduler"),ao=a(" by default which usually requires ~50 inference steps, but more performant schedulers like "),bt=r("a"),lo=a("DPMSolverMultistepScheduler"),no=a(", require only ~20 or 25 inference steps. Use the "),wt=r("a"),po=a("ConfigMixin.from_config()"),fo=a(" method to load a new scheduler:"),ri=f(),c(Ae.$$.fragment),oi=f(),Z=r("p"),uo=a("Now set the "),es=r("code"),ho=a("num_inference_steps"),co=a(" to 20:"),ai=f(),c(Ge.$$.fragment),li=f(),xe=r("div"),ts=r("img"),ni=f(),$t=r("p"),mo=a("Great, you\u2019ve managed to cut the inference time to just 4 seconds! \u26A1\uFE0F"),pi=f(),L=r("h2"),ee=r("a"),ss=r("span"),c(Ie.$$.fragment),go=f(),is=r("span"),_o=a("Memory"),fi=f(),te=r("p"),yo=a("The other key to improving pipeline performance is consuming less memory, which indirectly implies more speed, since you\u2019re often trying to maximize the number of images generated per second. The easiest way to see how many images you can generate at once is to try out different batch sizes until you get an "),rs=r("code"),vo=a("OutOfMemoryError"),bo=a(" (OOM)."),ui=f(),I=r("p"),wo=a("Create a function that\u2019ll generate a batch of images from a list of prompts and "),os=r("code"),$o=a("Generators"),Eo=a(". Make sure to assign each "),as=r("code"),ko=a("Generator"),Po=a(" a seed so you can reuse it if it produces a good result."),hi=f(),c(Te.$$.fragment),ci=f(),Et=r("p"),Do=a("You\u2019ll also need a function that\u2019ll display each batch of images:"),mi=f(),c(ze.$$.fragment),di=f(),se=r("p"),So=a("Start with "),ls=r("code"),jo=a("batch_size=4"),Mo=a(" and see how much memory you\u2019ve consumed:"),gi=f(),c(Ne.$$.fragment),_i=f(),T=r("p"),qo=a("Unless you have a GPU with more RAM, the code above probably returned an "),ns=r("code"),Ao=a("OOM"),Go=a(" error! Most of the memory is taken up by the cross-attention layers. Instead of running this operation in a batch, you can run it sequentially to save a significant amount of memory. All you have to do is configure the pipeline to use the "),kt=r("a"),xo=a("enable_attention_slicing()"),Io=a(" function:"),yi=f(),c(Oe.$$.fragment),vi=f(),ie=r("p"),To=a("Now try increasing the "),ps=r("code"),zo=a("batch_size"),No=a(" to 8!"),bi=f(),c(Ce.$$.fragment),wi=f(),Le=r("div"),fs=r("img"),$i=f(),Pt=r("p"),Oo=a("Whereas before you couldn\u2019t even generate a batch of 4 images, now you can generate a batch of 8 images at ~3.5 seconds per image! This is probably the fastest you can go on a T4 GPU without sacrificing quality."),Ei=f(),U=r("h2"),re=r("a"),us=r("span"),c(Ue.$$.fragment),Co=f(),hs=r("span"),Lo=a("Quality"),ki=f(),oe=r("p"),Uo=a("In the last two sections, you learned how to optimize the speed of your pipeline by using "),cs=r("code"),Bo=a("fp16"),Ho=a(", reducing the number of inference steps by using a more performant scheduler, and enabling attention slicing to reduce memory consumption. Now you\u2019re going to focus on how to improve the quality of generated images."),Pi=f(),B=r("h3"),ae=r("a"),ms=r("span"),c(Be.$$.fragment),Yo=f(),ds=r("span"),Ko=a("Better checkpoints"),Di=f(),le=r("p"),Wo=a("The most obvious step is to use better checkpoints. The Stable Diffusion model is a good starting point, and since its official launch, several improved versions have also been released. However, using a newer version doesn\u2019t automatically mean you\u2019ll get better results. You\u2019ll still have to experiment with different checkpoints yourself, and do a little research (such as using "),He=r("a"),Ro=a("negative prompts"),Vo=a(") to get the best results."),Si=f(),z=r("p"),Fo=a("As the field grows, there are more and more high-quality checkpoints finetuned to produce certain styles. Try exploring the "),Ye=r("a"),Qo=a("Hub"),Jo=a(" and "),Ke=r("a"),Xo=a("Diffusers Gallery"),Zo=a(" to find one you\u2019re interested in!"),ji=f(),H=r("h3"),ne=r("a"),gs=r("span"),c(We.$$.fragment),ea=f(),_s=r("span"),ta=a("Better pipeline components"),Mi=f(),pe=r("p"),sa=a("You can also try replacing the current pipeline components with a newer version. Let\u2019s try loading the latest "),Re=r("a"),ia=a("autodecoder"),ra=a(" from Stability AI into the pipeline, and generate some images:"),qi=f(),c(Ve.$$.fragment),Ai=f(),Fe=r("div"),ys=r("img"),Gi=f(),Y=r("h3"),fe=r("a"),vs=r("span"),c(Qe.$$.fragment),oa=f(),bs=r("span"),aa=a("Better prompt engineering"),xi=f(),ue=r("p"),la=a("The text prompt you use to generate an image is super important, so much so that it is called "),ws=r("em"),na=a("prompt engineering"),pa=a(". Some considerations to keep during prompt engineering are:"),Ii=f(),he=r("ul"),$s=r("li"),fa=a("How is the image or similar images of the one I want to generate stored on the internet?"),ua=f(),Es=r("li"),ha=a("What additional detail can I give that steers the model towards the style I want?"),Ti=f(),Dt=r("p"),ca=a("With this in mind, let\u2019s improve the prompt to include color and higher quality details:"),zi=f(),c(Je.$$.fragment),Ni=f(),St=r("p"),ma=a("Generate a batch of images with the new prompt:"),Oi=f(),c(Xe.$$.fragment),Ci=f(),Ze=r("div"),ks=r("img"),Li=f(),N=r("p"),da=a("Pretty impressive! Let\u2019s tweak the second image - corresponding to the "),Ps=r("code"),ga=a("Generator"),_a=a(" with a seed of "),Ds=r("code"),ya=a("1"),va=a(" - a bit more by adding some text about the age of the subject:"),Ui=f(),c(et.$$.fragment),Bi=f(),tt=r("div"),Ss=r("img"),Hi=f(),K=r("h2"),ce=r("a"),js=r("span"),c(st.$$.fragment),ba=f(),Ms=r("span"),wa=a("Next steps"),Yi=f(),me=r("p"),$a=a("In this tutorial, you learned how to optimize a "),jt=r("a"),Ea=a("DiffusionPipeline"),ka=a(" for computational and memory efficiency as well as improving the quality of generated outputs. If you\u2019re interested in making your pipeline even faster, take a look at the following resources:"),Ki=f(),O=r("ul"),it=r("li"),Pa=a("Enable "),Mt=r("a"),Da=a("xFormers"),Sa=a(" memory efficient attention mechanism for faster speed and reduced memory consumption."),ja=f(),W=r("li"),Ma=a("Learn how in "),qt=r("a"),qa=a("PyTorch 2.0"),Aa=a(", "),rt=r("a"),qs=r("code"),Ga=a("torch.compile"),xa=a(" can yield 2-9% faster inference speed."),Ia=f(),ot=r("li"),Ta=a("Many optimization techniques for inference are also included in this memory and speed "),At=r("a"),za=a("guide"),Na=a(", such as memory offloading."),this.h()},l(e){const i=kn('[data-svelte="svelte-1phssyn"]',document.head);v=o(i,"META",{name:!0,content:!0}),i.forEach(t),D=u(e),b=o(e,"H1",{class:!0});var at=n(b);k=o(at,"A",{id:!0,class:!0,href:!0});var As=n(k);P=o(As,"SPAN",{});var Ka=n(P);m(w.$$.fragment,Ka),Ka.forEach(t),As.forEach(t),E=u(at),M=o(at,"SPAN",{});var Wa=n(M);dr=l(Wa,"Effective and efficient diffusion"),Wa.forEach(t),at.forEach(t),Gs=u(e),m(ye.$$.fragment,e),xs=u(e),q=o(e,"P",{});var Gt=n(q);gr=l(Gt,"Getting the "),pt=o(Gt,"A",{href:!0});var Ra=n(pt);_r=l(Ra,"DiffusionPipeline"),Ra.forEach(t),yr=l(Gt," to generate images in a certain style or include what you want can be tricky. Often times, you have to run the "),ft=o(Gt,"A",{href:!0});var Va=n(ft);vr=l(Va,"DiffusionPipeline"),Va.forEach(t),br=l(Gt," several times before you end up with an image you\u2019re happy with. But generating something out of nothing is a computationally intensive process, especially if you\u2019re running inference over and over again."),Gt.forEach(t),Is=u(e),A=o(e,"P",{});var xt=n(A);wr=l(xt,"This is why it\u2019s important to get the most "),Bt=o(xt,"EM",{});var Fa=n(Bt);$r=l(Fa,"computational"),Fa.forEach(t),Er=l(xt," (speed) and "),Ht=o(xt,"EM",{});var Qa=n(Ht);kr=l(Qa,"memory"),Qa.forEach(t),Pr=l(xt," (GPU RAM) efficiency from the pipeline to reduce the time between inference cycles so you can iterate faster."),xt.forEach(t),Ts=u(e),R=o(e,"P",{});var Ri=n(R);Dr=l(Ri,"This tutorial walks you through how to generate faster and better with the "),ut=o(Ri,"A",{href:!0});var Ja=n(ut);Sr=l(Ja,"DiffusionPipeline"),Ja.forEach(t),jr=l(Ri,"."),Ri.forEach(t),zs=u(e),V=o(e,"P",{});var Vi=n(V);Mr=l(Vi,"Begin by loading the "),ve=o(Vi,"A",{href:!0,rel:!0});var Xa=n(ve);Yt=o(Xa,"CODE",{});var Za=n(Yt);qr=l(Za,"runwayml/stable-diffusion-v1-5"),Za.forEach(t),Xa.forEach(t),Ar=l(Vi," model:"),Vi.forEach(t),Ns=u(e),m(be.$$.fragment,e),Os=u(e),ht=o(e,"P",{});var el=n(ht);Gr=l(el,"The example prompt you\u2019ll use is a portrait of an old warrior chief, but feel free to use your own prompt:"),el.forEach(t),Cs=u(e),m(we.$$.fragment,e),Ls=u(e),C=o(e,"H2",{class:!0});var Fi=n(C);F=o(Fi,"A",{id:!0,class:!0,href:!0});var tl=n(F);Kt=o(tl,"SPAN",{});var sl=n(Kt);m($e.$$.fragment,sl),sl.forEach(t),tl.forEach(t),xr=u(Fi),Wt=o(Fi,"SPAN",{});var il=n(Wt);Ir=l(il,"Speed"),il.forEach(t),Fi.forEach(t),Us=u(e),m(Q.$$.fragment,e),Bs=u(e),ct=o(e,"P",{});var rl=n(ct);Tr=l(rl,"One of the simplest ways to speed up inference is to place the pipeline on a GPU the same way you would with any PyTorch module:"),rl.forEach(t),Hs=u(e),m(Ee.$$.fragment,e),Ys=u(e),G=o(e,"P",{});var It=n(G);zr=l(It,"To make sure you can use the same image and improve on it, use a "),ke=o(It,"A",{href:!0,rel:!0});var ol=n(ke);Rt=o(ol,"CODE",{});var al=n(Rt);Nr=l(al,"Generator"),al.forEach(t),ol.forEach(t),Or=l(It," and set a seed for "),mt=o(It,"A",{href:!0});var ll=n(mt);Cr=l(ll,"reproducibility"),ll.forEach(t),Lr=l(It,":"),It.forEach(t),Ks=u(e),m(Pe.$$.fragment,e),Ws=u(e),dt=o(e,"P",{});var nl=n(dt);Ur=l(nl,"Now you can generate an image:"),nl.forEach(t),Rs=u(e),m(De.$$.fragment,e),Vs=u(e),Se=o(e,"DIV",{class:!0});var pl=n(Se);Vt=o(pl,"IMG",{src:!0}),pl.forEach(t),Fs=u(e),S=o(e,"P",{});var de=n(S);Br=l(de,"This process took ~30 seconds on a T4 GPU (it might be faster if your allocated GPU is better than a T4). By default, the "),gt=o(de,"A",{href:!0});var fl=n(gt);Hr=l(fl,"DiffusionPipeline"),fl.forEach(t),Yr=l(de," runs inference with full "),Ft=o(de,"CODE",{});var ul=n(Ft);Kr=l(ul,"float32"),ul.forEach(t),Wr=l(de," precision for 50 inference steps. You can speed this up by switching to a lower precision like "),Qt=o(de,"CODE",{});var hl=n(Qt);Rr=l(hl,"float16"),hl.forEach(t),Vr=l(de," or running fewer inference steps."),de.forEach(t),Qs=u(e),J=o(e,"P",{});var Qi=n(J);Fr=l(Qi,"Let\u2019s start by loading the model in "),Jt=o(Qi,"CODE",{});var cl=n(Jt);Qr=l(cl,"float16"),cl.forEach(t),Jr=l(Qi," and generate an image:"),Qi.forEach(t),Js=u(e),m(je.$$.fragment,e),Xs=u(e),Me=o(e,"DIV",{class:!0});var ml=n(Me);Xt=o(ml,"IMG",{src:!0}),ml.forEach(t),Zs=u(e),_t=o(e,"P",{});var dl=n(_t);Xr=l(dl,"This time, it only took ~11 seconds to generate the image, which is almost 3x faster than before!"),dl.forEach(t),ei=u(e),m(X.$$.fragment,e),ti=u(e),x=o(e,"P",{});var Tt=n(x);Zr=l(Tt,"Another option is to reduce the number of inference steps. Choosing a more efficient scheduler could help decrease the number of steps without sacrificing output quality. You can find which schedulers are compatible with the current model in the "),yt=o(Tt,"A",{href:!0});var gl=n(yt);eo=l(gl,"DiffusionPipeline"),gl.forEach(t),to=l(Tt," by calling the "),Zt=o(Tt,"CODE",{});var _l=n(Zt);so=l(_l,"compatibles"),_l.forEach(t),io=l(Tt," method:"),Tt.forEach(t),si=u(e),m(qe.$$.fragment,e),ii=u(e),j=o(e,"P",{});var ge=n(j);ro=l(ge,"The Stable Diffusion model uses the "),vt=o(ge,"A",{href:!0});var yl=n(vt);oo=l(yl,"PNDMScheduler"),yl.forEach(t),ao=l(ge," by default which usually requires ~50 inference steps, but more performant schedulers like "),bt=o(ge,"A",{href:!0});var vl=n(bt);lo=l(vl,"DPMSolverMultistepScheduler"),vl.forEach(t),no=l(ge,", require only ~20 or 25 inference steps. Use the "),wt=o(ge,"A",{href:!0});var bl=n(wt);po=l(bl,"ConfigMixin.from_config()"),bl.forEach(t),fo=l(ge," method to load a new scheduler:"),ge.forEach(t),ri=u(e),m(Ae.$$.fragment,e),oi=u(e),Z=o(e,"P",{});var Ji=n(Z);uo=l(Ji,"Now set the "),es=o(Ji,"CODE",{});var wl=n(es);ho=l(wl,"num_inference_steps"),wl.forEach(t),co=l(Ji," to 20:"),Ji.forEach(t),ai=u(e),m(Ge.$$.fragment,e),li=u(e),xe=o(e,"DIV",{class:!0});var $l=n(xe);ts=o($l,"IMG",{src:!0}),$l.forEach(t),ni=u(e),$t=o(e,"P",{});var El=n($t);mo=l(El,"Great, you\u2019ve managed to cut the inference time to just 4 seconds! \u26A1\uFE0F"),El.forEach(t),pi=u(e),L=o(e,"H2",{class:!0});var Xi=n(L);ee=o(Xi,"A",{id:!0,class:!0,href:!0});var kl=n(ee);ss=o(kl,"SPAN",{});var Pl=n(ss);m(Ie.$$.fragment,Pl),Pl.forEach(t),kl.forEach(t),go=u(Xi),is=o(Xi,"SPAN",{});var Dl=n(is);_o=l(Dl,"Memory"),Dl.forEach(t),Xi.forEach(t),fi=u(e),te=o(e,"P",{});var Zi=n(te);yo=l(Zi,"The other key to improving pipeline performance is consuming less memory, which indirectly implies more speed, since you\u2019re often trying to maximize the number of images generated per second. The easiest way to see how many images you can generate at once is to try out different batch sizes until you get an "),rs=o(Zi,"CODE",{});var Sl=n(rs);vo=l(Sl,"OutOfMemoryError"),Sl.forEach(t),bo=l(Zi," (OOM)."),Zi.forEach(t),ui=u(e),I=o(e,"P",{});var zt=n(I);wo=l(zt,"Create a function that\u2019ll generate a batch of images from a list of prompts and "),os=o(zt,"CODE",{});var jl=n(os);$o=l(jl,"Generators"),jl.forEach(t),Eo=l(zt,". Make sure to assign each "),as=o(zt,"CODE",{});var Ml=n(as);ko=l(Ml,"Generator"),Ml.forEach(t),Po=l(zt," a seed so you can reuse it if it produces a good result."),zt.forEach(t),hi=u(e),m(Te.$$.fragment,e),ci=u(e),Et=o(e,"P",{});var ql=n(Et);Do=l(ql,"You\u2019ll also need a function that\u2019ll display each batch of images:"),ql.forEach(t),mi=u(e),m(ze.$$.fragment,e),di=u(e),se=o(e,"P",{});var er=n(se);So=l(er,"Start with "),ls=o(er,"CODE",{});var Al=n(ls);jo=l(Al,"batch_size=4"),Al.forEach(t),Mo=l(er," and see how much memory you\u2019ve consumed:"),er.forEach(t),gi=u(e),m(Ne.$$.fragment,e),_i=u(e),T=o(e,"P",{});var Nt=n(T);qo=l(Nt,"Unless you have a GPU with more RAM, the code above probably returned an "),ns=o(Nt,"CODE",{});var Gl=n(ns);Ao=l(Gl,"OOM"),Gl.forEach(t),Go=l(Nt," error! Most of the memory is taken up by the cross-attention layers. Instead of running this operation in a batch, you can run it sequentially to save a significant amount of memory. All you have to do is configure the pipeline to use the "),kt=o(Nt,"A",{href:!0});var xl=n(kt);xo=l(xl,"enable_attention_slicing()"),xl.forEach(t),Io=l(Nt," function:"),Nt.forEach(t),yi=u(e),m(Oe.$$.fragment,e),vi=u(e),ie=o(e,"P",{});var tr=n(ie);To=l(tr,"Now try increasing the "),ps=o(tr,"CODE",{});var Il=n(ps);zo=l(Il,"batch_size"),Il.forEach(t),No=l(tr," to 8!"),tr.forEach(t),bi=u(e),m(Ce.$$.fragment,e),wi=u(e),Le=o(e,"DIV",{class:!0});var Tl=n(Le);fs=o(Tl,"IMG",{src:!0}),Tl.forEach(t),$i=u(e),Pt=o(e,"P",{});var zl=n(Pt);Oo=l(zl,"Whereas before you couldn\u2019t even generate a batch of 4 images, now you can generate a batch of 8 images at ~3.5 seconds per image! This is probably the fastest you can go on a T4 GPU without sacrificing quality."),zl.forEach(t),Ei=u(e),U=o(e,"H2",{class:!0});var sr=n(U);re=o(sr,"A",{id:!0,class:!0,href:!0});var Nl=n(re);us=o(Nl,"SPAN",{});var Ol=n(us);m(Ue.$$.fragment,Ol),Ol.forEach(t),Nl.forEach(t),Co=u(sr),hs=o(sr,"SPAN",{});var Cl=n(hs);Lo=l(Cl,"Quality"),Cl.forEach(t),sr.forEach(t),ki=u(e),oe=o(e,"P",{});var ir=n(oe);Uo=l(ir,"In the last two sections, you learned how to optimize the speed of your pipeline by using "),cs=o(ir,"CODE",{});var Ll=n(cs);Bo=l(Ll,"fp16"),Ll.forEach(t),Ho=l(ir,", reducing the number of inference steps by using a more performant scheduler, and enabling attention slicing to reduce memory consumption. Now you\u2019re going to focus on how to improve the quality of generated images."),ir.forEach(t),Pi=u(e),B=o(e,"H3",{class:!0});var rr=n(B);ae=o(rr,"A",{id:!0,class:!0,href:!0});var Ul=n(ae);ms=o(Ul,"SPAN",{});var Bl=n(ms);m(Be.$$.fragment,Bl),Bl.forEach(t),Ul.forEach(t),Yo=u(rr),ds=o(rr,"SPAN",{});var Hl=n(ds);Ko=l(Hl,"Better checkpoints"),Hl.forEach(t),rr.forEach(t),Di=u(e),le=o(e,"P",{});var or=n(le);Wo=l(or,"The most obvious step is to use better checkpoints. The Stable Diffusion model is a good starting point, and since its official launch, several improved versions have also been released. However, using a newer version doesn\u2019t automatically mean you\u2019ll get better results. You\u2019ll still have to experiment with different checkpoints yourself, and do a little research (such as using "),He=o(or,"A",{href:!0,rel:!0});var Yl=n(He);Ro=l(Yl,"negative prompts"),Yl.forEach(t),Vo=l(or,") to get the best results."),or.forEach(t),Si=u(e),z=o(e,"P",{});var Ot=n(z);Fo=l(Ot,"As the field grows, there are more and more high-quality checkpoints finetuned to produce certain styles. Try exploring the "),Ye=o(Ot,"A",{href:!0,rel:!0});var Kl=n(Ye);Qo=l(Kl,"Hub"),Kl.forEach(t),Jo=l(Ot," and "),Ke=o(Ot,"A",{href:!0,rel:!0});var Wl=n(Ke);Xo=l(Wl,"Diffusers Gallery"),Wl.forEach(t),Zo=l(Ot," to find one you\u2019re interested in!"),Ot.forEach(t),ji=u(e),H=o(e,"H3",{class:!0});var ar=n(H);ne=o(ar,"A",{id:!0,class:!0,href:!0});var Rl=n(ne);gs=o(Rl,"SPAN",{});var Vl=n(gs);m(We.$$.fragment,Vl),Vl.forEach(t),Rl.forEach(t),ea=u(ar),_s=o(ar,"SPAN",{});var Fl=n(_s);ta=l(Fl,"Better pipeline components"),Fl.forEach(t),ar.forEach(t),Mi=u(e),pe=o(e,"P",{});var lr=n(pe);sa=l(lr,"You can also try replacing the current pipeline components with a newer version. Let\u2019s try loading the latest "),Re=o(lr,"A",{href:!0,rel:!0});var Ql=n(Re);ia=l(Ql,"autodecoder"),Ql.forEach(t),ra=l(lr," from Stability AI into the pipeline, and generate some images:"),lr.forEach(t),qi=u(e),m(Ve.$$.fragment,e),Ai=u(e),Fe=o(e,"DIV",{class:!0});var Jl=n(Fe);ys=o(Jl,"IMG",{src:!0}),Jl.forEach(t),Gi=u(e),Y=o(e,"H3",{class:!0});var nr=n(Y);fe=o(nr,"A",{id:!0,class:!0,href:!0});var Xl=n(fe);vs=o(Xl,"SPAN",{});var Zl=n(vs);m(Qe.$$.fragment,Zl),Zl.forEach(t),Xl.forEach(t),oa=u(nr),bs=o(nr,"SPAN",{});var en=n(bs);aa=l(en,"Better prompt engineering"),en.forEach(t),nr.forEach(t),xi=u(e),ue=o(e,"P",{});var pr=n(ue);la=l(pr,"The text prompt you use to generate an image is super important, so much so that it is called "),ws=o(pr,"EM",{});var tn=n(ws);na=l(tn,"prompt engineering"),tn.forEach(t),pa=l(pr,". Some considerations to keep during prompt engineering are:"),pr.forEach(t),Ii=u(e),he=o(e,"UL",{});var fr=n(he);$s=o(fr,"LI",{});var sn=n($s);fa=l(sn,"How is the image or similar images of the one I want to generate stored on the internet?"),sn.forEach(t),ua=u(fr),Es=o(fr,"LI",{});var rn=n(Es);ha=l(rn,"What additional detail can I give that steers the model towards the style I want?"),rn.forEach(t),fr.forEach(t),Ti=u(e),Dt=o(e,"P",{});var on=n(Dt);ca=l(on,"With this in mind, let\u2019s improve the prompt to include color and higher quality details:"),on.forEach(t),zi=u(e),m(Je.$$.fragment,e),Ni=u(e),St=o(e,"P",{});var an=n(St);ma=l(an,"Generate a batch of images with the new prompt:"),an.forEach(t),Oi=u(e),m(Xe.$$.fragment,e),Ci=u(e),Ze=o(e,"DIV",{class:!0});var ln=n(Ze);ks=o(ln,"IMG",{src:!0}),ln.forEach(t),Li=u(e),N=o(e,"P",{});var Ct=n(N);da=l(Ct,"Pretty impressive! Let\u2019s tweak the second image - corresponding to the "),Ps=o(Ct,"CODE",{});var nn=n(Ps);ga=l(nn,"Generator"),nn.forEach(t),_a=l(Ct," with a seed of "),Ds=o(Ct,"CODE",{});var pn=n(Ds);ya=l(pn,"1"),pn.forEach(t),va=l(Ct," - a bit more by adding some text about the age of the subject:"),Ct.forEach(t),Ui=u(e),m(et.$$.fragment,e),Bi=u(e),tt=o(e,"DIV",{class:!0});var fn=n(tt);Ss=o(fn,"IMG",{src:!0}),fn.forEach(t),Hi=u(e),K=o(e,"H2",{class:!0});var ur=n(K);ce=o(ur,"A",{id:!0,class:!0,href:!0});var un=n(ce);js=o(un,"SPAN",{});var hn=n(js);m(st.$$.fragment,hn),hn.forEach(t),un.forEach(t),ba=u(ur),Ms=o(ur,"SPAN",{});var cn=n(Ms);wa=l(cn,"Next steps"),cn.forEach(t),ur.forEach(t),Yi=u(e),me=o(e,"P",{});var hr=n(me);$a=l(hr,"In this tutorial, you learned how to optimize a "),jt=o(hr,"A",{href:!0});var mn=n(jt);Ea=l(mn,"DiffusionPipeline"),mn.forEach(t),ka=l(hr," for computational and memory efficiency as well as improving the quality of generated outputs. If you\u2019re interested in making your pipeline even faster, take a look at the following resources:"),hr.forEach(t),Ki=u(e),O=o(e,"UL",{});var Lt=n(O);it=o(Lt,"LI",{});var cr=n(it);Pa=l(cr,"Enable "),Mt=o(cr,"A",{href:!0});var dn=n(Mt);Da=l(dn,"xFormers"),dn.forEach(t),Sa=l(cr," memory efficient attention mechanism for faster speed and reduced memory consumption."),cr.forEach(t),ja=u(Lt),W=o(Lt,"LI",{});var Ut=n(W);Ma=l(Ut,"Learn how in "),qt=o(Ut,"A",{href:!0});var gn=n(qt);qa=l(gn,"PyTorch 2.0"),gn.forEach(t),Aa=l(Ut,", "),rt=o(Ut,"A",{href:!0,rel:!0});var _n=n(rt);qs=o(_n,"CODE",{});var yn=n(qs);Ga=l(yn,"torch.compile"),yn.forEach(t),_n.forEach(t),xa=l(Ut," can yield 2-9% faster inference speed."),Ut.forEach(t),Ia=u(Lt),ot=o(Lt,"LI",{});var mr=n(ot);Ta=l(mr,"Many optimization techniques for inference are also included in this memory and speed "),At=o(mr,"A",{href:!0});var vn=n(At);za=l(vn,"guide"),vn.forEach(t),Na=l(mr,", such as memory offloading."),mr.forEach(t),Lt.forEach(t),this.h()},h(){h(v,"name","hf:doc:metadata"),h(v,"content",JSON.stringify(qn)),h(k,"id","effective-and-efficient-diffusion"),h(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"),h(k,"href","#effective-and-efficient-diffusion"),h(b,"class","relative group"),h(pt,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),h(ft,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),h(ut,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),h(ve,"href","https://huggingface.co/runwayml/stable-diffusion-v1-5"),h(ve,"rel","nofollow"),h(F,"id","speed"),h(F,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(F,"href","#speed"),h(C,"class","relative group"),h(ke,"href","https://pytorch.org/docs/stable/generated/torch.Generator.html"),h(ke,"rel","nofollow"),h(mt,"href","./using-diffusers/reproducibility"),lt(Vt.src,Oa="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_1.png")||h(Vt,"src",Oa),h(Se,"class","flex justify-center"),h(gt,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),lt(Xt.src,Ca="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_2.png")||h(Xt,"src",Ca),h(Me,"class","flex justify-center"),h(yt,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),h(vt,"href","/docs/diffusers/v0.16.0/en/api/schedulers/pndm#diffusers.PNDMScheduler"),h(bt,"href","/docs/diffusers/v0.16.0/en/api/schedulers/multistep_dpm_solver#diffusers.DPMSolverMultistepScheduler"),h(wt,"href","/docs/diffusers/v0.16.0/en/api/configuration#diffusers.ConfigMixin.from_config"),lt(ts.src,La="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_3.png")||h(ts,"src",La),h(xe,"class","flex justify-center"),h(ee,"id","memory"),h(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"),h(ee,"href","#memory"),h(L,"class","relative group"),h(kt,"href","/docs/diffusers/v0.16.0/en/api/pipelines/stable_diffusion/latent_upscale#diffusers.StableDiffusionLatentUpscalePipeline.enable_attention_slicing"),lt(fs.src,Ua="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_5.png")||h(fs,"src",Ua),h(Le,"class","flex justify-center"),h(re,"id","quality"),h(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"),h(re,"href","#quality"),h(U,"class","relative group"),h(ae,"id","better-checkpoints"),h(ae,"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"),h(ae,"href","#better-checkpoints"),h(B,"class","relative group"),h(He,"href","https://minimaxir.com/2022/11/stable-diffusion-negative-prompt/"),h(He,"rel","nofollow"),h(Ye,"href","https://huggingface.co/models?library=diffusers&sort=downloads"),h(Ye,"rel","nofollow"),h(Ke,"href","https://huggingface.co/spaces/huggingface-projects/diffusers-gallery"),h(Ke,"rel","nofollow"),h(ne,"id","better-pipeline-components"),h(ne,"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"),h(ne,"href","#better-pipeline-components"),h(H,"class","relative group"),h(Re,"href","https://huggingface.co/stabilityai/stable-diffusion-2-1/tree/main/vae"),h(Re,"rel","nofollow"),lt(ys.src,Ba="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_6.png")||h(ys,"src",Ba),h(Fe,"class","flex justify-center"),h(fe,"id","better-prompt-engineering"),h(fe,"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"),h(fe,"href","#better-prompt-engineering"),h(Y,"class","relative group"),lt(ks.src,Ha="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_7.png")||h(ks,"src",Ha),h(Ze,"class","flex justify-center"),lt(Ss.src,Ya="https://huggingface.co/datasets/diffusers/docs-images/resolve/main/stable_diffusion_101/sd_101_8.png")||h(Ss,"src",Ya),h(tt,"class","flex justify-center"),h(ce,"id","next-steps"),h(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"),h(ce,"href","#next-steps"),h(K,"class","relative group"),h(jt,"href","/docs/diffusers/v0.16.0/en/api/diffusion_pipeline#diffusers.DiffusionPipeline"),h(Mt,"href","./optimization/xformers"),h(qt,"href","./optimization/torch2.0"),h(rt,"href","https://pytorch.org/docs/stable/generated/torch.compile.html"),h(rt,"rel","nofollow"),h(At,"href","./optimization/fp16")},m(e,i){s(document.head,v),p(e,D,i),p(e,b,i),s(b,k),s(k,P),d(w,P,null),s(b,E),s(b,M),s(M,dr),p(e,Gs,i),d(ye,e,i),p(e,xs,i),p(e,q,i),s(q,gr),s(q,pt),s(pt,_r),s(q,yr),s(q,ft),s(ft,vr),s(q,br),p(e,Is,i),p(e,A,i),s(A,wr),s(A,Bt),s(Bt,$r),s(A,Er),s(A,Ht),s(Ht,kr),s(A,Pr),p(e,Ts,i),p(e,R,i),s(R,Dr),s(R,ut),s(ut,Sr),s(R,jr),p(e,zs,i),p(e,V,i),s(V,Mr),s(V,ve),s(ve,Yt),s(Yt,qr),s(V,Ar),p(e,Ns,i),d(be,e,i),p(e,Os,i),p(e,ht,i),s(ht,Gr),p(e,Cs,i),d(we,e,i),p(e,Ls,i),p(e,C,i),s(C,F),s(F,Kt),d($e,Kt,null),s(C,xr),s(C,Wt),s(Wt,Ir),p(e,Us,i),d(Q,e,i),p(e,Bs,i),p(e,ct,i),s(ct,Tr),p(e,Hs,i),d(Ee,e,i),p(e,Ys,i),p(e,G,i),s(G,zr),s(G,ke),s(ke,Rt),s(Rt,Nr),s(G,Or),s(G,mt),s(mt,Cr),s(G,Lr),p(e,Ks,i),d(Pe,e,i),p(e,Ws,i),p(e,dt,i),s(dt,Ur),p(e,Rs,i),d(De,e,i),p(e,Vs,i),p(e,Se,i),s(Se,Vt),p(e,Fs,i),p(e,S,i),s(S,Br),s(S,gt),s(gt,Hr),s(S,Yr),s(S,Ft),s(Ft,Kr),s(S,Wr),s(S,Qt),s(Qt,Rr),s(S,Vr),p(e,Qs,i),p(e,J,i),s(J,Fr),s(J,Jt),s(Jt,Qr),s(J,Jr),p(e,Js,i),d(je,e,i),p(e,Xs,i),p(e,Me,i),s(Me,Xt),p(e,Zs,i),p(e,_t,i),s(_t,Xr),p(e,ei,i),d(X,e,i),p(e,ti,i),p(e,x,i),s(x,Zr),s(x,yt),s(yt,eo),s(x,to),s(x,Zt),s(Zt,so),s(x,io),p(e,si,i),d(qe,e,i),p(e,ii,i),p(e,j,i),s(j,ro),s(j,vt),s(vt,oo),s(j,ao),s(j,bt),s(bt,lo),s(j,no),s(j,wt),s(wt,po),s(j,fo),p(e,ri,i),d(Ae,e,i),p(e,oi,i),p(e,Z,i),s(Z,uo),s(Z,es),s(es,ho),s(Z,co),p(e,ai,i),d(Ge,e,i),p(e,li,i),p(e,xe,i),s(xe,ts),p(e,ni,i),p(e,$t,i),s($t,mo),p(e,pi,i),p(e,L,i),s(L,ee),s(ee,ss),d(Ie,ss,null),s(L,go),s(L,is),s(is,_o),p(e,fi,i),p(e,te,i),s(te,yo),s(te,rs),s(rs,vo),s(te,bo),p(e,ui,i),p(e,I,i),s(I,wo),s(I,os),s(os,$o),s(I,Eo),s(I,as),s(as,ko),s(I,Po),p(e,hi,i),d(Te,e,i),p(e,ci,i),p(e,Et,i),s(Et,Do),p(e,mi,i),d(ze,e,i),p(e,di,i),p(e,se,i),s(se,So),s(se,ls),s(ls,jo),s(se,Mo),p(e,gi,i),d(Ne,e,i),p(e,_i,i),p(e,T,i),s(T,qo),s(T,ns),s(ns,Ao),s(T,Go),s(T,kt),s(kt,xo),s(T,Io),p(e,yi,i),d(Oe,e,i),p(e,vi,i),p(e,ie,i),s(ie,To),s(ie,ps),s(ps,zo),s(ie,No),p(e,bi,i),d(Ce,e,i),p(e,wi,i),p(e,Le,i),s(Le,fs),p(e,$i,i),p(e,Pt,i),s(Pt,Oo),p(e,Ei,i),p(e,U,i),s(U,re),s(re,us),d(Ue,us,null),s(U,Co),s(U,hs),s(hs,Lo),p(e,ki,i),p(e,oe,i),s(oe,Uo),s(oe,cs),s(cs,Bo),s(oe,Ho),p(e,Pi,i),p(e,B,i),s(B,ae),s(ae,ms),d(Be,ms,null),s(B,Yo),s(B,ds),s(ds,Ko),p(e,Di,i),p(e,le,i),s(le,Wo),s(le,He),s(He,Ro),s(le,Vo),p(e,Si,i),p(e,z,i),s(z,Fo),s(z,Ye),s(Ye,Qo),s(z,Jo),s(z,Ke),s(Ke,Xo),s(z,Zo),p(e,ji,i),p(e,H,i),s(H,ne),s(ne,gs),d(We,gs,null),s(H,ea),s(H,_s),s(_s,ta),p(e,Mi,i),p(e,pe,i),s(pe,sa),s(pe,Re),s(Re,ia),s(pe,ra),p(e,qi,i),d(Ve,e,i),p(e,Ai,i),p(e,Fe,i),s(Fe,ys),p(e,Gi,i),p(e,Y,i),s(Y,fe),s(fe,vs),d(Qe,vs,null),s(Y,oa),s(Y,bs),s(bs,aa),p(e,xi,i),p(e,ue,i),s(ue,la),s(ue,ws),s(ws,na),s(ue,pa),p(e,Ii,i),p(e,he,i),s(he,$s),s($s,fa),s(he,ua),s(he,Es),s(Es,ha),p(e,Ti,i),p(e,Dt,i),s(Dt,ca),p(e,zi,i),d(Je,e,i),p(e,Ni,i),p(e,St,i),s(St,ma),p(e,Oi,i),d(Xe,e,i),p(e,Ci,i),p(e,Ze,i),s(Ze,ks),p(e,Li,i),p(e,N,i),s(N,da),s(N,Ps),s(Ps,ga),s(N,_a),s(N,Ds),s(Ds,ya),s(N,va),p(e,Ui,i),d(et,e,i),p(e,Bi,i),p(e,tt,i),s(tt,Ss),p(e,Hi,i),p(e,K,i),s(K,ce),s(ce,js),d(st,js,null),s(K,ba),s(K,Ms),s(Ms,wa),p(e,Yi,i),p(e,me,i),s(me,$a),s(me,jt),s(jt,Ea),s(me,ka),p(e,Ki,i),p(e,O,i),s(O,it),s(it,Pa),s(it,Mt),s(Mt,Da),s(it,Sa),s(O,ja),s(O,W),s(W,Ma),s(W,qt),s(qt,qa),s(W,Aa),s(W,rt),s(rt,qs),s(qs,Ga),s(W,xa),s(O,Ia),s(O,ot),s(ot,Ta),s(ot,At),s(At,za),s(ot,Na),Wi=!0},p(e,[i]){const at={};i&2&&(at.$$scope={dirty:i,ctx:e}),Q.$set(at);const As={};i&2&&(As.$$scope={dirty:i,ctx:e}),X.$set(As)},i(e){Wi||(g(w.$$.fragment,e),g(ye.$$.fragment,e),g(be.$$.fragment,e),g(we.$$.fragment,e),g($e.$$.fragment,e),g(Q.$$.fragment,e),g(Ee.$$.fragment,e),g(Pe.$$.fragment,e),g(De.$$.fragment,e),g(je.$$.fragment,e),g(X.$$.fragment,e),g(qe.$$.fragment,e),g(Ae.$$.fragment,e),g(Ge.$$.fragment,e),g(Ie.$$.fragment,e),g(Te.$$.fragment,e),g(ze.$$.fragment,e),g(Ne.$$.fragment,e),g(Oe.$$.fragment,e),g(Ce.$$.fragment,e),g(Ue.$$.fragment,e),g(Be.$$.fragment,e),g(We.$$.fragment,e),g(Ve.$$.fragment,e),g(Qe.$$.fragment,e),g(Je.$$.fragment,e),g(Xe.$$.fragment,e),g(et.$$.fragment,e),g(st.$$.fragment,e),Wi=!0)},o(e){_(w.$$.fragment,e),_(ye.$$.fragment,e),_(be.$$.fragment,e),_(we.$$.fragment,e),_($e.$$.fragment,e),_(Q.$$.fragment,e),_(Ee.$$.fragment,e),_(Pe.$$.fragment,e),_(De.$$.fragment,e),_(je.$$.fragment,e),_(X.$$.fragment,e),_(qe.$$.fragment,e),_(Ae.$$.fragment,e),_(Ge.$$.fragment,e),_(Ie.$$.fragment,e),_(Te.$$.fragment,e),_(ze.$$.fragment,e),_(Ne.$$.fragment,e),_(Oe.$$.fragment,e),_(Ce.$$.fragment,e),_(Ue.$$.fragment,e),_(Be.$$.fragment,e),_(We.$$.fragment,e),_(Ve.$$.fragment,e),_(Qe.$$.fragment,e),_(Je.$$.fragment,e),_(Xe.$$.fragment,e),_(et.$$.fragment,e),_(st.$$.fragment,e),Wi=!1},d(e){t(v),e&&t(D),e&&t(b),y(w),e&&t(Gs),y(ye,e),e&&t(xs),e&&t(q),e&&t(Is),e&&t(A),e&&t(Ts),e&&t(R),e&&t(zs),e&&t(V),e&&t(Ns),y(be,e),e&&t(Os),e&&t(ht),e&&t(Cs),y(we,e),e&&t(Ls),e&&t(C),y($e),e&&t(Us),y(Q,e),e&&t(Bs),e&&t(ct),e&&t(Hs),y(Ee,e),e&&t(Ys),e&&t(G),e&&t(Ks),y(Pe,e),e&&t(Ws),e&&t(dt),e&&t(Rs),y(De,e),e&&t(Vs),e&&t(Se),e&&t(Fs),e&&t(S),e&&t(Qs),e&&t(J),e&&t(Js),y(je,e),e&&t(Xs),e&&t(Me),e&&t(Zs),e&&t(_t),e&&t(ei),y(X,e),e&&t(ti),e&&t(x),e&&t(si),y(qe,e),e&&t(ii),e&&t(j),e&&t(ri),y(Ae,e),e&&t(oi),e&&t(Z),e&&t(ai),y(Ge,e),e&&t(li),e&&t(xe),e&&t(ni),e&&t($t),e&&t(pi),e&&t(L),y(Ie),e&&t(fi),e&&t(te),e&&t(ui),e&&t(I),e&&t(hi),y(Te,e),e&&t(ci),e&&t(Et),e&&t(mi),y(ze,e),e&&t(di),e&&t(se),e&&t(gi),y(Ne,e),e&&t(_i),e&&t(T),e&&t(yi),y(Oe,e),e&&t(vi),e&&t(ie),e&&t(bi),y(Ce,e),e&&t(wi),e&&t(Le),e&&t($i),e&&t(Pt),e&&t(Ei),e&&t(U),y(Ue),e&&t(ki),e&&t(oe),e&&t(Pi),e&&t(B),y(Be),e&&t(Di),e&&t(le),e&&t(Si),e&&t(z),e&&t(ji),e&&t(H),y(We),e&&t(Mi),e&&t(pe),e&&t(qi),y(Ve,e),e&&t(Ai),e&&t(Fe),e&&t(Gi),e&&t(Y),y(Qe),e&&t(xi),e&&t(ue),e&&t(Ii),e&&t(he),e&&t(Ti),e&&t(Dt),e&&t(zi),y(Je,e),e&&t(Ni),e&&t(St),e&&t(Oi),y(Xe,e),e&&t(Ci),e&&t(Ze),e&&t(Li),e&&t(N),e&&t(Ui),y(et,e),e&&t(Bi),e&&t(tt),e&&t(Hi),e&&t(K),y(st),e&&t(Yi),e&&t(me),e&&t(Ki),e&&t(O)}}}const qn={local:"effective-and-efficient-diffusion",sections:[{local:"speed",title:"Speed"},{local:"memory",title:"Memory"},{local:"quality",sections:[{local:"better-checkpoints",title:"Better checkpoints"},{local:"better-pipeline-components",title:"Better pipeline components"},{local:"better-prompt-engineering",title:"Better prompt engineering"}],title:"Quality"},{local:"next-steps",title:"Next steps"}],title:"Effective and efficient diffusion"};function An(nt){return Pn(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Nn extends wn{constructor(v){super();$n(this,v,An,Mn,En,{})}}export{Nn as default,qn as metadata};
