import{s as gl,o as kl,n as ll}from"../chunks/scheduler.5c93273d.js";import{S as Bl,i as vl,g as c,s as a,r as M,A as Cl,h as r,f as t,c as n,j as Gl,u as d,x as o,k as Il,y as Wl,a as s,v as m,d as f,t as w,w as J}from"../chunks/index.e43dd92b.js";import{T as Oe}from"../chunks/Tip.1cbfe904.js";import{C as j}from"../chunks/CodeBlock.6896320e.js";import{H as el,E as El}from"../chunks/getInferenceSnippets.bd93e167.js";function Rl(T){let p,u='参考这个最小示例 <a href="https://gist.github.com/sayakpaul/cfaebd221820d7b43fae638b4dfa01ba" rel="nofollow">脚本</a> 以在多个 GPU 上运行推理。要了解更多信息，请查看 <a href="https://huggingface.co/docs/accelerate/en/usage_guides/distributed_inference#distributed-inference-with-accelerate" rel="nofollow">使用 🤗 Accelerate 进行分布式推理</a> 指南。';return{c(){p=c("p"),p.innerHTML=u},l(i){p=r(i,"P",{"data-svelte-h":!0}),o(p)!=="svelte-1wqzskn"&&(p.innerHTML=u)},m(i,y){s(i,p,y)},p:ll,d(i){i&&t(p)}}}function Xl(T){let p,u='您可以在 <code>DiffusionPipeline</code> 中使用 <code>device_map</code> 将其模型级组件分布在多个设备上。请参考 <a href="../tutorials/inference_with_big_models#device-placement">设备放置</a> 指南了解更多信息。';return{c(){p=c("p"),p.innerHTML=u},l(i){p=r(i,"P",{"data-svelte-h":!0}),o(p)!=="svelte-i2nti4"&&(p.innerHTML=u)},m(i,y){s(i,p,y)},p:ll,d(i){i&&t(p)}}}function Nl(T){let p,u="<strong>仅</strong> 在此步骤加载文本编码器！扩散变换器和VAE在后续步骤中加载以节省内存。";return{c(){p=c("p"),p.innerHTML=u},l(i){p=r(i,"P",{"data-svelte-h":!0}),o(p)!=="svelte-1cfscz8"&&(p.innerHTML=u)},m(i,y){s(i,p,y)},p:ll,d(i){i&&t(p)}}}function Vl(T){let p,u="在任何时候，您可以尝试 <code>print(pipeline.hf_device_map)</code> 来查看各种模型如何在设备上分布。这对于跟踪模型的设备放置很有用。您也可以尝试 <code>print(transformer.hf_device_map)</code> 来查看变换器模型如何在设备上分片。";return{c(){p=c("p"),p.innerHTML=u},l(i){p=r(i,"P",{"data-svelte-h":!0}),o(p)!=="svelte-1aiohbf"&&(p.innerHTML=u)},m(i,y){s(i,p,y)},p:ll,d(i){i&&t(p)}}}function Hl(T){let p,u,i,y,_,oe,$,tl='在分布式设置中，您可以使用 🤗 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 或 <a href="https://pytorch.org/tutorials/beginner/dist_overview.html" rel="nofollow">PyTorch Distributed</a> 在多个 GPU 上运行推理，这对于并行生成多个提示非常有用。',Me,G,sl="本指南将向您展示如何使用 🤗 Accelerate 和 PyTorch Distributed 进行分布式推理。",de,I,me,g,al='🤗 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 是一个旨在简化在分布式设置中训练或运行推理的库。它简化了设置分布式环境的过程，让您可以专注于您的 PyTorch 代码。',fe,k,nl="首先，创建一个 Python 文件并初始化一个 <code>accelerate.PartialState</code> 来创建分布式环境；您的设置会自动检测，因此您无需明确定义 <code>rank</code> 或 <code>world_size</code>。将 <code>DiffusionPipeline</code> 移动到 <code>distributed_state.device</code> 以为每个进程分配一个 GPU。",we,B,pl="现在使用 <code>split_between_processes</code> 实用程序作为上下文管理器，自动在进程数之间分发提示。",Je,v,ue,C,il="使用 <code>--num_processes</code> 参数指定要使用的 GPU 数量，并调用 <code>accelerate launch</code> 来运行脚本：",ye,W,je,h,Te,E,he,R,cl=`PyTorch 支持 <a href="https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html" rel="nofollow"><code>DistributedDataParallel</code></a>，它启用了数据
并行性。`,be,X,rl="首先，创建一个 Python 文件并导入 <code>torch.distributed</code> 和 <code>torch.multiprocessing</code> 来设置分布式进程组，并为每个 GPU 上的推理生成进程。您还应该初始化一个 <code>DiffusionPipeline</code>：",Ue,N,Ze,V,ol='您需要创建一个函数来运行推理；<a href="https://pytorch.org/docs/stable/distributed.html?highlight=init_process_group#torch.distributed.init_process_group" rel="nofollow"><code>init_process_group</code></a> 处理创建一个分布式环境，指定要使用的后端类型、当前进程的 <code>rank</code> 以及参与进程的数量 <code>world_size</code>。如果您在 2 个 GPU 上并行运行推理，那么 <code>world_size</code> 就是 2。',_e,H,Ml="将 <code>DiffusionPipeline</code> 移动到 <code>rank</code>，并使用 <code>get_rank</code> 为每个进程分配一个 GPU，其中每个进程处理不同的提示：",$e,A,Ge,x,dl='要运行分布式推理，调用 <a href="https://pytorch.org/docs/stable/multiprocessing.html#torch.multiprocessing.spawn" rel="nofollow"><code>mp.spawn</code></a> 在 <code>world_size</code> 定义的 GPU 数量上运行 <code>run_inference</code> 函数：',Ie,z,ge,F,ml="完成推理脚本后，使用 <code>--nproc_per_node</code> 参数指定要使用的 GPU 数量，并调用 <code>torchrun</code> 来运行脚本：",ke,Y,Be,b,ve,Q,Ce,S,fl='现代扩散系统，如 <a href="../api/pipelines/flux">Flux</a>，非常大且包含多个模型。例如，<a href="https://hf.co/black-forest-labs/FLUX.1-dev" rel="nofollow">Flux.1-Dev</a> 由两个文本编码器 - <a href="https://hf.co/google/t5-v1_1-xxl" rel="nofollow">T5-XXL</a> 和 <a href="https://hf.co/openai/clip-vit-large-patch14" rel="nofollow">CLIP-L</a> - 一个 <a href="../api/models/flux_transformer">扩散变换器</a>，以及一个 <a href="../api/models/autoencoderkl">VAE</a> 组成。对于如此大的模型，在消费级 GPU 上运行推理可能具有挑战性。',We,P,wl="模型分片是一种技术，当模型无法容纳在单个 GPU 上时，将模型分布在多个 GPU 上。下面的示例假设有两个 16GB GPU 可用于推理。",Ee,L,Jl="开始使用文本编码器计算文本嵌入。通过设置 <code>device_map=&quot;balanced&quot;</code> 将文本编码器保持在两个GPU上。<code>balanced</code> 策略将模型均匀分布在所有可用GPU上。使用 <code>max_memory</code> 参数为每个GPU上的每个文本编码器分配最大内存量。",Re,U,Xe,q,Ne,D,ul="一旦文本嵌入计算完成，从GPU中移除它们以为扩散变换器腾出空间。",Ve,K,He,O,yl='接下来加载扩散变换器，它有125亿参数。这次，设置 <code>device_map=&quot;auto&quot;</code> 以自动将模型分布在两个16GB GPU上。<code>auto</code> 策略由 <a href="https://hf.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 支持，并作为 <a href="https://hf.co/docs/accelerate/concept_guides/big_model_inference" rel="nofollow">大模型推理</a> 功能的一部分可用。它首先将模型分布在最快的设备（GPU）上，然后在需要时移动到较慢的设备如CPU和硬盘。将模型参数存储在较慢设备上的权衡是推理延迟较慢。',Ae,ee,xe,Z,ze,le,jl="将变换器模型添加到管道中以进行去噪，但将其他模型级组件如文本编码器和VAE设置为 <code>None</code>，因为您还不需要它们。",Fe,te,Ye,se,Tl="从内存中移除管道和变换器，因为它们不再需要。",Qe,ae,Se,ne,hl="最后，使用变分自编码器（VAE）将潜在表示解码为图像。VAE通常足够小，可以在单个GPU上加载。",Pe,pe,Le,ie,bl="通过选择性加载和卸载在特定阶段所需的模型，并将最大模型分片到多个GPU上，可以在消费级GPU上运行大型模型的推理。",qe,ce,De,re,Ke;return _=new el({props:{title:"分布式推理",local:"分布式推理",headingTag:"h1"}}),I=new el({props:{title:"🤗 Accelerate",local:"-accelerate",headingTag:"h2"}}),v=new j({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwYWNjZWxlcmF0ZSUyMGltcG9ydCUyMFBhcnRpYWxTdGF0ZSUwQWZyb20lMjBkaWZmdXNlcnMlMjBpbXBvcnQlMjBEaWZmdXNpb25QaXBlbGluZSUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUyQyUyMHRvcmNoX2R0eXBlJTNEdG9yY2guZmxvYXQxNiUyQyUyMHVzZV9zYWZldGVuc29ycyUzRFRydWUlMEEpJTBBZGlzdHJpYnV0ZWRfc3RhdGUlMjAlM0QlMjBQYXJ0aWFsU3RhdGUoKSUwQXBpcGVsaW5lLnRvKGRpc3RyaWJ1dGVkX3N0YXRlLmRldmljZSklMEElMEF3aXRoJTIwZGlzdHJpYnV0ZWRfc3RhdGUuc3BsaXRfYmV0d2Vlbl9wcm9jZXNzZXMoJTVCJTIyYSUyMGRvZyUyMiUyQyUyMCUyMmElMjBjYXQlMjIlNUQpJTIwYXMlMjBwcm9tcHQlM0ElMEElMjAlMjAlMjAlMjByZXN1bHQlMjAlM0QlMjBwaXBlbGluZShwcm9tcHQpLmltYWdlcyU1QjAlNUQlMEElMjAlMjAlMjAlMjByZXN1bHQuc2F2ZShmJTIycmVzdWx0XyU3QmRpc3RyaWJ1dGVkX3N0YXRlLnByb2Nlc3NfaW5kZXglN0QucG5nJTIyKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> PartialState
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipeline = DiffusionPipeline.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>
)
distributed_state = PartialState()
pipeline.to(distributed_state.device)

<span class="hljs-keyword">with</span> distributed_state.split_between_processes([<span class="hljs-string">&quot;a dog&quot;</span>, <span class="hljs-string">&quot;a cat&quot;</span>]) <span class="hljs-keyword">as</span> prompt:
    result = pipeline(prompt).images[<span class="hljs-number">0</span>]
    result.save(<span class="hljs-string">f&quot;result_<span class="hljs-subst">{distributed_state.process_index}</span>.png&quot;</span>)`,wrap:!1}}),W=new j({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHJ1bl9kaXN0cmlidXRlZC5weSUyMC0tbnVtX3Byb2Nlc3NlcyUzRDI=",highlighted:"accelerate launch run_distributed.py --num_processes=2",wrap:!1}}),h=new Oe({props:{$$slots:{default:[Rl]},$$scope:{ctx:T}}}),E=new el({props:{title:"PyTorch Distributed",local:"pytorch-distributed",headingTag:"h2"}}),N=new j({props:{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjB0b3JjaC5kaXN0cmlidXRlZCUyMGFzJTIwZGlzdCUwQWltcG9ydCUyMHRvcmNoLm11bHRpcHJvY2Vzc2luZyUyMGFzJTIwbXAlMEElMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFzZCUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJzdGFibGUtZGlmZnVzaW9uLXYxLTUlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmZsb2F0MTYlMkMlMjB1c2Vfc2FmZXRlbnNvcnMlM0RUcnVlJTBBKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.distributed <span class="hljs-keyword">as</span> dist
<span class="hljs-keyword">import</span> torch.multiprocessing <span class="hljs-keyword">as</span> mp

<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

sd = DiffusionPipeline.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>
)`,wrap:!1}}),A=new j({props:{code:"ZGVmJTIwcnVuX2luZmVyZW5jZShyYW5rJTJDJTIwd29ybGRfc2l6ZSklM0ElMEElMjAlMjAlMjAlMjBkaXN0LmluaXRfcHJvY2Vzc19ncm91cCglMjJuY2NsJTIyJTJDJTIwcmFuayUzRHJhbmslMkMlMjB3b3JsZF9zaXplJTNEd29ybGRfc2l6ZSklMEElMEElMjAlMjAlMjAlMjBzZC50byhyYW5rKSUwQSUwQSUyMCUyMCUyMCUyMGlmJTIwdG9yY2guZGlzdHJpYnV0ZWQuZ2V0X3JhbmsoKSUyMCUzRCUzRCUyMDAlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9tcHQlMjAlM0QlMjAlMjJhJTIwZG9nJTIyJTBBJTIwJTIwJTIwJTIwZWxpZiUyMHRvcmNoLmRpc3RyaWJ1dGVkLmdldF9yYW5rKCklMjAlM0QlM0QlMjAxJTNBJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcHJvbXB0JTIwJTNEJTIwJTIyYSUyMGNhdCUyMiUwQSUwQSUyMCUyMCUyMCUyMGltYWdlJTIwJTNEJTIwc2QocHJvbXB0KS5pbWFnZXMlNUIwJTVEJTBBJTIwJTIwJTIwJTIwaW1hZ2Uuc2F2ZShmJTIyLiUyRiU3QidfJy5qb2luKHByb21wdCklN0QucG5nJTIyKQ==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">run_inference</span>(<span class="hljs-params">rank, world_size</span>):
    dist.init_process_group(<span class="hljs-string">&quot;nccl&quot;</span>, rank=rank, world_size=world_size)

    sd.to(rank)

    <span class="hljs-keyword">if</span> torch.distributed.get_rank() == <span class="hljs-number">0</span>:
        prompt = <span class="hljs-string">&quot;a dog&quot;</span>
    <span class="hljs-keyword">elif</span> torch.distributed.get_rank() == <span class="hljs-number">1</span>:
        prompt = <span class="hljs-string">&quot;a cat&quot;</span>

    image = sd(prompt).images[<span class="hljs-number">0</span>]
    image.save(<span class="hljs-string">f&quot;./<span class="hljs-subst">{<span class="hljs-string">&#x27;_&#x27;</span>.join(prompt)}</span>.png&quot;</span>)`,wrap:!1}}),z=new j({props:{code:"ZGVmJTIwbWFpbigpJTNBJTBBJTIwJTIwJTIwJTIwd29ybGRfc2l6ZSUyMCUzRCUyMDIlMEElMjAlMjAlMjAlMjBtcC5zcGF3bihydW5faW5mZXJlbmNlJTJDJTIwYXJncyUzRCh3b3JsZF9zaXplJTJDKSUyQyUyMG5wcm9jcyUzRHdvcmxkX3NpemUlMkMlMjBqb2luJTNEVHJ1ZSklMEElMEElMEFpZiUyMF9fbmFtZV9fJTIwJTNEJTNEJTIwJTIyX19tYWluX18lMjIlM0ElMEElMjAlMjAlMjAlMjBtYWluKCk=",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>():
    world_size = <span class="hljs-number">2</span>
    mp.spawn(run_inference, args=(world_size,), nprocs=world_size, join=<span class="hljs-literal">True</span>)


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    main()`,wrap:!1}}),Y=new j({props:{code:"dG9yY2hydW4lMjBydW5fZGlzdHJpYnV0ZWQucHklMjAtLW5wcm9jX3Blcl9ub2RlJTNEMg==",highlighted:"torchrun run_distributed.py --nproc_per_node=2",wrap:!1}}),b=new Oe({props:{warning:!1,$$slots:{default:[Xl]},$$scope:{ctx:T}}}),Q=new el({props:{title:"模型分片",local:"模型分片",headingTag:"h2"}}),U=new Oe({props:{warning:!1,$$slots:{default:[Nl]},$$scope:{ctx:T}}}),q=new j({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEZsdXhQaXBlbGluZSUwQWltcG9ydCUyMHRvcmNoJTBBJTBBcHJvbXB0JTIwJTNEJTIwJTIyYSUyMHBob3RvJTIwb2YlMjBhJTIwZG9nJTIwd2l0aCUyMGNhdC1saWtlJTIwbG9vayUyMiUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwRmx1eFBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJibGFjay1mb3Jlc3QtbGFicyUyRkZMVVguMS1kZXYlMjIlMkMlMEElMjAlMjAlMjAlMjB0cmFuc2Zvcm1lciUzRE5vbmUlMkMlMEElMjAlMjAlMjAlMjB2YWUlM0ROb25lJTJDJTBBJTIwJTIwJTIwJTIwZGV2aWNlX21hcCUzRCUyMmJhbGFuY2VkJTIyJTJDJTBBJTIwJTIwJTIwJTIwbWF4X21lbW9yeSUzRCU3QjAlM0ElMjAlMjIxNkdCJTIyJTJDJTIwMSUzQSUyMCUyMjE2R0IlMjIlN0QlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKSUwQXdpdGglMjB0b3JjaC5ub19ncmFkKCklM0ElMEElMjAlMjAlMjAlMjBwcmludCglMjJFbmNvZGluZyUyMHByb21wdHMuJTIyKSUwQSUyMCUyMCUyMCUyMHByb21wdF9lbWJlZHMlMkMlMjBwb29sZWRfcHJvbXB0X2VtYmVkcyUyQyUyMHRleHRfaWRzJTIwJTNEJTIwcGlwZWxpbmUuZW5jb2RlX3Byb21wdCglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcm9tcHQlM0Rwcm9tcHQlMkMlMjBwcm9tcHRfMiUzRE5vbmUlMkMlMjBtYXhfc2VxdWVuY2VfbGVuZ3RoJTNENTEyJTBBJTIwJTIwJTIwJTIwKQ==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline
<span class="hljs-keyword">import</span> torch

prompt = <span class="hljs-string">&quot;a photo of a dog with cat-like look&quot;</span>

pipeline = FluxPipeline.from_pretrained(
    <span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>,
    transformer=<span class="hljs-literal">None</span>,
    vae=<span class="hljs-literal">None</span>,
    device_map=<span class="hljs-string">&quot;balanced&quot;</span>,
    max_memory={<span class="hljs-number">0</span>: <span class="hljs-string">&quot;16GB&quot;</span>, <span class="hljs-number">1</span>: <span class="hljs-string">&quot;16GB&quot;</span>},
    torch_dtype=torch.bfloat16
)
<span class="hljs-keyword">with</span> torch.no_grad():
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Encoding prompts.&quot;</span>)
    prompt_embeds, pooled_prompt_embeds, text_ids = pipeline.encode_prompt(
        prompt=prompt, prompt_2=<span class="hljs-literal">None</span>, max_sequence_length=<span class="hljs-number">512</span>
    )`,wrap:!1}}),K=new j({props:{code:"aW1wb3J0JTIwZ2MlMjAlMEElMEFkZWYlMjBmbHVzaCgpJTNBJTBBJTIwJTIwJTIwJTIwZ2MuY29sbGVjdCgpJTBBJTIwJTIwJTIwJTIwdG9yY2guY3VkYS5lbXB0eV9jYWNoZSgpJTBBJTIwJTIwJTIwJTIwdG9yY2guY3VkYS5yZXNldF9tYXhfbWVtb3J5X2FsbG9jYXRlZCgpJTBBJTIwJTIwJTIwJTIwdG9yY2guY3VkYS5yZXNldF9wZWFrX21lbW9yeV9zdGF0cygpJTBBJTBBZGVsJTIwcGlwZWxpbmUudGV4dF9lbmNvZGVyJTBBZGVsJTIwcGlwZWxpbmUudGV4dF9lbmNvZGVyXzIlMEFkZWwlMjBwaXBlbGluZS50b2tlbml6ZXIlMEFkZWwlMjBwaXBlbGluZS50b2tlbml6ZXJfMiUwQWRlbCUyMHBpcGVsaW5lJTBBJTBBZmx1c2goKQ==",highlighted:`<span class="hljs-keyword">import</span> gc 

<span class="hljs-keyword">def</span> <span class="hljs-title function_">flush</span>():
    gc.collect()
    torch.cuda.empty_cache()
    torch.cuda.reset_max_memory_allocated()
    torch.cuda.reset_peak_memory_stats()

<span class="hljs-keyword">del</span> pipeline.text_encoder
<span class="hljs-keyword">del</span> pipeline.text_encoder_2
<span class="hljs-keyword">del</span> pipeline.tokenizer
<span class="hljs-keyword">del</span> pipeline.tokenizer_2
<span class="hljs-keyword">del</span> pipeline

flush()`,wrap:!1}}),ee=new j({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEF1dG9Nb2RlbCUwQWltcG9ydCUyMHRvcmNoJTIwJTBBJTBBdHJhbnNmb3JtZXIlMjAlM0QlMjBBdXRvTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMmJsYWNrLWZvcmVzdC1sYWJzJTJGRkxVWC4xLWRldiUyMiUyQyUyMCUwQSUyMCUyMCUyMCUyMHN1YmZvbGRlciUzRCUyMnRyYW5zZm9ybWVyJTIyJTJDJTBBJTIwJTIwJTIwJTIwZGV2aWNlX21hcCUzRCUyMmF1dG8lMjIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel
<span class="hljs-keyword">import</span> torch 

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}}),Z=new Oe({props:{warning:!1,$$slots:{default:[Vl]},$$scope:{ctx:T}}}),te=new j({props:{code:"cGlwZWxpbmUlMjAlM0QlMjBGbHV4UGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMCUyMmJsYWNrLWZvcmVzdC1sYWJzJTJGRkxVWC4xLWRldiUyMiUyQyUwQSUyMCUyMCUyMCUyMHRleHRfZW5jb2RlciUzRE5vbmUlMkMlMEElMjAlMjAlMjAlMjB0ZXh0X2VuY29kZXJfMiUzRE5vbmUlMkMlMEElMjAlMjAlMjAlMjB0b2tlbml6ZXIlM0ROb25lJTJDJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVyXzIlM0ROb25lJTJDJTBBJTIwJTIwJTIwJTIwdmFlJTNETm9uZSUyQyUwQSUyMCUyMCUyMCUyMHRyYW5zZm9ybWVyJTNEdHJhbnNmb3JtZXIlMkMlMEElMjAlMjAlMjAlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTBBKSUwQSUwQXByaW50KCUyMlJ1bm5pbmclMjBkZW5vaXNpbmcuJTIyKSUwQWhlaWdodCUyQyUyMHdpZHRoJTIwJTNEJTIwNzY4JTJDJTIwMTM2MCUwQWxhdGVudHMlMjAlM0QlMjBwaXBlbGluZSglMEElMjAlMjAlMjAlMEElMjAlMjAlMjAlMjAlMjAlMEFwcm9tcHRfZW1iZWRzJTNEcHJvbXB0X2VtYmVkcyUyQyUwQXBvb2xlZF9wcm9tcHRfZW1iZWRzJTNEcG9vbGVkX3Byb21wdF9lbWJlZHMlMkMlMEFudW1faW5mZXJlbmNlX3N0ZXBzJTNENTAlMkMlMEFndWlkYW5jZV9zY2FsZSUzRDMuNSUyQyUwQWhlaWdodCUzRGhlaWdodCUyQyUwQXdpZHRoJTNEd2lkdGglMkMlMEFvdXRwdXRfdHlwZSUzRCUyMmxhdGVudCUyMiUyQyUwQSkuaW1hZ2Vz",highlighted:`pipeline = FluxPipeline.from_pretrained(
    <span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>,
    text_encoder=<span class="hljs-literal">None</span>,
    text_encoder_2=<span class="hljs-literal">None</span>,
    tokenizer=<span class="hljs-literal">None</span>,
    tokenizer_2=<span class="hljs-literal">None</span>,
    vae=<span class="hljs-literal">None</span>,
    transformer=transformer,
    torch_dtype=torch.bfloat16
)

<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Running denoising.&quot;</span>)
height, width = <span class="hljs-number">768</span>, <span class="hljs-number">1360</span>
latents = pipeline(
   
     
prompt_embeds=prompt_embeds,
pooled_prompt_embeds=pooled_prompt_embeds,
num_inference_steps=<span class="hljs-number">50</span>,
guidance_scale=<span class="hljs-number">3.5</span>,
height=height,
width=width,
output_type=<span class="hljs-string">&quot;latent&quot;</span>,
).images`,wrap:!1}}),ae=new j({props:{code:"ZGVsJTIwcGlwZWxpbmUudHJhbnNmb3JtZXIlMEFkZWwlMjBwaXBlbGluZSUwQSUwQWZsdXNoKCk=",highlighted:`<span class="hljs-keyword">del</span> pipeline.transformer
<span class="hljs-keyword">del</span> pipeline

flush()`,wrap:!1}}),pe=new j({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEF1dG9lbmNvZGVyS0wlMEFmcm9tJTIwZGlmZnVzZXJzLmltYWdlX3Byb2Nlc3NvciUyMGltcG9ydCUyMFZhZUltYWdlUHJvY2Vzc29yJTBBaW1wb3J0JTIwdG9yY2glMjAlMEElMEF2YWUlMjAlM0QlMjBBdXRvZW5jb2RlcktMLmZyb21fcHJldHJhaW5lZChja3B0X2lkJTJDJTIwc3ViZm9sZGVyJTNEJTIydmFlJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5iZmxvYXQxNikudG8oJTIyY3VkYSUyMiklMEF2YWVfc2NhbGVfZmFjdG9yJTIwJTNEJTIwMiUyMCoqJTIwKGxlbih2YWUuY29uZmlnLmJsb2NrX291dF9jaGFubmVscyklMjAtJTIwMSklMEFpbWFnZV9wcm9jZXNzb3IlMjAlM0QlMjBWYWVJbWFnZVByb2Nlc3Nvcih2YWVfc2NhbGVfZmFjdG9yJTNEdmFlX3NjYWxlX2ZhY3RvciklMEElMEF3aXRoJTIwdG9yY2gubm9fZ3JhZCgpJTNBJTBBJTIwJTIwJTIwJTIwcHJpbnQoJTIyJUU4JUJGJTkwJUU4JUExJThDJUU4JUE3JUEzJUU3JUEwJTgxJUU0JUI4JUFEJUUzJTgwJTgyJTIyKSUwQSUyMCUyMCUyMCUyMGxhdGVudHMlMjAlM0QlMjBGbHV4UGlwZWxpbmUuX3VucGFja19sYXRlbnRzKGxhdGVudHMlMkMlMjBoZWlnaHQlMkMlMjB3aWR0aCUyQyUyMHZhZV9zY2FsZV9mYWN0b3IpJTBBJTIwJTIwJTIwJTIwbGF0ZW50cyUyMCUzRCUyMChsYXRlbnRzJTIwJTJGJTIwdmFlLmNvbmZpZy5zY2FsaW5nX2ZhY3RvciklMjAlMkIlMjB2YWUuY29uZmlnLnNoaWZ0X2ZhY3RvciUwQSUwQSUyMCUyMCUyMCUyMGltYWdlJTIwJTNEJTIwdmFlLmRlY29kZShsYXRlbnRzJTJDJTIwcmV0dXJuX2RpY3QlM0RGYWxzZSklNUIwJTVEJTBBJTIwJTIwJTIwJTIwaW1hZ2UlMjAlM0QlMjBpbWFnZV9wcm9jZXNzb3IucG9zdHByb2Nlc3MoaW1hZ2UlMkMlMjBvdXRwdXRfdHlwZSUzRCUyMnBpbCUyMiklMEElMjAlMjAlMjAlMjBpbWFnZSU1QjAlNUQuc2F2ZSglMjJzcGxpdF90cmFuc2Zvcm1lci5wbmclMjIp",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoencoderKL
<span class="hljs-keyword">from</span> diffusers.image_processor <span class="hljs-keyword">import</span> VaeImageProcessor
<span class="hljs-keyword">import</span> torch 

vae = AutoencoderKL.from_pretrained(ckpt_id, subfolder=<span class="hljs-string">&quot;vae&quot;</span>, torch_dtype=torch.bfloat16).to(<span class="hljs-string">&quot;cuda&quot;</span>)
vae_scale_factor = <span class="hljs-number">2</span> ** (<span class="hljs-built_in">len</span>(vae.config.block_out_channels) - <span class="hljs-number">1</span>)
image_processor = VaeImageProcessor(vae_scale_factor=vae_scale_factor)

<span class="hljs-keyword">with</span> torch.no_grad():
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;运行解码中。&quot;</span>)
    latents = FluxPipeline._unpack_latents(latents, height, width, vae_scale_factor)
    latents = (latents / vae.config.scaling_factor) + vae.config.shift_factor

    image = vae.decode(latents, return_dict=<span class="hljs-literal">False</span>)[<span class="hljs-number">0</span>]
    image = image_processor.postprocess(image, output_type=<span class="hljs-string">&quot;pil&quot;</span>)
    image[<span class="hljs-number">0</span>].save(<span class="hljs-string">&quot;split_transformer.png&quot;</span>)`,wrap:!1}}),ce=new El({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/training/distributed_inference.md"}}),{c(){p=c("meta"),u=a(),i=c("p"),y=a(),M(_.$$.fragment),oe=a(),$=c("p"),$.innerHTML=tl,Me=a(),G=c("p"),G.textContent=sl,de=a(),M(I.$$.fragment),me=a(),g=c("p"),g.innerHTML=al,fe=a(),k=c("p"),k.innerHTML=nl,we=a(),B=c("p"),B.innerHTML=pl,Je=a(),M(v.$$.fragment),ue=a(),C=c("p"),C.innerHTML=il,ye=a(),M(W.$$.fragment),je=a(),M(h.$$.fragment),Te=a(),M(E.$$.fragment),he=a(),R=c("p"),R.innerHTML=cl,be=a(),X=c("p"),X.innerHTML=rl,Ue=a(),M(N.$$.fragment),Ze=a(),V=c("p"),V.innerHTML=ol,_e=a(),H=c("p"),H.innerHTML=Ml,$e=a(),M(A.$$.fragment),Ge=a(),x=c("p"),x.innerHTML=dl,Ie=a(),M(z.$$.fragment),ge=a(),F=c("p"),F.innerHTML=ml,ke=a(),M(Y.$$.fragment),Be=a(),M(b.$$.fragment),ve=a(),M(Q.$$.fragment),Ce=a(),S=c("p"),S.innerHTML=fl,We=a(),P=c("p"),P.textContent=wl,Ee=a(),L=c("p"),L.innerHTML=Jl,Re=a(),M(U.$$.fragment),Xe=a(),M(q.$$.fragment),Ne=a(),D=c("p"),D.textContent=ul,Ve=a(),M(K.$$.fragment),He=a(),O=c("p"),O.innerHTML=yl,Ae=a(),M(ee.$$.fragment),xe=a(),M(Z.$$.fragment),ze=a(),le=c("p"),le.innerHTML=jl,Fe=a(),M(te.$$.fragment),Ye=a(),se=c("p"),se.textContent=Tl,Qe=a(),M(ae.$$.fragment),Se=a(),ne=c("p"),ne.textContent=hl,Pe=a(),M(pe.$$.fragment),Le=a(),ie=c("p"),ie.textContent=bl,qe=a(),M(ce.$$.fragment),De=a(),re=c("p"),this.h()},l(e){const l=Cl("svelte-u9bgzb",document.head);p=r(l,"META",{name:!0,content:!0}),l.forEach(t),u=n(e),i=r(e,"P",{}),Gl(i).forEach(t),y=n(e),d(_.$$.fragment,e),oe=n(e),$=r(e,"P",{"data-svelte-h":!0}),o($)!=="svelte-164t5yx"&&($.innerHTML=tl),Me=n(e),G=r(e,"P",{"data-svelte-h":!0}),o(G)!=="svelte-qajcxl"&&(G.textContent=sl),de=n(e),d(I.$$.fragment,e),me=n(e),g=r(e,"P",{"data-svelte-h":!0}),o(g)!=="svelte-10vyc10"&&(g.innerHTML=al),fe=n(e),k=r(e,"P",{"data-svelte-h":!0}),o(k)!=="svelte-k495sb"&&(k.innerHTML=nl),we=n(e),B=r(e,"P",{"data-svelte-h":!0}),o(B)!=="svelte-1224dwq"&&(B.innerHTML=pl),Je=n(e),d(v.$$.fragment,e),ue=n(e),C=r(e,"P",{"data-svelte-h":!0}),o(C)!=="svelte-1eq5z11"&&(C.innerHTML=il),ye=n(e),d(W.$$.fragment,e),je=n(e),d(h.$$.fragment,e),Te=n(e),d(E.$$.fragment,e),he=n(e),R=r(e,"P",{"data-svelte-h":!0}),o(R)!=="svelte-7gn49b"&&(R.innerHTML=cl),be=n(e),X=r(e,"P",{"data-svelte-h":!0}),o(X)!=="svelte-1vhpcpe"&&(X.innerHTML=rl),Ue=n(e),d(N.$$.fragment,e),Ze=n(e),V=r(e,"P",{"data-svelte-h":!0}),o(V)!=="svelte-109hrfe"&&(V.innerHTML=ol),_e=n(e),H=r(e,"P",{"data-svelte-h":!0}),o(H)!=="svelte-1qcsr3i"&&(H.innerHTML=Ml),$e=n(e),d(A.$$.fragment,e),Ge=n(e),x=r(e,"P",{"data-svelte-h":!0}),o(x)!=="svelte-1yf0k86"&&(x.innerHTML=dl),Ie=n(e),d(z.$$.fragment,e),ge=n(e),F=r(e,"P",{"data-svelte-h":!0}),o(F)!=="svelte-1ebzc0p"&&(F.innerHTML=ml),ke=n(e),d(Y.$$.fragment,e),Be=n(e),d(b.$$.fragment,e),ve=n(e),d(Q.$$.fragment,e),Ce=n(e),S=r(e,"P",{"data-svelte-h":!0}),o(S)!=="svelte-yu6o1n"&&(S.innerHTML=fl),We=n(e),P=r(e,"P",{"data-svelte-h":!0}),o(P)!=="svelte-1wtcutz"&&(P.textContent=wl),Ee=n(e),L=r(e,"P",{"data-svelte-h":!0}),o(L)!=="svelte-vjsvbo"&&(L.innerHTML=Jl),Re=n(e),d(U.$$.fragment,e),Xe=n(e),d(q.$$.fragment,e),Ne=n(e),D=r(e,"P",{"data-svelte-h":!0}),o(D)!=="svelte-101ikwk"&&(D.textContent=ul),Ve=n(e),d(K.$$.fragment,e),He=n(e),O=r(e,"P",{"data-svelte-h":!0}),o(O)!=="svelte-i97tc3"&&(O.innerHTML=yl),Ae=n(e),d(ee.$$.fragment,e),xe=n(e),d(Z.$$.fragment,e),ze=n(e),le=r(e,"P",{"data-svelte-h":!0}),o(le)!=="svelte-1lgsyuc"&&(le.innerHTML=jl),Fe=n(e),d(te.$$.fragment,e),Ye=n(e),se=r(e,"P",{"data-svelte-h":!0}),o(se)!=="svelte-x0nor"&&(se.textContent=Tl),Qe=n(e),d(ae.$$.fragment,e),Se=n(e),ne=r(e,"P",{"data-svelte-h":!0}),o(ne)!=="svelte-1v7ewyt"&&(ne.textContent=hl),Pe=n(e),d(pe.$$.fragment,e),Le=n(e),ie=r(e,"P",{"data-svelte-h":!0}),o(ie)!=="svelte-19rooo6"&&(ie.textContent=bl),qe=n(e),d(ce.$$.fragment,e),De=n(e),re=r(e,"P",{}),Gl(re).forEach(t),this.h()},h(){Il(p,"name","hf:doc:metadata"),Il(p,"content",Al)},m(e,l){Wl(document.head,p),s(e,u,l),s(e,i,l),s(e,y,l),m(_,e,l),s(e,oe,l),s(e,$,l),s(e,Me,l),s(e,G,l),s(e,de,l),m(I,e,l),s(e,me,l),s(e,g,l),s(e,fe,l),s(e,k,l),s(e,we,l),s(e,B,l),s(e,Je,l),m(v,e,l),s(e,ue,l),s(e,C,l),s(e,ye,l),m(W,e,l),s(e,je,l),m(h,e,l),s(e,Te,l),m(E,e,l),s(e,he,l),s(e,R,l),s(e,be,l),s(e,X,l),s(e,Ue,l),m(N,e,l),s(e,Ze,l),s(e,V,l),s(e,_e,l),s(e,H,l),s(e,$e,l),m(A,e,l),s(e,Ge,l),s(e,x,l),s(e,Ie,l),m(z,e,l),s(e,ge,l),s(e,F,l),s(e,ke,l),m(Y,e,l),s(e,Be,l),m(b,e,l),s(e,ve,l),m(Q,e,l),s(e,Ce,l),s(e,S,l),s(e,We,l),s(e,P,l),s(e,Ee,l),s(e,L,l),s(e,Re,l),m(U,e,l),s(e,Xe,l),m(q,e,l),s(e,Ne,l),s(e,D,l),s(e,Ve,l),m(K,e,l),s(e,He,l),s(e,O,l),s(e,Ae,l),m(ee,e,l),s(e,xe,l),m(Z,e,l),s(e,ze,l),s(e,le,l),s(e,Fe,l),m(te,e,l),s(e,Ye,l),s(e,se,l),s(e,Qe,l),m(ae,e,l),s(e,Se,l),s(e,ne,l),s(e,Pe,l),m(pe,e,l),s(e,Le,l),s(e,ie,l),s(e,qe,l),m(ce,e,l),s(e,De,l),s(e,re,l),Ke=!0},p(e,[l]){const Ul={};l&2&&(Ul.$$scope={dirty:l,ctx:e}),h.$set(Ul);const Zl={};l&2&&(Zl.$$scope={dirty:l,ctx:e}),b.$set(Zl);const _l={};l&2&&(_l.$$scope={dirty:l,ctx:e}),U.$set(_l);const $l={};l&2&&($l.$$scope={dirty:l,ctx:e}),Z.$set($l)},i(e){Ke||(f(_.$$.fragment,e),f(I.$$.fragment,e),f(v.$$.fragment,e),f(W.$$.fragment,e),f(h.$$.fragment,e),f(E.$$.fragment,e),f(N.$$.fragment,e),f(A.$$.fragment,e),f(z.$$.fragment,e),f(Y.$$.fragment,e),f(b.$$.fragment,e),f(Q.$$.fragment,e),f(U.$$.fragment,e),f(q.$$.fragment,e),f(K.$$.fragment,e),f(ee.$$.fragment,e),f(Z.$$.fragment,e),f(te.$$.fragment,e),f(ae.$$.fragment,e),f(pe.$$.fragment,e),f(ce.$$.fragment,e),Ke=!0)},o(e){w(_.$$.fragment,e),w(I.$$.fragment,e),w(v.$$.fragment,e),w(W.$$.fragment,e),w(h.$$.fragment,e),w(E.$$.fragment,e),w(N.$$.fragment,e),w(A.$$.fragment,e),w(z.$$.fragment,e),w(Y.$$.fragment,e),w(b.$$.fragment,e),w(Q.$$.fragment,e),w(U.$$.fragment,e),w(q.$$.fragment,e),w(K.$$.fragment,e),w(ee.$$.fragment,e),w(Z.$$.fragment,e),w(te.$$.fragment,e),w(ae.$$.fragment,e),w(pe.$$.fragment,e),w(ce.$$.fragment,e),Ke=!1},d(e){e&&(t(u),t(i),t(y),t(oe),t($),t(Me),t(G),t(de),t(me),t(g),t(fe),t(k),t(we),t(B),t(Je),t(ue),t(C),t(ye),t(je),t(Te),t(he),t(R),t(be),t(X),t(Ue),t(Ze),t(V),t(_e),t(H),t($e),t(Ge),t(x),t(Ie),t(ge),t(F),t(ke),t(Be),t(ve),t(Ce),t(S),t(We),t(P),t(Ee),t(L),t(Re),t(Xe),t(Ne),t(D),t(Ve),t(He),t(O),t(Ae),t(xe),t(ze),t(le),t(Fe),t(Ye),t(se),t(Qe),t(Se),t(ne),t(Pe),t(Le),t(ie),t(qe),t(De),t(re)),t(p),J(_,e),J(I,e),J(v,e),J(W,e),J(h,e),J(E,e),J(N,e),J(A,e),J(z,e),J(Y,e),J(b,e),J(Q,e),J(U,e),J(q,e),J(K,e),J(ee,e),J(Z,e),J(te,e),J(ae,e),J(pe,e),J(ce,e)}}}const Al='{"title":"分布式推理","local":"分布式推理","sections":[{"title":"🤗 Accelerate","local":"-accelerate","sections":[],"depth":2},{"title":"PyTorch Distributed","local":"pytorch-distributed","sections":[],"depth":2},{"title":"模型分片","local":"模型分片","sections":[],"depth":2}],"depth":1}';function xl(T){return kl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Pl extends Bl{constructor(p){super(),vl(this,p,xl,Hl,gl,{})}}export{Pl as component};
