import{s as ht,o as wt,n as W}from"../chunks/scheduler.5c93273d.js";import{S as Zt,i as _t,g as m,s as a,r as M,A as gt,h as i,f as e,c as p,j as bt,u as o,x as r,k as Ut,y as Ct,a as n,v as J,d as y,t as u,w as j}from"../chunks/index.e43dd92b.js";import{T as G}from"../chunks/Tip.1cbfe904.js";import{C as w}from"../chunks/CodeBlock.6896320e.js";import{H as x,E as xt}from"../chunks/getInferenceSnippets.d410b42d.js";import{H as It,a as Tt}from"../chunks/HfOption.d50154c3.js";function Wt(h){let t,b="请记住，这些技术可能需要根据模型进行调整。例如，基于 transformer 的扩散模型可能不会像基于 UNet 的模型那样从这些内存优化中同等受益。";return{c(){t=m("p"),t.textContent=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-1hr41i1"&&(t.textContent=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Gt(h){let t,b="设备放置是一个实验性功能，API 可能会更改。目前仅支持 <code>balanced</code> 策略。我们计划在未来支持额外的映射策略。";return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-3nvexn"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Vt(h){let t,b="<code>balanced</code> 设备放置策略将管道均匀分割到所有可用设备上。",c,f,U,T,Z="您可以使用 <code>hf_device_map</code> 检查管道的设备映射。",I,_,C;return f=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTJDJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZSUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLXhsLWJhc2UtMS4wJTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBJTIwJTIwJTIwJTIwZGV2aWNlX21hcCUzRCUyMmJhbGFuY2VkJTIyJTBBKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, StableDiffusionXLPipeline

pipeline = StableDiffusionXLPipeline.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
    torch_dtype=torch.float16,
    device_map=<span class="hljs-string">&quot;balanced&quot;</span>
)`,wrap:!1}}),_=new w({props:{code:"cHJpbnQocGlwZWxpbmUuaGZfZGV2aWNlX21hcCklMEElN0IndW5ldCclM0ElMjAxJTJDJTIwJ3ZhZSclM0ElMjAxJTJDJTIwJ3NhZmV0eV9jaGVja2VyJyUzQSUyMDAlMkMlMjAndGV4dF9lbmNvZGVyJyUzQSUyMDAlN0Q=",highlighted:`<span class="hljs-built_in">print</span>(pipeline.hf_device_map)
{<span class="hljs-string">&#x27;unet&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;vae&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;safety_checker&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;text_encoder&#x27;</span>: <span class="hljs-number">0</span>}`,wrap:!1}}),{c(){t=m("p"),t.innerHTML=b,c=a(),M(f.$$.fragment),U=a(),T=m("p"),T.innerHTML=Z,I=a(),M(_.$$.fragment)},l(d){t=i(d,"P",{"data-svelte-h":!0}),r(t)!=="svelte-on9lpr"&&(t.innerHTML=b),c=p(d),o(f.$$.fragment,d),U=p(d),T=i(d,"P",{"data-svelte-h":!0}),r(T)!=="svelte-7k1fi2"&&(T.innerHTML=Z),I=p(d),o(_.$$.fragment,d)},m(d,g){n(d,t,g),n(d,c,g),J(f,d,g),n(d,U,g),n(d,T,g),n(d,I,g),J(_,d,g),C=!0},p:W,i(d){C||(y(f.$$.fragment,d),y(_.$$.fragment,d),C=!0)},o(d){u(f.$$.fragment,d),u(_.$$.fragment,d),C=!1},d(d){d&&(e(t),e(c),e(U),e(T),e(I)),j(f,d),j(_,d)}}}function $t(h){let t,b='<code>device_map</code> 对于加载大型模型非常有用，例如具有 125 亿参数的 Flux diffusion transformer。将其设置为 <code>&quot;auto&quot;</code> 可以自动将模型首先分布到最快的设备上，然后再移动到较慢的设备。有关更多详细信息，请参阅 <a href="../training/distributed_inference#model-sharding">模型分片</a> 文档。',c,f,U,T,Z="您可以使用 <code>hf_device_map</code> 检查模型的设备映射。",I,_,C;return f=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBBdXRvTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMmJsYWNrLWZvcmVzdC1sYWJzJTJGRkxVWC4xLWRldiUyMiUyQyUyMCUwQSUyMCUyMCUyMCUyMHN1YmZvbGRlciUzRCUyMnRyYW5zZm9ybWVyJTIyJTJDJTBBJTIwJTIwJTIwJTIwZGV2aWNlX21hcCUzRCUyMmF1dG8lMjIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel

transformer = AutoModel.from_pretrained(
    <span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>, 
    subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
    device_map=<span class="hljs-string">&quot;auto&quot;</span>,
    torch_dtype=torch.bfloat16
)`,wrap:!1}}),_=new w({props:{code:"cHJpbnQodHJhbnNmb3JtZXIuaGZfZGV2aWNlX21hcCk=",highlighted:'<span class="hljs-built_in">print</span>(transformer.hf_device_map)',wrap:!1}}),{c(){t=m("p"),t.innerHTML=b,c=a(),M(f.$$.fragment),U=a(),T=m("p"),T.innerHTML=Z,I=a(),M(_.$$.fragment)},l(d){t=i(d,"P",{"data-svelte-h":!0}),r(t)!=="svelte-gz97o7"&&(t.innerHTML=b),c=p(d),o(f.$$.fragment,d),U=p(d),T=i(d,"P",{"data-svelte-h":!0}),r(T)!=="svelte-fe42wi"&&(T.innerHTML=Z),I=p(d),o(_.$$.fragment,d)},m(d,g){n(d,t,g),n(d,c,g),J(f,d,g),n(d,U,g),n(d,T,g),n(d,I,g),J(_,d,g),C=!0},p:W,i(d){C||(y(f.$$.fragment,d),y(_.$$.fragment,d),C=!0)},o(d){u(f.$$.fragment,d),u(_.$$.fragment,d),C=!1},d(d){d&&(e(t),e(c),e(U),e(T),e(I)),j(f,d),j(_,d)}}}function Bt(h){let t,b,c,f;return t=new Tt({props:{id:"device-map",option:"pipeline level",$$slots:{default:[Vt]},$$scope:{ctx:h}}}),c=new Tt({props:{id:"device-map",option:"model level",$$slots:{default:[$t]},$$scope:{ctx:h}}}),{c(){M(t.$$.fragment),b=a(),M(c.$$.fragment)},l(U){o(t.$$.fragment,U),b=p(U),o(c.$$.fragment,U)},m(U,T){J(t,U,T),n(U,b,T),J(c,U,T),f=!0},p(U,T){const Z={};T&2&&(Z.$$scope={dirty:T,ctx:U}),t.$set(Z);const I={};T&2&&(I.$$scope={dirty:T,ctx:U}),c.$set(I)},i(U){f||(y(t.$$.fragment,U),y(c.$$.fragment,U),f=!0)},o(U){u(t.$$.fragment,U),u(c.$$.fragment,U),f=!1},d(U){U&&e(b),j(t,U),j(c,U)}}}function kt(h){let t,b="<code>AutoencoderKLWan</code> 和 <code>AsymmetricAutoencoderKL</code> 类不支持切片。";return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-1y4zs6u"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function vt(h){let t,b="<code>AutoencoderKLWan</code> 和 <code>AsymmetricAutoencoderKL</code> 不支持平铺。";return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-1ncvf0i"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Nt(h){let t,b='在调用 <code>enable_sequential_cpu_offload()</code> 之前，不要将管道移动到 CUDA，否则节省的内存非常有限（更多细节请参考此 <a href="https://github.com/huggingface/diffusers/issues/1934" rel="nofollow">issue</a>）。这是一个状态操作，会在模型上安装钩子。';return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-p9ehy6"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Xt(h){let t,b='请注意，如果在安装钩子后模型在管道外部被重用（更多细节请参考 <a href="https://huggingface.co/docs/accelerate/en/package_reference/big_modeling#accelerate.hooks.remove_hook_from_module" rel="nofollow">移除钩子</a>），您需要按预期顺序运行整个管道和模型以正确卸载它们。这是一个状态操作，会在模型上安装钩子。';return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-yzdhjx"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Yt(h){let t,b="如果前向实现包含权重相关的输入设备转换，组卸载可能不适用于所有模型，因为它可能与组卸载的设备转换机制冲突。";return{c(){t=m("p"),t.textContent=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-hdipx1"&&(t.textContent=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Qt(h){let t,b="当 <code>use_stream=True</code> 在启用平铺的 VAEs 上时，确保在推理前进行虚拟前向传递（可以使用虚拟输入），以避免设备不匹配错误。这可能不适用于所有实现，因此如果遇到任何问题，请随时提出问题。";return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-l1vxq2"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function Rt(h){let t,b='将分层类型转换与<a href="#group-offloading">组卸载</a>结合使用，以获得更多内存节省。';return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-16mnvhz"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function zt(h){let t,b='如果前向实现包含权重的内部类型转换，分层类型转换可能不适用于所有模型。当前的分层类型转换实现假设前向传递独立于权重精度，并且输入数据类型始终在 <code>compute_dtype</code> 中指定（请参见<a href="https://github.com/huggingface/transformers/blob/7f5077e53682ca855afc826162b204ebf809f1f9/src/transformers/models/t5/modeling_t5.py#L294-L299" rel="nofollow">这里</a>以获取不兼容的实现）。',c,f,U='分层类型转换也可能在使用<a href="https://huggingface.co/docs/peft/index" rel="nofollow">PEFT</a>层的自定义建模实现上失败。有一些检查可用，但它们没有经过广泛测试或保证在所有情况下都能工作。';return{c(){t=m("p"),t.innerHTML=b,c=a(),f=m("p"),f.innerHTML=U},l(T){t=i(T,"P",{"data-svelte-h":!0}),r(t)!=="svelte-bu9ykn"&&(t.innerHTML=b),c=p(T),f=i(T,"P",{"data-svelte-h":!0}),r(f)!=="svelte-1g25yzq"&&(f.innerHTML=U)},m(T,Z){n(T,t,Z),n(T,c,Z),n(T,f,Z)},p:W,d(T){T&&(e(t),e(c),e(f))}}}function Et(h){let t,b='内存高效注意力优化内存使用 <em>和</em> <a href="./fp16#scaled-dot-product-attention">推理速度</a>！';return{c(){t=m("p"),t.innerHTML=b},l(c){t=i(c,"P",{"data-svelte-h":!0}),r(t)!=="svelte-1cyayt7"&&(t.innerHTML=b)},m(c,f){n(c,t,f)},p:W,d(c){c&&e(t)}}}function St(h){let t,b,c,f,U,T,Z,I='现代diffusion models，如 <a href="../api/pipelines/flux">Flux</a> 和 <a href="../api/pipelines/wan">Wan</a>，拥有数十亿参数，在您的硬件上进行推理时会占用大量内存。这是一个挑战，因为常见的 GPU 通常没有足够的内存。为了克服内存限制，您可以使用多个 GPU（如果可用）、将一些管道组件卸载到 CPU 等。',_,C,d="本指南将展示如何减少内存使用。",g,V,hl,S,wl,H,Jn='如果您有多个 GPU 的访问权限，有几种选项可以高效地在硬件上加载和分发大型模型。这些功能由 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 库支持，因此请确保先安装它。',Zl,A,_l,F,gl,L,yn="将大型检查点加载到多个分片中很有用，因为分片会逐个加载。这保持了低内存使用，只需要足够的内存来容纳模型大小和最大分片大小。我们建议当 fp32 检查点大于 5GB 时进行分片。默认分片大小为 5GB。",Cl,q,un="在 <code>save_pretrained()</code> 中使用 <code>max_shard_size</code> 参数对检查点进行分片。",xl,D,Il,P,jn="现在您可以使用分片检查点，而不是常规检查点，以节省内存。",Wl,K,Gl,O,Vl,$,$l,ss,dn=`<code>device_map</code> 参数控制管道或模型中的组件如何
单个模型中的层分布在多个设备上。`,Bl,B,kl,ls,fn="当设计您自己的 <code>device_map</code> 时，它应该是一个字典，包含模型的特定模块名称或层以及设备标识符（整数表示 GPU，<code>cpu</code> 表示 CPU，<code>disk</code> 表示磁盘）。",vl,es,bn="在模型上调用 <code>hf_device_map</code> 以查看模型层如何分布，然后设计您自己的映射。",Nl,ns,Xl,ts,Un="例如，下面的 <code>device_map</code> 将 <code>single_transformer_blocks.10</code> 到 <code>single_transformer_blocks.20</code> 放置在第二个 GPU（<code>1</code>）上。",Yl,as,Ql,ps,Tn="传递一个字典，将最大内存使用量映射到每个设备以强制执行限制。如果设备不在 <code>max_memory</code> 中，它将被忽略，管道组件不会分发到该设备。",Rl,cs,zl,ms,hn="Diffusers 默认使用所有设备的最大内存，但如果它们无法适应 GPU，则需要使用单个 GPU 并通过以下方法卸载到 CPU。",El,is,wn="<li><code>enable_model_cpu_offload()</code> 仅适用于单个 GPU，但非常大的模型可能无法适应它</li> <li>使用 <code>enable_sequential_cpu_offload()</code> 可能有效，但它极其缓慢，并且仅限于单个 GPU。</li>",Sl,rs,Zn="使用 <code>reset_device_map()</code> 方法来重置 <code>device_map</code>。如果您想在已进行设备映射的管道上使用方法如 <code>.to()</code>、<code>enable_sequential_cpu_offload()</code> 和 <code>enable_model_cpu_offload()</code>，这是必要的。",Hl,Ms,Al,os,Fl,Js,_n="VAE 切片通过将大批次输入拆分为单个数据批次并分别处理它们来节省内存。这种方法在同时生成多个图像时效果最佳。",Ll,ys,gn="例如，如果您同时生成 4 个图像，解码会将峰值激活内存增加 4 倍。VAE 切片通过一次只解码 1 个图像而不是所有 4 个图像来减少这种情况。",ql,us,Cn="调用 <code>enable_vae_slicing()</code> 来启用切片 VAE。您可以预期在解码多图像批次时性能会有小幅提升，而在单图像批次时没有性能影响。",Dl,js,Pl,k,Kl,ds,Ol,fs,xn="VAE 平铺通过将图像划分为较小的重叠图块而不是一次性处理整个图像来节省内存。这也减少了峰值内存使用量，因为 GPU 一次只处理一个图块。",se,bs,In="调用 <code>enable_vae_tiling()</code> 来启用 VAE 平铺。生成的图像可能因图块到图块的色调变化而有所不同，因为它们被单独解码，但图块之间不应有明显的接缝。对于低于预设（但可配置）限制的分辨率，平铺被禁用。例如，对于 <code>StableDiffusionPipeline</code> 中的 VAE，此限制为 512x512。",le,Us,ee,v,ne,Ts,te,hs,Wn=`卸载策略将非当前活动层移动
将模型移动到 CPU 以避免增加 GPU 内存。这些策略可以与量化和 torch.compile 结合使用，以平衡推理速度和内存使用。`,ae,ws,Gn='有关更多详细信息，请参考 <a href="./speed-memory-optims">编译和卸载量化模型</a> 指南。',pe,Zs,ce,_s,Vn="CPU 卸载选择性地将权重从 GPU 移动到 CPU。当需要某个组件时，它被传输到 GPU；当不需要时，它被移动到 CPU。此方法作用于子模块而非整个模型。它通过避免将整个模型存储在 GPU 上来节省内存。",me,gs,$n="CPU 卸载显著减少内存使用，但由于子模块在设备之间多次来回传递，它也非常慢。由于速度极慢，它通常不实用。",ie,N,re,Cs,Bn="调用 <code>enable_sequential_cpu_offload()</code> 以在管道上启用它。",Me,xs,oe,Is,Je,Ws,kn='模型卸载将整个模型移动到 GPU，而不是选择性地移动某些层或模型组件。一个主要管道模型，通常是文本编码器、UNet 和 VAE，被放置在 GPU 上，而其他组件保持在 CPU 上。像 UNet 这样运行多次的组件会一直留在 GPU 上，直到完全完成且不再需要。这消除了 <a href="#cpu-offloading">CPU 卸载</a> 的通信开销，使模型卸载成为一个更快的替代方案。权衡是内存节省不会那么大。',ye,X,ue,Gs,vn="调用 <code>enable_model_cpu_offload()</code> 以在管道上启用它。",je,Vs,de,$s,Nn="<code>enable_model_cpu_offload()</code> 在您单独使用 <code>encode_prompt()</code> 方法生成文本编码器隐藏状态时也有帮助。",fe,Bs,be,ks,Xn='组卸载将内部层组（<a href="https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html" rel="nofollow">torch.nn.ModuleList</a> 或 <a href="https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html" rel="nofollow">torch.nn.Sequential</a>）移动到 CPU。它比<a href="#model-offloading">模型卸载</a>使用更少的内存，并且比<a href="#cpu-offloading">CPU 卸载</a>更快，因为它减少了通信开销。',Ue,Y,Te,vs,Yn='调用 <code>enable_group_offload()</code> 为继承自 <code>ModelMixin</code> 的标准 Diffusers 模型组件启用它。对于不继承自 <code>ModelMixin</code> 的其他模型组件，例如通用 <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="nofollow">torch.nn.Module</a>，使用 <code>apply_group_offloading()</code> 代替。',he,Ns,Qn="<code>offload_type</code> 参数可以设置为 <code>block_level</code> 或 <code>leaf_level</code>。",we,Xs,Rn='<li><code>block_level</code> 基于 <code>num_blocks_per_group</code> 参数卸载层组。例如，如果 <code>num_blocks_per_group=2</code> 在一个有 40 层的模型上，每次加载和卸载 2 层（总共 20 次加载/卸载）。这大大减少了内存需求。</li> <li><code>leaf_level</code> 在最低级别卸载单个层，等同于<a href="#cpu-offloading">CPU 卸载</a>。但如果您使用流而不放弃推理速度，它可以更快。</li>',Ze,Ys,_e,Qs,ge,Rs,zn='<code>use_stream</code> 参数可以激活支持异步数据传输流的 CUDA 设备，以减少整体执行时间，与 <a href="#cpu-offloading">CPU 卸载</a> 相比。它通过使用层预取重叠数据传输和计算。下一个要执行的层在当前层仍在执行时加载到 GPU 上。这会显著增加 CPU 内存，因此请确保您有模型大小的 2 倍内存。',Ce,zs,En='设置 <code>record_stream=True</code> 以获得更多速度提升，代价是内存使用量略有增加。请参阅 <a href="https://pytorch.org/docs/stable/generated/torch.Tensor.record_stream.html" rel="nofollow">torch.Tensor.record_stream</a> 文档了解更多信息。',xe,Q,Ie,Es,Sn="如果您在使用启用 <code>use_stream</code> 的 <code>block_level</code> 组卸载，<code>num_blocks_per_group</code> 参数应设置为 <code>1</code>，否则会引发警告。",We,Ss,Ge,Hs,Hn="<code>low_cpu_mem_usage</code> 参数可以设置为 <code>True</code>，以在使用流进行组卸载时减少 CPU 内存使用。它最适合 <code>leaf_level</code> 卸载和 CPU 内存瓶颈的情况。通过动态创建固定张量而不是预先固定它们来节省内存。然而，这可能会增加整体执行时间。",Ve,As,$e,Fs,An="组卸载可能会消耗大量系统内存，具体取决于模型大小。在内存有限的系统上，尝试将组卸载到磁盘作为辅助内存。",Be,Ls,Fn="在 <code>enable_group_offload()</code> 或 <code>apply_group_offloading()</code> 中设置 <code>offload_to_disk_path</code> 参数，将模型卸载到磁盘。",ke,qs,ve,Ds,Ln='参考这些<a href="https://github.com/huggingface/diffusers/pull/11682#issue-3129365363" rel="nofollow">两个</a><a href="https://github.com/huggingface/diffusers/pull/11682#issuecomment-2955715126" rel="nofollow">表格</a>来比较速度和内存的权衡。',Ne,Ps,Xe,R,Ye,Ks,qn="分层类型转换将权重存储在较小的数据格式中（例如 <code>torch.float8_e4m3fn</code> 和 <code>torch.float8_e5m2</code>），以使用更少的内存，并在计算时将那些权重上转换为更高精度如 <code>torch.float16</code> 或 <code>torch.bfloat16</code>。某些层（归一化和调制相关权重）被跳过，因为将它们存储在 fp8 中可能会降低生成质量。",Qe,z,Re,Os,Dn="调用 <code>enable_layerwise_casting()</code> 来设置存储和计算数据类型。",ze,sl,Ee,ll,Pn="<code>apply_layerwise_casting()</code> 方法也可以在您需要更多控制和灵活性时使用。它可以通过在特定内部模块上调用它来部分应用于模型层。使用 <code>skip_modules_pattern</code> 或 <code>skip_modules_classes</code> 参数来指定要避免的模块，例如归一化和调制层。",Se,el,He,nl,Ae,tl,Kn='<a href="https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html" rel="nofollow">torch.channels_last</a> 将张量的存储方式从 <code>(批次大小, 通道数, 高度, 宽度)</code> 翻转为 <code>(批次大小, 高度, 宽度, 通道数)</code>。这使张量与硬件如何顺序访问存储在内存中的张量对齐，并避免了在内存中跳转以访问像素值。',Fe,al,On="并非所有运算符当前都支持通道最后格式，并且可能导致性能更差，但仍然值得尝试。",Le,pl,qe,cl,De,ml,st='<a href="https://pytorch.org/docs/stable/generated/torch.jit.trace.html" rel="nofollow">torch.jit.trace</a> 记录模型在样本输入上执行的操作，并根据记录的执行路径创建一个新的、优化的模型表示。在跟踪过程中，模型被优化以减少来自Python和动态控制流的开销，并且操作被融合在一起以提高效率。返回的可执行文件或 <a href="https://pytorch.org/docs/stable/generated/torch.jit.ScriptFunction.html" rel="nofollow">ScriptFunction</a> 可以被编译。',Pe,il,Ke,rl,lt="替换管道的 UNet 为追踪版本。",Oe,Ml,sn,ol,ln,E,en,Jl,et="Transformers 注意力机制是内存密集型的，尤其对于长序列，因此您可以尝试使用不同且更内存高效的注意力类型。",nn,yl,nt='默认情况下，如果安装了 PyTorch &gt;= 2.0，则使用 <a href="https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html" rel="nofollow">scaled dot-product attention (SDPA)</a>。您无需对代码进行任何额外更改。',tn,ul,tt=`SDPA 还支持 <a href="https://github.com/Dao-AILab/flash-attention" rel="nofollow">FlashAttention</a> 和 <a href="https://github.com/facebookresearch/xformers" rel="nofollow">xFormers</a>，以及 a
这是一个原生的 C++ PyTorch 实现。它会根据您的输入自动选择最优的实现。`,an,jl,at="您可以使用 <code>enable_xformers_memory_efficient_attention()</code> 方法显式地使用 xFormers。",pn,dl,cn,fl,pt="调用 <code>disable_xformers_memory_efficient_attention()</code> 来禁用它。",mn,bl,rn,Ul,Mn,Tl,on;return U=new x({props:{title:"减少内存使用",local:"减少内存使用",headingTag:"h1"}}),V=new G({props:{warning:!1,$$slots:{default:[Wt]},$$scope:{ctx:h}}}),S=new x({props:{title:"多个 GPU",local:"多个-gpu",headingTag:"h2"}}),A=new w({props:{code:"cGlwJTIwaW5zdGFsbCUyMC1VJTIwYWNjZWxlcmF0ZQ==",highlighted:"pip install -U accelerate",wrap:!1}}),F=new x({props:{title:"分片检查点",local:"分片检查点",headingTag:"h3"}}),D=new w({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEF1dG9Nb2RlbCUwQSUwQXVuZXQlMjAlM0QlMjBBdXRvTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnN0YWJpbGl0eWFpJTJGc3RhYmxlLWRpZmZ1c2lvbi14bC1iYXNlLTEuMCUyMiUyQyUyMHN1YmZvbGRlciUzRCUyMnVuZXQlMjIlMEEpJTBBdW5ldC5zYXZlX3ByZXRyYWluZWQoJTIyc2R4bC11bmV0LXNoYXJkZWQlMjIlMkMlMjBtYXhfc2hhcmRfc2l6ZSUzRCUyMjVHQiUyMik=",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel

unet = AutoModel.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>, subfolder=<span class="hljs-string">&quot;unet&quot;</span>
)
unet.save_pretrained(<span class="hljs-string">&quot;sdxl-unet-sharded&quot;</span>, max_shard_size=<span class="hljs-string">&quot;5GB&quot;</span>)`,wrap:!1}}),K=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTJDJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZSUwQSUwQXVuZXQlMjAlM0QlMjBBdXRvTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnVzZXJuYW1lJTJGc2R4bC11bmV0LXNoYXJkZWQlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYlMEEpJTBBcGlwZWxpbmUlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25YTFBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJzdGFiaWxpdHlhaSUyRnN0YWJsZS1kaWZmdXNpb24teGwtYmFzZS0xLjAlMjIlMkMlMEElMjAlMjAlMjAlMjB1bmV0JTNEdW5ldCUyQyUwQSUyMCUyMCUyMCUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUwQSkudG8oJTIyY3VkYSUyMik=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, StableDiffusionXLPipeline

unet = AutoModel.from_pretrained(
    <span class="hljs-string">&quot;username/sdxl-unet-sharded&quot;</span>, torch_dtype=torch.float16
)
pipeline = StableDiffusionXLPipeline.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
    unet=unet,
    torch_dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)`,wrap:!1}}),O=new x({props:{title:"设备放置",local:"设备放置",headingTag:"h3"}}),$=new G({props:{warning:!0,$$slots:{default:[Gt]},$$scope:{ctx:h}}}),B=new It({props:{id:"device-map",options:["pipeline level","model level"],$$slots:{default:[Bt]},$$scope:{ctx:h}}}),ns=new w({props:{code:"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MkMlMjAnc2luZ2xlX3RyYW5zZm9ybWVyX2Jsb2Nrcy4zMCclM0ElMjAnY3B1JyUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjMxJyUzQSUyMCdjcHUnJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMzInJTNBJTIwJ2NwdSclMkMlMjAnc2luZ2xlX3RyYW5zZm9ybWVyX2Jsb2Nrcy4zMyclM0ElMjAnY3B1JyUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjM0JyUzQSUyMCdjcHUnJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMzUnJTNBJTIwJ2NwdSclMkMlMjAnc2luZ2xlX3RyYW5zZm9ybWVyX2Jsb2Nrcy4zNiclM0ElMjAnY3B1JyUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjM3JyUzQSUyMCdjcHUnJTJDJTIwJ25vcm1fb3V0JyUzQSUyMCdjcHUnJTJDJTIwJ3Byb2pfb3V0JyUzQSUyMCdjcHUnJTdE",highlighted:`<span class="hljs-built_in">print</span>(transformer.hf_device_map)
{<span class="hljs-string">&#x27;pos_embed&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;time_text_embed&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;context_embedder&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;x_embedder&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;transformer_blocks&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.0&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.1&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.2&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.3&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.4&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.5&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.6&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.7&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.8&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.9&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.10&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.11&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.12&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.13&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.14&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.15&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.16&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.17&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.18&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.19&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.20&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.21&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.22&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.23&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.24&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.25&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.26&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.27&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.28&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.29&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.30&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.31&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.32&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.33&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.34&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.35&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.36&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.37&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;norm_out&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;proj_out&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>}`,wrap:!1}}),as=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTBBJTBBZGV2aWNlX21hcCUyMCUzRCUyMCU3QiUwQSUyMCUyMCUyMCUyMCdwb3NfZW1iZWQnJTNBJTIwMCUyQyUyMCd0aW1lX3RleHRfZW1iZWQnJTNBJTIwMCUyQyUyMCdjb250ZXh0X2VtYmVkZGVyJyUzQSUyMDAlMkMlMjAneF9lbWJlZGRlciclM0ElMjAwJTJDJTIwJ3RyYW5zZm9ybWVyX2Jsb2NrcyclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMCclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMSclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMiclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMyclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuNCclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuNSclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuNiclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuNyclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuOCclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuOSclM0ElMjAwJTJDJTIwJ3NpbmdsZV90cm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F9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel

device_map = {
    <span class="hljs-string">&#x27;pos_embed&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;time_text_embed&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;context_embedder&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;x_embedder&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;transformer_blocks&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.0&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.1&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.2&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.3&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.4&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.5&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.6&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.7&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.8&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.9&#x27;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;single_transformer_blocks.10&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.11&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.12&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.13&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.14&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.15&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.16&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.17&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.18&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.19&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.20&#x27;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;single_transformer_blocks.21&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.22&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.23&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.24&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.25&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.26&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.27&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.28&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.29&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.30&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.31&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.32&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.33&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.34&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.35&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.36&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;single_transformer_blocks.37&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;norm_out&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>, <span class="hljs-string">&#x27;proj_out&#x27;</span>: <span class="hljs-string">&#x27;cpu&#x27;</span>
}

transformer = AutoModel.from_pretrained(
    <span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>, 
    subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
    device_map=device_map,
    torch_dtype=torch.bfloat16
)`,wrap:!1}}),cs=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTJDJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZSUwQSUwQW1heF9tZW1vcnklMjAlM0QlMjAlN0IwJTNBJTIyMUdCJTIyJTJDJTIwMSUzQSUyMjFHQiUyMiU3RCUwQXBpcGVsaW5lJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLXhsLWJhc2UtMS4wJTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBJTIwJTIwJTIwJTIwZGV2aWNlX21hcCUzRCUyMmJhbGFuY2VkJTIyJTJDJTBBJTIwJTIwJTIwJTIwbWF4X21lbW9yeSUzRG1heF9tZW1vcnklMEEp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, StableDiffusionXLPipeline

max_memory = {<span class="hljs-number">0</span>:<span class="hljs-string">&quot;1GB&quot;</span>, <span class="hljs-number">1</span>:<span class="hljs-string">&quot;1GB&quot;</span>}
pipeline = StableDiffusionXLPipeline.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
    torch_dtype=torch.float16,
    device_map=<span class="hljs-string">&quot;balanced&quot;</span>,
    max_memory=max_memory
)`,wrap:!1}}),Ms=new w({props:{code:"cGlwZWxpbmUucmVzZXRfZGV2aWNlX21hcCgp",highlighted:"pipeline.reset_device_map()",wrap:!1}}),os=new x({props:{title:"VAE 切片",local:"vae-切片",headingTag:"h2"}}),js=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTJDJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZSUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLXhsLWJhc2UtMS4wJTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKS50byglMjJjdWRhJTIyKSUwQXBpcGVsaW5lLmVuYWJsZV92YWVfc2xpY2luZygpJTBBcGlwZWxpbmUoJTVCJTIyQW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyME1hcnMlMjIlNUQqMzIpLmltYWdlcyU1QjAlNUQlMEFwcmludChmJTIyTWF4JTIwbWVtb3J5JTIwcmVzZXJ2ZWQlM0ElMjAlN0J0b3JjaC5jdWRhLm1heF9tZW1vcnlfYWxsb2NhdGVkKCklMjAlMkYlMjAxMDI0KiozJTNBLjJmJTdEJTIwR0IlMjIp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, StableDiffusionXLPipeline

