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