import{s as ve,n as Je,o as Ze}from"../chunks/scheduler.53228c21.js";import{S as $e,i as Ge,e as a,s as i,c as m,h as Be,a as o,d as l,b as n,f as ge,g as c,j as p,k as oe,l as je,m as s,n as f,t as u,o as y,p as M}from"../chunks/index.100fac89.js";import{C as Ce}from"../chunks/CopyLLMTxtMenu.b0ff86f3.js";import{C as re}from"../chunks/CodeBlock.d30a6509.js";import{H as pe,E as He}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.136ef3fd.js";function Le(me){let r,x,R,V,b,W,w,E,h,ce='<p>Pipelines with a <img alt="MPS" src="https://img.shields.io/badge/MPS-000000?style=flat&amp;logo=apple&amp;logoColor=white%22"/> badge indicate a model can take advantage of the MPS backend on Apple silicon devices for faster inference. Feel free to open a <a href="https://github.com/huggingface/diffusers/compare" rel="nofollow">Pull Request</a> to add this badge to pipelines that are missing it.</p>',Y,U,fe='🤗 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:',k,T,ue='<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>',z,g,ye="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:",F,v,q,d,Me='<p>The PyTorch <a href="https://pytorch.org/docs/stable/notes/mps.html" rel="nofollow">mps</a> backend does not support NDArray sizes greater than <code>2**32</code>. Please open an <a href="https://github.com/huggingface/diffusers/issues/new/choose" rel="nofollow">Issue</a> if you encounter this problem so we can investigate.</p>',X,J,he="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.",Q,Z,A,$,N,G,de="This section lists some common issues with using the <code>mps</code> backend and how to solve them.",D,B,K,j,be="M1/M2 performance is very sensitive to memory pressure. When this occurs, the system automatically swaps if it needs to which significantly degrades performance.",O,C,we='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:',ee,H,te,L,Ue="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.",le,S,se,_,Te="Generating multiple prompts in a batch can crash or fail to work reliably. If this is the case, try iterating instead of batching.",ie,I,ne,P,ae;return b=new Ce({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),w=new pe({props:{title:"Metal Performance Shaders (MPS)",local:"metal-performance-shaders-mps",headingTag:"h1"}}),v=new re({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIpJTBBcGlwZSUyMCUzRCUyMHBpcGUudG8oJTIybXBzJTIyKSUwQSUwQSUyMyUyMFJlY29tbWVuZGVkJTIwaWYlMjB5b3VyJTIwY29tcHV0ZXIlMjBoYXMlMjAlM0MlMjA2NCUyMEdCJTIwb2YlMjBSQU0lMEFwaXBlLmVuYWJsZV9hdHRlbnRpb25fc2xpY2luZygpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQWltYWdlJTIwJTNEJTIwcGlwZShwcm9tcHQpLmltYWdlcyU1QjAlNUQlMEFpbWFnZQ==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;stable-diffusion-v1-5/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}}),Z=new re({props:{code:"JTIwJTIwZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBJTIwJTIwcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIpLnRvKCUyMm1wcyUyMiklMEElMjAlMjBwaXBlLmVuYWJsZV9hdHRlbnRpb25fc2xpY2luZygpJTBBJTBBJTIwJTIwcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQSUyMCUyMCUyMyUyMEZpcnN0LXRpbWUlMjAlMjJ3YXJtdXAlMjIlMjBwYXNzJTIwaWYlMjBQeVRvcmNoJTIwdmVyc2lvbiUyMGlzJTIwMS4xMyUwQSUyQiUyMF8lMjAlM0QlMjBwaXBlKHByb21wdCUyQyUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0QxKSUwQSUwQSUyMCUyMCUyMyUyMFJlc3VsdHMlMjBtYXRjaCUyMHRob3NlJTIwZnJvbSUyMHRoZSUyMENQVSUyMGRldmljZSUyMGFmdGVyJTIwdGhlJTIwd2FybXVwJTIwcGFzcy4lMEElMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGUocHJvbXB0KS5pbWFnZXMlNUIwJTVE",highlighted:`  from diffusers import DiffusionPipeline

  pipe = DiffusionPipeline.from_pretrained(&quot;stable-diffusion-v1-5/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}}),$=new pe({props:{title:"Troubleshoot",local:"troubleshoot",headingTag:"h2"}}),B=new pe({props:{title:"Attention slicing",local:"attention-slicing",headingTag:"h3"}}),H=new re({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEElMEFwaXBlbGluZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYlMkMlMjB2YXJpYW50JTNEJTIyZnAxNiUyMiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUpLnRvKCUyMm1wcyUyMiklMEFwaXBlbGluZS5lbmFibGVfYXR0ZW50aW9uX3NsaWNpbmcoKQ==",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;stable-diffusion-v1-5/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}}),S=new pe({props:{title:"Batch inference",local:"batch-inference",headingTag:"h3"}}),I=new He({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/mps.md"}}),{c(){r=a("meta"),x=i(),R=a("p"),V=i(),m(b.$$.fragment),W=i(),m(w.$$.fragment),E=i(),h=a("blockquote"),h.innerHTML=ce,Y=i(),U=a("p"),U.innerHTML=fe,k=i(),T=a("ul"),T.innerHTML=ue,z=i(),g=a("p"),g.innerHTML=ye,F=i(),m(v.$$.fragment),q=i(),d=a("blockquote"),d.innerHTML=Me,X=i(),J=a("p"),J.innerHTML=he,Q=i(),m(Z.$$.fragment),A=i(),m($.$$.fragment),N=i(),G=a("p"),G.innerHTML=de,D=i(),m(B.$$.fragment),K=i(),j=a("p"),j.textContent=be,O=i(),C=a("p"),C.innerHTML=we,ee=i(),m(H.$$.fragment),te=i(),L=a("p"),L.innerHTML=Ue,le=i(),m(S.$$.fragment),se=i(),_=a("p"),_.textContent=Te,ie=i(),m(I.$$.fragment),ne=i(),P=a("p"),this.h()},l(e){const t=Be("svelte-u9bgzb",document.head);r=o(t,"META",{name:!0,content:!0}),t.forEach(l),x=n(e),R=o(e,"P",{}),ge(R).forEach(l),V=n(e),c(b.$$.fragment,e),W=n(e),c(w.$$.fragment,e),E=n(e),h=o(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),p(h)!=="svelte-12cxje4"&&(h.innerHTML=ce),Y=n(e),U=o(e,"P",{"data-svelte-h":!0}),p(U)!=="svelte-j79ol3"&&(U.innerHTML=fe),k=n(e),T=o(e,"UL",{"data-svelte-h":!0}),p(T)!=="svelte-1rb0nsv"&&(T.innerHTML=ue),z=n(e),g=o(e,"P",{"data-svelte-h":!0}),p(g)!=="svelte-n7utwp"&&(g.innerHTML=ye),F=n(e),c(v.$$.fragment,e),q=n(e),d=o(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),p(d)!=="svelte-6dl78"&&(d.innerHTML=Me),X=n(e),J=o(e,"P",{"data-svelte-h":!0}),p(J)!=="svelte-1sliuep"&&(J.innerHTML=he),Q=n(e),c(Z.$$.fragment,e),A=n(e),c($.$$.fragment,e),N=n(e),G=o(e,"P",{"data-svelte-h":!0}),p(G)!=="svelte-161cphk"&&(G.innerHTML=de),D=n(e),c(B.$$.fragment,e),K=n(e),j=o(e,"P",{"data-svelte-h":!0}),p(j)!=="svelte-1oew7wr"&&(j.textContent=be),O=n(e),C=o(e,"P",{"data-svelte-h":!0}),p(C)!=="svelte-wm64dk"&&(C.innerHTML=we),ee=n(e),c(H.$$.fragment,e),te=n(e),L=o(e,"P",{"data-svelte-h":!0}),p(L)!=="svelte-xihi2j"&&(L.innerHTML=Ue),le=n(e),c(S.$$.fragment,e),se=n(e),_=o(e,"P",{"data-svelte-h":!0}),p(_)!=="svelte-lu9cjo"&&(_.textContent=Te),ie=n(e),c(I.$$.fragment,e),ne=n(e),P=o(e,"P",{}),ge(P).forEach(l),this.h()},h(){oe(r,"name","hf:doc:metadata"),oe(r,"content",Se),oe(h,"class","tip"),oe(d,"class","warning")},m(e,t){je(document.head,r),s(e,x,t),s(e,R,t),s(e,V,t),f(b,e,t),s(e,W,t),f(w,e,t),s(e,E,t),s(e,h,t),s(e,Y,t),s(e,U,t),s(e,k,t),s(e,T,t),s(e,z,t),s(e,g,t),s(e,F,t),f(v,e,t),s(e,q,t),s(e,d,t),s(e,X,t),s(e,J,t),s(e,Q,t),f(Z,e,t),s(e,A,t),f($,e,t),s(e,N,t),s(e,G,t),s(e,D,t),f(B,e,t),s(e,K,t),s(e,j,t),s(e,O,t),s(e,C,t),s(e,ee,t),f(H,e,t),s(e,te,t),s(e,L,t),s(e,le,t),f(S,e,t),s(e,se,t),s(e,_,t),s(e,ie,t),f(I,e,t),s(e,ne,t),s(e,P,t),ae=!0},p:Je,i(e){ae||(u(b.$$.fragment,e),u(w.$$.fragment,e),u(v.$$.fragment,e),u(Z.$$.fragment,e),u($.$$.fragment,e),u(B.$$.fragment,e),u(H.$$.fragment,e),u(S.$$.fragment,e),u(I.$$.fragment,e),ae=!0)},o(e){y(b.$$.fragment,e),y(w.$$.fragment,e),y(v.$$.fragment,e),y(Z.$$.fragment,e),y($.$$.fragment,e),y(B.$$.fragment,e),y(H.$$.fragment,e),y(S.$$.fragment,e),y(I.$$.fragment,e),ae=!1},d(e){e&&(l(x),l(R),l(V),l(W),l(E),l(h),l(Y),l(U),l(k),l(T),l(z),l(g),l(F),l(q),l(d),l(X),l(J),l(Q),l(A),l(N),l(G),l(D),l(K),l(j),l(O),l(C),l(ee),l(te),l(L),l(le),l(se),l(_),l(ie),l(ne),l(P)),l(r),M(b,e),M(w,e),M(v,e),M(Z,e),M($,e),M(B,e),M(H,e),M(S,e),M(I,e)}}}const Se='{"title":"Metal Performance Shaders (MPS)","local":"metal-performance-shaders-mps","sections":[{"title":"Troubleshoot","local":"troubleshoot","sections":[{"title":"Attention slicing","local":"attention-slicing","sections":[],"depth":3},{"title":"Batch inference","local":"batch-inference","sections":[],"depth":3}],"depth":2}],"depth":1}';function _e(me){return Ze(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class We extends $e{constructor(r){super(),Ge(this,r,_e,Le,ve,{})}}export{We as component};
