Search

Search papers and lessons

All-Reduce

Level 3 · 12 min read

All-reduce gives every machine in a group the sum (or another combined value) of all their numbers, and the ring version does it while sending each machine only about twice its own data.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • Linear algebra
  • Optimizers
  • Distributed systems
  • ReadReady to readNot yet

TL;DR

All-reduce is an operation for a group of machines. Each machine starts with its own list of numbers, and at the end every machine holds the same combined result, usually the element-wise sum of all the lists. It is how data-parallel training makes every copy of a model take the same Gradient step. The ring version does this while each machine sends only about twice its own data, no matter how many machines there are.

Builds on: Data Parallelism, Gradient · Leads to: Model Parallelism, Megatron-LM

Where it's used

Averaging gradients in data-parallel training. In the Goyal and colleagues ImageNet run (2017), 256 GPUs each computed a Gradient on their own shard of the batch. Averaging those gradients is an all-reduce: every GPU ends up with the same averaged gradient and applies the same update. The paper's speed came from making this step cheap relative to the compute.

Splitting layers across GPUs. Megatron-LM (Shoeybi and colleagues, 2019) splits the large matrices inside each Transformer layer across several GPUs. The partial results from the GPUs have to be summed before the next layer can run, and that sum is an all-reduce inside the forward and backward passes. The reduction happens many times per step, so its speed matters as much as the gradient averaging does.

Libraries that do it for you. Horovod (Sergeev and Del Balso, 2018) packages all-reduce so that a TensorFlow or PyTorch training script can average gradients across machines with a few lines of code. Underneath, it calls a communication library such as MPI or NVIDIA's NCCL, which implement all-reduce for GPUs that are linked by fast cables.

How it works

Take three workers. Each holds a Vector of three numbers, split into three chunks of one number each. Chunks are numbered 0, 1, 2.

  1. Start state. Worker 0 holds [1,2,3][1, 2, 3], worker 1 holds [4,5,6][4, 5, 6], worker 2 holds [7,8,9][7, 8, 9]. The goal is for every worker to end with the sum [12,15,18][12, 15, 18]. Check: 1+4+7=121 + 4 + 7 = 12, 2+5+8=152 + 5 + 8 = 15, 3+6+9=183 + 6 + 9 = 18.
  2. Arrange the workers in a ring: 0 sends to 1, 1 sends to 2, 2 sends to 0. In each round every worker sends one chunk to its right-hand neighbour, and the neighbour adds it to its own copy of that chunk. In round 1, worker 0 sends chunk 0 (value 1) to worker 1, which becomes 4+1=54 + 1 = 5. Worker 1 sends chunk 1 (value 5) to worker 2, which becomes 8+5=138 + 5 = 13. Worker 2 sends chunk 2 (value 9) to worker 0, which becomes 3+9=123 + 9 = 12. Check: each worker sent exactly one number.
  3. Round 2. Worker 0 sends chunk 2 (value 12) to worker 1, which becomes 6+12=186 + 12 = 18. Worker 1 sends chunk 0 (value 5) to worker 2, which becomes 7+5=127 + 5 = 12. Worker 2 sends chunk 1 (value 13) to worker 0, which becomes 2+13=152 + 13 = 15. Now each worker holds one chunk that is the full sum: worker 0 has chunk 1 = 15, worker 1 has chunk 2 = 18, worker 2 has chunk 0 = 12. This half of the algorithm is called reduce-scatter.
  4. All-gather. Two more rounds spread the finished chunks. In round 3 each worker forwards the finished chunk it owns. In round 4 each worker forwards the chunk it received in round 3. The receiver copies each chunk without adding. In round 3 the sends are 15, 18 and 12. In round 4 they are 12, 15 and 18. Check: after round 4 every worker holds [12,15,18][12, 15, 18].
  5. Cost check. Each worker sent 4 numbers in total, which is 2(N−1)=42(N - 1) = 4 sends with N=3N = 3. Its vector has P=3P = 3 numbers, so the total is 2(N−1)NP=43⋅3=4\frac{2(N-1)}{N} P = \frac{4}{3} \cdot 3 = 4.

The ring for three workers, which is the case the steps above follow. The same pattern works for any number of workers:

flowchart LR
  W0[Worker 0] -->|chunk| W1[Worker 1]
  W1 -->|chunk| W2[Worker 2]
  W2 -->|chunk| W0

The math

Definition: All-reduce

Let there be NN workers, worker kk holding a Vector xk∈RP\mathbf{x}_k \in \mathbb{R}^P of PP numbers. All-reduce with sum leaves every worker holding the same vector S=∑k=1Nxk\mathbf{S} = \sum_{k=1}^{N} \mathbf{x}_k, where ∑\sum is summation notation for adding the NN vectors. The sum is element by element, so the jj-th entry of S\mathbf{S} is the sum of the jj-th entries of all the xk\mathbf{x}_k.

Step 1 in symbols

