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.
- Start state. Worker 0 holds , worker 1 holds , worker 2 holds . The goal is for every worker to end with the sum . Check: , , .
- 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 . Worker 1 sends chunk 1 (value 5) to worker 2, which becomes . Worker 2 sends chunk 2 (value 9) to worker 0, which becomes . Check: each worker sent exactly one number.
- Round 2. Worker 0 sends chunk 2 (value 12) to worker 1, which becomes . Worker 1 sends chunk 0 (value 5) to worker 2, which becomes . Worker 2 sends chunk 1 (value 13) to worker 0, which becomes . 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.
- 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 .
- Cost check. Each worker sent 4 numbers in total, which is sends with . Its vector has numbers, so the total is .
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
Let there be workers, worker holding a Vector of numbers. All-reduce with sum leaves every worker holding the same vector , where is summation notation for adding the vectors. The sum is element by element, so the -th entry of is the sum of the -th entries of all the .
Step 1 in symbols
Split each vector into chunks of length . Write for chunk of worker , with chunks indexed . The target for chunk is . In the example, chunk 0 of worker 0 is the number 1, and .
Step 2 and 3 in symbols (reduce-scatter)
In a ring, after rounds, worker holds the complete sum of chunk . In the example with , 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 rounds copy each finished chunk around the ring until every worker holds all entries of . No addition happens in this half.
The cost in symbols
Let be the fixed delay of one message (latency) and the number of bytes per second a link carries (bandwidth). Let be the bytes per number. The ring takes message rounds, and each worker sends bytes in total, so The second term does not grow with once is large, because approaches 1. The first term grows linearly with .
Worked examples
Setup: worker 0 holds and worker 1 holds . The goal is for both to hold the sum.
- Worker 0 sends its number, 3, to worker 1.
- Worker 1 sends its number, 5, to worker 0.
- Worker 0 adds the received 5 to its own 3, getting .
- Worker 1 adds the received 3 to its own 5, getting .
- Both workers now hold .
Interpretation: with two workers, the ring is just a swap followed by an addition. Each worker moved one number and did one addition.
Setup: Gradient entries stored as 16-bit numbers, so bytes and the gradient is MB. There are workers linked at 100 gigabits per second, which is 12.5 GB/s.
- Naive approach: every worker sends its full gradient to each of the other 3 workers. That is MB sent per worker.
- Time for the naive approach: s, or 168 ms.
- Ring approach: each worker sends MB.
- Time for the ring: s, or 84 ms.
- Interpretation: the ring halves the time here, and the gap grows as grows, because the naive cost is proportional to and the ring cost stays near MB. The latency term is ignored in this estimate; for a 700 MB message it is small.
Setup: ResNet-50 has about 25.6 million parameters. Gradients are stored as 32-bit numbers, so bytes, and the gradient is MB. There are GPUs, linked at 25 GB/s. Assume one local forward and backward pass takes 50 ms (an assumption for this example, not a measurement).
- Ring factor: .
- Data sent per GPU per step: MB.
- Transfer time: s, about 7.2 ms.
- Latency term: message rounds. With a per-message delay of about 10 microseconds, that adds about 0.14 ms, which is small.
- 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.
- 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 , the data is MB, and the transfer time is s, about 7.7 ms. It is slightly more than the 8-GPU case, because the factor creeps toward 2 as 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 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 bytes, and the latency term grows with . For a small message on many machines, latency dominates, and a tree-shaped all-reduce, which takes about 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 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 chunks and does reduce-scatter (each machine ends with one summed chunk), then all-gather (every machine collects all chunks).
- Each machine sends about bytes, so bandwidth cost stays near as grows; latency grows with .
- 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 . Each worker must end with the same total, so the answer is 12 on all three.
QuestionWith workers and a gradient of MB, how many MB does each worker send in a ring all-reduce (ignore latency)?Show answer
The factor is . Then 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 rounds, each paying a fixed latency, while a tree takes about 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 , 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
- Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour (Goyal et al., 2017)
- Horovod: fast and easy distributed deep learning in TensorFlow (Sergeev and Del Balso, 2018)
- Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism (Shoeybi et al., 2019)
- Message Passing Interface (Wikipedia)