import{s as Te,o as ge,n as ve}from"../chunks/scheduler.8c3d61f6.js";import{S as $e,i as Je,g as o,s as i,r as c,A as Ze,h as p,f as s,c as n,j as Ue,u as f,x as m,k as oe,y as Ge,a as l,v as u,d as y,t as M,w as h}from"../chunks/index.da70eac4.js";import{T as Be}from"../chunks/Tip.6f698f24.js";import{C as pe}from"../chunks/CodeBlock.a9c4becf.js";import{H as ae,E as je}from"../chunks/getInferenceSnippets.cb466318.js";function Ce(W){let a,w='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.';return{c(){a=o("p"),a.innerHTML=w},l(r){a=p(r,"P",{"data-svelte-h":!0}),m(a)!=="svelte-1054fmo"&&(a.innerHTML=w)},m(r,R){l(r,a,R)},p:ve,d(r){r&&s(a)}}}function He(W){let a,w,r,R,U,Y,d,re='<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>',x,T,me='🤗 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:',E,g,ce='<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,v,fe="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:",k,$,F,b,X,J,ue="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,G,Q,B,ye="This section lists some common issues with using the <code>mps</code> backend and how to solve them.",N,j,D,C,Me="M1/M2 performance is very sensitive to memory pressure. When this occurs, the system automatically swaps if it needs to which significantly degrades performance.",K,H,he='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:',O,_,ee,S,de="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.",te,L,se,I,be="Generating multiple prompts in a batch can crash or fail to work reliably. If this is the case, try iterating instead of batching.",le,P,ie,V,ne;return U=new ae({props:{title:"Metal Performance Shaders (MPS)",local:"metal-performance-shaders-mps",headingTag:"h1"}}),$=new pe({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}}),b=new Be({props:{warning:!0,$$slots:{default:[Ce]},$$scope:{ctx:W}}}),Z=new pe({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}}),G=new ae({props:{title:"Troubleshoot",local:"troubleshoot",headingTag:"h2"}}),j=new ae({props:{title:"Attention slicing",local:"attention-slicing",headingTag:"h3"}}),_=new pe({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}}),L=new ae({props:{title:"Batch inference",local:"batch-inference",headingTag:"h3"}}),P=new je({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/mps.md"}}),{c(){a=o("meta"),w=i(),r=o("p"),R=i(),c(U.$$.fragment),Y=i(),d=o("blockquote"),d.innerHTML=re,x=i(),T=o("p"),T.innerHTML=me,E=i(),g=o("ul"),g.innerHTML=ce,z=i(),v=o("p"),v.innerHTML=fe,k=i(),c($.$$.fragment),F=i(),c(b.$$.fragment),X=i(),J=o("p"),J.innerHTML=ue,q=i(),c(Z.$$.fragment),A=i(),c(G.$$.fragment),Q=i(),B=o("p"),B.innerHTML=ye,N=i(),c(j.$$.fragment),D=i(),C=o("p"),C.textContent=Me,K=i(),H=o("p"),H.innerHTML=he,O=i(),c(_.$$.fragment),ee=i(),S=o("p"),S.innerHTML=de,te=i(),c(L.$$.fragment),se=i(),I=o("p"),I.textContent=be,le=i(),c(P.$$.fragment),ie=i(),V=o("p"),this.h()},l(e){const t=Ze("svelte-u9bgzb",document.head);a=p(t,"META",{name:!0,content:!0}),t.forEach(s),w=n(e),r=p(e,"P",{}),Ue(r).forEach(s),R=n(e),f(U.$$.fragment,e),Y=n(e),d=p(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),m(d)!=="svelte-12cxje4"&&(d.innerHTML=re),x=n(e),T=p(e,"P",{"data-svelte-h":!0}),m(T)!=="svelte-j79ol3"&&(T.innerHTML=me),E=n(e),g=p(e,"UL",{"data-svelte-h":!0}),m(g)!=="svelte-1rb0nsv"&&(g.innerHTML=ce),z=n(e),v=p(e,"P",{"data-svelte-h":!0}),m(v)!=="svelte-n7utwp"&&(v.innerHTML=fe),k=n(e),f($.$$.fragment,e),F=n(e),f(b.$$.fragment,e),X=n(e),J=p(e,"P",{"data-svelte-h":!0}),m(J)!=="svelte-1sliuep"&&(J.innerHTML=ue),q=n(e),f(Z.$$.fragment,e),A=n(e),f(G.$$.fragment,e),Q=n(e),B=p(e,"P",{"data-svelte-h":!0}),m(B)!=="svelte-161cphk"&&(B.innerHTML=ye),N=n(e),f(j.$$.fragment,e),D=n(e),C=p(e,"P",{"data-svelte-h":!0}),m(C)!=="svelte-1oew7wr"&&(C.textContent=Me),K=n(e),H=p(e,"P",{"data-svelte-h":!0}),m(H)!=="svelte-wm64dk"&&(H.innerHTML=he),O=n(e),f(_.$$.fragment,e),ee=n(e),S=p(e,"P",{"data-svelte-h":!0}),m(S)!=="svelte-xihi2j"&&(S.innerHTML=de),te=n(e),f(L.$$.fragment,e),se=n(e),I=p(e,"P",{"data-svelte-h":!0}),m(I)!=="svelte-lu9cjo"&&(I.textContent=be),le=n(e),f(P.$$.fragment,e),ie=n(e),V=p(e,"P",{}),Ue(V).forEach(s),this.h()},h(){oe(a,"name","hf:doc:metadata"),oe(a,"content",_e),oe(d,"class","tip")},m(e,t){Ge(document.head,a),l(e,w,t),l(e,r,t),l(e,R,t),u(U,e,t),l(e,Y,t),l(e,d,t),l(e,x,t),l(e,T,t),l(e,E,t),l(e,g,t),l(e,z,t),l(e,v,t),l(e,k,t),u($,e,t),l(e,F,t),u(b,e,t),l(e,X,t),l(e,J,t),l(e,q,t),u(Z,e,t),l(e,A,t),u(G,e,t),l(e,Q,t),l(e,B,t),l(e,N,t),u(j,e,t),l(e,D,t),l(e,C,t),l(e,K,t),l(e,H,t),l(e,O,t),u(_,e,t),l(e,ee,t),l(e,S,t),l(e,te,t),u(L,e,t),l(e,se,t),l(e,I,t),l(e,le,t),u(P,e,t),l(e,ie,t),l(e,V,t),ne=!0},p(e,[t]){const we={};t&2&&(we.$$scope={dirty:t,ctx:e}),b.$set(we)},i(e){ne||(y(U.$$.fragment,e),y($.$$.fragment,e),y(b.$$.fragment,e),y(Z.$$.fragment,e),y(G.$$.fragment,e),y(j.$$.fragment,e),y(_.$$.fragment,e),y(L.$$.fragment,e),y(P.$$.fragment,e),ne=!0)},o(e){M(U.$$.fragment,e),M($.$$.fragment,e),M(b.$$.fragment,e),M(Z.$$.fragment,e),M(G.$$.fragment,e),M(j.$$.fragment,e),M(_.$$.fragment,e),M(L.$$.fragment,e),M(P.$$.fragment,e),ne=!1},d(e){e&&(s(w),s(r),s(R),s(Y),s(d),s(x),s(T),s(E),s(g),s(z),s(v),s(k),s(F),s(X),s(J),s(q),s(A),s(Q),s(B),s(N),s(D),s(C),s(K),s(H),s(O),s(ee),s(S),s(te),s(se),s(I),s(le),s(ie),s(V)),s(a),h(U,e),h($,e),h(b,e),h(Z,e),h(G,e),h(j,e),h(_,e),h(L,e),h(P,e)}}}const _e='{"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 Se(W){return ge(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class We extends $e{constructor(a){super(),Je(this,a,Se,He,Te,{})}}export{We as component};
