TL;DR
All-to-all is a communication pattern for a group of machines in a distributed system. Each machine holds a separate block of data for every other machine, and after the exchange each machine holds the blocks that were addressed to it. Nothing is added up or averaged, as it is in All-Reduce; the data is only rearranged across the group. It is the step that moves tokens to the right experts in a Mixture of Experts layer and that re-shards large arrays between machines.
Builds on: Asynchronous Message Passing · Leads to: Mixture of Experts, the most common modern use of this exchange
Where it's used
Routing tokens to experts. In a mixture-of-experts model, a router (a learned scorer that rates the experts for each token) chooses one or a few experts per token, and those experts live on different machines. The sparsely gated layer of Shazeer and colleagues (2017) sends each token to the machine that holds its expert and sends the result back. The Switch Transformer (Fedus and colleagues, 2021) simplified this to one expert per token, the switch routing idea, so each token makes one trip to an expert and one trip back.
Recommendation models with split tables. A recommender's embedding tables (one learned vector per user or item) can be too large for one GPU, so the tables are split across GPUs, a form of model parallelism. To look up a batch of user and item ids, each GPU asks the other GPUs for the rows it needs, and the rows come back in a single exchange. This is an all-to-all: each pair of GPUs swaps a different block.
Re-sharding large arrays. Scientific codes and some attention layers split a matrix by rows on one step and by columns on the next. Changing the split means every machine must send a piece of its rows to every other machine, and that is an all-to-all. The Message Passing Interface defines it as a standard collective operation, meaning one that every machine in the group takes part in.
How it works
Take three machines, numbered 0, 1 and 2. Each machine holds three blocks, and block on machine is destined for machine . Use the value so each block is easy to track.
- Start state. Machine 0 holds , machine 1 holds and machine 2 holds . Block of machine goes to machine . Check: the three rows are the three machines' outgoing blocks.
- Keep the local block. Each machine keeps the block addressed to itself. Machine 0 keeps 0, machine 1 keeps 11, machine 2 keeps 22. Check: these three values are the diagonal of the grid, and they never cross the network.
- Send the other blocks. Each machine sends one block to each of the other two. Machine 0 sends 1 to machine 1 and 2 to machine 2. Machine 1 sends 10 to machine 0 and 12 to machine 2. Machine 2 sends 20 to machine 0 and 21 to machine 1. Check: each machine sent two blocks, six blocks in total cross the network.
- Assemble the received blocks. Each machine stacks what it has received in order of the sender. Machine 0 now holds , machine 1 holds and machine 2 holds . Check: the result is the original grid with rows and columns swapped.
- Cost check. Each machine sends 2 numbers out of its 3, which is for machines and numbers each.
flowchart LR M0[Machine 0 blocks 0 1 2] -->|block 1| M1[Machine 1] M0 -->|block 2| M2[Machine 2] M1 -->|block 0| M0 M1 -->|block 2| M2 M2 -->|block 0| M0 M2 -->|block 1| M1
The math
Let be the number of machines and the number of values each machine starts with, split into equal blocks of size . In an all-to-all, each machine keeps one block and sends the other blocks.
Step 5 in symbols: data sent per machine
Each machine sends values, so the fraction of its data that leaves the machine is . This fraction approaches 1 as grows, so a machine's outgoing traffic stays close to regardless of how many machines there are.
Step 3 in symbols: total traffic
The whole group sends values across the network. Traffic grows with the number of machines, even though each machine's share does not.
Latency term
If every block is sent as its own message, each machine sends messages. A fixed cost per message, called latency, is therefore paid times. For large and small blocks, this latency can dominate the transfer time, which is why implementations group blocks or use hierarchical schedules.
Worked examples
Use the setup from "How it works", where machine starts with the values for .
- Machine 0 starts with , so it sends to machine 1 and to machine 2.
- Machine 1 starts with , so it sends to machine 0 and to machine 2.
- Machine 2 starts with , so it sends to machine 0 and to machine 1.
- Machine 0 keeps and receives and , so it holds .
- Interpretation: the result is the transpose of the grid of blocks. Machine now holds column of the original arrangement.
Four GPUs each hold 8 tokens. A router sends each token to one expert, and each expert lives on one GPU. Assume the router's choices are spread evenly, so each token is equally likely to go to any of the four GPUs.
- Probability that a token's expert is on another GPU: , since three of the four GPUs are remote.
- Expected tokens leaving one GPU: .
- Expected tokens staying on the GPU: .
- Interpretation: six of every eight tokens cross the network in the dispatch exchange and cross back in a second all-to-all after the expert runs. A layer therefore needs two all-to-all exchanges, and the slowest GPU sets the pace.
This example uses an assumed link speed, not a measured one. Suppose each of GPUs holds gigabyte to re-shard, and each GPU can send at 100 gigabytes per second, all at once.
- Data each GPU sends: gigabytes.
- Time at 100 gigabytes per second: seconds, about 8.75 milliseconds.
- Interpretation: this is the bandwidth term only. Latency and congestion on a real network add to it, and the true time is larger.
- Try it: the same re-shard runs on 16 GPUs, each holding 1 gigabyte and sending at 100 gigabytes per second. How long does the bandwidth term take? Answer: seconds, about 9.4 milliseconds.
Common mistakes and tradeoffs
It is tempting to think that all-to-all is just an All-Reduce with extra steps. In fact all-reduce combines the values, so every machine ends with the same sum, while all-to-all moves distinct blocks and combines nothing. Using one where the other is needed gives the wrong answer, not just a slower one.
It is tempting to think that the cost grows only slowly with the number of machines because each machine sends about values. In fact the total traffic grows with , and the number of messages per machine grows with , so latency becomes the limit at large scale.
It is tempting to think that sparse models such as mixture-of-experts models avoid communication. In fact the tokens are exchanged every layer, twice, and an uneven split of tokens across experts makes one machine the slowest, which stalls the rest.
When to use it. Data has to be redistributed so that each piece goes to a different machine: routing to experts, re-sharding a matrix, or swapping embedding rows.
When not to use it. Every machine needs the same combined result, which is a reduction, so use All-Reduce. Only one machine needs the data, so a point-to-point message (one machine to one machine) or a broadcast (one machine sends the same data to all the others) is cheaper.
| Need | Pattern | Each machine ends with |
|---|---|---|
| Sum of everyone's values | All-reduce | The same total |
| Everyone's value on every machine | All-gather | Every machine's piece |
| Distinct block for each machine | All-to-all | The blocks addressed to it |
History
The Message Passing Interface, first published in 1994, defined a standard set of collective operations for parallel computers, including the all-to-all exchange, and it became the vocabulary for these patterns in scientific computing. The operation was then mostly used in high-performance computing until deep learning needed it. Shazeer and colleagues' 2017 mixture-of-experts layer made the exchange a regular part of neural network training, and the Switch Transformer in 2021 cut the routing to one expert per token to keep that exchange simpler.
Recap
- All-to-all sends a different block from every machine to every other machine, and combines nothing.
- Each machine sends a fraction of its data, but the total traffic across the group is times one machine's data, so it grows with the group size.
- Mixture-of-experts layers need two all-to-all exchanges per layer, to dispatch tokens and to return results.
- Read next: Mixture of Experts for how the router picks experts, and All-Reduce for the pattern that combines values instead.
Check yourself
QuestionRecall: how does all-to-all differ from all-reduce in what each machine ends with?Show answer
In an all-reduce every machine ends with the same combined value, such as the sum of all the inputs. In an all-to-all each machine ends with the distinct blocks that other machines addressed to it, and no values are combined.
QuestionFour machines each hold 12 values and split them into four blocks of three. In an all-to-all, how many values does each machine send?Show answer
Each machine keeps one block and sends the other three, so it sends values. The same answer comes from the formula .
QuestionWhy does a mixture-of-experts layer usually need two all-to-all exchanges rather than one?Show answer
The first exchange sends each token to the machine that holds its chosen expert. After the expert runs, the outputs must return to the machines the tokens came from, which needs a second exchange in the reverse direction.
QuestionA group of machines has an all-to-all that is slow even though each machine sends a modest amount of data. What is a likely cause that the bandwidth formula misses?Show answer
Latency. Each machine sends separate messages, and a fixed cost is paid for each. With many machines and small blocks, that per-message cost can dominate the time. Grouping blocks into fewer, larger messages is a common fix.
Sources
- Shazeer et al., "Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer", 2017 (arXiv:1701.06538).
- Fedus et al., "Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity", 2021 (arXiv:2101.03961).
- Message Passing Interface, Wikipedia.