pipeline = StableDiffusionXLPipeline.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
    torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.enable_vae_slicing()
pipeline([<span class="hljs-string">&quot;An astronaut riding a horse on Mars&quot;</span>]*<span class="hljs-number">32</span>).images[<span class="hljs-number">0</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB&quot;</span>)`,wrap:!1}}),k=new G({props:{warning:!0,$$slots:{default:[kt]},$$scope:{ctx:h}}}),ds=new x({props:{title:"VAE 平铺",local:"vae-平铺",headingTag:"h2"}}),Us=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b1BpcGVsaW5lRm9ySW1hZ2UySW1hZ2UlMEFmcm9tJTIwZGlmZnVzZXJzLnV0aWxzJTIwaW1wb3J0JTIwbG9hZF9pbWFnZSUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwQXV0b1BpcGVsaW5lRm9ySW1hZ2UySW1hZ2UuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnN0YWJpbGl0eWFpJTJGc3RhYmxlLWRpZmZ1c2lvbi14bC1iYXNlLTEuMCUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUwQSkudG8oJTIyY3VkYSUyMiklMEFwaXBlbGluZS5lbmFibGVfdmFlX3RpbGluZygpJTBBJTBBaW5pdF9pbWFnZSUyMCUzRCUyMGxvYWRfaW1hZ2UoJTIyaHR0cHMlM0ElMkYlMkZodWdnaW5nZmFjZS5jbyUyRmRhdGFzZXRzJTJGaHVnZ2luZ2ZhY2UlMkZkb2N1bWVudGF0aW9uLWltYWdlcyUyRnJlc29sdmUlMkZtYWluJTJGZGlmZnVzZXJzJTJGaW1nMmltZy1zZHhsLWluaXQucG5nJTIyKSUwQXByb21wdCUyMCUzRCUyMCUyMkFzdHJvbmF1dCUyMGluJTIwYSUyMGp1bmdsZSUyQyUyMGNvbGQlMjBjb2xvciUyMHBhbGV0dGUlMkMlMjBtdXRlZCUyMGNvbG9ycyUyQyUyMGRldGFpbGVkJTJDJTIwOGslMjIlMEFwaXBlbGluZShwcm9tcHQlMkMlMjBpbWFnZSUzRGluaXRfaW1hZ2UlMkMlMjBzdHJlbmd0aCUzRDAuNSkuaW1hZ2VzJTVCMCU1RCUwQXByaW50KGYlMjJNYXglMjBtZW1vcnklMjByZXNlcnZlZCUzQSUyMCU3QnRvcmNoLmN1ZGEubWF4X21lbW9yeV9hbGxvY2F0ZWQoKSUyMCUyRiUyMDEwMjQqKjMlM0EuMmYlN0QlMjBHQiUyMik=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForImage2Image
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> load_image

pipeline = AutoPipelineForImage2Image.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>, torch_dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.enable_vae_tiling()

init_image = load_image(<span class="hljs-string">&quot;https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-sdxl-init.png&quot;</span>)
prompt = <span class="hljs-string">&quot;Astronaut in a jungle, cold color palette, muted colors, detailed, 8k&quot;</span>
pipeline(prompt, image=init_image, strength=<span class="hljs-number">0.5</span>).images[<span class="hljs-number">0</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB&quot;</span>)`,wrap:!1}}),v=new G({props:{warning:!0,$$slots:{default:[vt]},$$scope:{ctx:h}}}),Ts=new x({props:{title:"卸载",local:"卸载",headingTag:"h2"}}),Zs=new x({props:{title:"CPU 卸载",local:"cpu-卸载",headingTag:"h3"}}),N=new G({props:{warning:!0,$$slots:{default:[Nt]},$$scope:{ctx:h}}}),xs=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlbGluZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJibGFjay1mb3Jlc3QtbGFicyUyRkZMVVguMS1zY2huZWxsJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5iZmxvYXQxNiUwQSklMEFwaXBlbGluZS5lbmFibGVfc2VxdWVudGlhbF9jcHVfb2ZmbG9hZCgpJTBBJTBBcGlwZWxpbmUoJTBBJTIwJTIwJTIwJTIwcHJvbXB0JTNEJTIyQW4lMjBhc3Ryb25hdXQlMjByaWRpbmclMjBhJTIwaG9yc2UlMjBvbiUyME1hcnMlMjIlMkMlMEElMjAlMjAlMjAlMjBndWlkYW5jZV9zY2FsZSUzRDAuJTJDJTBBJTIwJTIwJTIwJTIwaGVpZ2h0JTNENzY4JTJDJTBBJTIwJTIwJTIwJTIwd2lkdGglM0QxMzYwJTJDJTBBJTIwJTIwJTIwJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRDQlMkMlMEElMjAlMjAlMjAlMjBtYXhfc2VxdWVuY2VfbGVuZ3RoJTNEMjU2JTJDJTBBKS5pbWFnZXMlNUIwJTVEJTBBcHJpbnQoZiUyMk1heCUyMG1lbW9yeSUyMHJlc2VydmVkJTNBJTIwJTdCdG9yY2guY3VkYS5tYXhfbWVtb3J5X2FsbG9jYXRlZCgpJTIwJTJGJTIwMTAyNCoqMyUzQS4yZiU3RCUyMEdCJTIyKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;black-forest-labs/FLUX.1-schnell&quot;</span>, torch_dtype=torch.bfloat16
)
pipeline.enable_sequential_cpu_offload()

