Buckets:

rtrm's picture
download
raw
16.7 kB
import{s as Le,o as Ye,n as ve}from"../chunks/scheduler.8c3d61f6.js";import{S as Qe,i as qe,g as r,s as a,r as f,A as Fe,h as d,f as s,c as i,j as De,u,x as p,k as Je,y as Pe,a as l,v as c,d as h,t as y,w as g}from"../chunks/index.da70eac4.js";import{T as Ue}from"../chunks/Tip.1d9b8c37.js";import{C as Ze}from"../chunks/CodeBlock.00a903b3.js";import{H as q,E as Ae}from"../chunks/EditOnGithub.1e64e623.js";function Ke(T){let n,m='Optimizing for inference speed or reduced memory usage can lead to improved performance in the other category, so you should try to optimize for both whenever you can. This guide focuses on inference speed, but you can learn more about lowering memory usage in the <a href="memory">Reduce memory usage</a> guide.';return{c(){n=r("p"),n.innerHTML=m},l(o){n=d(o,"P",{"data-svelte-h":!0}),p(n)!=="svelte-1wnb177"&&(n.innerHTML=m)},m(o,w){l(o,n,w)},p:ve,d(o){o&&s(n)}}}function Oe(T){let n,m='Don’t use <a href="https://pytorch.org/docs/stable/amp.html#torch.autocast" rel="nofollow">torch.autocast</a> in any of the pipelines as it can lead to black images and is always slower than pure float16 precision.';return{c(){n=r("p"),n.innerHTML=m},l(o){n=d(o,"P",{"data-svelte-h":!0}),p(n)!=="svelte-148gqwg"&&(n.innerHTML=m)},m(o,w){l(o,n,w)},p:ve,d(o){o&&s(n)}}}function et(T){let n,m='Read the <a href="https://huggingface.co/blog/sd_distillation" rel="nofollow">Open-sourcing Knowledge Distillation Code and Weights of SD-Small and SD-Tiny</a> blog post to learn more about how knowledge distillation training works to produce a faster, smaller, and cheaper generative model.';return{c(){n=r("p"),n.innerHTML=m},l(o){n=d(o,"P",{"data-svelte-h":!0}),p(n)!=="svelte-11ogmeq"&&(n.innerHTML=m)},m(o,w){l(o,n,w)},p:ve,d(o){o&&s(n)}}}function tt(T){let n,m,o,w,U,F,v,xe='There are several ways to optimize Diffusers for inference speed, such as reducing the computational burden by lowering the data precision or using a lightweight distilled model. There are also memory-efficient attention implementations, <a href="xformers">xFormers</a> and <a href="https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html" rel="nofollow">scaled dot product attention</a> in PyTorch 2.0, that reduce memory usage which also indirectly speeds up inference. Different speed optimizations can be stacked together to get the fastest inference times.',P,b,A,x,Ge="The inference times below are obtained from generating a single 512x512 image from the prompt “a photo of an astronaut riding a horse on mars” with 50 DDIM steps on a NVIDIA A100.",K,G,_e="<thead><tr><th>setup</th> <th>latency</th> <th>speed-up</th></tr></thead> <tbody><tr><td>baseline</td> <td>5.27s</td> <td>x1</td></tr> <tr><td>tf32</td> <td>4.14s</td> <td>x1.27</td></tr> <tr><td>fp16</td> <td>3.51s</td> <td>x1.50</td></tr> <tr><td>combined</td> <td>3.41s</td> <td>x1.54</td></tr></tbody>",O,_,ee,I,Ie='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 speed up computations with typically negligible loss in numerical accuracy.',te,j,se,W,je='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.',le,V,ne,C,We="To save GPU memory and get more speed, set <code>torch_dtype=torch.float16</code> to load and run the model weights directly with half-precision weights.",ae,E,ie,M,oe,k,re,S,Ve="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 by 51% and improve latency on CPU/GPU by 43%. The distilled model is faster and uses less memory while generating images of comparable quality to the full Stable Diffusion model.",de,$,pe,X,Ce='The inference times below are obtained from generating 4 images from the prompt “a photo of an astronaut riding a horse on mars” with 25 PNDM steps on a NVIDIA A100. Each generation is repeated 3 times with the distilled Stable Diffusion v1.4 model by <a href="https://hf.co/nota-ai" rel="nofollow">Nota AI</a>.',me,H,Ee="<thead><tr><th>setup</th> <th>latency</th> <th>speed-up</th></tr></thead> <tbody><tr><td>baseline</td> <td>6.37s</td> <td>x1</td></tr> <tr><td>distilled</td> <td>4.18s</td> <td>x1.52</td></tr> <tr><td>distilled + tiny autoencoder</td> <td>3.83s</td> <td>x1.66</td></tr></tbody>",fe,R,ke="Let’s load the distilled Stable Diffusion model and compare it against the original Stable Diffusion model.",ue,B,ce,J,Se='<div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/original_sd.png"/> <figcaption class="mt-2 text-center text-sm text-gray-500">original Stable Diffusion</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/distilled_sd.png"/> <figcaption class="mt-2 text-center text-sm text-gray-500">distilled Stable Diffusion</figcaption></div>',he,z,ye,N,Xe='To speed inference up even more, replace the autoencoder with a <a href="https://huggingface.co/sayakpaul/taesdxl-diffusers" rel="nofollow">distilled version</a> of it.',ge,D,we,Z,He='<div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/distilled_sd_vae.png"/> <figcaption class="mt-2 text-center text-sm text-gray-500">distilled Stable Diffusion + Tiny AutoEncoder</figcaption></div>',Te,L,Re='More tiny autoencoder models for other Stable Diffusion models, like Stable Diffusion 3, are available from <a href="https://huggingface.co/madebyollin" rel="nofollow">madebyollin</a>.',be,Y,Me,Q,$e;return U=new q({props:{title:"Speed up inference",local:"speed-up-inference",headingTag:"h1"}}),b=new Ue({props:{warning:!1,$$slots:{default:[Ke]},$$scope:{ctx:T}}}),_=new q({props:{title:"TensorFloat-32",local:"tensorfloat-32",headingTag:"h2"}}),j=new Ze({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}}),V=new q({props:{title:"Half-precision weights",local:"half-precision-weights",headingTag:"h2"}}),E=new Ze({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlJTIwJTNEJTIwRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUwQSUyMCUyMCUyMCUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUwQSUyMCUyMCUyMCUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEpJTBBcGlwZSUyMCUzRCUyMHBpcGUudG8oJTIyY3VkYSUyMik=",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;stable-diffusion-v1-5/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>)`,wrap:!1}}),M=new Ue({props:{warning:!0,$$slots:{default:[Oe]},$$scope:{ctx:T}}}),k=new q({props:{title:"Distilled model",local:"distilled-model",headingTag:"h2"}}),$=new Ue({props:{warning:!1,$$slots:{default:[et]},$$scope:{ctx:T}}}),B=new Ze({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEElMEFkaXN0aWxsZWQlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIybm90YS1haSUyRmJrLXNkbS1zbWFsbCUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEpLnRvKCUyMmN1ZGElMjIpJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMGdvbGRlbiUyMHZhc2UlMjB3aXRoJTIwZGlmZmVyZW50JTIwZmxvd2VycyUyMiUwQWdlbmVyYXRvciUyMCUzRCUyMHRvcmNoLm1hbnVhbF9zZWVkKDIwMjMpJTBBaW1hZ2UlMjAlM0QlMjBkaXN0aWxsZWQoJTIyYSUyMGdvbGRlbiUyMHZhc2UlMjB3aXRoJTIwZGlmZmVyZW50JTIwZmxvd2VycyUyMiUyQyUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QyNSUyQyUyMGdlbmVyYXRvciUzRGdlbmVyYXRvcikuaW1hZ2VzJTVCMCU1RCUwQWltYWdl",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> torch
distilled = StableDiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;nota-ai/bk-sdm-small&quot;</span>, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
prompt = <span class="hljs-string">&quot;a golden vase with different flowers&quot;</span>
generator = torch.manual_seed(<span class="hljs-number">2023</span>)
image = distilled(<span class="hljs-string">&quot;a golden vase with different flowers&quot;</span>, num_inference_steps=<span class="hljs-number">25</span>, generator=generator).images[<span class="hljs-number">0</span>]
image`,wrap:!1}}),z=new q({props:{title:"Tiny AutoEncoder",local:"tiny-autoencoder",headingTag:"h3"}}),D=new Ze({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b2VuY29kZXJUaW55JTJDJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFkaXN0aWxsZWQlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIybm90YS1haSUyRmJrLXNkbS1zbWFsbCUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEpLnRvKCUyMmN1ZGElMjIpJTBBZGlzdGlsbGVkLnZhZSUyMCUzRCUyMEF1dG9lbmNvZGVyVGlueS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyc2F5YWtwYXVsJTJGdGFlc2QtZGlmZnVzZXJzJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTIwdXNlX3NhZmV0ZW5zb3JzJTNEVHJ1ZSUyQyUwQSkudG8oJTIyY3VkYSUyMiklMEElMEFwcm9tcHQlMjAlM0QlMjAlMjJhJTIwZ29sZGVuJTIwdmFzZSUyMHdpdGglMjBkaWZmZXJlbnQlMjBmbG93ZXJzJTIyJTBBZ2VuZXJhdG9yJTIwJTNEJTIwdG9yY2gubWFudWFsX3NlZWQoMjAyMyklMEFpbWFnZSUyMCUzRCUyMGRpc3RpbGxlZCglMjJhJTIwZ29sZGVuJTIwdmFzZSUyMHdpdGglMjBkaWZmZXJlbnQlMjBmbG93ZXJzJTIyJTJDJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRDI1JTJDJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yKS5pbWFnZXMlNUIwJTVEJTBBaW1hZ2U=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoencoderTiny, StableDiffusionPipeline
distilled = StableDiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;nota-ai/bk-sdm-small&quot;</span>, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
distilled.vae = AutoencoderTiny.from_pretrained(
<span class="hljs-string">&quot;sayakpaul/taesd-diffusers&quot;</span>, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
prompt = <span class="hljs-string">&quot;a golden vase with different flowers&quot;</span>
generator = torch.manual_seed(<span class="hljs-number">2023</span>)
image = distilled(<span class="hljs-string">&quot;a golden vase with different flowers&quot;</span>, num_inference_steps=<span class="hljs-number">25</span>, generator=generator).images[<span class="hljs-number">0</span>]
image`,wrap:!1}}),Y=new Ae({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/fp16.md"}}),{c(){n=r("meta"),m=a(),o=r("p"),w=a(),f(U.$$.fragment),F=a(),v=r("p"),v.innerHTML=xe,P=a(),f(b.$$.fragment),A=a(),x=r("p"),x.textContent=Ge,K=a(),G=r("table"),G.innerHTML=_e,O=a(),f(_.$$.fragment),ee=a(),I=r("p"),I.innerHTML=Ie,te=a(),f(j.$$.fragment),se=a(),W=r("p"),W.innerHTML=je,le=a(),f(V.$$.fragment),ne=a(),C=r("p"),C.innerHTML=We,ae=a(),f(E.$$.fragment),ie=a(),f(M.$$.fragment),oe=a(),f(k.$$.fragment),re=a(),S=r("p"),S.textContent=Ve,de=a(),f($.$$.fragment),pe=a(),X=r("p"),X.innerHTML=Ce,me=a(),H=r("table"),H.innerHTML=Ee,fe=a(),R=r("p"),R.textContent=ke,ue=a(),f(B.$$.fragment),ce=a(),J=r("div"),J.innerHTML=Se,he=a(),f(z.$$.fragment),ye=a(),N=r("p"),N.innerHTML=Xe,ge=a(),f(D.$$.fragment),we=a(),Z=r("div"),Z.innerHTML=He,Te=a(),L=r("p"),L.innerHTML=Re,be=a(),f(Y.$$.fragment),Me=a(),Q=r("p"),this.h()},l(e){const t=Fe("svelte-u9bgzb",document.head);n=d(t,"META",{name:!0,content:!0}),t.forEach(s),m=i(e),o=d(e,"P",{}),De(o).forEach(s),w=i(e),u(U.$$.fragment,e),F=i(e),v=d(e,"P",{"data-svelte-h":!0}),p(v)!=="svelte-iqdw6c"&&(v.innerHTML=xe),P=i(e),u(b.$$.fragment,e),A=i(e),x=d(e,"P",{"data-svelte-h":!0}),p(x)!=="svelte-1866xsh"&&(x.textContent=Ge),K=i(e),G=d(e,"TABLE",{"data-svelte-h":!0}),p(G)!=="svelte-pydj5"&&(G.innerHTML=_e),O=i(e),u(_.$$.fragment,e),ee=i(e),I=d(e,"P",{"data-svelte-h":!0}),p(I)!=="svelte-zhyvo6"&&(I.innerHTML=Ie),te=i(e),u(j.$$.fragment,e),se=i(e),W=d(e,"P",{"data-svelte-h":!0}),p(W)!=="svelte-cmdptw"&&(W.innerHTML=je),le=i(e),u(V.$$.fragment,e),ne=i(e),C=d(e,"P",{"data-svelte-h":!0}),p(C)!=="svelte-1qb96mj"&&(C.innerHTML=We),ae=i(e),u(E.$$.fragment,e),ie=i(e),u(M.$$.fragment,e),oe=i(e),u(k.$$.fragment,e),re=i(e),S=d(e,"P",{"data-svelte-h":!0}),p(S)!=="svelte-1b1l0wp"&&(S.textContent=Ve),de=i(e),u($.$$.fragment,e),pe=i(e),X=d(e,"P",{"data-svelte-h":!0}),p(X)!=="svelte-1h5auas"&&(X.innerHTML=Ce),me=i(e),H=d(e,"TABLE",{"data-svelte-h":!0}),p(H)!=="svelte-a4bqm9"&&(H.innerHTML=Ee),fe=i(e),R=d(e,"P",{"data-svelte-h":!0}),p(R)!=="svelte-1jin96g"&&(R.textContent=ke),ue=i(e),u(B.$$.fragment,e),ce=i(e),J=d(e,"DIV",{class:!0,"data-svelte-h":!0}),p(J)!=="svelte-1drjdrr"&&(J.innerHTML=Se),he=i(e),u(z.$$.fragment,e),ye=i(e),N=d(e,"P",{"data-svelte-h":!0}),p(N)!=="svelte-1nd97xm"&&(N.innerHTML=Xe),ge=i(e),u(D.$$.fragment,e),we=i(e),Z=d(e,"DIV",{class:!0,"data-svelte-h":!0}),p(Z)!=="svelte-5hr8va"&&(Z.innerHTML=He),Te=i(e),L=d(e,"P",{"data-svelte-h":!0}),p(L)!=="svelte-aixrxn"&&(L.innerHTML=Re),be=i(e),u(Y.$$.fragment,e),Me=i(e),Q=d(e,"P",{}),De(Q).forEach(s),this.h()},h(){Je(n,"name","hf:doc:metadata"),Je(n,"content",st),Je(J,"class","flex gap-4"),Je(Z,"class","flex justify-center")},m(e,t){Pe(document.head,n),l(e,m,t),l(e,o,t),l(e,w,t),c(U,e,t),l(e,F,t),l(e,v,t),l(e,P,t),c(b,e,t),l(e,A,t),l(e,x,t),l(e,K,t),l(e,G,t),l(e,O,t),c(_,e,t),l(e,ee,t),l(e,I,t),l(e,te,t),c(j,e,t),l(e,se,t),l(e,W,t),l(e,le,t),c(V,e,t),l(e,ne,t),l(e,C,t),l(e,ae,t),c(E,e,t),l(e,ie,t),c(M,e,t),l(e,oe,t),c(k,e,t),l(e,re,t),l(e,S,t),l(e,de,t),c($,e,t),l(e,pe,t),l(e,X,t),l(e,me,t),l(e,H,t),l(e,fe,t),l(e,R,t),l(e,ue,t),c(B,e,t),l(e,ce,t),l(e,J,t),l(e,he,t),c(z,e,t),l(e,ye,t),l(e,N,t),l(e,ge,t),c(D,e,t),l(e,we,t),l(e,Z,t),l(e,Te,t),l(e,L,t),l(e,be,t),c(Y,e,t),l(e,Me,t),l(e,Q,t),$e=!0},p(e,[t]){const Be={};t&2&&(Be.$$scope={dirty:t,ctx:e}),b.$set(Be);const ze={};t&2&&(ze.$$scope={dirty:t,ctx:e}),M.$set(ze);const Ne={};t&2&&(Ne.$$scope={dirty:t,ctx:e}),$.$set(Ne)},i(e){$e||(h(U.$$.fragment,e),h(b.$$.fragment,e),h(_.$$.fragment,e),h(j.$$.fragment,e),h(V.$$.fragment,e),h(E.$$.fragment,e),h(M.$$.fragment,e),h(k.$$.fragment,e),h($.$$.fragment,e),h(B.$$.fragment,e),h(z.$$.fragment,e),h(D.$$.fragment,e),h(Y.$$.fragment,e),$e=!0)},o(e){y(U.$$.fragment,e),y(b.$$.fragment,e),y(_.$$.fragment,e),y(j.$$.fragment,e),y(V.$$.fragment,e),y(E.$$.fragment,e),y(M.$$.fragment,e),y(k.$$.fragment,e),y($.$$.fragment,e),y(B.$$.fragment,e),y(z.$$.fragment,e),y(D.$$.fragment,e),y(Y.$$.fragment,e),$e=!1},d(e){e&&(s(m),s(o),s(w),s(F),s(v),s(P),s(A),s(x),s(K),s(G),s(O),s(ee),s(I),s(te),s(se),s(W),s(le),s(ne),s(C),s(ae),s(ie),s(oe),s(re),s(S),s(de),s(pe),s(X),s(me),s(H),s(fe),s(R),s(ue),s(ce),s(J),s(he),s(ye),s(N),s(ge),s(we),s(Z),s(Te),s(L),s(be),s(Me),s(Q)),s(n),g(U,e),g(b,e),g(_,e),g(j,e),g(V,e),g(E,e),g(M,e),g(k,e),g($,e),g(B,e),g(z,e),g(D,e),g(Y,e)}}}const st='{"title":"Speed up inference","local":"speed-up-inference","sections":[{"title":"TensorFloat-32","local":"tensorfloat-32","sections":[],"depth":2},{"title":"Half-precision weights","local":"half-precision-weights","sections":[],"depth":2},{"title":"Distilled model","local":"distilled-model","sections":[{"title":"Tiny AutoEncoder","local":"tiny-autoencoder","sections":[],"depth":3}],"depth":2}],"depth":1}';function lt(T){return Ye(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class dt extends Qe{constructor(n){super(),qe(this,n,lt,tt,Le,{})}}export{dt as component};

Xet Storage Details

Size:
16.7 kB
·
Xet hash:
17cb99b241a47ca2a1b5ab5e3536922c4bf6207bc601bc47337c21342651f21c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.