import{s as fe,o as Te,n as ye}from"../chunks/scheduler.182ea377.js";import{S as Je,i as Ue,g as o,s as n,r as d,A as we,h as p,f as s,c as a,j as Me,u as m,x as T,k as le,y as be,a as l,v as c,d as M,t as u,w as f}from"../chunks/index.1ea56653.js";import{H as ue,g as he}from"../chunks/Heading.71ac6e9f.js";import{T as ge}from"../chunks/Tip.67abc998.js";import{C as W}from"../chunks/CodeBlock.82c2494b.js";import{D as Ie}from"../chunks/DocNotebookDropdown.b1496a40.js";function je($){let i,w='Read this <a href="https://huggingface.co/blog/sd_distillation" rel="nofollow">blog post</a> to learn more about how knowledge distillation training works to produce a faster, smaller, and cheaper generative model.';return{c(){i=o("p"),i.innerHTML=w},l(r){i=p(r,"P",{"data-svelte-h":!0}),T(i)!=="svelte-gx7uq8"&&(i.innerHTML=w)},m(r,Q){l(r,i,Q)},p:ye,d(r){r&&s(i)}}}function _e($){let i,w,r,Q,b,G,h,k,g,ne='Stable Diffusion inference can be a computationally intensive process because it must iteratively denoise the latents to generate an image. To reduce the computational burden, you can use a <em>distilled</em> version of the Stable Diffusion model from <a href="https://huggingface.co/nota-ai" rel="nofollow">Nota AI</a>. The distilled version of their Stable Diffusion model eliminates some of the residual and attention blocks from the UNet, reducing the model size by 51% and improving latency on CPU/GPU by 43%.',x,y,F,I,ae="Let’s load the distilled Stable Diffusion model and compare it against the original Stable Diffusion model:",X,j,A,_,ie="Given a prompt, get the inference time for the original model:",P,V,H,E,oe="Time the distilled model inference:",Y,Z,q,J,pe='<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 (45781.5 ms)</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 (29884.2 ms)</figcaption></div>',D,R,z,B,re='To speed inference up even more, use a tiny distilled version of the <a href="https://huggingface.co/sayakpaul/taesdxl-diffusers" rel="nofollow">Stable Diffusion VAE</a> to denoise the latents into images. Replace the VAE in the distilled Stable Diffusion model with the tiny VAE:',L,N,K,C,de="Time the distilled model and distilled VAE inference:",O,S,ee,U,me='<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 (27165.7 ms)</figcaption></div>',te,v,se;return b=new ue({props:{title:"Distilled Stable Diffusion inference",local:"distilled-stable-diffusion-inference",headingTag:"h1"}}),h=new Ie({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/distilled_sd.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/distilled_sd.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/distilled_sd.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/distilled_sd.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/pytorch/distilled_sd.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/diffusers_doc/en/tensorflow/distilled_sd.ipynb"}]}}),y=new ge({props:{$$slots:{default:[je]},$$scope:{ctx:$}}}),j=new W({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEElMEFkaXN0aWxsZWQlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIybm90YS1haSUyRmJrLXNkbS1zbWFsbCUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEpLnRvKCUyMmN1ZGElMjIpJTBBJTBBb3JpZ2luYWwlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyQ29tcFZpcyUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNCUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEpLnRvKCUyMmN1ZGElMjIp",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>)

original = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>, torch_dtype=torch.float16, use_safetensors=<span class="hljs-literal">True</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)`,wrap:!1}}),V=new W({props:{code:"aW1wb3J0JTIwdGltZSUwQSUwQXNlZWQlMjAlM0QlMjAyMDIzJTBBZ2VuZXJhdG9yJTIwJTNEJTIwdG9yY2gubWFudWFsX3NlZWQoc2VlZCklMEElMEFOVU1fSVRFUlNfVE9fUlVOJTIwJTNEJTIwMyUwQU5VTV9JTkZFUkVOQ0VfU1RFUFMlMjAlM0QlMjAyNSUwQU5VTV9JTUFHRVNfUEVSX1BST01QVCUyMCUzRCUyMDQlMEElMEFwcm9tcHQlMjAlM0QlMjAlMjJhJTIwZ29sZGVuJTIwdmFzZSUyMHdpdGglMjBkaWZmZXJlbnQlMjBmbG93ZXJzJTIyJTBBJTBBc3RhcnQlMjAlM0QlMjB0aW1lLnRpbWVfbnMoKSUwQWZvciUyMF8lMjBpbiUyMHJhbmdlKE5VTV9JVEVSU19UT19SVU4pJTNBJTBBJTIwJTIwJTIwJTIwaW1hZ2VzJTIwJTNEJTIwb3JpZ2luYWwoJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJvbXB0JTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRE5VTV9JTkZFUkVOQ0VfU1RFUFMlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBnZW5lcmF0b3IlM0RnZW5lcmF0b3IlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1faW1hZ2VzX3Blcl9wcm9tcHQlM0ROVU1fSU1BR0VTX1BFUl9QUk9NUFQlMEElMjAlMjAlMjAlMjApLmltYWdlcyUwQWVuZCUyMCUzRCUyMHRpbWUudGltZV9ucygpJTBBb3JpZ2luYWxfc2QlMjAlM0QlMjBmJTIyJTdCKGVuZCUyMC0lMjBzdGFydCklMjAlMkYlMjAxZTYlM0EuMWYlN0QlMjIlMEElMEFwcmludChmJTIyRXhlY3V0aW9uJTIwdGltZSUyMC0tJTIwJTdCb3JpZ2luYWxfc2QlN0QlMjBtcyU1Q24lMjIpJTBBJTIyRXhlY3V0aW9uJTIwdGltZSUyMC0tJTIwNDU3ODEuNSUyMG1zJTIy",highlighted:`<span class="hljs-keyword">import</span> time

seed = <span class="hljs-number">2023</span>
generator = torch.manual_seed(seed)

NUM_ITERS_TO_RUN = <span class="hljs-number">3</span>
NUM_INFERENCE_STEPS = <span class="hljs-number">25</span>
NUM_IMAGES_PER_PROMPT = <span class="hljs-number">4</span>

prompt = <span class="hljs-string">&quot;a golden vase with different flowers&quot;</span>

start = time.time_ns()
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(NUM_ITERS_TO_RUN):
    images = original(
        prompt,
        num_inference_steps=NUM_INFERENCE_STEPS,
        generator=generator,
        num_images_per_prompt=NUM_IMAGES_PER_PROMPT
    ).images
end = time.time_ns()
original_sd = <span class="hljs-string">f&quot;<span class="hljs-subst">{(end - start) / <span class="hljs-number">1e6</span>:<span class="hljs-number">.1</span>f}</span>&quot;</span>

<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Execution time -- <span class="hljs-subst">{original_sd}</span> ms\\n&quot;</span>)
<span class="hljs-string">&quot;Execution time -- 45781.5 ms&quot;</span>`,wrap:!1}}),Z=new W({props:{code:"c3RhcnQlMjAlM0QlMjB0aW1lLnRpbWVfbnMoKSUwQWZvciUyMF8lMjBpbiUyMHJhbmdlKE5VTV9JVEVSU19UT19SVU4pJTNBJTBBJTIwJTIwJTIwJTIwaW1hZ2VzJTIwJTNEJTIwZGlzdGlsbGVkKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByb21wdCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0ROVU1fSU5GRVJFTkNFX1NURVBTJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX2ltYWdlc19wZXJfcHJvbXB0JTNETlVNX0lNQUdFU19QRVJfUFJPTVBUJTBBJTIwJTIwJTIwJTIwKS5pbWFnZXMlMEFlbmQlMjAlM0QlMjB0aW1lLnRpbWVfbnMoKSUwQSUwQWRpc3RpbGxlZF9zZCUyMCUzRCUyMGYlMjIlN0IoZW5kJTIwLSUyMHN0YXJ0KSUyMCUyRiUyMDFlNiUzQS4xZiU3RCUyMiUwQXByaW50KGYlMjJFeGVjdXRpb24lMjB0aW1lJTIwLS0lMjAlN0JkaXN0aWxsZWRfc2QlN0QlMjBtcyU1Q24lMjIpJTBBJTIyRXhlY3V0aW9uJTIwdGltZSUyMC0tJTIwMjk4ODQuMiUyMG1zJTIy",highlighted:`start = time.time_ns()
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(NUM_ITERS_TO_RUN):
    images = distilled(
        prompt,
        num_inference_steps=NUM_INFERENCE_STEPS,
        generator=generator,
        num_images_per_prompt=NUM_IMAGES_PER_PROMPT
    ).images
