Unit 4 of 13 · about 4 hours · derive, implement
All-reduce, gradient averaging and the all-gather and reduce-scatter duality
Data-parallel training is one repeated all-reduce. The ring duality also decides how sharded training later splits one average into two cheaper halves.
You'll be able to
- Derive per-rank bytes and steps for the ring forms of all-reduce, all-gather and reduce-scatter.
- Implement all three on several CPU processes and check every rank against a single-process oracle.
- Explain gradient averaging as a sum followed by a division, and state where each part happens.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Six primitives, the bandwidth lower bound and ring all-reduceMachine Learning Systems Volume II - Collective Communication
Choosing the Vehicle with The six core primitives, then Engineering the Flow with The bandwidth-optimal lower bound and Ring AllReduce
- Gradient averaging and the ring challenge (page read 2026-10-14)Writing Distributed Applications with PyTorch
Distributed Training: the
average_gradientsfunction withdist.all_reduceand division by world size, then Our Own Ring-Allreduce with thetorch.chunkexercise - Collective decomposition demoCS336 Spring 2025 - Lecture 8 Parallelism (executable lecture)
collective_operationsplustorch_distributedwithcollective_operations_main: the liveall_reduce,reduce_scatter_tensorandall_gather_into_tensorsequence that proves the decomposition - Ring costs for all-gather, reduce-scatter and all-reduceCS336 Assignment 2 (Spring 2026) - Sections 5.1 and 8.1
Section 8.1, Communication Primitives (pp. 40 to 41): the ring all-gather, ring reduce-scatter and ring all-reduce costs in the rendered equations
Focus on
- Ring all-reduce is a reduce-scatter followed by an all-gather. With ranks and payload , each half moves bytes per rank, so all-reduce moves .
- Gradient averaging is
dist.all_reduce(param.grad.data, op=dist.ReduceOp.SUM)on every rank, then division by the world size. The sum is collective; the division is local. - All-gather takes one tensor per rank and returns a length- list on every rank. Reduce-scatter takes a list of per-rank inputs and returns one shard per rank.
- The tutorial ring challenge sends full buffers, not chunks. Chunking into pieces is what turns its cost into the ring cost above.
Common mistakes · 3
- Passing one stacked tensor to
dist.reduce_scatter. It takes a list of per-rank input tensors and raises a type error otherwise. - Forgetting the division by world size after a SUM all-reduce. Every update then silently uses a learning rate scaled by worker count.
- Treating reduce-scatter as a cheaper drop-in for all-reduce. It returns only one shard per rank; the all-gather half is still owed.
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.Four ranks all-reduce a 1 GB gradient buffer. How many payload bytes cross each rank, and how does that compare with a naive full-buffer ring?Answer
Ring all-reduce moves GB = 1.5 GB per rank. A naive ring that forwards full buffers moves GB = 3 GB per rank, twice as much.
2.The tutorial all-reduces gradients and then divides by the world size. Would dividing first be wrong?Answer
No. Sum then divide and divide then sum both give the mean. DDP divides each bucket by the world size before its all-reduce, which also keeps low-precision sums smaller. The bug to avoid is dividing twice or not at all.
3.Two ranks hold [1, 2, 3, 4] and [10, 20, 30, 40]. What does each rank hold after reduce-scatter with SUM?Answer
Rank 0 holds [11, 22] and rank 1 holds [33, 44]: chunk 0 and chunk 1 of the elementwise sum.
4.All-gather and reduce-scatter each move about half the bytes of all-reduce. When does that factor of 2 fail?Answer
For small latency-bound messages, where startup cost dominates bytes moved. The factor of 2 holds in the bandwidth-bound regime with equal payloads.
Practice
- TensorTonic: Simulate Distributed Collectives
The
all_gatherandreduce_scatterpublic cases: rank-ordered concatenation and equal contiguous chunks of the sumThe
all_to_allcase belongs to the token-exchange unit; solve it there. - Writing Distributed Applications with PyTorch: gradient averaging
The
average_gradientsfunction on 4 gloo CPU processes: compare every parameter gradient against a single-process run on the joined batchKeep the MNIST data lines for the data-parallelism section; here a small fixed tensor batch is enough.
You're done when
Your hand bytes match the ring formulas, the TensorTonic cases pass, and all three gloo collectives match the single-process oracle on every rank.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 11 more in the section
- Machine Learning Systems Volume II - Collective CommunicationML Systems Textbook · primary
Read From Parallelism to Communication Patterns, Network Performance Modeling, Collective Operation Primitives, AllReduce Algorithms, Hierarchical Communication and Communication-Computation Overlap. Use its latency and bandwidth model to derive one communication budget, and return to The Distributed Training Step in https://mlsysbook.ai/vol2/distributed_training/distributed_training.html during DT-3.
- CS336 Spring 2025 - Lecture 8 Parallelism (executable lecture)Stanford University · support
Read
lecture_08.pyPart 1 (collective operations,torch.distributed, all-reduce and reduce-scatter benchmarks) and skim Part 2 (data, tensor and pipeline parallelism sketches). Run its CPU examples before the Playbook appendix. - Writing Distributed Applications with PyTorchPyTorch · required
Work through process-group setup, point-to-point communication and the collective operations, then adapt the examples to test broadcast, all-reduce, all-gather, reduce-scatter and all-to-all on Gloo with several CPU processes. Check backend support in the 2.14
torch.distributedreference; Gloo all-to-all runs on CPU in 2.14. - CS336 Assignment 2 (Spring 2026) - Sections 5.1 and 8.1Stanford University · required
Complete
distributed_communication_single_nodewith the Section 5.1.1 benchmarking practices on as many processes as you have, then derive the ring all-gather, reduce-scatter and all-reduce costs in Section 8.1 and solvealternate_ring_all_reduce.
Also in this section
- How to Scale Your Model - Sharded Matrices and GPU CollectivesGoogle DeepMind · required
Work the collective-cost problems in Part 3 (all-gather, reduce-scatter, all-reduce and all-to-all volumes, latency versus bandwidth) before opening each answer, then read How Do Collectives Work on GPUs in Part 12 at https://jax-ml.github.io/scaling-book/gpus. The worked answers use TPU link constants; redo them with your own bandwidth and latency.
- Ultra-Scale Playbook - A0: Parallel Programming Crash CourseHugging Face · required
Read A0 from processes and ranks through point-to-point communication, broadcast, reduce, all-reduce, all-gather, reduce-scatter, the ring all-reduce and asynchronous operations. Reproduce every small
torch.distributedexample on Gloo and annotate tensor ownership before moving to higher parallelism. - Building a Distributed Training Framework from First PrinciplesUmar Jamil · required
Watch only 04:32:59 for autograd and distributed mathematics, 05:04:19 for distributed graphs and DDP, 09:48:52 for collectives, and 10:49:41 for DeviceMesh, DDP and FSDP. Pause after each segment to reproduce the rank and tensor transitions with CPU torchrun; the nearly twenty-hour full video is not required.
- Picotron - Process Groups and Communication FilesHugging Face · required
Trace picotron/process_group_manager.py plus tp_communications.py, pp_communications.py and cp_communications.py. Draw every group, rank coordinate and tensor transfer for one 2 x 2 layout, then add a failing collective-order test in your own project.
- Picotron Tutorial - Steps 1 to 3 and Part 1 VideoHugging Face · support
Follow
step2_process_group_managerandstep3_dataloaderwith their diffs and the Part 1 video (model, process group manager and dataloader). Rebuild the rank grid yourself before reading the solution. - Machine Learning Engineering Open Book - Communication PatternsStas Bekman · required
Read
network/comms.mdfrom point-to-point through ring all-reduce, thennetwork/README.mdandnetwork/debug/README.mdselectively. Derive bytes and steps before running anything;network/benchmarks/all_reduce_bench.pyneeds two or more GPUs and reports bus bandwidth. - TensorTonic - Collective Simulation ProblemsTensorTonic · support
Solve Simulate Distributed Collectives and Calculate Collective Effective Bandwidth. The bandwidth problem counts ring passes; reconcile it with your alpha-beta model and with bus bandwidth before comparing numbers.
- AI Systems Performance Engineering - Chapter 4 Distributed Networking CommunicationChris Fregly (O'Reilly) · support
Read NCCL for Distributed Multi-GPU Communication, Topology Awareness in NCCL, NCCL Communication Algorithms, NCCL Communicator Lifecycle and Environment Gotchas, and Profiling and Debugging NCCL. With two or more GPUs, run
code/ch04/nccl_benchmark.pyfrom the companion repository and label the exact topology. - nccl-tests and its performance notesNVIDIA · support
With two or more GPUs, run
all_reduce_perfover a message-size sweep and readdoc/PERFORMANCE.mdfor algorithm bandwidth versus bus bandwidth. Without them, use the document to interpret published numbers. - PyTorch 2.14 `torch.distributed` referencePyTorch · reference
Look up the backends table,
init_process_groupwith timeout anddevice_id,new_groupandnew_subgroups, theWorkandasync_opcontract,monitored_barrier,destroy_process_groupandTORCH_DISTRIBUTED_DEBUG. Use it for API lookup beside the tutorial first pass. - PyTorch 2.14 torchrun (elastic launch) referencePyTorch · reference
Read Single-node multi-worker through the elastic membership rows for
RANK,WORLD_SIZEandLOCAL_RANK, plus flags--standalone,--nnodes,--nproc-per-node,--rdzv-backendand--rdzv-endpoint. Launch every CPU run withtorchrun --standaloneand a distinct port per run.