Skip to content
VoidIndex - Alexander Lee
Go back

分布式计算中的并行策略

Edit page

训练和推理用的是同一套并行概念。训练多出 DDP、FSDP 和 ZeRO,它们切分的是梯度和优化器状态,而推理没有这些东西。

总览

flowchart TD
    DC["分布式计算"] --> T[训练]
    DC --> I[推理]
    T --> TF["框架:PyTorch、DeepSpeed、Megatron"]
    T --> TS["策略:DDP、FSDP、ZeRO、TP、PP、CP、SP、EP"]
    I --> IF["框架:vLLM、TensorRT-LLM、SGLang"]
    I --> IS["策略:DP、TP、PP、EP、CP*"]

某个策略能不能用,取决于框架和版本。

对比

概念通用含义训练推理
DP每张 GPU 存一份完整模型,处理不同的数据。不同的训练数据不同的推理请求
DDP属于 DP。每张 GPU 用不同数据训练,再用 All-Reduce 同步梯度。常用不需要
FSDP把参数、梯度和优化器状态切分到各 GPU,每层的权重在用之前才聚合。常用一般不用
ZeRO去掉数据并行各 GPU 之间重复存放的优化器状态、梯度和参数。常用一般不用
TP切分张量,把计算分到不同 GPU 上。常用常用
PP不同的层放在不同 GPU 上,micro-batch 像流水线一样流过。常用常用
CP把一条长序列切到多张 GPU 上,attention 也一起切。可用看框架
SP切分 TP 没切、仍在每张卡上重复的序列维激活,比如 LayerNorm 和 dropout。可用看框架
EPMoE 的不同 expert 放在不同 GPU 上,每个 token 路由到它的 expert。常用常用

各策略的代价

Ψ = 参数量,N = 该并行的并行度。模型状态显存按混合精度 Adam 计算(2Ψ fp16 参数 + 2Ψ fp16 梯度 + 12Ψ fp32 优化器 = 16Ψ 字节)[2]。

策略切什么每卡模型状态主要通信典型放置
DP / DDPBatch16Ψ(完整一份)梯度 all-reduce,每步 2Ψ任意
ZeRO-1Batch + 优化器状态4Ψ + 12Ψ/N同 DP任意
ZeRO-2+ 梯度2Ψ + 14Ψ/N同 DP任意
ZeRO-3 / FSDP+ 参数16Ψ/N3Ψ(DP 的 1.5 倍)跨节点
TP层内的权重矩阵≈16Ψ/N每层 4 次 all-reduce(前向 + 反向)节点内(NVLink)
SP(Megatron)LayerNorm/dropout 激活,按序列维同 TP用 all-gather + reduce-scatter 代替 all-reduce,带宽不变总是和 TP 一起
PP层≈16Ψ/Nstage 之间点对点传激活跨节点
CP整条序列,含 attention不变Ring:点对点传 KV 块;Ulysses:all-to-all长上下文
EPMoE expertExpert 数 / Nall-to-all 分发 + 合并MoE 层

原理

常见布局按带宽嵌套:DP/FSDP 副本跨整个集群,PP stage 跨节点,TP + SP 在节点内走 NVLink。CP 和 EP 复用这个网格里的 rank [5]。

DP 和 DDP

ZeRO 和 FSDP

TP(张量并行)

SP(序列并行)

PP(流水线并行)

CP(上下文并行)

EP(专家并行)

用法

PyTorch 二维并行:节点内 TP,跨节点 FSDP [10]。

from torch.distributed.device_mesh import init_device_mesh
from torch.distributed.fsdp import fully_shard
from torch.distributed.tensor.parallel import (
    ColwiseParallel,
    RowwiseParallel,
    parallelize_module,
)

# 64 张 GPU:8 个节点 (dp) x 每节点 8 张 GPU (tp)
mesh = init_device_mesh("cuda", (8, 8), mesh_dim_names=("dp", "tp"))

for block in model.layers:
    parallelize_module(block.feed_forward, mesh["tp"], {
        "w1": ColwiseParallel(),
        "w3": ColwiseParallel(),
        "w2": RowwiseParallel(),
    })
    fully_shard(block, mesh=mesh["dp"])
