Training Optimization: Distributed Training
By Ludovico Bessi, Senior ML Engineer at Google ยท Updated
How a model too large for one GPU, trained on sequences too long for one GPU, gets split across many.
Why Distribute
Two limits force training across GPUs:
- Memory. With mixed precision and Adam, each parameter needs about 16 bytes of state. A 70B model needs about 1.1 TB before activations โ fourteen 80 GB GPUs just to hold it.
- Time. Training on trillions of tokens at about 6N FLOPs per token takes millions of GPU-hours. Only parallelism turns that into weeks.
Each parallelism strategy splits a different dimension of the problem. Real training runs combine several ("3D" or "4D" parallelism).
Data Parallelism
Every GPU holds a full copy of the model and processes a different slice of the batch. After the backward pass, gradients are averaged with an all-reduce, and every replica takes the same optimizer step.
Simple and efficient โ but every GPU must fit the whole model, its gradients and its optimizer state.
ZeRO and Fully Sharded Data Parallel
ZeRO (Rajbhandari et al., 2020) removes the redundancy: with N data-parallel GPUs, each one stores only 1/N of the state.
- Stage 1: shard optimizer states.
- Stage 2: also shard gradients.
- Stage 3: also shard the parameters themselves. Each layer's weights are gathered just before use and released afterwards.
PyTorch's FSDP implements the Stage 3 idea. The cost is extra communication (all-gathers of parameters in forward and backward), which overlaps well with compute on fast interconnects.
Tensor Parallelism
Split individual weight matrices across GPUs. In Megatron-LM's scheme, the first FFN matrix is split by columns and the second by rows, so each GPU computes a slice and a single all-reduce combines them. Attention is split by heads.
Tensor parallelism reduces per-GPU memory for weights and activations, but needs an all-reduce inside every layer, so it is used within a node, where GPUs are connected by NVLink. It is also the main way large models are split for inference.
Pipeline Parallelism
Split the model by layers: GPU 1 runs layers 1โ20, GPU 2 runs 21โ40, and so on. Activations flow forward between stages and gradients flow back.
A naive pipeline leaves most GPUs idle (the "bubble"). Splitting each batch into micro-batches (GPipe) and interleaving forward and backward passes (1F1B schedules) keeps stages busy. Pipeline parallelism needs little bandwidth, so it works across nodes.
Sequence and Context Parallelism
Long-context training creates a new problem: activations and attention scale with sequence length, and a single 128K-token sequence may not fit on one GPU even with FlashAttention. Sequence parallelism splits the sequence dimension across GPUs.
The difficulty is attention: every token needs the keys and values of every earlier token, which now live on other GPUs.
Ring Attention
Ring Attention (Liu et al., 2023) arranges GPUs in a ring. Each GPU holds a block of queries and a block of keys and values. In each step, every GPU computes attention between its queries and the KV block it currently holds, then passes that KV block to its neighbor. After a full trip around the ring, every query has seen every key. Communication overlaps with compute, and the online-softmax trick from FlashAttention combines the partial results exactly.
Distributed FlashAttention: LightSeq
LightSeq (Li et al., 2023; later retitled DistFlashAttn) targets long-context training with sequence-level parallelism that is independent of the number of attention heads (unlike Megatron's head-based splitting, which cannot use more GPUs than there are heads). Its main ideas:
- Distributed FlashAttention (DistAttn / DistFlashAttn): a distributed, memory-efficient attention that splits the sequence across workers and exchanges key/value blocks between them, in the spirit of FlashAttention.
- Load balancing for causal attention. With a causal mask, workers holding later tokens have much more work (they attend to everything before them). LightSeq rebalances the work so early workers help late ones.
- Overlapping communication with computation, so exchanging blocks does not stall the GPUs.
- Rematerialization-aware checkpointing: placing gradient checkpoints so that the FlashAttention-style recomputation in the backward pass is not done twice.
Compared with Megatron-LM using FlashAttention, the authors report 1.24โ2.01ร faster training and support for 2โ8ร longer sequences, with the largest gains on models with few heads (including GQA and MQA models).
DeepSpeed-Ulysses takes a different route: it splits the sequence for the non-attention layers, then uses all-to-all communication to re-split by heads for attention, and back again afterwards.
Expert Parallelism
For Mixture-of-Experts models, different experts live on different GPUs. Tokens are routed to the GPU holding their expert with an all-to-all exchange, then routed back. Load balancing across experts is the main challenge, both in training and at serving time.
Choosing a Combination
A typical large training run uses:
- Tensor parallelism inside a node (fast NVLink),
- Pipeline parallelism across a few nodes,
- Sequence/context parallelism for long-context phases,
- Data parallelism with ZeRO/FSDP across everything else.
The guiding principle: put the most communication-heavy split on the fastest links.
Implications for Inference
- Inference uses tensor parallelism to fit large models, usually within one node.
- Pipeline parallelism adds a hop per stage to every token's latency, so it is used mainly when a model does not fit on one node.
- Context parallelism (Ring Attention style) is increasingly used to speed up prefill for very long prompts.
- Expert parallelism is essential for serving large MoE models efficiently.
The training lesson carries over: communication is the tax on every split, so use the smallest degree of parallelism that fits.
Key Takeaways
- Data parallelism splits the batch; ZeRO/FSDP shard optimizer state, gradients and parameters to remove redundancy.
- Tensor parallelism splits matrices and needs fast links; pipeline parallelism splits layers and tolerates slow links.
- Sequence parallelism (Ring Attention, LightSeq, Ulysses) makes long-context training possible by splitting the sequence.
- LightSeq balances causal attention work and overlaps communication, scaling beyond the number of heads.
- At inference time, prefer the minimum parallelism needed: every split adds latency per token.