import{S as zm,i as Hm,s as Pm,e as s,k as f,w as y,t as n,M as qm,c as a,d as l,m,a as o,x as w,h as i,b as c,G as t,g as p,y as M,q as b,o as J,B as T,v as Km}from"../../chunks/vendor-hf-doc-builder.js";import{T as Ks}from"../../chunks/Tip-hf-doc-builder.js";import{I as W}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as Z}from"../../chunks/CodeBlock-hf-doc-builder.js";function Lm(B){let h,d,u,U,v,_;return{c(){h=s("p"),d=n("It is strongly discouraged to make use of "),u=s("a"),U=s("code"),v=n("torch.autocast"),_=n(` in any of the pipelines as it can lead to black images and is always slower than using pure
float16 precision.`),this.h()},l(F){h=a(F,"P",{});var C=o(h);d=i(C,"It is strongly discouraged to make use of "),u=a(C,"A",{href:!0,rel:!0});var Tt=o(u);U=a(Tt,"CODE",{});var Ye=o(U);v=i(Ye,"torch.autocast"),Ye.forEach(l),Tt.forEach(l),_=i(C,` in any of the pipelines as it can lead to black images and is always slower than using pure
float16 precision.`),C.forEach(l),this.h()},h(){c(u,"href","https://pytorch.org/docs/stable/amp.html#torch.autocast"),c(u,"rel","nofollow")},m(F,C){p(F,h,C),t(h,d),t(h,u),t(u,U),t(U,v),t(h,_)},d(F){F&&l(h)}}}function Om(B){let h;return{c(){h=n(`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(d){h=i(d,`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(d,u){p(d,h,u)},d(d){d&&l(h)}}}function ec(B){let h,d,u,U;return{c(){h=n("Consider using "),d=s("a"),u=n("model offloading"),U=n(" as another point in the optimization space: it will be much faster, but memory savings won't be as large."),this.h()},l(v){h=i(v,"Consider using "),d=a(v,"A",{href:!0});var _=o(d);u=i(_,"model offloading"),_.forEach(l),U=i(v," as another point in the optimization space: it will be much faster, but memory savings won't be as large."),this.h()},h(){c(d,"href","#model_offloading")},m(v,_){p(v,h,_),p(v,d,_),t(d,u),p(v,U,_)},d(v){v&&l(h),v&&l(d),v&&l(U)}}}function tc(B){let h;return{c(){h=n("This feature requires `accelerate` version 0.17.0 or larger.")},l(d){h=i(d,"This feature requires `accelerate` version 0.17.0 or larger.")},m(d,u){p(d,h,u)},d(d){d&&l(h)}}}function lc(B){let h,d;return{c(){h=s("p"),d=n("If you have PyTorch 2.0 installed, you shouldn\u2019t use xFormers!")},l(u){h=a(u,"P",{});var U=o(h);d=i(U,"If you have PyTorch 2.0 installed, you shouldn\u2019t use xFormers!"),U.forEach(l)},m(u,U){p(u,h,U),t(h,d)},d(u){u&&l(h)}}}function sc(B){let h,d,u,U,v,_,F,C,Tt,Ye,G,So,Jl,Ro,Ao,Fe,Xo,No,Ut,Yo,Fo,Ls,vt,Qo,Os,ye,Tl,Q,ea,xo,Ul,Do,zo,vl,Ho,Po,I,x,_l,qo,Ko,jl,Lo,Oo,El,en,tn,D,Il,ln,sn,gl,an,on,Zl,nn,rn,z,Bl,pn,fn,Cl,mn,cn,Gl,hn,dn,H,kl,un,yn,$l,wn,Mn,Wl,bn,Jn,P,Vl,Tn,Un,Sl,vn,_n,Rl,jn,ta,_t,En,la,q,we,Al,Qe,In,Xl,gn,sa,Me,Zn,xe,Bn,Cn,aa,De,oa,K,be,Nl,ze,Gn,Yl,kn,na,V,$n,Fl,Wn,Vn,Ql,Sn,Rn,ia,He,ra,Je,pa,L,Te,xl,Pe,An,Dl,Xn,fa,jt,Nn,ma,Ue,ca,ve,Yn,Et,Fn,Qn,ha,qe,da,It,xn,ua,O,_e,zl,Ke,Dn,Hl,zn,ya,gt,Hn,wa,S,Pn,Zt,qn,Kn,Bt,Ln,On,Ma,je,ei,Ct,ti,li,ba,Le,Ja,Gt,si,Ta,ee,Ee,Pl,Oe,ai,ql,oi,Ua,kt,ni,va,R,ii,$t,ri,pi,Wt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11bC5hbGxvd190ZjMyJTIwJTNEJTIwVHJ1ZQ==",highlighted:`<span class="hljs-keyword">import</span> torch

torch.backends.cuda.matmul.allow_tf32 = <span class="hljs-literal">True</span>`}}),ze=new W({}),He=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQXBpcGUlMjAlM0QlMjBwaXBlLnRvKCUyMmN1ZGElMjIpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQWltYWdlJTIwJTNEJTIwcGlwZShwcm9tcHQpLmltYWdlcyU1QjAlNUQ=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipe = DiffusionPipeline.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>]`}}),Je=new Ks({props:{warning:!0,$$slots:{default:[Lm]},$$scope:{ctx:B}}}),Pe=new W({}),Ue=new Ks({props:{$$slots:{default:[Om]},$$scope:{ctx:B}}}),qe=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQXBpcGUlMjAlM0QlMjBwaXBlLnRvKCUyMmN1ZGElMjIpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQXBpcGUuZW5hYmxlX2F0dGVudGlvbl9zbGljaW5nKCklMEFpbWFnZSUyMCUzRCUyMHBpcGUocHJvbXB0KS5pbWFnZXMlNUIwJTVE",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipe = DiffusionPipeline.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>]`}}),Ke=new W({}),Le=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQXBpcGUlMjAlM0QlMjBwaXBlLnRvKCUyMmN1ZGElMjIpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQXBpcGUuZW5hYmxlX3ZhZV9zbGljaW5nKCklMEFpbWFnZXMlMjAlM0QlMjBwaXBlKCU1QnByb21wdCU1RCUyMColMjAzMikuaW1hZ2Vz",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`}}),Oe=new W({}),et=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMkMlMjBVbmlQQ011bHRpc3RlcFNjaGVkdWxlciUwQSUwQXBpcGUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIycnVud2F5bWwlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYlMkMlMEEpJTBBcGlwZS5zY2hlZHVsZXIlMjAlM0QlMjBVbmlQQ011bHRpc3RlcFNjaGVkdWxlci5mcm9tX2NvbmZpZyhwaXBlLnNjaGVkdWxlci5jb25maWcpJTBBcGlwZSUyMCUzRCUyMHBpcGUudG8oJTIyY3VkYSUyMiklMEFwcm9tcHQlMjAlM0QlMjAlMjJhJTIwYmVhdXRpZnVsJTIwbGFuZHNjYXBlJTIwcGhvdG9ncmFwaCUyMiUwQXBpcGUuZW5hYmxlX3ZhZV90aWxpbmcoKSUwQXBpcGUuZW5hYmxlX3hmb3JtZXJzX21lbW9yeV9lZmZpY2llbnRfYXR0ZW50aW9uKCklMEElMEFpbWFnZSUyMCUzRCUyMHBpcGUoJTVCcHJvbXB0JTVEJTJDJTIwd2lkdGglM0QzODQwJTJDJTIwaGVpZ2h0JTNEMjIyNCUyQyUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QyMCkuaW1hZ2VzJTVCMCU1RA==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline, UniPCMultistepScheduler

pipe = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>,
    torch_dtype=torch.float16,
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)
prompt = <span class="hljs-string">&quot;a beautiful landscape photograph&quot;</span>
pipe.enable_vae_tiling()
pipe.enable_xformers_memory_efficient_attention()

image = pipe([prompt], width=<span class="hljs-number">3840</span>, height=<span class="hljs-number">2224</span>, num_inference_steps=<span class="hljs-number">20</span>).images[<span class="hljs-number">0</span>]`}}),tt=new W({}),lt=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQSUwQXByb21wdCUyMCUzRCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMEFwaXBlLmVuYWJsZV9zZXF1ZW50aWFsX2NwdV9vZmZsb2FkKCklMEFpbWFnZSUyMCUzRCUyMHBpcGUocHJvbXB0KS5pbWFnZXMlNUIwJTVE",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,
)

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>]`}}),Ce=new Ks({props:{$$slots:{default:[ec]},$$scope:{ctx:B}}}),st=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQSUwQXByb21wdCUyMCUzRCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMEFwaXBlLmVuYWJsZV9zZXF1ZW50aWFsX2NwdV9vZmZsb2FkKCklMEFwaXBlLmVuYWJsZV9hdHRlbnRpb25fc2xpY2luZygxKSUwQSUwQWltYWdlJTIwJTNEJTIwcGlwZShwcm9tcHQpLmltYWdlcyU1QjAlNUQ=",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,
)

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>]`}}),ot=new W({}),it=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTIwJTIwJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQSUwQXByb21wdCUyMCUzRCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMEFwaXBlLmVuYWJsZV9tb2RlbF9jcHVfb2ZmbG9hZCgpJTBBaW1hZ2UlMjAlM0QlMjBwaXBlKHByb21wdCkuaW1hZ2VzJTVCMCU1RA==",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,
)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
pipe.enable_model_cpu_offload()
image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),rt=new Z({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKSUwQSUwQXByb21wdCUyMCUzRCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMEFwaXBlLmVuYWJsZV9tb2RlbF9jcHVfb2ZmbG9hZCgpJTBBcGlwZS5lbmFibGVfYXR0ZW50aW9uX3NsaWNpbmcoMSklMEElMEFpbWFnZSUyMCUzRCUyMHBpcGUocHJvbXB0KS5pbWFnZXMlNUIwJTVE",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,
)

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

image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),$e=new Ks({props:{$$slots:{default:[tc]},$$scope:{ctx:B}}}),ft=new W({}),mt=new Z({props:{code:"cHJpbnQocGlwZS51bmV0LmNvbnZfb3V0LnN0YXRlX2RpY3QoKSU1QiUyMndlaWdodCUyMiU1RC5zdHJpZGUoKSklMjAlMjAlMjMlMjAoMjg4MCUyQyUyMDklMkMlMjAzJTJDJTIwMSklMEFwaXBlLnVuZXQudG8obWVtb3J5X2Zvcm1hdCUzRHRvcmNoLmNoYW5uZWxzX2xhc3QpJTIwJTIwJTIzJTIwaW4tcGxhY2UlMjBvcGVyYXRpb24lMEFwcmludCglMEElMjAlMjAlMjAlMjBwaXBlLnVuZXQuY29udl9vdXQuc3RhdGVfZGljdCgpJTVCJTIyd2VpZ2h0JTIyJTVELnN0cmlkZSgpJTBBKSUyMCUyMCUyMyUyMCgyODgwJTJDJTIwMSUyQyUyMDk2MCUyQyUyMDMyMCklMjBoYXZpbmclMjBhJTIwc3RyaWRlJTIwb2YlMjAxJTIwZm9yJTIwdGhlJTIwMm5kJTIwZGltZW5zaW9uJTIwcHJvdmVzJTIwdGhhdCUyMGl0JTIwd29ya3M=",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>`}}),ct=new W({}),ht=new Z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AlMjBzdGFydF90aW1lJTIwJTNEJTIwdGltZS50aW1lKCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmb3IlMjBfJTIwaW4lMjByYW5nZSh1bmV0X3J1bnNfcGVyX2V4cGVyaW1lbnQpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3JpZ19vdXRwdXQlMjAlM0QlMjB1bmV0KCppbnB1dHMpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdG9yY2guY3VkYS5zeW5jaHJvbml6ZSgpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJpbnQoZiUyMnVuZXQlMjBpbmZlcmVuY2UlMjB0b29rJTIwJTdCdGltZS50aW1lKCklMjAtJTIwc3RhcnRfdGltZSUzQS4yZiU3RCUyMHNlY29uZHMlMjIpJTBBJTBBJTIzJTIwc2F2ZSUyMHRoZSUyMG1vZGVsJTBBdW5ldF90cmFjZWQuc2F2ZSglMjJ1bmV0X3RyYWNlZC5wdCUyMik=",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>)`}}),dt=new Z({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGF0YWNsYXNzZXMlMjBpbXBvcnQlMjBkYXRhY2xhc3MlMEElMEElMEElNDBkYXRhY2xhc3MlMEFjbGFzcyUyMFVOZXQyRENvbmRpdGlvbk91dHB1dCUzQSUwQSUyMCUyMCUyMCUyMHNhbXBsZSUzQSUyMHRvcmNoLkZsb2F0VGVuc29yJTBBJTBBJTBBcGlwZSUyMCUzRCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUwQSUyMCUyMCUyMCUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUwQSkudG8oJTIyY3VkYSUyMiklMEElMEElMjMlMjB1c2UlMjBqaXR0ZWQlMjB1bmV0JTBBdW5ldF90cmFjZWQlMjAlM0QlMjB0b3JjaC5qaXQubG9hZCglMjJ1bmV0X3RyYWNlZC5wdCUyMiklMEElMEElMEElMjMlMjBkZWwlMjBwaXBlLnVuZXQlMEFjbGFzcyUyMFRyYWNlZFVOZXQodG9yY2gubm4uTW9kdWxlKSUzQSUwQSUyMCUyMCUyMCUyMGRlZiUyMF9faW5pdF9fKHNlbGYpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc3VwZXIoKS5fX2luaXRfXygpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2VsZi5pbl9jaGFubmVscyUyMCUzRCUyMHBpcGUudW5ldC5pbl9jaGFubmVscyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHNlbGYuZGV2aWNlJTIwJTNEJTIwcGlwZS51bmV0LmRldmljZSUwQSUwQSUyMCUyMCUyMCUyMGRlZiUyMGZvcndhcmQoc2VsZiUyQyUyMGxhdGVudF9tb2RlbF9pbnB1dCUyQyUyMHQlMkMlMjBlbmNvZGVyX2hpZGRlbl9zdGF0ZXMpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2FtcGxlJTIwJTNEJTIwdW5ldF90cmFjZWQobGF0ZW50X21vZGVsX2lucHV0JTJDJTIwdCUyQyUyMGVuY29kZXJfaGlkZGVuX3N0YXRlcyklNUIwJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwVU5ldDJEQ29uZGl0aW9uT3V0cHV0KHNhbXBsZSUzRHNhbXBsZSklMEElMEElMEFwaXBlLnVuZXQlMjAlM0QlMjBUcmFjZWRVTmV0KCklMEElMEF3aXRoJTIwdG9yY2guaW5mZXJlbmNlX21vZGUoKSUzQSUwQSUyMCUyMCUyMCUyMGltYWdlJTIwJTNEJTIwcGlwZSglNUJwcm9tcHQlNUQlMjAqJTIwMSUyQyUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0Q1MCkuaW1hZ2VzJTVCMCU1RA==",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>]`}}),ut=new W({}),Ne=new Ks({props:{warning:!0,$$slots:{default:[lc]},$$scope:{ctx:B}}}),Mt=new Z({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEElMEFwaXBlJTIwJTNEJTIwRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKS50byglMjJjdWRhJTIyKSUwQSUwQXBpcGUuZW5hYmxlX3hmb3JtZXJzX21lbW9yeV9lZmZpY2llbnRfYXR0ZW50aW9uKCklMEElMEF3aXRoJTIwdG9yY2guaW5mZXJlbmNlX21vZGUoKSUzQSUwQSUyMCUyMCUyMCUyMHNhbXBsZSUyMCUzRCUyMHBpcGUoJTIyYSUyMHNtYWxsJTIwY2F0JTIyKSUwQSUwQSUyMyUyMG9wdGlvbmFsJTNBJTIwWW91JTIwY2FuJTIwZGlzYWJsZSUyMGl0JTIwdmlhJTBBJTIzJTIwcGlwZS5kaXNhYmxlX3hmb3JtZXJzX21lbW9yeV9lZmZpY2llbnRfYXR0ZW50aW9uKCk=",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-keyword">import</span> torch

pipe = DiffusionPipeline.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(){h=s("meta"),d=f(),u=s("h1"),U=s("a"),v=s("span"),y(_.$$.fragment),F=f(),C=s("span"),Tt=n("Memory and speed"),Ye=f(),G=s("p"),So=n("We present some techniques and ideas to optimize \u{1F917} Diffusers "),Jl=s("em"),Ro=n("inference"),Ao=n(" for memory or speed. As a general rule, we recommend the use of "),Fe=s("a"),Xo=n("xFormers"),No=n(" for memory efficient attention, please see the recommended "),Ut=s("a"),Yo=n("installation instructions"),Fo=n("."),Ls=f(),vt=s("p"),Qo=n("We\u2019ll discuss how the following settings impact performance and memory."),Os=f(),ye=s("table"),Tl=s("thead"),Q=s("tr"),ea=s("th"),xo=f(),Ul=s("th"),Do=n("Latency"),zo=f(),vl=s("th"),Ho=n("Speedup"),Po=f(),I=s("tbody"),x=s("tr"),_l=s("td"),qo=n("original"),Ko=f(),jl=s("td"),Lo=n("9.50s"),Oo=f(),El=s("td"),en=n("x1"),tn=f(),D=s("tr"),Il=s("td"),ln=n("fp16"),sn=f(),gl=s("td"),an=n("3.61s"),on=f(),Zl=s("td"),nn=n("x2.63"),rn=f(),z=s("tr"),Bl=s("td"),pn=n("channels last"),fn=f(),Cl=s("td"),mn=n("3.30s"),cn=f(),Gl=s("td"),hn=n("x2.88"),dn=f(),H=s("tr"),kl=s("td"),un=n("traced UNet"),yn=f(),$l=s("td"),wn=n("3.21s"),Mn=f(),Wl=s("td"),bn=n("x2.96"),Jn=f(),P=s("tr"),Vl=s("td"),Tn=n("memory efficient attention"),Un=f(),Sl=s("td"),vn=n("2.63s"),_n=f(),Rl=s("td"),jn=n("x3.61"),ta=f(),_t=s("em"),En=n(`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.`),la=f(),q=s("h3"),we=s("a"),Al=s("span"),y(Qe.$$.fragment),In=f(),Xl=s("span"),gn=n("Use tf32 instead of fp32 (on Ampere and later CUDA devices)"),sa=f(),Me=s("p"),Zn=n("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 "),xe=s("a"),Bn=n("here"),Cn=n(". All you need to do is to add this before your inference:"),aa=f(),y(De.$$.fragment),oa=f(),K=s("h2"),be=s("a"),Nl=s("span"),y(ze.$$.fragment),Gn=f(),Yl=s("span"),kn=n("Half precision weights"),na=f(),V=s("p"),$n=n("To save more GPU memory and get 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 "),Fl=s("code"),Wn=n("fp16"),Vn=n(", and telling PyTorch to use the "),Ql=s("code"),Sn=n("float16"),Rn=n(" type when loading them:"),ia=f(),y(He.$$.fragment),ra=f(),y(Je.$$.fragment),pa=f(),L=s("h2"),Te=s("a"),xl=s("span"),y(Pe.$$.fragment),An=f(),Dl=s("span"),Xn=n("Sliced attention for additional memory savings"),fa=f(),jt=s("p"),Nn=n("For even additional memory savings, you can use a sliced version of attention that performs the computation in steps instead of all at once."),ma=f(),y(Ue.$$.fragment),ca=f(),ve=s("p"),Yn=n("To perform the attention computation sequentially over each head, you only need to invoke "),Et=s("a"),Fn=n("enable_attention_slicing()"),Qn=n(" in your pipeline before inference, like here:"),ha=f(),y(qe.$$.fragment),da=f(),It=s("p"),xn=n("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!"),ua=f(),O=s("h2"),_e=s("a"),zl=s("span"),y(Ke.$$.fragment),Dn=f(),Hl=s("span"),zn=n("Sliced VAE decode for larger batches"),ya=f(),gt=s("p"),Hn=n("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."),wa=f(),S=s("p"),Pn=n("You likely want to couple this with "),Zt=s("a"),qn=n("enable_attention_slicing()"),Kn=n(" or "),Bt=s("a"),Ln=n("enable_xformers_memory_efficient_attention()"),On=n(" to further minimize memory use."),Ma=f(),je=s("p"),ei=n("To perform the VAE decode one image at a time, invoke "),Ct=s("a"),ti=n("enable_vae_slicing()"),li=n(" in your pipeline before inference. For example:"),ba=f(),y(Le.$$.fragment),Ja=f(),Gt=s("p"),si=n("You may see a small performance boost in VAE decode on multi-image batches. There should be no performance impact on single-image batches."),Ta=f(),ee=s("h2"),Ee=s("a"),Pl=s("span"),y(Oe.$$.fragment),ai=f(),ql=s("span"),oi=n("Tiled VAE decode and encode for large images"),Ua=f(),kt=s("p"),ni=n("Tiled VAE processing makes it possible to work with large images on limited VRAM. For example, generating 4k images in 8GB of VRAM. Tiled VAE decoder splits the image into overlapping tiles, decodes the tiles, and blends the outputs to make the final image."),va=f(),R=s("p"),ii=n("You want to couple this with "),$t=s("a"),ri=n("enable_attention_slicing()"),pi=n(" or "),Wt=s("a"),fi=n("enable_xformers_memory_efficient_attention()"),mi=n(" to further minimize memory use."),_a=f(),Ie=s("p"),ci=n("To use tiled VAE processing, invoke "),Vt=s("a"),hi=n("enable_vae_tiling()"),di=n(" in your pipeline before inference. For example:"),ja=f(),y(et.$$.fragment),Ea=f(),St=s("p"),ui=n("The output image will have some tile-to-tile tone variation from the tiles having separate decoders, but you shouldn\u2019t see sharp seams between the tiles. The tiling is turned off for images that are 512x512 or smaller."),Ia=f(),Rt=s("a"),ga=f(),te=s("h2"),ge=s("a"),Kl=s("span"),y(tt.$$.fragment),yi=f(),Ll=s("span"),wi=n("Offloading to CPU with accelerate for memory savings"),Za=f(),At=s("p"),Mi=n("For additional memory savings, you can offload the weights to CPU and only load them to GPU when performing the forward pass."),Ba=f(),Ze=s("p"),bi=n("To perform CPU offloading, all you have to do is invoke "),Xt=s("a"),Ji=n("enable_sequential_cpu_offload()"),Ti=n(":"),Ca=f(),y(lt.$$.fragment),Ga=f(),Nt=s("p"),Ui=n("And you can get the memory consumption to < 3GB."),ka=f(),Be=s("p"),vi=n("Note that this method works at the submodule level, not on whole models. This is the best way to minimize memory consumption, but inference is much slower due to the iterative nature of the process. The UNet component of the pipeline runs several times (as many as "),Ol=s("code"),_i=n("num_inference_steps"),ji=n("); each time, the different submodules of the UNet are sequentially onloaded and then offloaded as they are needed, so the number of memory transfers is large."),$a=f(),y(Ce.$$.fragment),Wa=f(),Yt=s("p"),Ei=n("It is also possible to chain offloading with attention slicing for minimal memory consumption (< 2GB)."),Va=f(),y(st.$$.fragment),Sa=f(),g=s("p"),es=s("strong"),Ii=n("Note"),gi=n(": When using "),ts=s("code"),Zi=n("enable_sequential_cpu_offload()"),Bi=n(", it is important to "),ls=s("strong"),Ci=n("not"),Gi=n(" move the pipeline to CUDA beforehand or else the gain in memory consumption will only be minimal. See "),at=s("a"),ki=n("this issue"),$i=n(" for more information."),Ra=f(),le=s("p"),ss=s("strong"),Wi=n("Note"),Vi=n(": "),as=s("code"),Si=n("enable_sequential_cpu_offload()"),Ri=n(" is a stateful operation that installs hooks on the models."),Aa=f(),Ft=s("a"),Xa=f(),se=s("h2"),Ge=s("a"),os=s("span"),y(ot.$$.fragment),Ai=f(),ns=s("span"),Xi=n("Model offloading for fast inference and memory savings"),Na=f(),nt=s("p"),Qt=s("a"),Ni=n("Sequential CPU offloading"),Yi=n(", as discussed in the previous section, preserves a lot of memory but makes inference slower, because submodules are moved to GPU as needed, and immediately returned to CPU when a new module runs."),Ya=f(),A=s("p"),Fi=n("Full-model offloading is an alternative that moves whole models to the GPU, instead of handling each model\u2019s constituent "),is=s("em"),Qi=n("modules"),xi=n(". This results in a negligible impact on inference time (compared with moving the pipeline to "),rs=s("code"),Di=n("cuda"),zi=n("), while still providing some memory savings."),Fa=f(),xt=s("p"),Hi=n(`In this scenario, only one of the main components of the pipeline (typically: text encoder, unet and vae)
will be in the GPU while the others wait in the CPU. Components like the UNet that run for multiple iterations will stay on GPU until they are no longer needed.`),Qa=f(),ke=s("p"),Pi=n("This feature can be enabled by invoking "),ps=s("code"),qi=n("enable_model_cpu_offload()"),Ki=n(" on the pipeline, as shown below."),xa=f(),y(it.$$.fragment),Da=f(),Dt=s("p"),Li=n("This is also compatible with attention slicing for additional memory savings."),za=f(),y(rt.$$.fragment),Ha=f(),y($e.$$.fragment),Pa=f(),k=s("p"),fs=s("strong"),Oi=n("Note"),er=n(": "),ms=s("code"),tr=n("enable_model_cpu_offload()"),lr=n(` is a stateful operation that installs hooks on the models and state on the pipeline. In order to properly offload
models after they are called, it is required that the entire pipeline is run and models are called in the order the pipeline expects them to be. Exercise caution
if models are re-used outside the context of the pipeline after hooks have been installed. See `),pt=s("a"),sr=n("accelerate"),ar=n(`
for further docs on removing hooks.`),qa=f(),ae=s("h2"),We=s("a"),cs=s("span"),y(ft.$$.fragment),or=f(),hs=s("span"),nr=n("Using Channels Last memory format"),Ka=f(),zt=s("p"),ir=n("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."),La=f(),Ht=s("p"),rr=n("For example, in order to set the UNet model in our pipeline to use channels last format, we can use the following:"),Oa=f(),y(mt.$$.fragment),eo=f(),oe=s("h2"),Ve=s("a"),ds=s("span"),y(ct.$$.fragment),pr=f(),us=s("span"),fr=n("Tracing"),to=f(),Se=s("p"),mr=n("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 "),ys=s("code"),cr=n("ScriptFunction"),hr=n(" is returned that will be optimized using just-in-time compilation."),lo=f(),Pt=s("p"),dr=n("To trace our UNet model, we can use the following:"),so=f(),y(ht.$$.fragment),ao=f(),Re=s("p"),ur=n("Then we can replace the "),ws=s("code"),yr=n("unet"),wr=n(" attribute of the pipeline with the traced model like the following"),oo=f(),y(dt.$$.fragment),no=f(),ne=s("h2"),Ae=s("a"),Ms=s("span"),y(ut.$$.fragment),Mr=f(),bs=s("span"),br=n("Memory Efficient Attention"),io=f(),X=s("p"),Jr=n("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: "),yt=s("a"),Tr=n("code"),Ur=n(", "),wt=s("a"),vr=n("paper"),_r=n("."),ro=f(),qt=s("p"),jr=n("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):"),po=f(),Xe=s("table"),Js=s("thead"),ie=s("tr"),Ts=s("th"),Er=n("GPU"),Ir=f(),Us=s("th"),gr=n("Base Attention FP16"),Zr=f(),vs=s("th"),Br=n("Memory Efficient Attention FP16"),Cr=f(),j=s("tbody"),re=s("tr"),_s=s("td"),Gr=n("NVIDIA Tesla T4"),kr=f(),js=s("td"),$r=n("3.5it/s"),Wr=f(),Es=s("td"),Vr=n("5.5it/s"),Sr=f(),pe=s("tr"),Is=s("td"),Rr=n("NVIDIA 3060 RTX"),Ar=f(),gs=s("td"),Xr=n("4.6it/s"),Nr=f(),Zs=s("td"),Yr=n("7.8it/s"),Fr=f(),fe=s("tr"),Bs=s("td"),Qr=n("NVIDIA A10G"),xr=f(),Cs=s("td"),Dr=n("8.88it/s"),zr=f(),Gs=s("td"),Hr=n("15.6it/s"),Pr=f(),me=s("tr"),ks=s("td"),qr=n("NVIDIA RTX A6000"),Kr=f(),$s=s("td"),Lr=n("11.7it/s"),Or=f(),Ws=s("td"),ep=n("21.09it/s"),tp=f(),ce=s("tr"),Vs=s("td"),lp=n("NVIDIA TITAN RTX"),sp=f(),Ss=s("td"),ap=n("12.51it/s"),op=f(),Rs=s("td"),np=n("18.22it/s"),ip=f(),he=s("tr"),As=s("td"),rp=n("A100-SXM4-40GB"),pp=f(),Xs=s("td"),fp=n("18.6it/s"),mp=f(),Ns=s("td"),cp=n("29.it/s"),hp=f(),de=s("tr"),Ys=s("td"),dp=n("A100-SXM-80GB"),up=f(),Fs=s("td"),yp=n("18.7it/s"),wp=f(),Qs=s("td"),Mp=n("29.5it/s"),fo=f(),Kt=s("p"),bp=n("To leverage it just make sure you have:"),mo=f(),y(Ne.$$.fragment),co=f(),N=s("ul"),xs=s("li"),Jp=n("PyTorch > 1.12"),Tp=f(),Ds=s("li"),Up=n("Cuda available"),vp=f(),Lt=s("li"),Ot=s("a"),_p=n("Installed the xformers library"),jp=n("."),ho=f(),y(Mt.$$.fragment),this.h()},l(e){const r=qm('[data-svelte="svelte-1phssyn"]',document.head);h=a(r,"META",{name:!0,content:!0}),r.forEach(l),d=m(e),u=a(e,"H1",{class:!0});var bt=o(u);U=a(bt,"A",{id:!0,class:!0,href:!0});var zs=o(U);v=a(zs,"SPAN",{});var Hs=o(v);w(_.$$.fragment,Hs),Hs.forEach(l),zs.forEach(l),F=m(bt),C=a(bt,"SPAN",{});var Ps=o(C);Tt=i(Ps,"Memory and speed"),Ps.forEach(l),bt.forEach(l),Ye=m(e),G=a(e,"P",{});var $=o(G);So=i($,"We present some techniques and ideas to optimize \u{1F917} Diffusers "),Jl=a($,"EM",{});var gp=o(Jl);Ro=i(gp,"inference"),gp.forEach(l),Ao=i($," for memory or speed. As a general rule, we recommend the use of "),Fe=a($,"A",{href:!0,rel:!0});var Zp=o(Fe);Xo=i(Zp,"xFormers"),Zp.forEach(l),No=i($," for memory efficient attention, please see the recommended "),Ut=a($,"A",{href:!0});var Bp=o(Ut);Yo=i(Bp,"installation instructions"),Bp.forEach(l),Fo=i($,"."),$.forEach(l),Ls=m(e),vt=a(e,"P",{});var Cp=o(vt);Qo=i(Cp,"We\u2019ll discuss how the following settings impact performance and memory."),Cp.forEach(l),Os=m(e),ye=a(e,"TABLE",{});var yo=o(ye);Tl=a(yo,"THEAD",{});var Gp=o(Tl);Q=a(Gp,"TR",{});var el=o(Q);ea=a(el,"TH",{}),o(ea).forEach(l),xo=m(el),Ul=a(el,"TH",{});var kp=o(Ul);Do=i(kp,"Latency"),kp.forEach(l),zo=m(el),vl=a(el,"TH",{});var $p=o(vl);Ho=i($p,"Speedup"),$p.forEach(l),el.forEach(l),Gp.forEach(l),Po=m(yo),I=a(yo,"TBODY",{});var Y=o(I);x=a(Y,"TR",{});var tl=o(x);_l=a(tl,"TD",{});var Wp=o(_l);qo=i(Wp,"original"),Wp.forEach(l),Ko=m(tl),jl=a(tl,"TD",{});var Vp=o(jl);Lo=i(Vp,"9.50s"),Vp.forEach(l),Oo=m(tl),El=a(tl,"TD",{});var Sp=o(El);en=i(Sp,"x1"),Sp.forEach(l),tl.forEach(l),tn=m(Y),D=a(Y,"TR",{});var ll=o(D);Il=a(ll,"TD",{});var Rp=o(Il);ln=i(Rp,"fp16"),Rp.forEach(l),sn=m(ll),gl=a(ll,"TD",{});var Ap=o(gl);an=i(Ap,"3.61s"),Ap.forEach(l),on=m(ll),Zl=a(ll,"TD",{});var Xp=o(Zl);nn=i(Xp,"x2.63"),Xp.forEach(l),ll.forEach(l),rn=m(Y),z=a(Y,"TR",{});var sl=o(z);Bl=a(sl,"TD",{});var Np=o(Bl);pn=i(Np,"channels last"),Np.forEach(l),fn=m(sl),Cl=a(sl,"TD",{});var Yp=o(Cl);mn=i(Yp,"3.30s"),Yp.forEach(l),cn=m(sl),Gl=a(sl,"TD",{});var Fp=o(Gl);hn=i(Fp,"x2.88"),Fp.forEach(l),sl.forEach(l),dn=m(Y),H=a(Y,"TR",{});var al=o(H);kl=a(al,"TD",{});var Qp=o(kl);un=i(Qp,"traced UNet"),Qp.forEach(l),yn=m(al),$l=a(al,"TD",{});var xp=o($l);wn=i(xp,"3.21s"),xp.forEach(l),Mn=m(al),Wl=a(al,"TD",{});var Dp=o(Wl);bn=i(Dp,"x2.96"),Dp.forEach(l),al.forEach(l),Jn=m(Y),P=a(Y,"TR",{});var ol=o(P);Vl=a(ol,"TD",{});var zp=o(Vl);Tn=i(zp,"memory efficient attention"),zp.forEach(l),Un=m(ol),Sl=a(ol,"TD",{});var Hp=o(Sl);vn=i(Hp,"2.63s"),Hp.forEach(l),_n=m(ol),Rl=a(ol,"TD",{});var Pp=o(Rl);jn=i(Pp,"x3.61"),Pp.forEach(l),ol.forEach(l),Y.forEach(l),yo.forEach(l),ta=m(e),_t=a(e,"EM",{});var qp=o(_t);En=i(qp,`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.`),qp.forEach(l),la=m(e),q=a(e,"H3",{class:!0});var wo=o(q);we=a(wo,"A",{id:!0,class:!0,href:!0});var Kp=o(we);Al=a(Kp,"SPAN",{});var Lp=o(Al);w(Qe.$$.fragment,Lp),Lp.forEach(l),Kp.forEach(l),In=m(wo),Xl=a(wo,"SPAN",{});var Op=o(Xl);gn=i(Op,"Use tf32 instead of fp32 (on Ampere and later CUDA devices)"),Op.forEach(l),wo.forEach(l),sa=m(e),Me=a(e,"P",{});var Mo=o(Me);Zn=i(Mo,"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 "),xe=a(Mo,"A",{href:!0,rel:!0});var ef=o(xe);Bn=i(ef,"here"),ef.forEach(l),Cn=i(Mo,". All you need to do is to add this before your inference:"),Mo.forEach(l),aa=m(e),w(De.$$.fragment,e),oa=m(e),K=a(e,"H2",{class:!0});var bo=o(K);be=a(bo,"A",{id:!0,class:!0,href:!0});var tf=o(be);Nl=a(tf,"SPAN",{});var lf=o(Nl);w(ze.$$.fragment,lf),lf.forEach(l),tf.forEach(l),Gn=m(bo),Yl=a(bo,"SPAN",{});var sf=o(Yl);kn=i(sf,"Half precision weights"),sf.forEach(l),bo.forEach(l),na=m(e),V=a(e,"P",{});var nl=o(V);$n=i(nl,"To save more GPU memory and get 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 "),Fl=a(nl,"CODE",{});var af=o(Fl);Wn=i(af,"fp16"),af.forEach(l),Vn=i(nl,", and telling PyTorch to use the "),Ql=a(nl,"CODE",{});var of=o(Ql);Sn=i(of,"float16"),of.forEach(l),Rn=i(nl," type when loading them:"),nl.forEach(l),ia=m(e),w(He.$$.fragment,e),ra=m(e),w(Je.$$.fragment,e),pa=m(e),L=a(e,"H2",{class:!0});var Jo=o(L);Te=a(Jo,"A",{id:!0,class:!0,href:!0});var nf=o(Te);xl=a(nf,"SPAN",{});var rf=o(xl);w(Pe.$$.fragment,rf),rf.forEach(l),nf.forEach(l),An=m(Jo),Dl=a(Jo,"SPAN",{});var pf=o(Dl);Xn=i(pf,"Sliced attention for additional memory savings"),pf.forEach(l),Jo.forEach(l),fa=m(e),jt=a(e,"P",{});var ff=o(jt);Nn=i(ff,"For even additional memory savings, you can use a sliced version of attention that performs the computation in steps instead of all at once."),ff.forEach(l),ma=m(e),w(Ue.$$.fragment,e),ca=m(e),ve=a(e,"P",{});var To=o(ve);Yn=i(To,"To perform the attention computation sequentially over each head, you only need to invoke "),Et=a(To,"A",{href:!0});var mf=o(Et);Fn=i(mf,"enable_attention_slicing()"),mf.forEach(l),Qn=i(To," in your pipeline before inference, like here:"),To.forEach(l),ha=m(e),w(qe.$$.fragment,e),da=m(e),It=a(e,"P",{});var cf=o(It);xn=i(cf,"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!"),cf.forEach(l),ua=m(e),O=a(e,"H2",{class:!0});var Uo=o(O);_e=a(Uo,"A",{id:!0,class:!0,href:!0});var hf=o(_e);zl=a(hf,"SPAN",{});var df=o(zl);w(Ke.$$.fragment,df),df.forEach(l),hf.forEach(l),Dn=m(Uo),Hl=a(Uo,"SPAN",{});var uf=o(Hl);zn=i(uf,"Sliced VAE decode for larger batches"),uf.forEach(l),Uo.forEach(l),ya=m(e),gt=a(e,"P",{});var yf=o(gt);Hn=i(yf,"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."),yf.forEach(l),wa=m(e),S=a(e,"P",{});var il=o(S);Pn=i(il,"You likely want to couple this with "),Zt=a(il,"A",{href:!0});var wf=o(Zt);qn=i(wf,"enable_attention_slicing()"),wf.forEach(l),Kn=i(il," or "),Bt=a(il,"A",{href:!0});var Mf=o(Bt);Ln=i(Mf,"enable_xformers_memory_efficient_attention()"),Mf.forEach(l),On=i(il," to further minimize memory use."),il.forEach(l),Ma=m(e),je=a(e,"P",{});var vo=o(je);ei=i(vo,"To perform the VAE decode one image at a time, invoke "),Ct=a(vo,"A",{href:!0});var bf=o(Ct);ti=i(bf,"enable_vae_slicing()"),bf.forEach(l),li=i(vo," in your pipeline before inference. For example:"),vo.forEach(l),ba=m(e),w(Le.$$.fragment,e),Ja=m(e),Gt=a(e,"P",{});var Jf=o(Gt);si=i(Jf,"You may see a small performance boost in VAE decode on multi-image batches. There should be no performance impact on single-image batches."),Jf.forEach(l),Ta=m(e),ee=a(e,"H2",{class:!0});var _o=o(ee);Ee=a(_o,"A",{id:!0,class:!0,href:!0});var Tf=o(Ee);Pl=a(Tf,"SPAN",{});var Uf=o(Pl);w(Oe.$$.fragment,Uf),Uf.forEach(l),Tf.forEach(l),ai=m(_o),ql=a(_o,"SPAN",{});var vf=o(ql);oi=i(vf,"Tiled VAE decode and encode for large images"),vf.forEach(l),_o.forEach(l),Ua=m(e),kt=a(e,"P",{});var _f=o(kt);ni=i(_f,"Tiled VAE processing makes it possible to work with large images on limited VRAM. For example, generating 4k images in 8GB of VRAM. Tiled VAE decoder splits the image into overlapping tiles, decodes the tiles, and blends the outputs to make the final image."),_f.forEach(l),va=m(e),R=a(e,"P",{});var rl=o(R);ii=i(rl,"You want to couple this with "),$t=a(rl,"A",{href:!0});var jf=o($t);ri=i(jf,"enable_attention_slicing()"),jf.forEach(l),pi=i(rl," or "),Wt=a(rl,"A",{href:!0});var Ef=o(Wt);fi=i(Ef,"enable_xformers_memory_efficient_attention()"),Ef.forEach(l),mi=i(rl," to further minimize memory use."),rl.forEach(l),_a=m(e),Ie=a(e,"P",{});var jo=o(Ie);ci=i(jo,"To use tiled VAE processing, invoke "),Vt=a(jo,"A",{href:!0});var If=o(Vt);hi=i(If,"enable_vae_tiling()"),If.forEach(l),di=i(jo," in your pipeline before inference. For example:"),jo.forEach(l),ja=m(e),w(et.$$.fragment,e),Ea=m(e),St=a(e,"P",{});var gf=o(St);ui=i(gf,"The output image will have some tile-to-tile tone variation from the tiles having separate decoders, but you shouldn\u2019t see sharp seams between the tiles. The tiling is turned off for images that are 512x512 or smaller."),gf.forEach(l),Ia=m(e),Rt=a(e,"A",{name:!0}),o(Rt).forEach(l),ga=m(e),te=a(e,"H2",{class:!0});var Eo=o(te);ge=a(Eo,"A",{id:!0,class:!0,href:!0});var Zf=o(ge);Kl=a(Zf,"SPAN",{});var Bf=o(Kl);w(tt.$$.fragment,Bf),Bf.forEach(l),Zf.forEach(l),yi=m(Eo),Ll=a(Eo,"SPAN",{});var Cf=o(Ll);wi=i(Cf,"Offloading to CPU with accelerate for memory savings"),Cf.forEach(l),Eo.forEach(l),Za=m(e),At=a(e,"P",{});var Gf=o(At);Mi=i(Gf,"For additional memory savings, you can offload the weights to CPU and only load them to GPU when performing the forward pass."),Gf.forEach(l),Ba=m(e),Ze=a(e,"P",{});var Io=o(Ze);bi=i(Io,"To perform CPU offloading, all you have to do is invoke "),Xt=a(Io,"A",{href:!0});var kf=o(Xt);Ji=i(kf,"enable_sequential_cpu_offload()"),kf.forEach(l),Ti=i(Io,":"),Io.forEach(l),Ca=m(e),w(lt.$$.fragment,e),Ga=m(e),Nt=a(e,"P",{});var $f=o(Nt);Ui=i($f,"And you can get the memory consumption to < 3GB."),$f.forEach(l),ka=m(e),Be=a(e,"P",{});var go=o(Be);vi=i(go,"Note that this method works at the submodule level, not on whole models. This is the best way to minimize memory consumption, but inference is much slower due to the iterative nature of the process. The UNet component of the pipeline runs several times (as many as "),Ol=a(go,"CODE",{});var Wf=o(Ol);_i=i(Wf,"num_inference_steps"),Wf.forEach(l),ji=i(go,"); each time, the different submodules of the UNet are sequentially onloaded and then offloaded as they are needed, so the number of memory transfers is large."),go.forEach(l),$a=m(e),w(Ce.$$.fragment,e),Wa=m(e),Yt=a(e,"P",{});var Vf=o(Yt);Ei=i(Vf,"It is also possible to chain offloading with attention slicing for minimal memory consumption (< 2GB)."),Vf.forEach(l),Va=m(e),w(st.$$.fragment,e),Sa=m(e),g=a(e,"P",{});var ue=o(g);es=a(ue,"STRONG",{});var Sf=o(es);Ii=i(Sf,"Note"),Sf.forEach(l),gi=i(ue,": When using "),ts=a(ue,"CODE",{});var Rf=o(ts);Zi=i(Rf,"enable_sequential_cpu_offload()"),Rf.forEach(l),Bi=i(ue,", it is important to "),ls=a(ue,"STRONG",{});var Af=o(ls);Ci=i(Af,"not"),Af.forEach(l),Gi=i(ue," move the pipeline to CUDA beforehand or else the gain in memory consumption will only be minimal. See "),at=a(ue,"A",{href:!0,rel:!0});var Xf=o(at);ki=i(Xf,"this issue"),Xf.forEach(l),$i=i(ue," for more information."),ue.forEach(l),Ra=m(e),le=a(e,"P",{});var qs=o(le);ss=a(qs,"STRONG",{});var Nf=o(ss);Wi=i(Nf,"Note"),Nf.forEach(l),Vi=i(qs,": "),as=a(qs,"CODE",{});var Yf=o(as);Si=i(Yf,"enable_sequential_cpu_offload()"),Yf.forEach(l),Ri=i(qs," is a stateful operation that installs hooks on the models."),qs.forEach(l),Aa=m(e),Ft=a(e,"A",{name:!0}),o(Ft).forEach(l),Xa=m(e),se=a(e,"H2",{class:!0});var Zo=o(se);Ge=a(Zo,"A",{id:!0,class:!0,href:!0});var Ff=o(Ge);os=a(Ff,"SPAN",{});var Qf=o(os);w(ot.$$.fragment,Qf),Qf.forEach(l),Ff.forEach(l),Ai=m(Zo),ns=a(Zo,"SPAN",{});var xf=o(ns);Xi=i(xf,"Model offloading for fast inference and memory savings"),xf.forEach(l),Zo.forEach(l),Na=m(e),nt=a(e,"P",{});var Ep=o(nt);Qt=a(Ep,"A",{href:!0});var Df=o(Qt);Ni=i(Df,"Sequential CPU offloading"),Df.forEach(l),Yi=i(Ep,", as discussed in the previous section, preserves a lot of memory but makes inference slower, because submodules are moved to GPU as needed, and immediately returned to CPU when a new module runs."),Ep.forEach(l),Ya=m(e),A=a(e,"P",{});var pl=o(A);Fi=i(pl,"Full-model offloading is an alternative that moves whole models to the GPU, instead of handling each model\u2019s constituent "),is=a(pl,"EM",{});var zf=o(is);Qi=i(zf,"modules"),zf.forEach(l),xi=i(pl,". This results in a negligible impact on inference time (compared with moving the pipeline to "),rs=a(pl,"CODE",{});var Hf=o(rs);Di=i(Hf,"cuda"),Hf.forEach(l),zi=i(pl,"), while still providing some memory savings."),pl.forEach(l),Fa=m(e),xt=a(e,"P",{});var Pf=o(xt);Hi=i(Pf,`In this scenario, only one of the main components of the pipeline (typically: text encoder, unet and vae)
will be in the GPU while the others wait in the CPU. Components like the UNet that run for multiple iterations will stay on GPU until they are no longer needed.`),Pf.forEach(l),Qa=m(e),ke=a(e,"P",{});var Bo=o(ke);Pi=i(Bo,"This feature can be enabled by invoking "),ps=a(Bo,"CODE",{});var qf=o(ps);qi=i(qf,"enable_model_cpu_offload()"),qf.forEach(l),Ki=i(Bo," on the pipeline, as shown below."),Bo.forEach(l),xa=m(e),w(it.$$.fragment,e),Da=m(e),Dt=a(e,"P",{});var Kf=o(Dt);Li=i(Kf,"This is also compatible with attention slicing for additional memory savings."),Kf.forEach(l),za=m(e),w(rt.$$.fragment,e),Ha=m(e),w($e.$$.fragment,e),Pa=m(e),k=a(e,"P",{});var Jt=o(k);fs=a(Jt,"STRONG",{});var Lf=o(fs);Oi=i(Lf,"Note"),Lf.forEach(l),er=i(Jt,": "),ms=a(Jt,"CODE",{});var Of=o(ms);tr=i(Of,"enable_model_cpu_offload()"),Of.forEach(l),lr=i(Jt,` is a stateful operation that installs hooks on the models and state on the pipeline. In order to properly offload
models after they are called, it is required that the entire pipeline is run and models are called in the order the pipeline expects them to be. Exercise caution
if models are re-used outside the context of the pipeline after hooks have been installed. See `),pt=a(Jt,"A",{href:!0,rel:!0});var em=o(pt);sr=i(em,"accelerate"),em.forEach(l),ar=i(Jt,`
for further docs on removing hooks.`),Jt.forEach(l),qa=m(e),ae=a(e,"H2",{class:!0});var Co=o(ae);We=a(Co,"A",{id:!0,class:!0,href:!0});var tm=o(We);cs=a(tm,"SPAN",{});var lm=o(cs);w(ft.$$.fragment,lm),lm.forEach(l),tm.forEach(l),or=m(Co),hs=a(Co,"SPAN",{});var sm=o(hs);nr=i(sm,"Using Channels Last memory format"),sm.forEach(l),Co.forEach(l),Ka=m(e),zt=a(e,"P",{});var am=o(zt);ir=i(am,"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."),am.forEach(l),La=m(e),Ht=a(e,"P",{});var om=o(Ht);rr=i(om,"For example, in order to set the UNet model in our pipeline to use channels last format, we can use the following:"),om.forEach(l),Oa=m(e),w(mt.$$.fragment,e),eo=m(e),oe=a(e,"H2",{class:!0});var Go=o(oe);Ve=a(Go,"A",{id:!0,class:!0,href:!0});var nm=o(Ve);ds=a(nm,"SPAN",{});var im=o(ds);w(ct.$$.fragment,im),im.forEach(l),nm.forEach(l),pr=m(Go),us=a(Go,"SPAN",{});var rm=o(us);fr=i(rm,"Tracing"),rm.forEach(l),Go.forEach(l),to=m(e),Se=a(e,"P",{});var ko=o(Se);mr=i(ko,"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 "),ys=a(ko,"CODE",{});var pm=o(ys);cr=i(pm,"ScriptFunction"),pm.forEach(l),hr=i(ko," is returned that will be optimized using just-in-time compilation."),ko.forEach(l),lo=m(e),Pt=a(e,"P",{});var fm=o(Pt);dr=i(fm,"To trace our UNet model, we can use the following:"),fm.forEach(l),so=m(e),w(ht.$$.fragment,e),ao=m(e),Re=a(e,"P",{});var $o=o(Re);ur=i($o,"Then we can replace the "),ws=a($o,"CODE",{});var mm=o(ws);yr=i(mm,"unet"),mm.forEach(l),wr=i($o," attribute of the pipeline with the traced model like the following"),$o.forEach(l),oo=m(e),w(dt.$$.fragment,e),no=m(e),ne=a(e,"H2",{class:!0});var Wo=o(ne);Ae=a(Wo,"A",{id:!0,class:!0,href:!0});var cm=o(Ae);Ms=a(cm,"SPAN",{});var hm=o(Ms);w(ut.$$.fragment,hm),hm.forEach(l),cm.forEach(l),Mr=m(Wo),bs=a(Wo,"SPAN",{});var dm=o(bs);br=i(dm,"Memory Efficient Attention"),dm.forEach(l),Wo.forEach(l),io=m(e),X=a(e,"P",{});var fl=o(X);Jr=i(fl,"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: "),yt=a(fl,"A",{href:!0,rel:!0});var um=o(yt);Tr=i(um,"code"),um.forEach(l),Ur=i(fl,", "),wt=a(fl,"A",{href:!0,rel:!0});var ym=o(wt);vr=i(ym,"paper"),ym.forEach(l),_r=i(fl,"."),fl.forEach(l),ro=m(e),qt=a(e,"P",{});var wm=o(qt);jr=i(wm,"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):"),wm.forEach(l),po=m(e),Xe=a(e,"TABLE",{});var Vo=o(Xe);Js=a(Vo,"THEAD",{});var Mm=o(Js);ie=a(Mm,"TR",{});var ml=o(ie);Ts=a(ml,"TH",{});var bm=o(Ts);Er=i(bm,"GPU"),bm.forEach(l),Ir=m(ml),Us=a(ml,"TH",{});var Jm=o(Us);gr=i(Jm,"Base Attention FP16"),Jm.forEach(l),Zr=m(ml),vs=a(ml,"TH",{});var Tm=o(vs);Br=i(Tm,"Memory Efficient Attention FP16"),Tm.forEach(l),ml.forEach(l),Mm.forEach(l),Cr=m(Vo),j=a(Vo,"TBODY",{});var E=o(j);re=a(E,"TR",{});var cl=o(re);_s=a(cl,"TD",{});var Um=o(_s);Gr=i(Um,"NVIDIA Tesla T4"),Um.forEach(l),kr=m(cl),js=a(cl,"TD",{});var vm=o(js);$r=i(vm,"3.5it/s"),vm.forEach(l),Wr=m(cl),Es=a(cl,"TD",{});var _m=o(Es);Vr=i(_m,"5.5it/s"),_m.forEach(l),cl.forEach(l),Sr=m(E),pe=a(E,"TR",{});var hl=o(pe);Is=a(hl,"TD",{});var jm=o(Is);Rr=i(jm,"NVIDIA 3060 RTX"),jm.forEach(l),Ar=m(hl),gs=a(hl,"TD",{});var Em=o(gs);Xr=i(Em,"4.6it/s"),Em.forEach(l),Nr=m(hl),Zs=a(hl,"TD",{});var Im=o(Zs);Yr=i(Im,"7.8it/s"),Im.forEach(l),hl.forEach(l),Fr=m(E),fe=a(E,"TR",{});var dl=o(fe);Bs=a(dl,"TD",{});var gm=o(Bs);Qr=i(gm,"NVIDIA A10G"),gm.forEach(l),xr=m(dl),Cs=a(dl,"TD",{});var Zm=o(Cs);Dr=i(Zm,"8.88it/s"),Zm.forEach(l),zr=m(dl),Gs=a(dl,"TD",{});var Bm=o(Gs);Hr=i(Bm,"15.6it/s"),Bm.forEach(l),dl.forEach(l),Pr=m(E),me=a(E,"TR",{});var ul=o(me);ks=a(ul,"TD",{});var Cm=o(ks);qr=i(Cm,"NVIDIA RTX A6000"),Cm.forEach(l),Kr=m(ul),$s=a(ul,"TD",{});var Gm=o($s);Lr=i(Gm,"11.7it/s"),Gm.forEach(l),Or=m(ul),Ws=a(ul,"TD",{});var km=o(Ws);ep=i(km,"21.09it/s"),km.forEach(l),ul.forEach(l),tp=m(E),ce=a(E,"TR",{});var yl=o(ce);Vs=a(yl,"TD",{});var $m=o(Vs);lp=i($m,"NVIDIA TITAN RTX"),$m.forEach(l),sp=m(yl),Ss=a(yl,"TD",{});var Wm=o(Ss);ap=i(Wm,"12.51it/s"),Wm.forEach(l),op=m(yl),Rs=a(yl,"TD",{});var Vm=o(Rs);np=i(Vm,"18.22it/s"),Vm.forEach(l),yl.forEach(l),ip=m(E),he=a(E,"TR",{});var wl=o(he);As=a(wl,"TD",{});var Sm=o(As);rp=i(Sm,"A100-SXM4-40GB"),Sm.forEach(l),pp=m(wl),Xs=a(wl,"TD",{});var Rm=o(Xs);fp=i(Rm,"18.6it/s"),Rm.forEach(l),mp=m(wl),Ns=a(wl,"TD",{});var Am=o(Ns);cp=i(Am,"29.it/s"),Am.forEach(l),wl.forEach(l),hp=m(E),de=a(E,"TR",{});var Ml=o(de);Ys=a(Ml,"TD",{});var Xm=o(Ys);dp=i(Xm,"A100-SXM-80GB"),Xm.forEach(l),up=m(Ml),Fs=a(Ml,"TD",{});var Nm=o(Fs);yp=i(Nm,"18.7it/s"),Nm.forEach(l),wp=m(Ml),Qs=a(Ml,"TD",{});var Ym=o(Qs);Mp=i(Ym,"29.5it/s"),Ym.forEach(l),Ml.forEach(l),E.forEach(l),Vo.forEach(l),fo=m(e),Kt=a(e,"P",{});var Fm=o(Kt);bp=i(Fm,"To leverage it just make sure you have:"),Fm.forEach(l),mo=m(e),w(Ne.$$.fragment,e),co=m(e),N=a(e,"UL",{});var bl=o(N);xs=a(bl,"LI",{});var Qm=o(xs);Jp=i(Qm,"PyTorch > 1.12"),Qm.forEach(l),Tp=m(bl),Ds=a(bl,"LI",{});var xm=o(Ds);Up=i(xm,"Cuda available"),xm.forEach(l),vp=m(bl),Lt=a(bl,"LI",{});var Ip=o(Lt);Ot=a(Ip,"A",{href:!0});var Dm=o(Ot);_p=i(Dm,"Installed the xformers library"),Dm.forEach(l),jp=i(Ip,"."),Ip.forEach(l),bl.forEach(l),ho=m(e),w(Mt.$$.fragment,e),this.h()},h(){c(h,"name","hf:doc:metadata"),c(h,"content",JSON.stringify(ac)),c(U,"id","memory-and-speed"),c(U,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(U,"href","#memory-and-speed"),c(u,"class","relative group"),c(Fe,"href","https://github.com/facebookresearch/xformers"),c(Fe,"rel","nofollow"),c(Ut,"href","xformers"),c(we,"id","use-tf32-instead-of-fp32-on-ampere-and-later-cuda-devices"),c(we,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(we,"href","#use-tf32-instead-of-fp32-on-ampere-and-later-cuda-devices"),c(q,"class","relative group"),c(xe,"href","https://huggingface.co/docs/transformers/v4.18.0/en/performance#tf32"),c(xe,"rel","nofollow"),c(be,"id","half-precision-weights"),c(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"),c(be,"href","#half-precision-weights"),c(K,"class","relative group"),c(Te,"id","sliced-attention-for-additional-memory-savings"),c(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"),c(Te,"href","#sliced-attention-for-additional-memory-savings"),c(L,"class","relative group"),c(Et,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/upscale#diffusers.StableDiffusionUpscalePipeline.enable_attention_slicing"),c(_e,"id","sliced-vae-decode-for-larger-batches"),c(_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"),c(_e,"href","#sliced-vae-decode-for-larger-batches"),c(O,"class","relative group"),c(Zt,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/upscale#diffusers.StableDiffusionUpscalePipeline.enable_attention_slicing"),c(Bt,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/upscale#diffusers.StableDiffusionUpscalePipeline.enable_xformers_memory_efficient_attention"),c(Ct,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.enable_vae_slicing"),c(Ee,"id","tiled-vae-decode-and-encode-for-large-images"),c(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"),c(Ee,"href","#tiled-vae-decode-and-encode-for-large-images"),c(ee,"class","relative group"),c($t,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/upscale#diffusers.StableDiffusionUpscalePipeline.enable_attention_slicing"),c(Wt,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/upscale#diffusers.StableDiffusionUpscalePipeline.enable_xformers_memory_efficient_attention"),c(Vt,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.enable_vae_tiling"),c(Rt,"name","sequential_offloading"),c(ge,"id","offloading-to-cpu-with-accelerate-for-memory-savings"),c(ge,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(ge,"href","#offloading-to-cpu-with-accelerate-for-memory-savings"),c(te,"class","relative group"),c(Xt,"href","/docs/diffusers/v0.18.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.enable_sequential_cpu_offload"),c(at,"href","https://github.com/huggingface/diffusers/issues/1934"),c(at,"rel","nofollow"),c(Ft,"name","model_offloading"),c(Ge,"id","model-offloading-for-fast-inference-and-memory-savings"),c(Ge,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(Ge,"href","#model-offloading-for-fast-inference-and-memory-savings"),c(se,"class","relative group"),c(Qt,"href","#sequential_offloading"),c(pt,"href","https://huggingface.co/docs/accelerate/v0.18.0/en/package_reference/big_modeling#accelerate.hooks.remove_hook_from_module"),c(pt,"rel","nofollow"),c(We,"id","using-channels-last-memory-format"),c(We,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(We,"href","#using-channels-last-memory-format"),c(ae,"class","relative group"),c(Ve,"id","tracing"),c(Ve,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),c(Ve,"href","#tracing"),c(oe,"class","relative group"),c(Ae,"id","memory-efficient-attention"),c(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"),c(Ae,"href","#memory-efficient-attention"),c(ne,"class","relative group"),c(yt,"href","https://github.com/HazyResearch/flash-attention"),c(yt,"rel","nofollow"),c(wt,"href","https://arxiv.org/pdf/2205.14135.pdf"),c(wt,"rel","nofollow"),c(Ot,"href","xformers")},m(e,r){t(document.head,h),p(e,d,r),p(e,u,r),t(u,U),t(U,v),M(_,v,null),t(u,F),t(u,C),t(C,Tt),p(e,Ye,r),p(e,G,r),t(G,So),t(G,Jl),t(Jl,Ro),t(G,Ao),t(G,Fe),t(Fe,Xo),t(G,No),t(G,Ut),t(Ut,Yo),t(G,Fo),p(e,Ls,r),p(e,vt,r),t(vt,Qo),p(e,Os,r),p(e,ye,r),t(ye,Tl),t(Tl,Q),t(Q,ea),t(Q,xo),t(Q,Ul),t(Ul,Do),t(Q,zo),t(Q,vl),t(vl,Ho),t(ye,Po),t(ye,I),t(I,x),t(x,_l),t(_l,qo),t(x,Ko),t(x,jl),t(jl,Lo),t(x,Oo),t(x,El),t(El,en),t(I,tn),t(I,D),t(D,Il),t(Il,ln),t(D,sn),t(D,gl),t(gl,an),t(D,on),t(D,Zl),t(Zl,nn),t(I,rn),t(I,z),t(z,Bl),t(Bl,pn),t(z,fn),t(z,Cl),t(Cl,mn),t(z,cn),t(z,Gl),t(Gl,hn),t(I,dn),t(I,H),t(H,kl),t(kl,un),t(H,yn),t(H,$l),t($l,wn),t(H,Mn),t(H,Wl),t(Wl,bn),t(I,Jn),t(I,P),t(P,Vl),t(Vl,Tn),t(P,Un),t(P,Sl),t(Sl,vn),t(P,_n),t(P,Rl),t(Rl,jn),p(e,ta,r),p(e,_t,r),t(_t,En),p(e,la,r),p(e,q,r),t(q,we),t(we,Al),M(Qe,Al,null),t(q,In),t(q,Xl),t(Xl,gn),p(e,sa,r),p(e,Me,r),t(Me,Zn),t(Me,xe),t(xe,Bn),t(Me,Cn),p(e,aa,r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,ds),M(ct,ds,null),t(oe,pr),t(oe,us),t(us,fr),p(e,to,r),p(e,Se,r),t(Se,mr),t(Se,ys),t(ys,cr),t(Se,hr),p(e,lo,r),p(e,Pt,r),t(Pt,dr),p(e,so,r),M(ht,e,r),p(e,ao,r),p(e,Re,r),t(Re,ur),t(Re,ws),t(ws,yr),t(Re,wr),p(e,oo,r),M(dt,e,r),p(e,no,r),p(e,ne,r),t(ne,Ae),t(Ae,Ms),M(ut,Ms,null),t(ne,Mr),t(ne,bs),t(bs,br),p(e,io,r),p(e,X,r),t(X,Jr),t(X,yt),t(yt,Tr),t(X,Ur),t(X,wt),t(wt,vr),t(X,_r),p(e,ro,r),p(e,qt,r),t(qt,jr),p(e,po,r),p(e,Xe,r),t(Xe,Js),t(Js,ie),t(ie,Ts),t(Ts,Er),t(ie,Ir),t(ie,Us),t(Us,gr),t(ie,Zr),t(ie,vs),t(vs,Br),t(Xe,Cr),t(Xe,j),t(j,re),t(re,_s),t(_s,Gr),t(re,kr),t(re,js),t(js,$r),t(re,Wr),t(re,Es),t(Es,Vr),t(j,Sr),t(j,pe),t(pe,Is),t(Is,Rr),t(pe,Ar),t(pe,gs),t(gs,Xr),t(pe,Nr),t(pe,Zs),t(Zs,Yr),t(j,Fr),t(j,fe),t(fe,Bs),t(Bs,Qr),t(fe,xr),t(fe,Cs),t(Cs,Dr),t(fe,zr),t(fe,Gs),t(Gs,Hr),t(j,Pr),t(j,me),t(me,ks),t(ks,qr),t(me,Kr),t(me,$s),t($s,Lr),t(me,Or),t(me,Ws),t(Ws,ep),t(j,tp),t(j,ce),t(ce,Vs),t(Vs,lp),t(ce,sp),t(ce,Ss),t(Ss,ap),t(ce,op),t(ce,Rs),t(Rs,np),t(j,ip),t(j,he),t(he,As),t(As,rp),t(he,pp),t(he,Xs),t(Xs,fp),t(he,mp),t(he,Ns),t(Ns,cp),t(j,hp),t(j,de),t(de,Ys),t(Ys,dp),t(de,up),t(de,Fs),t(Fs,yp),t(de,wp),t(de,Qs),t(Qs,Mp),p(e,fo,r),p(e,Kt,r),t(Kt,bp),p(e,mo,r),M(Ne,e,r),p(e,co,r),p(e,N,r),t(N,xs),t(xs,Jp),t(N,Tp),t(N,Ds),t(Ds,Up),t(N,vp),t(N,Lt),t(Lt,Ot),t(Ot,_p),t(Lt,jp),p(e,ho,r),M(Mt,e,r),uo=!0},p(e,[r]){const bt={};r&2&&(bt.$$scope={dirty:r,ctx:e}),Je.$set(bt);const zs={};r&2&&(zs.$$scope={dirty:r,ctx:e}),Ue.$set(zs);const Hs={};r&2&&(Hs.$$scope={dirty:r,ctx:e}),Ce.$set(Hs);const Ps={};r&2&&(Ps.$$scope={dirty:r,ctx:e}),$e.$set(Ps);const $={};r&2&&($.$$scope={dirty:r,ctx:e}),Ne.$set($)},i(e){uo||(b(_.$$.fragment,e),b(Qe.$$.fragment,e),b(De.$$.fragment,e),b(ze.$$.fragment,e),b(He.$$.fragment,e),b(Je.$$.fragment,e),b(Pe.$$.fragment,e),b(Ue.$$.fragment,e),b(qe.$$.fragment,e),b(Ke.$$.fragment,e),b(Le.$$.fragment,e),b(Oe.$$.fragment,e),b(et.$$.fragment,e),b(tt.$$.fragment,e),b(lt.$$.fragment,e),b(Ce.$$.fragment,e),b(st.$$.fragment,e),b(ot.$$.fragment,e),b(it.$$.fragment,e),b(rt.$$.fragment,e),b($e.$$.fragment,e),b(ft.$$.fragment,e),b(mt.$$.fragment,e),b(ct.$$.fragment,e),b(ht.$$.fragment,e),b(dt.$$.fragment,e),b(ut.$$.fragment,e),b(Ne.$$.fragment,e),b(Mt.$$.fragment,e),uo=!0)},o(e){J(_.$$.fragment,e),J(Qe.$$.fragment,e),J(De.$$.fragment,e),J(ze.$$.fragment,e),J(He.$$.fragment,e),J(Je.$$.fragment,e),J(Pe.$$.fragment,e),J(Ue.$$.fragment,e),J(qe.$$.fragment,e),J(Ke.$$.fragment,e),J(Le.$$.fragment,e),J(Oe.$$.fragment,e),J(et.$$.fragment,e),J(tt.$$.fragment,e),J(lt.$$.fragment,e),J(Ce.$$.fragment,e),J(st.$$.fragment,e),J(ot.$$.fragment,e),J(it.$$.fragment,e),J(rt.$$.fragment,e),J($e.$$.fragment,e),J(ft.$$.fragment,e),J(mt.$$.fragment,e),J(ct.$$.fragment,e),J(ht.$$.fragment,e),J(dt.$$.fragment,e),J(ut.$$.fragment,e),J(Ne.$$.fragment,e),J(Mt.$$.fragment,e),uo=!1},d(e){l(h),e&&l(d),e&&l(u),T(_),e&&l(Ye),e&&l(G),e&&l(Ls),e&&l(vt),e&&l(Os),e&&l(ye),e&&l(ta),e&&l(_t),e&&l(la),e&&l(q),T(Qe),e&&l(sa),e&&l(Me),e&&l(aa),T(De,e),e&&l(oa),e&&l(K),T(ze),e&&l(na),e&&l(V),e&&l(ia),T(He,e),e&&l(ra),T(Je,e),e&&l(pa),e&&l(L),T(Pe),e&&l(fa),e&&l(jt),e&&l(ma),T(Ue,e),e&&l(ca),e&&l(ve),e&&l(ha),T(qe,e),e&&l(da),e&&l(It),e&&l(ua),e&&l(O),T(Ke),e&&l(ya),e&&l(gt),e&&l(wa),e&&l(S),e&&l(Ma),e&&l(je),e&&l(ba),T(Le,e),e&&l(Ja),e&&l(Gt),e&&l(Ta),e&&l(ee),T(Oe),e&&l(Ua),e&&l(kt),e&&l(va),e&&l(R),e&&l(_a),e&&l(Ie),e&&l(ja),T(et,e),e&&l(Ea),e&&l(St),e&&l(Ia),e&&l(Rt),e&&l(ga),e&&l(te),T(tt),e&&l(Za),e&&l(At),e&&l(Ba),e&&l(Ze),e&&l(Ca),T(lt,e),e&&l(Ga),e&&l(Nt),e&&l(ka),e&&l(Be),e&&l($a),T(Ce,e),e&&l(Wa),e&&l(Yt),e&&l(Va),T(st,e),e&&l(Sa),e&&l(g),e&&l(Ra),e&&l(le),e&&l(Aa),e&&l(Ft),e&&l(Xa),e&&l(se),T(ot),e&&l(Na),e&&l(nt),e&&l(Ya),e&&l(A),e&&l(Fa),e&&l(xt),e&&l(Qa),e&&l(ke),e&&l(xa),T(it,e),e&&l(Da),e&&l(Dt),e&&l(za),T(rt,e),e&&l(Ha),T($e,e),e&&l(Pa),e&&l(k),e&&l(qa),e&&l(ae),T(ft),e&&l(Ka),e&&l(zt),e&&l(La),e&&l(Ht),e&&l(Oa),T(mt,e),e&&l(eo),e&&l(oe),T(ct),e&&l(to),e&&l(Se),e&&l(lo),e&&l(Pt),e&&l(so),T(ht,e),e&&l(ao),e&&l(Re),e&&l(oo),T(dt,e),e&&l(no),e&&l(ne),T(ut),e&&l(io),e&&l(X),e&&l(ro),e&&l(qt),e&&l(po),e&&l(Xe),e&&l(fo),e&&l(Kt),e&&l(mo),T(Ne,e),e&&l(co),e&&l(N),e&&l(ho),T(Mt,e)}}}const ac={local:"memory-and-speed",sections:[{local:null,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:null},{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:"tiled-vae-decode-and-encode-for-large-images",title:"Tiled VAE decode and encode for large images"},{local:"offloading-to-cpu-with-accelerate-for-memory-savings",title:"Offloading to CPU with accelerate for memory savings"},{local:"model-offloading-for-fast-inference-and-memory-savings",title:"Model offloading for fast inference and 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 oc(B){return Km(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class fc extends zm{constructor(h){super();Hm(this,h,oc,sc,Pm,{})}}export{fc as default,ac as metadata};
