Buckets:

hf-doc-build/doc / diffusers /main /en /_app /pages /optimization /mps.mdx-hf-doc-builder.js
rtrm's picture
download
raw
13.9 kB
import{S as gt,i as Et,s as Zt,e as o,k as f,w as Q,t as r,M as $t,c as l,d as s,m as h,a as i,x as H,h as a,b as m,G as t,g as p,y as x,q as F,o as L,B as O,v as jt}from"../../chunks/vendor-hf-doc-builder.js";import{T as It}from"../../chunks/Tip-hf-doc-builder.js";import{I as Gt}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as rt}from"../../chunks/CodeBlock-hf-doc-builder.js";function Wt(me){let c,g,u,M,_,y,U,B,P;return{c(){c=o("p"),g=r("Generating multiple prompts in a batch can "),u=o("a"),M=r("crash"),_=r(" or fail to work reliably. We believe this is related to the "),y=o("a"),U=o("code"),B=r("mps"),P=r(" backend in PyTorch. While this is being investigated, you should iterate instead of batching."),this.h()},l(w){c=l(w,"P",{});var d=i(c);g=a(d,"Generating multiple prompts in a batch can "),u=l(d,"A",{href:!0,rel:!0});var N=i(u);M=a(N,"crash"),N.forEach(s),_=a(d," or fail to work reliably. We believe this is related to the "),y=l(d,"A",{href:!0,rel:!0});var b=i(y);U=l(b,"CODE",{});var E=i(U);B=a(E,"mps"),E.forEach(s),b.forEach(s),P=a(d," backend in PyTorch. While this is being investigated, you should iterate instead of batching."),d.forEach(s),this.h()},h(){m(u,"href","https://github.com/huggingface/diffusers/issues/363"),m(u,"rel","nofollow"),m(y,"href","https://github.com/pytorch/pytorch/issues/84039"),m(y,"rel","nofollow")},m(w,d){p(w,c,d),t(c,g),t(c,u),t(u,M),t(c,_),t(c,y),t(y,U),t(U,B),t(c,P)},d(w){w&&s(c)}}}function St(me){let c,g,u,M,_,y,U,B,P,w,d,N,b,E,Ee,Ze,fe,v,K,$e,je,ee,Ie,We,te,Se,Pe,Z,k,ke,Ae,se,Xe,Ce,he,T,Re,oe,Ye,Ne,le,Ve,qe,ue,A,de,$,ye,j,ze,re,De,Qe,Me,X,ve,G,I,ae,C,He,ie,xe,we,V,Fe,be,J,Le,ne,Oe,Ke,q,et,tt,Te,R,Je,W,st,pe,ot,lt,_e;return y=new Gt({}),A=new rt({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiklMEFwaXBlJTIwJTNEJTIwcGlwZS50byglMjJtcHMlMjIpJTBBJTBBJTIzJTIwUmVjb21tZW5kZWQlMjBpZiUyMHlvdXIlMjBjb21wdXRlciUyMGhhcyUyMCUzQyUyMDY0JTIwR0IlMjBvZiUyMFJBTSUwQXBpcGUuZW5hYmxlX2F0dGVudGlvbl9zbGljaW5nKCklMEElMEFwcm9tcHQlMjAlM0QlMjAlMjJhJTIwcGhvdG8lMjBvZiUyMGFuJTIwYXN0cm9uYXV0JTIwcmlkaW5nJTIwYSUyMGhvcnNlJTIwb24lMjBtYXJzJTIy",highlighted:`<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>)
pipe = pipe.to(<span class="hljs-string">&quot;mps&quot;</span>)
<span class="hljs-comment"># Recommended if your computer has &lt; 64 GB of RAM</span>
pipe.enable_attention_slicing()
prompt = <span class="hljs-string">&quot;a photo of an astronaut riding a horse on mars&quot;</span>`}}),$=new It({props:{warning:!0,$$slots:{default:[Wt]},$$scope:{ctx:me}}}),X=new rt({props:{code:"JTIwJTIwZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBJTIwJTIwcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMikudG8oJTIybXBzJTIyKSUwQSUyMCUyMHBpcGUuZW5hYmxlX2F0dGVudGlvbl9zbGljaW5nKCklMEElMEElMjAlMjBwcm9tcHQlMjAlM0QlMjAlMjJhJTIwcGhvdG8lMjBvZiUyMGFuJTIwYXN0cm9uYXV0JTIwcmlkaW5nJTIwYSUyMGhvcnNlJTIwb24lMjBtYXJzJTIyJTBBJTIzJTIwRmlyc3QtdGltZSUyMCUyMndhcm11cCUyMiUyMHBhc3MlMjBpZiUyMFB5VG9yY2glMjB2ZXJzaW9uJTIwaXMlMjAxLjEzJTBBJTJCJTIwXyUyMCUzRCUyMHBpcGUocHJvbXB0JTJDJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRDEpJTBBJTBBJTIzJTIwUmVzdWx0cyUyMG1hdGNoJTIwdGhvc2UlMjBmcm9tJTIwdGhlJTIwQ1BVJTIwZGV2aWNlJTIwYWZ0ZXIlMjB0aGUlMjB3YXJtdXAlMjBwYXNzLiUwQSUyMCUyMGltYWdlJTIwJTNEJTIwcGlwZShwcm9tcHQpLmltYWdlcyU1QjAlNUQ=",highlighted:` from diffusers import DiffusionPipeline
pipe = DiffusionPipeline.from_pretrained(&quot;runwayml/stable-diffusion-v1-5&quot;).to(&quot;mps&quot;)
pipe.enable_attention_slicing()
prompt = &quot;a photo of an astronaut riding a horse on mars&quot;
# First-time &quot;warmup&quot; pass if PyTorch version is 1.13
<span class="hljs-addition">+ _ = pipe(prompt, num_inference_steps=1)</span>
# Results match those from the CPU device after the warmup pass.
image = pipe(prompt).images[0]`}}),C=new Gt({}),R=new rt({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIycnVud2F5bWwlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYlMkMlMjB2YXJpYW50JTNEJTIyZnAxNiUyMiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUpLnRvKCUyMm1wcyUyMiklMEFwaXBlbGluZS5lbmFibGVfYXR0ZW50aW9uX3NsaWNpbmcoKQ==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
pipeline = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16, variant=<span class="hljs-string">&quot;fp16&quot;</span>, use_safetensors=<span class="hljs-literal">True</span>).to(<span class="hljs-string">&quot;mps&quot;</span>)
pipeline.enable_attention_slicing()`}}),{c(){c=o("meta"),g=f(),u=o("h1"),M=o("a"),_=o("span"),Q(y.$$.fragment),U=f(),B=o("span"),P=r("Metal Performance Shaders (MPS)"),w=f(),d=o("p"),N=r("\u{1F917} Diffusers is compatible with Apple silicon (M1/M2 chips) using the PyTorch "),b=o("a"),E=o("code"),Ee=r("mps"),Ze=r(" device, which uses the Metal framework to leverage the GPU on MacOS devices. You\u2019ll need to have:"),fe=f(),v=o("ul"),K=o("li"),$e=r("macOS computer with Apple silicon (M1/M2) hardware"),je=f(),ee=o("li"),Ie=r("macOS 12.6 or later (13.0 or later recommended)"),We=f(),te=o("li"),Se=r("arm64 version of Python"),Pe=f(),Z=o("li"),k=o("a"),ke=r("PyTorch 2.0"),Ae=r(" (recommended) or 1.13 (minimum version supported for "),se=o("code"),Xe=r("mps"),Ce=r(")"),he=f(),T=o("p"),Re=r("The "),oe=o("code"),Ye=r("mps"),Ne=r(" backend uses PyTorch\u2019s "),le=o("code"),Ve=r(".to()"),qe=r(" interface to move the Stable Diffusion pipeline on to your M1 or M2 device:"),ue=f(),Q(A.$$.fragment),de=f(),Q($.$$.fragment),ye=f(),j=o("p"),ze=r("If you\u2019re using "),re=o("strong"),De=r("PyTorch 1.13"),Qe=r(", you need to \u201Cprime\u201D the pipeline with an additional one-time pass through it. This is a temporary workaround for an issue where the first inference pass produces slightly different results than subsequent ones. You only need to do this pass once, and after just one inference step you can discard the result."),Me=f(),Q(X.$$.fragment),ve=f(),G=o("h2"),I=o("a"),ae=o("span"),Q(C.$$.fragment),He=f(),ie=o("span"),xe=r("Troubleshoot"),we=f(),V=o("p"),Fe=r("M1/M2 performance is very sensitive to memory pressure. When this occurs, the system automatically swaps if it needs to which significantly degrades performance."),be=f(),J=o("p"),Le=r("To prevent this from happening, we recommend "),ne=o("em"),Oe=r("attention slicing"),Ke=r(" to reduce memory pressure during inference and prevent swapping. This is especially relevant if your computer has less than 64GB of system RAM, or if you generate images at non-standard resolutions larger than 512\xD7512 pixels. Call the "),q=o("a"),et=r("enable_attention_slicing()"),tt=r(" function on your pipeline:"),Te=f(),Q(R.$$.fragment),Je=f(),W=o("p"),st=r("Attention slicing performs the costly attention operation in multiple steps instead of all at once. It usually improves performance by ~20% in computers without universal memory, but we\u2019ve observed "),pe=o("em"),ot=r("better performance"),lt=r(" in most Apple silicon computers unless you have 64GB of RAM or more."),this.h()},l(e){const n=$t('[data-svelte="svelte-1phssyn"]',document.head);c=l(n,"META",{name:!0,content:!0}),n.forEach(s),g=h(e),u=l(e,"H1",{class:!0});var Y=i(u);M=l(Y,"A",{id:!0,class:!0,href:!0});var at=i(M);_=l(at,"SPAN",{});var it=i(_);H(y.$$.fragment,it),it.forEach(s),at.forEach(s),U=h(Y),B=l(Y,"SPAN",{});var nt=i(B);P=a(nt,"Metal Performance Shaders (MPS)"),nt.forEach(s),Y.forEach(s),w=h(e),d=l(e,"P",{});var Ue=i(d);N=a(Ue,"\u{1F917} Diffusers is compatible with Apple silicon (M1/M2 chips) using the PyTorch "),b=l(Ue,"A",{href:!0,rel:!0});var pt=i(b);E=l(pt,"CODE",{});var ct=i(E);Ee=a(ct,"mps"),ct.forEach(s),pt.forEach(s),Ze=a(Ue," device, which uses the Metal framework to leverage the GPU on MacOS devices. You\u2019ll need to have:"),Ue.forEach(s),fe=h(e),v=l(e,"UL",{});var S=i(v);K=l(S,"LI",{});var mt=i(K);$e=a(mt,"macOS computer with Apple silicon (M1/M2) hardware"),mt.forEach(s),je=h(S),ee=l(S,"LI",{});var ft=i(ee);Ie=a(ft,"macOS 12.6 or later (13.0 or later recommended)"),ft.forEach(s),We=h(S),te=l(S,"LI",{});var ht=i(te);Se=a(ht,"arm64 version of Python"),ht.forEach(s),Pe=h(S),Z=l(S,"LI",{});var ce=i(Z);k=l(ce,"A",{href:!0,rel:!0});var ut=i(k);ke=a(ut,"PyTorch 2.0"),ut.forEach(s),Ae=a(ce," (recommended) or 1.13 (minimum version supported for "),se=l(ce,"CODE",{});var dt=i(se);Xe=a(dt,"mps"),dt.forEach(s),Ce=a(ce,")"),ce.forEach(s),S.forEach(s),he=h(e),T=l(e,"P",{});var z=i(T);Re=a(z,"The "),oe=l(z,"CODE",{});var yt=i(oe);Ye=a(yt,"mps"),yt.forEach(s),Ne=a(z," backend uses PyTorch\u2019s "),le=l(z,"CODE",{});var Mt=i(le);Ve=a(Mt,".to()"),Mt.forEach(s),qe=a(z," interface to move the Stable Diffusion pipeline on to your M1 or M2 device:"),z.forEach(s),ue=h(e),H(A.$$.fragment,e),de=h(e),H($.$$.fragment,e),ye=h(e),j=l(e,"P",{});var Be=i(j);ze=a(Be,"If you\u2019re using "),re=l(Be,"STRONG",{});var vt=i(re);De=a(vt,"PyTorch 1.13"),vt.forEach(s),Qe=a(Be,", you need to \u201Cprime\u201D the pipeline with an additional one-time pass through it. This is a temporary workaround for an issue where the first inference pass produces slightly different results than subsequent ones. You only need to do this pass once, and after just one inference step you can discard the result."),Be.forEach(s),Me=h(e),H(X.$$.fragment,e),ve=h(e),G=l(e,"H2",{class:!0});var Ge=i(G);I=l(Ge,"A",{id:!0,class:!0,href:!0});var wt=i(I);ae=l(wt,"SPAN",{});var bt=i(ae);H(C.$$.fragment,bt),bt.forEach(s),wt.forEach(s),He=h(Ge),ie=l(Ge,"SPAN",{});var Tt=i(ie);xe=a(Tt,"Troubleshoot"),Tt.forEach(s),Ge.forEach(s),we=h(e),V=l(e,"P",{});var Jt=i(V);Fe=a(Jt,"M1/M2 performance is very sensitive to memory pressure. When this occurs, the system automatically swaps if it needs to which significantly degrades performance."),Jt.forEach(s),be=h(e),J=l(e,"P",{});var D=i(J);Le=a(D,"To prevent this from happening, we recommend "),ne=l(D,"EM",{});var _t=i(ne);Oe=a(_t,"attention slicing"),_t.forEach(s),Ke=a(D," to reduce memory pressure during inference and prevent swapping. This is especially relevant if your computer has less than 64GB of system RAM, or if you generate images at non-standard resolutions larger than 512\xD7512 pixels. Call the "),q=l(D,"A",{href:!0});var Ut=i(q);et=a(Ut,"enable_attention_slicing()"),Ut.forEach(s),tt=a(D," function on your pipeline:"),D.forEach(s),Te=h(e),H(R.$$.fragment,e),Je=h(e),W=l(e,"P",{});var ge=i(W);st=a(ge,"Attention slicing performs the costly attention operation in multiple steps instead of all at once. It usually improves performance by ~20% in computers without universal memory, but we\u2019ve observed "),pe=l(ge,"EM",{});var Bt=i(pe);ot=a(Bt,"better performance"),Bt.forEach(s),lt=a(ge," in most Apple silicon computers unless you have 64GB of RAM or more."),ge.forEach(s),this.h()},h(){m(c,"name","hf:doc:metadata"),m(c,"content",JSON.stringify(Pt)),m(M,"id","metal-performance-shaders-mps"),m(M,"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(M,"href","#metal-performance-shaders-mps"),m(u,"class","relative group"),m(b,"href","https://pytorch.org/docs/stable/notes/mps.html"),m(b,"rel","nofollow"),m(k,"href","https://pytorch.org/get-started/locally/"),m(k,"rel","nofollow"),m(I,"id","troubleshoot"),m(I,"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(I,"href","#troubleshoot"),m(G,"class","relative group"),m(q,"href","/docs/diffusers/main/en/api/pipelines/stable_diffusion/image_variation#diffusers.StableDiffusionImageVariationPipeline.enable_attention_slicing")},m(e,n){t(document.head,c),p(e,g,n),p(e,u,n),t(u,M),t(M,_),x(y,_,null),t(u,U),t(u,B),t(B,P),p(e,w,n),p(e,d,n),t(d,N),t(d,b),t(b,E),t(E,Ee),t(d,Ze),p(e,fe,n),p(e,v,n),t(v,K),t(K,$e),t(v,je),t(v,ee),t(ee,Ie),t(v,We),t(v,te),t(te,Se),t(v,Pe),t(v,Z),t(Z,k),t(k,ke),t(Z,Ae),t(Z,se),t(se,Xe),t(Z,Ce),p(e,he,n),p(e,T,n),t(T,Re),t(T,oe),t(oe,Ye),t(T,Ne),t(T,le),t(le,Ve),t(T,qe),p(e,ue,n),x(A,e,n),p(e,de,n),x($,e,n),p(e,ye,n),p(e,j,n),t(j,ze),t(j,re),t(re,De),t(j,Qe),p(e,Me,n),x(X,e,n),p(e,ve,n),p(e,G,n),t(G,I),t(I,ae),x(C,ae,null),t(G,He),t(G,ie),t(ie,xe),p(e,we,n),p(e,V,n),t(V,Fe),p(e,be,n),p(e,J,n),t(J,Le),t(J,ne),t(ne,Oe),t(J,Ke),t(J,q),t(q,et),t(J,tt),p(e,Te,n),x(R,e,n),p(e,Je,n),p(e,W,n),t(W,st),t(W,pe),t(pe,ot),t(W,lt),_e=!0},p(e,[n]){const Y={};n&2&&(Y.$$scope={dirty:n,ctx:e}),$.$set(Y)},i(e){_e||(F(y.$$.fragment,e),F(A.$$.fragment,e),F($.$$.fragment,e),F(X.$$.fragment,e),F(C.$$.fragment,e),F(R.$$.fragment,e),_e=!0)},o(e){L(y.$$.fragment,e),L(A.$$.fragment,e),L($.$$.fragment,e),L(X.$$.fragment,e),L(C.$$.fragment,e),L(R.$$.fragment,e),_e=!1},d(e){s(c),e&&s(g),e&&s(u),O(y),e&&s(w),e&&s(d),e&&s(fe),e&&s(v),e&&s(he),e&&s(T),e&&s(ue),O(A,e),e&&s(de),O($,e),e&&s(ye),e&&s(j),e&&s(Me),O(X,e),e&&s(ve),e&&s(G),O(C),e&&s(we),e&&s(V),e&&s(be),e&&s(J),e&&s(Te),O(R,e),e&&s(Je),e&&s(W)}}}const Pt={local:"metal-performance-shaders-mps",sections:[{local:"troubleshoot",title:"Troubleshoot"}],title:"Metal Performance Shaders (MPS)"};function kt(me){return jt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Yt extends gt{constructor(c){super();Et(this,c,kt,St,Zt,{})}}export{Yt as default,Pt as metadata};

Xet Storage Details

Size:
13.9 kB
·
Xet hash:
984513bfbe69e3e27f2c62a3d0efb257c8a7ac9f13ab87ad0a57d371fd787f2b

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