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

Rank-consistent loss and metric reductionDistributed batch normalization vocabulary and limits
1

Read

In this order, only the parts listed

0 of 3 done

  1. 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)

  2. SyncBatchNorm conversion and limitsPyTorch SyncBatchNorm API

    convert_sync_batchnorm(module, process_group): converts all BatchNorm layers before the DDP wrap; stats sync within the group; sync disabled in eval; single GPU per process note

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

2

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_batchnorm before 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.
3

Try it yourself

Closed-book, before checking anything

On 2 CPU ranks with uneven shards (4 versus 2 samples), compute the global mean both ways: plain average of local means and sum-over-count. Show they differ, then implement the correct reduction and match a single-process mean over all 6 samples.
4

Quiz yourself

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

0 got · 0 missed

  1. 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. 2.When is averaging local means correct?Answer

    With equal local counts and the same reduction. Otherwise weight by counts.

  3. 3.Where does convert_sync_batchnorm go relative to the DDP wrap?Answer

    Before wrapping. Then wrap; only single GPU per process is supported.

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

5

Practice

  • Deep Learning at Scale: Scene Parsing with DDP

    Hands-On Exercise #4 Scene Parsing with DDP (pp. 247 to 251): Lightning strategy=ddp training with sync_dist=True validation reduction; compare the 1-GPU baseline against rank-consistent 2-GPU numbers

    With 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=True path 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_batchnorm to 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_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.

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