import{s as ge,n as $e,o as Be}from"../chunks/scheduler.e4ff9b64.js";import{S as Ze,i as Ce,e as p,s as i,c as J,h as Ge,a,d as l,b as n,f as be,g as m,j as o,k as ae,l as Ve,m as s,n as f,t as r,o as T,p as M}from"../chunks/index.09f1bca0.js";import{C as ve,H as oe,E as Re}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.dde31fc2.js";import{C as Ue}from"../chunks/CodeBlock.54e44b16.js";function Se(Je){let U,I,H,_,u,j,h,P,c,me='<p>带有 <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> 来为缺少此徽章的管道添加它。</p>',k,d,fe='🤗 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 的性能。您需要具备：',x,w,re='<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>',W,b,Te="<code>mps</code> 后端使用 PyTorch 的 <code>.to()</code> 接口将 Stable Diffusion 管道移动到您的 M1 或 M2 设备上：",Q,g,Y,y,Me='<p>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> 以便我们调查。</p>',N,$,ce="如果您使用 <strong>PyTorch 1.13</strong>，您需要通过管道进行一次额外的”预热”传递。这是一个临时解决方法，用于解决首次推理传递产生的结果与后续传递略有不同的问题。您只需要执行此传递一次，并且在仅进行一次推理步骤后可以丢弃结果。",q,B,D,Z,O,C,ye="本节列出了使用 <code>mps</code> 后端时的一些常见问题及其解决方法。",X,G,A,V,ue="M1/M2 性能对内存压力非常敏感。当发生这种情况时，系统会自动交换内存，这会显著降低性能。",K,v,he="为了防止这种情况发生，我们建议使用<em>注意力切片</em>来减少推理过程中的内存压力并防止交换。这在您的计算机系统内存少于 64GB 或生成非标准分辨率（大于 512×512 像素）的图像时尤其相关。在您的管道上调用 <code>enable_attention_slicing()</code> 函数：",ee,R,te,S,de="注意力切片将昂贵的注意力操作分多个步骤执行，而不是一次性完成。在没有统一内存的计算机中，它通常能提高约 20% 的性能，但我们观察到在大多数 Apple 芯片计算机中，除非您有 64GB 或更多 RAM，否则性能会<em>更好</em>。",le,L,se,F,we="批量生成多个提示可能会导致崩溃或无法可靠工作。如果是这种情况，请尝试迭代而不是批量处理。",ie,E,ne,z,pe;return u=new ve({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),h=new oe({props:{title:"Metal Performance Shaders (MPS)",local:"metal-performance-shaders-mps",headingTag:"h1"}}),g=new Ue({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}}),B=new Ue({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}}),Z=new oe({props:{title:"故障排除",local:"故障排除",headingTag:"h2"}}),G=new oe({props:{title:"注意力切片",local:"注意力切片",headingTag:"h3"}}),R=new Ue({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 oe({props:{title:"批量推理",local:"批量推理",headingTag:"h3"}}),E=new Re({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/optimization/mps.md"}}),{c(){U=p("meta"),I=i(),H=p("p"),_=i(),J(u.$$.fragment),j=i(),J(h.$$.fragment),P=i(),c=p("blockquote"),c.innerHTML=me,k=i(),d=p("p"),d.innerHTML=fe,x=i(),w=p("ul"),w.innerHTML=re,W=i(),b=p("p"),b.innerHTML=Te,Q=i(),J(g.$$.fragment),Y=i(),y=p("blockquote"),y.innerHTML=Me,N=i(),$=p("p"),$.innerHTML=ce,q=i(),J(B.$$.fragment),D=i(),J(Z.$$.fragment),O=i(),C=p("p"),C.innerHTML=ye,X=i(),J(G.$$.fragment),A=i(),V=p("p"),V.textContent=ue,K=i(),v=p("p"),v.innerHTML=he,ee=i(),J(R.$$.fragment),te=i(),S=p("p"),S.innerHTML=de,le=i(),J(L.$$.fragment),se=i(),F=p("p"),F.textContent=we,ie=i(),J(E.$$.fragment),ne=i(),z=p("p"),this.h()},l(e){const t=Ge("svelte-u9bgzb",document.head);U=a(t,"META",{name:!0,content:!0}),t.forEach(l),I=n(e),H=a(e,"P",{}),be(H).forEach(l),_=n(e),m(u.$$.fragment,e),j=n(e),m(h.$$.fragment,e),P=n(e),c=a(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),o(c)!=="svelte-ctti2"&&(c.innerHTML=me),k=n(e),d=a(e,"P",{"data-svelte-h":!0}),o(d)!=="svelte-y25hik"&&(d.innerHTML=fe),x=n(e),w=a(e,"UL",{"data-svelte-h":!0}),o(w)!=="svelte-cef1u9"&&(w.innerHTML=re),W=n(e),b=a(e,"P",{"data-svelte-h":!0}),o(b)!=="svelte-171v7e8"&&(b.innerHTML=Te),Q=n(e),m(g.$$.fragment,e),Y=n(e),y=a(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),o(y)!=="svelte-n0f302"&&(y.innerHTML=Me),N=n(e),$=a(e,"P",{"data-svelte-h":!0}),o($)!=="svelte-7zprdl"&&($.innerHTML=ce),q=n(e),m(B.$$.fragment,e),D=n(e),m(Z.$$.fragment,e),O=n(e),C=a(e,"P",{"data-svelte-h":!0}),o(C)!=="svelte-ql61eg"&&(C.innerHTML=ye),X=n(e),m(G.$$.fragment,e),A=n(e),V=a(e,"P",{"data-svelte-h":!0}),o(V)!=="svelte-1akzsmv"&&(V.textContent=ue),K=n(e),v=a(e,"P",{"data-svelte-h":!0}),o(v)!=="svelte-g6wzn3"&&(v.innerHTML=he),ee=n(e),m(R.$$.fragment,e),te=n(e),S=a(e,"P",{"data-svelte-h":!0}),o(S)!=="svelte-1v632ht"&&(S.innerHTML=de),le=n(e),m(L.$$.fragment,e),se=n(e),F=a(e,"P",{"data-svelte-h":!0}),o(F)!=="svelte-uf2gl6"&&(F.textContent=we),ie=n(e),m(E.$$.fragment,e),ne=n(e),z=a(e,"P",{}),be(z).forEach(l),this.h()},h(){ae(U,"name","hf:doc:metadata"),ae(U,"content",Le),ae(c,"class","tip"),ae(y,"class","warning")},m(e,t){Ve(document.head,U),s(e,I,t),s(e,H,t),s(e,_,t),f(u,e,t),s(e,j,t),f(h,e,t),s(e,P,t),s(e,c,t),s(e,k,t),s(e,d,t),s(e,x,t),s(e,w,t),s(e,W,t),s(e,b,t),s(e,Q,t),f(g,e,t),s(e,Y,t),s(e,y,t),s(e,N,t),s(e,$,t),s(e,q,t),f(B,e,t),s(e,D,t),f(Z,e,t),s(e,O,t),s(e,C,t),s(e,X,t),f(G,e,t),s(e,A,t),s(e,V,t),s(e,K,t),s(e,v,t),s(e,ee,t),f(R,e,t),s(e,te,t),s(e,S,t),s(e,le,t),f(L,e,t),s(e,se,t),s(e,F,t),s(e,ie,t),f(E,e,t),s(e,ne,t),s(e,z,t),pe=!0},p:$e,i(e){pe||(r(u.$$.fragment,e),r(h.$$.fragment,e),r(g.$$.fragment,e),r(B.$$.fragment,e),r(Z.$$.fragment,e),r(G.$$.fragment,e),r(R.$$.fragment,e),r(L.$$.fragment,e),r(E.$$.fragment,e),pe=!0)},o(e){T(u.$$.fragment,e),T(h.$$.fragment,e),T(g.$$.fragment,e),T(B.$$.fragment,e),T(Z.$$.fragment,e),T(G.$$.fragment,e),T(R.$$.fragment,e),T(L.$$.fragment,e),T(E.$$.fragment,e),pe=!1},d(e){e&&(l(I),l(H),l(_),l(j),l(P),l(c),l(k),l(d),l(x),l(w),l(W),l(b),l(Q),l(Y),l(y),l(N),l($),l(q),l(D),l(O),l(C),l(X),l(A),l(V),l(K),l(v),l(ee),l(te),l(S),l(le),l(se),l(F),l(ie),l(ne),l(z)),l(U),M(u,e),M(h,e),M(g,e),M(B,e),M(Z,e),M(G,e),M(R,e),M(L,e),M(E,e)}}}const Le='{"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(Je){return Be(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class _e extends Ze{constructor(U){super(),Ce(this,U,Fe,Se,ge,{})}}export{_e as component};
