Unit 12 of 12 · about 4 hours · implement

Sharded equivalence tests and auto-wrap, OOM and replication failures

A decreasing loss can hide wrong averaging, incomplete synchronization or replicated state. The section checkpoint needs evidence that the update is right and the intended state is actually sharded.

You'll be able to

  • Build independent loss, gradient, clipping, update and shard-ownership checks for a tiny sharded run.
  • Diagnose a correctness failure from rank-local tensor metadata and collective order.
  • Distinguish gather-window OOMs, retained full weights, unexpected replication and allocator reserve.

Covers

Single-process equivalence and rank-local state testsAuto-wrap mistakes, OOMs and unexpected replication
1

Read

In this order, only the parts listed

0 of 2 done

  1. What the FSDP public checks actually proveCS336 Assignment 2 (Spring 2026) - Sections 6, 7 and 8.3

    test_fsdp_correctness, _test_fsdp_correctness, test_fsdp_gradient_sync and _test_fsdp_gradient_sync

    Read the oracle construction, loss normalization and assertions. Do not reread the whole assignment or copy the test implementation into your trainer.

  2. Debug the current ownership contractPyTorch FSDP2 Tutorial (fully_shard)

    User contract, Communication Grouping and Scheduling; fully_shard parameters ignored_params and reshard_after_forward

2

Focus on

  • Use the DT-3 update oracle with fixed initial state, data, precision, normalization and optimizer settings. Compare reconstructed gradients and updates, global clipping and reduced metrics separately, within justified tolerances.
  • Inspect unique parameter identities, names, global shapes, local shard sizes, dtypes and optimizer references. Check moments after their first allocation. Loss agreement alone cannot prove memory ownership.
  • The assignment's tests check SGD updates and replicated gradient agreement, not gathered-weight lifetime, Adam resume or sharding memory. Keep those trainlab checks independent of public-test success.
  • For legacy FSDP1, inspect the auto-wrap result; for FSDP2, inspect explicit bottom-up groups and root coverage. Diagnose a budget failure from the live-state ledger first. With two or more GPUs, compare allocator peaks and gather timelines before assigning an OOM to fragmentation.
Common mistakes · 3
  • Comparing to a reference with a different batch, precision policy or optimizer initialization and treating any discrepancy as a sharding defect.
  • Summing DTensor global element counts on every rank as if they were local allocations. Inspect local storage and include replicated categories explicitly.
  • Ignoring unmanaged parameters. In FSDP2, ignored_params are neither sharded nor gradient-reduced by FSDP; a replicated trainable parameter still needs a synchronization contract.
3

Try it yourself

Closed-book, before checking anything

Use a tiny CPU Gloo sharded model and the full-batch oracle. Introduce one fault at a time: omit the data-degree division in educational gradient reduction, leave one intended parameter replicated, or clip only local shards. Each fault must fail the appropriate numerical or ownership assertion. In the live-state simulator, change from block groups to a root-only group or retain all full weights; choose a budget that the original schedule fits and show which event exceeds it, without provoking a real machine OOM.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Why is matching the loss curve not enough?Answer

    Small update errors can still decrease loss, and fully replicated training can be numerically correct while failing the sharding objective. Check gradients, updates and rank-local ownership separately.

  2. 2.Which evidence distinguishes retained full weights from optimizer-state growth?Answer

    Inspect full-parameter lifetimes across forward and backward, and optimizer tensor allocations after the first step. Their categories and allocation times differ even if both raise peak memory.

  3. 3.Can a root-only group be numerically correct but a bad memory configuration?Answer

    Yes. It can gather the entire managed model at once. Correct updates do not imply that its peak working set fits the chosen budget.

  4. 4.What makes a tied-parameter ownership check meaningful?Answer

    It checks the shared tensor's identity and all use sites, ensures one compatible sharding lifecycle and optimizer update, and verifies that aliases reconstruct the same logical values.

You're done when

The correct run passes the numerical and ownership oracles, each injected fault fails a targeted check, and you diagnose one correctness and one memory failure from saved evidence.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 9 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.

  • 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

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