import{s as pe,o as re,n as me}from"../chunks/scheduler.8c3d61f6.js";import{S as ce,i as ue,g as p,s as n,r as $,A as fe,h as r,f as s,c as a,j as ne,u as Z,x as c,k as ae,y as he,a as l,v as j,d as G,t as I,w as C}from"../chunks/index.589a98e8.js";import{T as de}from"../chunks/Tip.42aa8582.js";import{C as D}from"../chunks/CodeBlock.36627b28.js";import{H as oe,E as ye}from"../chunks/EditOnGithub.e5a8d9cb.js";function Me(X){let i,u='Generating multiple prompts in a batch can <a href="https://github.com/huggingface/diffusers/issues/363" rel="nofollow">crash</a> or fail to work reliably. We believe this is related to the <a href="https://github.com/pytorch/pytorch/issues/84039" rel="nofollow"><code>mps</code></a> backend in PyTorch. While this is being investigated, you should iterate instead of batching.';return{c(){i=p("p"),i.innerHTML=u},l(o){i=r(o,"P",{"data-svelte-h":!0}),c(i)!=="svelte-17wj08e"&&(i.innerHTML=u)},m(o,W){l(o,i,W)},p:me,d(o){o&&s(i)}}}function we(X){let i,u,o,W,f,H,h,F='🤗 Diffusers is compatible with Apple silicon (M1/M2 chips) using the PyTorch <a href="https://pytorch.org/docs/stable/notes/mps.html" rel="nofollow"><code>mps</code></a> device, which uses the Metal framework to leverage the GPU on MacOS devices. You’ll need to have:',S,d,K='<li>macOS computer with Apple silicon (M1/M2) hardware</li> <li>macOS 12.6 or later (13.0 or later recommended)</li> <li>arm64 version of Python</li> <li><a href="https://pytorch.org/get-started/locally/" rel="nofollow">PyTorch 2.0</a> (recommended) or 1.13 (minimum version supported for <code>mps</code>)</li>',E,y,O="The <code>mps</code> backend uses PyTorch’s <code>.to()</code> interface to move the Stable Diffusion pipeline on to your M1 or M2 device:",P,M,R,m,Y,w,ee="If you’re using <strong>PyTorch 1.13</strong>, you need to “prime” 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.",V,T,k,J,z,b,te="M1/M2 performance is very sensitive to memory pressure. When this occurs, the system automatically swaps if it needs to which significantly degrades performance.",N,v,se='To prevent this from happening, we recommend <em>attention slicing</em> 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×512 pixels. Call the <a href="/docs/diffusers/main/en/api/pipelines/overview#diffusers.DiffusionPipeline.enable_attention_slicing">enable_attention_slicing()</a> function on your pipeline:',x,U,L,g,le="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’ve observed <em>better performance</em> in most Apple silicon computers unless you have 64GB of RAM or more.",q,B,Q,_,A;return f=new oe({props:{title:"Metal Performance Shaders (MPS)",local:"metal-performance-shaders-mps",headingTag:"h1"}}),M=new D({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiklMEFwaXBlJTIwJTNEJTIwcGlwZS50byglMjJtcHMlMjIpJTBBJTBBJTIzJTIwUmVjb21tZW5kZWQlMjBpZiUyMHlvdXIlMjBjb21wdXRlciUyMGhhcyUyMCUzQyUyMDY0JTIwR0IlMjBvZiUyMFJBTSUwQXBpcGUuZW5hYmxlX2F0dGVudGlvbl9zbGljaW5nKCklMEElMEFwcm9tcHQlMjAlM0QlMjAlMjJhJTIwcGhvdG8lMjBvZiUyMGFuJTIwYXN0cm9uYXV0JTIwcmlkaW5nJTIwYSUyMGhvcnNlJTIwb24lMjBtYXJzJTIyJTBBaW1hZ2UlMjAlM0QlMjBwaXBlKHByb21wdCkuaW1hZ2VzJTVCMCU1RCUwQWltYWdl",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>
image = pipe(prompt).images[<span class="hljs-number">0</span>]
image`,wrap:!1}}),m=new de({props:{warning:!0,$$slots:{default:[Me]},$$scope:{ctx:X}}}),T=new D({props:{code:"JTIwJTIwZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBJTIwJTIwcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMikudG8oJTIybXBzJTIyKSUwQSUyMCUyMHBpcGUuZW5hYmxlX2F0dGVudGlvbl9zbGljaW5nKCklMEElMEElMjAlMjBwcm9tcHQlMjAlM0QlMjAlMjJhJTIwcGhvdG8lMjBvZiUyMGFuJTIwYXN0cm9uYXV0JTIwcmlkaW5nJTIwYSUyMGhvcnNlJTIwb24lMjBtYXJzJTIyJTBBJTIwJTIwJTIzJTIwRmlyc3QtdGltZSUyMCUyMndhcm11cCUyMiUyMHBhc3MlMjBpZiUyMFB5VG9yY2glMjB2ZXJzaW9uJTIwaXMlMjAxLjEzJTBBJTJCJTIwXyUyMCUzRCUyMHBpcGUocHJvbXB0JTJDJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRDEpJTBBJTBBJTIwJTIwJTIzJTIwUmVzdWx0cyUyMG1hdGNoJTIwdGhvc2UlMjBmcm9tJTIwdGhlJTIwQ1BVJTIwZGV2aWNlJTIwYWZ0ZXIlMjB0aGUlMjB3YXJtdXAlMjBwYXNzLiUwQSUyMCUyMGltYWdlJTIwJTNEJTIwcGlwZShwcm9tcHQpLmltYWdlcyU1QjAlNUQ=",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]`,wrap:!1}}),J=new oe({props:{title:"Troubleshoot",local:"troubleshoot",headingTag:"h2"}}),U=new D({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEElMEFwaXBlbGluZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJydW53YXltbCUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHZhcmlhbnQlM0QlMjJmcDE2JTIyJTJDJTIwdXNlX3NhZmV0ZW5zb3JzJTNEVHJ1ZSkudG8oJTIybXBzJTIyKSUwQXBpcGVsaW5lLmVuYWJsZV9hdHRlbnRpb25fc2xpY2luZygp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-keyword">import</span> torch

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()`,wrap:!1}}),B=new ye({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/mps.md"}}),{c(){i=p("meta"),u=n(),o=p("p"),W=n(),$(f.$$.fragment),H=n(),h=p("p"),h.innerHTML=F,S=n(),d=p("ul"),d.innerHTML=K,E=n(),y=p("p"),y.innerHTML=O,P=n(),$(M.$$.fragment),R=n(),$(m.$$.fragment),Y=n(),w=p("p"),w.innerHTML=ee,V=n(),$(T.$$.fragment),k=n(),$(J.$$.fragment),z=n(),b=p("p"),b.textContent=te,N=n(),v=p("p"),v.innerHTML=se,x=n(),$(U.$$.fragment),L=n(),g=p("p"),g.innerHTML=le,q=n(),$(B.$$.fragment),Q=n(),_=p("p"),this.h()},l(e){const t=fe("svelte-u9bgzb",document.head);i=r(t,"META",{name:!0,content:!0}),t.forEach(s),u=a(e),o=r(e,"P",{}),ne(o).forEach(s),W=a(e),Z(f.$$.fragment,e),H=a(e),h=r(e,"P",{"data-svelte-h":!0}),c(h)!=="svelte-j79ol3"&&(h.innerHTML=F),S=a(e),d=r(e,"UL",{"data-svelte-h":!0}),c(d)!=="svelte-1rb0nsv"&&(d.innerHTML=K),E=a(e),y=r(e,"P",{"data-svelte-h":!0}),c(y)!=="svelte-n7utwp"&&(y.innerHTML=O),P=a(e),Z(M.$$.fragment,e),R=a(e),Z(m.$$.fragment,e),Y=a(e),w=r(e,"P",{"data-svelte-h":!0}),c(w)!=="svelte-1sliuep"&&(w.innerHTML=ee),V=a(e),Z(T.$$.fragment,e),k=a(e),Z(J.$$.fragment,e),z=a(e),b=r(e,"P",{"data-svelte-h":!0}),c(b)!=="svelte-1oew7wr"&&(b.textContent=te),N=a(e),v=r(e,"P",{"data-svelte-h":!0}),c(v)!=="svelte-wm64dk"&&(v.innerHTML=se),x=a(e),Z(U.$$.fragment,e),L=a(e),g=r(e,"P",{"data-svelte-h":!0}),c(g)!=="svelte-xihi2j"&&(g.innerHTML=le),q=a(e),Z(B.$$.fragment,e),Q=a(e),_=r(e,"P",{}),ne(_).forEach(s),this.h()},h(){ae(i,"name","hf:doc:metadata"),ae(i,"content",Te)},m(e,t){he(document.head,i),l(e,u,t),l(e,o,t),l(e,W,t),j(f,e,t),l(e,H,t),l(e,h,t),l(e,S,t),l(e,d,t),l(e,E,t),l(e,y,t),l(e,P,t),j(M,e,t),l(e,R,t),j(m,e,t),l(e,Y,t),l(e,w,t),l(e,V,t),j(T,e,t),l(e,k,t),j(J,e,t),l(e,z,t),l(e,b,t),l(e,N,t),l(e,v,t),l(e,x,t),j(U,e,t),l(e,L,t),l(e,g,t),l(e,q,t),j(B,e,t),l(e,Q,t),l(e,_,t),A=!0},p(e,[t]){const ie={};t&2&&(ie.$$scope={dirty:t,ctx:e}),m.$set(ie)},i(e){A||(G(f.$$.fragment,e),G(M.$$.fragment,e),G(m.$$.fragment,e),G(T.$$.fragment,e),G(J.$$.fragment,e),G(U.$$.fragment,e),G(B.$$.fragment,e),A=!0)},o(e){I(f.$$.fragment,e),I(M.$$.fragment,e),I(m.$$.fragment,e),I(T.$$.fragment,e),I(J.$$.fragment,e),I(U.$$.fragment,e),I(B.$$.fragment,e),A=!1},d(e){e&&(s(u),s(o),s(W),s(H),s(h),s(S),s(d),s(E),s(y),s(P),s(R),s(Y),s(w),s(V),s(k),s(z),s(b),s(N),s(v),s(x),s(L),s(g),s(q),s(Q),s(_)),s(i),C(f,e),C(M,e),C(m,e),C(T,e),C(J,e),C(U,e),C(B,e)}}}const Te='{"title":"Metal Performance Shaders (MPS)","local":"metal-performance-shaders-mps","sections":[{"title":"Troubleshoot","local":"troubleshoot","sections":[],"depth":2}],"depth":1}';function Je(X){return re(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class $e extends ce{constructor(i){super(),ue(this,i,Je,we,pe,{})}}export{$e as component};