fully_shard(model, mesh=mesh["dp"])

Megatron-LM 把每种策略做成一个参数 [11]。DP 的大小是剩下的部分:world_size / (TP × PP × CP)。

torchrun --nproc-per-node 8 --nnodes 16 pretrain_gpt.py \
    --tensor-model-parallel-size 8 --sequence-parallel \
    --pipeline-model-parallel-size 4 \
    --context-parallel-size 2 \
    --expert-model-parallel-size 8 \
    --use-distributed-optimizer

数据

7.5B 模型在 ZeRO 下的每卡模型状态显存(GB),混合精度 Adam(K = 12),取自 ZeRO 论文表 1 [2]。

DP 并行度DP(基线)ZeRO-1ZeRO-2ZeRO-3
1120120120120
412052.541.330
1612035.621.67.5
6412031.416.61.88
102412030.115.10.12

规模参考:Megatron-LM 在 3072 张 A100 上用 TP = 8、PP = 64 训练了 1T 参数的 GPT,达到 502 PFLOP/s,为峰值的 52% [5]。

vLLM 支持情况

凡是切分权重、KV cache 或请求的策略,vLLM 都支持。只用于训练的策略,在推理时没有东西可切。

策略vLLM参数 / 说明
DP支持-dp N。每个 rank 是一个完整的 engine 副本。MoE 模型里 DP attention 和 EP expert 配合使用 [12]
DDP不适用没有梯度要同步
ZeRO / FSDP不适用没有梯度和优化器状态;TP 切分权重,不需要每步 all-gather
TP支持-tp N,Megatron 式 [13]
PP支持-pp N,通常跨节点
EP支持--enable-expert-parallel。MoE 层使用大小为 TP × DP 的 EP 组 [14]
CP支持,两种-dcp N 沿 token 切分 decode 阶段的 KV cache(MLA 和 GQA)。-pcp N 切分 prefill,仍在开发中 [15]
SP仅内部使用在 EP、TP > 1 且 DP > 1 时,序列并行 MoE 会自动开启。基于 torch.compile 的 SP pass(pass_config.enable_sp)已于 2026-10-08 从 main 移除 [16]
# 2 个节点 x 8 张 GPU:节点内 TP,跨节点 PP
vllm serve $MODEL -tp 8 -pp 2 --distributed-executor-backend ray

# MoE:8 张 GPU 上 DP attention + EP expert
vllm serve $MODEL -dp 8 --enable-expert-parallel

# MLA 模型只有 1 个 KV head:TP=8 会把 KV cache 复制 8 份;DCP=8 去掉这些副本
vllm serve deepseek-ai/DeepSeek-R1 -tp 8 -dcp 8

注意事项

参考资料

  1. Li et al., PyTorch Distributed: Experiences on Accelerating Data Parallel Training
  2. Rajbhandari et al., ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
  3. Korthikanti et al., Reducing Activation Recomputation in Large Transformer Models
  4. Liu et al., Ring Attention with Blockwise Transformers for Near-Infinite Context
  5. Narayanan et al., Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM
  6. Zhao et al., PyTorch FSDP: Experiences on Scaling Fully Sharded Data Parallel
  7. Shoeybi et al., Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism
  8. Jacobs et al., DeepSpeed Ulysses: System Optimizations for Enabling Training of Extreme Long Sequence Transformer Models
  9. Lepikhin et al., GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding
  10. PyTorch tutorial: Large Scale Transformer Model Training with Tensor Parallel
  11. NVIDIA Megatron-LM repository
  12. vLLM docs: Data Parallel Deployment
  13. vLLM docs: Parallelism and Scaling
  14. vLLM docs: Expert Parallel Deployment
  15. vLLM docs: Context Parallel Deployment
  16. vLLM PR #60517: Remove torch.compile-based sequence parallelism and AsyncTP

Edit page
Share this post:

Previous Post
从 Pascal 到 Blackwell:NVIDIA 数据中心 GPU
Next Post
verl 入门:跑通 GRPO,看懂一步训练