SysDesignAtlas

Data Parallelism & ZeRO

Replicating the model across GPUs, why optimizer state is the memory hog, and how sharding it turns a technique that hits a wall at 1B parameters into one that trains 500B.

HLDHard11 min readaidistributed-trainingzerofsdp
Confidence

#The simple version

Put a full copy of the model on every GPU and give each one a different slice of the training examples. Each computes its gradients — the adjustments it thinks the weights need, based on the examples it saw. Average those across all GPUs, and every copy applies the identical average, so the replicas never drift apart.

That averaging step is the entire cost of data parallelism, and everything below is about making it cheaper.

   GPU 0: full model, batch slice 0 → gradients g₀
   GPU 1: full model, batch slice 1 → gradients g₁
   GPU 2: full model, batch slice 2 → gradients g₂
   GPU 3: full model, batch slice 3 → gradients g₃
                          ↓
            ALL-REDUCE:  ḡ = (g₀+g₁+g₂+g₃)/4
                          ↓
   every GPU applies the same ḡ → replicas stay in sync

In PyTorch this plain version is called DDP (DistributedDataParallel). It's simple, it's the default, and it scales remarkably well — up to the point where the model no longer fits on one GPU. That point arrives much sooner than people expect.

#Why it hits a wall

The wall is not the weights. It's everything else training requires — and most of that is optimizer state. The optimizer doesn't just apply each gradient; it keeps running statistics per parameter (how that weight has been trending, and how erratic it's been) so it can adapt the step size. Adam, the standard choice, keeps two such numbers per parameter, in full precision, which ends up dwarfing the model itself.

#The ZeRO stages

  1. 1 Stage 0 plain DDP — everything replicated · 16P bytes/GPU
  2. 2 Stage 1 shard optimizer state · 4P + 12P/N
  3. 3 Stage 2 + shard gradients · 2P + 14P/N
  4. 4 Stage 3 + shard parameters · 16P/N
Each stage shards more and communicates more. Stage 3 is FSDP.

How to read the formulas: anything still copied on every GPU costs its full size, and anything sharded costs its size divided by N. In stage 1, for example, weights (2P) and gradients (2P) are still copied everywhere — that's the 4P — while the 12P of optimizer state is split N ways.

Stage 3 is the interesting one, because sharding parameters means no GPU holds the full model. Before computing a layer, the GPUs all-gather that layer's parameters; after computing, they discard them.

   ZeRO-3 forward pass, per layer:

     all-gather layer L's parameters from all ranks
     compute layer L
     free the gathered parameters immediately
     → move to layer L+1

   Backward pass: the same, plus a reduce-scatter of gradients.

   Communication per step ≈ 1.5× DDP's all-reduce volume,
   but spread across many small operations rather than one
   large one. That makes it easier to overlap (fetch layer
   L+1 while computing layer L), but harder to make
   efficient, because every small message pays its own
   fixed latency cost.

FSDP (Fully Sharded Data Parallel) is PyTorch's built-in implementation of the same idea. Instead of installing a separate library (ZeRO ships in Microsoft's DeepSpeed), you wrap parts of your model — say, each transformer block — in an FSDP wrapper, and each wrapped block becomes one unit that is gathered and freed together. Same mechanics, different ergonomics, and it's what most new PyTorch code uses.

#The communication cost, honestly

StrategyMemory per GPUCommunication per step
DDP16P2P (one all-reduce)
ZeRO-14P + 12P/N2P
ZeRO-22P + 14P/N2P
ZeRO-316P/N~3P (gathers in fwd + bwd, plus reduce-scatter)

ZeRO-3 costs roughly 1.5× the communication for a ~10× memory reduction at scale. On NVLink that trade is almost free. Across nodes on a slow fabric it can dominate the step time.

Practical rule: use the lowest stage that fits. If ZeRO-2 fits your model, don't pay for stage 3. Many teams reach for FSDP by default and are network-bound for no reason.

#Activation memory, the other half

Everything above is parameter-related. Activations — the intermediate values saved during forward for use in backward — are a separate and often larger term.

Activation checkpointing (also called gradient checkpointing) is the standard answer: don't save intermediate activations; save only the input to each transformer block, and recompute the rest during backward.

   Memory:  O(layers) → O(√layers) or just the block boundaries
   Compute: +33% (one extra forward pass through each block)

   → Almost always worth it. Trading a third more compute for
     a several-fold memory reduction is a good deal when
     memory is what's stopping you from running at all.

