Unit 4 of 12 · about 4 hours · explain, measure
Reshard-after-forward policies and forward and backward prefetch
A policy can save one gather yet retain a large full tensor. Prefetch can hide a gather yet create an OOM. Both choices need a lifecycle timeline, not a universal fastest setting.
You'll be able to
- Predict full-parameter lifetimes and gather counts for reshard-after-forward policies.
- Compare prefetch depth using a live-state timeline and identify communication that remains exposed.
- Explain the memory cost of keeping weights through backward or gathering future groups early.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Reshard policy in PyTorch 2.14PyTorch FSDP2 Tutorial (fully_shard)
fully_shard, parameterreshard_after_forward;FSDPModule.set_modules_to_forward_prefetch,set_modules_to_backward_prefetchandunshardRead the Boolean and default policies first. Use the integer policy only in the simulator unless you have a suitable mesh; only a non-trivial divisor of the mesh shard dim size gives distinct behavior: 1 collapses to
Falseand the shard size itself toTrue, both without an error. - Implicit and explicit prefetchPyTorch FSDP2 Tutorial (fully_shard)
Forward/Backward with Prefetching, including both timeline diagrams and explicit-prefetch code (page read 2026-10-14)
Focus on
- With
reshard_after_forward=True, discard the full weight after forward and gather it for backward. Withreshard_after_forward=False, retain it until backward and avoid that gather. - In 2.14,
reshard_after_forward=Noneresolves to true for non-root modules and false for the forward root. Set the policy explicitly when comparing schedules; do not assume a root-only run has two gathers. - Backward's default prefetch follows reverse post-forward order. An explicit list overrides it; deeper prefetch increases simultaneous gathered state and is only useful if earlier launch reduces waiting.
- Use a deterministic event simulator first to measure live bytes and dependency stalls. With two or more GPUs, compare synchronized fixed-workload traces, allocated peaks and step times for these policies. CPU Gloo timings do not establish GPU overlap or interconnect performance.
Common mistakes · 3
- Treating asynchronous launch as proof of overlap. Only a timeline with concurrent work and satisfied dependencies establishes overlap.
- Copying a fixed prefetch list onto rank-divergent execution. Every group must still execute compatible collectives in the same order.
- Using
unshard()on the root and assuming it recursively gathers every nested FSDP group. It is not recursive.
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 does retaining full parameters after forward buy?Answer
It removes the backward parameter all-gather for those parameters, at the cost of a longer full-parameter lifetime. Gradient reduction is still required.
2.Why can deeper prefetch make a previously fitting model fail?Answer
Several gathered groups and their staging buffers can coexist with current compute and saved activations. The persistent shards have not changed, but the live peak has.
3.Why might the default keep the root's full parameters?Answer
The root is needed near the forward/backward boundary. Freeing and immediately gathering its parameters again can add traffic with little lifetime reduction. This is a heuristic, not proof that every root should be retained.
You're done when
Your policy comparison gives gather counts, peak live bytes and dependency stalls for identical work, and explains one case where more prefetch is worse.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 10 more in the 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.
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. - 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.
- 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.