Unit 1 of 12 · about 3 hours · derive
The replicated data-parallel memory baseline by dtype
Sharding savings are only meaningful against a stated baseline. Build on GPU memory: allocated, reserved, peak and memory models, and Optimizers from SGD to AdamW; here the new job is accounting by rank and dtype.
You'll be able to
- Derive replicated parameter, gradient and Adam state bytes from the tensors actually kept by a precision policy.
- Separate persistent model state from gathered weights, activations, temporary buffers and allocator reserve.
Covers
Read
In this order, only the parts listed
0 of 2 done
- ZeRO: the model-state baselineZeRO: Memory Optimizations Toward Training Trillion Parameter Models
arXiv:1910.02054v3, Section 3.1, Model States: Optimizer States, Gradients and Parameters; Section 3.2, Residual Memory Consumption
- Memory usage revisitedUltra-Scale Playbook - Zero Redundancy Optimizer (ZeRO)
Zero Redundancy Optimizer (ZeRO), subsection Memory usage revisited
Stop before ZeRO-1. Its extra FP32 accumulation buffer is a specific storage choice, not a mandatory extra copy under every mixed-precision implementation.
Focus on
- For parameter count , sum each stored category once. The paper keeps half-precision parameters and gradients plus FP32 master parameters and two FP32 moments: bytes.
- FP32 parameters with FP32 gradients and two FP32 Adam moments also use tensor bytes, but have different lifetimes and communication payloads. Autocast alone does not require a persistent half-precision parameter copy.
- Inspect Adam after its first update, when moments exist. Count per-parameter step scalars separately; gradients may be absent after clearing them.
- Use decimal GB for bytes and GiB for bytes. Persistent model-state totals are not peak process or device memory.
Common mistakes · 3
- Counting an FP32 master parameter twice when the optimizer already updates the original FP32 parameter.
- Dividing activations by world size while holding local microbatch fixed. ZeRO changes model-state ownership, not that local workload.
- Measuring only before the first optimizer step and concluding that Adam has no state.
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.Does BF16 compute imply two-byte persistent parameters?Answer
No. FSDP2 can keep original FP32 shards for the optimizer while gathering BF16 parameters for compute. State the storage policy separately from compute precision.
2.Why is the paper's optimizer multiplier 12 rather than 8 bytes per parameter?Answer
It includes the four-byte FP32 master parameter as well as two four-byte Adam moments.
3.Can a model whose persistent state fits still run out of memory?Answer
Yes. Activations, full gathered weights, full local gradients, communication staging, optimizer temporaries and allocator reserve can raise the peak above capacity.
You're done when
Your ledger names every tensor category and reconciles its byte count with a small CPU model, without calling the persistent total a peak.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 9 more in the 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.
- 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
- 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. - 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.