Unit 2 of 11 · about 4 hours · explain, implement
Full and sharded formats and distributed checkpoint state-dict contracts
Sharded checkpoints are the only format that scales past one device's memory. The state-dict contract of pre-allocated storage plus name management is what later makes resharding possible.
You'll be able to
- Explain when each rank writes its own shard versus gathering a full copy, and what the
.metadataplus one-file-per-rank layout guarantees. - Save and load a tiny FSDP2 model with
get_state_dictandset_state_dictplus aStatefulwrapper on CPU, and convert the result to a single-file torch checkpoint.
Covers
Read
In this order, only the parts listed
0 of 4 done
- DCP save and load with FSDP2PyTorch Distributed Checkpoint Recipes
How DCP works: parallel save, reshard at load, multiple files with at least one per rank, in-place operation,
Statefulhandling; Saving:AppState,get_state_dict,dcp.save; Loading: rebuild model and optimizer first, thendcp.load, at the same or a different world size (page read 2026-10-14) - Sharded save and load in the distributed-training guideDistributed Training Guide - Sharded Checkpoints, Determinism and Elastic Training
Sharded Checkpoints with
StateDictOptions(full_state_dict=False, cpu_offload=True); Saving a sharded checkpoint: barrier, then all ranks callget_state_dictfor one file per rank; Loading a sharded checkpoint:get_state_dict,load,set_state_dict; Converting a sharded checkpoint to a full state dict checkpointThe chapter links the unversioned
stablereference; read the 2.14 pages instead, where FSDP2 (fully_shard) is current and FSDP1 helpers are legacy. - Formats: DCP versus
torch.savePyTorch Distributed Checkpoint RecipesFormats:
dcp_to_torch_saveandtorch_save_to_dcpwithpython -m torch.distributed.checkpoint.format_utils dcp_to_torch <dir> <out>(page read 2026-10-14) - Per-rank files without DCPPicotron - Checkpoint State
CheckpointManager._get_checkpoint_pathnames files by TP and PP rank;save_checkpointwrites model, optimizer,trained_stepsandtrained_tokenswithtorch.savefrom DP and CP rank 0 only;load_checkpointassumes the same topology
Focus on
- DCP loads into pre-allocated storage: rebuild the model and optimizer before
dcp.load, because sharding for the load plan comes from the live objects.torch.loadneeds only a path. - Sharded means every rank saves. The checkpoint is the set of per-rank files plus
.metadata; a missing shard is a corrupt checkpoint, not a smaller one. get_state_dictwithfull_state_dict=Falsekeeps shards local with CPU offload;Truegathers the full copy on each rank. Choose per use, not per habit: shards for training, full for export.- Picotron's hand-rolled per-rank
torch.savefiles carry no resharding metadata, so restore needs the identical TP and PP layout. That limitation is what DCP's planner plus.metadataremoves.
Common mistakes · 3
- Expecting
dcp.loadto create the model. It updates the passed state dict in place, so an unbuilt model gives it nothing to write into. - Saving only on rank 0 with a sharded state dict. Skipped ranks lose their shards and
.metadatadescribes files that do not exist. - Reading one rank's
.distcpfile as a partial model. Shards are planner-assigned ranges, not layers; onlydcp.loadreassembles them.
Try it yourself
Closed-book, before checking anything
fully_shard toy model plus Adam state with the recipe's AppState, mutate both, reload, and show all 16 flattened state entries equal with torch.equal. Then convert the directory with dcp_to_torch_save.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does the DCP loading example rebuild the FSDP model and optimizer before calling
dcp.load?AnswerDCP loads in place into pre-allocated storage and takes each rank's sharding from the live objects. Without them it has nowhere to write and no resharding plan.
2.A 4-rank checkpoint directory holds
.metadataand three.distcpfiles. Is it loadable?AnswerNo. One shard is missing, so the checkpoint is incomplete. Resume must refuse it and fall back to the last known good, never train on three quarters of the state.
3.When should
full_state_dictbe true at save time, and what does it cost?AnswerOnly to export for single-process use such as inference. Every rank gathers the full model, so peak memory holds the whole copy instead of one shard.
4.Picotron saves one
.pthper TP and PP rank withtorch.save. What breaks when the job restarts with a different TP degree?AnswerEach file holds a fixed TP slice with no resharding metadata, and
load_checkpointassumes the same topology. The slices no longer match the new layout.
Practice
- PyTorch DCP recipe: FSDP save plus load, CPU-ported
Port the Saving and Loading examples to CPU with gloo and
torchrun --nproc-per-node 2(no per-rank CUDA device): save after one step, mutate, reload, and assert bit-exact parameters and optimizer state; then rundcp_to_torch_saveon the directoryThe recipe targets an 8-GPU server with NCCL but the logic is identical on gloo. Expect
.metadataplus one__<rank>_0.distcpper rank.
You're done when
Your CPU port saves, reloads bit-exactly and converts to a single file, and you can state which half of the contract (get_state_dict versus set_state_dict) each failure violates.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 10 more in the section
- PyTorch Distributed Checkpoint RecipesPyTorch · required
Complete the distributed checkpoint recipe with a tiny sharded model on CPU, then
dcp.async_savefrom the 2.14 reference at https://docs.pytorch.org/docs/2.14/distributed.checkpoint.html. The recipe saves model and optimizer only: add scheduler, scaler, RNG and dataloader state yourself, then inject partial directories, missing shards and a changed world-size restore. - Distributed Training Guide - Sharded Checkpoints, Determinism and Elastic TrainingLambda Labs · required
Run
related-topics/elastic-training/toy.pywith two CPU processes (hide any GPU so it uses Gloo) to see a crash-and-restart loop, then read the determinism related topic and the Chapter 4 sharded save and load. Prove the same next batches and updates after one interrupted run. - Picotron - Checkpoint StateHugging Face · support
Inspect picotron/checkpoint.py and every call site in train.py. List which state is saved, how ranks participate and which DT-1 reproducibility requirements need to be added in your project.
Also in this section
- Machine Learning Systems Volume II - Chapter 7 Fault ToleranceML Systems Textbook · primary
Read Failure Analysis at Scale, Hardware Fault Taxonomy, Check-and-Verify, Fault Injection Tools and Frameworks, Checkpointing, Failure Detection and Recovery, Elastic Recovery and Distributed Debugging and Observability. Turn them into a failure matrix before implementing the PyTorch DCP lab.
- TorchFeather - Distributed Checkpoint ManagerTorchFeather · required
Trace components/checkpoint.py through state wrappers, DCP save and load, asynchronous mode, retention and cleanup. Write a failure table for every barrier, future, temporary artifact and deletion boundary.
- Machine Learning Engineering Open Book - Checkpoints, Storage and Fault ToleranceStas Bekman · required
Read
training/fault-tolerance/README.md(frequent checkpoints, kill and save switches, watchdogs, preemption) and the "Don't forget the checksums" and benchmark sections ofstorage/README.md. Turn them into an operational save, verify, publish, retain and recover runbook. - Nanotron - Distributed SerializationHugging Face · support
Read the serialization section of
docs/docs.md(metadata, per-rank optimizer and RNG shards) and its statement that checkpoints are not topology agnostic, then the reasons insrc/nanotron/serialize/. Use it as the worked example of why resharding is hard. - PyTorch Elastic Run DocumentationPyTorch · support
Use failure modes and membership changes to understand agent, worker group, rendezvous and restart semantics. Implement only bounded local restart tests; elasticity is not a substitute for checkpoint correctness.
- torchftPyTorch · support
Read the README and
train_ddp.py: per-step quorum through the lighthouse, fault-tolerant DDP and HSDP, and live peer recovery. Compare per-step healing with restart-from-checkpoint. - TorchTitan - CheckpointingPyTorch · support
Read
docs/checkpoint.mdand the checkpointer component at a pinned commit: intervals, model-only and export saves, asynchronous modes and keys excluded from loading. - The Llama 3 Herd of Models - Reliability and Operational ChallengesLlama Team, Meta (2024) · support
Read the reliability section and its interruption table (root causes of unexpected interruptions over a 54-day snapshot) and use it to set failure rates in your checkpoint-interval model.
- Bumblebee - DCP and Checkpoint Smoke TestsBumblebee · depth
Only as a possible capstone target: read
primitive/ckpt/dcp.pyand the checkpoint smoke tests in a clean checkout. Its own roadmap lists reshardable optimizer state as a gap. - OLMo-core CheckpointerAi2 · support
Read
src/olmo_core/train/checkpoint.py(_temporary_wd,_get_tmp_dir,_teardown_tmp_dir,_save_metadata) for temp-directory atomic publish with barrier discipline, andsrc/test/train/checkpoint_test.py(run_checkpointer) for the save, find and reload assertions.