Unit 12 of 12 · about 3 hours · implement
Failure cleanup and rank-attributed logs in data-parallel training
A multi-process job with one log stream is undebuggable. Rank tags plus bounded cleanup turn hangs into readable first-cause lines.
You'll be able to
- Add rank-attributed logging to every data-parallel log line.
- Implement bounded teardown with barrier timeout and
destroy_process_groupinfinally. - Attribute a rank crash to its first cause from per-rank logs.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Rank-attributed logs and rank-zero side effectsDistributed Training Guide - Chapter 2 DDP
Lines 43 to 50: the
[rank=...]log format plus rank and world-size line; line 132: progress bar disabled above rank 0; lines 122 to 125 and 192 to 199: rank-0-only directory and checkpoint with barrier guards; lines 272 to 280:rank0_first - Rank-attributed failure walkDistributed Training Guide - Chapter 2 DDP
Per-rank step logs with the rank tag,
dist.barrierper step, rank-0 state persist; each rank raises when its draw falls under 0.001, so the job crashes on its own within a few hundred steps (a 2-rank CPU run on PyTorch 2.14.1 stopped at step 402 in about 2 s). Deletetoy-state.jsonbefore a fresh run: a rerun resumes from its step count. - Teardown and checkpoint barriersPyTorch DDP Tutorial, Design Note and Communication Hooks
Save and Load Checkpoints: save on rank 0,
dist.barrierbefore load, per-rankmap_location;cleanupwithdestroy_process_group; file deletion needs no extra barrier after backward all-reduce (page read 2026-10-14)
Focus on
- Tag every line with rank (plus local rank at launch): the
[rank=...]format makes interleaved streams attributable. - Rank 0 owns side effects: downloads, directories, checkpoints, progress. Barriers bracket shared artifacts so no rank reads unfinished state.
- Teardown runs in
finallywith a bounded timeout plusdestroy_process_group. A barrier with no explicit timeout waits for the process-group default (30 minutes, or 10 for NCCL, in 2.14.1) while a peer is stuck, so pass a short one. - Read first cause, not the reporter: the earliest stuck rank with its last timer phase (data, backward, collective) attributes the fault.
Common mistakes · 3
- One shared log stream with no rank tags. Interleaved ranks cannot attribute a stall.
- Skipping teardown on the exception path. Orphan procs hold the port and the next run collides; use a distinct port per run.
- Checkpointing on all ranks to one path concurrently. Writes interleave; rank-0-only plus barrier is the rule.
Try it yourself
Closed-book, before checking anything
try with finally teardown; raise on rank 1 at step 3. Show the survivor exits boundedly and the logs name rank 1 with its last phase.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why barriers on both sides of
rank0_first?AnswerRank 0 acts first while others wait; the trailing barrier stops early readers of unfinished state.
2.A survivor sits in a barrier for many minutes while its peer is stuck. Missing what?Answer
A short explicit timeout and teardown in
finally. Without one the barrier waits for the process-group default timeout.3.Rank 1 times out in backward all-reduce. Culprit?Answer
Not necessarily rank 1. Find the slowest rank and its last phase; the waiter reports, the straggler causes.
4.Why a distinct port per run?Answer
Crashed runs leave orphans holding ports; concurrent workers collide on fixed ports.
Practice
- Training-guide lab: elastic-training
toy.pyRun on 2 CPU ranks with the GPU hidden (
CUDA_VISIBLE_DEVICES=selectsgloo) undertimeout: read the rank-tagged step logs, watch barrier-coupled progress, find the raising rank in the traceback and the rank-0 state file, and state the first-cause rankRead here for log attribution and teardown only; restart membership belongs to the checkpointing section.
You're done when
Your crash run names the first-cause rank within a bounded time and no orphan holds the port.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 9 more in the section
- Distributed Training Guide - Chapter 2 DDPLambda Labs · support
Apply Chapter 2 to a tiny causal-LM loop:
torchrun, rank-aware logs,DistributedSampler, rank-zero side effects and the ZeRO memory subsection, plus the gradient-accumulation and effective-batch related topics. Its README saysoptimizer.step()synchronizes gradients; DDP averages them during backward, so correct that line in your notes. - PyTorch DDP Tutorial, Design Note and Communication HooksPyTorch · support
Use the tutorial for setup,
DistributedSampler,no_sync, unused parameters and teardown, the design note at https://docs.pytorch.org/docs/2.14/notes/ddp.html for buckets and gradient-ready order, and https://docs.pytorch.org/docs/2.14/ddp_comm_hooks.html for communication hooks. Consult them after the from-scratch path so wrappers do not replace the mechanism model.
Also in this section
- CS336 Assignment 2 (Spring 2026) - Section 5 Distributed Data Parallel TrainingStanford University · primary
Complete
naive_ddp,naive_ddp_benchmarking,minimal_ddp_flat_benchmarking,ddp_overlap_individual_parametersand its benchmarking problem with your own implementation.tests/test_ddp.pyruns on CPU with Gloo; keep the benchmark method and written derivations even when multiple GPUs are unavailable. This year's handout has no bucketed problem: build buckets from Picotron. - Picotron - Data ParallelismHugging Face · required
Read
data_parallel/bucket.pyand theDataParallelBucketpath besidetrain.py, and rebuild bucketed overlap with an equality test against one process. ReadDataParallelNaiveas a trap: itsregister_post_accumulate_grad_hookhook receives the parameter, so it averages parameters and leaves gradients local; show the unequal gradients on two CPU ranks. - Picotron Tutorial - Steps 5 and 6 and Part 3 VideoHugging Face · support
Follow
step5_data_parallel_naiveandstep6_data_parallel_bucketwith the Part 3 video. Step 5 carries the same parameter-hook trap as Picotron; use step 6 for the equivalence gate. - Ultra-Scale Playbook - Data ParallelismHugging Face · required
Read Data Parallelism through Revisiting global batch size and Our journey up to now (overlap, bucketing, accumulation and the global batch equation). Derive replicated-state memory, gradient communication and batch constraints for your tiny model before measuring it; ZeRO belongs to DT-4.
- PyTorch Distributed: Experiences on Accelerating Data Parallel TrainingLi et al. (VLDB 2020) · support
Read the design sections on gradient bucketing, overlap with backward,
no_syncand unused-parameter detection. Use them to explain why buckets follow reverse registration order. - Deep Learning at Scale - Chapter 7 Data ParallelismSuneeta Mall (O'Reilly) · support
Read Data Partitioning, Sampling Strategies, DDP and the data-pipeline sections, and use the small DDP exercises as extra practice. Its FSDP and ZeRO material predates FSDP2.
- TensorTonic - Synchronize Data-Parallel GradientsTensorTonic · support
Solve Synchronize Data-Parallel Gradients before the CS336 DDP tests.
- AI Systems Performance Engineering - Chapter 4 Overlap and DDP StrategiesChris Fregly (O'Reilly) · support
Read Overlapping Communication and Computation, Achieving Maximal Overlap in Practice and Distributed Data Parallel Strategies in Chapter 4, and the Chapter 13 streams section on overlapping communication. With two or more GPUs, compare
code/ch04/ddp_no_overlap.pyandddp_overlap.py; they refuse one-rank runs. - PyTorch SyncBatchNorm APIPyTorch · reference
Use
convert_sync_batchnormto convert BatchNorm layers before wrapping with DDP, and read the training-only sync plus single-GPU-per-process limits.