Unit 9 of 12 · about 4 hours · implement, measure

DDP communication hooks, compression contracts and a PowerSGD experiment

Compression trades bytes for bias. The hook contract decides where the bias enters and who corrects it.

You'll be able to

  • Explain the comm-hook contract: GradBucket in, Future of one same-shape tensor out, division owned by the hook.
  • Implement fp16_compress_hook on a 2-rank CPU DDP job and bound its error versus vanilla all-reduce.
  • Run a small PowerSGD experiment and report the compression-versus-accuracy trade with its tuning knobs.

Covers

Communication hooks and compression contractsPowerSGD or low-precision communication hook experiment
1

Read

In this order, only the parts listed

0 of 3 done

  1. Comm-hook contract and default hooksPyTorch DDP Tutorial, Design Note and Communication Hooks

    How to Use plus What Does a Communication Hook Operate On: GradBucket.buffer, per-bucket call, single-tensor Future; Default hooks: allreduce_hook reference plus fp16_compress_hook and bf16_compress_hook; Debugging hooks: noop_hook headroom-only warning

  2. PowerSGD state and hooksPyTorch DDP Tutorial, Design Note and Communication Hooks

    PowerSGD State (matrix_approximation_rank, start_powerSGD_iter, min_compression_rate, use_error_feedback, warm_start, grad-sized extra memory, minimum start 2) plus PowerSGD Hooks (per-tensor path versus batched variant) and the serialization example

  3. Hook registration contractPyTorch DDP Tutorial, Design Note and Communication Hooks

    register_comm_hook(state, hook): hook signature, buckets not predivided by world size, register once before backward, Future holds the new bucket value

2

Focus on

  • Each hook takes one flattened bucket buffer and returns a Future of a single tensor with the same shape. Custom ops divide by world size themselves: buckets are not predivided.
  • The fp16_compress_hook casts, all-reduces in fp16, casts back. Bound it near 1e-3 relative (a 2-rank gloo ToyModel run on PyTorch 2.14.1 stayed within it); bandwidth claims need NCCL hardware.
  • PowerSGD keeps low-rank factors with error feedback and warm start at about grad-sized extra memory, defers compression to start_powerSGD_iter, tunes rank 1, 2, 4 upward, and skips vectors and incompressible tensors.
  • The noop_hook measures all-reduce headroom only. Never train with it.
Common mistakes · 3
  • Forgetting the division in a custom hook. Buckets arrive undivided, so grads come out R times too large.
  • Registering the hook after backward started. It applies late or errors; register before the loop.
  • Reading CPU gloo compression timing as interconnect evidence, or compressing from step 0 with feedback on. Bandwidth claims need NCCL hardware and early sensitive steps need vanilla all-reduce.
3

Try it yourself

Closed-book, before checking anything

On 2 CPU ranks, register fp16_compress_hook on one ToyModel DDP job and allreduce_hook on an identical one; assert grads agree within 1e-3 relative for 5 steps. Then switch to powerSGD_hook with matrix_approximation_rank=1 and start_powerSGD_iter=10 and show the first 10 steps match vanilla exactly.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.A hook returns what, and who divides?Answer

    A Future of a single tensor with the bucket buffer shape. Custom ops divide by world size themselves.

  2. 2.Why is start_powerSGD_iter at least 2 with error feedback on?Answer

    DDP rebuilds buckets at iteration 1, which conflicts with tensors memorized before the rebuild.

  3. 3.fp16 hook error bound on the CPU demo?Answer

    Near 1e-3 relative versus fp32 vanilla. Bandwidth claims need NCCL hardware.

  4. 4.Which tensors does PowerSGD skip, and why?Answer

    Vectors such as biases, plus shapes failing min_compression_rate. Overhead would exceed the saving.

5

Practice

  • PyTorch docs: PowerSGD hook experiment

    PowerSGD State plus PowerSGD Hooks sections with the end-to-end register-and-compare example: PowerSGDState(process_group, matrix_approximation_rank=1, start_powerSGD_iter=10) with powerSGD_hook on the ToyModel DDP job versus vanilla allreduce_hook; report steps-to-match and final param distance

    CPU gloo bounds the error only. Throughput or compression-ratio claims need NCCL hardware.

You're done when

The fp16 run stays within its bound and you can state the PowerSGD rank, start-iter and feedback trade without notes.

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.