ZeRO and Fully Sharded Training

Sharded training changes when parameters, gradients and optimizer state exist on each rank. Exact gather, reduce-scatter and reshard boundaries are needed to reason about memory, communication and checkpoints.

Section 4 of 10 · 12 units · about 51 hours

0/12

By the end you can Implement optimizer-state sharding and a minimal fully sharded step, match an unsharded oracle, predict and inspect peak live state, and save and restore a sharded checkpoint without silent replication.

Units

Build it

Mini project · about 14 to 18 hours

Fully sharded training with resharding evidence

Shard the data-parallel trainer in parallel/ from full replication to full sharding with checkpoints that survive changed layouts. This turns the memory ledger into per-rank ownership and gives the capstone its recovery story.

  1. 1Ledger replicated state by dtype against measured tensors after the first optimizer update; pass the TensorTonic ZeRO accounting cases and extend the CS336 sharded-optimizer tests with disjoint-data reduction and memory-saving checks.
  2. 2Build a tiny gather-compute-release and reduce-scatter trainer that matches the oracle over several updates; pass the CS336 FSDP tests and log where each full weight and reduced shard lives on every rank.
  3. 3Compare reshard-after-forward policies and prefetch depths on identical work with event timelines, keeping oracle equivalence; report live bytes and exposed stalls, with device traces only on two or more GPUs.
  4. 4Inventory wrap groups against the largest live group and simulate four-rank hybrid ownership with both gradient reductions; construct on the meta device and configure FSDP2 on a CPU mesh, checking shard and buffer materialization, dtypes, same-policy updates and global clipping against the oracle.
  5. 5Derive peak live memory and dtype-aware send volume, separating the ideal estimate from the live-state ledger and CPU simulation from device profiles; keep recompute gradient-equivalent with event counts and ledger a selective offload tier.
  6. 6Round-trip model and optimizer checkpoints at one world size with next-update equivalence and ownership inspection; restore onto a smaller world size, compare against the full format, and prove the next fixed-global-batch update.
  7. 7Seed gradient-scaling, clipping and replication faults plus one gather-peak budget break; show each fails its targeted check and diagnose one correctness and one memory failure from rank evidence.
What you hand in
  • Sharded trainer with ZeRO ledgers, lifecycle logs and wrap-group inventory.
  • Same-size and changed-layout checkpoint restores with next-update proofs.
  • Peak and volume derivations, offload ledger and the negative-test evidence.
How to check it
How it's graded

Shard accounting

Pass: Ledgers reconcile with measured tensors and the accounting cases pass with the allocation rule stated.

Great: Disjoint-data reduction and memory savings are checked beyond the public tests.

Lifecycle correctness

Pass: Gather and reduce-scatter runs match the oracle and logs place every weight and shard.

Great: Reshard and prefetch timelines explain one case where deeper prefetch is worse.

Checkpoint recovery

Pass: Same-size and changed-layout restores prove next-update equivalence with ownership intact.

Great: The full-format comparison and the direct restore agree, and metadata limits are stated.

Negative evidence

Pass: Each seeded fault fails its targeted check with rank evidence kept.

Great: The gather-peak break is budgeted in advance and the diagnosis names the live window.

Resources

  • CS336 Assignment 2 (Spring 2026) - Sections 6, 7 and 8.3Stanford University

    Complete optimizer_state_sharding and optimizer_state_sharding_accounting (Section 6), fsdp and fsdp_accounting (Section 7) and fsdp_calcs (Section 8.3) with your own code. tests/test_sharded_optimizer.py and tests/test_fsdp.py run 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

    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.

  • PyTorch FSDP2 Tutorial (fully_shard)PyTorch

    Work through fully_shard per 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.

  • ZeRO: Memory Optimizations Toward Training Trillion Parameter ModelsRajbhandari et al. (SC 2020)

    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.

  • PyTorch FSDP: Experiences on Scaling Fully Sharded Data ParallelZhao et al. (VLDB 2023)

    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

    Read docs/fsdp.md for the FSDP1 to FSDP2 changes, dim-0 DTensor shards, reshard_after_forward values and hybrid sharding on a 2D mesh, then torchtitan/distributed/fsdp.py. Pin the commit you read.

  • DeepSpeed ZeRO TutorialDeepSpeed

    Use the stage definitions and the zero_optimization configuration as the original vocabulary for the three stages and offload. Running it needs CUDA GPUs.

  • Distributed Training Guide - Chapter 4 FSDPLambda Labs

    Read model initialization on the meta device, sharding, reshard policy, CPU offload and sharded checkpoint code in 04-fully-sharded-data-parallel as a practitioner second view. Its scripts need CUDA.

  • TorchFeather - FSDP and Activation CheckpointingTorchFeather

    Watch the video segment at 10:49:41 on meshes, DDP and FSDP, then read distributed/activation_checkpoint.py and the FSDP calls in model/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

    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

    Solve Account for ZeRO Memory by Stage before the CS336 accounting problems.