pipeline(
    prompt=<span class="hljs-string">&quot;An astronaut riding a horse on Mars&quot;</span>,
    guidance_scale=<span class="hljs-number">0.</span>,
    height=<span class="hljs-number">768</span>,
    width=<span class="hljs-number">1360</span>,
    num_inference_steps=<span class="hljs-number">4</span>,
    max_sequence_length=<span class="hljs-number">256</span>,
).images[<span class="hljs-number">0</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB&quot;</span>)`,wrap:!1}}),Is=new x({props:{title:"模型卸载",local:"模型卸载",headingTag:"h3"}}),X=new G({props:{warning:!0,$$slots:{default:[Xt]},$$scope:{ctx:h}}}),Vs=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlbGluZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJibGFjay1mb3Jlc3QtbGFicyUyRkZMVVguMS1zY2huZWxsJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5iZmxvYXQxNiUwQSklMEFwaXBlbGluZS5lbmFibGVfbW9kZWxfY3B1X29mZmxvYWQoKSUwQSUwQXBpcGVsaW5lKCUwQSUyMCUyMCUyMCUyMHByb21wdCUzRCUyMkFuJTIwYXN0cm9uYXV0JTIwcmlkaW5nJTIwYSUyMGhvcnNlJTIwb24lMjBNYXJzJTIyJTJDJTBBJTIwJTIwJTIwJTIwZ3VpZGFuY2Vfc2NhbGUlM0QwLiUyQyUwQSUyMCUyMCUyMCUyMGhlaWdodCUzRDc2OCUyQyUwQSUyMCUyMCUyMCUyMHdpZHRoJTNEMTM2MCUyQyUwQSUyMCUyMCUyMCUyMG51bV9pbmZlcmVuY2Vfc3RlcHMlM0Q0JTJDJTBBJTIwJTIwJTIwJTIwbWF4X3NlcXVlbmNlX2xlbmd0aCUzRDI1NiUyQyUwQSkuaW1hZ2VzJTVCMCU1RCUwQXByaW50KGYlMjIlRTYlOUMlODAlRTUlQTQlQTclRTUlODYlODUlRTUlQUQlOTglRTQlQkYlOUQlRTclOTUlOTklM0ElMjAlN0J0b3JjaC5jdWRhLm1heF9tZW1vcnlfYWxsb2NhdGVkKCklMjAlMkYlMjAxMDI0KiozJTNBLjJmJTdEJTIwR0IlMjIp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;black-forest-labs/FLUX.1-schnell&quot;</span>, torch_dtype=torch.bfloat16
)
pipeline.enable_model_cpu_offload()

pipeline(
    prompt=<span class="hljs-string">&quot;An astronaut riding a horse on Mars&quot;</span>,
    guidance_scale=<span class="hljs-number">0.</span>,
    height=<span class="hljs-number">768</span>,
    width=<span class="hljs-number">1360</span>,
    num_inference_steps=<span class="hljs-number">4</span>,
    max_sequence_length=<span class="hljs-number">256</span>,
).images[<span class="hljs-number">0</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;最大内存保留: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB&quot;</span>)`,wrap:!1}}),Bs=new x({props:{title:"组卸载",local:"组卸载",headingTag:"h3"}}),Y=new G({props:{warning:!0,$$slots:{default:[Yt]},$$scope:{ctx:h}}}),Ys=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YUGlwZWxpbmUlMEFmcm9tJTIwZGlmZnVzZXJzLmhvb2tzJTIwaW1wb3J0JTIwYXBwbHlfZ3JvdXBfb2ZmbG9hZGluZyUwQWZyb20lMjBkaWZmdXNlcnMudXRpbHMlMjBpbXBvcnQlMjBleHBvcnRfdG9fdmlkZW8lMEElMEFvbmxvYWRfZGV2aWNlJTIwJTNEJTIwdG9yY2guZGV2aWNlKCUyMmN1ZGElMjIpJTBBb2ZmbG9hZF9kZXZpY2UlMjAlM0QlMjB0b3JjaC5kZXZpY2UoJTIyY3B1JTIyKSUwQXBpcGVsaW5lJTIwJTNEJTIwQ29nVmlkZW9YUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUyMlRIVURNJTJGQ29nVmlkZW9YLTViJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5iZmxvYXQxNiklMEElMEElMjMlMjAlRTUlQUYlQjklMjBEaWZmdXNlcnMlMjAlRTYlQTglQTElRTUlOUUlOEIlRTUlQUUlOUUlRTclOEUlQjAlRTQlQkQlQkYlRTclOTQlQTglMjBlbmFibGVfZ3JvdXBfb2Zmb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W1faW5mZXJlbmNlX3N0ZXBzJTNENTApLmZyYW1lcyU1QjAlNUQlMEFwcmludChmJTIyTWF4JTIwbWVtb3J5JTIwcmVzZXJ2ZWQlM0ElMjAlN0J0b3JjaC5jdWRhLm1heF9tZW1vcnlfYWxsb2NhdGVkKCklMjAlMkYlMjAxMDI0KiozJTNBLjJmJTdEJTIwR0IlMjIpJTBBZXhwb3J0X3RvX3ZpZGVvKHZpZGVvJTJDJTIwJTIyb3V0cHV0Lm1wNCUyMiUyQyUyMGZwcyUzRDgp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline
<span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_group_offloading
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video

