Unit 8 of 12 · about 3 hours · explain, implement
Rank-consistent loss and metrics, and distributed batch normalization
Local batch stats and local means lie. A reported loss that mixes denominators cannot be compared across runs.
You'll be able to
- Reduce rank-local losses and metrics to one consistent global number with the correct denominator.
- Convert BatchNorm to SyncBatchNorm before DDP and state its training-only and single-GPU limits.
- Explain DDP buffer broadcast versus gradient averaging.
Covers
Read
In this order, only the parts listed
0 of 3 done
- DDP buffers and BatchNorm stateDeep Learning at Scale - Chapter 7 Data Parallelism
Distributed Data Parallel (pp. 242 to 243): replicas plus per-forward buffer broadcast from the leader, BatchNorm batch statistics through buffers; Devil in the details box on start methods (p. 243)
- SyncBatchNorm conversion and limitsPyTorch SyncBatchNorm API
convert_sync_batchnorm(module, process_group): converts allBatchNormlayers before the DDP wrap; stats sync within the group; sync disabled in eval; single GPU per process note - Buffer broadcast versus param syncPyTorch DDP Tutorial, Design Note and Communication Hooks
Note that buffers (for example BatchNorm stats) broadcast from rank 0 every iteration while parameters are never broadcast; grads are all-reduced
Focus on
- Global mean needs the global denominator: average rank-local means only with equal local counts. With 4 versus 2 samples, sum-then-divide gives 2.667 while averaging the means gives 3.0.
- Reduce metrics as SUM and COUNT, then divide once. Never average per-rank accuracies over different denominators.
- Convert with
convert_sync_batchnormbefore wrapping with DDP. Stats sync within the group during training only; eval uses running estimates; only single GPU per process is supported. Its training forward rejects CPU tensors (SyncBatchNorm expected input tensor to be on GPU), so a CPU check reduces sums and counts by hand. The BatchNorm axes are the ones from Batch, layer, group and RMS normalization. - DDP broadcasts buffers from rank 0 every forward but never rebroadcasts params; grads are the all-reduced quantity in backward.
Common mistakes · 3
- Averaging per-rank accuracies or losses over unequal local shards (padding, last batches, join). The small shard gets overweighted.
- Skipping conversion and running per-rank BatchNorm. Local stats plus running estimates diverge silently across ranks.
- Expecting stat sync in eval. SyncBatchNorm syncs during training only.
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.Rank losses 2.0 over 4 samples and 4.0 over 2 samples. Global mean?Answer
2.667, from
(2.0 * 4 + 4.0 * 2) / 6. Averaging the means gives 3.0, which overweights the small shard.2.When is averaging local means correct?Answer
With equal local counts and the same reduction. Otherwise weight by counts.
3.Where does
convert_sync_batchnormgo relative to the DDP wrap?AnswerBefore wrapping. Then wrap; only single GPU per process is supported.
4.Buffers versus params in a DDP iteration?Answer
Buffers broadcast from rank 0 to all ranks each forward; params are never rebroadcast; grads all-reduce in backward.
Practice
- Deep Learning at Scale: Scene Parsing with DDP
Hands-On Exercise #4 Scene Parsing with DDP (pp. 247 to 251): Lightning
strategy=ddptraining withsync_dist=Truevalidation reduction; compare the 1-GPU baseline against rank-consistent 2-GPU numbersWith two or more GPUs, run it and compare the rank-consistent numbers against the 1-GPU baseline. On one GPU or CPU, read the
sync_dist=Truepath and treat the book's 1-GPU versus 2-GPU V100 numbers as the author's example. Focus on the metric-reduction path, not the segmentation scores.
You're done when
Your reduction matches sum-over-count on uneven shards, and you can state the BatchNorm conversion order plus its limits without notes.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 8 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. - 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.
- 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.
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. - 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.