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_parameters cost and static_graph promises, 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.grad does not work with DDP.

Covers

Unused parameters and static versus dynamic graphsDivergent control flow and collective mismatch
1

Read

In this order, only the parts listed

0 of 3 done

  1. 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 timeout for stragglers; Basic Use Case broadcast contract (page read 2026-10-14)

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

  3. Static graphs and related constructor flagsPyTorch DDP Tutorial, Design Note and Communication Hooks

    find_unused_parameters default False, static_graph semantics (its logging-key check is stale in 2.14.1), skip_all_reduce_unused_params, no_sync forward-inside warning, no torch.autograd.grad support

2

Focus on

  • find_unused_parameters=True walks 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=True promises 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: the can_set_static_graph logging 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=True DDP 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=True always to be safe. It pays a traversal every forward; a stable graph with static_graph=True is 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.grad with DDP. The module only works when grads accumulate into param.grad.
3

Try it yourself

Closed-book, before checking anything

On 2 CPU ranks, train a model with a frozen bias and a tied linear pair through the CS336 suite and show both pass unchanged. Then sketch the divergent case on paper: rank 0 runs an extra DDP forward that rank 1 skips, and name the first mismatched collective.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.What does find_unused_parameters cost, and when is it worth it?Answer

    A 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. 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. 3.What does static_graph=True newly support?Answer

    Reentrant 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. 4.How does equivalence treat the ToyModel's fc2.fc.bias with requires_grad=False?Answer

    It stays equal with no grad and no sync. Init broadcast covers it; the collective touches only requires_grad params.

5

Practice

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_parameters and its benchmarking problem with your own implementation. tests/test_ddp.py runs 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.py and the DataParallelBucket path beside train.py, and rebuild bucketed overlap with an equality test against one process. Read DataParallelNaive as a trap: its register_post_accumulate_grad_hook hook 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_naive and step6_data_parallel_bucket with 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 says optimizer.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_sync and 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.py and ddp_overlap.py; they refuse one-rank runs.

  • PyTorch SyncBatchNorm APIPyTorch · reference

    Use convert_sync_batchnorm to convert BatchNorm layers before wrapping with DDP, and read the training-only sync plus single-GPU-per-process limits.