17.2 Collectives over processes: ring all-reduce
Mixed precision, loss scaling, gradient accumulation, and activation checkpointing made one process train the capstone on a laptop. Every larger run splits the batch over several workers instead: each computes the gradient of its own slice, and before anyone takes a step the gradients must be averaged across all of them. That averaging is an all-reduce, and it is the operation every data-parallel step waits on.
Concepts from other sections (3)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- data parallelism (DDP): each rank holds a full model replica, computes the gradient of its batch slice, and all ranks average gradients before stepping. Read more: 17.3 Data parallelism and ZeRO stages 1 to 3
- ZeRO: sharding training state across ranks so that each piece is stored once instead of times. Read more: 17.3 Data parallelism and ZeRO stages 1 to 3
- Process group: ranks cooperating in a particular collective or parallel axis. Read more: 17.4 Parallelism, topology, and the global training batch
On a laptop the workers are processes on the same machine, which is enough to build the real algorithm. Processes cannot share Python objects, so every byte moves through an explicit channel, and you can count those bytes. This section builds the collectives (all_reduce, reduce_scatter, all_gather, broadcast, barrier) over pipes with the bandwidth-optimal ring, and the next section, Data parallelism and ZeRO stages 1 to 3, builds data parallelism and ZeRO on top. The section is optional: a laptop capstone needs mixed precision and accumulation, not data parallelism.
There is nothing to build first. The only background is the byte counting of Counting: parameters, FLOPs, KV bytes, and memory plans in Mathematics: counting bytes per parameter is the same arithmetic as counting bytes per link. The solution is comm.py (chunk_bounds, Comm with send, recv, reduce_scatter, all_gather, all_reduce, broadcast, barrier, and spawn), and the contract is comm.pyi.
Learning objectives
- Derive the ring reduce-scatter and all-gather index rules and trace them by hand on three ranks.
- Show that a ring all-reduce sends bytes per rank and explain why that is optimal.
- Explain why a blocking ring deadlocks and how parity ordering breaks the cycle.
- Make a multi-process run fail loudly when one rank crashes.
Notation
Section titled “Notation”| Symbol | Meaning | Type / shape |
|---|---|---|
(world) | number of processes | int |
(rank) | this process’s number, | int |
| rank ‘s input array, entries | float64[n] | |
| the elementwise sum all ranks want | float64[n] | |
chunk of the flat array: entries from chunk_bounds | slice | |
| right, left | ranks and | int |
| bytes per entry (8 for float64) | int |
Processes and channels
Section titled “Processes and channels”spawn(fn, p) starts processes with multiprocessing’s “spawn” method (a fresh interpreter each, so fn must be importable by name) and connects every pair of ranks with a full-duplex pipe. send(x, dst) writes a copy of the array, and recv(src) blocks until the next array from src arrives; messages between two ranks arrive in order. A pipe holds only a few kilobytes, so a larger send blocks until the receiver reads. That one fact will matter a great deal when we get to deadlock.
Why a ring
Section titled “Why a ring”The obvious all-reduce sends every array to rank 0, adds, and sends the sum back. Rank 0 then receives entries and sends , so its link carries traffic that grows with while the other links idle. The ring keeps every link equally busy.
The ring starts by splitting the flat array into contiguous chunks , sized as numpy.array_split sizes them (the first chunks one entry larger). Rank owns chunk . ZeRO shards parameters by the same bounds in the next section, so the bounds must agree everywhere.
Reduce-scatter. In step , rank sends chunk to its right neighbour, receives chunk from its left neighbour, and adds what it received into its own copy of that chunk. Follow one chunk : it starts at rank , which sends its part right; each rank adds its own part and passes the partial sum on; after steps it reaches rank holding all contributions. So every rank ends holding its own chunk of the sum, , having sent chunks.
All-gather. Now each rank has one finished chunk and needs the others. In step , rank sends chunk right and receives chunk from the left. After steps every rank has every chunk. All-reduce is reduce-scatter followed by all-gather.
Counting bytes and rounding
Section titled “Counting bytes and rounding”Each half sends chunks of about entries, so
which approaches and never grows with : doubling the ranks halves each chunk. A lower bound says no all-reduce can do better (each rank must send at least of its data out and receive the same in), which is why NCCL uses rings for large messages.
The ring also changes the order of addition. The sum of chunk is accumulated in ring order starting at rank , not in rank order, and floating-point addition is not associative, so the result differs from numpy.sum in the last bits. But each chunk is reduced once and then copied, so all ranks hold identical bits, which is what keeps data-parallel replicas identical.
Deadlock, broadcast, barrier, and failure
Section titled “Deadlock, broadcast, barrier, and failure”If every rank does send then recv in a step, and the message is larger than the pipe buffer, every rank blocks in send waiting for its right neighbour to recv, which is itself blocked in send: a cycle of waits, forever. Breaking the cycle needs one rank that receives first. The contract’s rule is that even ranks send then receive, and odd ranks receive then send. With , rank 1 is always receiver-first, so the chain unwinds from there, for odd and even alike. MPI’s alternatives are MPI_Sendrecv and non-blocking sends.
broadcast(x, src) passes around the ring from src: each rank receives from the left and forwards right, except the rank just before src, which would send it back to where it started and leave a message in that pipe that the next collective would read by mistake. A barrier makes every rank wait until all have arrived; it is multiprocessing’s Barrier.
Finally, a rank that raises must stop the whole run with its traceback; otherwise its neighbours wait forever in a recv. spawn collects each rank’s result or traceback, terminates the rest on the first error, and raises TimeoutError if the ranks do not finish in time.
Working it by hand
Section titled “Working it by hand”Three ranks hold , , . With , each chunk is one entry: is entry 0, entry 1, entry 2. The table traces the two reduce-scatter (RS) steps and the two all-gather (AG) steps:
| step | rank 0 sends | rank 1 sends | rank 2 sends | rank 0 holds after | rank 1 holds after | rank 2 holds after |
|---|---|---|---|---|---|---|
| RS 0 | : | : | : | |||
| RS 1 | : | : | : | |||
| AG 0 | gets | gets | gets | |||
| AG 1 | gets | gets | gets |
In step RS 0, rank sends chunk and adds what arrives into chunk : rank 0 receives rank 2’s and adds its own 2. After reduce-scatter rank holds chunk of the sum: , , . After all-gather every rank holds . Each rank sent 4 entries of 8 bytes, 32 bytes, matching .
Designing the API
Section titled “Designing the API”def chunk_bounds(n: int, world: int) -> list[tuple[int, int]] # numpy.array_split sizesclass Comm: rank: int; world: int; bytes_sent: int def send(self, x, dst: int) -> None; def recv(self, src: int) -> NDArray def reduce_scatter(self, x) -> NDArray # chunk `rank` of the sum def all_gather(self, x) -> NDArray # every rank's chunk, in rank order def all_reduce(self, x, op="sum") -> NDArray # "sum" or "mean", x's shape def broadcast(self, x, src: int) -> NDArray def barrier(self) -> Nonedef spawn(fn, world: int, *args, timeout: float = 60.0) -> list # fn(comm, *args) per rankA worker is a module-level function fn(comm, *args), and spawn returns the workers’ return values by rank. Collectives must be called by every rank in the same order.
Building it step by step
Section titled “Building it step by step”The code follows from six observations.
- Every rank must end with the same full sum, and no link may carry more than its share. Gathering to rank 0 fails the second requirement (its link carries entries each way). Splitting the array into chunks and making each rank responsible for reducing one chunk spreads the work: each chunk’s sum is built by one walk around the ring.
- A chunk’s walk visits every rank once. Chunk must collect contributions and end at its owner , so it starts at and takes hops. In step rank therefore forwards the partial sum it received in step , which is chunk , and receives chunk . Check with : rank sends chunk , as required.
- The all-gather is the same walk without the addition. After reduce-scatter rank owns finished chunk ; in step it forwards chunk (its own at , then whatever it just received) and stores what arrives as chunk .
- Each step is “send right, receive left” on every rank at once, which is a cycle of waits. Blocking pipes break it only if some rank receives first. Alternating by parity puts a receiver-first rank (rank 1) on every ring with .
- The chunking must be a shared, deterministic function. ZeRO shards parameters by the same bounds, so
chunk_boundsis one function witharray_split’s sizes, not an inline computation. - Failure must be loud. A crashed rank leaves its neighbours blocked in
recvforever, so the parent must collect results with a deadline and terminate everyone on the first error.
Point to point
Section titled “Point to point”Point to point sends the dtype, shape, and raw bytes, so the receiver rebuilds exactly the array that was sent, and it counts bytes, not entries:
def send(self, x: ArrayLike, dst: int) -> None: if dst == self.rank or not 0 <= dst < self.world: raise ValueError(f"rank {self.rank} cannot send to {dst} (world {self.world})") a = np.ascontiguousarray(x) self._conns[dst].send((a.dtype.str, a.shape, a.tobytes())) self.bytes_sent += a.nbytes # bytes, so the 2(p-1)/p n beta claim is measurable
def recv(self, src: int) -> NDArray: dt, shape, raw = self._conns[src].recv() # frombuffer gives a read-only view of the message; copy so the receiver owns a writable array return np.frombuffer(raw, dtype=np.dtype(dt)).reshape(shape).copy()One ring step and the two halves
Section titled “One ring step and the two halves”One ring step, in the deadlock-free order:
def _exchange(self, out: NDArray) -> NDArray: right, left = (self.rank + 1) % self.world, (self.rank - 1) % self.world if self.rank % 2 == 0: # even ranks: send, then receive self.send(out, right) return self.recv(left) got = self.recv(left) # odd ranks: receive first, which breaks the cycle of blocked sends self.send(out, right) return gotThe two halves are the index rules of observations 2 and 3, verbatim:
def reduce_scatter(self, x: ArrayLike) -> NDArray: flat = np.array(x).reshape(-1) # a copy: the caller's array is never written p, r = self.world, self.rank b = chunk_bounds(flat.size, p) for s in range(p - 1): # p - 1 hops: a chunk visits every rank once si, ri = (r - s - 1) % p, (r - s - 2) % p got = self._exchange(flat[b[si][0] : b[si][1]]) flat[b[ri][0] : b[ri][1]] += got # add into the chunk that arrived, not the one sent return flat[b[r][0] : b[r][1]].copy() # rank r owns chunk r of the sum
def all_gather(self, x: ArrayLike) -> NDArray: p, r = self.world, self.rank chunks: list = [None] * p chunks[r] = np.array(x).reshape(-1) for s in range(p - 1): si, ri = (r - s) % p, (r - s - 1) % p chunks[ri] = self._exchange(chunks[si]) # forward what you have, store what arrives return np.concatenate(chunks) # rank order, whatever order chunks arrived in
def all_reduce(self, x: ArrayLike, op: str = "sum") -> NDArray: # ... op must be "sum" or "mean" a = np.asarray(x) out = self.all_gather(self.reduce_scatter(a)).reshape(a.shape) return out / self.world if op == "mean" else outBecause all_gather takes each rank’s chunk as given, it works for unequal chunk sizes too: the sizes come from the arrays themselves, not from a formula.
Broadcast and spawn
Section titled “Broadcast and spawn”Broadcast walks the ring once from src and stops one rank short:
def broadcast(self, x: ArrayLike, src: int) -> NDArray: p, r = self.world, self.rank if p == 1: return np.array(x) right, left = (r + 1) % p, (r - 1) % p data = np.array(x) if r == src else self.recv(left) if right != src: # the source already has it; a send here would sit in the pipe self.send(data, right) # and be read by the next collective as its first message return dataspawn connects every pair of ranks with a duplex pipe, starts each rank in _run (which puts ("ok", result) or ("err", traceback) on a shared queue), and collects results against one deadline:
try: for _ in range(world): left = deadline - time.monotonic() try: rank, status, value = results.get(timeout=max(left, 0.01)) except Exception: # queue.Empty raise TimeoutError(f"spawn: ranks did not finish within {timeout} s (deadlock?)") from None if status == "err": raise RuntimeError(f"rank {rank} failed:\n{value}") # first error stops the run out[rank] = valuefinally: for p in procs: if p.is_alive(): p.terminate() # neighbours blocked in recv on a dead rank are killed, not left hanging # ... joinThe full solution is comm.py, and a model test suite is test_l11_2_mine.py.
Why this design
Section titled “Why this design”All-reduce takes steps and sends bytes per rank; the bandwidth term is optimal. The latency term grows with , which is why production libraries switch to trees for small messages. Gather-to-root, by contrast, slows down linearly in because the root’s link carries entries each way; the ring’s per-link traffic is constant.
Non-blocking sends (threads or MPI_Isend) would avoid the deadlock too, but they add buffering and a second concurrency mechanism. Parity ordering is three lines and works for every with blocking pipes. Its cost is that in each step the odd ranks wait for the even ranks’ sends, so a step takes roughly two message times instead of one.
reduce_scatter copies its input once and recv copies each message. That spends memory to guarantee that the caller’s gradient is never mutated and that every received array is writable, both of which the next section relies on.
What can go wrong
Section titled “What can go wrong”A correct solution must pass these checks:
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| hand example: ring all-reduce | unit | the three-rank trace: chunks, sums, 32 bytes per rank | the ring’s indices are pinned to exact values |
| chunk bounds | unit | numpy.array_split sizes for , | ZeRO shards line up |
all-reduce matches numpy.sum | differential | on 35 entries, sum and mean, bitwise-equal ranks, input untouched | DDP replicas stay identical |
| bytes moved is | property | exactly for | the bandwidth argument |
| the two halves on their own | unit | the halves on their own, unequal chunks | ZeRO stage 2 and 3 |
| broadcast from every source | unit | every source, bytes per rank, a clean pipe afterwards | DDP’s initial weights |
| large messages do not deadlock | boundary | 1 MiB messages on a ring of 3 | real gradient sizes |
| point to point | unit | order, dtype, shape, a writable copy, bad ranks | the layer under everything |
| a rank’s error propagates | boundary | a crash on rank 1 raises its traceback quickly | no silent hangs |
| barrier | unit | a late rank holds everyone | ordering side effects |
Most bugs in a ring are off-by-one errors in the indices or the step count. If you add the received chunk into the one you just sent, the sums come out wrong and different on every rank; reduce_scatter adds into chunk ri = (r - s - 2) % p, not the sent si. One step too few in reduce-scatter leaves partial sums for , and one step too few in all-gather leaves stale chunks on some ranks; both halves loop range(p - 1) times. If "mean" returns the sum, gradients come out times too large; all_reduce divides by world for "mean".
Warning: If every rank sends first, the ring deadlocks as soon as a message exceeds the pipe buffer.
_exchangemakes even ranks send first and odd ranks receive first.
The remaining pitfalls concern data handling and process management. Accumulating into the caller’s array changes the caller’s gradient under it; reduce_scatter works on np.array(x), a copy. Chunks of the wrong sizes make ZeRO shards and collectives disagree; chunk_bounds gives the first n % world chunks one extra entry. Counting entries instead of bytes puts the bandwidth numbers off by ; the code uses bytes_sent += a.nbytes. Broadcasting back to the source leaves a stray message that corrupts the next collective; broadcast skips the send when right == src. Ignoring a rank’s failure lets the run “finish” with a missing result; spawn raises RuntimeError with the rank’s traceback. A barrier that does not wait lets side effects race; barrier waits on a shared Barrier. Returning a read-only view of the message leaves the receiver unable to write its own array; recv returns np.frombuffer(...).copy().
Where this leads
Section titled “Where this leads”Data parallelism and ZeRO stages 1 to 3 runs every collective through Comm: DDP all-reduces gradients, and ZeRO uses reduce-scatter and all-gather on parameter chunks.
The production versions keep the same algorithms and add scale:
| Solution piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
Comm over pipes | NCCL | rings and trees over NVLink and InfiniBand, chunked and pipelined so every link is busy at once | NCCL source, src/collectives/ |
all_reduce | torch.distributed.all_reduce | process groups, backends (NCCL, Gloo, MPI), async handles | torch/distributed/distributed_c10d.py |
| the ring | tree and recursive halving-doubling | lower latency for small messages ( steps instead of ) | Thakur et al. (2005) |
spawn | torchrun | rendezvous across machines, restarts, elastic world sizes | torch/distributed/run.py |
Further reading
Section titled “Further reading”- Patarasuk and Yuan, “Bandwidth Optimal All-reduce Algorithms for Clusters of Workstations” (JPDC, 2009).
- Thakur, Rabenseifner, and Gropp, “Optimization of Collective Communication Operations in MPICH” (IJHPCA, 2005).
- The NCCL documentation on collective operations.
Key terms
Section titled “Key terms”- collective: an operation that every rank calls in the same order and that moves data among all of them.
- all-reduce: every rank ends with the elementwise reduction (sum or mean) of all ranks’ arrays.
- reduce-scatter: every rank ends with one chunk of the reduction.
- all-gather: every rank ends with the concatenation of all ranks’ chunks, in rank order.
- ring: a topology in which each rank sends only to its right neighbour and receives only from its left.
- deadlock: a cycle of blocked operations, each waiting on the next, so none can proceed.
Summary
Section titled “Summary”- An all-reduce is a reduce-scatter followed by an all-gather; on a ring each half takes steps of one chunk of entries.
- Every rank sends of the array whatever is, so adding ranks does not add traffic per link.
- The ring adds in its own order: the result equals
numpy.sumto rounding, and every rank holds the same bits. - A blocking send on a ring deadlocks once messages outgrow the pipe buffer; even ranks send first and odd ranks receive first.
- A crashed rank must stop the run:
spawncollects results against a deadline and terminates every rank on the first error.
Review questions
Section titled “Review questions”-
Why does rank add the received data into chunk and not into the chunk it just sent?
Answer
The left neighbour sent its chunk . That is the chunk that arrived, so that is where the partial sum continues. Adding into the sent chunk mixes partial sums of two different chunks.
-
Why do all ranks hold identical bits even though the result differs from
numpy.sum?Answer
Each chunk is summed exactly once, by its walk around the ring, in a fixed order starting at rank . The all-gather then only copies that result. The order differs from
numpy.sum’s, so the last bits can differ, but no rank ever re-adds anything. -
Why does doubling not double the traffic per rank?
Answer
Each rank sends chunks, but each chunk is entries, so the total is . More ranks means more, smaller messages.
-
What happens if every rank sends first and the message is 1 MiB?
Answer
A pipe buffers a few kilobytes, so every
sendblocks until the right neighbour reads. Every neighbour is itself blocked insend, so nobody reachesrecv: a cycle of waits.spawn’s deadline turns that into aTimeoutErrorinstead of a hang.