Unit 2 of 12 · about 5 hours · derive
ZeRO-1, ZeRO-2 and ZeRO-3 partitioning
Each ZeRO stage removes a different replicated category. That distinction predicts both the memory floor and the communication needed to preserve data-parallel updates.
You'll be able to
- Derive per-rank model-state memory for ZeRO-1, ZeRO-2 and ZeRO-3 from ownership, including the unsharded floor.
- Explain which rank updates each shard and how all ranks obtain the next parameter version.
- Distinguish the assignment's whole-parameter optimizer partition from ideal evenly pooled sharding.
Covers
Read
In this order, only the parts listed
0 of 3 done
- The three ZeRO stagesUltra-Scale Playbook - Zero Redundancy Optimizer (ZeRO)
Zero Redundancy Optimizer (ZeRO): ZeRO-1: Partitioning optimizer states; ZeRO-2: Adding gradient partitioning; ZeRO-3: Adding parameter partitioning (FSDP)
Stop before Tensor Parallelism. Read the 'two times faster' aside as an equal-payload ring byte comparison, not a measured latency guarantee.
- Ownership and memory savingsZeRO: Memory Optimizations Toward Training Trillion Parameter Models
arXiv:1910.02054v3, Sections 5.1 to 5.3, Deep Dive into ZeRO-DP; Figure 1 and Table 1
- Optimizer-state sharding contractCS336 Assignment 2 (Spring 2026) - Sections 6, 7 and 8.3
Section 6, problems
optimizer_state_shardingandoptimizer_state_sharding_accounting, printed pp. 37 to 38Implement the CPU correctness part now. For accounting parts (a) and (b), use a CPU tensor ledger first; with two or more GPUs, measure a model that fits your GPUs rather than requiring the handout's xl configuration.
Focus on
- Let , and be total parameter, gradient and optimizer bytes, and the rank count. Ideal even-shard persistent totals are , , and for DDP through ZeRO-3.
- In the paper's layout, , and . The advertised fourfold and eightfold stage-1 and stage-2 savings are large-degree limits, not exact savings at every rank count.
- The assignment retains replicated parameters and gradients, updates only owned whole parameters, then broadcasts them. Its public test supplies matching gradients on both ranks; gradient averaging on disjoint data remains your trainer's responsibility.
- Whole-parameter ownership, per-tensor dim-0 sharding, padding and pooled-byte ceilings are different allocation models. Write down which one your calculation uses.
Common mistakes · 3
- Calling optimizer-state sharding gradient synchronization. Ownership of moments does not itself average different ranks' gradients.
- Assigning tied parameter aliases to different owners and applying the update twice.
- Using the ideal persistent ZeRO-3 total as an OOM prediction without a gather window.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.What memory floor remains as the rank count grows under ZeRO-2?Answer
The full replicated parameter storage. Gradients and optimizer state shrink ideally, but parameters do not.
2.Does ZeRO-3 split a layer's matrix multiplication across the ranks?Answer
No. It splits stored model state; each rank gathers the required weights and computes on its own data. Tensor parallelism splits the layer computation.
3.Why does the assignment need broadcasts after the optimizer step?Answer
Each owner changes only its parameters. Broadcasting those updates restores a consistent full parameter replica for the next forward on every rank.
4.Do equal-payload reduce-scatter and all-gather always cost exactly the same as an all-reduce?Answer
Their total ring send volume equals a bandwidth-optimal ring all-reduce. Different dtypes, launch counts, topology, implementation and overlap can change elapsed time.
Practice
- TensorTonic: Account for ZeRO Memory by Stage
Implement
zero_memory_accounting(parameter_bytes, gradient_bytes, optimizer_bytes, world_size)Free. Public cases cover even four-way sharding, an uneven three-way byte pool and one rank. Follow its pooled-byte ceilings; they are not PyTorch's per-tensor allocation rule.
- CS336 Assignment 2:
optimizer_state_shardingSection 6; implement
get_sharded_optimizerintests/adapters.py, then runpytest tests/test_sharded_optimizer.pyThe two-process CPU Gloo tests compare AdamW final parameters after ten updates, including tied weights. They do not check disjoint-data gradient reduction or memory savings; add those checks in trainlab.
You're done when
You derive each stage's persistent-state ledger, pass both named practice suites and explain what their tests leave unproved.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 8 more in the section
- CS336 Assignment 2 (Spring 2026) - Sections 6, 7 and 8.3Stanford University · primary
Complete
optimizer_state_shardingandoptimizer_state_sharding_accounting(Section 6),fsdpandfsdp_accounting(Section 7) andfsdp_calcs(Section 8.3) with your own code.tests/test_sharded_optimizer.pyandtests/test_fsdp.pyrun on CPU with Gloo. For each variant, predict per-rank state, peak live tensors and collective bytes before testing tiny cases. - Ultra-Scale Playbook - Zero Redundancy Optimizer (ZeRO)Hugging Face · required
Read the ZeRO-1, ZeRO-2 and ZeRO-3 progression. Reproduce the memory diagrams for one transformer layer and annotate every all-gather, reduce-scatter and reshard boundary. Its "two times faster" line holds for bytes moved at equal payload in the bandwidth-bound regime.
- ZeRO: Memory Optimizations Toward Training Trillion Parameter ModelsRajbhandari et al. (SC 2020) · required
Read the memory analysis (model states with optimizer bytes per parameter) and the stage-by-stage savings and communication analysis. Reproduce its per-device memory for one model and degree.
Also in this section
- PyTorch FSDP2 Tutorial (fully_shard)PyTorch · required
Work through
fully_shardper block then root,MixedPrecisionPolicy,reshard_after_forward, explicit prefetch and DTensor state dicts, and check each against the 2.14 API reference. FSDP1 (FullyShardedDataParallel) is the legacy API; record the PyTorch version beside every result. - PyTorch FSDP: Experiences on Scaling Fully Sharded Data ParallelZhao et al. (VLDB 2023) · support
Read the design sections on wrapping granularity, prefetching, rate limiting and hybrid sharding. Its flat parameter describes FSDP1; FSDP2 shards each parameter as a DTensor.
- TorchTitan - FSDP2 NotesPyTorch · support
Read
docs/fsdp.mdfor the FSDP1 to FSDP2 changes, dim-0 DTensor shards,reshard_after_forwardvalues and hybrid sharding on a 2D mesh, thentorchtitan/distributed/fsdp.py. Pin the commit you read. - DeepSpeed ZeRO TutorialDeepSpeed · support
Use the stage definitions and the
zero_optimizationconfiguration as the original vocabulary for the three stages and offload. Running it needs CUDA GPUs. - Distributed Training Guide - Chapter 4 FSDPLambda Labs · support
Read model initialization on the meta device, sharding, reshard policy, CPU offload and sharded checkpoint code in
04-fully-sharded-data-parallelas a practitioner second view. Its scripts need CUDA. - TorchFeather - FSDP and Activation CheckpointingTorchFeather · support
Watch the video segment at 10:49:41 on meshes, DDP and FSDP, then read
distributed/activation_checkpoint.pyand the FSDP calls inmodel/parallelize.py. Trace how parallel dimensions select the mesh, FSDP policy, precision and reshard behavior; do not adopt the full framework. - How to Scale Your Model - Chapter 5 TrainingGoogle DeepMind · support
Work the FSDP, model-state sharding, rematerialization and offload calculations in Chapter 5. Translate each JAX-oriented layout into equivalent parameter, activation and collective ownership without implementing JAX.
- TensorTonic - Account for ZeRO Memory by StageTensorTonic · support
Solve Account for ZeRO Memory by Stage before the CS336 accounting problems.