import{s as Te,o as ge,n as we}from"../chunks/scheduler.182ea377.js";import{S as ye,i as be,g as r,s as i,r as d,A as Me,h as p,f as n,c as l,j as ue,u,x as f,k as ce,y as Je,a as s,v as c,d as h,t as $,w}from"../chunks/index.abf12888.js";import{T as he}from"../chunks/Tip.230e2334.js";import{C as $e}from"../chunks/CodeBlock.57fe6e13.js";import{H as ne,E as ve}from"../chunks/EditOnGithub.9b8e78e4.js";function _e(Z){let a,m='In many cases, optimizing for speed or memory leads to improved performance in the other, so you should try to optimize for both whenever you can. This guide focuses on inference speed, but you can learn more about preserving memory in the <a href="memory">Reduce memory usage</a> guide.';return{c(){a=r("p"),a.innerHTML=m},l(o){a=p(o,"P",{"data-svelte-h":!0}),f(a)!=="svelte-1dpd5a8"&&(a.innerHTML=m)},m(o,T){s(o,a,T)},p:we,d(o){o&&n(a)}}}function Ue(Z){let a,m='Don’t use <a href="https://pytorch.org/docs/stable/amp.html#torch.autocast" rel="nofollow"><code>torch.autocast</code></a> in any of the pipelines as it can lead to black images and is always slower than pure float16 precision.';return{c(){a=r("p"),a.innerHTML=m},l(o){a=p(o,"P",{"data-svelte-h":!0}),f(a)!=="svelte-19ow5yd"&&(a.innerHTML=m)},m(o,T){s(o,a,T)},p:we,d(o){o&&n(a)}}}function xe(Z){let a,m,o,T,b,B,M,se='There are several ways to optimize 🤗 Diffusers for inference speed. As a general rule of thumb, we recommend using either <a href="xformers">xFormers</a> or <code>torch.nn.functional.scaled_dot_product_attention</code> in PyTorch 2.0 for their memory-efficient attention.',D,g,S,J,ae="The results below are obtained from generating a single 512x512 image from the prompt <code>a photo of an astronaut riding a horse on mars</code> with 50 DDIM steps on a Nvidia Titan RTX, demonstrating the speed-up you can expect.",k,v,ie="<thead><tr><th></th> <th>latency</th> <th>speed-up</th></tr></thead> <tbody><tr><td>original</td> <td>9.50s</td> <td>x1</td></tr> <tr><td>fp16</td> <td>3.61s</td> <td>x2.63</td></tr> <tr><td>channels last</td> <td>3.30s</td> <td>x2.88</td></tr> <tr><td>traced UNet</td> <td>3.21s</td> <td>x2.96</td></tr> <tr><td>memory efficient attention</td> <td>2.63s</td> <td>x3.61</td></tr></tbody>",F,_,z,U,le='On Ampere and later CUDA devices, matrix multiplications and convolutions can use the <a href="https://blogs.nvidia.com/blog/2020/05/14/tensorfloat-32-precision-format/" rel="nofollow">TensorFloat-32 (TF32)</a> mode for faster, but slightly less accurate computations. By default, PyTorch enables TF32 mode for convolutions but not matrix multiplications. Unless your network requires full float32 precision, we recommend enabling TF32 for matrix multiplications. It can significantly speeds up computations with typically negligible loss in numerical accuracy.',R,x,N,C,oe='You can learn more about TF32 in the <a href="https://huggingface.co/docs/transformers/en/perf_train_gpu_one#tf32" rel="nofollow">Mixed precision training</a> guide.',V,I,X,H,re="To save GPU memory and get more speed, try loading and running the model weights directly in half-precision or float16:",q,j,Y,y,Q,E,A,G,pe="You could also use a distilled Stable Diffusion model and autoencoder to speed up inference. During distillation, many of the UNet’s residual and attention blocks are shed to reduce the model size. The distilled model is faster and uses less memory while generating images of comparable quality to the full Stable Diffusion model.",K,L,me='Learn more about in the <a href="../using-diffusers/distilled_sd">Distilled Stable Diffusion inference</a> guide!',O,P,ee,W,te;return b=new ne({props:{title:"Speed up inference",local:"speed-up-inference",headingTag:"h1"}}),g=new he({props:{$$slots:{default:[_e]},$$scope:{ctx:Z}}}),_=new ne({props:{title:"Use TensorFloat-32",local:"use-tensorfloat-32",headingTag:"h2"}}),x=new $e({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEF0b3JjaC5iYWNrZW5kcy5jdWRhLm1hdG11bC5hbGxvd190ZjMyJTIwJTNEJTIwVHJ1ZQ==",highlighted:`<span class="hljs-keyword">import</span> torch

torch.backends.cuda.matmul.allow_tf32 = <span class="hljs-literal">True</span>`,wrap:!1}}),I=new ne({props:{title:"Half-precision weights",local:"half-precision-weights",headingTag:"h2"}}),j=new $e({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBJTIwJTIwJTIwJTIwdXNlX3NhZmV0ZW5zb3JzJTNEVHJ1ZSUyQyUwQSklMEFwaXBlJTIwJTNEJTIwcGlwZS50byglMjJjdWRhJTIyKSUwQSUwQXByb21wdCUyMCUzRCUyMCUyMmElMjBwaG90byUyMG9mJTIwYW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyMG1hcnMlMjIlMEFpbWFnZSUyMCUzRCUyMHBpcGUocHJvbXB0KS5pbWFnZXMlNUIwJTVE",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,
    use_safetensors=<span class="hljs-literal">True</span>,
)
pipe = pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>
image = pipe(prompt).images[<span class="hljs-number">0</span>]`,wrap:!1}}),y=new he({props:{warning:!0,$$slots:{default:[Ue]},$$scope:{ctx:Z}}}),E=new ne({props:{title:"Distilled model",local:"distilled-model",headingTag:"h2"}}),P=new ve({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/fp16.md"}}),{c(){a=r("meta"),m=i(),o=r("p"),T=i(),d(b.$$.fragment),B=i(),M=r("p"),M.innerHTML=se,D=i(),d(g.$$.fragment),S=i(),J=r("p"),J.innerHTML=ae,k=i(),v=r("table"),v.innerHTML=ie,F=i(),d(_.$$.fragment),z=i(),U=r("p"),U.innerHTML=le,R=i(),d(x.$$.fragment),N=i(),C=r("p"),C.innerHTML=oe,V=i(),d(I.$$.fragment),X=i(),H=r("p"),H.textContent=re,q=i(),d(j.$$.fragment),Y=i(),d(y.$$.fragment),Q=i(),d(E.$$.fragment),A=i(),G=r("p"),G.textContent=pe,K=i(),L=r("p"),L.innerHTML=me,O=i(),d(P.$$.fragment),ee=i(),W=r("p"),this.h()},l(e){const t=Me("svelte-u9bgzb",document.head);a=p(t,"META",{name:!0,content:!0}),t.forEach(n),m=l(e),o=p(e,"P",{}),ue(o).forEach(n),T=l(e),u(b.$$.fragment,e),B=l(e),M=p(e,"P",{"data-svelte-h":!0}),f(M)!=="svelte-1pehmy1"&&(M.innerHTML=se),D=l(e),u(g.$$.fragment,e),S=l(e),J=p(e,"P",{"data-svelte-h":!0}),f(J)!=="svelte-1iu9895"&&(J.innerHTML=ae),k=l(e),v=p(e,"TABLE",{"data-svelte-h":!0}),f(v)!=="svelte-1osaemz"&&(v.innerHTML=ie),F=l(e),u(_.$$.fragment,e),z=l(e),U=p(e,"P",{"data-svelte-h":!0}),f(U)!=="svelte-sgc3mn"&&(U.innerHTML=le),R=l(e),u(x.$$.fragment,e),N=l(e),C=p(e,"P",{"data-svelte-h":!0}),f(C)!=="svelte-ahb0o9"&&(C.innerHTML=oe),V=l(e),u(I.$$.fragment,e),X=l(e),H=p(e,"P",{"data-svelte-h":!0}),f(H)!=="svelte-rv9wu9"&&(H.textContent=re),q=l(e),u(j.$$.fragment,e),Y=l(e),u(y.$$.fragment,e),Q=l(e),u(E.$$.fragment,e),A=l(e),G=p(e,"P",{"data-svelte-h":!0}),f(G)!=="svelte-yfaonv"&&(G.textContent=pe),K=l(e),L=p(e,"P",{"data-svelte-h":!0}),f(L)!=="svelte-1f6kjv7"&&(L.innerHTML=me),O=l(e),u(P.$$.fragment,e),ee=l(e),W=p(e,"P",{}),ue(W).forEach(n),this.h()},h(){ce(a,"name","hf:doc:metadata"),ce(a,"content",Ce)},m(e,t){Je(document.head,a),s(e,m,t),s(e,o,t),s(e,T,t),c(b,e,t),s(e,B,t),s(e,M,t),s(e,D,t),c(g,e,t),s(e,S,t),s(e,J,t),s(e,k,t),s(e,v,t),s(e,F,t),c(_,e,t),s(e,z,t),s(e,U,t),s(e,R,t),c(x,e,t),s(e,N,t),s(e,C,t),s(e,V,t),c(I,e,t),s(e,X,t),s(e,H,t),s(e,q,t),c(j,e,t),s(e,Y,t),c(y,e,t),s(e,Q,t),c(E,e,t),s(e,A,t),s(e,G,t),s(e,K,t),s(e,L,t),s(e,O,t),c(P,e,t),s(e,ee,t),s(e,W,t),te=!0},p(e,[t]){const fe={};t&2&&(fe.$$scope={dirty:t,ctx:e}),g.$set(fe);const de={};t&2&&(de.$$scope={dirty:t,ctx:e}),y.$set(de)},i(e){te||(h(b.$$.fragment,e),h(g.$$.fragment,e),h(_.$$.fragment,e),h(x.$$.fragment,e),h(I.$$.fragment,e),h(j.$$.fragment,e),h(y.$$.fragment,e),h(E.$$.fragment,e),h(P.$$.fragment,e),te=!0)},o(e){$(b.$$.fragment,e),$(g.$$.fragment,e),$(_.$$.fragment,e),$(x.$$.fragment,e),$(I.$$.fragment,e),$(j.$$.fragment,e),$(y.$$.fragment,e),$(E.$$.fragment,e),$(P.$$.fragment,e),te=!1},d(e){e&&(n(m),n(o),n(T),n(B),n(M),n(D),n(S),n(J),n(k),n(v),n(F),n(z),n(U),n(R),n(N),n(C),n(V),n(X),n(H),n(q),n(Y),n(Q),n(A),n(G),n(K),n(L),n(O),n(ee),n(W)),n(a),w(b,e),w(g,e),w(_,e),w(x,e),w(I,e),w(j,e),w(y,e),w(E,e),w(P,e)}}}const Ce='{"title":"Speed up inference","local":"speed-up-inference","sections":[{"title":"Use TensorFloat-32","local":"use-tensorfloat-32","sections":[],"depth":2},{"title":"Half-precision weights","local":"half-precision-weights","sections":[],"depth":2},{"title":"Distilled model","local":"distilled-model","sections":[],"depth":2}],"depth":1}';function Ie(Z){return ge(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Pe extends ye{constructor(a){super(),be(this,a,Ie,xe,Te,{})}}export{Pe as component};
