Accelerate documentation

分布式推理

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

分布式推理

分布式推理可以分为两类:

  1. 将整个模型加载到每个 GPU 上,并每次将批次的一部分通过每个 GPU 上的模型副本
  2. 将模型的各个部分加载到每个 GPU 上,并一次处理单个输入

本指南涵盖第一个括号。有关第二个括号,请参阅大模型推理。

自动将批次的各个分块发送到每个已加载的模型

这是内存占用最高的方案,因为它要求每个 GPU 在任意时刻都在内存中保留一份完整的模型副本。

通常,在执行此操作时,用户会将模型发送到特定设备以从 CPU 加载,然后将每个提示词(prompt)移动到不同的设备。

使用 diffusers 库构建的基本流水线可能如下所示:

import torch
import torch.distributed as dist
from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)

随后,基于特定提示执行推理:

def run_inference(rank, world_size):
    dist.init_process_group("nccl", rank=rank, world_size=world_size)
    pipe.to(rank)

    if torch.distributed.get_rank() == 0:
        prompt = "a dog"
    elif torch.distributed.get_rank() == 1:
        prompt = "a cat"

    result = pipe(prompt).images[0]
    result.save(f"result_{rank}.png")

大家会注意到,我们必须检查 rank 才能确定要发送什么 prompt,这可能会略显繁琐。

用户可能还会认为,在使用 Accelerate 时,利用 Accelerator 来为这类任务准备数据加载器,也是一种管理此问题的简便方式。(如需了解更多,请参阅 快速入门 中的相关章节)

它能处理吗?可以。但它是否引入了不必要的额外代码?是的。

借助 Accelerate,我们可以使用 Accelerator.split_between_processes() 上下文管理器(该管理器同样存在于 PartialState 和 AcceleratorState 中)来简化这一流程。 该函数会自动将你传入的任何数据(无论是提示词、一组张量,还是包含先前数据的字典等)拆分到所有进程上(可能会进行填充),以便你立即使用。

让我们使用此上下文管理器重写上述示例:

import torch
from accelerate import PartialState  # Can also be Accelerator or AcceleratorState
from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
distributed_state = PartialState()
pipe.to(distributed_state.device)

# Assume two processes
with distributed_state.split_between_processes(["a dog", "a cat"]) as prompt:
    result = pipe(prompt).images[0]
    result.save(f"result_{distributed_state.process_index}.png")

然后,要启动代码,我们可以使用 Accelerate:

如果您已使用 accelerate config 生成了要使用的配置文件:

accelerate launch distributed_inference.py

如果您有想要使用的特定配置文件:

accelerate launch --config_file my_config.json distributed_inference.py

或者,如果您不想创建任何配置文件,并在两个 GPU 上启动:

注意:您会收到一些关于根据系统推测值的警告。要消除这些警告,您可以运行 accelerate config default,或者通过 accelerate config 创建配置文件。

accelerate launch --num_processes 2 distributed_inference.py

现在,我们只需几行代码就能轻松完成拆分此数据所需的样板代码。

但如果提示词在 GPU 上的分布是奇数怎么办?例如,我们有 3 个提示词,但只有 2 个 GPU?

在上下文管理器下,第一块 GPU 会接收前两个提示,第二块 GPU 接收第三个,从而确保所有提示都被拆分且无需额外开销。

但是,如果我们想对所有 GPU的结果执行某些操作呢?(例如将它们全部收集起来并进行某种后处理) 你可以传入 apply_padding=True,以确保提示词列表被填充到相同的长度,多余的数据将从最后一个样本中获取。这样,所有 GPU 将拥有相同数量的提示词,然后你就可以收集结果了。

这仅在尝试执行某些操作(例如收集结果)时才需要,此时每个设备上的数据长度必须相同。基本的推理过程不需要此操作。

例如:

import torch
from accelerate import PartialState  # Can also be Accelerator or AcceleratorState
from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
distributed_state = PartialState()
pipe.to(distributed_state.device)

# Assume two processes
with distributed_state.split_between_processes(["a dog", "a cat", "a chicken"], apply_padding=True) as prompt:
    result = pipe(prompt).images

在第一块 GPU 上,提示词为 ["a dog", "a cat"],在第二块 GPU 上则为 ["a chicken", "a chicken"]。 请确保丢弃最后一个样本,因为它将是前一个样本的重复。

您可以在此处找到更复杂的示例,例如如何将其与 LLM 配合使用。

Update on GitHub