Split each vector into NN chunks of length P/NP / N. Write xk(j)\mathbf{x}_k^{(j)} for chunk jj of worker kk, with chunks indexed j=0,…,N−1j = 0, \dots, N-1. The target for chunk jj is S(j)=∑kxk(j)\mathbf{S}^{(j)} = \sum_{k} \mathbf{x}_k^{(j)}. In the example, chunk 0 of worker 0 is the number 1, and S(0)=1+4+7=12\mathbf{S}^{(0)} = 1 + 4 + 7 = 12.

Step 2 and 3 in symbols (reduce-scatter)

In a ring, after N−1N - 1 rounds, worker kk holds the complete sum of chunk (k+1) mod N(k + 1) \bmod N. In the example with N=3N = 3, worker 0 holds chunk 1 (the sum 15), worker 1 holds chunk 2 (the sum 18), and worker 2 holds chunk 0 (the sum 12). Each round adds exactly one incoming chunk to a local chunk, so the work is spread evenly across the workers.

Step 4 in symbols (all-gather)

Another N−1N - 1 rounds copy each finished chunk around the ring until every worker holds all NN entries of S\mathbf{S}. No addition happens in this half.

The cost in symbols

Let α\alpha be the fixed delay of one message (latency) and β\beta the number of bytes per second a link carries (bandwidth). Let ss be the bytes per number. The ring takes 2(N−1)2(N - 1) message rounds, and each worker sends 2(N−1)NPs\frac{2(N-1)}{N} P s bytes in total, so Tring≈2(N−1) α+2(N−1)N⋅Psβ.T_{\text{ring}} \approx 2(N - 1)\,\alpha + \frac{2(N - 1)}{N} \cdot \frac{P s}{\beta}. The second term does not grow with NN once NN is large, because N−1N\frac{N-1}{N} approaches 1. The first term grows linearly with NN.

Worked examples

Example
Tiny: two workers, one number each

Setup: worker 0 holds 33 and worker 1 holds 55. The goal is for both to hold the sum.

  1. Worker 0 sends its number, 3, to worker 1.
  2. Worker 1 sends its number, 5, to worker 0.
  3. Worker 0 adds the received 5 to its own 3, getting 88.
  4. Worker 1 adds the received 3 to its own 5, getting 88.
  5. Both workers now hold 88.

Interpretation: with two workers, the ring is just a swap followed by an addition. Each worker moved one number and did one addition.

Example
Standard: a 350-million-parameter gradient on four machines

Setup: P=350,000,000P = 350{,}000{,}000 Gradient entries stored as 16-bit numbers, so s=2s = 2 bytes and the gradient is 700700 MB. There are N=4N = 4 workers linked at 100 gigabits per second, which is 12.5 GB/s.

  1. Naive approach: every worker sends its full gradient to each of the other 3 workers. That is 3×700=2,1003 \times 700 = 2{,}100 MB sent per worker.
  2. Time for the naive approach: 2,100 MB/12,500 MB/s=0.1682{,}100 \text{ MB} / 12{,}500 \text{ MB/s} = 0.168 s, or 168 ms.
  3. Ring approach: each worker sends 2(N−1)N×700=64×700=1,050\frac{2(N-1)}{N} \times 700 = \frac{6}{4} \times 700 = 1{,}050 MB.
  4. Time for the ring: 1,050/12,500=0.0841{,}050 / 12{,}500 = 0.084 s, or 84 ms.
  5. Interpretation: the ring halves the time here, and the gap grows as NN grows, because the naive cost is proportional to NN and the ring cost stays near 2×7002 \times 700 MB. The latency term 2(N−1)α2(N-1)\alpha is ignored in this estimate; for a 700 MB message it is small.
Example
Realistic: a ResNet-50 gradient on eight GPUs

Setup: ResNet-50 has about 25.6 million parameters. Gradients are stored as 32-bit numbers, so s=4s = 4 bytes, and the gradient is 25.6×106×4=102.425.6 \times 10^6 \times 4 = 102.4 MB. There are N=8N = 8 GPUs, linked at 25 GB/s. Assume one local forward and backward pass takes 50 ms (an assumption for this example, not a measurement).

  1. Ring factor: 2(N−1)N=148=1.75\frac{2(N-1)}{N} = \frac{14}{8} = 1.75.
  2. Data sent per GPU per step: 1.75×102.4=179.21.75 \times 102.4 = 179.2 MB.
  3. Transfer time: 179.2 MB/25,000 MB/s=0.00717179.2 \text{ MB} / 25{,}000 \text{ MB/s} = 0.00717 s, about 7.2 ms.
  4. Latency term: 2×7=142 \times 7 = 14 message rounds. With a per-message delay of about 10 microseconds, that adds about 0.14 ms, which is small.
  5. Overlap: if the framework starts sending each layer's gradient as soon as that layer's backward pass finishes, most of the 7 ms can hide under the 50 ms of compute. The step then costs close to the compute time, not compute plus communication.
  6. Interpretation: at this size, the communication is small compared to the compute, so the all-reduce is not the bottleneck. It becomes the bottleneck when the link is much slower or the model much larger.

