import{s as wa,o as Za,n as ha}from"../chunks/scheduler.e4ff9b64.js";import{S as _a,i as Ca,e as p,s as a,c as M,h as xa,a as c,d as e,b as t,f as Ta,g as r,j as i,k as I,l as Ia,m as n,n as o,t as J,o as y,p as j}from"../chunks/index.09f1bca0.js";import{C as ga,H as C,E as Wa}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.11400f6f.js";import{C as T}from"../chunks/CodeBlock.78f3be90.js";import{H as Ga,a as fa}from"../chunks/HfOption.44827c7f.js";function Va(W){let d,x="<code>balanced</code> 设备放置策略将管道均匀分割到所有可用设备上。",f,U,u,b,_="您可以使用 <code>hf_device_map</code> 检查管道的设备映射。",Z,h,g;return U=new T({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}}),h=new T({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(){d=p("p"),d.innerHTML=x,f=a(),M(U.$$.fragment),u=a(),b=p("p"),b.innerHTML=_,Z=a(),M(h.$$.fragment)},l(m){d=c(m,"P",{"data-svelte-h":!0}),i(d)!=="svelte-on9lpr"&&(d.innerHTML=x),f=t(m),r(U.$$.fragment,m),u=t(m),b=c(m,"P",{"data-svelte-h":!0}),i(b)!=="svelte-7k1fi2"&&(b.innerHTML=_),Z=t(m),r(h.$$.fragment,m)},m(m,w){n(m,d,w),n(m,f,w),o(U,m,w),n(m,u,w),n(m,b,w),n(m,Z,w),o(h,m,w),g=!0},p:ha,i(m){g||(J(U.$$.fragment,m),J(h.$$.fragment,m),g=!0)},o(m){y(U.$$.fragment,m),y(h.$$.fragment,m),g=!1},d(m){m&&(e(d),e(f),e(u),e(b),e(Z)),j(U,m),j(h,m)}}}function ka(W){let d,x='<code>device_map</code> 对于加载大型模型非常有用，例如具有 125 亿参数的 Flux diffusion transformer。将其设置为 <code>&quot;auto&quot;</code> 可以自动将模型首先分布到最快的设备上，然后再移动到较慢的设备。有关更多详细信息，请参阅 <a href="../training/distributed_inference#model-sharding">模型分片</a> 文档。',f,U,u,b,_="您可以使用 <code>hf_device_map</code> 检查模型的设备映射。",Z,h,g;return U=new T({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}}),h=new T({props:{code:"cHJpbnQodHJhbnNmb3JtZXIuaGZfZGV2aWNlX21hcCk=",highlighted:'<span class="hljs-built_in">print</span>(transformer.hf_device_map)',wrap:!1}}),{c(){d=p("p"),d.innerHTML=x,f=a(),M(U.$$.fragment),u=a(),b=p("p"),b.innerHTML=_,Z=a(),M(h.$$.fragment)},l(m){d=c(m,"P",{"data-svelte-h":!0}),i(d)!=="svelte-gz97o7"&&(d.innerHTML=x),f=t(m),r(U.$$.fragment,m),u=t(m),b=c(m,"P",{"data-svelte-h":!0}),i(b)!=="svelte-fe42wi"&&(b.innerHTML=_),Z=t(m),r(h.$$.fragment,m)},m(m,w){n(m,d,w),n(m,f,w),o(U,m,w),n(m,u,w),n(m,b,w),n(m,Z,w),o(h,m,w),g=!0},p:ha,i(m){g||(J(U.$$.fragment,m),J(h.$$.fragment,m),g=!0)},o(m){y(U.$$.fragment,m),y(h.$$.fragment,m),g=!1},d(m){m&&(e(d),e(f),e(u),e(b),e(Z)),j(U,m),j(h,m)}}}function Ba(W){let d,x,f,U;return d=new fa({props:{id:"device-map",option:"pipeline level",$$slots:{default:[Va]},$$scope:{ctx:W}}}),f=new fa({props:{id:"device-map",option:"model level",$$slots:{default:[ka]},$$scope:{ctx:W}}}),{c(){M(d.$$.fragment),x=a(),M(f.$$.fragment)},l(u){r(d.$$.fragment,u),x=t(u),r(f.$$.fragment,u)},m(u,b){o(d,u,b),n(u,x,b),o(f,u,b),U=!0},p(u,b){const _={};b&2&&(_.$$scope={dirty:b,ctx:u}),d.$set(_);const Z={};b&2&&(Z.$$scope={dirty:b,ctx:u}),f.$set(Z)},i(u){U||(J(d.$$.fragment,u),J(f.$$.fragment,u),U=!0)},o(u){y(d.$$.fragment,u),y(f.$$.fragment,u),U=!1},d(u){u&&e(x),j(d,u),j(f,u)}}}function va(W){let d,x,f,U,u,b,_,Z,h,g='现代diffusion models，如 <a href="../api/pipelines/flux">Flux</a> 和 <a href="../api/pipelines/wan">Wan</a>，拥有数十亿参数，在您的硬件上进行推理时会占用大量内存。这是一个挑战，因为常见的 GPU 通常没有足够的内存。为了克服内存限制，您可以使用多个 GPU（如果可用）、将一些管道组件卸载到 CPU 等。',m,w,Jn="本指南将展示如何减少内存使用。",fs,G,yn="<p>请记住，这些技术可能需要根据模型进行调整。例如，基于 transformer 的扩散模型可能不会像基于 UNet 的模型那样从这些内存优化中同等受益。</p>",hs,E,ws,S,jn='如果您有多个 GPU 的访问权限，有几种选项可以高效地在硬件上加载和分发大型模型。这些功能由 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 库支持，因此请确保先安装它。',Zs,H,_s,A,Cs,F,un="将大型检查点加载到多个分片中很有用，因为分片会逐个加载。这保持了低内存使用，只需要足够的内存来容纳模型大小和最大分片大小。我们建议当 fp32 检查点大于 5GB 时进行分片。默认分片大小为 5GB。",xs,L,dn="在 <code>save_pretrained()</code> 中使用 <code>max_shard_size</code> 参数对检查点进行分片。",Is,q,gs,D,bn="现在您可以使用分片检查点，而不是常规检查点，以节省内存。",Ws,P,Gs,K,Vs,V,Un="<p>设备放置是一个实验性功能，API 可能会更改。目前仅支持 <code>balanced</code> 策略。我们计划在未来支持额外的映射策略。</p>",ks,O,Tn=`<code>device_map</code> 参数控制管道或模型中的组件如何
单个模型中的层分布在多个设备上。`,Bs,k,vs,ll,fn="当设计您自己的 <code>device_map</code> 时，它应该是一个字典，包含模型的特定模块名称或层以及设备标识符（整数表示 GPU，<code>cpu</code> 表示 CPU，<code>disk</code> 表示磁盘）。",Ns,sl,hn="在模型上调用 <code>hf_device_map</code> 以查看模型层如何分布，然后设计您自己的映射。",Xs,el,Qs,nl,wn="例如，下面的 <code>device_map</code> 将 <code>single_transformer_blocks.10</code> 到 <code>single_transformer_blocks.20</code> 放置在第二个 GPU（<code>1</code>）上。",Ys,al,Rs,tl,Zn="传递一个字典，将最大内存使用量映射到每个设备以强制执行限制。如果设备不在 <code>max_memory</code> 中，它将被忽略，管道组件不会分发到该设备。",zs,pl,$s,cl,_n="Diffusers 默认使用所有设备的最大内存，但如果它们无法适应 GPU，则需要使用单个 GPU 并通过以下方法卸载到 CPU。",Es,il,Cn="<li><code>enable_model_cpu_offload()</code> 仅适用于单个 GPU，但非常大的模型可能无法适应它</li> <li>使用 <code>enable_sequential_cpu_offload()</code> 可能有效，但它极其缓慢，并且仅限于单个 GPU。</li>",Ss,ml,xn="使用 <code>reset_device_map()</code> 方法来重置 <code>device_map</code>。如果您想在已进行设备映射的管道上使用方法如 <code>.to()</code>、<code>enable_sequential_cpu_offload()</code> 和 <code>enable_model_cpu_offload()</code>，这是必要的。",Hs,Ml,As,rl,Fs,ol,In="VAE 切片通过将大批次输入拆分为单个数据批次并分别处理它们来节省内存。这种方法在同时生成多个图像时效果最佳。",Ls,Jl,gn="例如，如果您同时生成 4 个图像，解码会将峰值激活内存增加 4 倍。VAE 切片通过一次只解码 1 个图像而不是所有 4 个图像来减少这种情况。",qs,yl,Wn="调用 <code>enable_vae_slicing()</code> 来启用切片 VAE。您可以预期在解码多图像批次时性能会有小幅提升，而在单图像批次时没有性能影响。",Ds,jl,Ps,B,Gn="<p><code>AutoencoderKLWan</code> 和 <code>AsymmetricAutoencoderKL</code> 类不支持切片。</p>",Ks,ul,Os,dl,Vn="VAE 平铺通过将图像划分为较小的重叠图块而不是一次性处理整个图像来节省内存。这也减少了峰值内存使用量，因为 GPU 一次只处理一个图块。",le,bl,kn="调用 <code>enable_vae_tiling()</code> 来启用 VAE 平铺。生成的图像可能因图块到图块的色调变化而有所不同，因为它们被单独解码，但图块之间不应有明显的接缝。对于低于预设（但可配置）限制的分辨率，平铺被禁用。例如，对于 <code>StableDiffusionPipeline</code> 中的 VAE，此限制为 512x512。",se,Ul,ee,v,Bn="<p><code>AutoencoderKLWan</code> 和 <code>AsymmetricAutoencoderKL</code> 不支持平铺。</p>",ne,Tl,ae,fl,vn=`卸载策略将非当前活动层移动
将模型移动到 CPU 以避免增加 GPU 内存。这些策略可以与量化和 torch.compile 结合使用，以平衡推理速度和内存使用。`,te,hl,Nn='有关更多详细信息，请参考 <a href="./speed-memory-optims">编译和卸载量化模型</a> 指南。',pe,wl,ce,Zl,Xn="CPU 卸载选择性地将权重从 GPU 移动到 CPU。当需要某个组件时，它被传输到 GPU；当不需要时，它被移动到 CPU。此方法作用于子模块而非整个模型。它通过避免将整个模型存储在 GPU 上来节省内存。",ie,_l,Qn="CPU 卸载显著减少内存使用，但由于子模块在设备之间多次来回传递，它也非常慢。由于速度极慢，它通常不实用。",me,N,Yn='<p>在调用 <code>enable_sequential_cpu_offload()</code> 之前，不要将管道移动到 CUDA，否则节省的内存非常有限（更多细节请参考此 <a href="https://github.com/huggingface/diffusers/issues/1934" rel="nofollow">issue</a>）。这是一个状态操作，会在模型上安装钩子。</p>',Me,Cl,Rn="调用 <code>enable_sequential_cpu_offload()</code> 以在管道上启用它。",re,xl,oe,Il,Je,gl,zn='模型卸载将整个模型移动到 GPU，而不是选择性地移动某些层或模型组件。一个主要管道模型，通常是文本编码器、UNet 和 VAE，被放置在 GPU 上，而其他组件保持在 CPU 上。像 UNet 这样运行多次的组件会一直留在 GPU 上，直到完全完成且不再需要。这消除了 <a href="#cpu-offloading">CPU 卸载</a> 的通信开销，使模型卸载成为一个更快的替代方案。权衡是内存节省不会那么大。',ye,X,$n='<p>请注意，如果在安装钩子后模型在管道外部被重用（更多细节请参考 <a href="https://huggingface.co/docs/accelerate/en/package_reference/big_modeling#accelerate.hooks.remove_hook_from_module" rel="nofollow">移除钩子</a>），您需要按预期顺序运行整个管道和模型以正确卸载它们。这是一个状态操作，会在模型上安装钩子。</p>',je,Wl,En="调用 <code>enable_model_cpu_offload()</code> 以在管道上启用它。",ue,Gl,de,Vl,Sn="<code>enable_model_cpu_offload()</code> 在您单独使用 <code>encode_prompt()</code> 方法生成文本编码器隐藏状态时也有帮助。",be,kl,Ue,Bl,Hn='组卸载将内部层组（<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>更快，因为它减少了通信开销。',Te,Q,An="<p>如果前向实现包含权重相关的输入设备转换，组卸载可能不适用于所有模型，因为它可能与组卸载的设备转换机制冲突。</p>",fe,vl,Fn='调用 <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,Nl,Ln="<code>offload_type</code> 参数可以设置为 <code>block_level</code> 或 <code>leaf_level</code>。",we,Xl,qn='<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,Ql,_e,Yl,Ce,Rl,Dn='<code>use_stream</code> 参数可以激活支持异步数据传输流的 CUDA 设备，以减少整体执行时间，与 <a href="#cpu-offloading">CPU 卸载</a> 相比。它通过使用层预取重叠数据传输和计算。下一个要执行的层在当前层仍在执行时加载到 GPU 上。这会显著增加 CPU 内存，因此请确保您有模型大小的 2 倍内存。',xe,zl,Pn='设置 <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> 文档了解更多信息。',Ie,Y,Kn="<p>当 <code>use_stream=True</code> 在启用平铺的 VAEs 上时，确保在推理前进行虚拟前向传递（可以使用虚拟输入），以避免设备不匹配错误。这可能不适用于所有实现，因此如果遇到任何问题，请随时提出问题。</p>",ge,$l,On="如果您在使用启用 <code>use_stream</code> 的 <code>block_level</code> 组卸载，<code>num_blocks_per_group</code> 参数应设置为 <code>1</code>，否则会引发警告。",We,El,Ge,Sl,la="<code>low_cpu_mem_usage</code> 参数可以设置为 <code>True</code>，以在使用流进行组卸载时减少 CPU 内存使用。它最适合 <code>leaf_level</code> 卸载和 CPU 内存瓶颈的情况。通过动态创建固定张量而不是预先固定它们来节省内存。然而，这可能会增加整体执行时间。",Ve,Hl,ke,Al,sa="组卸载可能会消耗大量系统内存，具体取决于模型大小。在内存有限的系统上，尝试将组卸载到磁盘作为辅助内存。",Be,Fl,ea="在 <code>enable_group_offload()</code> 或 <code>apply_group_offloading()</code> 中设置 <code>offload_to_disk_path</code> 参数，将模型卸载到磁盘。",ve,Ll,Ne,ql,na='参考这些<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>来比较速度和内存的权衡。',Xe,Dl,Qe,R,aa='<p>将分层类型转换与<a href="#group-offloading">组卸载</a>结合使用，以获得更多内存节省。</p>',Ye,Pl,ta="分层类型转换将权重存储在较小的数据格式中（例如 <code>torch.float8_e4m3fn</code> 和 <code>torch.float8_e5m2</code>），以使用更少的内存，并在计算时将那些权重上转换为更高精度如 <code>torch.float16</code> 或 <code>torch.bfloat16</code>。某些层（归一化和调制相关权重）被跳过，因为将它们存储在 fp8 中可能会降低生成质量。",Re,z,pa='<p>如果前向实现包含权重的内部类型转换，分层类型转换可能不适用于所有模型。当前的分层类型转换实现假设前向传递独立于权重精度，并且输入数据类型始终在 <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>以获取不兼容的实现）。</p> <p>分层类型转换也可能在使用<a href="https://huggingface.co/docs/peft/index" rel="nofollow">PEFT</a>层的自定义建模实现上失败。有一些检查可用，但它们没有经过广泛测试或保证在所有情况下都能工作。</p>',ze,Kl,ca="调用 <code>enable_layerwise_casting()</code> 来设置存储和计算数据类型。",$e,Ol,Ee,ls,ia="<code>apply_layerwise_casting()</code> 方法也可以在您需要更多控制和灵活性时使用。它可以通过在特定内部模块上调用它来部分应用于模型层。使用 <code>skip_modules_pattern</code> 或 <code>skip_modules_classes</code> 参数来指定要避免的模块，例如归一化和调制层。",Se,ss,He,es,Ae,ns,ma='<a href="https://pytorch.org/tutorials/intermediate/memory_format_tutorial.html" rel="nofollow">torch.channels_last</a> 将张量的存储方式从 <code>(批次大小, 通道数, 高度, 宽度)</code> 翻转为 <code>(批次大小, 高度, 宽度, 通道数)</code>。这使张量与硬件如何顺序访问存储在内存中的张量对齐，并避免了在内存中跳转以访问像素值。',Fe,as,Ma="并非所有运算符当前都支持通道最后格式，并且可能导致性能更差，但仍然值得尝试。",Le,ts,qe,ps,De,cs,ra='<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,is,Ke,ms,oa="替换管道的 UNet 为追踪版本。",Oe,Ms,ln,rs,sn,$,Ja='<p>内存高效注意力优化内存使用 <em>和</em> <a href="./fp16#scaled-dot-product-attention">推理速度</a>！</p>',en,os,ya="Transformers 注意力机制是内存密集型的，尤其对于长序列，因此您可以尝试使用不同且更内存高效的注意力类型。",nn,Js,ja='默认情况下，如果安装了 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>。您无需对代码进行任何额外更改。',an,ys,ua=`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 实现。它会根据您的输入自动选择最优的实现。`,tn,js,da="您可以使用 <code>enable_xformers_memory_efficient_attention()</code> 方法显式地使用 xFormers。",pn,us,cn,ds,ba="调用 <code>disable_xformers_memory_efficient_attention()</code> 来禁用它。",mn,bs,Mn,Us,rn,Ts,on;return u=new ga({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),_=new C({props:{title:"减少内存使用",local:"减少内存使用",headingTag:"h1"}}),E=new C({props:{title:"多个 GPU",local:"多个-gpu",headingTag:"h2"}}),H=new T({props:{code:"cGlwJTIwaW5zdGFsbCUyMC1VJTIwYWNjZWxlcmF0ZQ==",highlighted:"pip install -U accelerate",wrap:!1}}),A=new C({props:{title:"分片检查点",local:"分片检查点",headingTag:"h3"}}),q=new T({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}}),P=new T({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}}),K=new C({props:{title:"设备放置",local:"设备放置",headingTag:"h3"}}),k=new Ga({props:{id:"device-map",options:["pipeline level","model level"],$$slots:{default:[Ba]},$$scope:{ctx:W}}}),el=new T({props:{code:"cHJpbnQodHJhbnNmb3JtZXIuaGZfZGV2aWNlX21hcCklMEElN0IncG9zX2VtYmVkJyUzQSUyMDAlMkMlMjAndGltZV90ZXh0X2VtYmVkJyUzQSUyMDAlMkMlMjAnY29udGV4dF9lbWJlZGRlciclM0ElMjAwJTJDJTIwJ3hfZW1iZWRkZXInJTNBJTIwMCUyQyUyMCd0cmFuc2Zvcm1lcl9ibG9ja3MnJTNBJTIwMCUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjAnJTNBJTIwMCUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjEnJTNBJTIwMCUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjInJTNBJTIwMCU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2xlX3RyYW5zZm9ybWVyX2Jsb2Nrcy4zNiclM0ElMjAnY3B1JyUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjM3JyUzQSUyMCdjcHUnJTJDJTIwJ25vcm1fb3V0JyUzQSUyMCdjcHUnJTJDJTIwJ3Byb2pfb3V0JyUzQSUyMCdjcHUnJTdE",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}}),al=new T({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQXV0b01vZGVsJTBBJTBBZGV2aWNlX21hcCUyMCUzRCUyMCU3QiUwQSUyMCUyMCUyMCUyMCdwb3NfZW1iZWQnJTNBJTIwMCUyQyUyMCd0aW1lX3RleHRfZW1iZWQnJTNBJTIwMCUyQyUyMCdjb250ZXh0X2VtYmVkZGVyJyUzQSUyMDAlMkMlMjAneF9lbWJlZGRlciclM0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yclM0ElMjAnY3B1JyUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjM0JyUzQSUyMCdjcHUnJTJDJTIwJ3NpbmdsZV90cmFuc2Zvcm1lcl9ibG9ja3MuMzUnJTNBJTIwJ2NwdSclMkMlMjAnc2luZ2xlX3RyYW5zZm9ybWVyX2Jsb2Nrcy4zNiclM0ElMjAnY3B1JyUyQyUyMCdzaW5nbGVfdHJhbnNmb3JtZXJfYmxvY2tzLjM3JyUzQSUyMCdjcHUnJTJDJTIwJ25vcm1fb3V0JyUzQSUyMCdjcHUnJTJDJTIwJ3Byb2pfb3V0JyUzQSUyMCdjcHUnJTBBJTdEJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBBdXRvTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMmJsYWNrLWZvcmVzdC1sYWJzJTJGRkxVWC4xLWRldiUyMiUyQyUyMCUwQSUyMCUyMCUyMCUyMHN1YmZvbGRlciUzRCUyMnRyYW5zZm9ybWVyJTIyJTJDJTBBJTIwJTIwJTIwJTIwZGV2aWNlX21hcCUzRGRldmljZV9tYXAlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKQ==",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}}),pl=new T({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}}),Ml=new T({props:{code:"cGlwZWxpbmUucmVzZXRfZGV2aWNlX21hcCgp",highlighted:"pipeline.reset_device_map()",wrap:!1}}),rl=new C({props:{title:"VAE 切片",local:"vae-切片",headingTag:"h2"}}),jl=new T({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}}),ul=new C({props:{title:"VAE 平铺",local:"vae-平铺",headingTag:"h2"}}),Ul=new T({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}}),Tl=new C({props:{title:"卸载",local:"卸载",headingTag:"h2"}}),wl=new C({props:{title:"CPU 卸载",local:"cpu-卸载",headingTag:"h3"}}),xl=new T({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}}),Il=new C({props:{title:"模型卸载",local:"模型卸载",headingTag:"h3"}}),Gl=new T({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}}),kl=new C({props:{title:"组卸载",local:"组卸载",headingTag:"h3"}}),Ql=new T({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YUGlwZWxpbmUlMEFmcm9tJTIwZGlmZnVzZXJzLmhvb2tzJTIwaW1wb3J0JTIwYXBwbHlfZ3JvdXBfb2ZmbG9hZGluZyUwQWZyb20lMjBkaWZmdXNlcnMudXRpbHMlMjBpbXBvcnQlMjBleHBvcnRfdG9fdmlkZW8lMEEl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wQSUyMCUyMCUyMCUyMCUyMlRoZSUyMGJhY2tncm91bmQlMjBpbmNsdWRlcyUyMGElMjBzbWFsbCUyQyUyMGZsb3dpbmclMjBzdHJlYW0lMjBhbmQlMjB2aWJyYW50JTIwZ3JlZW4lMjBmb2xpYWdlJTJDJTIwZW5oYW5jaW5nJTIwdGhlJTIwcGVhY2VmdWwlMjBhbmQlMjBtYWdpY2FsJTIwJTIyJTBBJTIwJTIwJTIwJTIwJTIyYXRtb3NwaGVyZSUyMG9mJTIwdGhpcyUyMHVuaXF1ZSUyMG11c2ljYWwlMjBwZXJmb3JtYW5jZS4lMjIlMEEpJTBBdmlkZW8lMjAlM0QlMjBwaXBlbGluZShwcm9tcHQlM0Rwcm9tcHQlMkMlMjBndWlkYW5jZV9zY2FsZSUzRDYlMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTApLmZyYW1lcyU1QjAlNUQlMEFwcmludChmJTIyTWF4JTIwbWVtb3J5JTIwcmVzZXJ2ZWQlM0ElMjAlN0J0b3JjaC5jdWRhLm1heF9tZW1vcnlfYWxsb2NhdGVkKCklMjAlMkYlMjAxMDI0KiozJTNBLjJmJTdEJTIwR0IlMjIpJTBBZXhwb3J0X3RvX3ZpZGVvKHZpZGVvJTJDJTIwJTIyb3V0cHV0Lm1wNCUyMiUyQyUyMGZwcyUzRDgp",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}}),Yl=new C({props:{title:"CUDA 流",local:"cuda-流",headingTag:"h4"}}),El=new T({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}}),Hl=new C({props:{title:"卸载到磁盘",local:"卸载到磁盘",headingTag:"h4"}}),Ll=new T({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}}),Dl=new C({props:{title:"分层类型转换",local:"分层类型转换",headingTag:"h2"}}),Ol=new T({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YUGlwZWxpbmUlMkMlMjBDb2dWaWRlb1hUcmFuc2Zvcm1lcjNETW9kZWwlMEFmcm9tJTIwZGlmZnVzZXJzLnV0aWxzJTIwaW1wb3J0JTIwZXhwb3J0X3RvX3ZpZGVvJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBDb2dWaWRlb1hUcmFuc2Zvcm1lcjNETW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMlRIVURNJTJGQ29nVmlkZW9YLTViJTIyJTJDJTBBJTIwJTIwJTIwJTIwc3ViZm9sZGVyJTNEJTIydHJhbnNmb3JtZXIlMjIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKSUwQXRyYW5zZm9ybWVyLmVuYWJsZV9sYXllcndpc2VfY2FzdGluZyhzdG9yYWdlX2R0eXBlJTNEdG9yY2guZmxvYXQ4X2U0bTNmbiUyQyUyMGNvbXB1dGVfZHR5cGUlM0R0b3JjaC5iZmxvYXQxNiklMEElMEFwaXBlbGluZSUyMCUzRCUyMENvZ1ZpZGVvWFBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMjJUSFVETSUyRkNvZ1ZpZGVvWC01YiUyMiUyQyUwQSUyMCUyMCUyMCUyMHRyYW5zZm9ybWVyJTNEdHJhbnNmb3JtZXIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKS50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hbmQlMjB2aWJyYW50JTIwZ3JlZW4lMjBmb2xpYWdlJTJDJTIwZW5oYW5jaW5nJTIwdGhlJTIwcGVhY2VmdWwlMjBhbmQlMjBtYWdpY2FsJTIwJTIyJTBBJTIwJTIwJTIwJTIwJTIyYXRtb3NwaGVyZSUyMG9mJTIwdGhpcyUyMHVuaXF1ZSUyMG11c2ljYWwlMjBwZXJmb3JtYW5jZS4lMjIlMEEpJTBBdmlkZW8lMjAlM0QlMjBwaXBlbGluZShwcm9tcHQlM0Rwcm9tcHQlMkMlMjBndWlkYW5jZV9zY2FsZSUzRDYlMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTApLmZyYW1lcyU1QjAlNUQlMEFwcmludChmJTIyTWF4JTIwbWVtb3J5JTIwcmVzZXJ2ZWQlM0ElMjAlN0J0b3JjaC5jdWRhLm1heF9tZW1vcnlfYWxsb2NhdGVkKCklMjAlMkYlMjAxMDI0KiozJTNBLjJmJTdEJTIwR0IlMjIpJTBBZXhwb3J0X3RvX3ZpZGVvKHZpZGVvJTJDJTIwJTIyb3V0cHV0Lm1wNCUyMiUyQyUyMGZwcyUzRDgp",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}}),ss=new T({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}}),es=new C({props:{title:"torch.channels_last",local:"torchchannelslast",headingTag:"h2"}}),ts=new T({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}}),ps=new C({props:{title:"torch.jit.trace",local:"torchjittrace",headingTag:"h2"}}),is=new T({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}}),Ms=new T({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUlMEFmcm9tJTIwZGF0YWNsYXNzZXMlMjBpbXBvcnQlMjBkYXRhY2xhc3MlMEElMEElNDBkYXRhY2xhc3MlMEFjbGFzcyUyMFVOZX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MpJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2FtcGxlJTIwJTNEJTIwdW5ldF90cmFjZWQobGF0ZW50X21vZGVsX2lucHV0JTJDJTIwdCUyQyUyMGVuY29kZXJfaGlkZGVuX3N0YXRlcyklNUIwJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwVU5ldDJEQ29uZGl0aW9uT3V0cHV0KHNhbXBsZSUzRHNhbXBsZSklMEElMEFwaXBlbGluZS51bmV0JTIwJTNEJTIwVHJhY2VkVU5ldCgpJTBBJTBBd2l0aCUyMHRvcmNoLmluZmVyZW5jZV9tb2RlKCklM0ElMEElMjAlMjAlMjAlMjBpbWFnZSUyMCUzRCUyMHBpcGUoJTVCcHJvbXB0JTVEJTIwKiUyMDElMkMlMjBudW1faW5mZXJlbmNlX3N0ZXBzJTNENTApLmltYWdlcyU1QjAlNUQ=",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}}),rs=new C({props:{title:"内存高效注意力",local:"内存高效注意力",headingTag:"h2"}}),us=new T({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}}),bs=new T({props:{code:"cGlwZWxpbmUuZGlzYWJsZV94Zm9ybWVyc19tZW1vcnlfZWZmaWNpZW50X2F0dGVudGlvbigp",highlighted:"pipeline.disable_xformers_memory_efficient_attention()",wrap:!1}}),Us=new Wa({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/optimization/memory.md"}}),{c(){d=p("meta"),x=a(),f=p("p"),U=a(),M(u.$$.fragment),b=a(),M(_.$$.fragment),Z=a(),h=p("p"),h.innerHTML=g,m=a(),w=p("p"),w.textContent=Jn,fs=a(),G=p("blockquote"),G.innerHTML=yn,hs=a(),M(E.$$.fragment),ws=a(),S=p("p"),S.innerHTML=jn,Zs=a(),M(H.$$.fragment),_s=a(),M(A.$$.fragment),Cs=a(),F=p("p"),F.textContent=un,xs=a(),L=p("p"),L.innerHTML=dn,Is=a(),M(q.$$.fragment),gs=a(),D=p("p"),D.textContent=bn,Ws=a(),M(P.$$.fragment),Gs=a(),M(K.$$.fragment),Vs=a(),V=p("blockquote"),V.innerHTML=Un,ks=a(),O=p("p"),O.innerHTML=Tn,Bs=a(),M(k.$$.fragment),vs=a(),ll=p("p"),ll.innerHTML=fn,Ns=a(),sl=p("p"),sl.innerHTML=hn,Xs=a(),M(el.$$.fragment),Qs=a(),nl=p("p"),nl.innerHTML=wn,Ys=a(),M(al.$$.fragment),Rs=a(),tl=p("p"),tl.innerHTML=Zn,zs=a(),M(pl.$$.fragment),$s=a(),cl=p("p"),cl.textContent=_n,Es=a(),il=p("ul"),il.innerHTML=Cn,Ss=a(),ml=p("p"),ml.innerHTML=xn,Hs=a(),M(Ml.$$.fragment),As=a(),M(rl.$$.fragment),Fs=a(),ol=p("p"),ol.textContent=In,Ls=a(),Jl=p("p"),Jl.textContent=gn,qs=a(),yl=p("p"),yl.innerHTML=Wn,Ds=a(),M(jl.$$.fragment),Ps=a(),B=p("blockquote"),B.innerHTML=Gn,Ks=a(),M(ul.$$.fragment),Os=a(),dl=p("p"),dl.textContent=Vn,le=a(),bl=p("p"),bl.innerHTML=kn,se=a(),M(Ul.$$.fragment),ee=a(),v=p("blockquote"),v.innerHTML=Bn,ne=a(),M(Tl.$$.fragment),ae=a(),fl=p("p"),fl.textContent=vn,te=a(),hl=p("p"),hl.innerHTML=Nn,pe=a(),M(wl.$$.fragment),ce=a(),Zl=p("p"),Zl.textContent=Xn,ie=a(),_l=p("p"),_l.textContent=Qn,me=a(),N=p("blockquote"),N.innerHTML=Yn,Me=a(),Cl=p("p"),Cl.innerHTML=Rn,re=a(),M(xl.$$.fragment),oe=a(),M(Il.$$.fragment),Je=a(),gl=p("p"),gl.innerHTML=zn,ye=a(),X=p("blockquote"),X.innerHTML=$n,je=a(),Wl=p("p"),Wl.innerHTML=En,ue=a(),M(Gl.$$.fragment),de=a(),Vl=p("p"),Vl.innerHTML=Sn,be=a(),M(kl.$$.fragment),Ue=a(),Bl=p("p"),Bl.innerHTML=Hn,Te=a(),Q=p("blockquote"),Q.innerHTML=An,fe=a(),vl=p("p"),vl.innerHTML=Fn,he=a(),Nl=p("p"),Nl.innerHTML=Ln,we=a(),Xl=p("ul"),Xl.innerHTML=qn,Ze=a(),M(Ql.$$.fragment),_e=a(),M(Yl.$$.fragment),Ce=a(),Rl=p("p"),Rl.innerHTML=Dn,xe=a(),zl=p("p"),zl.innerHTML=Pn,Ie=a(),Y=p("blockquote"),Y.innerHTML=Kn,ge=a(),$l=p("p"),$l.innerHTML=On,We=a(),M(El.$$.fragment),Ge=a(),Sl=p("p"),Sl.innerHTML=la,Ve=a(),M(Hl.$$.fragment),ke=a(),Al=p("p"),Al.textContent=sa,Be=a(),Fl=p("p"),Fl.innerHTML=ea,ve=a(),M(Ll.$$.fragment),Ne=a(),ql=p("p"),ql.innerHTML=na,Xe=a(),M(Dl.$$.fragment),Qe=a(),R=p("blockquote"),R.innerHTML=aa,Ye=a(),Pl=p("p"),Pl.innerHTML=ta,Re=a(),z=p("blockquote"),z.innerHTML=pa,ze=a(),Kl=p("p"),Kl.innerHTML=ca,$e=a(),M(Ol.$$.fragment),Ee=a(),ls=p("p"),ls.innerHTML=ia,Se=a(),M(ss.$$.fragment),He=a(),M(es.$$.fragment),Ae=a(),ns=p("p"),ns.innerHTML=ma,Fe=a(),as=p("p"),as.textContent=Ma,Le=a(),M(ts.$$.fragment),qe=a(),M(ps.$$.fragment),De=a(),cs=p("p"),cs.innerHTML=ra,Pe=a(),M(is.$$.fragment),Ke=a(),ms=p("p"),ms.textContent=oa,Oe=a(),M(Ms.$$.fragment),ln=a(),M(rs.$$.fragment),sn=a(),$=p("blockquote"),$.innerHTML=Ja,en=a(),os=p("p"),os.textContent=ya,nn=a(),Js=p("p"),Js.innerHTML=ja,an=a(),ys=p("p"),ys.innerHTML=ua,tn=a(),js=p("p"),js.innerHTML=da,pn=a(),M(us.$$.fragment),cn=a(),ds=p("p"),ds.innerHTML=ba,mn=a(),M(bs.$$.fragment),Mn=a(),M(Us.$$.fragment),rn=a(),Ts=p("p"),this.h()},l(l){const s=xa("svelte-u9bgzb",document.head);d=c(s,"META",{name:!0,content:!0}),s.forEach(e),x=t(l),f=c(l,"P",{}),Ta(f).forEach(e),U=t(l),r(u.$$.fragment,l),b=t(l),r(_.$$.fragment,l),Z=t(l),h=c(l,"P",{"data-svelte-h":!0}),i(h)!=="svelte-kg0q1b"&&(h.innerHTML=g),m=t(l),w=c(l,"P",{"data-svelte-h":!0}),i(w)!=="svelte-i3qlfe"&&(w.textContent=Jn),fs=t(l),G=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(G)!=="svelte-1j60o52"&&(G.innerHTML=yn),hs=t(l),r(E.$$.fragment,l),ws=t(l),S=c(l,"P",{"data-svelte-h":!0}),i(S)!=="svelte-13on0j6"&&(S.innerHTML=jn),Zs=t(l),r(H.$$.fragment,l),_s=t(l),r(A.$$.fragment,l),Cs=t(l),F=c(l,"P",{"data-svelte-h":!0}),i(F)!=="svelte-1080qfz"&&(F.textContent=un),xs=t(l),L=c(l,"P",{"data-svelte-h":!0}),i(L)!=="svelte-1ji9awf"&&(L.innerHTML=dn),Is=t(l),r(q.$$.fragment,l),gs=t(l),D=c(l,"P",{"data-svelte-h":!0}),i(D)!=="svelte-1q2d295"&&(D.textContent=bn),Ws=t(l),r(P.$$.fragment,l),Gs=t(l),r(K.$$.fragment,l),Vs=t(l),V=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(V)!=="svelte-1v88325"&&(V.innerHTML=Un),ks=t(l),O=c(l,"P",{"data-svelte-h":!0}),i(O)!=="svelte-1yx7fp"&&(O.innerHTML=Tn),Bs=t(l),r(k.$$.fragment,l),vs=t(l),ll=c(l,"P",{"data-svelte-h":!0}),i(ll)!=="svelte-5mk4n7"&&(ll.innerHTML=fn),Ns=t(l),sl=c(l,"P",{"data-svelte-h":!0}),i(sl)!=="svelte-n6hxwz"&&(sl.innerHTML=hn),Xs=t(l),r(el.$$.fragment,l),Qs=t(l),nl=c(l,"P",{"data-svelte-h":!0}),i(nl)!=="svelte-yo76tu"&&(nl.innerHTML=wn),Ys=t(l),r(al.$$.fragment,l),Rs=t(l),tl=c(l,"P",{"data-svelte-h":!0}),i(tl)!=="svelte-ze8jh9"&&(tl.innerHTML=Zn),zs=t(l),r(pl.$$.fragment,l),$s=t(l),cl=c(l,"P",{"data-svelte-h":!0}),i(cl)!=="svelte-1fbdini"&&(cl.textContent=_n),Es=t(l),il=c(l,"UL",{"data-svelte-h":!0}),i(il)!=="svelte-15h2fme"&&(il.innerHTML=Cn),Ss=t(l),ml=c(l,"P",{"data-svelte-h":!0}),i(ml)!=="svelte-t6jreo"&&(ml.innerHTML=xn),Hs=t(l),r(Ml.$$.fragment,l),As=t(l),r(rl.$$.fragment,l),Fs=t(l),ol=c(l,"P",{"data-svelte-h":!0}),i(ol)!=="svelte-hhjzz5"&&(ol.textContent=In),Ls=t(l),Jl=c(l,"P",{"data-svelte-h":!0}),i(Jl)!=="svelte-azd05b"&&(Jl.textContent=gn),qs=t(l),yl=c(l,"P",{"data-svelte-h":!0}),i(yl)!=="svelte-1jcre3h"&&(yl.innerHTML=Wn),Ds=t(l),r(jl.$$.fragment,l),Ps=t(l),B=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(B)!=="svelte-u9dy4s"&&(B.innerHTML=Gn),Ks=t(l),r(ul.$$.fragment,l),Os=t(l),dl=c(l,"P",{"data-svelte-h":!0}),i(dl)!=="svelte-1n49s7i"&&(dl.textContent=Vn),le=t(l),bl=c(l,"P",{"data-svelte-h":!0}),i(bl)!=="svelte-9l3su6"&&(bl.innerHTML=kn),se=t(l),r(Ul.$$.fragment,l),ee=t(l),v=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(v)!=="svelte-1w22rhs"&&(v.innerHTML=Bn),ne=t(l),r(Tl.$$.fragment,l),ae=t(l),fl=c(l,"P",{"data-svelte-h":!0}),i(fl)!=="svelte-18bl8b2"&&(fl.textContent=vn),te=t(l),hl=c(l,"P",{"data-svelte-h":!0}),i(hl)!=="svelte-1o67dnx"&&(hl.innerHTML=Nn),pe=t(l),r(wl.$$.fragment,l),ce=t(l),Zl=c(l,"P",{"data-svelte-h":!0}),i(Zl)!=="svelte-8sy35u"&&(Zl.textContent=Xn),ie=t(l),_l=c(l,"P",{"data-svelte-h":!0}),i(_l)!=="svelte-19u9hu8"&&(_l.textContent=Qn),me=t(l),N=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(N)!=="svelte-14l2uyw"&&(N.innerHTML=Yn),Me=t(l),Cl=c(l,"P",{"data-svelte-h":!0}),i(Cl)!=="svelte-1u58nwg"&&(Cl.innerHTML=Rn),re=t(l),r(xl.$$.fragment,l),oe=t(l),r(Il.$$.fragment,l),Je=t(l),gl=c(l,"P",{"data-svelte-h":!0}),i(gl)!=="svelte-9x85p5"&&(gl.innerHTML=zn),ye=t(l),X=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(X)!=="svelte-6420if"&&(X.innerHTML=$n),je=t(l),Wl=c(l,"P",{"data-svelte-h":!0}),i(Wl)!=="svelte-1l12c2i"&&(Wl.innerHTML=En),ue=t(l),r(Gl.$$.fragment,l),de=t(l),Vl=c(l,"P",{"data-svelte-h":!0}),i(Vl)!=="svelte-1k1me1b"&&(Vl.innerHTML=Sn),be=t(l),r(kl.$$.fragment,l),Ue=t(l),Bl=c(l,"P",{"data-svelte-h":!0}),i(Bl)!=="svelte-19yi3hk"&&(Bl.innerHTML=Hn),Te=t(l),Q=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(Q)!=="svelte-1ql5wwf"&&(Q.innerHTML=An),fe=t(l),vl=c(l,"P",{"data-svelte-h":!0}),i(vl)!=="svelte-eltwhr"&&(vl.innerHTML=Fn),he=t(l),Nl=c(l,"P",{"data-svelte-h":!0}),i(Nl)!=="svelte-14zzrw4"&&(Nl.innerHTML=Ln),we=t(l),Xl=c(l,"UL",{"data-svelte-h":!0}),i(Xl)!=="svelte-1ray78r"&&(Xl.innerHTML=qn),Ze=t(l),r(Ql.$$.fragment,l),_e=t(l),r(Yl.$$.fragment,l),Ce=t(l),Rl=c(l,"P",{"data-svelte-h":!0}),i(Rl)!=="svelte-pfde8s"&&(Rl.innerHTML=Dn),xe=t(l),zl=c(l,"P",{"data-svelte-h":!0}),i(zl)!=="svelte-1vw5frn"&&(zl.innerHTML=Pn),Ie=t(l),Y=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(Y)!=="svelte-14tpl6d"&&(Y.innerHTML=Kn),ge=t(l),$l=c(l,"P",{"data-svelte-h":!0}),i($l)!=="svelte-w0fj4y"&&($l.innerHTML=On),We=t(l),r(El.$$.fragment,l),Ge=t(l),Sl=c(l,"P",{"data-svelte-h":!0}),i(Sl)!=="svelte-t6xcks"&&(Sl.innerHTML=la),Ve=t(l),r(Hl.$$.fragment,l),ke=t(l),Al=c(l,"P",{"data-svelte-h":!0}),i(Al)!=="svelte-1nq3fej"&&(Al.textContent=sa),Be=t(l),Fl=c(l,"P",{"data-svelte-h":!0}),i(Fl)!=="svelte-1hbe5xk"&&(Fl.innerHTML=ea),ve=t(l),r(Ll.$$.fragment,l),Ne=t(l),ql=c(l,"P",{"data-svelte-h":!0}),i(ql)!=="svelte-17y83c8"&&(ql.innerHTML=na),Xe=t(l),r(Dl.$$.fragment,l),Qe=t(l),R=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(R)!=="svelte-1rchtg8"&&(R.innerHTML=aa),Ye=t(l),Pl=c(l,"P",{"data-svelte-h":!0}),i(Pl)!=="svelte-1f732i8"&&(Pl.innerHTML=ta),Re=t(l),z=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(z)!=="svelte-hsn5v6"&&(z.innerHTML=pa),ze=t(l),Kl=c(l,"P",{"data-svelte-h":!0}),i(Kl)!=="svelte-zead05"&&(Kl.innerHTML=ca),$e=t(l),r(Ol.$$.fragment,l),Ee=t(l),ls=c(l,"P",{"data-svelte-h":!0}),i(ls)!=="svelte-txkzq8"&&(ls.innerHTML=ia),Se=t(l),r(ss.$$.fragment,l),He=t(l),r(es.$$.fragment,l),Ae=t(l),ns=c(l,"P",{"data-svelte-h":!0}),i(ns)!=="svelte-8d4g9i"&&(ns.innerHTML=ma),Fe=t(l),as=c(l,"P",{"data-svelte-h":!0}),i(as)!=="svelte-kbl8lh"&&(as.textContent=Ma),Le=t(l),r(ts.$$.fragment,l),qe=t(l),r(ps.$$.fragment,l),De=t(l),cs=c(l,"P",{"data-svelte-h":!0}),i(cs)!=="svelte-1feyw5u"&&(cs.innerHTML=ra),Pe=t(l),r(is.$$.fragment,l),Ke=t(l),ms=c(l,"P",{"data-svelte-h":!0}),i(ms)!=="svelte-qm13iw"&&(ms.textContent=oa),Oe=t(l),r(Ms.$$.fragment,l),ln=t(l),r(rs.$$.fragment,l),sn=t(l),$=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i($)!=="svelte-mizp64"&&($.innerHTML=Ja),en=t(l),os=c(l,"P",{"data-svelte-h":!0}),i(os)!=="svelte-1m5qcbu"&&(os.textContent=ya),nn=t(l),Js=c(l,"P",{"data-svelte-h":!0}),i(Js)!=="svelte-5jk4ak"&&(Js.innerHTML=ja),an=t(l),ys=c(l,"P",{"data-svelte-h":!0}),i(ys)!=="svelte-1f4dy2o"&&(ys.innerHTML=ua),tn=t(l),js=c(l,"P",{"data-svelte-h":!0}),i(js)!=="svelte-b9wnp4"&&(js.innerHTML=da),pn=t(l),r(us.$$.fragment,l),cn=t(l),ds=c(l,"P",{"data-svelte-h":!0}),i(ds)!=="svelte-go6u4e"&&(ds.innerHTML=ba),mn=t(l),r(bs.$$.fragment,l),Mn=t(l),r(Us.$$.fragment,l),rn=t(l),Ts=c(l,"P",{}),Ta(Ts).forEach(e),this.h()},h(){I(d,"name","hf:doc:metadata"),I(d,"content",Na),I(G,"class","tip"),I(V,"class","warning"),I(B,"class","warning"),I(v,"class","warning"),I(N,"class","warning"),I(X,"class","warning"),I(Q,"class","warning"),I(Y,"class","tip"),I(R,"class","tip"),I(z,"class","warning"),I($,"class","tip")},m(l,s){Ia(document.head,d),n(l,x,s),n(l,f,s),n(l,U,s),o(u,l,s),n(l,b,s),o(_,l,s),n(l,Z,s),n(l,h,s),n(l,m,s),n(l,w,s),n(l,fs,s),n(l,G,s),n(l,hs,s),o(E,l,s),n(l,ws,s),n(l,S,s),n(l,Zs,s),o(H,l,s),n(l,_s,s),o(A,l,s),n(l,Cs,s),n(l,F,s),n(l,xs,s),n(l,L,s),n(l,Is,s),o(q,l,s),n(l,gs,s),n(l,D,s),n(l,Ws,s),o(P,l,s),n(l,Gs,s),o(K,l,s),n(l,Vs,s),n(l,V,s),n(l,ks,s),n(l,O,s),n(l,Bs,s),o(k,l,s),n(l,vs,s),n(l,ll,s),n(l,Ns,s),n(l,sl,s),n(l,Xs,s),o(el,l,s),n(l,Qs,s),n(l,nl,s),n(l,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l,ze,s),n(l,Kl,s),n(l,$e,s),o(Ol,l,s),n(l,Ee,s),n(l,ls,s),n(l,Se,s),o(ss,l,s),n(l,He,s),o(es,l,s),n(l,Ae,s),n(l,ns,s),n(l,Fe,s),n(l,as,s),n(l,Le,s),o(ts,l,s),n(l,qe,s),o(ps,l,s),n(l,De,s),n(l,cs,s),n(l,Pe,s),o(is,l,s),n(l,Ke,s),n(l,ms,s),n(l,Oe,s),o(Ms,l,s),n(l,ln,s),o(rs,l,s),n(l,sn,s),n(l,$,s),n(l,en,s),n(l,os,s),n(l,nn,s),n(l,Js,s),n(l,an,s),n(l,ys,s),n(l,tn,s),n(l,js,s),n(l,pn,s),o(us,l,s),n(l,cn,s),n(l,ds,s),n(l,mn,s),o(bs,l,s),n(l,Mn,s),o(Us,l,s),n(l,rn,s),n(l,Ts,s),on=!0},p(l,[s]){const Ua={};s&2&&(Ua.$$scope={dirty:s,ctx:l}),k.$set(Ua)},i(l){on||(J(u.$$.fragment,l),J(_.$$.fragment,l),J(E.$$.fragment,l),J(H.$$.fragment,l),J(A.$$.fragment,l),J(q.$$.fragment,l),J(P.$$.fragment,l),J(K.$$.fragment,l),J(k.$$.fragment,l),J(el.$$.fragment,l),J(al.$$.fragment,l),J(pl.$$.fragment,l),J(Ml.$$.fragment,l),J(rl.$$.fragment,l),J(jl.$$.fragment,l),J(ul.$$.fragment,l),J(Ul.$$.fragment,l),J(Tl.$$.fragment,l),J(wl.$$.fragment,l),J(xl.$$.fragment,l),J(Il.$$.fragment,l),J(Gl.$$.fragment,l),J(kl.$$.fragment,l),J(Ql.$$.fragment,l),J(Yl.$$.fragment,l),J(El.$$.fragment,l),J(Hl.$$.fragment,l),J(Ll.$$.fragment,l),J(Dl.$$.fragment,l),J(Ol.$$.fragment,l),J(ss.$$.fragment,l),J(es.$$.fragment,l),J(ts.$$.fragment,l),J(ps.$$.fragment,l),J(is.$$.fragment,l),J(Ms.$$.fragment,l),J(rs.$$.fragment,l),J(us.$$.fragment,l),J(bs.$$.fragment,l),J(Us.$$.fragment,l),on=!0)},o(l){y(u.$$.fragment,l),y(_.$$.fragment,l),y(E.$$.fragment,l),y(H.$$.fragment,l),y(A.$$.fragment,l),y(q.$$.fragment,l),y(P.$$.fragment,l),y(K.$$.fragment,l),y(k.$$.fragment,l),y(el.$$.fragment,l),y(al.$$.fragment,l),y(pl.$$.fragment,l),y(Ml.$$.fragment,l),y(rl.$$.fragment,l),y(jl.$$.fragment,l),y(ul.$$.fragment,l),y(Ul.$$.fragment,l),y(Tl.$$.fragment,l),y(wl.$$.fragment,l),y(xl.$$.fragment,l),y(Il.$$.fragment,l),y(Gl.$$.fragment,l),y(kl.$$.fragment,l),y(Ql.$$.fragment,l),y(Yl.$$.fragment,l),y(El.$$.fragment,l),y(Hl.$$.fragment,l),y(Ll.$$.fragment,l),y(Dl.$$.fragment,l),y(Ol.$$.fragment,l),y(ss.$$.fragment,l),y(es.$$.fragment,l),y(ts.$$.fragment,l),y(ps.$$.fragment,l),y(is.$$.fragment,l),y(Ms.$$.fragment,l),y(rs.$$.fragment,l),y(us.$$.fragment,l),y(bs.$$.fragment,l),y(Us.$$.fragment,l),on=!1},d(l){l&&(e(x),e(f),e(U),e(b),e(Z),e(h),e(m),e(w),e(fs),e(G),e(hs),e(ws),e(S),e(Zs),e(_s),e(Cs),e(F),e(xs),e(L),e(Is),e(gs),e(D),e(Ws),e(Gs),e(Vs),e(V),e(ks),e(O),e(Bs),e(vs),e(ll),e(Ns),e(sl),e(Xs),e(Qs),e(nl),e(Ys),e(Rs),e(tl),e(zs),e($s),e(cl),e(Es),e(il),e(Ss),e(ml),e(Hs),e(As),e(Fs),e(ol),e(Ls),e(Jl),e(qs),e(yl),e(Ds),e(Ps),e(B),e(Ks),e(Os),e(dl),e(le),e(bl),e(se),e(ee),e(v),e(ne),e(ae),e(fl),e(te),e(hl),e(pe),e(ce),e(Zl),e(ie),e(_l),e(me),e(N),e(Me),e(Cl),e(re),e(oe),e(Je),e(gl),e(ye),e(X),e(je),e(Wl),e(ue),e(de),e(Vl),e(be),e(Ue),e(Bl),e(Te),e(Q),e(fe),e(vl),e(he),e(Nl),e(we),e(Xl),e(Ze),e(_e),e(Ce),e(Rl),e(xe),e(zl),e(Ie),e(Y),e(ge),e($l),e(We),e(Ge),e(Sl),e(Ve),e(ke),e(Al),e(Be),e(Fl),e(ve),e(Ne),e(ql),e(Xe),e(Qe),e(R),e(Ye),e(Pl),e(Re),e(z),e(ze),e(Kl),e($e),e(Ee),e(ls),e(Se),e(He),e(Ae),e(ns),e(Fe),e(as),e(Le),e(qe),e(De),e(cs),e(Pe),e(Ke),e(ms),e(Oe),e(ln),e(sn),e($),e(en),e(os),e(nn),e(Js),e(an),e(ys),e(tn),e(js),e(pn),e(cn),e(ds),e(mn),e(Mn),e(rn),e(Ts)),e(d),j(u,l),j(_,l),j(E,l),j(H,l),j(A,l),j(q,l),j(P,l),j(K,l),j(k,l),j(el,l),j(al,l),j(pl,l),j(Ml,l),j(rl,l),j(jl,l),j(ul,l),j(Ul,l),j(Tl,l),j(wl,l),j(xl,l),j(Il,l),j(Gl,l),j(kl,l),j(Ql,l),j(Yl,l),j(El,l),j(Hl,l),j(Ll,l),j(Dl,l),j(Ol,l),j(ss,l),j(es,l),j(ts,l),j(ps,l),j(is,l),j(Ms,l),j(rs,l),j(us,l),j(bs,l),j(Us,l)}}}const Na='{"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 Xa(W){return Za(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ea extends _a{constructor(d){super(),Ca(this,d,Xa,va,wa,{})}}export{Ea as component};