onload_device = torch.device(<span class="hljs-string">&quot;cuda&quot;</span>)
offload_device = torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>, torch_dtype=torch.bfloat16)

<span class="hljs-comment"># 对 Diffusers 模型实现使用 enable_group_offload 方法</span>
pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type=<span class="hljs-string">&quot;leaf_level&quot;</span>)
pipeline.vae.enable_group_offload(onload_device=onload_device, offload_type=<span class="hljs-string">&quot;leaf_level&quot;</span>)

<span class="hljs-comment"># 对其他模型组件使用 apply_group_offloading 方法</span>
apply_group_offloading(pipeline.text_encoder, onload_device=onload_device, offload_type=<span class="hljs-string">&quot;block_level&quot;</span>, num_blocks_per_group=<span class="hljs-number">2</span>)

prompt = (
<span class="hljs-string">&quot;A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. &quot;</span>
    <span class="hljs-string">&quot;The panda&#x27;s fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other &quot;</span>
    <span class="hljs-string">&quot;pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, &quot;</span>
    <span class="hljs-string">&quot;casting a gentle glow on the scene. The panda&#x27;s face is expressive, showing concentration and joy as it plays. &quot;</span>
    <span class="hljs-string">&quot;The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical &quot;</span>
    <span class="hljs-string">&quot;atmosphere of this unique musical performance.&quot;</span>
)
video = pipeline(prompt=prompt, guidance_scale=<span class="hljs-number">6</span>, num_inference_steps=<span class="hljs-number">50</span>).frames[<span class="hljs-number">0</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB&quot;</span>)
export_to_video(video, <span class="hljs-string">&quot;output.mp4&quot;</span>, fps=<span class="hljs-number">8</span>)`,wrap:!1}}),Qs=new x({props:{title:"CUDA 流",local:"cuda-流",headingTag:"h4"}}),Q=new G({props:{warning:!1,$$slots:{default:[Qt]},$$scope:{ctx:h}}}),Ss=new w({props:{code:"cGlwZWxpbmUudHJhbnNmb3JtZXIuZW5hYmxlX2dyb3VwX29mZmxvYWQob25sb2FkX2RldmljZSUzRG9ubG9hZF9kZXZpY2UlMkMlMjBvZmZsb2FkX2RldmljZSUzRG9mZmxvYWRfZGV2aWNlJTJDJTIwb2ZmbG9hZF90eXBlJTNEJTIybGVhZl9sZXZlbCUyMiUyQyUyMHVzZV9zdHJlYW0lM0RUcnVlJTJDJTIwcmVjb3JkX3N0cmVhbSUzRFRydWUp",highlighted:'pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type=<span class="hljs-string">&quot;leaf_level&quot;</span>, use_stream=<span class="hljs-literal">True</span>, record_stream=<span class="hljs-literal">True</span>)',wrap:!1}}),As=new x({props:{title:"卸载到磁盘",local:"卸载到磁盘",headingTag:"h4"}}),qs=new w({props:{code:"cGlwZWxpbmUudHJhbnNmb3JtZXIuZW5hYmxlX2dyb3VwX29mZmxvYWQob25sb2FkX2RldmljZSUzRG9ubG9hZF9kZXZpY2UlMkMlMjBvZmZsb2FkX2RldmljZSUzRG9mZmxvYWRfZGV2aWNlJTJDJTIwb2ZmbG9hZF90eXBlJTNEJTIybGVhZl9sZXZlbCUyMiUyQyUyMG9mZmxvYWRfdG9fZGlza19wYXRoJTNEJTIycGF0aCUyRnRvJTJGZGlzayUyMiklMEElMEFhcHBseV9ncm91cF9vZmZsb2FkaW5nKHBpcGVsaW5lLnRleHRfZW5jb2RlciUyQyUyMG9ubG9hZF9kZXZpY2UlM0RvbmxvYWRfZGV2aWNlJTJDJTIwb2ZmbG9hZF90eXBlJTNEJTIyYmxvY2tfbGV2ZWwlMjIlMkMlMjBudW1fYmxvY2tzX3Blcl9ncm91cCUzRDIlMkMlMjBvZmZsb2FkX3RvX2Rpc2tfcGF0aCUzRCUyMnBhdGglMkZ0byUyRmRpc2slMjIp",highlighted:`pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type=<span class="hljs-string">&quot;leaf_level&quot;</span>, offload_to_disk_path=<span class="hljs-string">&quot;path/to/disk&quot;</span>)

