Across 32 GPUs, how much data does each rank send in NCCL's all-reduce, all-gather, reduce-scatter and all-to-all of a 400 MB buffer?
answer
- count what leaves each rank
- a family of (n-1)/n factors
- all-reduce is two halves
- reduce-scatter and all-gather mirror each other
- the factor nccl-tests applies to busbw
basics
~10 sWith S the full 400 MB array and n = 32, all-reduce sends 2(n-1)/n x S, about 775 MB per rank; reduce-scatter, all-gather and all-to-all each send (n-1)/n x S, about 387.5 MB.
solid answer
~40 sLet `S` be the full array (400 MB) and `n` the rank count (32). Reduce-scatter leaves each rank one reduced `S/n` shard (12.5 MB) and costs each rank `(n-1)/n · S`, about 387.5 MB sent; all-gather is the mirror image, from a 12.5 MB shard to the full 400 MB, at the same cost. All-reduce is a reduce-scatter followed by an all-gather, so `2(n-1)/n · S`, about 775 MB per rank, whichever algorithm NCCL uses. All-to-all sends 31 of each rank's 32 chunks to other ranks, again about 387.5 MB, but to every peer, so on four 8-GPU nodes most of it crosses the network. Divide by bus bandwidth to estimate time: at 25 GB/s an all-reduce of this buffer takes about 31 ms.
go deeper
Recall what each collective leaves on every rank: all-reduce gives everyone the sum, all-gather the concatenation, reduce-scatter one reduced shard, all-to-all a different chunk from each peer.
Explain the per-rank byte counts with the (n-1)/n factor, show all-reduce as reduce-scatter plus all-gather, and turn bytes into milliseconds with a bus bandwidth.
Use the arithmetic to sanity-check a step: if a measured collective is far slower than size over bus bandwidth times its factor, find which link it actually crossed before blaming the model.
Use per-collective volumes to compare plans and interconnects: which collectives a layout issues, how much of each crosses the inter-node fabric, and what that implies for the network you buy or share.
## The vocabulary A **collective** is a communication call that every rank in a group makes together. Take **n** ranks (here 32: four nodes of eight GPUs) and let **S** be the size of the *full* array, 400 MB in this example. The per-rank figures below are the **bandwidth-optimal minimum** each rank must send; nccl-tests' performance notes derive them independently of the algorithm, as long as the algorithm is built from point-to-point transfers, which ring and tree both are. ## What each collective moves | Collective | Input on each rank | Output on each rank | Sent per rank (n = 32, S = 400 MB) | nccl-tests busbw factor | |---|---|---|---|---| | All-reduce | S | S, the element-wise sum | 2(n-1)/n · S ≈ 775 MB | 2(n-1)/n | | Reduce-scatter | S | S/n = 12.5 MB, one reduced shard | (n-1)/n · S ≈ 387.5 MB | (n-1)/n | | All-gather | S/n = 12.5 MB | S, every shard in rank order | (n-1)/n · S ≈ 387.5 MB | (n-1)/n | | All-to-all | S as 32 chunks of 12.5 MB | 32 chunks, chunk i from rank i | (n-1)/n · S ≈ 387.5 MB | (n-1)/n | | Broadcast | S on the root | S | the root must push all S out | 1 | Two points the table hides: - **Reduce-scatter and all-gather are halves of all-reduce.** NCCL's guide states it directly: a reduce-scatter followed by an all-gather is equivalent to an all-reduce. That is why all-reduce costs twice either one, and why an engine can issue the two halves as separate calls at different points of a step. - **All-to-all reduces nothing.** It transposes: rank j's i-th chunk becomes rank i's j-th chunk. Its byte count matches all-gather's, but every rank exchanges a chunk with every other rank. ## Why all-reduce costs about twice the buffer, and stays there Each element must be summed across n inputs (n-1 additions) and the result written to n outputs. Spread over n ranks, that is `2(n-1)/n · S` per rank. The factor is 1.75 at 8 ranks and 1.9375 at 32, so per-rank **volume** is almost flat as the job grows. What does grow with n is the number of **steps**, and so the fixed latency per call, which is why small messages behave differently from large ones. ## Turning bytes into time The time for a large collective is roughly `S / busbw × factor`, where **busbw** is the per-rank bottleneck bandwidth nccl-tests reports. With an illustrative 25 GB/s: 1. All-reduce: 0.4 GB / 25 GB/s × 1.9375 ≈ 31 ms. 2. Reduce-scatter, all-gather or all-to-all: 0.4 GB / 25 GB/s × 0.96875 ≈ 15.5 ms. 3. Broadcast: 0.4 GB / 25 GB/s ≈ 16 ms, limited by the root's single link. These estimates hold for **large** buffers. For small ones the fixed cost of each step dominates, the formula underestimates the time, and the number of steps the algorithm takes matters more than the bytes. Use the bandwidth of the **slowest link the traffic crosses**. Inside one node that is NVLink or NVSwitch; across nodes it is the InfiniBand or RoCE adapter each GPU gets. ## Where the bytes travel On four 8-GPU nodes, all-to-all sends each rank's 31 outgoing chunks to their owners, and 24 of those owners sit on other nodes, so about 300 MB of each rank's 387.5 MB leaves the node through a network adapter. A ring all-gather or all-reduce sends to one neighbour per step, so most hops stay on NVLink and only the hops at node boundaries use an adapter; NCCL spreads several rings over channels and adapters so that the adapters share the load. ## Mistakes interviewers listen for - Saying an all-reduce sends "n copies" of the buffer from every rank. - Pricing all-gather like all-reduce, or reduce-scatter like broadcast. - Claiming tree moves fewer bytes than ring; the difference between them is latency. - Describing all-to-all as a reduction. - Using NVLink bandwidth to estimate a collective whose bytes cross nodes.
- Why does each rank's all-reduce volume barely change when the job grows from 8 to 32 ranks?The factor `2(n-1)/n` approaches 2 as n grows: 1.75 at 8 ranks, 1.9375 at 32. Each rank still sends a little under twice the buffer, so bandwidth cost per rank is nearly flat. What grows with n is the number of steps, and so the latency of each call, which matters most for small messages.
- How does all-to-all's traffic differ from all-gather's when the 32 ranks span four nodes?Both send `(n-1)/n · S` per rank, but all-to-all sends a distinct chunk to every peer. With 8 GPUs per node, 24 of each rank's 31 chunks go off-node, so about 300 MB of its 387.5 MB crosses an adapter and the inter-node links set the time. A ring all-gather sends to one neighbour per step, so most of its hops stay inside a node.
- An all-reduce sends n full copies of the buffer from every rank.
- All-gather costs as much per rank as all-reduce.
- Tree all-reduce moves fewer bytes per rank than ring all-reduce.
- All-to-all leaves each rank the sum of the other ranks' chunks.
- Doubling the GPU count doubles each rank's all-reduce traffic.