Unit 4 of 12 · about 4 hours · implement

Minimal data parallelism with an explicit all-reduce

One explicit collective loop, written by hand, is the reference every wrapper must match. Wrappers hide ordering; this unit makes it visible.

You'll be able to

  • Implement naive data parallelism by hand: broadcast at init, shard the batch evenly, all-reduce and average grads, step locally.
  • Pass the CS336 DDP public tests for the plain and tied-weight toy models on CPU.
  • State the per-step collective volume for the toy model from its parameter count.

Covers

Minimal data parallelism with explicit all-reduce
1

Read

In this order, only the parts listed

0 of 3 done

  1. Naive DDP: broadcast, shard, all-reduce, stepCS336 Assignment 2 (Spring 2026) - Section 5 Distributed Data Parallel Training

    Section 5.2 A Naive Implementation of Distributed Data Parallel Training (p. 33): broadcast from rank 0, shard batch n over d devices, local forward and backward, all-reduce average grads, local optimizer step; problems naive_ddp (5pt) and naive_ddp_benchmarking (3pt)

  2. Public DDP tests and adapter contractCS336 Assignment 2 (Spring 2026) - Section 5 Distributed Data Parallel Training

    Tests test_DistributedDataParallel over ToyModel and ToyModelWithTiedWeights at world size 2 on gloo: validate_ddp_net_equivalence broadcast check, disjoint 10-row shards, 5 SGD steps at lr 0.1 with torch.allclose per step; adapters get_ddp plus ddp_on_after_backward

  3. Naive-path trap (read, do not copy)Picotron - Data Parallelism

    Class DataParallelNaive: register_backward_hook with register_post_accumulate_grad_hook in _allreduce_grads (lines 33 to 49). Read-only trap: the hook receives the parameter, so this path averages parameters and leaves param.grad local.

2

Focus on

  • Init broadcasts params from rank 0; the CS336 check validate_ddp_net_equivalence all-gathers the state_dict and asserts allclose on every rank.
  • Each rank forwards its disjoint shard (20 rows over 2 ranks gives offset = rank * local_bs with 10 rows each), backprops a local mean loss, then all_reduce SUM plus divide by world size.
  • The optimizer step stays local: identical averaged grads plus identical init keep replicas in sync with no per-step broadcast.
  • Frozen params with requires_grad=False carry no grads: no_grad_fixed_param and the _FC2 bias stay equal and untouched.
Common mistakes · 3
  • Copying the Picotron naive hook. register_post_accumulate_grad_hook receives the parameter tensor, not the grad: the shipped code all-reduces weights while grads diverge (for example grad[0,0] of -0.0538 on rank 0 versus -0.0662 on rank 1 after one backward on different inputs, 2 CPU ranks on gloo, PyTorch 2.14.1).
  • Dividing by world size twice, or never. All-reduce SUM followed by exactly one division matches the mean oracle.
  • Requiring grads for frozen params. The test asserts they stay equal without sync; only requires_grad params enter the collective.
3

Try it yourself

Closed-book, before checking anything

Implement get_ddp and ddp_on_after_backward in your own repo and run pytest tests/test_ddp.py on CPU (world size 2, gloo) until both model variants pass all 5 steps.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.What three operations does naive DDP need, and when?Answer

    Broadcast params once at init; per step, all-reduce SUM grads plus one division; local optimizer step. No per-step broadcast.

  2. 2.The Picotron naive hook all-reduces its argument and divides. Why do grads still diverge across ranks?Answer

    Its argument is the parameter. Parameters get averaged while param.grad stays local on every rank.

  3. 3.20 examples, world size 2. Which rows does rank 1 train on?Answer

    Rows 10 to 19: offset = rank * local_bs with local_bs 10.

  4. 4.Why does the test call ddp_on_after_backward before the optimizer step?Answer

    Async implementations wait on grad handles there. A blocking naive version implements it as a no-op.

5

Practice

  • CS336 Assignment 2: naive_ddp

    Problem (naive_ddp), Section 5.2; connect your container to get_ddp in tests/adapters.py and pass tests/test_ddp.py

    Runs on CPU with gloo. Implement in your own repository; keep the benchmark write-up even where two GPUs are unavailable.

You're done when

Both toy variants pass all 5 steps, and you can explain the Picotron hook trap without notes.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 9 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_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.

Also in this section

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