Skip to content
VoidIndex - Alexander Lee
Go back

Parallelism Strategies in Distributed Computing

Edit page

Training and inference use the same parallelism concepts. Training adds DDP, FSDP and ZeRO, which shard gradients and optimizer states that inference doesn't have.

Overview

flowchart TD
    DC["Distributed computing"] --> T[Training]
    DC --> I[Inference]
    T --> TF["Frameworks: PyTorch, DeepSpeed, Megatron"]
    T --> TS["Strategies: DDP, FSDP, ZeRO, TP, PP, CP, SP, EP"]
    I --> IF["Frameworks: vLLM, TensorRT-LLM, SGLang"]
    I --> IS["Strategies: DP, TP, PP, EP, CP*"]

Whether a strategy is available depends on the framework and its version.

Comparison

ConceptGeneral meaningTrainingInference
DPEach GPU keeps a full copy of the model and processes different data.Different training dataDifferent inference requests
DDPDDP belongs to DP, Each GPU trains on different data and synchronizes the gradients using All-Reduce.CommonNot needed
FSDPShards parameters, gradients and optimizer states across GPUs, and gathers each layer's weights just before use.CommonUsually not used
ZeRORemoves duplicate copies of optimizer states, gradients and parameters across data-parallel GPUs.CommonUsually not used
TPSplit the tensors, distribute the computation to different GPUs.CommonCommon
PPPuts different layers on different GPUs and streams micro-batches through them.CommonCommon
CPSplits a long sequence across GPUs, attention included.AvailableDepends on framework
SPSplits the sequence-dimension activations that TP leaves replicated, such as LayerNorm and dropout.AvailableDepends on framework
EPPuts different MoE experts on different GPUs and routes each token to its experts.CommonCommon

Cost per strategy

Ψ = parameter count, N = degree of that parallelism. Model-state memory assumes mixed-precision Adam (2Ψ fp16 params + 2Ψ fp16 grads + 12Ψ fp32 optimizer = 16Ψ bytes) [2].

StrategySplitsPer-GPU model statesMain communicationTypical placement
DP / DDPBatch16Ψ (full copy)Gradient all-reduce, 2Ψ per stepAny
ZeRO-1Batch + optimizer states4Ψ + 12Ψ/NSame as DPAny
ZeRO-2+ gradients2Ψ + 14Ψ/NSame as DPAny
ZeRO-3 / FSDP+ parameters16Ψ/N3Ψ (1.5× DP)Across nodes
TPWeight matrices inside a layer≈16Ψ/N4 all-reduces per layer (fwd + bwd)Inside a node (NVLink)
SP (Megatron)LayerNorm/dropout activations, sequence dimSame as TPAll-gather + reduce-scatter in place of all-reduce, same bandwidthAlways with TP
PPLayers≈16Ψ/NP2P activations between stagesAcross nodes
CPFull sequence, attention includedUnchangedRing: P2P KV blocks; Ulysses: all-to-allLong context
EPMoE expertsExperts / NAll-to-all dispatch + combineMoE layers

How it works

The usual layout is nested by bandwidth: DP/FSDP replicas across the cluster, PP stages across nodes, and TP + SP inside a node on NVLink. CP and EP reuse ranks inside that grid [5].

DP and DDP

ZeRO and FSDP

TP (tensor parallelism)

SP (sequence parallelism)

PP (pipeline parallelism)

CP (context parallelism)

EP (expert parallelism)

Usage

PyTorch 2D parallelism: TP inside a node, FSDP across nodes [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 GPUs: 8 nodes (dp) x 8 GPUs per node (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 exposes each strategy as a flag [11]. DP size is whatever is left: 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

Numbers

Per-GPU model-state memory (GB) for a 7.5B model under ZeRO, mixed-precision Adam (K = 12), from ZeRO Table 1 [2].

DP degreeDP (baseline)ZeRO-1ZeRO-2ZeRO-3
1120120120120
412052.541.330
1612035.621.67.5
6412031.416.61.88
102412030.115.10.12

For scale: Megatron-LM trained a 1T-parameter GPT on 3072 A100s with TP = 8 and PP = 64, reaching 502 PFLOP/s, 52% of peak [5].

vLLM support

vLLM supports every strategy that splits weights, KV cache or requests. The training-only ones have nothing to shard at inference time.

StrategyvLLMFlag / note
DPYes-dp N. Each rank is a full engine replica. For MoE, DP attention pairs with EP experts [12]
DDPNot applicableNo gradients to sync
ZeRO / FSDPNot applicableNo gradients or optimizer states; TP shards the weights without a per-step all-gather
TPYes-tp N, Megatron-style [13]
PPYes-pp N, usually across nodes
EPYes--enable-expert-parallel. MoE layers use an EP group of size TP × DP [14]
CPYes, two kinds-dcp N shards the decode KV cache along tokens (MLA and GQA). -pcp N splits prefill and is still under development [15]
SPInternal onlySequence-parallel MoE turns on automatically with EP, TP > 1 and DP > 1. The torch.compile SP pass (pass_config.enable_sp) was removed on main on 2026-10-08 [16]
# 2 nodes x 8 GPUs: TP inside a node, PP across nodes
vllm serve $MODEL -tp 8 -pp 2 --distributed-executor-backend ray

# MoE: DP attention + EP experts on 8 GPUs
vllm serve $MODEL -dp 8 --enable-expert-parallel

# MLA model, 1 KV head: TP=8 copies the KV cache 8x; DCP=8 removes the copies
vllm serve deepseek-ai/DeepSeek-R1 -tp 8 -dcp 8

Pitfalls

References

  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
Open-Source RL Frameworks for LLMs: verl, OpenRLHF, TRL, NeMo RL, ROLL, slime, AReaL