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

Reshard-after-forward policiesForward and backward prefetch ordering
1

Read

In this order, only the parts listed

0 of 2 done

  1. Reshard policy in PyTorch 2.14PyTorch FSDP2 Tutorial (fully_shard)

    fully_shard, parameter reshard_after_forward; FSDPModule.set_modules_to_forward_prefetch, set_modules_to_backward_prefetch and unshard

    Read 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 False and the shard size itself to True, both without an error.

  2. 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)

2

Focus on

  • With reshard_after_forward=True, discard the full weight after forward and gather it for backward. With reshard_after_forward=False, retain it until backward and avoid that gather.
  • In 2.14, reshard_after_forward=None resolves 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.
3

Try it yourself

Closed-book, before checking anything

Draw a three-group forward/backward event trace twice: first release every group's full weights after forward, then retain them until backward. Assume each group needs its weight in both passes, no accumulation and no prefetch. Count two gathers per group in the first trace and one in the second; compute the simultaneous full-weight bytes. Add one-group-ahead prefetch and show the extra live window before testing the schedules in trainlab.
4

Quiz yourself

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

0 got · 0 missed

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

Also in this 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.

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

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