Unit 7 of 12 · about 4 hours · explain, implement
Unused parameters, static graphs and divergent control flow
One rank taking a different branch turns a collective into a hang. Static versus dynamic is the difference between paying traversal every step and promising stability.
You'll be able to
- Explain
find_unused_parameterscost andstatic_graphpromises, and choose between them for a given model. - Implement correct handling for frozen and tied params, and diagnose a divergent-control hang from the sync-point rule.
- State why
torch.autograd.graddoes not work with DDP.
Covers
Read
In this order, only the parts listed
0 of 3 done
- DDP sync points and stragglersPyTorch DDP Tutorial, Design Note and Communication Hooks
Skewed Processing Speeds: constructor, forward and backward as sync points in the same order, plus large
timeoutfor stragglers; Basic Use Case broadcast contract (page read 2026-10-14) - Unused-parameter handling in the design notePyTorch DDP Tutorial, Design Note and Communication Hooks
Forward Pass: graph traversal from the output with
find_unused_parameters=True, all buckets still reduced, overhead warning - Static graphs and related constructor flagsPyTorch DDP Tutorial, Design Note and Communication Hooks
find_unused_parametersdefaultFalse,static_graphsemantics (its logging-key check is stale in 2.14.1),skip_all_reduce_unused_params,no_syncforward-inside warning, notorch.autograd.gradsupport
Focus on
find_unused_parameters=Truewalks the autograd graph from the output on every forward to mark unused params ready. It still reduces all buckets; it only stops the wait. Pay it only when some forwards genuinely skip params.static_graph=Truepromises the used set and the control flow never change. It then supports reentrant backward, activation checkpointing applied several times or over unused parameters, and parameters outside forward. Decide from graph stability: thecan_set_static_graphlogging key the docstring names is absent from_get_ddp_logging_data()in PyTorch 2.14.1.- Constructor, forward and backward are sync points every rank must reach in the same order at about the same time. A rank-dependent branch gives a collective mismatch: hang, timeout, or wrong grads.
- With
skip_all_reduce_unused_params=TrueDDP skips reducing unused params. The same params must stay unused on every rank for the whole run; otherwise ranks desynchronize and can hang.
Common mistakes · 3
- Setting
find_unused_parameters=Truealways to be safe. It pays a traversal every forward; a stable graph withstatic_graph=Trueis cheaper. - Changing graph shape per iteration (for example depth by input) with
static_graph=True. The promise breaks and training hangs or corrupts. - Calling
torch.autograd.gradwith DDP. The module only works when grads accumulate intoparam.grad.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.What does
find_unused_parameterscost, and when is it worth it?AnswerA per-forward graph walk from the output. Pay it only when some params are genuinely unused in some forwards; otherwise use a static graph.
2.Two ranks run different numbers of DDP forwards per step. Symptom?Answer
Collective mismatch: hang, timeout, or wrong grads. Constructor, forward and backward must match in count and order on all ranks.
3.What does
static_graph=Truenewly support?AnswerReentrant backward, activation checkpointing applied several times or over unused parameters, and parameters outside the forward function. It also skips the per-step search for unused parameters.
4.How does equivalence treat the ToyModel's
fc2.fc.biaswithrequires_grad=False?AnswerIt stays equal with no grad and no sync. Init broadcast covers it; the collective touches only
requires_gradparams.
Practice
- CS336 Assignment 2: frozen and tied params in
test_ddp.pyThe frozen-parameter cases (
no_grad_fixed_param,_FC2bias withrequires_grad=False) and the tied-weight case (ToyModelWithTiedWeightswithfc4.weighttied tofc2.weight) insidetests/test_ddp.py
You're done when
You can decide between find_unused_parameters and static_graph for a given model and explain a divergent hang from the sync-point rule.
Only when you can do this without notes.
Leads to
Notes
Resources · 1 for this unit, 10 more in the section
- 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. - 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. - 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.