Skip to main content
Distributed training

Chapter 5: The ZeRO memory-optimization series

How ZeRO-1/2/3 shard state and what they cost in communication, plus how ZeRO-Offload and Infinity move state onto heterogeneous memory

ZeRO DeepSpeed Memory optimization Offload Communication analysis

About this chapter

ZeRO (Zero Redundancy Optimizer) is the core technique behind DeepSpeed. This chapter starts from “where is the redundancy in training state,” walks stage by stage through how sharding removes it, and looks at each stage’s communication cost and when to use it.


Sections

1. ZeRO’s core insight

  • Redundancy analysis: under DDP every GPU redundantly stores the full optimizer state (12Ψ12\Psi), gradients (2Ψ2\Psi), and parameters (2Ψ2\Psi); on an NN-GPU cluster, (N−1)/N(N-1)/N of that storage is waste
  • Design idea: a “shard–aggregate” pattern—each GPU normally holds only 1N\frac{1}{N}, fetches the full data over the network when needed, then discards it
  • Three-stage progression: from the easiest piece to shard (optimizer state) to the hardest (parameters), cutting redundancy step by step

2. ZeRO-1: shard optimizer state

  • What is sharded: Adam’s FP32 parameter copy + first-order momentum + second-order momentum (12Ψ12\Psi Bytes total)
  • Communication pattern: gradients still need AllReduce (same as DDP); after the parameter update you need AllGather to sync parameters
  • Memory savings: per GPU drops from 16Ψ16\Psi to 4Ψ+12ΨN4\Psi + \frac{12\Psi}{N}
  • Communication volume: same as DDP (2Ψ2\Psi) + an extra AllGather of parameters
  • When it fits: when optimizer state dominates memory (Adam is ~75%); a little extra communication buys a large memory cut

3. ZeRO-2: also shard gradients

  • What is sharded: on top of ZeRO-1, gradients are stored sharded too
  • Communication pattern: backward uses ReduceScatter (instead of AllReduce); each GPU keeps only the aggregated gradient for its own shard
  • Memory savings: per GPU drops to 2Ψ+14ΨN2\Psi + \frac{14\Psi}{N}
  • Communication volume: ReduceScatter = Ψ\Psi (half of AllReduce’s 2Ψ2\Psi, but you lose the AllGather that AllReduce implies)
  • When it fits: medium-to-large models where sharding optimizer state + gradients still leaves parameters fitting on one GPU

4. ZeRO-3: shard parameters too

  • What is sharded: parameters, gradients, and optimizer state are all sharded
  • Communication pattern:
    • Forward: AllGather to rebuild the current layer’s parameters → compute → free
    • Backward: AllGather parameters → compute gradients → ReduceScatter gradients → free parameters
  • Memory savings: per GPU drops to 16ΨN\frac{16\Psi}{N} (ideal linear scaling)
  • Communication volume: 3Ψ3\Psi (forward AllGather Ψ\Psi + backward AllGather Ψ\Psi + ReduceScatter Ψ\Psi), 50% more than DDP
  • When it fits: the parameters themselves do not fit on one GPU, so you must shard

5. ZeRO-Offload and ZeRO-Infinity

  • ZeRO-Offload: offload optimizer state and gradient computation to the CPU; the GPU only does forward/backward
    • When it fits: training large models on few GPUs (1–4)
    • Cost: CPU–GPU transfer bandwidth becomes the bottleneck (PCIe 4.0: ~32 GB/s)
  • ZeRO-Infinity: on top of Offload, also uses NVMe SSD storage
    • When it fits: training extremely large models (trillion parameters) on limited GPUs
    • Key techniques: chunked prefetch, overlap of compute with I/O

6. ZeRO selection guide

StageMemory savingsCommunication costWhen it fits
ZeRO-1optimizer state 12ΨN\frac{12\Psi}{N}close to DDPparameters + gradients fit on one GPU
ZeRO-2+ gradientsslightly above DDPparameters fit on one GPU
ZeRO-3everything50% more than DDPparameters do not fit either
Offloaduses CPU memory+ PCIe transfersfew GPUs, large model

7. How ZeRO relates to FSDP

  • FSDP FULL_SHARD ≈ ZeRO-3, SHARD_GRAD_OP ≈ ZeRO-2
  • ZeRO is the algorithm / paper-level idea; FSDP is the native PyTorch implementation; DeepSpeed is Microsoft’s separate implementation
  • FSDP vs DeepSpeed: prefer FSDP inside the PyTorch ecosystem; use DeepSpeed when you need Offload/Infinity or already have a DeepSpeed config

Learning goals

  • Sketch, for each ZeRO-1/2/3 stage, what is sharded and how communication works
  • Compute per-GPU memory formulas and per-step communication volume for each stage
  • Explain ZeRO-Offload’s CPU offload mechanism and its performance bottleneck
  • Pick the right ZeRO stage from model size and hardware