Sequence-length scaling is the thing to watch. Activation memory is linear in sequence length (and attention's is quadratic without FlashAttention). Doubling context doubles this term, which is why long-context training needs sequence parallelism — covered in model parallelism.

#Batch size and the correctness question

Data parallelism multiplies the effective batch size: 64 GPUs × 16 per GPU = 1,024 global batch. That's a training dynamics change, not just a throughput change.

  • Learning rate must scale. Linear scaling (LR ∝ batch size) with a warmup period is the standard recipe. Without it, large-batch training diverges or converges worse.
  • Large batches stop paying off, past a point. A bigger batch gives a more accurate gradient, but you take fewer steps through the same data. Up to a critical batch size, that trade is free. Beyond it, each extra example adds little accuracy, so you need more total tokens to reach the same quality — the extra GPUs buy speed but waste data and compute.
  • Gradient accumulation decouples batch size from GPU count: run k micro-batches one after another, add up their gradients, and all-reduce once. This gets you a large effective batch on few GPUs, and it reduces communication frequency by k.

#When data parallelism is not enough

   Decision path:

     Model fits on one GPU with optimizer state?
       → DDP. Simplest, fastest. Stop here.

     Model fits, optimizer state doesn't?
       → ZeRO-1 or 2. Cheap, no extra communication.

     Model weights don't fit on one GPU?
       → ZeRO-3 / FSDP. Works surprisingly far.

     ZeRO-3 is network-bound, or a single LAYER doesn't fit?
       → Tensor parallelism within a node.

     Still too large, or too many nodes for ZeRO-3?
       → Add pipeline parallelism across nodes.

     Sequence length is the constraint?
       → Sequence / context parallelism.

Real frontier training runs combine all of these — "3D parallelism" — with data parallelism as the outermost dimension. That composition is the subject of the next page.

#Check yourself

What is the memory bill for training with Adam?Show answer
~16 bytes per parameter: BF16 weights and gradients (2 + 2), FP32 momentum and variance (4 + 4) and FP32 master weights (4). For 7B that's 112 GB, 75% of it optimizer state.
What insight does ZeRO exploit?Show answer
Every data-parallel GPU holds an identical copy of optimizer state, gradients and parameters. Shard them across the group and gather each piece only when needed.
Give the ZeRO stages and their memory per GPU.Show answer
Stage 1 shards optimizer state (4P + 12P/N), stage 2 adds gradients (2P + 14P/N), stage 3 adds parameters (16P/N) — 7B on 64 GPUs drops from 112 GB to ~1.8 GB.
What does ZeRO-3 cost, and what is the practical rule?Show answer
About 1.5× DDP's communication — all-gathers in forward and backward plus a reduce-scatter — in many small operations. Use the lowest stage that fits.
What is activation checkpointing?Show answer
Save only each block's input and recompute the rest during backward: ~33% more compute for a large memory cut. Activations can exceed parameter memory at long sequence lengths.
How does data parallelism change training dynamics?Show answer
It multiplies the global batch: scale the learning rate (linear scaling with warmup), watch the critical batch size beyond which sample efficiency drops, and use gradient accumulation to decouple batch from GPU count.
Why report MFU instead of GPU count?Show answer
Useful FLOPs ÷ peak is honest and comparable; 60% scaling efficiency on 512 GPUs is 307 GPUs of work. 40–50% MFU is good, under 30% means investigate.
When do you move beyond data parallelism?Show answer
When ZeRO-3 is network-bound or a single layer doesn't fit (add tensor parallelism in-node), too many nodes (add pipeline), or sequence length binds (sequence parallelism).

#What to carry forward

  • Optimizer state is 75% of the training memory bill, not the weights.
  • ZeRO shards the redundant copies: stage 1 optimizer, stage 2 gradients, stage 3 parameters.
  • Use the lowest stage that fits. Stage 3 costs ~1.5× communication for ~10× memory.
  • Activations are a separate, often larger term. Checkpointing trades 33% compute for a large memory win.
  • Large batches change training dynamics — scale the learning rate, watch the critical batch size.
  • Report MFU, not GPU count. 40–50% is good; under 30% means investigate.
00:00
/ 11m