import{s as ge,o as be,n as we}from"../chunks/scheduler.5c93273d.js";import{S as Be,i as Ze,g as o,s as n,r as f,A as Ge,h as U,f as l,c as p,j as $e,u as r,x as m,k as he,y as Ce,a as s,v as T,d as c,t as u,w as M}from"../chunks/index.e43dd92b.js";import{T as de}from"../chunks/Tip.1cbfe904.js";import{C as ae}from"../chunks/CodeBlock.6896320e.js";import{H as pe,E as Ve}from"../chunks/getInferenceSnippets.d410b42d.js";function ve(E){let i,J='带有 <img alt="MPS" src="https://img.shields.io/badge/MPS-000000?style=flat&amp;logo=apple&amp;logoColor=white%22"/> 徽章的管道表示模型可以利用 Apple silicon 设备上的 MPS 后端进行更快的推理。欢迎提交 <a href="https://github.com/huggingface/diffusers/compare" rel="nofollow">Pull Request</a> 来为缺少此徽章的管道添加它。';return{c(){i=o("p"),i.innerHTML=J},l(a){i=U(a,"P",{"data-svelte-h":!0}),m(i)!=="svelte-1sc4wjh"&&(i.innerHTML=J)},m(a,y){s(a,i,y)},p:we,d(a){a&&l(i)}}}function Re(E){let i,J='PyTorch <a href="https://pytorch.org/docs/stable/notes/mps.html" rel="nofollow">mps</a> 后端不支持大小超过 <code>2**32</code> 的 NDArray。如果您遇到此问题，请提交 <a href="https://github.com/huggingface/diffusers/issues/new/choose" rel="nofollow">Issue</a> 以便我们调查。';return{c(){i=o("p"),i.innerHTML=J},l(a){i=U(a,"P",{"data-svelte-h":!0}),m(i)!=="svelte-ls7332"&&(i.innerHTML=J)},m(a,y){s(a,i,y)},p:we,d(a){a&&l(i)}}}function _e(E){let i,J,a,y,d,j,$,P,w,oe='🤗 Diffusers 与 Apple silicon（M1/M2 芯片）兼容，使用 PyTorch 的 <a href="https://pytorch.org/docs/stable/notes/mps.html" rel="nofollow"><code>mps</code></a> 设备，该设备利用 Metal 框架来发挥 MacOS 设备上 GPU 的性能。您需要具备：',W,g,Ue='<li>配备 Apple silicon（M1/M2）硬件的 macOS 计算机</li> <li>macOS 12.6 或更高版本（推荐 13.0 或更高）</li> <li>arm64 版本的 Python</li> <li><a href="https://pytorch.org/get-started/locally/" rel="nofollow">PyTorch 2.0</a>（推荐）或 1.13（支持 <code>mps</code> 的最低版本）</li>',k,b,me="<code>mps</code> 后端使用 PyTorch 的 <code>.to()</code> 接口将 Stable Diffusion 管道移动到您的 M1 或 M2 设备上：",x,B,Y,h,Q,Z,Je="如果您使用 <strong>PyTorch 1.13</strong>，您需要通过管道进行一次额外的”预热”传递。这是一个临时解决方法，用于解决首次推理传递产生的结果与后续传递略有不同的问题。您只需要执行此传递一次，并且在仅进行一次推理步骤后可以丢弃结果。",N,G,D,C,q,V,fe="本节列出了使用 <code>mps</code> 后端时的一些常见问题及其解决方法。",X,v,A,R,re="M1/M2 性能对内存压力非常敏感。当发生这种情况时，系统会自动交换内存，这会显著降低性能。",O,_,Te="为了防止这种情况发生，我们建议使用<em>注意力切片</em>来减少推理过程中的内存压力并防止交换。这在您的计算机系统内存少于 64GB 或生成非标准分辨率（大于 512×512 像素）的图像时尤其相关。在您的管道上调用 <code>enable_attention_slicing()</code> 函数：",K,S,ee,F,ce="注意力切片将昂贵的注意力操作分多个步骤执行，而不是一次性完成。在没有统一内存的计算机中，它通常能提高约 20% 的性能，但我们观察到在大多数 Apple 芯片计算机中，除非您有 64GB 或更多 RAM，否则性能会<em>更好</em>。",te,H,le,I,ue="批量生成多个提示可能会导致崩溃或无法可靠工作。如果是这种情况，请尝试迭代而不是批量处理。",se,z,ie,L,ne;return d=new pe({props:{title:"Metal Performance Shaders (MPS)",local:"metal-performance-shaders-mps",headingTag:"h1"}}),$=new de({props:{warning:!1,$$slots:{default:[ve]},$$scope:{ctx:E}}}),B=new ae({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIpJTBBcGlwZSUyMCUzRCUyMHBpcGUudG8oJTIybXBzJTIyKSUwQSUwQSUyMyUyMCVFNSVBNiU4MiVFNiU5RSU5QyVFNiU4MiVBOCVFNyU5QSU4NCVFOCVBRSVBMSVFNyVBRSU5NyVFNiU5QyVCQSVFNSU4NiU4NSVFNSVBRCU5OCVFNSVCMCU4RiVFNCVCQSU4RSUyMDY0JTIwR0IlRUYlQkMlOEMlRTYlOEUlQTglRTglOEQlOTAlRTQlQkQlQkYlRTclOTQlQTglMEFwaXBlLmVuYWJsZV9hdHRlbnRpb25fc2xpY2luZygpJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQWltYWdlJTIwJTNEJTIwcGlwZShwcm9tcHQpLmltYWdlcyU1QjAlNUQlMEFpbWFnZQ==",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"># 如果您的计算机内存小于 64 GB，推荐使用</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}}),h=new de({props:{warning:!0,$$slots:{default:[Re]},$$scope:{ctx:E}}}),G=new ae({props:{code:"JTIwJTIwZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMERpZmZ1c2lvblBpcGVsaW5lJTBBJTBBJTIwJTIwcGlwZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIpLnRvKCUyMm1wcyUyMiklMEElMjAlMjBwaXBlLmVuYWJsZV9hdHRlbnRpb25fc2xpY2luZygpJTBBJTBBJTIwJTIwcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhbiUyMGFzdHJvbmF1dCUyMHJpZGluZyUyMGElMjBob3JzZSUyMG9uJTIwbWFycyUyMiUwQSUyMCUyMCUyMyUyMCVFNSVBNiU4MiVFNiU5RSU5QyUyMFB5VG9yY2glMjAlRTclODklODglRTYlOUMlQUMlRTYlOTglQUYlMjAxLjEzJUVGJUJDJThDJUU4JUJGJTlCJUU4JUExJThDJUU5JUE2JTk2JUU2JUFDJUExJTIyJUU5JUEyJTg0JUU3JTgzJUFEJTIyJUU0JUJDJUEwJUU5JTgwJTkyJTBBJTJCJTIwXyUyMCUzRCUyMHBpcGUocHJvbXB0JTJDJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRDEpJTBBJTBBJTIwJTIwJTIzJTIwJUU5JUEyJTg0JUU3JTgzJUFEJUU0JUJDJUEwJUU5JTgwJTkyJUU1JTkwJThFJUVGJUJDJThDJUU3JUJCJTkzJUU2JTlFJTlDJUU0JUI4JThFJTIwQ1BVJTIwJUU4JUFFJUJFJUU1JUE0JTg3JUU0JUI4JThBJUU3JTlBJTg0JUU3JUJCJTkzJUU2JTlFJTlDJUU1JThDJUI5JUU5JTg1JThEJUUzJTgwJTgyJTBBJTIwJTIwaW1hZ2UlMjAlM0QlMjBwaXBlKHByb21wdCkuaW1hZ2VzJTVCMCU1RA==",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;
  # 如果 PyTorch 版本是 1.13，进行首次&quot;预热&quot;传递
<span class="hljs-addition">+ _ = pipe(prompt, num_inference_steps=1)</span>

  # 预热传递后，结果与 CPU 设备上的结果匹配。
  image = pipe(prompt).images[0]`,wrap:!1}}),C=new pe({props:{title:"故障排除",local:"故障排除",headingTag:"h2"}}),v=new pe({props:{title:"注意力切片",local:"注意力切片",headingTag:"h3"}}),S=new ae({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}}),H=new pe({props:{title:"批量推理",local:"批量推理",headingTag:"h3"}}),z=new Ve({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/optimization/mps.md"}}),{c(){i=o("meta"),J=n(),a=o("p"),y=n(),f(d.$$.fragment),j=n(),f($.$$.fragment),P=n(),w=o("p"),w.innerHTML=oe,W=n(),g=o("ul"),g.innerHTML=Ue,k=n(),b=o("p"),b.innerHTML=me,x=n(),f(B.$$.fragment),Y=n(),f(h.$$.fragment),Q=n(),Z=o("p"),Z.innerHTML=Je,N=n(),f(G.$$.fragment),D=n(),f(C.$$.fragment),q=n(),V=o("p"),V.innerHTML=fe,X=n(),f(v.$$.fragment),A=n(),R=o("p"),R.textContent=re,O=n(),_=o("p"),_.innerHTML=Te,K=n(),f(S.$$.fragment),ee=n(),F=o("p"),F.innerHTML=ce,te=n(),f(H.$$.fragment),le=n(),I=o("p"),I.textContent=ue,se=n(),f(z.$$.fragment),ie=n(),L=o("p"),this.h()},l(e){const t=Ge("svelte-u9bgzb",document.head);i=U(t,"META",{name:!0,content:!0}),t.forEach(l),J=p(e),a=U(e,"P",{}),$e(a).forEach(l),y=p(e),r(d.$$.fragment,e),j=p(e),r($.$$.fragment,e),P=p(e),w=U(e,"P",{"data-svelte-h":!0}),m(w)!=="svelte-y25hik"&&(w.innerHTML=oe),W=p(e),g=U(e,"UL",{"data-svelte-h":!0}),m(g)!=="svelte-cef1u9"&&(g.innerHTML=Ue),k=p(e),b=U(e,"P",{"data-svelte-h":!0}),m(b)!=="svelte-171v7e8"&&(b.innerHTML=me),x=p(e),r(B.$$.fragment,e),Y=p(e),r(h.$$.fragment,e),Q=p(e),Z=U(e,"P",{"data-svelte-h":!0}),m(Z)!=="svelte-7zprdl"&&(Z.innerHTML=Je),N=p(e),r(G.$$.fragment,e),D=p(e),r(C.$$.fragment,e),q=p(e),V=U(e,"P",{"data-svelte-h":!0}),m(V)!=="svelte-ql61eg"&&(V.innerHTML=fe),X=p(e),r(v.$$.fragment,e),A=p(e),R=U(e,"P",{"data-svelte-h":!0}),m(R)!=="svelte-1akzsmv"&&(R.textContent=re),O=p(e),_=U(e,"P",{"data-svelte-h":!0}),m(_)!=="svelte-g6wzn3"&&(_.innerHTML=Te),K=p(e),r(S.$$.fragment,e),ee=p(e),F=U(e,"P",{"data-svelte-h":!0}),m(F)!=="svelte-1v632ht"&&(F.innerHTML=ce),te=p(e),r(H.$$.fragment,e),le=p(e),I=U(e,"P",{"data-svelte-h":!0}),m(I)!=="svelte-uf2gl6"&&(I.textContent=ue),se=p(e),r(z.$$.fragment,e),ie=p(e),L=U(e,"P",{}),$e(L).forEach(l),this.h()},h(){he(i,"name","hf:doc:metadata"),he(i,"content",Se)},m(e,t){Ce(document.head,i),s(e,J,t),s(e,a,t),s(e,y,t),T(d,e,t),s(e,j,t),T($,e,t),s(e,P,t),s(e,w,t),s(e,W,t),s(e,g,t),s(e,k,t),s(e,b,t),s(e,x,t),T(B,e,t),s(e,Y,t),T(h,e,t),s(e,Q,t),s(e,Z,t),s(e,N,t),T(G,e,t),s(e,D,t),T(C,e,t),s(e,q,t),s(e,V,t),s(e,X,t),T(v,e,t),s(e,A,t),s(e,R,t),s(e,O,t),s(e,_,t),s(e,K,t),T(S,e,t),s(e,ee,t),s(e,F,t),s(e,te,t),T(H,e,t),s(e,le,t),s(e,I,t),s(e,se,t),T(z,e,t),s(e,ie,t),s(e,L,t),ne=!0},p(e,[t]){const Me={};t&2&&(Me.$$scope={dirty:t,ctx:e}),$.$set(Me);const ye={};t&2&&(ye.$$scope={dirty:t,ctx:e}),h.$set(ye)},i(e){ne||(c(d.$$.fragment,e),c($.$$.fragment,e),c(B.$$.fragment,e),c(h.$$.fragment,e),c(G.$$.fragment,e),c(C.$$.fragment,e),c(v.$$.fragment,e),c(S.$$.fragment,e),c(H.$$.fragment,e),c(z.$$.fragment,e),ne=!0)},o(e){u(d.$$.fragment,e),u($.$$.fragment,e),u(B.$$.fragment,e),u(h.$$.fragment,e),u(G.$$.fragment,e),u(C.$$.fragment,e),u(v.$$.fragment,e),u(S.$$.fragment,e),u(H.$$.fragment,e),u(z.$$.fragment,e),ne=!1},d(e){e&&(l(J),l(a),l(y),l(j),l(P),l(w),l(W),l(g),l(k),l(b),l(x),l(Y),l(Q),l(Z),l(N),l(D),l(q),l(V),l(X),l(A),l(R),l(O),l(_),l(K),l(ee),l(F),l(te),l(le),l(I),l(se),l(ie),l(L)),l(i),M(d,e),M($,e),M(B,e),M(h,e),M(G,e),M(C,e),M(v,e),M(S,e),M(H,e),M(z,e)}}}const Se='{"title":"Metal Performance Shaders (MPS)","local":"metal-performance-shaders-mps","sections":[{"title":"故障排除","local":"故障排除","sections":[{"title":"注意力切片","local":"注意力切片","sections":[],"depth":3},{"title":"批量推理","local":"批量推理","sections":[],"depth":3}],"depth":2}],"depth":1}';function Fe(E){return be(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class je extends Be{constructor(i){super(),Ze(this,i,Fe,_e,ge,{})}}export{je as component};
