Phase 6: Distributed Training · ~90 min · Python
Collective Ops From Scratch
The four collective operations that hold distributed training together are allreduce, broadcast, allgather, and reduce_scatter.
What you will learn
- Implement ring allreduce in two passes (reduce-scatter then allgather) and prove the per-rank communication volume is 2(N-1)/N bytes per element.
- Build broadcast, allgather, and reduce_scatter on top of point-to-point sends over `multiprocessing.Queue`.
- Verify every primitive against a `torch.distributed` gloo reference for the same input.
- Defend the choice of ring versus tree on cluster shape, latency floor, and bandwidth ceiling.
Introduction
Every other primitive a training framework offers is a wrapper around these. Build them once over a multiprocessing.Queue mesh, verify them against a reference implementation, and the rest of the track becomes plumbing.
- Type: Build
- Languages: Python
- Prerequisites: Earlier lessons in this course
- Time: ~90 min
Objective
Learning objectives
- Implement ring allreduce in two passes (reduce-scatter then allgather) and prove the per-rank communication volume is 2(N-1)/N bytes per element.
- Build broadcast, allgather, and reduce_scatter on top of point-to-point sends over
multiprocessing.Queue. - Verify every primitive against a
torch.distributed gloo reference for the same input. - Defend the choice of ring versus tree on cluster shape, latency floor, and bandwidth ceiling.
The Problem
A naive allreduce over N ranks sends N times the tensor to a root and broadcasts N times back. Bandwidth scales as $O(N)$ per rank, the root becomes a bottleneck, and the wall-clock floor is the slowest link times N. Ring allreduce flattens that into $2(N-1)$ chunks of size $T/N$, so per-rank bytes drop to $2T(N-1)/N$ independent of cluster size. Tree allreduce wins on small N and high-latency links because depth is $\log_2(N)$ hops instead of $2(N-1)$. Pick the wrong topology for the cluster shape and the slowest GPU dictates step time.
Every distributed training framework you will read this track depends on these four primitives. PyTorch DDP synchronises gradients with one allreduce per parameter bucket. ZeRO shards optimiser state by reduce_scatter and broadcasts updated parameters by allgather. FSDP turns the full forward into allgather plus reduce_scatter. Pipeline parallel needs broadcast for activations across stage groups. If you cannot implement the four collectives, you cannot reason about why training stalls, why the gradient mismatch shows up at rank 3, or why the pipeline bubble doubles when you swap topologies.
The Concept
Ring allreduce in two passes
Split the tensor into N equal chunks indexed 0..N-1. Each rank owns chunk index equal to its rank. Pass 1, reduce-scatter, runs N-1 steps. At step s, rank r sends chunk (r - s) mod N to rank (r + 1) mod N and receives chunk (r - s - 1) mod N from rank (r - 1) mod N, accumulating the received chunk into its local copy. After N-1 steps, rank r owns the full sum for chunk r. Pass 2, allgather, runs another N-1 steps and rotates the finished chunks around the ring until every rank holds the full sum for every chunk.
| Primitive | Per-rank bytes | Steps | When to use |
|---|
| Ring allreduce | 2T(N-1)/N | 2(N-1) | Large T, fat-pipe homogeneous cluster |
| Tree allreduce | T log2(N) | 2 log2(N) | Small T or high-latency links |
| Broadcast | T | log2(N) tree | Parameter init, scalar config |
| Allgather | T(N-1)/N | N-1 | Sharded forward, ZeRO unshard |
| Reduce_scatter | T(N-1)/N | N-1 | ZeRO gradient sharding |
Queue mesh as a stand-in for NCCL
NCCL runs over PCIe and NVLink with hardware-offloaded reductions. On CPU you do not have that. A multiprocessing.Queue per ring edge gives you ordered point-to-point delivery with a single producer and single consumer. The reduction happens in user space, so you pay Python overhead, but the wire pattern is identical to NCCL ring allreduce. Reason about correctness on the queue version and the cluster behaviour follows.
Verify against gloo
Every primitive lands with a unit test that compares its output against torch.distributed initialised with the gloo backend on the same tensor across the same world size. If your ring allreduce diverges from gloo by more than float32 epsilon, the test fails. Verification against a reference implementation is non-negotiable; without it the primitive looks correct until step 10000 of a real training run.
This lesson is free
You've read the first 4 sections. The rest covers Build, Check Yourself, Check Yourself, Key Terms & Next. A free account opens all of it, with its practice questions. No card.
Create a free account — the opening phases of 29 of 35 courses, and the first lesson of every phase, are free, no credit card.
Browse all courses · View pricing · DeVenture Academy