并行
并行策略通过将计算分布在多个设备上,帮助加速扩散 Transformer,从而实现更快的推理/训练速度。有关更多信息,请参阅分布式推理指南。
ParallelConfig
class diffusers.ParallelConfig
< source >( context_parallel_config: diffusers.models._modeling_parallel.ContextParallelConfig | None = Nonetensor_parallel_config: diffusers.models._modeling_parallel.TensorParallelConfig | None = None_rank: int = None_world_size: int = None_device: device = None_mesh: DeviceMesh = None )
用于应用不同并行策略的配置。
ContextParallelConfig
class diffusers.ContextParallelConfig
< source >( ring_degree: int | None = Noneulysses_degree: int | None = Noneconvert_to_fp32: bool = Truerotate_method: typing.Literal['allgather', 'alltoall'] = 'allgather'mesh: typing.Optional[torch.distributed.device_mesh.DeviceMesh] = Noneulysses_anything: bool = Falsering_anything: bool = False_rank: int = None_world_size: int = None_device: device = None_mesh: DeviceMesh = None_flattened_mesh: DeviceMesh = None_ring_mesh: DeviceMesh = None_ulysses_mesh: DeviceMesh = None_ring_local_rank: int = None_ulysses_local_rank: int = None )
参数
- ring_degree (
int, 可选,默认为1) — 用于 Ring Attention 的设备数量。序列将在设备之间进行拆分。每个设备计算其本地 Q 与按环形顺序依次传递的 KV 块之间的注意力。显存占用更低(每次仅持有 1/N 的 KV),计算与通信可重叠,但需要 N 次迭代才能看到所有 token。最适合显存/带宽受限的长序列场景。在上下文并行区域内用于 ring attention 的设备数量。必须是上下文并行网格中设备总数的约数。 - ulysses_degree (
int,可选,默认为1) — 用于 Ulysses Attention 的设备数量。序列在设备之间进行切分。每个设备计算本地的 QKV,然后通过 all-gather 收集所有 KV 分块,以一次性计算完整的注意力。这种方式内存占用较高(需存储所有 KV),且需要高带宽的 all-to-all 通信,但延迟较低。最适合在互连带宽良好的情况下处理中等长度的序列。 - convert_to_fp32 (
bool,可选,默认为True) — 是否将输出和 LSE 转换为 float32,以确保 ring attention 的数值稳定性。 - rotate_method (
str,可选,默认为"allgather") — 在 ring attention 中用于跨设备轮换 key/value 状态的方法。目前仅支持"allgather"。 - ulysses_anything (
bool,可选,默认为False) — 是否启用“Ulysses Anything”模式。该模式支持无法被ulysses_degree整除的任意序列长度和头数。启用时,ulysses_degree必须大于 1,且ring_degree必须为 1。 - ring_anything (
bool,可选,默认为False) — 是否启用“Ring Anything”模式,该模式支持任意序列长度。启用时,ring_degree必须大于 1,且ulysses_degree必须为 1。 - mesh (
torch.distributed.device_mesh.DeviceMesh, 可选) — 用于上下文并行的自定义设备网格。如果提供,将使用该网格,而不是创建新的网格。当将上下文并行与其他共享同一设备网格的并行策略(例如 FSDP、张量并行)结合使用时,这非常有用。该网格必须同时具有 “ring” 和 “ulysses” 维度。对于未使用的维度,请使用大小 1(例如,仅使用 FSDP 的 ring attention 时,使用mesh_shape=(2, 1, 4)和mesh_dim_names=("ring", "ulysses", "fsdp"))。
用于上下文并行的配置。
diffusers.hooks.apply_context_parallel
< source >( module: Moduleparallel_config: ContextParallelConfigplan: dict[str, dict[str, dict[str | int, diffusers.models._modeling_parallel.ContextParallelInput | list[diffusers.models._modeling_parallel.ContextParallelInput] | tuple[diffusers.models._modeling_parallel.ContextParallelInput, ...]] | diffusers.models._modeling_parallel.ContextParallelOutput | list[diffusers.models._modeling_parallel.ContextParallelOutput] | tuple[diffusers.models._modeling_parallel.ContextParallelOutput, ...]]] )
在模型上应用上下文并行。
TensorParallelConfig
类 diffusers.TensorParallelConfig
< source >( tp_degree: int = 1mesh: typing.Optional[torch.distributed.device_mesh.DeviceMesh] = None_rank: int = None_world_size: int = None_device: device = None_mesh: DeviceMesh = None_tp_degree: int = None )
用于张量并行的配置。
张量并行将权重矩阵(按列和按行)分片到多个设备上。每个设备计算部分结果;在层边界处通过 AllReduce/AllGather 重建完整输出。使用 torch.distributed.tensor.parallelize_module 配合 ColwiseParallel / RowwiseParallel 分片方式。支持的设备类型为 "cuda"、"neuron" 和 "tpu"。
diffusers.hooks.apply_tensor_parallel
< source >( model: Moduleconfig: TensorParallelConfigtp_plan: dictweights_already_sharded: bool = False )
参数
- model (
torch.nn.Module) — 要就地分片的模型。 - config (
TensorParallelConfig) — 张量并行配置。其设备网格必须已通过config.setup(...)完成设置。 - tp_plan (
dict) — 一个将模块名称通配符映射到"colwise"/"rowwise"样式(或其打包变体)的扁平映射,通常是模型的_tp_plan。 - weights_already_sharded (
bool,默认为False) — 计划中的参数是否已经是DTensor分片,例如在通过流式from_pretrained加载之后。如果为True,则仅注册前向钩子。此处显式传入该值而非自动检测,因为如果某个计划中的参数缺失于检查点中,它仍然会是一个 meta tensor,这会导致检测逻辑将一个实际上已部分分片的模型误判为“未分片”。
根据模型的扁平化 _tp_plan 对其应用张量并行。