import{s as pe,o as re,n as me}from"../chunks/scheduler.8c3d61f6.js";import{S as ce,i as fe,g as p,s as n,r as G,A as ue,h as r,f as s,c as o,j as ne,u as $,x as c,k as oe,y as ye,a as l,v as B,d as j,t as H,w as _}from"../chunks/index.da70eac4.js";import{T as Me}from"../chunks/Tip.1d9b8c37.js";import{C as N}from"../chunks/CodeBlock.00a903b3.js";import{H as ae,E as de}from"../chunks/EditOnGithub.1e64e623.js";function he(I){let i,f='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=f},l(a){i=r(a,"P",{"data-svelte-h":!0}),c(i)!=="svelte-17wj08e"&&(i.innerHTML=f)},m(a,C){l(a,i,C)},p:me,d(a){a&&s(i)}}}function be(I){let i,f,a,C,u,R,y,D='🤗 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:',W,M,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>',L,d,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:",V,h,Y,m,P,b,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.",E,w,z,U,F,T,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.",x,J,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/pr_10133/en/api/pipelines/overview#diffusers.DiffusionPipeline.enable_attention_slicing">enable_attention_slicing()</a> function on your pipeline:',X,v,k,Z,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,g,Q,S,A;return u=new ae({props:{title:"Metal Performance Shaders (MPS)",local:"metal-performance-shaders-mps",headingTag:"h1"}}),h=new N({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}}),m=new Me({props:{warning:!0,$$slots:{default:[he]},$$scope:{ctx:I}}}),w=new N({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}}),U=new ae({props:{title:"Troubleshoot",local:"troubleshoot",headingTag:"h2"}}),v=new N({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}}),g=new de({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/mps.md"}}),{c(){i=p("meta"),f=n(),a=p("p"),C=n(),G(u.$$.fragment),R=n(),y=p("p"),y.innerHTML=D,W=n(),M=p("ul"),M.innerHTML=K,L=n(),d=p("p"),d.innerHTML=O,V=n(),G(h.$$.fragment),Y=n(),G(m.$$.fragment),P=n(),b=p("p"),b.innerHTML=ee,E=n(),G(w.$$.fragment),z=n(),G(U.$$.fragment),F=n(),T=p("p"),T.textContent=te,x=n(),J=p("p"),J.innerHTML=se,X=n(),G(v.$$.fragment),k=n(),Z=p("p"),Z.innerHTML=le,q=n(),G(g.$$.fragment),Q=n(),S=p("p"),this.h()},l(e){const t=ue("svelte-u9bgzb",document.head);i=r(t,"META",{name:!0,content:!0}),t.forEach(s),f=o(e),a=r(e,"P",{}),ne(a).forEach(s),C=o(e),$(u.$$.fragment,e),R=o(e),y=r(e,"P",{"data-svelte-h":!0}),c(y)!=="svelte-j79ol3"&&(y.innerHTML=D),W=o(e),M=r(e,"UL",{"data-svelte-h":!0}),c(M)!=="svelte-1rb0nsv"&&(M.innerHTML=K),L=o(e),d=r(e,"P",{"data-svelte-h":!0}),c(d)!=="svelte-n7utwp"&&(d.innerHTML=O),V=o(e),$(h.$$.fragment,e),Y=o(e),$(m.$$.fragment,e),P=o(e),b=r(e,"P",{"data-svelte-h":!0}),c(b)!=="svelte-1sliuep"&&(b.innerHTML=ee),E=o(e),$(w.$$.fragment,e),z=o(e),$(U.$$.fragment,e),F=o(e),T=r(e,"P",{"data-svelte-h":!0}),c(T)!=="svelte-1oew7wr"&&(T.textContent=te),x=o(e),J=r(e,"P",{"data-svelte-h":!0}),c(J)!=="svelte-1t1hdly"&&(J.innerHTML=se),X=o(e),$(v.$$.fragment,e),k=o(e),Z=r(e,"P",{"data-svelte-h":!0}),c(Z)!=="svelte-xihi2j"&&(Z.innerHTML=le),q=o(e),$(g.$$.fragment,e),Q=o(e),S=r(e,"P",{}),ne(S).forEach(s),this.h()},h(){oe(i,"name","hf:doc:metadata"),oe(i,"content",we)},m(e,t){ye(document.head,i),l(e,f,t),l(e,a,t),l(e,C,t),B(u,e,t),l(e,R,t),l(e,y,t),l(e,W,t),l(e,M,t),l(e,L,t),l(e,d,t),l(e,V,t),B(h,e,t),l(e,Y,t),B(m,e,t),l(e,P,t),l(e,b,t),l(e,E,t),B(w,e,t),l(e,z,t),B(U,e,t),l(e,F,t),l(e,T,t),l(e,x,t),l(e,J,t),l(e,X,t),B(v,e,t),l(e,k,t),l(e,Z,t),l(e,q,t),B(g,e,t),l(e,Q,t),l(e,S,t),A=!0},p(e,[t]){const ie={};t&2&&(ie.$$scope={dirty:t,ctx:e}),m.$set(ie)},i(e){A||(j(u.$$.fragment,e),j(h.$$.fragment,e),j(m.$$.fragment,e),j(w.$$.fragment,e),j(U.$$.fragment,e),j(v.$$.fragment,e),j(g.$$.fragment,e),A=!0)},o(e){H(u.$$.fragment,e),H(h.$$.fragment,e),H(m.$$.fragment,e),H(w.$$.fragment,e),H(U.$$.fragment,e),H(v.$$.fragment,e),H(g.$$.fragment,e),A=!1},d(e){e&&(s(f),s(a),s(C),s(R),s(y),s(W),s(M),s(L),s(d),s(V),s(Y),s(P),s(b),s(E),s(z),s(F),s(T),s(x),s(J),s(X),s(k),s(Z),s(q),s(Q),s(S)),s(i),_(u,e),_(h,e),_(m,e),_(w,e),_(U,e),_(v,e),_(g,e)}}}const we='{"title":"Metal Performance Shaders (MPS)","local":"metal-performance-shaders-mps","sections":[{"title":"Troubleshoot","local":"troubleshoot","sections":[],"depth":2}],"depth":1}';function Ue(I){return re(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ge extends ce{constructor(i){super(),fe(this,i,Ue,be,pe,{})}}export{Ge as component};
