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