end = time.time_ns()

distilled_sd = <span class="hljs-string">f&quot;<span class="hljs-subst">{(end - start) / <span class="hljs-number">1e6</span>:<span class="hljs-number">.1</span>f}</span>&quot;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Execution time -- <span class="hljs-subst">{distilled_sd}</span> ms\\n&quot;</span>)
<span class="hljs-string">&quot;Execution time -- 29884.2 ms&quot;</span>`,wrap:!1}}),R=new ue({props:{title:"Tiny AutoEncoder",local:"tiny-autoencoder",headingTag:"h2"}}),N=new W({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEF1dG9lbmNvZGVyVGlueSUwQSUwQWRpc3RpbGxlZC52YWUlMjAlM0QlMjBBdXRvZW5jb2RlclRpbnkuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnNheWFrcGF1bCUyRnRhZXNkLWRpZmZ1c2VycyUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMkMlMEEpLnRvKCUyMmN1ZGElMjIp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoencoderTiny

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>)`,wrap:!1}}),S=new W({props:{code:"c3RhcnQlMjAlM0QlMjB0aW1lLnRpbWVfbnMoKSUwQWZvciUyMF8lMjBpbiUyMHJhbmdlKE5VTV9JVEVSU19UT19SVU4pJTNBJTBBJTIwJTIwJTIwJTIwaW1hZ2VzJTIwJTNEJTIwZGlzdGlsbGVkKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHByb21wdCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0ROVU1fSU5GRVJFTkNFX1NURVBTJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZ2VuZXJhdG9yJTNEZ2VuZXJhdG9yJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbnVtX2ltYWdlc19wZXJfcHJvbXB0JTNETlVNX0lNQUdFU19QRVJfUFJPTVBUJTBBJTIwJTIwJTIwJTIwKS5pbWFnZXMlMEFlbmQlMjAlM0QlMjB0aW1lLnRpbWVfbnMoKSUwQSUwQWRpc3RpbGxlZF90aW55X3NkJTIwJTNEJTIwZiUyMiU3QihlbmQlMjAtJTIwc3RhcnQpJTIwJTJGJTIwMWU2JTNBLjFmJTdEJTIyJTBBcHJpbnQoZiUyMkV4ZWN1dGlvbiUyMHRpbWUlMjAtLSUyMCU3QmRpc3RpbGxlZF90aW55X3NkJTdEJTIwbXMlNUNuJTIyKSUwQSUyMkV4ZWN1dGlvbiUyMHRpbWUlMjAtLSUyMDI3MTY1LjclMjBtcyUyMg==",highlighted:`start = time.time_ns()
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(NUM_ITERS_TO_RUN):
    images = distilled(
        prompt,
        num_inference_steps=NUM_INFERENCE_STEPS,
        generator=generator,
        num_images_per_prompt=NUM_IMAGES_PER_PROMPT
    ).images
end = time.time_ns()

distilled_tiny_sd = <span class="hljs-string">f&quot;<span class="hljs-subst">{(end - start) / <span class="hljs-number">1e6</span>:<span class="hljs-number">.1</span>f}</span>&quot;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Execution time -- <span class="hljs-subst">{distilled_tiny_sd}</span> ms\\n&quot;</span>)
<span class="hljs-string">&quot;Execution time -- 27165.7 ms&quot;</span>`,wrap:!1}}),{c(){i=o("meta"),w=n(),r=o("p"),Q=n(),d(b.$$.fragment),G=n(),d(h.$$.fragment),k=n(),g=o("p"),g.innerHTML=ne,x=n(),d(y.$$.fragment),F=n(),I=o("p"),I.textContent=ae,X=n(),d(j.$$.fragment),A=n(),_=o("p"),_.textContent=ie,P=n(),d(V.$$.fragment),H=n(),E=o("p"),E.textContent=oe,Y=n(),d(Z.$$.fragment),q=n(),J=o("div"),J.innerHTML=pe,D=n(),d(R.$$.fragment),z=n(),B=o("p"),B.innerHTML=re,L=n(),d(N.$$.fragment),K=n(),C=o("p"),C.textContent=de,O=n(),d(S.$$.fragment),ee=n(),U=o("div"),U.innerHTML=me,te=n(),v=o("p"),this.h()},l(e){const t=we("svelte-u9bgzb",document.head);i=p(t,"META",{name:!0,content:!0}),t.forEach(s),w=a(e),r=p(e,"P",{}),Me(r).forEach(s),Q=a(e),m(b.$$.fragment,e),G=a(e),m(h.$$.fragment,e),k=a(e),g=p(e,"P",{"data-svelte-h":!0}),T(g)!=="svelte-12qm2ae"&&(g.innerHTML=ne),x=a(e),m(y.$$.fragment,e),F=a(e),I=p(e,"P",{"data-svelte-h":!0}),T(I)!=="svelte-vjfb98"&&(I.textContent=ae),X=a(e),m(j.$$.fragment,e),A=a(e),_=p(e,"P",{"data-svelte-h":!0}),T(_)!=="svelte-8we0fz"&&(_.textContent=ie),P=a(e),m(V.$$.fragment,e),H=a(e),E=p(e,"P",{"data-svelte-h":!0}),T(E)!=="svelte-ovey6g"&&(E.textContent=oe),Y=a(e),m(Z.$$.fragment,e),q=a(e),J=p(e,"DIV",{class:!0,"data-svelte-h":!0}),T(J)!=="svelte-1jk7wa2"&&(J.innerHTML=pe),D=a(e),m(R.$$.fragment,e),z=a(e),B=p(e,"P",{"data-svelte-h":!0}),T(B)!=="svelte-160eppo"&&(B.innerHTML=re),L=a(e),m(N.$$.fragment,e),K=a(e),C=p(e,"P",{"data-svelte-h":!0}),T(C)!=="svelte-5vjlmh"&&(C.textContent=de),O=a(e),m(S.$$.fragment,e),ee=a(e),U=p(e,"DIV",{class:!0,"data-svelte-h":!0}),T(U)!=="svelte-1h7zr2v"&&(U.innerHTML=me),te=a(e),v=p(e,"P",{}),Me(v).forEach(s),this.h()},h(){le(i,"name","hf:doc:metadata"),le(i,"content",Ve),le(J,"class","flex gap-4"),le(U,"class","flex justify-center")},m(e,t){be(document.head,i),l(e,w,t),l(e,r,t),l(e,Q,t),c(b,e,t),l(e,G,t),c(h,e,t),l(e,k,t),l(e,g,t),l(e,x,t),c(y,e,t),l(e,F,t),l(e,I,t),l(e,X,t),c(j,e,t),l(e,A,t),l(e,_,t),l(e,P,t),c(V,e,t),l(e,H,t),l(e,E,t),l(e,Y,t),c(Z,e,t),l(e,q,t),l(e,J,t),l(e,D,t),c(R,e,t),l(e,z,t),l(e,B,t),l(e,L,t),c(N,e,t),l(e,K,t),l(e,C,t),l(e,O,t),c(S,e,t),l(e,ee,t),l(e,U,t),l(e,te,t),l(e,v,t),se=!0},p(e,[t]){const ce={};t&2&&(ce.$$scope={dirty:t,ctx:e}),y.$set(ce)},i(e){se||(M(b.$$.fragment,e),M(h.$$.fragment,e),M(y.$$.fragment,e),M(j.$$.fragment,e),M(V.$$.fragment,e),M(Z.$$.fragment,e),M(R.$$.fragment,e),M(N.$$.fragment,e),M(S.$$.fragment,e),se=!0)},o(e){u(b.$$.fragment,e),u(h.$$.fragment,e),u(y.$$.fragment,e),u(j.$$.fragment,e),u(V.$$.fragment,e),u(Z.$$.fragment,e),u(R.$$.fragment,e),u(N.$$.fragment,e),u(S.$$.fragment,e),se=!1},d(e){e&&(s(w),s(r),s(Q),s(G),s(k),s(g),s(x),s(F),s(I),s(X),s(A),s(_),s(P),s(H),s(E),s(Y),s(q),s(J),s(D),s(z),s(B),s(L),s(K),s(C),s(O),s(ee),s(U),s(te),s(v)),s(i),f(b,e),f(h,e),f(y,e),f(j,e),f(V,e),f(Z,e),f(R,e),f(N,e),f(S,e)}}}const Ve='{"title":"Distilled Stable Diffusion inference","local":"distilled-stable-diffusion-inference","sections":[{"title":"Tiny AutoEncoder","local":"tiny-autoencoder","sections":[],"depth":2}],"depth":1}';function Ee($){return Te(()=>{new URLSearchParams(window.location.search).get("fw"),window.hfDocGoto=he}),[]}class Qe extends Je{constructor(i){super(),Ue(this,i,Ee,_e,fe,{})}}export{Qe as component};
