3.4Distributed training: data, tensor, pipeline & expert parallelism
A frontier model doesn't fit on one GPU, or even a hundred. Here are the four ways to split the work, and what each costs in memory and communication.
Two walls: memory and time
Training a model needs more than its weights in memory. In standard mixed-precisionMixed-precision trainingDoing most math in a low-precision format (FP16, BF16 or FP8) while keeping a high-precision (FP32) master copy of the weights and sensitive operations. training with AdamAdam / AdamWAn optimizer that combines momentum with a per-parameter adaptive step size. AdamW decouples weight decay from the update., every parameter carries:
- 2 bytes: the BF16 (or FP16) working copy of the weight,
- 2 bytes: its BF16 gradient,
- 4 bytes: an FP32 master copy of the weight,
- 4 + 4 bytes: Adam’s two moment estimates in FP32.
That’s 16 bytes per parameter, the breakdown used in the ZeRO paper, before counting activations (the intermediate results saved during the forward pass for use in backpropagation). A 70B-parameter model needs about 1.1 TB just for this state. A top GPU like the H100 has 80 GB. You’d need at least 14 GPUs simply to hold the model, and that’s before any computation.
The second wall is time. Llama 3.1 405B took about FLOPs to train (see scaling laws). Even at a very optimistic useful FLOPs per second per GPU, one GPU would need about 3,000 years. Meta used up to 16,384 H100 GPUs. So the question isn’t whether to split the work, but how.
You could give each crew a full set of blueprints and its own building site, then compare notes at the end of each day (data parallelism). Or have every crew work on the same floor at once, each building one section of each wall, constantly shouting measurements to each other (tensor parallelism). Or run an assembly line where crew 1 pours foundations, crew 2 frames walls and so on, passing each unit along (pipeline parallelism). Or have specialist crews (electricians, plumbers) and route each job to the right specialist (expert parallelism).
The blueprints and tools are the weights and optimizer states; the buildings are training examples; the shouting is communication between GPUs. Every scheme saves something and pays for it with more coordination.
Data parallelism and its sharded cousins
Data parallelismData parallelismEvery GPU holds the whole model and processes a different slice of the batch; gradients are averaged with an all-reduce. (DP) is the simplest: every GPU holds a full replica of the model, takes a different slice of the global batch, and computes gradients on it. Before the optimizer step, GPUs average their gradients with an all-reduce, a collective operation after which every GPU holds the same sum. With a ring all-reduce each GPU sends and receives about twice the gradient size per step, independent of the number of GPUs, and the communication can overlap with the backward pass. DP scales throughput beautifully. But it does nothing for memory: each GPU still needs 16 bytes per parameter.
ZeROZeRO / FSDPData parallelism that shards optimizer states, gradients and (at stage 3) the weights themselves across GPUs, gathering them only when needed. (Rajbhandari et al., 2019) noticed that this replication is wasteful. Every GPU stores the same optimizer states and runs the same update. So shard them:
- ZeRO-1 shards the optimizer states (the 12-byte part): each GPU updates only its 1/N slice of the parameters, then the new weights are all-gathered.
- ZeRO-2 also shards gradients: gradients are reduce-scattered so each GPU only receives the summed gradients for its own slice.
- ZeRO-3 also shards the weights. Before a layer runs, GPUs all-gather its full weights, use them, and discard them. PyTorch’s FSDP (Fully Sharded Data Parallel) implements the same idea.
ZeRO-1 and 2 communicate about as much as plain DP; ZeRO-3 about 1.5× as much, in exchange for dividing all the training state by the number of GPUs.
Rows L1–L4: the part of each layer’s weights a GPU holds. Bottom strip: its share of the global batch (a solid grey bar means it processes the whole batch together with the others). Red links: who talks to whom (a red frame around all GPUs means everyone exchanges with everyone).
Every GPU holds a full copy of everything and sees a different slice of the batch. Simple and fast, but each GPU needs ~16 bytes per parameter.
Try this: set the model to 7B. Plain DP needs 112 GB per GPU, which doesn’t fit. ZeRO-1 brings it to 38.5 GB; ZeRO-3, tensor or pipeline parallelism bring it to 14 GB. Now push to 70B: even full 8-way sharding needs 140 GB per GPU, so you need more GPUs and a combination of strategies.
Tensor parallelism: split the matrices
Tensor parallelismTensor parallelismSplitting individual weight matrices across GPUs so each computes part of every layer; requires very fast interconnects. (TP), popularized by NVIDIA’s Megatron-LM, splits individual weight matrices. Take a Transformer MLP: . Split by columns across GPUs; each GPU computes its own slice of and applies GeLU locally (it’s elementwise, so no communication is needed). Split by rows; each GPU multiplies its slice and produces a partial sum of . One all-reduce adds the partial sums. Attention is split the same way, by heads.
All GPUs process the same tokens at the same moment, so TP divides both weight memory and per-token compute. The price is two all-reduces of activations per layer in the forward pass, and two more in the backward pass, right in the critical path. That’s only fast enough over NVLink-class links inside a single server, so TP is usually limited to the 8 GPUs of one node.
Pipeline parallelism: split the layers
Pipeline parallelismPipeline parallelismGiving each GPU a contiguous block of layers and streaming micro-batches through them like an assembly line. (PP) gives each GPU a contiguous block of layers: GPU 0 has layers 1–10, GPU 1 has layers 11–20, and so on. Communication is cheap: each stage just sends its output activations to the next stage (and gradients back). That works fine across servers.
The catch is idle time. With one batch, stage 2 waits while stage 1 computes, then stage 1 waits while the rest of the pipeline finishes forward and backward. The fix is to cut the batch into micro-batches and stream them through like an assembly line. Startup and wind-down still leave gaps, called the pipeline bubble.
GPipe runs all forward passes, then all backward passes. 1F1B (one-forward-one-backward, from PipeDream and Megatron) starts each micro-batch’s backward pass as soon as possible. In this simple model both have the same bubble, , but 1F1B frees each micro-batch’s activations much sooner: the first stage holds at most micro-batches in flight instead of all . That’s why 1F1B (and refinements such as interleaved stages and DeepSeek’s DualPipe, which also overlaps computation with communication) is the standard.
Expert parallelism: split the experts
In a Mixture-of-Experts model, each token is routed to a few of many experts. Expert parallelismExpert parallelismPlacing different experts of a Mixture-of-Experts layer on different GPUs and sending each token to the GPU holding its chosen expert (an all-to-all exchange). (EP) places different experts on different GPUs. After the router decides, an all-to-all exchange ships every token to the GPU holding its chosen expert, and a second all-to-all brings the results back. Attention and other shared layers use ordinary data parallelism. EP is what makes trillion-parameter MoE models trainable, but the all-to-all traffic is heavy and uneven routing can leave some GPUs overloaded, so load balancing matters.
Choosing a combination
How do engineers pick the degrees for each kind of parallelism? A rough recipe, given a cluster of 8-GPU servers:
- Start from memory. If the model plus optimizer state fits on one GPU with ZeRO-1 or FSDP sharding, plain data parallelism with sharding is simplest and usually fastest. Many models up to tens of billions of parameters are trained this way.
- Add tensor parallelism inside the node when single layers or their activations get too large (very wide models, long sequences). Keep TP at 8 or below so it stays on NVLink.
- Add pipeline parallelism across nodes when the model still doesn’t fit. Pick enough micro-batches to keep the bubble small, which in turn constrains the global batch size.
- Add expert parallelism for MoE models, sized so each GPU holds a manageable number of experts and the all-to-all traffic stays within fast links where possible.
- Fill the remaining GPUs with data parallelism. Total GPUs = DP × TP × PP (with EP and context parallelism carved out of these dimensions).
The constraints pull against each other: the global batch size can’t grow without limit (too large a batch hurts optimization), so more DP means fewer micro-batches for PP, which means a bigger bubble. Finding a good configuration is a real optimization problem, and teams measure the result as MFU (model FLOPs utilization: useful FLOPs achieved divided by the hardware’s peak). Well-tuned large runs typically report something like 35–45% MFU on BF16 hardware; the rest is lost to communication, bubbles, memory traffic and stragglers.
Two more memory tricks complete the toolkit. Activation checkpointing (recomputation) stores only some activations and recomputes the rest during the backward pass, trading about a third more compute for much less memory. And lower precision shrinks everything; that’s the next lesson.
MATH CORNER · optionalMemory and bubble arithmetic
Training state per GPU. Let be the parameter count and the data-parallel degree (bytes, mixed precision with Adam):
| scheme | weights | grads | optimizer (FP32 master + + ) | total |
|---|---|---|---|---|
| DP | ||||
| ZeRO-1 | ||||
| ZeRO-2 | ||||
| ZeRO-3 / FSDP |
Worked example: , . DP: GB. ZeRO-1: GB. ZeRO-2: GB. ZeRO-3: GB. (The ZeRO paper’s example: a 7.5B model needs 120 GB per GPU with plain DP.)
Ring all-reduce. For a message of bytes on GPUs, each GPU sends bytes. With 7B BF16 gradients ( GB) and , that’s about 24.5 GB per GPU per step, which is why the all-reduce is overlapped with the backward pass.
Pipeline bubble. With stages, micro-batches and forward/backward times , an ideal step takes while each stage is busy for , so
: with the bubble is ; with it’s ; with , about . More micro-batches help, but each extra in-flight micro-batch costs activation memory, which is what 1F1B keeps in check.
In code
With PyTorch, FSDP-style sharding is a wrapper around the model (shown here with the fully_shard API from FSDP2), launched with one process per GPU:
# launch: torchrun --nproc_per_node=8 train.py
import torch, torch.distributed as dist
from torch.distributed.fsdp import fully_shard # FSDP2 API (PyTorch ≥ 2.6)
dist.init_process_group("nccl")
torch.cuda.set_device(dist.get_rank() % torch.cuda.device_count())
model = MyTransformer().cuda()
for block in model.blocks: # shard each Transformer block separately,
fully_shard(block) # so only one block's weights are gathered at a time
fully_shard(model)
opt = torch.optim.AdamW(model.parameters(), lr=3e-4)
for batch in loader: # each rank gets a different slice of data
loss = model(batch).loss
loss.backward() # grads are reduce-scattered to their owners
opt.step(); opt.zero_grad() # each rank updates only its own shard
- “Adding GPUs with data parallelism lets you train a bigger model.” Plain DP replicates everything, so the largest trainable model is set by one GPU’s memory. You need sharding (ZeRO/FSDP) or model parallelism to go bigger.
- “Tensor parallelism scales to thousands of GPUs.” Its frequent all-reduces sit in the critical path of every layer, so it’s practical only over very fast links, typically within one 8-GPU node.
- “Pipeline parallelism has no overhead because communication is cheap.” Its cost is idle time (the bubble) and the activation memory of micro-batches in flight, not bandwidth.
- “The model weights are the main memory cost.” In mixed-precision Adam training, the weights’ BF16 copy is just 2 of the ~16 bytes per parameter; optimizer state dominates, and activations can exceed all of it at long sequence lengths.
Check your understanding
Q1.How much memory does the training state of a 13B-parameter model take with plain mixed-precision Adam (no sharding)?
Q2.Which strategy shards the optimizer states but still keeps a full copy of the BF16 weights on every GPU?
Q3.Why is tensor parallelism usually kept within a single server?
Q4.A pipeline has 8 stages. Roughly what bubble fraction do you get with 8 micro-batches?
Q5.What communication pattern does expert parallelism rely on?
Further reading
- paperZeRO: Memory Optimizations Toward Training Trillion Parameter Models — The source of the 16-bytes-per-parameter accounting and the three ZeRO stages.
- paperMegatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism — Column- and row-parallel splitting of Transformer layers.
- paperGPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism — Micro-batched pipeline parallelism.
- paperEfficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM — Combining TP, PP (1F1B, interleaved) and DP at scale.
- blogThe Ultra-Scale Playbook (Hugging Face) — Hugging Face's long, practical guide to every parallelism strategy.
- paperDeepSeek-V3 Technical Report — Section 3 covers DualPipe, expert parallelism and its training infrastructure.