import{S as bo,i as $o,s as go,e as s,k as u,w as N,t as n,M as Eo,c as r,d as o,m as c,a as i,x as O,h as a,b as p,G as t,g as f,y as G,q as L,o as B,B as H,v as Po}from"../../chunks/vendor-hf-doc-builder.js";import{T as ko}from"../../chunks/Tip-hf-doc-builder.js";import{I as Ae}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as _o}from"../../chunks/CodeBlock-hf-doc-builder.js";function Ao(Me){let m,b,h,w;return{c(){m=s("p"),b=s("strong"),h=n("If you are using PyTorch 1.13"),w=n(" you need to \u201Cprime\u201D the pipeline using an additional one-time pass through it. This is a temporary workaround for a weird issue we detected: the first inference pass produces slightly different results than subsequent ones. You only need to do this pass once, and it\u2019s ok to use just one inference step and discard the result.")},l(_){m=r(_,"P",{});var d=i(m);b=r(d,"STRONG",{});var X=i(b);h=a(X,"If you are using PyTorch 1.13"),X.forEach(o),w=a(d," you need to \u201Cprime\u201D the pipeline using an additional one-time pass through it. This is a temporary workaround for a weird issue we detected: the first inference pass produces slightly different results than subsequent ones. You only need to do this pass once, and it\u2019s ok to use just one inference step and discard the result."),d.forEach(o)},m(_,d){f(_,m,d),t(m,b),t(b,h),t(m,w)},d(_){_&&o(m)}}}function Mo(Me){let m,b,h,w,_,d,X,ie,Je,Se,T,ze,ne,Qe,Ve,Te,P,D,ae,W,Xe,le,Ze,De,y,pe,et,tt,fe,ot,st,ue,rt,it,v,nt,ce,at,lt,me,pt,ft,he,ut,ct,U,mt,ht,qe,k,q,de,Y,dt,ve,vt,Ie,$,wt,we,yt,_t,ye,bt,$t,Re,I,xe,Z,gt,Ce,K,je,A,R,_e,F,Et,be,Pt,Ne,ee,kt,Oe,g,At,$e,Mt,St,ge,Tt,Dt,Ge,J,Le,M,x,Ee,z,qt,Pe,It,Be,te,S,Rt,Q,xt,Ct,C,ke,jt,Nt,Ot,He;return d=new Ae({}),W=new Ae({}),Y=new Ae({}),I=new ko({props:{warning:!0,$$slots:{default:[Ao]},$$scope:{ctx:Me}}}),K=new _o({props:{code:`from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("mps")

# Recommended if your computer has < 64 GB of RAM
pipe.enable_attention_slicing()

prompt = "a photo of an astronaut riding a horse on mars"

# First-time "warmup" pass if PyTorch version is 1.13 (see explanation above)
_ = pipe(prompt, num_inference_steps=1)

# Results match those from the CPU device after the warmup pass.
image = pipe(prompt).images[0]`,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>

<span class="hljs-comment"># First-time &quot;warmup&quot; pass if PyTorch version is 1.13 (see explanation above)</span>
_ = pipe(prompt, num_inference_steps=<span class="hljs-number">1</span>)

<span class="hljs-comment"># Results match those from the CPU device after the warmup pass.</span>
image = pipe(prompt).images[<span class="hljs-number">0</span>]`}}),F=new Ae({}),J=new _o({props:{code:"pipeline.enable_attention_slicing()",highlighted:"pipeline.enable_attention_slicing()"}}),z=new Ae({}),{c(){m=s("meta"),b=u(),h=s("h1"),w=s("a"),_=s("span"),N(d.$$.fragment),X=u(),ie=s("span"),Je=n("How to use Stable Diffusion in Apple Silicon (M1/M2)"),Se=u(),T=s("p"),ze=n("\u{1F917} Diffusers is compatible with Apple silicon for Stable Diffusion inference, using the PyTorch "),ne=s("code"),Qe=n("mps"),Ve=n(" device. These are the steps you need to follow to use your M1 or M2 computer with Stable Diffusion."),Te=u(),P=s("h2"),D=s("a"),ae=s("span"),N(W.$$.fragment),Xe=u(),le=s("span"),Ze=n("Requirements"),De=u(),y=s("ul"),pe=s("li"),et=n("Mac computer with Apple silicon (M1/M2) hardware."),tt=u(),fe=s("li"),ot=n("macOS 12.6 or later (13.0 or later recommended)."),st=u(),ue=s("li"),rt=n("arm64 version of Python."),it=u(),v=s("li"),nt=n("PyTorch 2.0 (recommended) or 1.13 (minimum version supported for "),ce=s("code"),at=n("mps"),lt=n("). You can install it with "),me=s("code"),pt=n("pip"),ft=n(" or "),he=s("code"),ut=n("conda"),ct=n(" using the instructions in "),U=s("a"),mt=n("https://pytorch.org/get-started/locally/"),ht=n("."),qe=u(),k=s("h2"),q=s("a"),de=s("span"),N(Y.$$.fragment),dt=u(),ve=s("span"),vt=n("Inference Pipeline"),Ie=u(),$=s("p"),wt=n("The snippet below demonstrates how to use the "),we=s("code"),yt=n("mps"),_t=n(" backend using the familiar "),ye=s("code"),bt=n("to()"),$t=n(" interface to move the Stable Diffusion pipeline to your M1 or M2 device."),Re=u(),N(I.$$.fragment),xe=u(),Z=s("p"),gt=n("We strongly recommend you use PyTorch 2 or better, as it solves a number of problems like the one described in the previous tip."),Ce=u(),N(K.$$.fragment),je=u(),A=s("h2"),R=s("a"),_e=s("span"),N(F.$$.fragment),Et=u(),be=s("span"),Pt=n("Performance Recommendations"),Ne=u(),ee=s("p"),kt=n("M1/M2 performance is very sensitive to memory pressure. The system will automatically swap if it needs to, but performance will degrade significantly when it does."),Oe=u(),g=s("p"),At=n("We recommend you use "),$e=s("em"),Mt=n("attention slicing"),St=n(" to reduce memory pressure during inference and prevent swapping, particularly if your computer has less than 64 GB of system RAM, or if you generate images at non-standard resolutions larger than 512 \xD7 512 pixels. Attention slicing performs the costly attention operation in multiple steps instead of all at once. It usually has a performance impact of ~20% in computers without universal memory, but we have observed "),ge=s("em"),Tt=n("better performance"),Dt=n(" in most Apple Silicon computers, unless you have 64 GB or more."),Ge=u(),N(J.$$.fragment),Le=u(),M=s("h2"),x=s("a"),Ee=s("span"),N(z.$$.fragment),qt=u(),Pe=s("span"),It=n("Known Issues"),Be=u(),te=s("ul"),S=s("li"),Rt=n("Generating multiple prompts in a batch "),Q=s("a"),xt=n("crashes or doesn\u2019t work reliably"),Ct=n(". We believe this is related to the "),C=s("a"),ke=s("code"),jt=n("mps"),Nt=n(" backend in PyTorch"),Ot=n(". This is being resolved, but for now we recommend to iterate instead of batching."),this.h()},l(e){const l=Eo('[data-svelte="svelte-1phssyn"]',document.head);m=r(l,"META",{name:!0,content:!0}),l.forEach(o),b=c(e),h=r(e,"H1",{class:!0});var V=i(h);w=r(V,"A",{id:!0,class:!0,href:!0});var Lt=i(w);_=r(Lt,"SPAN",{});var Bt=i(_);O(d.$$.fragment,Bt),Bt.forEach(o),Lt.forEach(o),X=c(V),ie=r(V,"SPAN",{});var Ht=i(ie);Je=a(Ht,"How to use Stable Diffusion in Apple Silicon (M1/M2)"),Ht.forEach(o),V.forEach(o),Se=c(e),T=r(e,"P",{});var We=i(T);ze=a(We,"\u{1F917} Diffusers is compatible with Apple silicon for Stable Diffusion inference, using the PyTorch "),ne=r(We,"CODE",{});var Wt=i(ne);Qe=a(Wt,"mps"),Wt.forEach(o),Ve=a(We," device. These are the steps you need to follow to use your M1 or M2 computer with Stable Diffusion."),We.forEach(o),Te=c(e),P=r(e,"H2",{class:!0});var Ue=i(P);D=r(Ue,"A",{id:!0,class:!0,href:!0});var Ut=i(D);ae=r(Ut,"SPAN",{});var Yt=i(ae);O(W.$$.fragment,Yt),Yt.forEach(o),Ut.forEach(o),Xe=c(Ue),le=r(Ue,"SPAN",{});var Kt=i(le);Ze=a(Kt,"Requirements"),Kt.forEach(o),Ue.forEach(o),De=c(e),y=r(e,"UL",{});var j=i(y);pe=r(j,"LI",{});var Ft=i(pe);et=a(Ft,"Mac computer with Apple silicon (M1/M2) hardware."),Ft.forEach(o),tt=c(j),fe=r(j,"LI",{});var Jt=i(fe);ot=a(Jt,"macOS 12.6 or later (13.0 or later recommended)."),Jt.forEach(o),st=c(j),ue=r(j,"LI",{});var zt=i(ue);rt=a(zt,"arm64 version of Python."),zt.forEach(o),it=c(j),v=r(j,"LI",{});var E=i(v);nt=a(E,"PyTorch 2.0 (recommended) or 1.13 (minimum version supported for "),ce=r(E,"CODE",{});var Qt=i(ce);at=a(Qt,"mps"),Qt.forEach(o),lt=a(E,"). You can install it with "),me=r(E,"CODE",{});var Vt=i(me);pt=a(Vt,"pip"),Vt.forEach(o),ft=a(E," or "),he=r(E,"CODE",{});var Xt=i(he);ut=a(Xt,"conda"),Xt.forEach(o),ct=a(E," using the instructions in "),U=r(E,"A",{href:!0,rel:!0});var Zt=i(U);mt=a(Zt,"https://pytorch.org/get-started/locally/"),Zt.forEach(o),ht=a(E,"."),E.forEach(o),j.forEach(o),qe=c(e),k=r(e,"H2",{class:!0});var Ye=i(k);q=r(Ye,"A",{id:!0,class:!0,href:!0});var eo=i(q);de=r(eo,"SPAN",{});var to=i(de);O(Y.$$.fragment,to),to.forEach(o),eo.forEach(o),dt=c(Ye),ve=r(Ye,"SPAN",{});var oo=i(ve);vt=a(oo,"Inference Pipeline"),oo.forEach(o),Ye.forEach(o),Ie=c(e),$=r(e,"P",{});var oe=i($);wt=a(oe,"The snippet below demonstrates how to use the "),we=r(oe,"CODE",{});var so=i(we);yt=a(so,"mps"),so.forEach(o),_t=a(oe," backend using the familiar "),ye=r(oe,"CODE",{});var ro=i(ye);bt=a(ro,"to()"),ro.forEach(o),$t=a(oe," interface to move the Stable Diffusion pipeline to your M1 or M2 device."),oe.forEach(o),Re=c(e),O(I.$$.fragment,e),xe=c(e),Z=r(e,"P",{});var io=i(Z);gt=a(io,"We strongly recommend you use PyTorch 2 or better, as it solves a number of problems like the one described in the previous tip."),io.forEach(o),Ce=c(e),O(K.$$.fragment,e),je=c(e),A=r(e,"H2",{class:!0});var Ke=i(A);R=r(Ke,"A",{id:!0,class:!0,href:!0});var no=i(R);_e=r(no,"SPAN",{});var ao=i(_e);O(F.$$.fragment,ao),ao.forEach(o),no.forEach(o),Et=c(Ke),be=r(Ke,"SPAN",{});var lo=i(be);Pt=a(lo,"Performance Recommendations"),lo.forEach(o),Ke.forEach(o),Ne=c(e),ee=r(e,"P",{});var po=i(ee);kt=a(po,"M1/M2 performance is very sensitive to memory pressure. The system will automatically swap if it needs to, but performance will degrade significantly when it does."),po.forEach(o),Oe=c(e),g=r(e,"P",{});var se=i(g);At=a(se,"We recommend you use "),$e=r(se,"EM",{});var fo=i($e);Mt=a(fo,"attention slicing"),fo.forEach(o),St=a(se," to reduce memory pressure during inference and prevent swapping, particularly if your computer has less than 64 GB of system RAM, or if you generate images at non-standard resolutions larger than 512 \xD7 512 pixels. Attention slicing performs the costly attention operation in multiple steps instead of all at once. It usually has a performance impact of ~20% in computers without universal memory, but we have observed "),ge=r(se,"EM",{});var uo=i(ge);Tt=a(uo,"better performance"),uo.forEach(o),Dt=a(se," in most Apple Silicon computers, unless you have 64 GB or more."),se.forEach(o),Ge=c(e),O(J.$$.fragment,e),Le=c(e),M=r(e,"H2",{class:!0});var Fe=i(M);x=r(Fe,"A",{id:!0,class:!0,href:!0});var co=i(x);Ee=r(co,"SPAN",{});var mo=i(Ee);O(z.$$.fragment,mo),mo.forEach(o),co.forEach(o),qt=c(Fe),Pe=r(Fe,"SPAN",{});var ho=i(Pe);It=a(ho,"Known Issues"),ho.forEach(o),Fe.forEach(o),Be=c(e),te=r(e,"UL",{});var vo=i(te);S=r(vo,"LI",{});var re=i(S);Rt=a(re,"Generating multiple prompts in a batch "),Q=r(re,"A",{href:!0,rel:!0});var wo=i(Q);xt=a(wo,"crashes or doesn\u2019t work reliably"),wo.forEach(o),Ct=a(re,". We believe this is related to the "),C=r(re,"A",{href:!0,rel:!0});var Gt=i(C);ke=r(Gt,"CODE",{});var yo=i(ke);jt=a(yo,"mps"),yo.forEach(o),Nt=a(Gt," backend in PyTorch"),Gt.forEach(o),Ot=a(re,". This is being resolved, but for now we recommend to iterate instead of batching."),re.forEach(o),vo.forEach(o),this.h()},h(){p(m,"name","hf:doc:metadata"),p(m,"content",JSON.stringify(So)),p(w,"id","how-to-use-stable-diffusion-in-apple-silicon-m1m2"),p(w,"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"),p(w,"href","#how-to-use-stable-diffusion-in-apple-silicon-m1m2"),p(h,"class","relative group"),p(D,"id","requirements"),p(D,"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"),p(D,"href","#requirements"),p(P,"class","relative group"),p(U,"href","https://pytorch.org/get-started/locally/"),p(U,"rel","nofollow"),p(q,"id","inference-pipeline"),p(q,"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"),p(q,"href","#inference-pipeline"),p(k,"class","relative group"),p(R,"id","performance-recommendations"),p(R,"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"),p(R,"href","#performance-recommendations"),p(A,"class","relative group"),p(x,"id","known-issues"),p(x,"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"),p(x,"href","#known-issues"),p(M,"class","relative group"),p(Q,"href","https://github.com/huggingface/diffusers/issues/363"),p(Q,"rel","nofollow"),p(C,"href","https://github.com/pytorch/pytorch/issues/84039"),p(C,"rel","nofollow")},m(e,l){t(document.head,m),f(e,b,l),f(e,h,l),t(h,w),t(w,_),G(d,_,null),t(h,X),t(h,ie),t(ie,Je),f(e,Se,l),f(e,T,l),t(T,ze),t(T,ne),t(ne,Qe),t(T,Ve),f(e,Te,l),f(e,P,l),t(P,D),t(D,ae),G(W,ae,null),t(P,Xe),t(P,le),t(le,Ze),f(e,De,l),f(e,y,l),t(y,pe),t(pe,et),t(y,tt),t(y,fe),t(fe,ot),t(y,st),t(y,ue),t(ue,rt),t(y,it),t(y,v),t(v,nt),t(v,ce),t(ce,at),t(v,lt),t(v,me),t(me,pt),t(v,ft),t(v,he),t(he,ut),t(v,ct),t(v,U),t(U,mt),t(v,ht),f(e,qe,l),f(e,k,l),t(k,q),t(q,de),G(Y,de,null),t(k,dt),t(k,ve),t(ve,vt),f(e,Ie,l),f(e,$,l),t($,wt),t($,we),t(we,yt),t($,_t),t($,ye),t(ye,bt),t($,$t),f(e,Re,l),G(I,e,l),f(e,xe,l),f(e,Z,l),t(Z,gt),f(e,Ce,l),G(K,e,l),f(e,je,l),f(e,A,l),t(A,R),t(R,_e),G(F,_e,null),t(A,Et),t(A,be),t(be,Pt),f(e,Ne,l),f(e,ee,l),t(ee,kt),f(e,Oe,l),f(e,g,l),t(g,At),t(g,$e),t($e,Mt),t(g,St),t(g,ge),t(ge,Tt),t(g,Dt),f(e,Ge,l),G(J,e,l),f(e,Le,l),f(e,M,l),t(M,x),t(x,Ee),G(z,Ee,null),t(M,qt),t(M,Pe),t(Pe,It),f(e,Be,l),f(e,te,l),t(te,S),t(S,Rt),t(S,Q),t(Q,xt),t(S,Ct),t(S,C),t(C,ke),t(ke,jt),t(C,Nt),t(S,Ot),He=!0},p(e,[l]){const V={};l&2&&(V.$$scope={dirty:l,ctx:e}),I.$set(V)},i(e){He||(L(d.$$.fragment,e),L(W.$$.fragment,e),L(Y.$$.fragment,e),L(I.$$.fragment,e),L(K.$$.fragment,e),L(F.$$.fragment,e),L(J.$$.fragment,e),L(z.$$.fragment,e),He=!0)},o(e){B(d.$$.fragment,e),B(W.$$.fragment,e),B(Y.$$.fragment,e),B(I.$$.fragment,e),B(K.$$.fragment,e),B(F.$$.fragment,e),B(J.$$.fragment,e),B(z.$$.fragment,e),He=!1},d(e){o(m),e&&o(b),e&&o(h),H(d),e&&o(Se),e&&o(T),e&&o(Te),e&&o(P),H(W),e&&o(De),e&&o(y),e&&o(qe),e&&o(k),H(Y),e&&o(Ie),e&&o($),e&&o(Re),H(I,e),e&&o(xe),e&&o(Z),e&&o(Ce),H(K,e),e&&o(je),e&&o(A),H(F),e&&o(Ne),e&&o(ee),e&&o(Oe),e&&o(g),e&&o(Ge),H(J,e),e&&o(Le),e&&o(M),H(z),e&&o(Be),e&&o(te)}}}const So={local:"how-to-use-stable-diffusion-in-apple-silicon-m1m2",sections:[{local:"requirements",title:"Requirements"},{local:"inference-pipeline",title:"Inference Pipeline"},{local:"performance-recommendations",title:"Performance Recommendations"},{local:"known-issues",title:"Known Issues"}],title:"How to use Stable Diffusion in Apple Silicon (M1/M2)"};function To(Me){return Po(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class xo extends bo{constructor(m){super();$o(this,m,To,Mo,go,{})}}export{xo as default,So as metadata};
