Unit 9 of 12 · about 5 hours · derive, measure

Peak memory from live windows and FSDP communication volume

Persistent ZeRO-3 state can fit while a gather window does not. Build on Memory, arithmetic intensity and reconciling estimates; here the estimate must include the distributed lifecycle and critical path.

You'll be able to

  • Derive peak live memory from stored shards, gathered groups, activations, gradients and staging lifetimes.
  • Derive per-rank collective send volume with separate parameter and reduction dtypes.
  • Validate a byte or lifetime prediction and distinguish total communication from exposed communication.

Covers

Peak memory from live parameter and activation windowsCommunication volume and exposed collectives
1

Read

In this order, only the parts listed

0 of 3 done

  1. FSDP memory and communication accountingCS336 Assignment 2 (Spring 2026) - Sections 6, 7 and 8.3

    Section 7, fsdp_accounting, p. 39; Section 8.3, fsdp_calcs, pp. 42 to 44; use the notation and bandwidth assumptions at the start of Section 8

    Keep the handout's buffer-excluded estimate separate from your real peak, which includes gather staging. Solve the analytical questions on CPU; with two or more GPUs, profile a fitting model instead of the prescribed xl model.

  2. Stage-3 communication volumeZeRO: Memory Optimizations Toward Training Trillion Parameter Models

    arXiv:1910.02054v3, Section 7, Communication Analysis of ZeRO-DP

  3. A communication roofline, not an overlap guaranteeHow to Scale Your Model - Chapter 5 Training

    Fully-Sharded Data Parallelism (FSDP), its communication/compute equations; Takeaways from LLM Training on TPUs, forward/backward communication table

    Use the equations and table, not the claim that all ZeRO stages have the same communication cost. For release-after-forward FSDP with weights needed in backward, there are two gathers plus a reduce-scatter; its own table shows more total traffic than DDP. TPU bandwidth and batch thresholds are not GPU measurements.

2

Focus on

  • Compute peak as a maximum over time: . The categories are disjoint: stored model state excluding gradients, full gathered weights, saved activations, live gradients and other temporaries. Add allocator reserve separately.
  • Let be managed parameter elements, the shard degree, and , parameter-gather and gradient-reduction bytes per element. With even shards, a ring, two gathers, one reduce-scatter and no accumulation or extra recompute gathers, each rank sends bytes per step; it receives the same volume.
  • At equal payload dtypes, that is 1.5 times DDP's ring all-reduce send volume. If BF16 weights are gathered and FP32 gradients reduced, recompute the ratio; do not apply the equal-payload slogan.
  • Full local gradients and collective staging can coexist with gathered weights. Prefetch changes simultaneous live groups; it need not change total bytes. Exposed communication is determined by dependency slack, launch timing and contention, not by total volume alone.
Common mistakes · 3
  • Adding maxima from different phases as if they occurred together, or counting an alias as another allocation.
  • Reading the Scaling Book's 'same communication cost' sentence as equal total step traffic. Similar ideal communication-to-compute bounds do not mean equal byte counts.
  • Calling the sum of sent and received bytes the send volume, or treating a raw payload/time ratio as NCCL bus bandwidth without the collective correction.
3

Try it yourself

Closed-book, before checking anything

For a simulated single FSDP group with , , BF16 gathers and FP32 gradient reduction, derive 6,144 sent bytes per rank for two gathers plus one reduce-scatter. Compare with 6,144 sent bytes for DDP reducing the same FP32 gradients: the byte ratio is one, not 1.5. Exclude padding, metadata and accumulation. Then sum disjoint live categories at each event in your trainlab trace and predict its peak before inspecting the measured or simulated ledger.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.When does the familiar 1.5 traffic ratio apply?Answer

    For equal parameter and gradient payload dtypes, even shards and bandwidth-optimal ring accounting: two gathers plus one reduce-scatter versus DDP's two-pass all-reduce. Retention, padding or extra gathers change the comparison.

  2. 2.What is the model-state memory benefit of narrower wrap groups?Answer

    They reduce the size of individual materialized groups, potentially reducing the peak. Prefetch, activations and staging still determine which groups coexist.

  3. 3.Can equal total bytes give different step times?Answer

    Yes. Launch count, payload size, link contention and where the collectives lie on the critical path can differ. Only measured traces establish overlap on a device topology.

5

Practice

  • CS336 Assignment 2: fsdp_calcs and fsdp_accounting

    fsdp_calcs (a) to (c), Section 8.3; fsdp_accounting (a) and the conditional profiling in (b), Section 7

    These written questions have no public accounting tests. Separate the handout's ideal estimate from a trainlab live-state ledger, and label CPU simulation versus conditional multi-GPU profiling evidence.

You're done when

You solve the named accounting questions, reproduce the dtype-aware byte count and explain the difference between persistent state, simulated live peak and any measured allocator peak.

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_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.

  • 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.

  • 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.

Also in this section

  • 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.

  • PyTorch FSDP2 Tutorial (fully_shard)PyTorch · required

    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.

  • 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.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 · support

    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 · support

    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 · support

    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.

  • TensorTonic - Account for ZeRO Memory by StageTensorTonic · support

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