import{S as Nf,i as Uf,s as If,e as a,k as p,w as u,t as r,M as Cf,c as n,d as s,m as f,a as o,x as d,h as i,b as h,G as t,g as c,y as m,q as _,o as v,B as y,v as Ff}from"../../chunks/vendor-hf-doc-builder.js";import{T as Rf}from"../../chunks/Tip-hf-doc-builder.js";import{I as D}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as k}from"../../chunks/CodeBlock-hf-doc-builder.js";function Mf($a){let g;return{c(){g=r(`Attention slicing is useful even if a batch size of just 1 is used - as long
  as the model uses more than one attention head. If there is more than one
  attention head the *QK^T* attention matrix can be computed sequentially for
  each head which can save a significant amount of memory.`)},l(A){g=i(A,`Attention slicing is useful even if a batch size of just 1 is used - as long
  as the model uses more than one attention head. If there is more than one
  attention head the *QK^T* attention matrix can be computed sequentially for
  each head which can save a significant amount of memory.`)},m(A,T){c(A,g,T)},d(A){A&&s(g)}}}function zf($a){let g,A,T,re,Xt,Pe,Ln,Yt,Xn,Ea,j,Yn,Wt,Wn,Kn,xe,Qn,Jn,ot,Zn,eo,ja,rt,to,ka,ie,Kt,U,Ta,so,Qt,ao,no,Jt,oo,ro,w,I,Zt,io,lo,es,po,fo,ts,co,ho,C,ss,uo,mo,as,_o,vo,ns,yo,wo,F,os,bo,go,rs,$o,Eo,is,jo,ko,R,ls,To,Do,ps,Ao,Po,fs,xo,So,M,cs,qo,No,hs,Uo,Io,us,Co,Fo,z,ds,Ro,Mo,ms,zo,Ho,_s,Vo,Go,H,vs,Oo,Bo,ys,Lo,Xo,ws,Yo,Da,it,Wo,Aa,V,le,bs,Se,Ko,gs,Qo,Pa,qe,Ne,Jo,Zo,xa,pe,er,$s,tr,sr,Sa,Ue,qa,G,fe,Es,Ie,ar,js,nr,Na,ce,or,Ce,rr,ir,Ua,Fe,Ia,O,he,ks,Re,lr,Ts,pr,Ca,P,fr,Ds,cr,hr,As,ur,dr,Fa,Me,Ra,ue,mr,Ps,_r,vr,Ma,B,de,xs,ze,yr,Ss,wr,za,x,br,qs,gr,$r,Ns,Er,jr,Ha,He,Va,L,me,Us,Ve,kr,Is,Tr,Ga,lt,Dr,Oa,_e,Ba,ve,Ar,pt,Pr,xr,La,Ge,Xa,ft,Sr,Ya,X,ye,Cs,Oe,qr,Fs,Nr,Wa,ct,Ur,Ka,S,Ir,ht,Cr,Fr,ut,Rr,Mr,Qa,we,zr,dt,Hr,Vr,Ja,Be,Za,mt,Gr,en,Y,be,Rs,Le,Or,Ms,Br,tn,_t,Lr,sn,ge,Xr,zs,Yr,Wr,an,Xe,nn,vt,Kr,on,yt,Qr,rn,Ye,ln,W,$e,Hs,We,Jr,Vs,Zr,pn,wt,ei,fn,bt,ti,cn,Ke,hn,K,Ee,Gs,Qe,si,Os,ai,un,je,ni,Bs,oi,ri,dn,gt,ii,mn,Je,_n,ke,li,Ls,pi,fi,vn,Ze,yn,Q,Te,Xs,et,ci,Ys,hi,wn,q,ui,tt,di,mi,st,_i,vi,bn,$t,yi,gn,De,Ws,J,Ks,wi,bi,Qs,gi,$i,Js,Ei,ji,b,Z,Zs,ki,Ti,ea,Di,Ai,ta,Pi,xi,ee,sa,Si,qi,aa,Ni,Ui,na,Ii,Ci,te,oa,Fi,Ri,ra,Mi,zi,ia,Hi,Vi,se,la,Gi,Oi,pa,Bi,Li,fa,Xi,Yi,ae,ca,Wi,Ki,ha,Qi,Ji,ua,Zi,el,ne,da,tl,sl,ma,al,nl,_a,ol,rl,oe,va,il,ll,ya,pl,fl,wa,cl,$n,Et,hl,En,N,ba,ul,dl,ga,ml,_l,jt,kt,vl,yl,jn,at,kn;return Pe=new D({}),Se=new D({}),Ue=new k({props:{code:`import torch

torch.backends.cudnn.benchmark = True`,highlighted:`<span class="hljs-keyword">import</span> torch

torch.backends.cudnn.benchmark = <span class="hljs-literal">True</span>`}}),Ie=new D({}),Fe=new k({props:{code:`import torch

torch.backends.cuda.matmul.allow_tf32 = True`,highlighted:`<span class="hljs-keyword">import</span> torch

torch.backends.cuda.matmul.allow_tf32 = <span class="hljs-literal">True</span>`}}),Re=new D({}),Me=new k({props:{code:`from torch import autocast
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
with autocast("cuda"):
    image = pipe(prompt).images[0]`,highlighted:`<span class="hljs-keyword">from</span> torch <span class="hljs-keyword">import</span> autocast
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
<span class="hljs-keyword">with</span> autocast(<span class="hljs-string">&quot;cuda&quot;</span>):
    image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),ze=new D({}),He=new k({props:{code:`pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    
    torch_dtype=torch.float16,
)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]`,highlighted:`pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    
    torch_dtype=torch.float16,
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),Ve=new D({}),_e=new Rf({props:{$$slots:{default:[Mf]},$$scope:{ctx:$a}}}),Ge=new k({props:{code:`import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    
    torch_dtype=torch.float16,
)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
pipe.enable_attention_slicing()
image = pipe(prompt).images[0]`,highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    
    torch_dtype=torch.float16,
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
pipe.enable_attention_slicing()
image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),Oe=new D({}),Be=new k({props:{code:`import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    
    torch_dtype=torch.float16,
)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
pipe.enable_vae_slicing()
images = pipe([prompt] * 32).images`,highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    
    torch_dtype=torch.float16,
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
pipe.enable_vae_slicing()
images = pipe([prompt] * <span class="hljs-number">32</span>).images`}}),Le=new D({}),Xe=new k({props:{code:`import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    
    torch_dtype=torch.float16,
)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
pipe.enable_sequential_cpu_offload()
image = pipe(prompt).images[0]`,highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    
    torch_dtype=torch.float16,
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
pipe.enable_sequential_cpu_offload()
image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),Ye=new k({props:{code:`import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    
    torch_dtype=torch.float16,
)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
pipe.enable_sequential_cpu_offload()
pipe.enable_attention_slicing(1)

image = pipe(prompt).images[0]`,highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    
    torch_dtype=torch.float16,
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
pipe.enable_sequential_cpu_offload()
pipe.enable_attention_slicing(<span class="hljs-number">1</span>)

image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),We=new D({}),Ke=new k({props:{code:`print(pipe.unet.conv_out.state_dict()["weight"].stride())  # (2880, 9, 3, 1)
pipe.unet.to(memory_format=torch.channels_last)  # in-place operation
print(
    pipe.unet.conv_out.state_dict()["weight"].stride()
)  # (2880, 1, 960, 320) having a stride of 1 for the 2nd dimension proves that it works`,highlighted:`<span class="hljs-built_in">print</span>(pipe.unet.conv_out.state_dict()[<span class="hljs-string">&quot;weight&quot;</span>].stride())  <span class="hljs-comment"># (2880, 9, 3, 1)</span>
pipe.unet.to(memory_format=torch.channels_last)  <span class="hljs-comment"># in-place operation</span>
<span class="hljs-built_in">print</span>(
    pipe.unet.conv_out.state_dict()[<span class="hljs-string">&quot;weight&quot;</span>].stride()
)  <span class="hljs-comment"># (2880, 1, 960, 320) having a stride of 1 for the 2nd dimension proves that it works</span>`}}),Qe=new D({}),Je=new k({props:{code:`import time
import torch
from diffusers import StableDiffusionPipeline
import functools

# torch disable grad
torch.set_grad_enabled(False)

# set variables
n_experiments = 2
unet_runs_per_experiment = 50

# load inputs
def generate_inputs():
    sample = torch.randn(2, 4, 64, 64).half().cuda()
    timestep = torch.rand(1).half().cuda() * 999
    encoder_hidden_states = torch.randn(2, 77, 768).half().cuda()
    return sample, timestep, encoder_hidden_states


pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
).to("cuda")
unet = pipe.unet
unet.eval()
unet.to(memory_format=torch.channels_last)  # use channels_last memory format
unet.forward = functools.partial(unet.forward, return_dict=False)  # set return_dict=False as default

# warmup
for _ in range(3):
    with torch.inference_mode():
        inputs = generate_inputs()
        orig_output = unet(*inputs)

# trace
print("tracing..")
unet_traced = torch.jit.trace(unet, inputs)
unet_traced.eval()
print("done tracing")


# warmup and optimize graph
for _ in range(5):
    with torch.inference_mode():
        inputs = generate_inputs()
        orig_output = unet_traced(*inputs)


# benchmarking
with torch.inference_mode():
    for _ in range(n_experiments):
        torch.cuda.synchronize()
        start_time = time.time()
        for _ in range(unet_runs_per_experiment):
            orig_output = unet_traced(*inputs)
        torch.cuda.synchronize()
        print(f"unet traced inference took {time.time() - start_time:.2f} seconds")
    for _ in range(n_experiments):
        torch.cuda.synchronize()
        start_time = time.time()
        for _ in range(unet_runs_per_experiment):
            orig_output = unet(*inputs)
        torch.cuda.synchronize()
        print(f"unet inference took {time.time() - start_time:.2f} seconds")

# save the model
unet_traced.save("unet_traced.pt")`,highlighted:`<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> functools

<span class="hljs-comment"># torch disable grad</span>
torch.set_grad_enabled(<span class="hljs-literal">False</span>)

<span class="hljs-comment"># set variables</span>
n_experiments = <span class="hljs-number">2</span>
unet_runs_per_experiment = <span class="hljs-number">50</span>

<span class="hljs-comment"># load inputs</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">generate_inputs</span>():
    sample = torch.randn(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">64</span>, <span class="hljs-number">64</span>).half().cuda()
    timestep = torch.rand(<span class="hljs-number">1</span>).half().cuda() * <span class="hljs-number">999</span>
    encoder_hidden_states = torch.randn(<span class="hljs-number">2</span>, <span class="hljs-number">77</span>, <span class="hljs-number">768</span>).half().cuda()
    <span class="hljs-keyword">return</span> sample, timestep, encoder_hidden_states


pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
unet = pipe.unet
unet.<span class="hljs-built_in">eval</span>()
unet.to(memory_format=torch.channels_last)  <span class="hljs-comment"># use channels_last memory format</span>
unet.forward = functools.partial(unet.forward, return_dict=<span class="hljs-literal">False</span>)  <span class="hljs-comment"># set return_dict=False as default</span>

<span class="hljs-comment"># warmup</span>
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>):
    <span class="hljs-keyword">with</span> torch.inference_mode():
        inputs = generate_inputs()
        orig_output = unet(*inputs)

<span class="hljs-comment"># trace</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;tracing..&quot;</span>)
unet_traced = torch.jit.trace(unet, inputs)
unet_traced.<span class="hljs-built_in">eval</span>()
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;done tracing&quot;</span>)


<span class="hljs-comment"># warmup and optimize graph</span>
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):
    <span class="hljs-keyword">with</span> torch.inference_mode():
        inputs = generate_inputs()
        orig_output = unet_traced(*inputs)


<span class="hljs-comment"># benchmarking</span>
<span class="hljs-keyword">with</span> torch.inference_mode():
    <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n_experiments):
        torch.cuda.synchronize()
        start_time = time.time()
        <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(unet_runs_per_experiment):
            orig_output = unet_traced(*inputs)
        torch.cuda.synchronize()
        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;unet traced inference took <span class="hljs-subst">{time.time() - start_time:<span class="hljs-number">.2</span>f}</span> seconds&quot;</span>)
    <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n_experiments):
        torch.cuda.synchronize()
        start_time = time.time()
        <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(unet_runs_per_experiment):
            orig_output = unet(*inputs)
        torch.cuda.synchronize()
        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;unet inference took <span class="hljs-subst">{time.time() - start_time:<span class="hljs-number">.2</span>f}</span> seconds&quot;</span>)