apply_group_offloading(pipeline.text_encoder, onload_device=onload_device, offload_type=<span class="hljs-string">&quot;block_level&quot;</span>, num_blocks_per_group=<span class="hljs-number">2</span>, offload_to_disk_path=<span class="hljs-string">&quot;path/to/disk&quot;</span>)`,wrap:!1}}),Ps=new x({props:{title:"分层类型转换",local:"分层类型转换",headingTag:"h2"}}),R=new G({props:{warning:!1,$$slots:{default:[Rt]},$$scope:{ctx:h}}}),z=new G({props:{warning:!0,$$slots:{default:[zt]},$$scope:{ctx:h}}}),sl=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YUGlwZWxpbmUlMkMlMjBDb2dWaWRlb1hUcmFuc2Zvcm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V9zY2FsZSUzRDYlMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTApLmZyYW1lcyU1QjAlNUQlMEFwcmludChmJTIyTWF4JTIwbWVtb3J5JTIwcmVzZXJ2ZWQlM0ElMjAlN0J0b3JjaC5jdWRhLm1heF9tZW1vcnlfYWxsb2NhdGVkKCklMjAlMkYlMjAxMDI0KiozJTNBLjJmJTdEJTIwR0IlMjIpJTBBZXhwb3J0X3RvX3ZpZGVvKHZpZGVvJTJDJTIwJTIyb3V0cHV0Lm1wNCUyMiUyQyUyMGZwcyUzRDgp",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, CogVideoXTransformer3DModel
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video

transformer = CogVideoXTransformer3DModel.from_pretrained(
    <span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>,
    subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
    torch_dtype=torch.bfloat16
)
transformer.enable_layerwise_casting(storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16)

pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>,
    transformer=transformer,
    torch_dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
prompt = (
    <span class="hljs-string">&quot;A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. &quot;</span>
    <span class="hljs-string">&quot;The panda&#x27;s fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other &quot;</span>
    <span class="hljs-string">&quot;pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, &quot;</span>
    <span class="hljs-string">&quot;casting a gentle glow on the scene. The panda&#x27;s face is expressive, showing concentration and joy as it plays. &quot;</span>
    <span class="hljs-string">&quot;The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical &quot;</span>
    <span class="hljs-string">&quot;atmosphere of this unique musical performance.&quot;</span>
)
video = pipeline(prompt=prompt, guidance_scale=<span class="hljs-number">6</span>, num_inference_steps=<span class="hljs-number">50</span>).frames[<span class="hljs-number">0</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Max memory reserved: <span class="hljs-subst">{torch.cuda.max_memory_allocated() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.2</span>f}</span> GB&quot;</span>)
export_to_video(video, <span class="hljs-string">&quot;output.mp4&quot;</span>, fps=<span class="hljs-number">8</span>)`,wrap:!1}}),el=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YVHJhbnNmb3JtZXIzRE1vZGVsJTBBZnJvbSUyMGRpZmZ1c2Vycy5ob29rcyUyMGltcG9ydCUyMGFwcGx5X2xheWVyd2lzZV9jYXN0aW5nJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBDb2dWaWRlb1hUcmFuc2Zvcm1lcjNETW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMlRIVURNJTJGQ29nVmlkZW9YLTViJTIyJTJDJTBBJTIwJTIwJTIwJTIwc3ViZm9sZGVyJTNEJTIydHJhbnNmb3JtZXIlMjIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKSUwQSUwQSUyMyUyMCVFOCVCNyVCMyVFOCVCRiU4NyVFNSVCRCU5MiVFNCVCOCU4MCVFNSU4QyU5NiVFNSVCMSU4MiUwQWFwcGx5X2xheWVyd2lzZV9jYXN0aW5nKCUwQSUyMCUyMCUyMCUyMHRyYW5zZm9ybWVyJTJDJTBBJTIwJTIwJTIwJTIwc3RvcmFnZV9kdHlwZSUzRHRvcmNoLmZsb2F0OF9lNG0zZm4lMkMlMEElMjAlMjAlMjAlMjBjb21wdXRlX2R0eXBlJTNEdG9yY2guYmZsb2F0MTYlMkMlMEElMjAlMjAlMjAlMjBza2lwX21vZHVsZXNfY2xhc3NlcyUzRCU1QiUyMm5vcm0lMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBub25fYmxvY2tpbmclM0RUcnVlJTJDJTBBKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXTransformer3DModel
<span class="hljs-keyword">from</span> diffusers.hooks <span class="hljs-keyword">import</span> apply_layerwise_casting

transformer = CogVideoXTransformer3DModel.from_pretrained(
    <span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>,
    subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
    torch_dtype=torch.bfloat16
)

<span class="hljs-comment"># 跳过归一化层</span>
apply_layerwise_casting(
    transformer,
    storage_dtype=torch.float8_e4m3fn,
    compute_dtype=torch.bfloat16,
    skip_modules_classes=[<span class="hljs-string">&quot;norm&quot;</span>],
    non_blocking=<span class="hljs-literal">True</span>,
)`,wrap:!1}}),nl=new x({props:{title:"torch.channels_last",local:"torchchannelslast",headingTag:"h2"}}),pl=new w({props:{code:"cHJpbnQocGlwZWxpbmUudW5ldC5jb252X291dC5zdGF0ZV9kaWN0KCklNUIlMjJ3ZWlnaHQlMjIlNUQuc3RyaWRlKCkpJTIwJTIwJTIzJTIwKDI4ODAlMkMlMjA5JTJDJTIwMyUyQyUyMDEpJTBBcGlwZWxpbmUudW5ldC50byhtZW1vcnlfZm9ybWF0JTNEdG9yY2guY2hhbm5lbHNfbGFzdCklMjAlMjAlMjMlMjAlRTUlOEUlOUYlRTUlOUMlQjAlRTYlOTMlOEQlRTQlQkQlOUMlMEFwcmludCglMEElMjAlMjAlMjAlMjBwaXBlbGluZS51bmV0LmNvbnZfb3V0LnN0YXRlX2RpY3QoKSU1QiUyMndlaWdodCUyMiU1RC5zdHJpZGUoKSUwQSklMjAlMjAlMjMlMjAoMjg4MCUyQyUyMDElMkMlMjA5NjAlMkMlMjAzMjApJTIwJUU3JUFDJUFDJUU0JUJBJThDJUU0JUI4JUFBJUU3JUJCJUI0JUU1JUJBJUE2JUU3JTlBJTg0JUU4JUI3JUE4JUU1JUJBJUE2JUU0JUI4JUJBMSVFOCVBRiU4MSVFNiU5OCU4RSVFNSVBRSU4MyVFNiU5QyU4OSVFNiU5NSU4OA==",highlighted:`<span class="hljs-built_in">print</span>(pipeline.unet.conv_out.state_dict()[<span class="hljs-string">&quot;weight&quot;</span>].stride())  <span class="hljs-comment"># (2880, 9, 3, 1)</span>
pipeline.unet.to(memory_format=torch.channels_last)  <span class="hljs-comment"># 原地操作</span>
<span class="hljs-built_in">print</span>(
    pipeline.unet.conv_out.state_dict()[<span class="hljs-string">&quot;weight&quot;</span>].stride()
)  <span class="hljs-comment"># (2880, 1, 960, 320) 第二个维度的跨度为1证明它有效</span>`,wrap:!1}}),cl=new x({props:{title:"torch.jit.trace",local:"torchjittrace",headingTag:"h2"}}),il=new w({props:{code:" ",highlighted:`<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> functools

<span class="hljs-comment"># torch 禁用梯度</span>
torch.set_grad_enabled(<span class="hljs-literal">False</span>)

<span class="hljs-comment"># 设置变量</span>
n_experiments = <span class="hljs-number">2</span>
unet_runs_per_experiment = <span class="hljs-number">50</span>

