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