Collective ops: all-reduce, all-gather, reduce-scatter
Describe what each collective produces across N devices, for example summing gradients computed on different GPUs. Explain why all-reduce = reduce-scatter + all-gather, and estimate the bytes each one sends.
Builds on: Compute, memory and scale, What training does
What the animation says
The traffic jam
Eight GPUs each compute a gradient on different text. To update the weights, every GPU needs the sum of all eight gradients. The naive fix: send everything to one GPU, add it there, and send the sum back. For a seven-billion-weight model in bf16, each gradient is fourteen gigabytes. So the hub receives seven times fourteen, ninety-eight gigabytes, then sends ninety-eight back. The other links mostly wait. That one GPU is a traffic jam. We need a pattern that shares the work.
Cut into chunks: reduce-scatter
Shrink it to three GPUs. They hold one, two, three; four, five, six; and seven, eight, nine. Cut each gradient into three chunks, one per GPU. Here each chunk is a single number. Reduce-scatter: each GPU ends up owning one chunk, summed over all GPUs. GPU one owns the first chunk: one plus four plus seven, twelve. GPU two owns fifteen, GPU three owns eighteen. Next, all-gather: each GPU shares the chunk it owns. What does GPU three hold afterwards?
All-gather completes all-reduce
Twelve, fifteen, eighteen. All-gather passes owned chunks around without adding, so every GPU ends with all of them side by side. That is exactly the full sum of the three gradients, position by position. Every GPU holding the full sum is called all-reduce. So all-reduce is reduce-scatter followed by all-gather. This short check confirms the results match. It does not simulate the individual sends.
The ring
How do GPUs do this without a hub? Arrange them in a ring. Each one sends only to its next neighbour. Each step, every GPU sends one chunk at the same time. The receiver adds its own copy and passes the partial sum on. Step one: GPU two sends its four to GPU three, which adds its seven and holds eleven. Meanwhile, GPU three's eight becomes ten at GPU one, and GPU one's three becomes nine at GPU two. Step two is next. Tap the number GPU three sends on.
Counting bytes
After step two, each GPU owns a finished chunk. Three GPUs took two steps. In general, reduce-scatter takes G minus one steps. Each step, every GPU sends one chunk: S over G bytes, where S is its full vector. So reduce-scatter sends G minus one over G times S per GPU. All-gather reuses the ring for G minus one more steps, passing finished chunks without adding. Same bytes again. All-reduce is both phases: twice that, just under two S. Eight GPUs, a fourteen-gigabyte gradient. How much does each GPU send for all-reduce?
Ring beats the hub
Twenty-four and a half gigabytes. Seven eighths of fourteen is twelve and a quarter per phase, times two phases. Compare the hub: it received ninety-eight gigabytes and sent ninety-eight back. The ring spreads the load evenly. Add more GPUs and the cost per GPU barely moves. It creeps toward two S. Recap. Reduce-scatter: each GPU owns one summed chunk. All-gather: everyone gets every chunk. All-reduce: everyone gets the full sum.
Three collectives, cut into chunks
The ring and its byte cost
Three GPUs, then a real gradient
Simulate reduce-scatter + all-gather
import numpy as np
G = 4
g = np.random.randn(G, 8) # row i = GPU i's gradient
chunks = np.split(g, G, axis=1) # G chunks, each [G, 2]
owned = [c.sum(axis=0) for c in chunks] # reduce-scatter: GPU j owns sum of chunk j
full = np.concatenate(owned) # all-gather: everyone gets every summed chunk
print(np.allclose(full, g.sum(axis=0))) # True: equals all-reduce
S = g[0].nbytes # bytes in one GPU's vector: 8 float64 = 64
print((G-1)/G*S, 2*(G-1)/G*S) # 48.0 (RS or AG), 96.0 (AR) bytes per GPU
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses