Search

Search papers and lessons

All-to-All Communication

Level 3 · 10 min read

All-to-all sends a different block of data from every machine to every other machine, so each machine ends up with the blocks addressed to it, with no numbers combined.

On this page

  • Colour = topic mix
  • Discrete math
  • Distributed systems
  • Networking
  • Programming languages
  • Systems
  • ReadReady to readNot yet

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 jj on machine ii is destined for machine jj. Use the value 10i+j10 i + j so each block is easy to track.

  1. Start state. Machine 0 holds [0,1,2][0, 1, 2], machine 1 holds [10,11,12][10, 11, 12] and machine 2 holds [20,21,22][20, 21, 22]. Block jj of machine ii goes to machine jj. Check: the three rows are the three machines' outgoing blocks.
  2. 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.
  3. 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.
  4. Assemble the received blocks. Each machine stacks what it has received in order of the sender. Machine 0 now holds [0,10,20][0, 10, 20], machine 1 holds [1,11,21][1, 11, 21] and machine 2 holds [2,12,22][2, 12, 22]. Check: the result is the original grid with rows and columns swapped.
  5. Cost check. Each machine sends 2 numbers out of its 3, which is N−1NP=23⋅3=2\frac{N - 1}{N} P = \frac{2}{3} \cdot 3 = 2 for N=3N = 3 machines and P=3P = 3 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 NN be the number of machines and PP the number of values each machine starts with, split into NN equal blocks of size P/NP / N. In an all-to-all, each machine keeps one block and sends the other N−1N - 1 blocks.

Step 5 in symbols: data sent per machine

Each machine sends (N−1)⋅PN(N - 1) \cdot \frac{P}{N} values, so the fraction of its data that leaves the machine is N−1N\frac{N - 1}{N}. This fraction approaches 1 as NN grows, so a machine's outgoing traffic stays close to PP regardless of how many machines there are.

Step 3 in symbols: total traffic

The whole group sends N⋅(N−1)⋅PN=(N−1)PN \cdot (N - 1) \cdot \frac{P}{N} = (N - 1) P 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 N−1N - 1 messages. A fixed cost per message, called latency, is therefore paid N−1N - 1 times. For large NN and small blocks, this latency can dominate the transfer time, which is why implementations group blocks or use hierarchical schedules.

Worked examples

Example
1. Tiny: the three-machine transpose

Use the setup from "How it works", where machine ii starts with the values 10i+j10 i + j for j=0,1,2j = 0, 1, 2.

  1. Machine 0 starts with [0,1,2][0, 1, 2], so it sends 11 to machine 1 and 22 to machine 2.
  2. Machine 1 starts with [10,11,12][10, 11, 12], so it sends 1010 to machine 0 and 1212 to machine 2.
  3. Machine 2 starts with [20,21,22][20, 21, 22], so it sends 2020 to machine 0 and 2121 to machine 1.
  4. Machine 0 keeps 00 and receives 1010 and 2020, so it holds [0,10,20][0, 10, 20].
  5. Interpretation: the result is the transpose of the grid of blocks. Machine jj now holds column jj of the original arrangement.
Example
2. Standard: routing tokens to experts

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.

  1. Probability that a token's expert is on another GPU: 34\frac{3}{4}, since three of the four GPUs are remote.
  2. Expected tokens leaving one GPU: 8⋅34=68 \cdot \frac{3}{4} = 6.
  3. Expected tokens staying on the GPU: 8−6=28 - 6 = 2.
  4. 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.
Example
3. Realistic: how long does a re-shard take?

This example uses an assumed link speed, not a measured one. Suppose each of N=8N = 8 GPUs holds P=1P = 1 gigabyte to re-shard, and each GPU can send at 100 gigabytes per second, all at once.

  1. Data each GPU sends: N−1NP=78⋅1=0.875\frac{N - 1}{N} P = \frac{7}{8} \cdot 1 = 0.875 gigabytes.
  2. Time at 100 gigabytes per second: 0.875/100=0.008750.875 / 100 = 0.00875 seconds, about 8.75 milliseconds.
  3. Interpretation: this is the bandwidth term only. Latency and congestion on a real network add to it, and the true time is larger.
  4. 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: 1516⋅1/100=0.009375\frac{15}{16} \cdot 1 / 100 = 0.009375 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 PP values. In fact the total traffic grows with (N−1)P(N - 1) P, and the number of messages per machine grows with N−1N - 1, 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.

NeedPatternEach machine ends with
Sum of everyone's valuesAll-reduceThe same total
Everyone's value on every machineAll-gatherEvery machine's piece
Distinct block for each machineAll-to-allThe 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 N−1N\frac{N - 1}{N} of its data, but the total traffic across the group is N−1N - 1 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 3⋅3=93 \cdot 3 = 9 values. The same answer comes from the formula N−1NP=34⋅12=9\frac{N - 1}{N} P = \frac{3}{4} \cdot 12 = 9.

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 N−1N - 1 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.