Buckets:

hf-doc-build/doc / diffusers /main /en /_app /pages /optimization /fp16.mdx-hf-doc-builder.js
rtrm's picture
download
raw
14.2 kB
import{S as Cr,i as Rr,s as jr,e as o,k as f,w as V,t as l,M as Gr,c as a,d as r,m as c,a as s,x as Y,h as n,b as m,G as e,g as d,y as O,q as Q,o as L,B as K,v as Sr}from"../../chunks/vendor-hf-doc-builder.js";import{T as Pr}from"../../chunks/Tip-hf-doc-builder.js";import{I as er}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as Ar}from"../../chunks/CodeBlock-hf-doc-builder.js";function Br(ee){let p,E,h,u,T;return{c(){p=o("p"),E=l("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 "),h=o("a"),u=l("Reduce memory usage"),T=l(" guide."),this.h()},l(v){p=a(v,"P",{});var y=s(p);E=n(y,"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 "),h=a(y,"A",{href:!0});var w=s(h);u=n(w,"Reduce memory usage"),w.forEach(r),T=n(y," guide."),y.forEach(r),this.h()},h(){m(h,"href","memory")},m(v,y){d(v,p,y),e(p,E),e(p,h),e(h,u),e(p,T)},d(v){v&&r(p)}}}function Zr(ee){let p,E,h,u,T,v;return{c(){p=o("p"),E=l("Don\u2019t use "),h=o("a"),u=o("code"),T=l("torch.autocast"),v=l(" in any of the pipelines as it can lead to black images and is always slower than pure float16 precision."),this.h()},l(y){p=a(y,"P",{});var w=s(p);E=n(w,"Don\u2019t use "),h=a(w,"A",{href:!0,rel:!0});var te=s(h);u=a(te,"CODE",{});var B=s(u);T=n(B,"torch.autocast"),B.forEach(r),te.forEach(r),v=n(w," in any of the pipelines as it can lead to black images and is always slower than pure float16 precision."),w.forEach(r),this.h()},h(){m(h,"href","https://pytorch.org/docs/stable/amp.html#torch.autocast"),m(h,"rel","nofollow")},m(y,w){d(y,p,w),e(p,E),e(p,h),e(h,u),e(u,T),e(p,v)},d(y){y&&r(p)}}}function Nr(ee){let p,E,h,u,T,v,y,w,te,B,g,tt,re,rt,ot,pe,at,st,Re,F,je,P,lt,he,nt,it,Ge,A,de,b,Se,ft,me,ct,pt,ue,ht,dt,_,J,ve,mt,ut,ye,vt,yt,we,wt,Tt,U,Te,_t,Et,_e,gt,$t,Ee,bt,Jt,M,ge,Ut,Mt,$e,xt,Dt,be,It,kt,x,Je,Ft,Pt,Ue,At,Ct,Me,Rt,jt,D,xe,Gt,St,De,Bt,Zt,Ie,Nt,Be,I,C,ke,Z,Wt,Fe,Ht,Ze,R,zt,N,qt,Xt,Ne,W,We,j,Vt,H,Yt,Ot,He,k,G,Pe,z,Qt,Ae,Lt,ze,oe,Kt,qe,q,Xe,S,Ve;return v=new er({}),F=new Pr({props:{$$slots:{default:[Br]},$$scope:{ctx:ee}}}),Z=new er({}),W=new Ar({props:{code:"aW1wb3J0JTIwdG9yY2glMEElMEF0b3JjaC5iYWNrZW5kcy5jdWRhLm1hdG11bC5hbGxvd190ZjMyJTIwJTNEJTIwVHJ1ZQ==",highlighted:`<span class="hljs-keyword">import</span> torch
torch.backends.cuda.matmul.allow_tf32 = <span class="hljs-literal">True</span>`}}),z=new er({}),q=new Ar({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>]`}}),S=new Pr({props:{warning:!0,$$slots:{default:[Zr]},$$scope:{ctx:ee}}}),{c(){p=o("meta"),E=f(),h=o("h1"),u=o("a"),T=o("span"),V(v.$$.fragment),y=f(),w=o("span"),te=l("Speed up inference"),B=f(),g=o("p"),tt=l("There are several ways to optimize \u{1F917} Diffusers for inference speed. As a general rule of thumb, we recommend using either "),re=o("a"),rt=l("xFormers"),ot=l(" or "),pe=o("code"),at=l("torch.nn.functional.scaled_dot_product_attention"),st=l(" in PyTorch 2.0 for their memory-efficient attention."),Re=f(),V(F.$$.fragment),je=f(),P=o("p"),lt=l("The results below are obtained from generating a single 512x512 image from the prompt "),he=o("code"),nt=l("a photo of an astronaut riding a horse on mars"),it=l(" with 50 DDIM steps on a Nvidia Titan RTX, demonstrating the speed-up you can expect."),Ge=f(),A=o("table"),de=o("thead"),b=o("tr"),Se=o("th"),ft=f(),me=o("th"),ct=l("latency"),pt=f(),ue=o("th"),ht=l("speed-up"),dt=f(),_=o("tbody"),J=o("tr"),ve=o("td"),mt=l("original"),ut=f(),ye=o("td"),vt=l("9.50s"),yt=f(),we=o("td"),wt=l("x1"),Tt=f(),U=o("tr"),Te=o("td"),_t=l("fp16"),Et=f(),_e=o("td"),gt=l("3.61s"),$t=f(),Ee=o("td"),bt=l("x2.63"),Jt=f(),M=o("tr"),ge=o("td"),Ut=l("channels last"),Mt=f(),$e=o("td"),xt=l("3.30s"),Dt=f(),be=o("td"),It=l("x2.88"),kt=f(),x=o("tr"),Je=o("td"),Ft=l("traced UNet"),Pt=f(),Ue=o("td"),At=l("3.21s"),Ct=f(),Me=o("td"),Rt=l("x2.96"),jt=f(),D=o("tr"),xe=o("td"),Gt=l("memory efficient attention"),St=f(),De=o("td"),Bt=l("2.63s"),Zt=f(),Ie=o("td"),Nt=l("x3.61"),Be=f(),I=o("h2"),C=o("a"),ke=o("span"),V(Z.$$.fragment),Wt=f(),Fe=o("span"),Ht=l("Use TensorFloat-32"),Ze=f(),R=o("p"),zt=l("On Ampere and later CUDA devices, matrix multiplications and convolutions can use the "),N=o("a"),qt=l("TensorFloat-32 (TF32)"),Xt=l(" 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."),Ne=f(),V(W.$$.fragment),We=f(),j=o("p"),Vt=l("You can learn more about TF32 in the "),H=o("a"),Yt=l("Mixed precision training"),Ot=l(" guide."),He=f(),k=o("h2"),G=o("a"),Pe=o("span"),V(z.$$.fragment),Qt=f(),Ae=o("span"),Lt=l("Half-precision weights"),ze=f(),oe=o("p"),Kt=l("To save GPU memory and get more speed, try loading and running the model weights directly in half-precision or float16:"),qe=f(),V(q.$$.fragment),Xe=f(),V(S.$$.fragment),this.h()},l(t){const i=Gr('[data-svelte="svelte-1phssyn"]',document.head);p=a(i,"META",{name:!0,content:!0}),i.forEach(r),E=c(t),h=a(t,"H1",{class:!0});var X=s(h);u=a(X,"A",{id:!0,class:!0,href:!0});var Ce=s(u);T=a(Ce,"SPAN",{});var tr=s(T);Y(v.$$.fragment,tr),tr.forEach(r),Ce.forEach(r),y=c(X),w=a(X,"SPAN",{});var rr=s(w);te=n(rr,"Speed up inference"),rr.forEach(r),X.forEach(r),B=c(t),g=a(t,"P",{});var ae=s(g);tt=n(ae,"There are several ways to optimize \u{1F917} Diffusers for inference speed. As a general rule of thumb, we recommend using either "),re=a(ae,"A",{href:!0});var or=s(re);rt=n(or,"xFormers"),or.forEach(r),ot=n(ae," or "),pe=a(ae,"CODE",{});var ar=s(pe);at=n(ar,"torch.nn.functional.scaled_dot_product_attention"),ar.forEach(r),st=n(ae," in PyTorch 2.0 for their memory-efficient attention."),ae.forEach(r),Re=c(t),Y(F.$$.fragment,t),je=c(t),P=a(t,"P",{});var Ye=s(P);lt=n(Ye,"The results below are obtained from generating a single 512x512 image from the prompt "),he=a(Ye,"CODE",{});var sr=s(he);nt=n(sr,"a photo of an astronaut riding a horse on mars"),sr.forEach(r),it=n(Ye," with 50 DDIM steps on a Nvidia Titan RTX, demonstrating the speed-up you can expect."),Ye.forEach(r),Ge=c(t),A=a(t,"TABLE",{});var Oe=s(A);de=a(Oe,"THEAD",{});var lr=s(de);b=a(lr,"TR",{});var se=s(b);Se=a(se,"TH",{}),s(Se).forEach(r),ft=c(se),me=a(se,"TH",{});var nr=s(me);ct=n(nr,"latency"),nr.forEach(r),pt=c(se),ue=a(se,"TH",{});var ir=s(ue);ht=n(ir,"speed-up"),ir.forEach(r),se.forEach(r),lr.forEach(r),dt=c(Oe),_=a(Oe,"TBODY",{});var $=s(_);J=a($,"TR",{});var le=s(J);ve=a(le,"TD",{});var fr=s(ve);mt=n(fr,"original"),fr.forEach(r),ut=c(le),ye=a(le,"TD",{});var cr=s(ye);vt=n(cr,"9.50s"),cr.forEach(r),yt=c(le),we=a(le,"TD",{});var pr=s(we);wt=n(pr,"x1"),pr.forEach(r),le.forEach(r),Tt=c($),U=a($,"TR",{});var ne=s(U);Te=a(ne,"TD",{});var hr=s(Te);_t=n(hr,"fp16"),hr.forEach(r),Et=c(ne),_e=a(ne,"TD",{});var dr=s(_e);gt=n(dr,"3.61s"),dr.forEach(r),$t=c(ne),Ee=a(ne,"TD",{});var mr=s(Ee);bt=n(mr,"x2.63"),mr.forEach(r),ne.forEach(r),Jt=c($),M=a($,"TR",{});var ie=s(M);ge=a(ie,"TD",{});var ur=s(ge);Ut=n(ur,"channels last"),ur.forEach(r),Mt=c(ie),$e=a(ie,"TD",{});var vr=s($e);xt=n(vr,"3.30s"),vr.forEach(r),Dt=c(ie),be=a(ie,"TD",{});var yr=s(be);It=n(yr,"x2.88"),yr.forEach(r),ie.forEach(r),kt=c($),x=a($,"TR",{});var fe=s(x);Je=a(fe,"TD",{});var wr=s(Je);Ft=n(wr,"traced UNet"),wr.forEach(r),Pt=c(fe),Ue=a(fe,"TD",{});var Tr=s(Ue);At=n(Tr,"3.21s"),Tr.forEach(r),Ct=c(fe),Me=a(fe,"TD",{});var _r=s(Me);Rt=n(_r,"x2.96"),_r.forEach(r),fe.forEach(r),jt=c($),D=a($,"TR",{});var ce=s(D);xe=a(ce,"TD",{});var Er=s(xe);Gt=n(Er,"memory efficient attention"),Er.forEach(r),St=c(ce),De=a(ce,"TD",{});var gr=s(De);Bt=n(gr,"2.63s"),gr.forEach(r),Zt=c(ce),Ie=a(ce,"TD",{});var $r=s(Ie);Nt=n($r,"x3.61"),$r.forEach(r),ce.forEach(r),$.forEach(r),Oe.forEach(r),Be=c(t),I=a(t,"H2",{class:!0});var Qe=s(I);C=a(Qe,"A",{id:!0,class:!0,href:!0});var br=s(C);ke=a(br,"SPAN",{});var Jr=s(ke);Y(Z.$$.fragment,Jr),Jr.forEach(r),br.forEach(r),Wt=c(Qe),Fe=a(Qe,"SPAN",{});var Ur=s(Fe);Ht=n(Ur,"Use TensorFloat-32"),Ur.forEach(r),Qe.forEach(r),Ze=c(t),R=a(t,"P",{});var Le=s(R);zt=n(Le,"On Ampere and later CUDA devices, matrix multiplications and convolutions can use the "),N=a(Le,"A",{href:!0,rel:!0});var Mr=s(N);qt=n(Mr,"TensorFloat-32 (TF32)"),Mr.forEach(r),Xt=n(Le," 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."),Le.forEach(r),Ne=c(t),Y(W.$$.fragment,t),We=c(t),j=a(t,"P",{});var Ke=s(j);Vt=n(Ke,"You can learn more about TF32 in the "),H=a(Ke,"A",{href:!0,rel:!0});var xr=s(H);Yt=n(xr,"Mixed precision training"),xr.forEach(r),Ot=n(Ke," guide."),Ke.forEach(r),He=c(t),k=a(t,"H2",{class:!0});var et=s(k);G=a(et,"A",{id:!0,class:!0,href:!0});var Dr=s(G);Pe=a(Dr,"SPAN",{});var Ir=s(Pe);Y(z.$$.fragment,Ir),Ir.forEach(r),Dr.forEach(r),Qt=c(et),Ae=a(et,"SPAN",{});var kr=s(Ae);Lt=n(kr,"Half-precision weights"),kr.forEach(r),et.forEach(r),ze=c(t),oe=a(t,"P",{});var Fr=s(oe);Kt=n(Fr,"To save GPU memory and get more speed, try loading and running the model weights directly in half-precision or float16:"),Fr.forEach(r),qe=c(t),Y(q.$$.fragment,t),Xe=c(t),Y(S.$$.fragment,t),this.h()},h(){m(p,"name","hf:doc:metadata"),m(p,"content",JSON.stringify(Wr)),m(u,"id","speed-up-inference"),m(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"),m(u,"href","#speed-up-inference"),m(h,"class","relative group"),m(re,"href","xformers"),m(C,"id","use-tensorfloat32"),m(C,"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"),m(C,"href","#use-tensorfloat32"),m(I,"class","relative group"),m(N,"href","https://blogs.nvidia.com/blog/2020/05/14/tensorfloat-32-precision-format/"),m(N,"rel","nofollow"),m(H,"href","https://huggingface.co/docs/transformers/en/perf_train_gpu_one#tf32"),m(H,"rel","nofollow"),m(G,"id","halfprecision-weights"),m(G,"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"),m(G,"href","#halfprecision-weights"),m(k,"class","relative group")},m(t,i){e(document.head,p),d(t,E,i),d(t,h,i),e(h,u),e(u,T),O(v,T,null),e(h,y),e(h,w),e(w,te),d(t,B,i),d(t,g,i),e(g,tt),e(g,re),e(re,rt),e(g,ot),e(g,pe),e(pe,at),e(g,st),d(t,Re,i),O(F,t,i),d(t,je,i),d(t,P,i),e(P,lt),e(P,he),e(he,nt),e(P,it),d(t,Ge,i),d(t,A,i),e(A,de),e(de,b),e(b,Se),e(b,ft),e(b,me),e(me,ct),e(b,pt),e(b,ue),e(ue,ht),e(A,dt),e(A,_),e(_,J),e(J,ve),e(ve,mt),e(J,ut),e(J,ye),e(ye,vt),e(J,yt),e(J,we),e(we,wt),e(_,Tt),e(_,U),e(U,Te),e(Te,_t),e(U,Et),e(U,_e),e(_e,gt),e(U,$t),e(U,Ee),e(Ee,bt),e(_,Jt),e(_,M),e(M,ge),e(ge,Ut),e(M,Mt),e(M,$e),e($e,xt),e(M,Dt),e(M,be),e(be,It),e(_,kt),e(_,x),e(x,Je),e(Je,Ft),e(x,Pt),e(x,Ue),e(Ue,At),e(x,Ct),e(x,Me),e(Me,Rt),e(_,jt),e(_,D),e(D,xe),e(xe,Gt),e(D,St),e(D,De),e(De,Bt),e(D,Zt),e(D,Ie),e(Ie,Nt),d(t,Be,i),d(t,I,i),e(I,C),e(C,ke),O(Z,ke,null),e(I,Wt),e(I,Fe),e(Fe,Ht),d(t,Ze,i),d(t,R,i),e(R,zt),e(R,N),e(N,qt),e(R,Xt),d(t,Ne,i),O(W,t,i),d(t,We,i),d(t,j,i),e(j,Vt),e(j,H),e(H,Yt),e(j,Ot),d(t,He,i),d(t,k,i),e(k,G),e(G,Pe),O(z,Pe,null),e(k,Qt),e(k,Ae),e(Ae,Lt),d(t,ze,i),d(t,oe,i),e(oe,Kt),d(t,qe,i),O(q,t,i),d(t,Xe,i),O(S,t,i),Ve=!0},p(t,[i]){const X={};i&2&&(X.$$scope={dirty:i,ctx:t}),F.$set(X);const Ce={};i&2&&(Ce.$$scope={dirty:i,ctx:t}),S.$set(Ce)},i(t){Ve||(Q(v.$$.fragment,t),Q(F.$$.fragment,t),Q(Z.$$.fragment,t),Q(W.$$.fragment,t),Q(z.$$.fragment,t),Q(q.$$.fragment,t),Q(S.$$.fragment,t),Ve=!0)},o(t){L(v.$$.fragment,t),L(F.$$.fragment,t),L(Z.$$.fragment,t),L(W.$$.fragment,t),L(z.$$.fragment,t),L(q.$$.fragment,t),L(S.$$.fragment,t),Ve=!1},d(t){r(p),t&&r(E),t&&r(h),K(v),t&&r(B),t&&r(g),t&&r(Re),K(F,t),t&&r(je),t&&r(P),t&&r(Ge),t&&r(A),t&&r(Be),t&&r(I),K(Z),t&&r(Ze),t&&r(R),t&&r(Ne),K(W,t),t&&r(We),t&&r(j),t&&r(He),t&&r(k),K(z),t&&r(ze),t&&r(oe),t&&r(qe),K(q,t),t&&r(Xe),K(S,t)}}}const Wr={local:"speed-up-inference",sections:[{local:"use-tensorfloat32",title:"Use TensorFloat-32"},{local:"halfprecision-weights",title:"Half-precision weights"}],title:"Speed up inference"};function Hr(ee){return Sr(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Yr extends Cr{constructor(p){super();Rr(this,p,Hr,Nr,jr,{})}}export{Yr as default,Wr as metadata};

Xet Storage Details

Size:
14.2 kB
·
Xet hash:
08e4a2dabfe25aa3eef088e514bb4a3de2e1f2b270271d7628c2fd7929bec0fe

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