Unit 8 of 12 · about 3 hours · implement

Meta-device and deferred parameter initialization

A model may fail before its first step if initialization materializes the whole model on each device. Shard-first initialization avoids that peak but makes initialization and buffer completeness part of the correctness contract.

You'll be able to

  • Construct a meta model, shard it before materializing storage and initialize or load every parameter and buffer.
  • Distinguish metadata-only construction from deferred replay of initialization operations.
  • Verify shard initialization against an explicitly defined full-model oracle rather than relying on seeds alone.

Covers

Meta-device and deferred parameter initialization
1

Read

In this order, only the parts listed

0 of 3 done

  1. Shard first, materialize secondPyTorch FSDP2 Tutorial (fully_shard)

    FSDP1-to-FSDP2 migration guide, New fully_shard() usage and Migration Steps (page read 2026-10-14)

  2. Meta initialization and buffersDistributed Training Guide - Chapter 4 FSDP

    Delayed initialization - the meta device; Sharding the meta model; Initializing the model, including the rotary-buffer example

    Read these initialization subsections only, not the Run Command. Meta avoids tensor data storage, not all Python memory. The overview's claim that each layer gathers optimizer state is incorrect; only weights are needed for layer compute.

  3. Deferred initialization is a different mechanismPyTorch FSDP: Experiences on Scaling Fully Sharded Data Parallel

    arXiv:2304.11277v2, Section 3.1, Model Initialization

    Use this to distinguish recorded initialization replay from the current meta flow, not as an FSDP2 API recipe.

2

Focus on

  • A meta tensor has shape and dtype but no data to copy. to_empty allocates uninitialized destination storage; it does not restore values or replay random initialization.
  • Create the module under torch.device('meta'), apply bottom-up FSDP2 sharding with the intended mesh, then materialize only the stored shards. Initialize them or load a compatible checkpoint before any compute or optimizer step.
  • Initialize buffers too. A module-specific reset_parameters() may not rebuild position or rotary buffers. Enumerate parameters and buffers and give each an initialization or load path.
  • An identical seed alone is not an initialization specification across shard layouts. Check reconstructed values against a full tensor, or define a global-index-based initializer that is independent of rank count.
Common mistakes · 3
  • Calling .to('cpu') on a meta tensor and expecting its values. There are no values to copy.
  • Materializing the full model before applying sharding, defeating the initialization memory saving.
  • Assuming the base nn.Module implements recursive reset_parameters(). Initialization is model-specific.
3

Try it yourself

Closed-book, before checking anything

In two CPU Gloo processes, construct torch.nn.Linear(8, 4) under torch.device('meta'), shard it on init_device_mesh('cpu', (2,)), and check that its parameters remain meta. Call to_empty(device='cpu'); under torch.no_grad(), fill the stored weight with 0.25 and bias with zero. For torch.ones(2, 8), obtain a two-by-four output filled with 2 on each rank, matching an ordinary CPU linear layer initialized identically. Add a buffer and show that it also requires explicit initialization.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Does meta construction preserve the random parameter values that construction would have generated?Answer

    No. It produces metadata without tensor data. Deferred initialization that records and replays operations is a separate mechanism.

  2. 2.What must be true before the first forward after to_empty?Answer

    Every parameter and buffer the forward reads must have valid initialized or loaded values on the intended device and with a consistent distributed layout.

  3. 3.Why is constant initialization useful in the tiny check?Answer

    Its full-tensor meaning is independent of RNG and partitioning, so it isolates meta materialization and shard reconstruction from random-initializer behavior. It is not a recommended training initializer.

You're done when

The tiny meta-initialized model matches the ordinary model, no meta or uninitialized tensor reaches compute, and each buffer has a checked initialization path.

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.

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

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.

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

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