All courses

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

▶ Watch this lesson Start the whole course

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.

How do 8 GPUs add up their gradients?

Eight GPUs each compute a gradient on different text. Every weight's update needs the **sum** of all eight gradients, and every GPU needs that sum. The naive approach sends everything to one GPU, which adds the gradients and sends the result back. That GPU becomes a traffic jam. Which communication patterns avoid this, what does each one leave on each GPU, and how many bytes does each GPU send?

Three collectives, cut into chunks

Cut each GPU's gradient into $G$ equal chunks, where $G$ is the number of GPUs. - **Reduce-scatter**: each GPU ends up owning one chunk, summed over all GPUs. - **All-gather**: each GPU contributes the chunk it owns. Everyone ends with all chunks side by side. - **All-reduce**: everyone ends with the full sum. Run reduce-scatter, then all-gather the summed chunks, and you get exactly all-reduce.

The ring and its byte cost

Let $S$ be the size in bytes of one device's full vector, so one chunk is $S/G$ bytes. Arrange the $G$ devices in a ring. Each device sends only to its next neighbor. - **Reduce-scatter** takes $G-1$ steps. In each step every device sends one chunk ($S/G$ bytes). The receiver adds its own copy of that chunk and passes the partial sum on. - **All-gather** uses the same ring and $G-1$ steps, passing finished chunks without adding. Bytes sent per device: $$\text{RS}=\text{AG}=\tfrac{G-1}{G}S,\qquad \text{AR}=2\tfrac{G-1}{G}S\approx 2S$$

Three GPUs, then a real gradient

With $G=3$, the GPUs hold $[1,2,3]$, $[4,5,6]$ and $[7,8,9]$. Each chunk is one number. - Reduce-scatter: GPU1 gets $1+4+7=12$, GPU2 gets $15$, GPU3 gets $18$. - All-gather: every GPU gets $[12,15,18]$, which is the all-reduce result. Now a 7B-weight gradient in bf16 ($S=14$ GB) on $G=8$ GPUs. Each phase sends $\frac78\cdot14=12.25$ GB per GPU, so all-reduce sends $24.5$ GB. Funnelling everything to one GPU would make it receive $7\cdot14=98$ GB and then send $98$ GB back.

Simulate reduce-scatter + all-gather

Rows are GPUs. Summing each chunk across rows is the reduce-scatter. Concatenating the summed chunks is the all-gather. This checks the results only. It does not simulate the ring's individual sends.
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

Reduce-scatter leaves each device one summed chunk. All-gather shares chunks so everyone has all of them. All-reduce, which equals reduce-scatter followed by all-gather, leaves everyone the full sum. On a ring, each device sends $\frac{G-1}{G}S$ bytes per phase, so about $2S$ for all-reduce, almost regardless of $G$.

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.