Topic 39 of 62 · Training at scale
FSDP, ZeRO, and training memory
HardWhat do ZeRO-1, ZeRO-2, and ZeRO-3 / FSDP shard, and why is peak GPU memory higher than the sharded floor?
1 · Start with the intuition
In standard DDP, every GPU holds an identical copy of the optimizer states, gradients, and weights—wasting (D − 1)/D of your cluster’s memory on duplicates. ZeRO (and PyTorch FSDP) eliminates that redundancy in three stages across D data-parallel ranks: ZeRO-1 shards the optimizer states (cutting the 12 bytes/param of FP32 master weights + Adam moments by D); ZeRO-2 also shards the gradients (using reduce-scatter instead of all-reduce); and ZeRO-3 / FSDP FULL_SHARD also shards the parameters themselves, all-gathering a layer’s weights just before its forward or backward pass and freeing them immediately after.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Shard persistent state
Each rank permanently stores only 1/D of the optimizer state (ZeRO-1), + gradients (ZeRO-2), + parameters (ZeRO-3 / FSDP).
Next, we will look at: All-gather per layer. Move on when this operation makes sense.
Visualizing the behavior
Persistent memory per rank: 1B params, 4 data-parallel ranks
Derived- BF16 weights (2 B/param)
- BF16 gradients (2 B/param)
- FP32 master + Adam m, v (12 B/param)
3 · The governing equation
Classic mixed-precision Adam baseline (unsharded): 2 (W) + 2 (G) + 12 (FP32 master + m + v) = 16P bytes ZeRO-1 per rank: 2P + 2P + 12P/D ZeRO-2 per rank: 2P + (2P + 12P)/D ZeRO-3 / FSDP: 16P / D + activations + transient all-gather window + comm buffers
Notation: P is parameter count and D is the sharding world size. The transient all-gather window equals the full unsharded parameter + gradient size of the active FSDP unit(s) plus any prefetched next unit.
Replacing DDP’s all-reduce (which is a reduce-scatter followed by an all-gather of gradients) with ZeRO-2 keeps the exact same communication volume (2P bytes per step) because the all-gather now happens on forward weights while backward only needs a reduce-scatter. ZeRO-3 / FSDP adds one extra all-gather of the weights during the backward pass, increasing parameter/gradient communication volume to ~3P bytes (1.5× DDP). Crucially, ZeRO/FSDP only shards the parameter-scaled floor—it does not shard activations, which still scale with local microbatch and sequence length on every GPU.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)Worked example · Lay out a 7B training run on 64 GPUs
You have 8 nodes × 8 GPUs (80 GB each, NVLink within a node, slower network between nodes). Train a 7B model at 4,096-token sequences with a target of about 4M tokens per optimizer step.
AdamW · bf16 weights and grads · FP32 master and moments (16 bytes/param) · h = 4096, 32 layers, 32 heads · activation estimate 34·s·b·h bytes per layer with fused attention (Korthikanti et al.)
- 01
Can plain DDP hold the model?
DDP replicates all training state on every GPU. Count it before choosing a strategy: 7e9 × 16 bytes = 112 GB.
112 GB > 80 GB per GPU, so replication is ruled out before activations are even counted.
Next question: Where should the state be sharded? Read the next part of the worked explanation when you are ready; you do not need to guess first.
5 · Common misconception to avoid
Dividing 16P by D and assuming that is your peak GPU memory. Peak memory during backward includes the persistent 16P/D shards PLUS all stored forward activations PLUS the full unsharded weights and gradients of the currently executing (and prefetched) FSDP unit.
In practice · implementation & engineering rule of thumb
On multi-node clusters where model state fits inside one 8-GPU node, use HYBRID_SHARD (ZeRO-3 within each NVLink node, DDP all-reduce across nodes) to keep frequent parameter all-gathers off the slower inter-node network.
# Size per-GPU steady state vs peak FSDP memory before launching: steady_state_gb = (16 * num_params) / sharding_degree / 1e9 single_layer_gb = (4 * params_per_layer) / 1e9 # All-gathered BF16 weights + grads
Go one level deeper
Follow-ups that often come after a correct first answer.
- What sharding costs in communication
- ZeRO-1 and ZeRO-2 keep roughly DDP’s communication volume: a reduce-scatter plus an all-gather equals one all-reduce. ZeRO-3 also all-gathers parameters in forward and again in backward, about 1.5× DDP’s volume according to the ZeRO paper. It relies on prefetching to hide that.
- Hybrid sharding
- FSDP’s HYBRID_SHARD shards within a node and replicates across nodes. The frequent parameter all-gathers stay on fast intra-node links, and only a gradient all-reduce crosses the slower network.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy can wrapping the entire model as a single FSDP unit cause an out-of-memory error even with ZeRO-3 enabled?
Show follow-up reasoning
Because FSDP all-gathers all parameters inside a unit before running it. If the entire model is one unit, the all-gather materializes 100% of the unsharded weights on every GPU at once, defeating parameter sharding. Wrapping at the Transformer block level keeps the transient all-gather window to just one (or two, with prefetching) layers at a time.