<span class="hljs-comment"># save the model</span>
unet_traced.save(<span class="hljs-string">&quot;unet_traced.pt&quot;</span>)`}}),Ze=new k({props:{code:`from diffusers import StableDiffusionPipeline
import torch
from dataclasses import dataclass


@dataclass
class UNet2DConditionOutput:
    sample: torch.FloatTensor


pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
).to("cuda")

# use jitted unet
unet_traced = torch.jit.load("unet_traced.pt")
# del pipe.unet
class TracedUNet(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.in_channels = pipe.unet.in_channels
        self.device = pipe.unet.device

    def forward(self, latent_model_input, t, encoder_hidden_states):
        sample = unet_traced(latent_model_input, t, encoder_hidden_states)[0]
        return UNet2DConditionOutput(sample=sample)


pipe.unet = TracedUNet()

with torch.inference_mode():
    image = pipe([prompt] * 1, num_inference_steps=50).images[0]`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass


<span class="hljs-meta">@dataclass</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">UNet2DConditionOutput</span>:
    sample: torch.FloatTensor


pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-comment"># use jitted unet</span>
unet_traced = torch.jit.load(<span class="hljs-string">&quot;unet_traced.pt&quot;</span>)
<span class="hljs-comment"># del pipe.unet</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">TracedUNet</span>(torch.nn.Module):
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>):
        <span class="hljs-built_in">super</span>().__init__()
        self.in_channels = pipe.unet.in_channels
        self.device = pipe.unet.device

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, latent_model_input, t, encoder_hidden_states</span>):
        sample = unet_traced(latent_model_input, t, encoder_hidden_states)[<span class="hljs-number">0</span>]
        <span class="hljs-keyword">return</span> UNet2DConditionOutput(sample=sample)


pipe.unet = TracedUNet()

<span class="hljs-keyword">with</span> torch.inference_mode():
    image = pipe([prompt] * <span class="hljs-number">1</span>, num_inference_steps=<span class="hljs-number">50</span>).images[<span class="hljs-number">0</span>]`}}),et=new D({}),at=new k({props:{code:`from diffusers import StableDiffusionPipeline
import torch

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
).to("cuda")

pipe.enable_xformers_memory_efficient_attention()

with torch.inference_mode():
    sample = pipe("a small cat")

# optional: You can disable it via
# pipe.disable_xformers_memory_efficient_attention()`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> torch

pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)

pipe.enable_xformers_memory_efficient_attention()

<span class="hljs-keyword">with</span> torch.inference_mode():
    sample = pipe(<span class="hljs-string">&quot;a small cat&quot;</span>)

<span class="hljs-comment"># optional: You can disable it via</span>
<span class="hljs-comment"># pipe.disable_xformers_memory_efficient_attention()</span>`}}),{c(){g=a("meta"),A=p(),T=a("h1"),re=a("a"),Xt=a("span"),u(Pe.$$.fragment),Ln=p(),Yt=a("span"),Xn=r("Memory and speed"),Ea=p(),j=a("p"),Yn=r("We present some techniques and ideas to optimize \u{1F917} Diffusers "),Wt=a("em"),Wn=r("inference"),Kn=r(" for memory or speed. As a general rule, we recommend the use of "),xe=a("a"),Qn=r("xFormers"),Jn=r(" for memory efficient attention, please see the recommended "),ot=a("a"),Zn=r("installation instructions"),eo=r("."),ja=p(),rt=a("p"),to=r("We\u2019ll discuss how the following settings impact performance and memory."),ka=p(),ie=a("table"),Kt=a("thead"),U=a("tr"),Ta=a("th"),so=p(),Qt=a("th"),ao=r("Latency"),no=p(),Jt=a("th"),oo=r("Speedup"),ro=p(),w=a("tbody"),I=a("tr"),Zt=a("td"),io=r("original"),lo=p(),es=a("td"),po=r("9.50s"),fo=p(),ts=a("td"),co=r("x1"),ho=p(),C=a("tr"),ss=a("td"),uo=r("cuDNN auto-tuner"),mo=p(),as=a("td"),_o=r("9.37s"),vo=p(),ns=a("td"),yo=r("x1.01"),wo=p(),F=a("tr"),os=a("td"),bo=r("autocast (fp16)"),go=p(),rs=a("td"),$o=r("5.47s"),Eo=p(),is=a("td"),jo=r("x1.74"),ko=p(),R=a("tr"),ls=a("td"),To=r("fp16"),Do=p(),ps=a("td"),Ao=r("3.61s"),Po=p(),fs=a("td"),xo=r("x2.63"),So=p(),M=a("tr"),cs=a("td"),qo=r("channels last"),No=p(),hs=a("td"),Uo=r("3.30s"),Io=p(),us=a("td"),Co=r("x2.88"),Fo=p(),z=a("tr"),ds=a("td"),Ro=r("traced UNet"),Mo=p(),ms=a("td"),zo=r("3.21s"),Ho=p(),_s=a("td"),Vo=r("x2.96"),Go=p(),H=a("tr"),vs=a("td"),Oo=r("memory efficient attention"),Bo=p(),ys=a("td"),Lo=r("2.63s"),Xo=p(),ws=a("td"),Yo=r("x3.61"),Da=p(),it=a("em"),Wo=r(`obtained on NVIDIA TITAN RTX by generating a single image of size 512x512 from
  the prompt "a photo of an astronaut riding a horse on mars" with 50 DDIM
  steps.`),Aa=p(),V=a("h2"),le=a("a"),bs=a("span"),u(Se.$$.fragment),Ko=p(),gs=a("span"),Qo=r("Enable cuDNN auto-tuner"),Pa=p(),qe=a("p"),Ne=a("a"),Jo=r("NVIDIA cuDNN"),Zo=r("\xA0supports many algorithms to compute a convolution. Autotuner runs a short benchmark and selects the kernel with the best performance on a given hardware for a given input size."),xa=p(),pe=a("p"),er=r("Since we\u2019re using "),$s=a("strong"),tr=r("convolutional networks"),sr=r(" (other types currently not supported), we can enable cuDNN autotuner before launching the inference by setting:"),Sa=p(),u(Ue.$$.fragment),qa=p(),G=a("h3"),fe=a("a"),Es=a("span"),u(Ie.$$.fragment),ar=p(),js=a("span"),nr=r("Use tf32 instead of fp32 (on Ampere and later CUDA devices)"),Na=p(),ce=a("p"),or=r("On Ampere and later CUDA devices matrix multiplications and convolutions can use the TensorFloat32 (TF32) mode for faster but slightly less accurate computations. By default PyTorch enables TF32 mode for convolutions but not matrix multiplications, and unless a network requires full float32 precision we recommend enabling this setting for matrix multiplications, too. It can significantly speed up computations with typically negligible loss of numerical accuracy. You can read more about it "),Ce=a("a"),rr=r("here"),ir=r(". All you need to do is to add this before your inference:"),Ua=p(),u(Fe.$$.fragment),Ia=p(),O=a("h2"),he=a("a"),ks=a("span"),u(Re.$$.fragment),lr=p(),Ts=a("span"),pr=r("Automatic mixed precision (AMP)"),Ca=p(),P=a("p"),fr=r("If you use a CUDA GPU, you can take advantage of "),Ds=a("code"),cr=r("torch.autocast"),hr=r(" to perform inference roughly twice as fast at the cost of slightly lower precision. All you need to do is put your inference call inside an "),As=a("code"),ur=r("autocast"),dr=r(" context manager. The following example shows how to do it using Stable Diffusion text-to-image generation as an example:"),Fa=p(),u(Me.$$.fragment),Ra=p(),ue=a("p"),mr=r("Despite the precision loss, in our experience the final image results look the same as the "),Ps=a("code"),_r=r("float32"),vr=r(" versions. Feel free to experiment and report back!"),Ma=p(),B=a("h2"),de=a("a"),xs=a("span"),u(ze.$$.fragment),yr=p(),Ss=a("span"),wr=r("Half precision weights"),za=p(),x=a("p"),br=r("To save more GPU memory and get even more speed, you can load and run the model weights directly in half precision. This involves loading the float16 version of the weights, which was saved to a branch named "),qs=a("code"),gr=r("fp16"),$r=r(", and telling PyTorch to use the "),Ns=a("code"),Er=r("float16"),jr=r(" type when loading them:"),Ha=p(),u(He.$$.fragment),Va=p(),L=a("h2"),me=a("a"),Us=a("span"),u(Ve.$$.fragment),kr=p(),Is=a("span"),Tr=r("Sliced attention for additional memory savings"),Ga=p(),lt=a("p"),Dr=r("For even additional memory savings, you can use a sliced version of attention that performs the computation in steps instead of all at once."),Oa=p(),u(_e.$$.fragment),Ba=p(),ve=a("p"),Ar=r("To perform the attention computation sequentially over each head, you only need to invoke "),pt=a("a"),Pr=r("enable_attention_slicing()"),xr=r(" in your pipeline before inference, like here:"),La=p(),u(Ge.$$.fragment),Xa=p(),ft=a("p"),Sr=r("There\u2019s a small performance penalty of about 10% slower inference times, but this method allows you to use Stable Diffusion in as little as 3.2 GB of VRAM!"),Ya=p(),X=a("h2"),ye=a("a"),Cs=a("span"),u(Oe.$$.fragment),qr=p(),Fs=a("span"),Nr=r("Sliced VAE decode for larger batches"),Wa=p(),ct=a("p"),Ur=r("To decode large batches of images with limited VRAM, or to enable batches with 32 images or more, you can use sliced VAE decode that decodes the batch latents one image at a time."),Ka=p(),S=a("p"),Ir=r("You likely want to couple this with "),ht=a("a"),Cr=r("enable_attention_slicing()"),Fr=r(" or "),ut=a("a"),Rr=r("enable_xformers_memory_efficient_attention()"),Mr=r(" to further minimize memory use."),Qa=p(),we=a("p"),zr=r("To perform the VAE decode one image at a time, invoke "),dt=a("a"),Hr=r("enable_vae_slicing()"),Vr=r(" in your pipeline before inference. For example:"),Ja=p(),u(Be.$$.fragment),Za=p(),mt=a("p"),Gr=r("You may see a small performance boost in VAE decode on multi-image batches. There should be no performance impact on single-image batches."),en=p(),Y=a("h2"),be=a("a"),Rs=a("span"),u(Le.$$.fragment),Or=p(),Ms=a("span"),Br=r("Offloading to CPU with accelerate for memory savings"),tn=p(),_t=a("p"),Lr=r("For additional memory savings, you can offload the weights to CPU and load them to GPU when performing the forward pass."),sn=p(),ge=a("p"),Xr=r("To perform CPU offloading, all you have to do is invoke "),zs=a("code"),Yr=r("enable_sequential_cpu_offload()"),Wr=r(":"),an=p(),u(Xe.$$.fragment),nn=p(),vt=a("p"),Kr=r("And you can get the memory consumption to < 2GB."),on=p(),yt=a("p"),Qr=r("If is also possible to chain it with attention slicing for minimal memory consumption, running it in as little as < 800mb of GPU vRAM:"),rn=p(),u(Ye.$$.fragment),ln=p(),W=a("h2"),$e=a("a"),Hs=a("span"),u(We.$$.fragment),Jr=p(),Vs=a("span"),Zr=r("Using Channels Last memory format"),pn=p(),wt=a("p"),ei=r("Channels last memory format is an alternative way of ordering NCHW tensors in memory preserving dimensions ordering. Channels last tensors ordered in such a way that channels become the densest dimension (aka storing images pixel-per-pixel). Since not all operators currently support channels last format it may result in a worst performance, so it\u2019s better to try it and see if it works for your model."),fn=p(),bt=a("p"),ti=r("For example, in order to set the UNet model in our pipeline to use channels last format, we can use the following:"),cn=p(),u(Ke.$$.fragment),hn=p(),K=a("h2"),Ee=a("a"),Gs=a("span"),u(Qe.$$.fragment),si=p(),Os=a("span"),ai=r("Tracing"),un=p(),je=a("p"),ni=r("Tracing runs an example input tensor through your model, and captures the operations that are invoked as that input makes its way through the model\u2019s layers so that an executable or "),Bs=a("code"),oi=r("ScriptFunction"),ri=r(" is returned that will be optimized using just-in-time compilation."),dn=p(),gt=a("p"),ii=r("To trace our UNet model, we can use the following:"),mn=p(),u(Je.$$.fragment),_n=p(),ke=a("p"),li=r("Then we can replace the "),Ls=a("code"),pi=r("unet"),fi=r(" attribute of the pipeline with the traced model like the following"),vn=p(),u(Ze.$$.fragment),yn=p(),Q=a("h2"),Te=a("a"),Xs=a("span"),u(et.$$.fragment),ci=p(),Ys=a("span"),hi=r("Memory Efficient Attention"),wn=p(),q=a("p"),ui=r("Recent work on optimizing the bandwitdh in the attention block has generated huge speed ups and gains in GPU memory usage. The most recent being Flash Attention from @tridao: "),tt=a("a"),di=r("code"),mi=r(", "),st=a("a"),_i=r("paper"),vi=r("."),bn=p(),$t=a("p"),yi=r("Here are the speedups we obtain on a few Nvidia GPUs when running the inference at 512x512 with a batch size of 1 (one prompt):"),gn=p(),De=a("table"),Ws=a("thead"),J=a("tr"),Ks=a("th"),wi=r("GPU"),bi=p(),Qs=a("th"),gi=r("Base Attention FP16"),$i=p(),Js=a("th"),Ei=r("Memory Efficient Attention FP16"),ji=p(),b=a("tbody"),Z=a("tr"),Zs=a("td"),ki=r("NVIDIA Tesla T4"),Ti=p(),ea=a("td"),Di=r("3.5it/s"),Ai=p(),ta=a("td"),Pi=r("5.5it/s"),xi=p(),ee=a("tr"),sa=a("td"),Si=r("NVIDIA 3060 RTX"),qi=p(),aa=a("td"),Ni=r("4.6it/s"),Ui=p(),na=a("td"),Ii=r("7.8it/s"),Ci=p(),te=a("tr"),oa=a("td"),Fi=r("NVIDIA A10G"),Ri=p(),ra=a("td"),Mi=r("8.88it/s"),zi=p(),ia=a("td"),Hi=r("15.6it/s"),Vi=p(),se=a("tr"),la=a("td"),Gi=r("NVIDIA RTX A6000"),Oi=p(),pa=a("td"),Bi=r("11.7it/s"),Li=p(),fa=a("td"),Xi=r("21.09it/s"),Yi=p(),ae=a("tr"),ca=a("td"),Wi=r("NVIDIA TITAN RTX"),Ki=p(),ha=a("td"),Qi=r("12.51it/s"),Ji=p(),ua=a("td"),Zi=r("18.22it/s"),el=p(),ne=a("tr"),da=a("td"),tl=r("A100-SXM4-40GB"),sl=p(),ma=a("td"),al=r("18.6it/s"),nl=p(),_a=a("td"),ol=r("29.it/s"),rl=p(),oe=a("tr"),va=a("td"),il=r("A100-SXM-80GB"),ll=p(),ya=a("td"),pl=r("18.7it/s"),fl=p(),wa=a("td"),cl=r("29.5it/s"),$n=p(),Et=a("p"),hl=r("To leverage it just make sure you have:"),En=p(),N=a("ul"),ba=a("li"),ul=r("PyTorch > 1.12"),dl=p(),ga=a("li"),ml=r("Cuda available"),_l=p(),jt=a("li"),kt=a("a"),vl=r("Installed the xformers library"),yl=r("."),jn=p(),u(at.$$.fragment),this.h()},l(e){const l=Cf('[data-svelte="svelte-1phssyn"]',document.head);g=n(l,"META",{name:!0,content:!0}),l.forEach(s),A=f(e),T=n(e,"H1",{class:!0});var nt=o(T);re=n(nt,"A",{id:!0,class:!0,href:!0});var gl=o(re);Xt=n(gl,"SPAN",{});var $l=o(Xt);d(Pe.$$.fragment,$l),$l.forEach(s),gl.forEach(s),Ln=f(nt),Yt=n(nt,"SPAN",{});var El=o(Yt);Xn=i(El,"Memory and speed"),El.forEach(s),nt.forEach(s),Ea=f(e),j=n(e,"P",{});var Ae=o(j);Yn=i(Ae,"We present some techniques and ideas to optimize \u{1F917} Diffusers "),Wt=n(Ae,"EM",{});var jl=o(Wt);Wn=i(jl,"inference"),jl.forEach(s),Kn=i(Ae," for memory or speed. As a general rule, we recommend the use of "),xe=n(Ae,"A",{href:!0,rel:!0});var kl=o(xe);Qn=i(kl,"xFormers"),kl.forEach(s),Jn=i(Ae," for memory efficient attention, please see the recommended "),ot=n(Ae,"A",{href:!0});var Tl=o(ot);Zn=i(Tl,"installation instructions"),Tl.forEach(s),eo=i(Ae,"."),Ae.forEach(s),ja=f(e),rt=n(e,"P",{});var Dl=o(rt);to=i(Dl,"We\u2019ll discuss how the following settings impact performance and memory."),Dl.forEach(s),ka=f(e),ie=n(e,"TABLE",{});var Tn=o(ie);Kt=n(Tn,"THEAD",{});var Al=o(Kt);U=n(Al,"TR",{});var Tt=o(U);Ta=n(Tt,"TH",{}),o(Ta).forEach(s),so=f(Tt),Qt=n(Tt,"TH",{});var Pl=o(Qt);ao=i(Pl,"Latency"),Pl.forEach(s),no=f(Tt),Jt=n(Tt,"TH",{});var xl=o(Jt);oo=i(xl,"Speedup"),xl.forEach(s),Tt.forEach(s),Al.forEach(s),ro=f(Tn),w=n(Tn,"TBODY",{});var $=o(w);I=n($,"TR",{});var Dt=o(I);Zt=n(Dt,"TD",{});var Sl=o(Zt);io=i(Sl,"original"),Sl.forEach(s),lo=f(Dt),es=n(Dt,"TD",{});var ql=o(es);po=i(ql,"9.50s"),ql.forEach(s),fo=f(Dt),ts=n(Dt,"TD",{});var Nl=o(ts);co=i(Nl,"x1"),Nl.forEach(s),Dt.forEach(s),ho=f($),C=n($,"TR",{});var At=o(C);ss=n(At,"TD",{});var Ul=o(ss);uo=i(Ul,"cuDNN auto-tuner"),Ul.forEach(s),mo=f(At),as=n(At,"TD",{});var Il=o(as);_o=i(Il,"9.37s"),Il.forEach(s),vo=f(At),ns=n(At,"TD",{});var Cl=o(ns);yo=i(Cl,"x1.01"),Cl.forEach(s),At.forEach(s),wo=f($),F=n($,"TR",{});var Pt=o(F);os=n(Pt,"TD",{});var Fl=o(os);bo=i(Fl,"autocast (fp16)"),Fl.forEach(s),go=f(Pt),rs=n(Pt,"TD",{});var Rl=o(rs);$o=i(Rl,"5.47s"),Rl.forEach(s),Eo=f(Pt),is=n(Pt,"TD",{});var Ml=o(is);jo=i(Ml,"x1.74"),Ml.forEach(s),Pt.forEach(s),ko=f($),R=n($,"TR",{});var xt=o(R);ls=n(xt,"TD",{});var zl=o(ls);To=i(zl,"fp16"),zl.forEach(s),Do=f(xt),ps=n(xt,"TD",{});var Hl=o(ps);Ao=i(Hl,"3.61s"),Hl.forEach(s),Po=f(xt),fs=n(xt,"TD",{});var Vl=o(fs);xo=i(Vl,"x2.63"),Vl.forEach(s),xt.forEach(s),So=f($),M=n($,"TR",{});var St=o(M);cs=n(St,"TD",{});var Gl=o(cs);qo=i(Gl,"channels last"),Gl.forEach(s),No=f(St),hs=n(St,"TD",{});var Ol=o(hs);Uo=i(Ol,"3.30s"),Ol.forEach(s),Io=f(St),us=n(St,"TD",{});var Bl=o(us);Co=i(Bl,"x2.88"),Bl.forEach(s),St.forEach(s),Fo=f($),z=n($,"TR",{});var qt=o(z);ds=n(qt,"TD",{});var Ll=o(ds);Ro=i(Ll,"traced UNet"),Ll.forEach(s),Mo=f(qt),ms=n(qt,"TD",{});var Xl=o(ms);zo=i(Xl,"3.21s"),Xl.forEach(s),Ho=f(qt),_s=n(qt,"TD",{});var Yl=o(_s);Vo=i(Yl,"x2.96"),Yl.forEach(s),qt.forEach(s),Go=f($),H=n($,"TR",{});var Nt=o(H);vs=n(Nt,"TD",{});var Wl=o(vs);Oo=i(Wl,"memory efficient attention"),Wl.forEach(s),Bo=f(Nt),ys=n(Nt,"TD",{});var Kl=o(ys);Lo=i(Kl,"2.63s"),Kl.forEach(s),Xo=f(Nt),ws=n(Nt,"TD",{});var Ql=o(ws);Yo=i(Ql,"x3.61"),Ql.forEach(s),Nt.forEach(s),$.forEach(s),Tn.forEach(s),Da=f(e),it=n(e,"EM",{});var Jl=o(it);Wo=i(Jl,`obtained on NVIDIA TITAN RTX by generating a single image of size 512x512 from
  the prompt "a photo of an astronaut riding a horse on mars" with 50 DDIM
  steps.`),Jl.forEach(s),Aa=f(e),V=n(e,"H2",{class:!0});var Dn=o(V);le=n(Dn,"A",{id:!0,class:!0,href:!0});var Zl=o(le);bs=n(Zl,"SPAN",{});var ep=o(bs);d(Se.$$.fragment,ep),ep.forEach(s),Zl.forEach(s),Ko=f(Dn),gs=n(Dn,"SPAN",{});var tp=o(gs);Qo=i(tp,"Enable cuDNN auto-tuner"),tp.forEach(s),Dn.forEach(s),Pa=f(e),qe=n(e,"P",{});var wl=o(qe);Ne=n(wl,"A",{href:!0,rel:!0});var sp=o(Ne);Jo=i(sp,"NVIDIA cuDNN"),sp.forEach(s),Zo=i(wl,"\xA0supports many algorithms to compute a convolution. Autotuner runs a short benchmark and selects the kernel with the best performance on a given hardware for a given input size."),wl.forEach(s),xa=f(e),pe=n(e,"P",{});var An=o(pe);er=i(An,"Since we\u2019re using "),$s=n(An,"STRONG",{});var ap=o($s);tr=i(ap,"convolutional networks"),ap.forEach(s),sr=i(An," (other types currently not supported), we can enable cuDNN autotuner before launching the inference by setting:"),An.forEach(s),Sa=f(e),d(Ue.$$.fragment,e),qa=f(e),G=n(e,"H3",{class:!0});var Pn=o(G);fe=n(Pn,"A",{id:!0,class:!0,href:!0});var np=o(fe);Es=n(np,"SPAN",{});var op=o(Es);d(Ie.$$.fragment,op),op.forEach(s),np.forEach(s),ar=f(Pn),js=n(Pn,"SPAN",{});var rp=o(js);nr=i(rp,"Use tf32 instead of fp32 (on Ampere and later CUDA devices)"),rp.forEach(s),Pn.forEach(s),Na=f(e),ce=n(e,"P",{});var xn=o(ce);or=i(xn,"On Ampere and later CUDA devices matrix multiplications and convolutions can use the TensorFloat32 (TF32) mode for faster but slightly less accurate computations. By default PyTorch enables TF32 mode for convolutions but not matrix multiplications, and unless a network requires full float32 precision we recommend enabling this setting for matrix multiplications, too. It can significantly speed up computations with typically negligible loss of numerical accuracy. You can read more about it "),Ce=n(xn,"A",{href:!0,rel:!0});var ip=o(Ce);rr=i(ip,"here"),ip.forEach(s),ir=i(xn,". All you need to do is to add this before your inference:"),xn.forEach(s),Ua=f(e),d(Fe.$$.fragment,e),Ia=f(e),O=n(e,"H2",{class:!0});var Sn=o(O);he=n(Sn,"A",{id:!0,class:!0,href:!0});var lp=o(he);ks=n(lp,"SPAN",{});var pp=o(ks);d(Re.$$.fragment,pp),pp.forEach(s),lp.forEach(s),lr=f(Sn),Ts=n(Sn,"SPAN",{});var fp=o(Ts);pr=i(fp,"Automatic mixed precision (AMP)"),fp.forEach(s),Sn.forEach(s),Ca=f(e),P=n(e,"P",{});var Ut=o(P);fr=i(Ut,"If you use a CUDA GPU, you can take advantage of "),Ds=n(Ut,"CODE",{});var cp=o(Ds);cr=i(cp,"torch.autocast"),cp.forEach(s),hr=i(Ut," to perform inference roughly twice as fast at the cost of slightly lower precision. All you need to do is put your inference call inside an "),As=n(Ut,"CODE",{});var hp=o(As);ur=i(hp,"autocast"),hp.forEach(s),dr=i(Ut," context manager. The following example shows how to do it using Stable Diffusion text-to-image generation as an example:"),Ut.forEach(s),Fa=f(e),d(Me.$$.fragment,e),Ra=f(e),ue=n(e,"P",{});var qn=o(ue);mr=i(qn,"Despite the precision loss, in our experience the final image results look the same as the "),Ps=n(qn,"CODE",{});var up=o(Ps);_r=i(up,"float32"),up.forEach(s),vr=i(qn," versions. Feel free to experiment and report back!"),qn.forEach(s),Ma=f(e),B=n(e,"H2",{class:!0});var Nn=o(B);de=n(Nn,"A",{id:!0,class:!0,href:!0});var dp=o(de);xs=n(dp,"SPAN",{});var mp=o(xs);d(ze.$$.fragment,mp),mp.forEach(s),dp.forEach(s),yr=f(Nn),Ss=n(Nn,"SPAN",{});var _p=o(Ss);wr=i(_p,"Half precision weights"),_p.forEach(s),Nn.forEach(s),za=f(e),x=n(e,"P",{});var It=o(x);br=i(It,"To save more GPU memory and get even more speed, you can load and run the model weights directly in half precision. This involves loading the float16 version of the weights, which was saved to a branch named "),qs=n(It,"CODE",{});var vp=o(qs);gr=i(vp,"fp16"),vp.forEach(s),$r=i(It,", and telling PyTorch to use the "),Ns=n(It,"CODE",{});var yp=o(Ns);Er=i(yp,"float16"),yp.forEach(s),jr=i(It," type when loading them:"),It.forEach(s),Ha=f(e),d(He.$$.fragment,e),Va=f(e),L=n(e,"H2",{class:!0});var Un=o(L);me=n(Un,"A",{id:!0,class:!0,href:!0});var wp=o(me);Us=n(wp,"SPAN",{});var bp=o(Us);d(Ve.$$.fragment,bp),bp.forEach(s),wp.forEach(s),kr=f(Un),Is=n(Un,"SPAN",{});var gp=o(Is);Tr=i(gp,"Sliced attention for additional memory savings"),gp.forEach(s),Un.forEach(s),Ga=f(e),lt=n(e,"P",{});var $p=o(lt);Dr=i($p,"For even additional memory savings, you can use a sliced version of attention that performs the computation in steps instead of all at once."),$p.forEach(s),Oa=f(e),d(_e.$$.fragment,e),Ba=f(e),ve=n(e,"P",{});var In=o(ve);Ar=i(In,"To perform the attention computation sequentially over each head, you only need to invoke "),pt=n(In,"A",{href:!0});var Ep=o(pt);Pr=i(Ep,"enable_attention_slicing()"),Ep.forEach(s),xr=i(In," in your pipeline before inference, like here:"),In.forEach(s),La=f(e),d(Ge.$$.fragment,e),Xa=f(e),ft=n(e,"P",{});var jp=o(ft);Sr=i(jp,"There\u2019s a small performance penalty of about 10% slower inference times, but this method allows you to use Stable Diffusion in as little as 3.2 GB of VRAM!"),jp.forEach(s),Ya=f(e),X=n(e,"H2",{class:!0});var Cn=o(X);ye=n(Cn,"A",{id:!0,class:!0,href:!0});var kp=o(ye);Cs=n(kp,"SPAN",{});var Tp=o(Cs);d(Oe.$$.fragment,Tp),Tp.forEach(s),kp.forEach(s),qr=f(Cn),Fs=n(Cn,"SPAN",{});var Dp=o(Fs);Nr=i(Dp,"Sliced VAE decode for larger batches"),Dp.forEach(s),Cn.forEach(s),Wa=f(e),ct=n(e,"P",{});var Ap=o(ct);Ur=i(Ap,"To decode large batches of images with limited VRAM, or to enable batches with 32 images or more, you can use sliced VAE decode that decodes the batch latents one image at a time."),Ap.forEach(s),Ka=f(e),S=n(e,"P",{});var Ct=o(S);Ir=i(Ct,"You likely want to couple this with "),ht=n(Ct,"A",{href:!0});var Pp=o(ht);Cr=i(Pp,"enable_attention_slicing()"),Pp.forEach(s),Fr=i(Ct," or "),ut=n(Ct,"A",{href:!0});var xp=o(ut);Rr=i(xp,"enable_xformers_memory_efficient_attention()"),xp.forEach(s),Mr=i(Ct," to further minimize memory use."),Ct.forEach(s),Qa=f(e),we=n(e,"P",{});var Fn=o(we);zr=i(Fn,"To perform the VAE decode one image at a time, invoke "),dt=n(Fn,"A",{href:!0});var Sp=o(dt);Hr=i(Sp,"enable_vae_slicing()"),Sp.forEach(s),Vr=i(Fn," in your pipeline before inference. For example:"),Fn.forEach(s),Ja=f(e),d(Be.$$.fragment,e),Za=f(e),mt=n(e,"P",{});var qp=o(mt);Gr=i(qp,"You may see a small performance boost in VAE decode on multi-image batches. There should be no performance impact on single-image batches."),qp.forEach(s),en=f(e),Y=n(e,"H2",{class:!0});var Rn=o(Y);be=n(Rn,"A",{id:!0,class:!0,href:!0});var Np=o(be);Rs=n(Np,"SPAN",{});var Up=o(Rs);d(Le.$$.fragment,Up),Up.forEach(s),Np.forEach(s),Or=f(Rn),Ms=n(Rn,"SPAN",{});var Ip=o(Ms);Br=i(Ip,"Offloading to CPU with accelerate for memory savings"),Ip.forEach(s),Rn.forEach(s),tn=f(e),_t=n(e,"P",{});var Cp=o(_t);Lr=i(Cp,"For additional memory savings, you can offload the weights to CPU and load them to GPU when performing the forward pass."),Cp.forEach(s),sn=f(e),ge=n(e,"P",{});var Mn=o(ge);Xr=i(Mn,"To perform CPU offloading, all you have to do is invoke "),zs=n(Mn,"CODE",{});var Fp=o(zs);Yr=i(Fp,"enable_sequential_cpu_offload()"),Fp.forEach(s),Wr=i(Mn,":"),Mn.forEach(s),an=f(e),d(Xe.$$.fragment,e),nn=f(e),vt=n(e,"P",{});var Rp=o(vt);Kr=i(Rp,"And you can get the memory consumption to < 2GB."),Rp.forEach(s),on=f(e),yt=n(e,"P",{});var Mp=o(yt);Qr=i(Mp,"If is also possible to chain it with attention slicing for minimal memory consumption, running it in as little as < 800mb of GPU vRAM:"),Mp.forEach(s),rn=f(e),d(Ye.$$.fragment,e),ln=f(e),W=n(e,"H2",{class:!0});var zn=o(W);$e=n(zn,"A",{id:!0,class:!0,href:!0});var zp=o($e);Hs=n(zp,"SPAN",{});var Hp=o(Hs);d(We.$$.fragment,Hp),Hp.forEach(s),zp.forEach(s),Jr=f(zn),Vs=n(zn,"SPAN",{});var Vp=o(Vs);Zr=i(Vp,"Using Channels Last memory format"),Vp.forEach(s),zn.forEach(s),pn=f(e),wt=n(e,"P",{});var Gp=o(wt);ei=i(Gp,"Channels last memory format is an alternative way of ordering NCHW tensors in memory preserving dimensions ordering. Channels last tensors ordered in such a way that channels become the densest dimension (aka storing images pixel-per-pixel). Since not all operators currently support channels last format it may result in a worst performance, so it\u2019s better to try it and see if it works for your model."),Gp.forEach(s),fn=f(e),bt=n(e,"P",{});var Op=o(bt);ti=i(Op,"For example, in order to set the UNet model in our pipeline to use channels last format, we can use the following:"),Op.forEach(s),cn=f(e),d(Ke.$$.fragment,e),hn=f(e),K=n(e,"H2",{class:!0});var Hn=o(K);Ee=n(Hn,"A",{id:!0,class:!0,href:!0});var Bp=o(Ee);Gs=n(Bp,"SPAN",{});var Lp=o(Gs);d(Qe.$$.fragment,Lp),Lp.forEach(s),Bp.forEach(s),si=f(Hn),Os=n(Hn,"SPAN",{});var Xp=o(Os);ai=i(Xp,"Tracing"),Xp.forEach(s),Hn.forEach(s),un=f(e),je=n(e,"P",{});var Vn=o(je);ni=i(Vn,"Tracing runs an example input tensor through your model, and captures the operations that are invoked as that input makes its way through the model\u2019s layers so that an executable or "),Bs=n(Vn,"CODE",{});var Yp=o(Bs);oi=i(Yp,"ScriptFunction"),Yp.forEach(s),ri=i(Vn," is returned that will be optimized using just-in-time compilation."),Vn.forEach(s),dn=f(e),gt=n(e,"P",{});var Wp=o(gt);ii=i(Wp,"To trace our UNet model, we can use the following:"),Wp.forEach(s),mn=f(e),d(Je.$$.fragment,e),_n=f(e),ke=n(e,"P",{});var Gn=o(ke);li=i(Gn,"Then we can replace the "),Ls=n(Gn,"CODE",{});var Kp=o(Ls);pi=i(Kp,"unet"),Kp.forEach(s),fi=i(Gn," attribute of the pipeline with the traced model like the following"),Gn.forEach(s),vn=f(e),d(Ze.$$.fragment,e),yn=f(e),Q=n(e,"H2",{class:!0});var On=o(Q);Te=n(On,"A",{id:!0,class:!0,href:!0});var Qp=o(Te);Xs=n(Qp,"SPAN",{});var Jp=o(Xs);d(et.$$.fragment,Jp),Jp.forEach(s),Qp.forEach(s),ci=f(On),Ys=n(On,"SPAN",{});var Zp=o(Ys);hi=i(Zp,"Memory Efficient Attention"),Zp.forEach(s),On.forEach(s),wn=f(e),q=n(e,"P",{});var Ft=o(q);ui=i(Ft,"Recent work on optimizing the bandwitdh in the attention block has generated huge speed ups and gains in GPU memory usage. The most recent being Flash Attention from @tridao: "),tt=n(Ft,"A",{href:!0,rel:!0});var ef=o(tt);di=i(ef,"code"),ef.forEach(s),mi=i(Ft,", "),st=n(Ft,"A",{href:!0,rel:!0});var tf=o(st);_i=i(tf,"paper"),tf.forEach(s),vi=i(Ft,"."),Ft.forEach(s),bn=f(e),$t=n(e,"P",{});var sf=o($t);yi=i(sf,"Here are the speedups we obtain on a few Nvidia GPUs when running the inference at 512x512 with a batch size of 1 (one prompt):"),sf.forEach(s),gn=f(e),De=n(e,"TABLE",{});var Bn=o(De);Ws=n(Bn,"THEAD",{});var af=o(Ws);J=n(af,"TR",{});var Rt=o(J);Ks=n(Rt,"TH",{});var nf=o(Ks);wi=i(nf,"GPU"),nf.forEach(s),bi=f(Rt),Qs=n(Rt,"TH",{});var of=o(Qs);gi=i(of,"Base Attention FP16"),of.forEach(s),$i=f(Rt),Js=n(Rt,"TH",{});var rf=o(Js);Ei=i(rf,"Memory Efficient Attention FP16"),rf.forEach(s),Rt.forEach(s),af.forEach(s),ji=f(Bn),b=n(Bn,"TBODY",{});var E=o(b);Z=n(E,"TR",{});var Mt=o(Z);Zs=n(Mt,"TD",{});var lf=o(Zs);ki=i(lf,"NVIDIA Tesla T4"),lf.forEach(s),Ti=f(Mt),ea=n(Mt,"TD",{});var pf=o(ea);Di=i(pf,"3.5it/s"),pf.forEach(s),Ai=f(Mt),ta=n(Mt,"TD",{});var ff=o(ta);Pi=i(ff,"5.5it/s"),ff.forEach(s),Mt.forEach(s),xi=f(E),ee=n(E,"TR",{});var zt=o(ee);sa=n(zt,"TD",{});var cf=o(sa);Si=i(cf,"NVIDIA 3060 RTX"),cf.forEach(s),qi=f(zt),aa=n(zt,"TD",{});var hf=o(aa);Ni=i(hf,"4.6it/s"),hf.forEach(s),Ui=f(zt),na=n(zt,"TD",{});var uf=o(na);Ii=i(uf,"7.8it/s"),uf.forEach(s),zt.forEach(s),Ci=f(E),te=n(E,"TR",{});var Ht=o(te);oa=n(Ht,"TD",{});var df=o(oa);Fi=i(df,"NVIDIA A10G"),df.forEach(s),Ri=f(Ht),ra=n(Ht,"TD",{});var mf=o(ra);Mi=i(mf,"8.88it/s"),mf.forEach(s),zi=f(Ht),ia=n(Ht,"TD",{});var _f=o(ia);Hi=i(_f,"15.6it/s"),_f.forEach(s),Ht.forEach(s),Vi=f(E),se=n(E,"TR",{});var Vt=o(se);la=n(Vt,"TD",{});var vf=o(la);Gi=i(vf,"NVIDIA RTX A6000"),vf.forEach(s),Oi=f(Vt),pa=n(Vt,"TD",{});var yf=o(pa);Bi=i(yf,"11.7it/s"),yf.forEach(s),Li=f(Vt),fa=n(Vt,"TD",{});var wf=o(fa);Xi=i(wf,"21.09it/s"),wf.forEach(s),Vt.forEach(s),Yi=f(E),ae=n(E,"TR",{});var Gt=o(ae);ca=n(Gt,"TD",{});var bf=o(ca);Wi=i(bf,"NVIDIA TITAN RTX"),bf.forEach(s),Ki=f(Gt),ha=n(Gt,"TD",{});var gf=o(ha);Qi=i(gf,"12.51it/s"),gf.forEach(s),Ji=f(Gt),ua=n(Gt,"TD",{});var $f=o(ua);Zi=i($f,"18.22it/s"),$f.forEach(s),Gt.forEach(s),el=f(E),ne=n(E,"TR",{});var Ot=o(ne);da=n(Ot,"TD",{});var Ef=o(da);tl=i(Ef,"A100-SXM4-40GB"),Ef.forEach(s),sl=f(Ot),ma=n(Ot,"TD",{});var jf=o(ma);al=i(jf,"18.6it/s"),jf.forEach(s),nl=f(Ot),_a=n(Ot,"TD",{});var kf=o(_a);ol=i(kf,"29.it/s"),kf.forEach(s),Ot.forEach(s),rl=f(E),oe=n(E,"TR",{});var Bt=o(oe);va=n(Bt,"TD",{});var Tf=o(va);il=i(Tf,"A100-SXM-80GB"),Tf.forEach(s),ll=f(Bt),ya=n(Bt,"TD",{});var Df=o(ya);pl=i(Df,"18.7it/s"),Df.forEach(s),fl=f(Bt),wa=n(Bt,"TD",{});var Af=o(wa);cl=i(Af,"29.5it/s"),Af.forEach(s),Bt.forEach(s),E.forEach(s),Bn.forEach(s),$n=f(e),Et=n(e,"P",{});var Pf=o(Et);hl=i(Pf,"To leverage it just make sure you have:"),Pf.forEach(s),En=f(e),N=n(e,"UL",{});var Lt=o(N);ba=n(Lt,"LI",{});var xf=o(ba);ul=i(xf,"PyTorch > 1.12"),xf.forEach(s),dl=f(Lt),ga=n(Lt,"LI",{});var Sf=o(ga);ml=i(Sf,"Cuda available"),Sf.forEach(s),_l=f(Lt),jt=n(Lt,"LI",{});var bl=o(jt);kt=n(bl,"A",{href:!0});var qf=o(kt);vl=i(qf,"Installed the xformers library"),qf.forEach(s),yl=i(bl,"."),bl.forEach(s),Lt.forEach(s),jn=f(e),d(at.$$.fragment,e),this.h()},h(){h(g,"name","hf:doc:metadata"),h(g,"content",JSON.stringify(Hf)),h(re,"id","memory-and-speed"),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","#memory-and-speed"),h(T,"class","relative group"),h(xe,"href","https://github.com/facebookresearch/xformers"),h(xe,"rel","nofollow"),h(ot,"href","xformers"),h(le,"id","enable-cudnn-autotuner"),h(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"),h(le,"href","#enable-cudnn-autotuner"),h(V,"class","relative group"),h(Ne,"href","https://developer.nvidia.com/cudnn"),h(Ne,"rel","nofollow"),h(fe,"id","use-tf32-instead-of-fp32-on-ampere-and-later-cuda-devices"),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","#use-tf32-instead-of-fp32-on-ampere-and-later-cuda-devices"),h(G,"class","relative group"),h(Ce,"href","https://huggingface.co/docs/transformers/v4.18.0/en/performance#tf32"),h(Ce,"rel","nofollow"),h(he,"id","automatic-mixed-precision-amp"),h(he,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(he,"href","#automatic-mixed-precision-amp"),h(O,"class","relative group"),h(de,"id","half-precision-weights"),h(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"),h(de,"href","#half-precision-weights"),h(B,"class","relative group"),h(me,"id","sliced-attention-for-additional-memory-savings"),h(me,"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(me,"href","#sliced-attention-for-additional-memory-savings"),h(L,"class","relative group"),h(pt,"href","/docs/diffusers/v0.11.0/en/api/pipelines/stable_diffusion#diffusers.StableDiffusionUpscalePipeline.enable_attention_slicing"),h(ye,"id","sliced-vae-decode-for-larger-batches"),h(ye,"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(ye,"href","#sliced-vae-decode-for-larger-batches"),h(X,"class","relative group"),h(ht,"href","/docs/diffusers/v0.11.0/en/api/pipelines/stable_diffusion#diffusers.StableDiffusionUpscalePipeline.enable_attention_slicing"),h(ut,"href","/docs/diffusers/v0.11.0/en/api/pipelines/stable_diffusion#diffusers.StableDiffusionUpscalePipeline.enable_xformers_memory_efficient_attention"),h(dt,"href","/docs/diffusers/v0.11.0/en/api/pipelines/stable_diffusion#diffusers.StableDiffusionPipeline.enable_vae_slicing"),h(be,"id","offloading-to-cpu-with-accelerate-for-memory-savings"),h(be,"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(be,"href","#offloading-to-cpu-with-accelerate-for-memory-savings"),h(Y,"class","relative group"),h($e,"id","using-channels-last-memory-format"),h($e,"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($e,"href","#using-channels-last-memory-format"),h(W,"class","relative group"),h(Ee,"id","tracing"),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","#tracing"),h(K,"class","relative group"),h(Te,"id","memory-efficient-attention"),h(Te,"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(Te,"href","#memory-efficient-attention"),h(Q,"class","relative group"),h(tt,"href","https://github.com/HazyResearch/flash-attention"),h(tt,"rel","nofollow"),h(st,"href","https://arxiv.org/pdf/2205.14135.pdf"),h(st,"rel","nofollow"),h(kt,"href","xformers")},m(e,l){t(document.head,g),c(e,A,l),c(e,T,l),t(T,re),t(re,Xt),m(Pe,Xt,null),t(T,Ln),t(T,Yt),t(Yt,Xn),c(e,Ea,l),c(e,j,l),t(j,Yn),t(j,Wt),t(Wt,Wn),t(j,Kn),t(j,xe),t(xe,Qn),t(j,Jn),t(j,ot),t(ot,Zn),t(j,eo),c(e,ja,l),c(e,rt,l),t(rt,to),c(e,ka,l),c(e,ie,l),t(ie,Kt),t(Kt,U),t(U,Ta),t(U,so),t(U,Qt),t(Qt,ao),t(U,no),t(U,Jt),t(Jt,oo),t(ie,ro),t(ie,w),t(w,I),t(I,Zt),t(Zt,io),t(I,lo),t(I,es),t(es,po),t(I,fo),t(I,ts),t(ts,co),t(w,ho),t(w,C),t(C,ss),t(ss,uo),t(C,mo),t(C,as),t(as,_o),t(C,vo),t(C,ns),t(ns,yo),t(w,wo),t(w,F),t(F,os),t(os,bo),t(F,go),t(F,rs),t(rs,$o),t(F,Eo),t(F,is),t(is,jo),t(w,ko),t(w,R),t(R,ls),t(ls,To),t(R,Do),t(R,ps),t(ps,Ao),t(R,Po),t(R,fs),t(fs,xo),t(w,So),t(w,M),t(M,cs),t(cs,qo),t(M,No),t(M,hs),t(hs,Uo),t(M,Io),t(M,us),t(us,Co),t(w,Fo),t(w,z),t(z,ds),t(ds,Ro),t(z,Mo),t(z,ms),t(ms,zo),t(z,Ho),t(z,_s),t(_s,Vo),t(w,Go),t(w,H),t(H,vs),t(vs,Oo),t(H,Bo),t(H,ys),t(ys,Lo),t(H,Xo),t(H,ws),t(ws,Yo),c(e,Da,l),c(e,it,l),t(it,Wo),c(e,Aa,l),c(e,V,l),t(V,le),t(le,bs),m(Se,bs,null),t(V,Ko),t(V,gs),t(gs,Qo),c(e,Pa,l),c(e,qe,l),t(qe,Ne),t(Ne,Jo),t(qe,Zo),c(e,xa,l),c(e,pe,l),t(pe,er),t(pe,$s),t($s,tr),t(pe,sr),c(e,Sa,l),m(Ue,e,l),c(e,qa,l),c(e,G,l),t(G,fe),t(fe,Es),m(Ie,Es,null),t(G,ar),t(G,js),t(js,nr),c(e,Na,l),c(e,ce,l),t(ce,or),t(ce,Ce),t(Ce,rr),t(ce,ir),c(e,Ua,l),m(Fe,e,l),c(e,Ia,l),c(e,O,l),t(O,he),t(he,ks),m(Re,ks,null),t(O,lr),t(O,Ts),t(Ts,pr),c(e,Ca,l),c(e,P,l),t(P,fr),t(P,Ds),t(Ds,cr),t(P,hr),t(P,As),t(As,ur),t(P,dr),c(e,Fa,l),m(Me,e,l),c(e,Ra,l),c(e,ue,l),t(ue,mr),t(ue,Ps),t(Ps,_r),t(ue,vr),c(e,Ma,l),c(e,B,l),t(B,de),t(de,xs),m(ze,xs,null),t(B,yr),t(B,Ss),t(Ss,wr),c(e,za,l),c(e,x,l),t(x,br),t(x,qs),t(qs,gr),t(x,$r),t(x,Ns),t(Ns,Er),t(x,jr),c(e,Ha,l),m(He,e,l),c(e,Va,l),c(e,L,l),t(L,me),t(me,Us),m(Ve,Us,null),t(L,kr),t(L,Is),t(Is,Tr),c(e,Ga,l),c(e,lt,l),t(lt,Dr),c(e,Oa,l),m(_e,e,l),c(e,Ba,l),c(e,ve,l),t(ve,Ar),t(ve,pt),t(pt,Pr),t(ve,xr),c(e,La,l),m(Ge,e,l),c(e,Xa,l),c(e,ft,l),t(ft,Sr),c(e,Ya,l),c(e,X,l),t(X,ye),t(ye,Cs),m(Oe,Cs,null),t(X,qr),t(X,Fs),t(Fs,Nr),c(e,Wa,l),c(e,ct,l),t(ct,Ur),c(e,Ka,l),c(e,S,l),t(S,Ir),t(S,ht),t(ht,Cr),t(S,Fr),t(S,ut),t(ut,Rr),t(S,Mr),c(e,Qa,l),c(e,we,l),t(we,zr),t(we,dt),t(dt,Hr),t(we,Vr),c(e,Ja,l),m(Be,e,l),c(e,Za,l),c(e,mt,l),t(mt,Gr),c(e,en,l),c(e,Y,l),t(Y,be),t(be,Rs),m(Le,Rs,null),t(Y,Or),t(Y,Ms),t(Ms,Br),c(e,tn,l),c(e,_t,l),t(_t,Lr),c(e,sn,l),c(e,ge,l),t(ge,Xr),t(ge,zs),t(zs,Yr),t(ge,Wr),c(e,an,l),m(Xe,e,l),c(e,nn,l),c(e,vt,l),t(vt,Kr),c(e,on,l),c(e,yt,l),t(yt,Qr),c(e,rn,l),m(Ye,e,l),c(e,ln,l),c(e,W,l),t(W,$e),t($e,Hs),m(We,Hs,null),t(W,Jr),t(W,Vs),t(Vs,Zr),c(e,pn,l),c(e,wt,l),t(wt,ei),c(e,fn,l),c(e,bt,l),t(bt,ti),c(e,cn,l),m(Ke,e,l),c(e,hn,l),c(e,K,l),t(K,Ee),t(Ee,Gs),m(Qe,Gs,null),t(K,si),t(K,Os),t(Os,ai),c(e,un,l),c(e,je,l),t(je,ni),t(je,Bs),t(Bs,oi),t(je,ri),c(e,dn,l),c(e,gt,l),t(gt,ii),c(e,mn,l),m(Je,e,l),c(e,_n,l),c(e,ke,l),t(ke,li),t(ke,Ls),t(Ls,pi),t(ke,fi),c(e,vn,l),m(Ze,e,l),c(e,yn,l),c(e,Q,l),t(Q,Te),t(Te,Xs),m(et,Xs,null),t(Q,ci),t(Q,Ys),t(Ys,hi),c(e,wn,l),c(e,q,l),t(q,ui),t(q,tt),t(tt,di),t(q,mi),t(q,st),t(st,_i),t(q,vi),c(e,bn,l),c(e,$t,l),t($t,yi),c(e,gn,l),c(e,De,l),t(De,Ws),t(Ws,J),t(J,Ks),t(Ks,wi),t(J,bi),t(J,Qs),t(Qs,gi),t(J,$i),t(J,Js),t(Js,Ei),t(De,ji),t(De,b),t(b,Z),t(Z,Zs),t(Zs,ki),t(Z,Ti),t(Z,ea),t(ea,Di),t(Z,Ai),t(Z,ta),t(ta,Pi),t(b,xi),t(b,ee),t(ee,sa),t(sa,Si),t(ee,qi),t(ee,aa),t(aa,Ni),t(ee,Ui),t(ee,na),t(na,Ii),t(b,Ci),t(b,te),t(te,oa),t(oa,Fi),t(te,Ri),t(te,ra),t(ra,Mi),t(te,zi),t(te,ia),t(ia,Hi),t(b,Vi),t(b,se),t(se,la),t(la,Gi),t(se,Oi),t(se,pa),t(pa,Bi),t(se,Li),t(se,fa),t(fa,Xi),t(b,Yi),t(b,ae),t(ae,ca),t(ca,Wi),t(ae,Ki),t(ae,ha),t(ha,Qi),t(ae,Ji),t(ae,ua),t(ua,Zi),t(b,el),t(b,ne),t(ne,da),t(da,tl),t(ne,sl),t(ne,ma),t(ma,al),t(ne,nl),t(ne,_a),t(_a,ol),t(b,rl),t(b,oe),t(oe,va),t(va,il),t(oe,ll),t(oe,ya),t(ya,pl),t(oe,fl),t(oe,wa),t(wa,cl),c(e,$n,l),c(e,Et,l),t(Et,hl),c(e,En,l),c(e,N,l),t(N,ba),t(ba,ul),t(N,dl),t(N,ga),t(ga,ml),t(N,_l),t(N,jt),t(jt,kt),t(kt,vl),t(jt,yl),c(e,jn,l),m(at,e,l),kn=!0},p(e,[l]){const nt={};l&2&&(nt.$$scope={dirty:l,ctx:e}),_e.$set(nt)},i(e){kn||(_(Pe.$$.fragment,e),_(Se.$$.fragment,e),_(Ue.$$.fragment,e),_(Ie.$$.fragment,e),_(Fe.$$.fragment,e),_(Re.$$.fragment,e),_(Me.$$.fragment,e),_(ze.$$.fragment,e),_(He.$$.fragment,e),_(Ve.$$.fragment,e),_(_e.$$.fragment,e),_(Ge.$$.fragment,e),_(Oe.$$.fragment,e),_(Be.$$.fragment,e),_(Le.$$.fragment,e),_(Xe.$$.fragment,e),_(Ye.$$.fragment,e),_(We.$$.fragment,e),_(Ke.$$.fragment,e),_(Qe.$$.fragment,e),_(Je.$$.fragment,e),_(Ze.$$.fragment,e),_(et.$$.fragment,e),_(at.$$.fragment,e),kn=!0)},o(e){v(Pe.$$.fragment,e),v(Se.$$.fragment,e),v(Ue.$$.fragment,e),v(Ie.$$.fragment,e),v(Fe.$$.fragment,e),v(Re.$$.fragment,e),v(Me.$$.fragment,e),v(ze.$$.fragment,e),v(He.$$.fragment,e),v(Ve.$$.fragment,e),v(_e.$$.fragment,e),v(Ge.$$.fragment,e),v(Oe.$$.fragment,e),v(Be.$$.fragment,e),v(Le.$$.fragment,e),v(Xe.$$.fragment,e),v(Ye.$$.fragment,e),v(We.$$.fragment,e),v(Ke.$$.fragment,e),v(Qe.$$.fragment,e),v(Je.$$.fragment,e),v(Ze.$$.fragment,e),v(et.$$.fragment,e),v(at.$$.fragment,e),kn=!1},d(e){s(g),e&&s(A),e&&s(T),y(Pe),e&&s(Ea),e&&s(j),e&&s(ja),e&&s(rt),e&&s(ka),e&&s(ie),e&&s(Da),e&&s(it),e&&s(Aa),e&&s(V),y(Se),e&&s(Pa),e&&s(qe),e&&s(xa),e&&s(pe),e&&s(Sa),y(Ue,e),e&&s(qa),e&&s(G),y(Ie),e&&s(Na),e&&s(ce),e&&s(Ua),y(Fe,e),e&&s(Ia),e&&s(O),y(Re),e&&s(Ca),e&&s(P),e&&s(Fa),y(Me,e),e&&s(Ra),e&&s(ue),e&&s(Ma),e&&s(B),y(ze),e&&s(za),e&&s(x),e&&s(Ha),y(He,e),e&&s(Va),e&&s(L),y(Ve),e&&s(Ga),e&&s(lt),e&&s(Oa),y(_e,e),e&&s(Ba),e&&s(ve),e&&s(La),y(Ge,e),e&&s(Xa),e&&s(ft),e&&s(Ya),e&&s(X),y(Oe),e&&s(Wa),e&&s(ct),e&&s(Ka),e&&s(S),e&&s(Qa),e&&s(we),e&&s(Ja),y(Be,e),e&&s(Za),e&&s(mt),e&&s(en),e&&s(Y),y(Le),e&&s(tn),e&&s(_t),e&&s(sn),e&&s(ge),e&&s(an),y(Xe,e),e&&s(nn),e&&s(vt),e&&s(on),e&&s(yt),e&&s(rn),y(Ye,e),e&&s(ln),e&&s(W),y(We),e&&s(pn),e&&s(wt),e&&s(fn),e&&s(bt),e&&s(cn),y(Ke,e),e&&s(hn),e&&s(K),y(Qe),e&&s(un),e&&s(je),e&&s(dn),e&&s(gt),e&&s(mn),y(Je,e),e&&s(_n),e&&s(ke),e&&s(vn),y(Ze,e),e&&s(yn),e&&s(Q),y(et),e&&s(wn),e&&s(q),e&&s(bn),e&&s($t),e&&s(gn),e&&s(De),e&&s($n),e&&s(Et),e&&s(En),e&&s(N),e&&s(jn),y(at,e)}}}const Hf={local:"memory-and-speed",sections:[{local:"enable-cudnn-autotuner",sections:[{local:"use-tf32-instead-of-fp32-on-ampere-and-later-cuda-devices",title:"Use tf32 instead of fp32 (on Ampere and later CUDA devices)"}],title:"Enable cuDNN auto-tuner"},{local:"automatic-mixed-precision-amp",title:"Automatic mixed precision (AMP)"},{local:"half-precision-weights",title:"Half precision weights"},{local:"sliced-attention-for-additional-memory-savings",title:"Sliced attention for additional memory savings"},{local:"sliced-vae-decode-for-larger-batches",title:"Sliced VAE decode for larger batches"},{local:"offloading-to-cpu-with-accelerate-for-memory-savings",title:"Offloading to CPU with accelerate for memory savings"},{local:"using-channels-last-memory-format",title:"Using Channels Last memory format"},{local:"tracing",title:"Tracing"},{local:"memory-efficient-attention",title:"Memory Efficient Attention"}],title:"Memory and speed"};function Vf($a){return Ff(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xf extends Nf{constructor(g){super();Uf(this,g,Vf,zf,If,{})}}export{Xf as default,Hf as metadata};
