Unit 5 of 12 · about 4 hours · explain
DDP construction, parameter registration and reducer buckets
Buckets in reverse order are why communication starts before backward finishes. Misreading the order makes every later overlap prediction wrong.
You'll be able to
- Explain DDP construction: rank-0 broadcast, reducer creation, bucket formation in reverse registration order.
- Explain gradient-ready order: one hook per parameter, async all-reduce per full bucket, blocking wait at end of backward.
- Contrast bucketed DDP with the hand-rolled per-param loop on launch count and overlap.
Covers
Read
In this order, only the parts listed
0 of 4 done
- DDP design note: construction and backwardPyTorch DDP Tutorial, Design Note and Communication Hooks
Construction: broadcast of
state_dictfrom rank 0, reducer with buckets sized bybucket_cap_mb, mapping in roughly reverseModel.parameters()order, one autograd hook per parameter; Backward Pass: per-ready-grad marking, async all-reduce per full bucket, blocking wait, averaged grads inparam.grad - DDP constructor surfacePyTorch DDP Tutorial, Design Note and Communication Hooks
bucket_cap_mbdefault 25 MiB; parameters allreduce in reverse registration order;find_unused_parametersdefaultFalse;static_graph,gradient_as_bucket_view,no_syncandregister_comm_hooksignatures - Overlapped per-parameter DDPCS336 Assignment 2 (Spring 2026) - Section 5 Distributed Data Parallel Training
Section 5.3.2 Overlapping Computation with Communication of Individual Parameter Gradients (pp. 34 to 36):
register_post_accumulate_grad_hookper param,async_op=Trueall-reduce per ready grad,finish_gradient_synchronizationwait; problemsddp_overlap_individual_parameters(5pt) plus benchmarking (1pt) - PyTorch DDP paper: bucketing and overlapPyTorch Distributed: Experiences on Accelerating Data Parallel Training
arXiv:2006.15704v1, Sections 3 and 5.2: reducer design, overlap through autograd hooks and buckets, default 25MB buckets, with 10 to 25MB best on ResNet50 with NCCL, 50MB best on BERT with NCCL and 5MB best with Gloo, on 16 GPUs
Focus on
- The design note maps buckets in roughly reverse
Model.parameters()order because backward readies last-layer grads first. In PyTorch 2.14.1 with default flags, construction puts every parameter in one bucket; after the first backward DDP rebuilds the buckets in the order the grads actually became ready. Withfind_unused_parameters=Truethe construction-time buckets stay. - The reducer fires one async all-reduce per full bucket and always launches buckets in index order on every rank; mismatched order hangs or corrupts.
- After backward returns,
param.gradalready holds the averaged grad on every rank; the optimizer step is local. - The CS336 overlap build is per-parameter, not bucketed: one async all-reduce per ready grad plus an explicit finish wait. This handout has no bucketed problem; buckets come from Picotron in the next unit.
Common mistakes · 3
- Assuming buckets follow forward order. They follow reverse registration; forward order delays every launch.
- Assuming launches follow ready order across ranks. Index order is the safety rule; ready order can differ per rank.
- Adding or removing params after construction. Hooks were registered at construction for the exact set and order.
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.Why reverse order?Answer
Backward computes last-layer grads first, so reverse registration matches readiness and buckets fill at the earliest time.
2.The reverse-order assumption is wrong for a model. Consequence?Answer
Later launches and less overlap, until DDP rebuilds the buckets in observed ready order after the first iteration (not with
find_unused_parameters=True). Correctness holds either way.3.Why bucket index order instead of ready order?Answer
Every rank must invoke matching all-reduces in the same order; otherwise backward hangs or grads mismatch.
4.CS336 overlap versus DDP buckets?Answer
CS336 launches one async all-reduce per ready param plus a finish wait; DDP groups params into buckets for fewer launches with the same overlap idea.
Practice
- CS336 Assignment 2:
ddp_overlap_individual_parametersProblem (
ddp_overlap_individual_parameters), Section 5.3.2; connect toget_ddpplusddp_on_after_backwardand runtests/test_ddp.py5 timesPer-param overlap only. Bucketed overlap is built from Picotron in the next unit.
You're done when
You can narrate construction, buckets, hooks, ordered all-reduce and the param.grad handoff without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 8 more in the 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. - 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. - 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.
Also in this section
- 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.
- 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.