<span class="hljs-comment"># 加载样本输入</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">generate_inputs</span>():
    sample = torch.randn((<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">64</span>, <span class="hljs-number">64</span>), device=<span class="hljs-string">&quot;cuda&quot;</span>, dtype=torch.float16)
    timestep = torch.rand(<span class="hljs-number">1</span>, device=<span class="hljs-string">&quot;cuda&quot;</span>, dtype=torch.float16) * <span class="hljs-number">999</span>
    encoder_hidden_states = torch.randn((<span class="hljs-number">2</span>, <span class="hljs-number">77</span>, <span class="hljs-number">768</span>), device=<span class="hljs-string">&quot;cuda&quot;</span>, dtype=torch.float16)
    <span class="hljs-keyword">return</span> sample, timestep, encoder_hidden_states


pipeline = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>,
    torch_dtype=torch.float16,
    use_safetensors=<span class="hljs-literal">True</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
unet = pipeline.unet
unet.<span class="hljs-built_in">eval</span>()
unet.to(memory
_<span class="hljs-built_in">format</span>=torch.channels_last)  <span class="hljs-comment"># 使用 channels_last 内存格式</span>
unet.forward = functools.partial(unet.forward, return_dict=<span class="hljs-literal">False</span>)  <span class="hljs-comment"># 设置 return_dict=False 为默认</span>

<span class="hljs-comment"># 预热</span>
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">3</span>):
    <span class="hljs-keyword">with</span> torch.inference_mode():
        inputs = generate_inputs()
        orig_output = unet(*inputs)

<span class="hljs-comment"># 追踪</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;tracing..&quot;</span>)
unet_traced = torch.jit.trace(unet, inputs)
unet_traced.<span class="hljs-built_in">eval</span>()
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;done tracing&quot;</span>)

<span class="hljs-comment"># 预热和优化图</span>
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>):
    <span class="hljs-keyword">with</span> torch.inference_mode():
        inputs = generate_inputs()
        orig_output = unet_traced(*inputs)

<span class="hljs-comment"># 基准测试</span>
<span class="hljs-keyword">with</span> torch.inference_mode():
    <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n_experiments):
        torch.cuda.synchronize()
        start_time = time.time()
        <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(unet_runs_per_experiment):
            orig_output = unet_traced(*inputs)
        torch.cuda.synchronize()
        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;unet traced inference took <span class="hljs-subst">{time.time() - start_time:<span class="hljs-number">.2</span>f}</span> seconds&quot;</span>)
    <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n_experiments):
        torch.cuda.synchronize()
        start_time = time.time()
        <span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(unet_runs_per_experiment):
            orig_output = unet(*inputs)
        torch.cuda.synchronize()
        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;unet inference took <span class="hljs-subst">{time.time() - start_time:<span class="hljs-number">.2</span>f}</span> seconds&quot;</span>)

