import{s as Ge,o as Ie,n as Be}from"../chunks/scheduler.5c93273d.js";import{S as Ce,i as ge,g as p,s as a,r as o,A as ve,h as c,f as t,c as n,j as ke,u as r,x as i,k as ol,y as We,a as s,v as M,d,t as m,w as J}from"../chunks/index.e43dd92b.js";import{T as $e}from"../chunks/Tip.3538f9e3.js";import{C as u}from"../chunks/CodeBlock.6896320e.js";import{H as le,E as Ee}from"../chunks/getInferenceSnippets.7a494bf3.js";function Re(rl){let w,b='参考这个最小示例 <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(){w=p("p"),w.innerHTML=b},l(y){w=c(y,"P",{"data-svelte-h":!0}),i(w)!=="svelte-1wqzskn"&&(w.innerHTML=b)},m(y,cl){s(y,w,cl)},p:Be,d(y){y&&t(w)}}}function Xe(rl){let w,b,y,cl,U,Ml,Z,ee='在分布式设置中，您可以使用 🤗 <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 上运行推理，这对于并行生成多个提示非常有用。',dl,_,te="本指南将向您展示如何使用 🤗 Accelerate 和 PyTorch Distributed 进行分布式推理。",ml,k,Jl,G,se='🤗 <a href="https://huggingface.co/docs/accelerate/index" rel="nofollow">Accelerate</a> 是一个旨在简化在分布式设置中训练或运行推理的库。它简化了设置分布式环境的过程，让您可以专注于您的 PyTorch 代码。',wl,I,ae="首先，创建一个 Python 文件并初始化一个 <code>accelerate.PartialState</code> 来创建分布式环境；您的设置会自动检测，因此您无需明确定义 <code>rank</code> 或 <code>world_size</code>。将 <code>DiffusionPipeline</code> 移动到 <code>distributed_state.device</code> 以为每个进程分配一个 GPU。",ul,B,ne="现在使用 <code>split_between_processes</code> 实用程序作为上下文管理器，自动在进程数之间分发提示。",yl,C,fl,g,pe="使用 <code>--num_processes</code> 参数指定要使用的 GPU 数量，并调用 <code>accelerate launch</code> 来运行脚本：",jl,v,Tl,f,hl,W,bl,$,ce=`PyTorch 支持 <a href="https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html" rel="nofollow"><code>DistributedDataParallel</code></a>，它启用了数据
并行性。`,Ul,E,ie="首先，创建一个 Python 文件并导入 <code>torch.distributed</code> 和 <code>torch.multiprocessing</code> 来设置分布式进程组，并为每个 GPU 上的推理生成进程。您还应该初始化一个 <code>DiffusionPipeline</code>：",Zl,R,_l,X,oe='您需要创建一个函数来运行推理；<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。',kl,N,re="将 <code>DiffusionPipeline</code> 移动到 <code>rank</code>，并使用 <code>get_rank</code> 为每个进程分配一个 GPU，其中每个进程处理不同的提示：",Gl,V,Il,H,Me='要运行分布式推理，调用 <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> 函数：',Bl,A,Cl,x,de="完成推理脚本后，使用 <code>--nproc_per_node</code> 参数指定要使用的 GPU 数量，并调用 <code>torchrun</code> 来运行脚本：",gl,z,vl,j,me='<p>您可以在 <code>DiffusionPipeline</code> 中使用 <code>device_map</code> 将其模型级组件分布在多个设备上。请参考 <a href="../tutorials/inference_with_big_models#device-placement">设备放置</a> 指南了解更多信息。</p>',Wl,F,$l,Q,Je='现代扩散系统，如 <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 上运行推理可能具有挑战性。',El,Y,we="模型分片是一种技术，当模型无法容纳在单个 GPU 上时，将模型分布在多个 GPU 上。下面的示例假设有两个 16GB GPU 可用于推理。",Rl,S,ue="开始使用文本编码器计算文本嵌入。通过设置 <code>device_map=&quot;balanced&quot;</code> 将文本编码器保持在两个GPU上。<code>balanced</code> 策略将模型均匀分布在所有可用GPU上。使用 <code>max_memory</code> 参数为每个GPU上的每个文本编码器分配最大内存量。",Xl,T,ye="<p><strong>仅</strong> 在此步骤加载文本编码器！扩散变换器和VAE在后续步骤中加载以节省内存。</p>",Nl,L,Vl,P,fe="一旦文本嵌入计算完成，从GPU中移除它们以为扩散变换器腾出空间。",Hl,q,Al,D,je='接下来加载扩散变换器，它有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和硬盘。将模型参数存储在较慢设备上的权衡是推理延迟较慢。',xl,K,zl,h,Te="<p>在任何时候，您可以尝试 <code>print(pipeline.hf_device_map)</code> 来查看各种模型如何在设备上分布。这对于跟踪模型的设备放置很有用。您也可以尝试 <code>print(transformer.hf_device_map)</code> 来查看变换器模型如何在设备上分片。</p>",Fl,O,he="将变换器模型添加到管道中以进行去噪，但将其他模型级组件如文本编码器和VAE设置为 <code>None</code>，因为您还不需要它们。",Ql,ll,Yl,el,be="从内存中移除管道和变换器，因为它们不再需要。",Sl,tl,Ll,sl,Ue="最后，使用变分自编码器（VAE）将潜在表示解码为图像。VAE通常足够小，可以在单个GPU上加载。",Pl,al,ql,nl,Ze="通过选择性加载和卸载在特定阶段所需的模型，并将最大模型分片到多个GPU上，可以在消费级GPU上运行大型模型的推理。",Dl,pl,Kl,il,Ol;return U=new le({props:{title:"分布式推理",local:"分布式推理",headingTag:"h1"}}),k=new le({props:{title:"🤗 Accelerate",local:"-accelerate",headingTag:"h2"}}),C=new u({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}}),v=new u({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHJ1bl9kaXN0cmlidXRlZC5weSUyMC0tbnVtX3Byb2Nlc3NlcyUzRDI=",highlighted:"accelerate launch run_distributed.py --num_processes=2",wrap:!1}}),f=new $e({props:{$$slots:{default:[Re]},$$scope:{ctx:rl}}}),W=new le({props:{title:"PyTorch Distributed",local:"pytorch-distributed",headingTag:"h2"}}),R=new u({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}}),V=new u({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}}),A=new u({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}}),z=new u({props:{code:"dG9yY2hydW4lMjBydW5fZGlzdHJpYnV0ZWQucHklMjAtLW5wcm9jX3Blcl9ub2RlJTNEMg==",highlighted:"torchrun run_distributed.py --nproc_per_node=2",wrap:!1}}),F=new le({props:{title:"模型分片",local:"模型分片",headingTag:"h2"}}),L=new u({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}}),q=new u({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}}),K=new u({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}}),ll=new u({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}}),tl=new u({props:{code:"ZGVsJTIwcGlwZWxpbmUudHJhbnNmb3JtZXIlMEFkZWwlMjBwaXBlbGluZSUwQSUwQWZsdXNoKCk=",highlighted:`<span class="hljs-keyword">del</span> pipeline.transformer
<span class="hljs-keyword">del</span> pipeline

flush()`,wrap:!1}}),al=new u({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}}),pl=new Ee({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/training/distributed_inference.md"}}),{c(){w=p("meta"),b=a(),y=p("p"),cl=a(),o(U.$$.fragment),Ml=a(),Z=p("p"),Z.innerHTML=ee,dl=a(),_=p("p"),_.textContent=te,ml=a(),o(k.$$.fragment),Jl=a(),G=p("p"),G.innerHTML=se,wl=a(),I=p("p"),I.innerHTML=ae,ul=a(),B=p("p"),B.innerHTML=ne,yl=a(),o(C.$$.fragment),fl=a(),g=p("p"),g.innerHTML=pe,jl=a(),o(v.$$.fragment),Tl=a(),o(f.$$.fragment),hl=a(),o(W.$$.fragment),bl=a(),$=p("p"),$.innerHTML=ce,Ul=a(),E=p("p"),E.innerHTML=ie,Zl=a(),o(R.$$.fragment),_l=a(),X=p("p"),X.innerHTML=oe,kl=a(),N=p("p"),N.innerHTML=re,Gl=a(),o(V.$$.fragment),Il=a(),H=p("p"),H.innerHTML=Me,Bl=a(),o(A.$$.fragment),Cl=a(),x=p("p"),x.innerHTML=de,gl=a(),o(z.$$.fragment),vl=a(),j=p("blockquote"),j.innerHTML=me,Wl=a(),o(F.$$.fragment),$l=a(),Q=p("p"),Q.innerHTML=Je,El=a(),Y=p("p"),Y.textContent=we,Rl=a(),S=p("p"),S.innerHTML=ue,Xl=a(),T=p("blockquote"),T.innerHTML=ye,Nl=a(),o(L.$$.fragment),Vl=a(),P=p("p"),P.textContent=fe,Hl=a(),o(q.$$.fragment),Al=a(),D=p("p"),D.innerHTML=je,xl=a(),o(K.$$.fragment),zl=a(),h=p("blockquote"),h.innerHTML=Te,Fl=a(),O=p("p"),O.innerHTML=he,Ql=a(),o(ll.$$.fragment),Yl=a(),el=p("p"),el.textContent=be,Sl=a(),o(tl.$$.fragment),Ll=a(),sl=p("p"),sl.textContent=Ue,Pl=a(),o(al.$$.fragment),ql=a(),nl=p("p"),nl.textContent=Ze,Dl=a(),o(pl.$$.fragment),Kl=a(),il=p("p"),this.h()},l(l){const e=ve("svelte-u9bgzb",document.head);w=c(e,"META",{name:!0,content:!0}),e.forEach(t),b=n(l),y=c(l,"P",{}),ke(y).forEach(t),cl=n(l),r(U.$$.fragment,l),Ml=n(l),Z=c(l,"P",{"data-svelte-h":!0}),i(Z)!=="svelte-164t5yx"&&(Z.innerHTML=ee),dl=n(l),_=c(l,"P",{"data-svelte-h":!0}),i(_)!=="svelte-qajcxl"&&(_.textContent=te),ml=n(l),r(k.$$.fragment,l),Jl=n(l),G=c(l,"P",{"data-svelte-h":!0}),i(G)!=="svelte-10vyc10"&&(G.innerHTML=se),wl=n(l),I=c(l,"P",{"data-svelte-h":!0}),i(I)!=="svelte-k495sb"&&(I.innerHTML=ae),ul=n(l),B=c(l,"P",{"data-svelte-h":!0}),i(B)!=="svelte-1224dwq"&&(B.innerHTML=ne),yl=n(l),r(C.$$.fragment,l),fl=n(l),g=c(l,"P",{"data-svelte-h":!0}),i(g)!=="svelte-1eq5z11"&&(g.innerHTML=pe),jl=n(l),r(v.$$.fragment,l),Tl=n(l),r(f.$$.fragment,l),hl=n(l),r(W.$$.fragment,l),bl=n(l),$=c(l,"P",{"data-svelte-h":!0}),i($)!=="svelte-7gn49b"&&($.innerHTML=ce),Ul=n(l),E=c(l,"P",{"data-svelte-h":!0}),i(E)!=="svelte-1vhpcpe"&&(E.innerHTML=ie),Zl=n(l),r(R.$$.fragment,l),_l=n(l),X=c(l,"P",{"data-svelte-h":!0}),i(X)!=="svelte-109hrfe"&&(X.innerHTML=oe),kl=n(l),N=c(l,"P",{"data-svelte-h":!0}),i(N)!=="svelte-1qcsr3i"&&(N.innerHTML=re),Gl=n(l),r(V.$$.fragment,l),Il=n(l),H=c(l,"P",{"data-svelte-h":!0}),i(H)!=="svelte-1yf0k86"&&(H.innerHTML=Me),Bl=n(l),r(A.$$.fragment,l),Cl=n(l),x=c(l,"P",{"data-svelte-h":!0}),i(x)!=="svelte-1ebzc0p"&&(x.innerHTML=de),gl=n(l),r(z.$$.fragment,l),vl=n(l),j=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(j)!=="svelte-zkfe1n"&&(j.innerHTML=me),Wl=n(l),r(F.$$.fragment,l),$l=n(l),Q=c(l,"P",{"data-svelte-h":!0}),i(Q)!=="svelte-yu6o1n"&&(Q.innerHTML=Je),El=n(l),Y=c(l,"P",{"data-svelte-h":!0}),i(Y)!=="svelte-1wtcutz"&&(Y.textContent=we),Rl=n(l),S=c(l,"P",{"data-svelte-h":!0}),i(S)!=="svelte-vjsvbo"&&(S.innerHTML=ue),Xl=n(l),T=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(T)!=="svelte-zyi0qz"&&(T.innerHTML=ye),Nl=n(l),r(L.$$.fragment,l),Vl=n(l),P=c(l,"P",{"data-svelte-h":!0}),i(P)!=="svelte-101ikwk"&&(P.textContent=fe),Hl=n(l),r(q.$$.fragment,l),Al=n(l),D=c(l,"P",{"data-svelte-h":!0}),i(D)!=="svelte-i97tc3"&&(D.innerHTML=je),xl=n(l),r(K.$$.fragment,l),zl=n(l),h=c(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(h)!=="svelte-1l0lzn4"&&(h.innerHTML=Te),Fl=n(l),O=c(l,"P",{"data-svelte-h":!0}),i(O)!=="svelte-1lgsyuc"&&(O.innerHTML=he),Ql=n(l),r(ll.$$.fragment,l),Yl=n(l),el=c(l,"P",{"data-svelte-h":!0}),i(el)!=="svelte-x0nor"&&(el.textContent=be),Sl=n(l),r(tl.$$.fragment,l),Ll=n(l),sl=c(l,"P",{"data-svelte-h":!0}),i(sl)!=="svelte-1v7ewyt"&&(sl.textContent=Ue),Pl=n(l),r(al.$$.fragment,l),ql=n(l),nl=c(l,"P",{"data-svelte-h":!0}),i(nl)!=="svelte-19rooo6"&&(nl.textContent=Ze),Dl=n(l),r(pl.$$.fragment,l),Kl=n(l),il=c(l,"P",{}),ke(il).forEach(t),this.h()},h(){ol(w,"name","hf:doc:metadata"),ol(w,"content",Ne),ol(j,"class","tip"),ol(T,"class","tip"),ol(h,"class","tip")},m(l,e){We(document.head,w),s(l,b,e),s(l,y,e),s(l,cl,e),M(U,l,e),s(l,Ml,e),s(l,Z,e),s(l,dl,e),s(l,_,e),s(l,ml,e),M(k,l,e),s(l,Jl,e),s(l,G,e),s(l,wl,e),s(l,I,e),s(l,ul,e),s(l,B,e),s(l,yl,e),M(C,l,e),s(l,fl,e),s(l,g,e),s(l,jl,e),M(v,l,e),s(l,Tl,e),M(f,l,e),s(l,hl,e),M(W,l,e),s(l,bl,e),s(l,$,e),s(l,Ul,e),s(l,E,e),s(l,Zl,e),M(R,l,e),s(l,_l,e),s(l,X,e),s(l,kl,e),s(l,N,e),s(l,Gl,e),M(V,l,e),s(l,Il,e),s(l,H,e),s(l,Bl,e),M(A,l,e),s(l,Cl,e),s(l,x,e),s(l,gl,e),M(z,l,e),s(l,vl,e),s(l,j,e),s(l,Wl,e),M(F,l,e),s(l,$l,e),s(l,Q,e),s(l,El,e),s(l,Y,e),s(l,Rl,e),s(l,S,e),s(l,Xl,e),s(l,T,e),s(l,Nl,e),M(L,l,e),s(l,Vl,e),s(l,P,e),s(l,Hl,e),M(q,l,e),s(l,Al,e),s(l,D,e),s(l,xl,e),M(K,l,e),s(l,zl,e),s(l,h,e),s(l,Fl,e),s(l,O,e),s(l,Ql,e),M(ll,l,e),s(l,Yl,e),s(l,el,e),s(l,Sl,e),M(tl,l,e),s(l,Ll,e),s(l,sl,e),s(l,Pl,e),M(al,l,e),s(l,ql,e),s(l,nl,e),s(l,Dl,e),M(pl,l,e),s(l,Kl,e),s(l,il,e),Ol=!0},p(l,[e]){const _e={};e&2&&(_e.$$scope={dirty:e,ctx:l}),f.$set(_e)},i(l){Ol||(d(U.$$.fragment,l),d(k.$$.fragment,l),d(C.$$.fragment,l),d(v.$$.fragment,l),d(f.$$.fragment,l),d(W.$$.fragment,l),d(R.$$.fragment,l),d(V.$$.fragment,l),d(A.$$.fragment,l),d(z.$$.fragment,l),d(F.$$.fragment,l),d(L.$$.fragment,l),d(q.$$.fragment,l),d(K.$$.fragment,l),d(ll.$$.fragment,l),d(tl.$$.fragment,l),d(al.$$.fragment,l),d(pl.$$.fragment,l),Ol=!0)},o(l){m(U.$$.fragment,l),m(k.$$.fragment,l),m(C.$$.fragment,l),m(v.$$.fragment,l),m(f.$$.fragment,l),m(W.$$.fragment,l),m(R.$$.fragment,l),m(V.$$.fragment,l),m(A.$$.fragment,l),m(z.$$.fragment,l),m(F.$$.fragment,l),m(L.$$.fragment,l),m(q.$$.fragment,l),m(K.$$.fragment,l),m(ll.$$.fragment,l),m(tl.$$.fragment,l),m(al.$$.fragment,l),m(pl.$$.fragment,l),Ol=!1},d(l){l&&(t(b),t(y),t(cl),t(Ml),t(Z),t(dl),t(_),t(ml),t(Jl),t(G),t(wl),t(I),t(ul),t(B),t(yl),t(fl),t(g),t(jl),t(Tl),t(hl),t(bl),t($),t(Ul),t(E),t(Zl),t(_l),t(X),t(kl),t(N),t(Gl),t(Il),t(H),t(Bl),t(Cl),t(x),t(gl),t(vl),t(j),t(Wl),t($l),t(Q),t(El),t(Y),t(Rl),t(S),t(Xl),t(T),t(Nl),t(Vl),t(P),t(Hl),t(Al),t(D),t(xl),t(zl),t(h),t(Fl),t(O),t(Ql),t(Yl),t(el),t(Sl),t(Ll),t(sl),t(Pl),t(ql),t(nl),t(Dl),t(Kl),t(il)),t(w),J(U,l),J(k,l),J(C,l),J(v,l),J(f,l),J(W,l),J(R,l),J(V,l),J(A,l),J(z,l),J(F,l),J(L,l),J(q,l),J(K,l),J(ll,l),J(tl,l),J(al,l),J(pl,l)}}}const Ne='{"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 Ve(rl){return Ie(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Qe extends Ce{constructor(w){super(),ge(this,w,Ve,Xe,Ge,{})}}export{Qe as component};
