import{s as ye,o as Je,n as Ue}from"../chunks/scheduler.182ea377.js";import{S as we,i as be,g as o,s as n,r as d,A as ge,h as p,f as s,c as a,j as fe,u as m,x as T,k as ae,y as he,a as l,v as u,d as M,t as c,w as f}from"../chunks/index.abf12888.js";import{T as Ie}from"../chunks/Tip.230e2334.js";import{C as W}from"../chunks/CodeBlock.57fe6e13.js";import{D as je}from"../chunks/DocNotebookDropdown.5fa27ace.js";import{H as Te,E as _e}from"../chunks/EditOnGithub.9b8e78e4.js";function Ve(G){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:Ue,d(r){r&&s(i)}}}function Ee(G){let i,w,r,Q,b,k,g,x,h,ie='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%.',F,y,X,I,oe="Let’s load the distilled Stable Diffusion model and compare it against the original Stable Diffusion model:",A,j,P,_,pe="Given a prompt, get the inference time for the original model:",H,V,Y,E,re="Time the distilled model inference:",q,Z,z,J,de='<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,L,B,me='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:',K,N,O,C,ue="Time the distilled model and distilled VAE inference:",ee,$,te,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>',se,S,le,v,ne;return b=new Te({props:{title:"Distilled Stable Diffusion inference",local:"distilled-stable-diffusion-inference",headingTag:"h1"}}),g=new je({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 Ie({props:{$$slots:{default:[Ve]},$$scope:{ctx:G}}}),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 Te({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}}),$=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}}),S=new _e({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/using-diffusers/distilled_sd.md"}}),{c(){i=o("meta"),w=n(),r=o("p"),Q=n(),d(b.$$.fragment),k=n(),d(g.$$.fragment),x=n(),h=o("p"),h.innerHTML=ie,F=n(),d(y.$$.fragment),X=n(),I=o("p"),I.textContent=oe,A=n(),d(j.$$.fragment),P=n(),_=o("p"),_.textContent=pe,H=n(),d(V.$$.fragment),Y=n(),E=o("p"),E.textContent=re,q=n(),d(Z.$$.fragment),z=n(),J=o("div"),J.innerHTML=de,D=n(),d(R.$$.fragment),L=n(),B=o("p"),B.innerHTML=me,K=n(),d(N.$$.fragment),O=n(),C=o("p"),C.textContent=ue,ee=n(),d($.$$.fragment),te=n(),U=o("div"),U.innerHTML=Me,se=n(),d(S.$$.fragment),le=n(),v=o("p"),this.h()},l(e){const t=ge("svelte-u9bgzb",document.head);i=p(t,"META",{name:!0,content:!0}),t.forEach(s),w=a(e),r=p(e,"P",{}),fe(r).forEach(s),Q=a(e),m(b.$$.fragment,e),k=a(e),m(g.$$.fragment,e),x=a(e),h=p(e,"P",{"data-svelte-h":!0}),T(h)!=="svelte-12qm2ae"&&(h.innerHTML=ie),F=a(e),m(y.$$.fragment,e),X=a(e),I=p(e,"P",{"data-svelte-h":!0}),T(I)!=="svelte-vjfb98"&&(I.textContent=oe),A=a(e),m(j.$$.fragment,e),P=a(e),_=p(e,"P",{"data-svelte-h":!0}),T(_)!=="svelte-8we0fz"&&(_.textContent=pe),H=a(e),m(V.$$.fragment,e),Y=a(e),E=p(e,"P",{"data-svelte-h":!0}),T(E)!=="svelte-ovey6g"&&(E.textContent=re),q=a(e),m(Z.$$.fragment,e),z=a(e),J=p(e,"DIV",{class:!0,"data-svelte-h":!0}),T(J)!=="svelte-1jk7wa2"&&(J.innerHTML=de),D=a(e),m(R.$$.fragment,e),L=a(e),B=p(e,"P",{"data-svelte-h":!0}),T(B)!=="svelte-160eppo"&&(B.innerHTML=me),K=a(e),m(N.$$.fragment,e),O=a(e),C=p(e,"P",{"data-svelte-h":!0}),T(C)!=="svelte-5vjlmh"&&(C.textContent=ue),ee=a(e),m($.$$.fragment,e),te=a(e),U=p(e,"DIV",{class:!0,"data-svelte-h":!0}),T(U)!=="svelte-1h7zr2v"&&(U.innerHTML=Me),se=a(e),m(S.$$.fragment,e),le=a(e),v=p(e,"P",{}),fe(v).forEach(s),this.h()},h(){ae(i,"name","hf:doc:metadata"),ae(i,"content",Ze),ae(J,"class","flex gap-4"),ae(U,"class","flex justify-center")},m(e,t){he(document.head,i),l(e,w,t),l(e,r,t),l(e,Q,t),u(b,e,t),l(e,k,t),u(g,e,t),l(e,x,t),l(e,h,t),l(e,F,t),u(y,e,t),l(e,X,t),l(e,I,t),l(e,A,t),u(j,e,t),l(e,P,t),l(e,_,t),l(e,H,t),u(V,e,t),l(e,Y,t),l(e,E,t),l(e,q,t),u(Z,e,t),l(e,z,t),l(e,J,t),l(e,D,t),u(R,e,t),l(e,L,t),l(e,B,t),l(e,K,t),u(N,e,t),l(e,O,t),l(e,C,t),l(e,ee,t),u($,e,t),l(e,te,t),l(e,U,t),l(e,se,t),u(S,e,t),l(e,le,t),l(e,v,t),ne=!0},p(e,[t]){const ce={};t&2&&(ce.$$scope={dirty:t,ctx:e}),y.$set(ce)},i(e){ne||(M(b.$$.fragment,e),M(g.$$.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($.$$.fragment,e),M(S.$$.fragment,e),ne=!0)},o(e){c(b.$$.fragment,e),c(g.$$.fragment,e),c(y.$$.fragment,e),c(j.$$.fragment,e),c(V.$$.fragment,e),c(Z.$$.fragment,e),c(R.$$.fragment,e),c(N.$$.fragment,e),c($.$$.fragment,e),c(S.$$.fragment,e),ne=!1},d(e){e&&(s(w),s(r),s(Q),s(k),s(x),s(h),s(F),s(X),s(I),s(A),s(P),s(_),s(H),s(Y),s(E),s(q),s(z),s(J),s(D),s(L),s(B),s(K),s(O),s(C),s(ee),s(te),s(U),s(se),s(le),s(v)),s(i),f(b,e),f(g,e),f(y,e),f(j,e),f(V,e),f(Z,e),f(R,e),f(N,e),f($,e),f(S,e)}}}const Ze='{"title":"Distilled Stable Diffusion inference","local":"distilled-stable-diffusion-inference","sections":[{"title":"Tiny AutoEncoder","local":"tiny-autoencoder","sections":[],"depth":2}],"depth":1}';function Re(G){return Je(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ve extends we{constructor(i){super(),be(this,i,Re,Ee,ye,{})}}export{ve as component};