Try it: the same ResNet-50 gradient on 16 GPUs with the same 25 GB/s link. Find the ring factor, the data per GPU, and the transfer time in milliseconds. Answer: the factor is 2×15/16=1.8752 \times 15 / 16 = 1.875, the data is 1.875×102.4=1921.875 \times 102.4 = 192 MB, and the transfer time is 192/25,000=0.00768192 / 25{,}000 = 0.00768 s, about 7.7 ms. It is slightly more than the 8-GPU case, because the factor creeps toward 2 as NN grows.

Common mistakes and tradeoffs

It is tempting to think all-reduce works by sending everything to one machine that adds it up and then sends the result back. In fact that hub approach is a valid implementation, but the hub's link carries data from every other machine, so it is the bottleneck. The ring spreads the traffic evenly, so every link carries about the same amount, because each machine sends and receives about 2P2P numbers in total regardless of the group size.

It is tempting to think that adding machines makes communication cheaper, since the work is divided. In fact the work per machine falls, but the communication per machine stays near 2Ps2 P s bytes, and the latency term 2(N−1)α2(N-1)\alpha grows with NN. For a small message on many machines, latency dominates, and a tree-shaped all-reduce, which takes about 2log⁡2N2 \log_2 N rounds, is faster. The ring wins for large messages such as full gradients; the tree wins for small messages on large groups.

It is tempting to think that the result is always an average. In fact the basic operation is a sum. Frameworks often divide by NN afterwards to get the average, so the code must say which one it wants, because Stochastic Gradient Descent takes its step size from the gradient it is given. If shards have different sizes, the average must weight each shard by its size, as in Data Parallelism.

It is tempting to think that communication is free once it can overlap with compute. In fact overlap only hides communication that is shorter than the compute it runs under. A slow link or a small per-GPU batch makes the all-reduce visible in the step time. Critical Batch Size and GPU Memory Hierarchy are the other limits on how far this scales.

Use all-reduce when every machine needs the same combined result each step, as with gradient averaging. Do not use it when machines only need part of the result, or when they can tolerate stale values; then a parameter server, asynchronous updates, or gradient compression can be cheaper. Compare these with the Asynchronous Message Passing model, where the lack of a time bound makes any collective step fragile if a machine in the Distributed System fails.

History

Collective operations such as all-reduce were part of parallel computing long before deep learning. The Message Passing Interface standard, first published in 1994, defines a reduction that combines values across processes and returns the result to all of them. Researchers in high-performance computing studied ring-style schedules in the 2000s, because they use the links evenly.

Deep learning needed this when models grew beyond one device. Goyal and colleagues (2017) showed that averaging Gradients over large batches on 256 GPUs could match the accuracy of smaller runs, and their communication step was an all-reduce. That is the data-parallel pattern at scale. Around the same time, teams at Baidu and then Uber brought ring all-reduce into deep learning frameworks, and Horovod (2018) made it available to ordinary training scripts. It caught on because the same operation served gradient averaging, and because GPU vendors built fast all-reduce libraries for it.

Recap

  • All-reduce gives every machine the same combined result, usually the sum of everyone's vector.
  • The ring version splits each vector into NN chunks and does reduce-scatter (each machine ends with one summed chunk), then all-gather (every machine collects all chunks).
  • Each machine sends about 2N−1NPs2 \frac{N-1}{N} P s bytes, so bandwidth cost stays near 2Ps2 P s as NN grows; latency grows with NN.
  • Data-parallel training uses all-reduce to average Gradients, and Model Parallelism uses it to combine partial results inside layers.

Read next: Model Parallelism, to see how the same communication appears when the model itself is split, and Megatron-LM, which uses all-reduce inside every transformer layer.

Check yourself

QuestionThree workers hold the numbers 2, 4 and 6 (one chunk each). What value does every worker hold after all-reduce with sum?Show answer

Every worker holds 2+4+6=122 + 4 + 6 = 12. Each worker must end with the same total, so the answer is 12 on all three.

QuestionWith N=8N = 8 workers and a gradient of Ps=400P s = 400 MB, how many MB does each worker send in a ring all-reduce (ignore latency)?Show answer

The factor is 2(N−1)N=148=1.75\frac{2(N-1)}{N} = \frac{14}{8} = 1.75. Then 1.75×400=7001.75 \times 400 = 700 MB per worker.

QuestionWhy does the ring use more message rounds than a tree for a very small message, yet still get chosen for large gradients?Show answer

The ring takes 2(N−1)2(N-1) rounds, each paying a fixed latency, while a tree takes about 2log⁡2N2 \log_2 N rounds. For a very small message, the fixed latency dominates, so fewer rounds win. For a large gradient, the transfer time dominates, and the ring's near-constant data per machine, about 2Ps2 P s, beats the tree, which sends more data over its busiest link.

QuestionA team averages gradients over 64 GPUs by calling an all-reduce with sum and then does not divide by 64. What happens to the learning rate's effective step, and how would you fix it?Show answer

The update uses the sum, which is 64 times the average. The step is 64 times larger than intended, so training can diverge unless the learning rate is reduced by a factor of 64. The fix is to divide the summed gradient by 64 (or use the framework's average option) so the update matches a single-device run on the same batch.

Sources