<span class="hljs-comment"># 保存模型</span>
unet_traced.save(<span class="hljs-string">&quot;unet_traced.pt&quot;</span>)`,wrap:!1}}),Ml=new w({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEFmcm9tJTIwZGF0YWNsYXNzZXMlMjBpbXBvcnQlMjBkYXRhY2xhc3MlMEElMEElNDBkYXRhY2xhc3MlMEFjbGFzcyUyMFVOZXQyRENvbmRpdGlvbk91dHB1dCUzQSUwQSUyMCUyMCUyMCUyMHNhbXBsZSUzQSUyMHRvcmNoLlRlbnNvciUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUwQSUyMCUyMCUyMCUyMHRvcmNoX2R0eXBlJTNEdG9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VU5ldCgpJTBBJTBBd2l0aCUyMHRvcmNoLmluZmVyZW5jZV9tb2RlKCklM0ElMEElMjAlMjAlMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGUoJTVCcHJvbXB0JTVEJTIwKiUyMDElMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTApLmltYWdlcyU1QjAlNUQ=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass

<span class="hljs-meta">@dataclass</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">UNet2DConditionOutput</span>:
    sample: torch.Tensor

pipeline = StableDiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>,
    torch_dtype=torch.float16,
    use_safetensors=<span class="hljs-literal">True</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-comment"># 使用 jitted unet</span>
unet_traced = torch.jit.load(<span class="hljs-string">&quot;unet_traced.pt&quot;</span>)

<span class="hljs-comment"># del pipeline.unet</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">TracedUNet</span>(torch.nn.Module):
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>):
        <span class="hljs-built_in">super</span>().__init__()
        self.in_channels = pipe.unet.config.in_channels
        self.device = pipe.unet.device

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, latent_model_input, t, encoder_hidden_states</span>):
        sample = unet_traced(latent_model_input, t, encoder_hidden_states)[<span class="hljs-number">0</span>]
        <span class="hljs-keyword">return</span> UNet2DConditionOutput(sample=sample)

pipeline.unet = TracedUNet()

<span class="hljs-keyword">with</span> torch.inference_mode():
    image = pipe([prompt] * <span class="hljs-number">1</span>, num_inference_steps=<span class="hljs-number">50</span>).images[<span class="hljs-number">0</span>]`,wrap:!1}}),ol=new x({props:{title:"内存高效注意力",local:"内存高效注意力",headingTag:"h2"}}),E=new G({props:{warning:!1,$$slots:{default:[Et]},$$scope:{ctx:h}}}),dl=new w({props:{code:"JTIzJTIwcGlwJTIwaW5zdGFsbCUyMHhmb3JtZXJzJTBBaW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZSUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uWExQaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyc3RhYmlsaXR5YWklMkZzdGFibGUtZGlmZnVzaW9uLXhsLWJhc2UtMS4wJTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKS50byglMjJjdWRhJTIyKSUwQXBpcGVsaW5lLmVuYWJsZV94Zm9ybWVyc19tZW1vcnlfZWZmaWNpZW50X2F0dGVudGlvbigp",highlighted:`<span class="hljs-comment"># pip install xformers</span>
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionXLPipeline

pipeline = StableDiffusionXLPipeline.from_pretrained(
    <span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
    torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.enable_xformers_memory_efficient_attention()`,wrap:!1}}),bl=new w({props:{code:"cGlwZWxpbmUuZGlzYWJsZV94Zm9ybWVyc19tZW1vcnlfZWZmaWNpZW50X2F0dGVudGlvbigp",highlighted:"pipeline.disable_xformers_memory_efficient_attention()",wrap:!1}}),Ul=new xt({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/optimization/memory.md"}}),{c(){t=m("meta"),b=a(),c=m("p"),f=a(),M(U.$$.fragment),T=a(),Z=m("p"),Z.innerHTML=I,_=a(),C=m("p"),C.textContent=d,g=a(),M(V.$$.fragment),hl=a(),M(S.$$.fragment),wl=a(),H=m("p"),H.innerHTML=Jn,Zl=a(),M(A.$$.fragment),_l=a(),M(F.$$.fragment),gl=a(),L=m("p"),L.textContent=yn,Cl=a(),q=m("p"),q.innerHTML=un,xl=a(),M(D.$$.fragment),Il=a(),P=m("p"),P.textContent=jn,Wl=a(),M(K.$$.fragment),Gl=a(),M(O.$$.fragment),Vl=a(),M($.$$.fragment),$l=a(),ss=m("p"),ss.innerHTML=dn,Bl=a(),M(B.$$.fragment),kl=a(),ls=m("p"),ls.innerHTML=fn,vl=a(),es=m("p"),es.innerHTML=bn,Nl=a(),M(ns.$$.fragment),Xl=a(),ts=m("p"),ts.innerHTML=Un,Yl=a(),M(as.$$.fragment),Ql=a(),ps=m("p"),ps.innerHTML=Tn,Rl=a(),M(cs.$$.fragment),zl=a(),ms=m("p"),ms.textContent=hn,El=a(),is=m("ul"),is.innerHTML=wn,Sl=a(),rs=m("p"),rs.innerHTML=Zn,Hl=a(),M(Ms.$$.fragment),Al=a(),M(os.$$.fragment),Fl=a(),Js=m("p"),Js.textContent=_n,Ll=a(),ys=m("p"),ys.textContent=gn,ql=a(),us=m("p"),us.innerHTML=Cn,Dl=a(),M(js.$$.fragment),Pl=a(),M(k.$$.fragment),Kl=a(),M(ds.$$.fragment),Ol=a(),fs=m("p"),fs.textContent=xn,se=a(),bs=m("p"),bs.innerHTML=In,le=a(),M(Us.$$.fragment),ee=a(),M(v.$$.fragment),ne=a(),M(Ts.$$.fragment),te=a(),hs=m("p"),hs.textContent=Wn,ae=a(),ws=m("p"),ws.innerHTML=Gn,pe=a(),M(Zs.$$.fragment),ce=a(),_s=m("p"),_s.textContent=Vn,me=a(),gs=m("p"),gs.textContent=$n,ie=a(),M(N.$$.fragment),re=a(),Cs=m("p"),Cs.innerHTML=Bn,Me=a(),M(xs.$$.fragment),oe=a(),M(Is.$$.fragment),Je=a(),Ws=m("p"),Ws.innerHTML=kn,ye=a(),M(X.$$.fragment),ue=a(),Gs=m("p"),Gs.innerHTML=vn,je=a(),M(Vs.$$.fragment),de=a(),$s=m("p"),$s.innerHTML=Nn,fe=a(),M(Bs.$$.fragment),be=a(),ks=m("p"),ks.innerHTML=Xn,Ue=a(),M(Y.$$.fragment),Te=a(),vs=m("p"),vs.innerHTML=Yn,he=a(),Ns=m("p"),Ns.innerHTML=Qn,we=a(),Xs=m("ul"),Xs.innerHTML=Rn,Ze=a(),M(Ys.$$.fragment),_e=a(),M(Qs.$$.fragment),ge=a(),Rs=m("p"),Rs.innerHTML=zn,Ce=a(),zs=m("p"),zs.innerHTML=En,xe=a(),M(Q.$$.fragment),Ie=a(),Es=m("p"),Es.innerHTML=Sn,We=a(),M(Ss.$$.fragment),Ge=a(),Hs=m("p"),Hs.innerHTML=Hn,Ve=a(),M(As.$$.fragment),$e=a(),Fs=m("p"),Fs.textContent=An,Be=a(),Ls=m("p"),Ls.innerHTML=Fn,ke=a(),M(qs.$$.fragment),ve=a(),Ds=m("p"),Ds.innerHTML=Ln,Ne=a(),M(Ps.$$.fragment),Xe=a(),M(R.$$.fragment),Ye=a(),Ks=m("p"),Ks.innerHTML=qn,Qe=a(),M(z.$$.fragment),Re=a(),Os=m("p"),Os.innerHTML=Dn,ze=a(),M(sl.$$.fragment),Ee=a(),ll=m("p"),ll.innerHTML=Pn,Se=a(),M(el.$$.fragment),He=a(),M(nl.$$.fragment),Ae=a(),tl=m("p"),tl.innerHTML=Kn,Fe=a(),al=m("p"),al.textContent=On,Le=a(),M(pl.$$.fragment),qe=a(),M(cl.$$.fragment),De=a(),ml=m("p"),ml.innerHTML=st,Pe=a(),M(il.$$.fragment),Ke=a(),rl=m("p"),rl.textContent=lt,Oe=a(),M(Ml.$$.fragment),sn=a(),M(ol.$$.fragment),ln=a(),M(E.$$.fragment),en=a(),Jl=m("p"),Jl.textContent=et,nn=a(),yl=m("p"),yl.innerHTML=nt,tn=a(),ul=m("p"),ul.innerHTML=tt,an=a(),jl=m("p"),jl.innerHTML=at,pn=a(),M(dl.$$.fragment),cn=a(),fl=m("p"),fl.innerHTML=pt,mn=a(),M(bl.$$.fragment),rn=a(),M(Ul.$$.fragment),Mn=a(),Tl=m("p"),this.h()},l(s){const l=gt("svelte-u9bgzb",document.head);t=i(l,"META",{name:!0,content:!0}),l.forEach(e),b=p(s),c=i(s,"P",{}),bt(c).forEach(e),f=p(s),o(U.$$.fragment,s),T=p(s),Z=i(s,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-kg0q1b"&&(Z.innerHTML=I),_=p(s),C=i(s,"P",{"data-svelte-h":!0}),r(C)!=="svelte-i3qlfe"&&(C.textContent=d),g=p(s),o(V.$$.fragment,s),hl=p(s),o(S.$$.fragment,s),wl=p(s),H=i(s,"P",{"data-svelte-h":!0}),r(H)!=="svelte-13on0j6"&&(H.innerHTML=Jn),Zl=p(s),o(A.$$.fragment,s),_l=p(s),o(F.$$.fragment,s),gl=p(s),L=i(s,"P",{"data-svelte-h":!0}),r(L)!=="svelte-1080qfz"&&(L.textContent=yn),Cl=p(s),q=i(s,"P",{"data-svelte-h":!0}),r(q)!=="svelte-1ji9awf"&&(q.innerHTML=un),xl=p(s),o(D.$$.fragment,s),Il=p(s),P=i(s,"P",{"data-svelte-h":!0}),r(P)!=="svelte-1q2d295"&&(P.textContent=jn),Wl=p(s),o(K.$$.fragment,s),Gl=p(s),o(O.$$.fragment,s),Vl=p(s),o($.$$.fragment,s),$l=p(s),ss=i(s,"P",{"data-svelte-h":!0}),r(ss)!=="svelte-1yx7fp"&&(ss.innerHTML=dn),Bl=p(s),o(B.$$.fragment,s),kl=p(s),ls=i(s,"P",{"data-svelte-h":!0}),r(ls)!=="svelte-5mk4n7"&&(ls.innerHTML=fn),vl=p(s),es=i(s,"P",{"data-svelte-h":!0}),r(es)!=="svelte-n6hxwz"&&(es.innerHTML=bn),Nl=p(s),o(ns.$$.fragment,s),Xl=p(s),ts=i(s,"P",{"data-svelte-h":!0}),r(ts)!=="svelte-yo76tu"&&(ts.innerHTML=Un),Yl=p(s),o(as.$$.fragment,s),Ql=p(s),ps=i(s,"P",{"data-svelte-h":!0}),r(ps)!=="svelte-ze8jh9"&&(ps.innerHTML=Tn),Rl=p(s),o(cs.$$.fragment,s),zl=p(s),ms=i(s,"P",{"data-svelte-h":!0}),r(ms)!=="svelte-1fbdini"&&(ms.textContent=hn),El=p(s),is=i(s,"UL",{"data-svelte-h":!0}),r(is)!=="svelte-15h2fme"&&(is.innerHTML=wn),Sl=p(s),rs=i(s,"P",{"data-svelte-h":!0}),r(rs)!=="svelte-t6jreo"&&(rs.innerHTML=Zn),Hl=p(s),o(Ms.$$.fragment,s),Al=p(s),o(os.$$.fragment,s),Fl=p(s),Js=i(s,"P",{"data-svelte-h":!0}),r(Js)!=="svelte-hhjzz5"&&(Js.textContent=_n),Ll=p(s),ys=i(s,"P",{"data-svelte-h":!0}),r(ys)!=="svelte-azd05b"&&(ys.textContent=gn),ql=p(s),us=i(s,"P",{"data-svelte-h":!0}),r(us)!=="svelte-1jcre3h"&&(us.innerHTML=Cn),Dl=p(s),o(js.$$.fragment,s),Pl=p(s),o(k.$$.fragment,s),Kl=p(s),o(ds.$$.fragment,s),Ol=p(s),fs=i(s,"P",{"data-svelte-h":!0}),r(fs)!=="svelte-1n49s7i"&&(fs.textContent=xn),se=p(s),bs=i(s,"P",{"data-svelte-h":!0}),r(bs)!=="svelte-9l3su6"&&(bs.innerHTML=In),le=p(s),o(Us.$$.fragment,s),ee=p(s),o(v.$$.fragment,s),ne=p(s),o(Ts.$$.fragment,s),te=p(s),hs=i(s,"P",{"data-svelte-h":!0}),r(hs)!=="svelte-18bl8b2"&&(hs.textContent=Wn),ae=p(s),ws=i(s,"P",{"data-svelte-h":!0}),r(ws)!=="svelte-1o67dnx"&&(ws.innerHTML=Gn),pe=p(s),o(Zs.$$.fragment,s),ce=p(s),_s=i(s,"P",{"data-svelte-h":!0}),r(_s)!=="svelte-8sy35u"&&(_s.textContent=Vn),me=p(s),gs=i(s,"P",{"data-svelte-h":!0}),r(gs)!=="svelte-19u9hu8"&&(gs.textContent=$n),ie=p(s),o(N.$$.fragment,s),re=p(s),Cs=i(s,"P",{"data-svelte-h":!0}),r(Cs)!=="svelte-1u58nwg"&&(Cs.innerHTML=Bn),Me=p(s),o(xs.$$.fragment,s),oe=p(s),o(Is.$$.fragment,s),Je=p(s),Ws=i(s,"P",{"data-svelte-h":!0}),r(Ws)!=="svelte-9x85p5"&&(Ws.innerHTML=kn),ye=p(s),o(X.$$.fragment,s),ue=p(s),Gs=i(s,"P",{"data-svelte-h":!0}),r(Gs)!=="svelte-1l12c2i"&&(Gs.innerHTML=vn),je=p(s),o(Vs.$$.fragment,s),de=p(s),$s=i(s,"P",{"data-svelte-h":!0}),r($s)!=="svelte-1k1me1b"&&($s.innerHTML=Nn),fe=p(s),o(Bs.$$.fragment,s),be=p(s),ks=i(s,"P",{"data-svelte-h":!0}),r(ks)!=="svelte-19yi3hk"&&(ks.innerHTML=Xn),Ue=p(s),o(Y.$$.fragment,s),Te=p(s),vs=i(s,"P",{"data-svelte-h":!0}),r(vs)!=="svelte-eltwhr"&&(vs.innerHTML=Yn),he=p(s),Ns=i(s,"P",{"data-svelte-h":!0}),r(Ns)!=="svelte-14zzrw4"&&(Ns.innerHTML=Qn),we=p(s),Xs=i(s,"UL",{"data-svelte-h":!0}),r(Xs)!=="svelte-1ray78r"&&(Xs.innerHTML=Rn),Ze=p(s),o(Ys.$$.fragment,s),_e=p(s),o(Qs.$$.fragment,s),ge=p(s),Rs=i(s,"P",{"data-svelte-h":!0}),r(Rs)!=="svelte-pfde8s"&&(Rs.innerHTML=zn),Ce=p(s),zs=i(s,"P",{"data-svelte-h":!0}),r(zs)!=="svelte-1vw5frn"&&(zs.innerHTML=En),xe=p(s),o(Q.$$.fragment,s),Ie=p(s),Es=i(s,"P",{"data-svelte-h":!0}),r(Es)!=="svelte-w0fj4y"&&(Es.innerHTML=Sn),We=p(s),o(Ss.$$.fragment,s),Ge=p(s),Hs=i(s,"P",{"data-svelte-h":!0}),r(Hs)!=="svelte-t6xcks"&&(Hs.innerHTML=Hn),Ve=p(s),o(As.$$.fragment,s),$e=p(s),Fs=i(s,"P",{"data-svelte-h":!0}),r(Fs)!=="svelte-1nq3fej"&&(Fs.textContent=An),Be=p(s),Ls=i(s,"P",{"data-svelte-h":!0}),r(Ls)!=="svelte-1hbe5xk"&&(Ls.innerHTML=Fn),ke=p(s),o(qs.$$.fragment,s),ve=p(s),Ds=i(s,"P",{"data-svelte-h":!0}),r(Ds)!=="svelte-17y83c8"&&(Ds.innerHTML=Ln),Ne=p(s),o(Ps.$$.fragment,s),Xe=p(s),o(R.$$.fragment,s),Ye=p(s),Ks=i(s,"P",{"data-svelte-h":!0}),r(Ks)!=="svelte-1f732i8"&&(Ks.innerHTML=qn),Qe=p(s),o(z.$$.fragment,s),Re=p(s),Os=i(s,"P",{"data-svelte-h":!0}),r(Os)!=="svelte-zead05"&&(Os.innerHTML=Dn),ze=p(s),o(sl.$$.fragment,s),Ee=p(s),ll=i(s,"P",{"data-svelte-h":!0}),r(ll)!=="svelte-txkzq8"&&(ll.innerHTML=Pn),Se=p(s),o(el.$$.fragment,s),He=p(s),o(nl.$$.fragment,s),Ae=p(s),tl=i(s,"P",{"data-svelte-h":!0}),r(tl)!=="svelte-8d4g9i"&&(tl.innerHTML=Kn),Fe=p(s),al=i(s,"P",{"data-svelte-h":!0}),r(al)!=="svelte-kbl8lh"&&(al.textContent=On),Le=p(s),o(pl.$$.fragment,s),qe=p(s),o(cl.$$.fragment,s),De=p(s),ml=i(s,"P",{"data-svelte-h":!0}),r(ml)!=="svelte-1feyw5u"&&(ml.innerHTML=st),Pe=p(s),o(il.$$.fragment,s),Ke=p(s),rl=i(s,"P",{"data-svelte-h":!0}),r(rl)!=="svelte-qm13iw"&&(rl.textContent=lt),Oe=p(s),o(Ml.$$.fragment,s),sn=p(s),o(ol.$$.fragment,s),ln=p(s),o(E.$$.fragment,s),en=p(s),Jl=i(s,"P",{"data-svelte-h":!0}),r(Jl)!=="svelte-1m5qcbu"&&(Jl.textContent=et),nn=p(s),yl=i(s,"P",{"data-svelte-h":!0}),r(yl)!=="svelte-5jk4ak"&&(yl.innerHTML=nt),tn=p(s),ul=i(s,"P",{"data-svelte-h":!0}),r(ul)!=="svelte-1f4dy2o"&&(ul.innerHTML=tt),an=p(s),jl=i(s,"P",{"data-svelte-h":!0}),r(jl)!=="svelte-b9wnp4"&&(jl.innerHTML=at),pn=p(s),o(dl.$$.fragment,s),cn=p(s),fl=i(s,"P",{"data-svelte-h":!0}),r(fl)!=="svelte-go6u4e"&&(fl.innerHTML=pt),mn=p(s),o(bl.$$.fragment,s),rn=p(s),o(Ul.$$.fragment,s),Mn=p(s),Tl=i(s,"P",{}),bt(Tl).forEach(e),this.h()},h(){Ut(t,"name","hf:doc:metadata"),Ut(t,"content",Ht)},m(s,l){Ct(document.head,t),n(s,b,l),n(s,c,l),n(s,f,l),J(U,s,l),n(s,T,l),n(s,Z,l),n(s,_,l),n(s,C,l),n(s,g,l),J(V,s,l),n(s,hl,l),J(S,s,l),n(s,wl,l),n(s,H,l),n(s,Zl,l),J(A,s,l),n(s,_l,l),J(F,s,l),n(s,gl,l),n(s,L,l),n(s,Cl,l),n(s,q,l),n(s,xl,l),J(D,s,l),n(s,Il,l),n(s,P,l),n(s,Wl,l),J(K,s,l),n(s,Gl,l),J(O,s,l),n(s,Vl,l),J($,s,l),n(s,$l,l),n(s,ss,l),n(s,Bl,l),J(B,s,l),n(s,kl,l),n(s,ls,l),n(s,vl,l),n(s,es,l),n(s,Nl,l),J(ns,s,l),n(s,Xl,l),n(s,ts,l),n(s,Yl,l),J(as,s,l),n(s,Ql,l),n(s,ps,l),n(s,Rl,l),J(cs,s,l),n(s,zl,l),n(s,ms,l),n(s,El,l),n(s,is,l),n(s,Sl,l),n(s,rs,l),n(s,Hl,l),J(Ms,s,l),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s,Le,l),J(pl,s,l),n(s,qe,l),J(cl,s,l),n(s,De,l),n(s,ml,l),n(s,Pe,l),J(il,s,l),n(s,Ke,l),n(s,rl,l),n(s,Oe,l),J(Ml,s,l),n(s,sn,l),J(ol,s,l),n(s,ln,l),J(E,s,l),n(s,en,l),n(s,Jl,l),n(s,nn,l),n(s,yl,l),n(s,tn,l),n(s,ul,l),n(s,an,l),n(s,jl,l),n(s,pn,l),J(dl,s,l),n(s,cn,l),n(s,fl,l),n(s,mn,l),J(bl,s,l),n(s,rn,l),J(Ul,s,l),n(s,Mn,l),n(s,Tl,l),on=!0},p(s,[l]){const ct={};l&2&&(ct.$$scope={dirty:l,ctx:s}),V.$set(ct);const mt={};l&2&&(mt.$$scope={dirty:l,ctx:s}),$.$set(mt);const it={};l&2&&(it.$$scope={dirty:l,ctx:s}),B.$set(it);const rt={};l&2&&(rt.$$scope={dirty:l,ctx:s}),k.$set(rt);const Mt={};l&2&&(Mt.$$scope={dirty:l,ctx:s}),v.$set(Mt);const ot={};l&2&&(ot.$$scope={dirty:l,ctx:s}),N.$set(ot);const Jt={};l&2&&(Jt.$$scope={dirty:l,ctx:s}),X.$set(Jt);const yt={};l&2&&(yt.$$scope={dirty:l,ctx:s}),Y.$set(yt);const ut={};l&2&&(ut.$$scope={dirty:l,ctx:s}),Q.$set(ut);const jt={};l&2&&(jt.$$scope={dirty:l,ctx:s}),R.$set(jt);const dt={};l&2&&(dt.$$scope={dirty:l,ctx:s}),z.$set(dt);const ft={};l&2&&(ft.$$scope={dirty:l,ctx:s}),E.$set(ft)},i(s){on||(y(U.$$.fragment,s),y(V.$$.fragment,s),y(S.$$.fragment,s),y(A.$$.fragment,s),y(F.$$.fragment,s),y(D.$$.fragment,s),y(K.$$.fragment,s),y(O.$$.fragment,s),y($.$$.fragment,s),y(B.$$.fragment,s),y(ns.$$.fragment,s),y(as.$$.fragment,s),y(cs.$$.fragment,s),y(Ms.$$.fragment,s),y(os.$$.fragment,s),y(js.$$.fragment,s),y(k.$$.fragment,s),y(ds.$$.fragment,s),y(Us.$$.fragment,s),y(v.$$.fragment,s),y(Ts.$$.fragment,s),y(Zs.$$.fragment,s),y(N.$$.fragment,s),y(xs.$$.fragment,s),y(Is.$$.fragment,s),y(X.$$.fragment,s),y(Vs.$$.fragment,s),y(Bs.$$.fragment,s),y(Y.$$.fragment,s),y(Ys.$$.fragment,s),y(Qs.$$.fragment,s),y(Q.$$.fragment,s),y(Ss.$$.fragment,s),y(As.$$.fragment,s),y(qs.$$.fragment,s),y(Ps.$$.fragment,s),y(R.$$.fragment,s),y(z.$$.fragment,s),y(sl.$$.fragment,s),y(el.$$.fragment,s),y(nl.$$.fragment,s),y(pl.$$.fragment,s),y(cl.$$.fragment,s),y(il.$$.fragment,s),y(Ml.$$.fragment,s),y(ol.$$.fragment,s),y(E.$$.fragment,s),y(dl.$$.fragment,s),y(bl.$$.fragment,s),y(Ul.$$.fragment,s),on=!0)},o(s){u(U.$$.fragment,s),u(V.$$.fragment,s),u(S.$$.fragment,s),u(A.$$.fragment,s),u(F.$$.fragment,s),u(D.$$.fragment,s),u(K.$$.fragment,s),u(O.$$.fragment,s),u($.$$.fragment,s),u(B.$$.fragment,s),u(ns.$$.fragment,s),u(as.$$.fragment,s),u(cs.$$.fragment,s),u(Ms.$$.fragment,s),u(os.$$.fragment,s),u(js.$$.fragment,s),u(k.$$.fragment,s),u(ds.$$.fragment,s),u(Us.$$.fragment,s),u(v.$$.fragment,s),u(Ts.$$.fragment,s),u(Zs.$$.fragment,s),u(N.$$.fragment,s),u(xs.$$.fragment,s),u(Is.$$.fragment,s),u(X.$$.fragment,s),u(Vs.$$.fragment,s),u(Bs.$$.fragment,s),u(Y.$$.fragment,s),u(Ys.$$.fragment,s),u(Qs.$$.fragment,s),u(Q.$$.fragment,s),u(Ss.$$.fragment,s),u(As.$$.fragment,s),u(qs.$$.fragment,s),u(Ps.$$.fragment,s),u(R.$$.fragment,s),u(z.$$.fragment,s),u(sl.$$.fragment,s),u(el.$$.fragment,s),u(nl.$$.fragment,s),u(pl.$$.fragment,s),u(cl.$$.fragment,s),u(il.$$.fragment,s),u(Ml.$$.fragment,s),u(ol.$$.fragment,s),u(E.$$.fragment,s),u(dl.$$.fragment,s),u(bl.$$.fragment,s),u(Ul.$$.fragment,s),on=!1},d(s){s&&(e(b),e(c),e(f),e(T),e(Z),e(_),e(C),e(g),e(hl),e(wl),e(H),e(Zl),e(_l),e(gl),e(L),e(Cl),e(q),e(xl),e(Il),e(P),e(Wl),e(Gl),e(Vl),e($l),e(ss),e(Bl),e(kl),e(ls),e(vl),e(es),e(Nl),e(Xl),e(ts),e(Yl),e(Ql),e(ps),e(Rl),e(zl),e(ms),e(El),e(is),e(Sl),e(rs),e(Hl),e(Al),e(Fl),e(Js),e(Ll),e(ys),e(ql),e(us),e(Dl),e(Pl),e(Kl),e(Ol),e(fs),e(se),e(bs),e(le),e(ee),e(ne),e(te),e(hs),e(ae),e(ws),e(pe),e(ce),e(_s),e(me),e(gs),e(ie),e(re),e(Cs),e(Me),e(oe),e(Je),e(Ws),e(ye),e(ue),e(Gs),e(je),e(de),e($s),e(fe),e(be),e(ks),e(Ue),e(Te),e(vs),e(he),e(Ns),e(we),e(Xs),e(Ze),e(_e),e(ge),e(Rs),e(Ce),e(zs),e(xe),e(Ie),e(Es),e(We),e(Ge),e(Hs),e(Ve),e($e),e(Fs),e(Be),e(Ls),e(ke),e(ve),e(Ds),e(Ne),e(Xe),e(Ye),e(Ks),e(Qe),e(Re),e(Os),e(ze),e(Ee),e(ll),e(Se),e(He),e(Ae),e(tl),e(Fe),e(al),e(Le),e(qe),e(De),e(ml),e(Pe),e(Ke),e(rl),e(Oe),e(sn),e(ln),e(en),e(Jl),e(nn),e(yl),e(tn),e(ul),e(an),e(jl),e(pn),e(cn),e(fl),e(mn),e(rn),e(Mn),e(Tl)),e(t),j(U,s),j(V,s),j(S,s),j(A,s),j(F,s),j(D,s),j(K,s),j(O,s),j($,s),j(B,s),j(ns,s),j(as,s),j(cs,s),j(Ms,s),j(os,s),j(js,s),j(k,s),j(ds,s),j(Us,s),j(v,s),j(Ts,s),j(Zs,s),j(N,s),j(xs,s),j(Is,s),j(X,s),j(Vs,s),j(Bs,s),j(Y,s),j(Ys,s),j(Qs,s),j(Q,s),j(Ss,s),j(As,s),j(qs,s),j(Ps,s),j(R,s),j(z,s),j(sl,s),j(el,s),j(nl,s),j(pl,s),j(cl,s),j(il,s),j(Ml,s),j(ol,s),j(E,s),j(dl,s),j(bl,s),j(Ul,s)}}}const Ht='{"title":"减少内存使用","local":"减少内存使用","sections":[{"title":"多个 GPU","local":"多个-gpu","sections":[{"title":"分片检查点","local":"分片检查点","sections":[],"depth":3},{"title":"设备放置","local":"设备放置","sections":[],"depth":3}],"depth":2},{"title":"VAE 切片","local":"vae-切片","sections":[],"depth":2},{"title":"VAE 平铺","local":"vae-平铺","sections":[],"depth":2},{"title":"卸载","local":"卸载","sections":[{"title":"CPU 卸载","local":"cpu-卸载","sections":[],"depth":3},{"title":"模型卸载","local":"模型卸载","sections":[],"depth":3},{"title":"组卸载","local":"组卸载","sections":[{"title":"CUDA 流","local":"cuda-流","sections":[],"depth":4},{"title":"卸载到磁盘","local":"卸载到磁盘","sections":[],"depth":4}],"depth":3}],"depth":2},{"title":"分层类型转换","local":"分层类型转换","sections":[],"depth":2},{"title":"torch.channels_last","local":"torchchannelslast","sections":[],"depth":2},{"title":"torch.jit.trace","local":"torchjittrace","sections":[],"depth":2},{"title":"内存高效注意力","local":"内存高效注意力","sections":[],"depth":2}],"depth":1}';function At(h){return wt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ot extends Zt{constructor(t){super(),_t(this,t,At,St,ht,{})}}export{Ot as component};
