Unit 5 of 12 · about 5 hours · explain

Flat parameters, wrapping granularity and hybrid sharding

Wrapping determines what must fit in memory at once and which ranks communicate. Understanding both FSDP generations lets you read legacy code without importing its configuration into FSDP2.

You'll be able to

  • Explain FSDP1 flat storage, original-parameter views and communication grouping without treating them as FSDP2's representation.
  • Choose wrapping boundaries from the largest live group, launch count and shared-parameter ownership.
  • Map hybrid sharding's sharding and replication groups and explain both gradient reductions.

Covers

Flat parameters, handles and original-parameter viewsModule wrapping and sharding granularityHybrid sharded data parallel vocabulary
1

Read

In this order, only the parts listed

0 of 3 done

  1. Legacy flat parameters and hybrid shardingPyTorch FSDP: Experiences on Scaling Fully Sharded Data Parallel

    arXiv:2304.11277v2, Sections 3.2.1 Full Sharding, 3.2.2 Hybrid Sharding and 3.2.3 Autograd; Figures 3 and 4

    Read the representation and ownership design, not the paper's cluster benchmarks. This paper describes FSDP1.

  2. From flat parameters to DTensorTorchTitan - FSDP2 Notes

    Why FSDP2? and FSDP1 <> FSDP2 API Differences, including the mesh and state-dict tables

    Use the tables for migration vocabulary only. The statements that forward prefetch is 'not yet implemented' or 'under discussion' are stale; use the 2.14 prefetch APIs from the preceding unit.

  3. Current grouping and mesh contractPyTorch FSDP2 Tutorial (fully_shard)

    Communication Grouping and Scheduling; fully_shard parameter mesh and the FSDP1 comparison

2

Focus on

  • FSDP1 flattens and concatenates a group's parameters, pads, then chunks the flat storage. Original parameter tensors are views into that storage. A handle manages this group's lifecycle; it is not a separate model-state category.
  • FSDP2 shards each parameter as a DTensor, normally along dim 0, but still groups communication by fully_shard calls. Per-parameter representation does not mean one collective per parameter.
  • Apply fully_shard bottom-up to blocks, then the root. The root owns remaining parameters such as embeddings and the output head. Root-only grouping materializes the whole model's managed weights together.
  • For a 2D hybrid mesh, the first dimension replicates and the second shards. Reduce-scatter within each shard group, then all-reduce matching gradient shards across replicas. Model-state memory scales with shard degree, not total world size.
Common mistakes · 3
  • Dividing persistent memory by all four ranks in a two-by-two hybrid mesh. Each shard is replicated twice.
  • Wrapping tied aliases as unrelated groups. A shared parameter needs one compatible ownership and gather lifecycle for every use.
  • Assuming smaller groups always run faster. They lower individual gather sizes but increase collective launches and can expose latency.
3

Try it yourself

Closed-book, before checking anything

Draw a deterministic four-rank mesh with rows [0, 1] and [2, 3]. Identify sharding groups [0, 1] and [2, 3], replication groups [0, 2] and [1, 3], and each parameter shard's two copies. Sum gradient contributions within rows, then across columns; show that each owner obtains the same averaged shard as a global single-process oracle. Contrast root-only, per-block and per-linear communication groups on a two-block model.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Does FSDP2 use a FlatParameter for each group?Answer

    No. It stores per-parameter DTensor shards and groups them for communication. FlatParameter is the legacy FSDP1 representation.

  2. 2.Why does hybrid sharding need a second gradient reduction?Answer

    The reduce-scatter only combines data contributions inside one shard group. Matching shards in other replicated groups saw other data and must also be combined.

  3. 3.What happens to parameters not assigned by the inner sharding calls?Answer

    A later root call groups the remaining parameters. Omitting that call can leave intended-to-be-sharded parameters unmanaged.

You're done when

You can reconstruct the hybrid reduction by hand, identify every group's parameters, and explain why your chosen granularity differs from both root-only and per-parameter grouping.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 8 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.

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

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.

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