Unit 8 of 11 · about 3 hours · implement
Preemption signals and bounded graceful shutdown
Preemption is scheduled, not random. A bounded handler converts a killed job into a clean checkpoint instead of lost work.
You'll be able to
- Install a signal handler that finishes the current iteration, saves a checkpoint and exits inside a grace budget.
- Derive the grace budget from iteration time plus save time, and synchronize the exit decision across ranks.
Covers
Read
In this order, only the parts listed
0 of 2 done
- Graceful exit before the time limitMachine Learning Engineering Open Book - Checkpoints, Storage and Fault Tolerance
Dealing with forced job preemption: grace budget as iteration time plus save time, doubled; Approach A (a training-script exit-duration argument,
args.exit_duration_in_minsin the quoted Megatron code, with anall_reduceMAX sync); Approach B.1 (the#SBATCH --signal=USR1@600directive with aSIGUSR1handler and exit-asap flag); Approach B.2 (launcher signal forwarding withB:USR1,trapandpgrepchildren); QoS-based SLURM preemption - Kill and save switchesMachine Learning Engineering Open Book - Checkpoints, Storage and Fault Tolerance
Kill switch: poll for a file before each iteration, save and exit; Save switch: save and continue, removing the switch file
Focus on
- Grace time must cover a full started iteration plus the save: measure both, then double. Exiting early wastes nothing; exiting late loses the save.
- The exit decision is collective. Reduce one flag across ranks, as BLOOM's
all_reduceMAX pattern does, so no rank saves while another trains on. - Signals sent to the launcher do not reach the workers: a
SIGUSR1sent to thetorchrunagent terminated it, and no worker handler fired (2 CPU processes, PyTorch 2.14.1). Signal each worker process directly, or forward with the B.2 trap. - Kill and save switches are the same mechanism without the scheduler. A file poll before each iteration gives operators a manual preemption path.
Common mistakes · 3
- Relying on the default 30 to 60 seconds of
SIGTERM. Large-model saves take minutes, so request the early signal (USR1@600) and handle that instead. - Saving inside the signal handler. Handlers run asynchronously, so set a flag and let the main loop finish the iteration, then save and exit.
- Letting one rank exit on its signal while others continue. The job hangs in a collective, so synchronize the decision before anyone leaves.
Try it yourself
Closed-book, before checking anything
SIGUSR1 handler that sets a flag, finishes the iteration, saves model plus step, and exits. Send the signal mid-run and show both ranks exit 0 with a loadable checkpoint at the post-iteration step.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.An iteration takes up to 2 minutes and a save takes up to 2 minutes. What grace period do you request, and why not exactly 4?Answer
At least 8 minutes (double the sum). Exact budgets fail on jitter: a slow iteration plus a slow save overruns 4 minutes and the kill lands mid-write.
2.Rank 0's handler sets its flag but rank 1 never receives the signal. How does the job still exit cleanly?Answer
The flag is reduced across ranks before acting (MAX over the done tensor), so one rank's signal becomes every rank's decision in the same iteration.
3.Why does Approach B.2 send the signal to the
sbatchshell instead of the launcher?AnswerLaunchers such as
torchrundo not forwardSIGUSR1to workers. The shell traps it and re-sends to the child processes found withpgrep.4.A QoS preemption can land at any time, unlike a time-limit preemption. What changes in the handler?Answer
Nothing in the handler: the same flag, collective decision and bounded save run. What changes is that the grace duration is fixed by policy, so the budget must fit the worst case, not the schedule.
Practice
- Stas
SIGUSR1trap demo as a bounded-shutdown testApproach B.1
sigusr1.slurmhandler plus the train-loop flag pattern; test on a two-process CPU job by sendingkill -s USR1 "$pid"mid-run and assert both ranks save and exit within the budgetNo SLURM needed for the mechanism test: signal by PID and verify the checkpoint loads afterwards. The SLURM headers only schedule the signal.
You're done when
A mid-iteration signal produces a complete checkpoint at a clean boundary on every rank, inside a budget you derived from measured iteration and save times.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 12 more in the section
- 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.
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.
- 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. - 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.
- 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.
- 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.