IIntroduction to AI Systems Performance EngineeringTechnical textbook
Chapter 13: Network Topology and Collective Communication
13.3

Collective communication algorithms

Simulate ring reduce-scatter and all-gather round by round and calculate sent, received, and reduced bytes per rank

Before you begin

Follow a linked prerequisite when you cannot explain it from memory. Background skills without links are stated as abilities rather than hidden terminology.

  • Previous section's startup-plus-byte model
  • Ability to add corresponding vector elements
  • Chapter introduction's definitions of rank, communicator, and process group

Four training processes finish backward with four local copies of the same eight-element gradient vector. Rank 0 holds [1, 1, …], rank 1 holds [2, 2, …], rank 2 holds [3, 3, …], and rank 3 holds [4, 4, …]. Before every replica applies an equivalent optimizer update, each process needs the elementwise sum [10, 10, …]. No one process should become a permanent central server for the operation.

NCCL and PyTorch call this group operation an all-reduce: all participating ranks contribute an input, a reduction operation combines corresponding elements, and all ranks receive the complete result. A rank is one participating process in the communicator. A communicator stores the group membership and communication context needed for those processes to execute compatible operations.

A PyTorch Profiler or Nsight Systems capture records a collective event on every rank, but one rectangular event bar hides the internal movement. To expose the mechanism, draw one row per rank and one column per communication round. Label which two-element chunk moves to the next rank, which partial sum is updated, and which rank owns each completed chunk. The drawing can be checked by simulating the vector values after every round.

A ring all-reduce first performs reduce-scatter, which combines inputs while leaving one completed chunk on each rank. All-gather then circulates those completed chunks until every rank has the full vector. Tree algorithms arrange the same group contract with a different dependency depth. The concrete schedule determines round count, payload bytes, link use, and the signature visible in a communication benchmark.

Identify the collective contract

A broadcast begins with one source buffer and finishes with a copy on every rank. A reduce combines values at one destination. An all-reduce combines values and returns the complete result to every rank. An all-gather begins with one shard per rank and returns every shard to every rank. A reduce-scatter combines full logical inputs and leaves one reduced shard per rank.

An all-to-all is different: each rank sends a distinct portion to every destination. Its traffic depends on the complete rank-pair pattern and can stress bisection bandwidth. Mixture-of-experts dispatch commonly uses this operation because each token can route to an expert on another rank.

Derive equal-split all-to-all traffic

Let each of P ranks begin with N bytes divided equally among P destinations. One N/P-byte slice is already at its destination and needs no network transfer. The rank sends the other P−1 slices, so its off-rank injection volume is (P−1)N/P bytes and its receive volume is equal. As P grows, the off-rank volume approaches N per rank even though each peer message becomes smaller.

This byte count does not predict time by itself. All ranks communicate with many peers, and routes can converge on the same NIC, switch uplink, or fabric cut. A pairwise link can be fast while the all-to-all is limited by endpoint message concurrency or bisection bandwidth. For expert routing, replace N/P with the actual per-destination token counts and include packing, padding, and capacity overflow.

Derive four-rank ring reduce-scatter

Let ranks R0 through R3 each hold chunks [0,1,2,3]. Arrange them in the ring R0→R1→R2→R3→R0. In every round, each rank sends one chunk to its next neighbor and receives one from its previous neighbor. The receiving rank reduces the values into the corresponding local partial result.

After P−1 = 3 rounds, each rank owns one fully reduced chunk. The ownership index depends on the schedule. Every rank sent three chunks of size N/4, so sent volume is 3N/4. Receive volume is equal. The full reduced tensor is not yet replicated.

Derive ring all-gather

Begin with the reduced chunk owned by each rank. In round one, every rank sends its owned chunk to the next neighbor and stores the received chunk. In the next rounds, it forwards chunks that other ranks still need.

After another P−1 rounds, every rank holds all P reduced chunks. Each rank again sent (P−1)N/P bytes. Combining phases gives 2(P−1) rounds and 2(P−1)N/P sent bytes per rank.

Compare ring and tree depth

The ring's dependent round count grows linearly with P. For large buffers, every link can carry one chunk per round and sustain an efficient pipeline. The per-rank byte volume approaches 2N as P becomes large.

A balanced tree has approximately log2(P) levels for a reduction and another log2(P) for a broadcast. The tree pays fewer startup stages for small messages. Link use and per-node load depend on the implementation and physical mapping, so the crossover must be measured on the target topology.

Use hierarchy for nonuniform topology

A flat algorithm assumes comparable links among ranks. Multi-node servers usually have fast local GPU fabrics and slower cross-node network links. Let each node contain G ranks and let there be Q nodes. One bandwidth-balanced hierarchical plan first reduce-scatters the N-byte tensor within each node, so each local rank owns an N/G-byte partial shard. Corresponding local ranks then all-reduce those shards across Q nodes. A local all-gather reconstructs the complete result inside each node.

During the global phase, each rank sends 2(Q−1)/Q × N/G bytes under the ideal ring model. Summed over G local ranks, one node sends 2(Q−1)N/Q bytes across the network. This calculation separates constrained network traffic from the two local phases. Actual NIC and rail mapping determines whether the G cross-node groups use independent paths or contend for one port.

A leader-based hierarchy is different. Local ranks reduce to one leader, leaders exchange a full N-byte result, and leaders broadcast locally. The leader design can reduce the number of network participants but concentrates endpoint traffic and memory work. Do not refer to both designs as one hierarchical byte count; draw ownership and calculate the selected schedule.

Include implementation details

Libraries divide large buffers into chunks and channels. Multiple channels use parallel paths or endpoints. Protocols select payload granularity and synchronization behavior. Reduce-scatter also performs local arithmetic and memory traffic.

The ideal byte and round model predicts broad behavior. The model does not specify actual channels, protocol, routing, or overlap. Compare the model with library logs, completed-payload bandwidth, bus-bandwidth conventions, and rank timelines before assigning a cause to the difference.

Draw a rank timeline

For a small rank count, write one row per rank and one column per dependent round. Label the chunk sent, chunk received, local reduction, and resulting ownership. This exposes whether a claimed schedule sends a chunk before it exists, uses a link twice in one round, or finishes without every rank owning the promised result.

Then map every logical edge to the physical route. Two ring edges that look independent can share one NIC, PCIe switch, or fabric cut. The logical timeline supplies dependency depth; the route map supplies contention. Both are needed to predict elapsed time.

Worked example: Modeling a ring all-reduce

Eight ranks all-reduce a 1 GiB gradient buffer. Begin with an ideal uniform bidirectional ring, then examine a topology containing two four-GPU nodes.

  1. Divide the buffer into eight 128 MiB chunks. Reduce-scatter uses seven rounds, and every rank sends one 128 MiB chunk in each round: 896 MiB per rank.
  2. All-gather uses seven more rounds and sends another 896 MiB per rank. Total sent volume is 1,792 MiB, or 1.75 GiB. Each rank receives the same volume.
  3. Use the alpha–beta model: 14 startup stages plus 1.75 GiB divided by the effective steady ring rate, with an additional local reduction term for the first seven stages.
  4. Now place ranks 0–3 on node A and ranks 4–7 on node B in one contiguous logical ring. Two directed ring edges cross between nodes. Each node sends one 128 MiB chunk across its outgoing network edge in each of 14 rounds, for 1.75 GiB sent across the network per node under this mapping.
  5. Construct a hierarchical alternative. A four-rank local reduce-scatter leaves one 256 MiB shard on each local rank. Each corresponding pair across the two nodes all-reduces 256 MiB, which sends 256 MiB per rank under the two-rank ring formula. Aggregate network send volume is therefore 4 × 256 MiB = 1 GiB per node, followed by a local all-gather. Include the extra local phases and actual rail contention in the time comparison.
  6. Benchmark both at the target message size. Inspect NCCL's selected algorithm and actual rank mapping, and compare transfer after the last arrival so upstream skew does not contaminate the collective estimate.

Conclusion: The decomposition explains why a ring can move more than one buffer per rank yet remain efficient: every link carries a steady fraction while work is distributed.

Common errors

  • Calling every group synchronization an all-reduce. The collective contract determines bytes, ownership, and dependencies.
  • Counting only one phase of ring all-reduce. Reduce-scatter and all-gather each use P−1 rounds.
  • Applying the flat uniform-link model to a hierarchical cluster without counting network crossings and shared cuts.
  • Selecting ring or tree only from message size. Topology, rank count, protocol, and implementation determine the measured crossover.

Reference summary

A collective defines what every rank must receive, not how the library moves the data. In a ring all-reduce, each rank divides an N-byte tensor into P chunks. During P−1 reduce-scatter rounds, every rank sends one N/P-byte chunk and reduces one received chunk. During P−1 all-gather rounds, every rank sends one completed N/P-byte chunk until all ranks hold the full reduced tensor.

ring all-reduce bytes sent per rank = 2(P−1)N/P
The ideal ring has 2(P−1) dependent rounds. Each rank sends N/P bytes in every round. Receive volume is equal and local reduction work occurs during reduce-scatter.
equal-split all-to-all off-rank bytes sent per rank = (P−1)N/P
Each rank retains its N/P local destination slice and sends one N/P slice to each of the other P−1 ranks. Uneven routing requires the actual destination counts.
  • Ring algorithms use many rounds but distribute traffic evenly and can sustain high bandwidth for large buffers.
  • Tree algorithms reduce the dependency depth to approximately logarithmic in the rank count, which can reduce latency for smaller messages.
  • Hierarchical algorithms perform local phases on fast intra-node links and a smaller global phase across the network.
  • Reduce-scatter combines corresponding inputs and leaves one reduced shard on each rank, so a sharded downstream consumer can avoid a full replicated result.
  • All-gather performs the opposite ownership transition: it starts with one shard per rank and reconstructs the full logical tensor on every rank, increasing transient replication when every consumer needs the complete value.
  • All-to-all sends a distinct portion to every peer and can stress endpoint concurrency and fabric bisection bandwidth.
  • All ranks in a communicator must issue compatible collectives in a compatible order. A mismatch prevents the algorithm from completing.

Knowledge check

Why can a hierarchical all-reduce outperform one flat ring on multi-node eight-GPU servers?

Show the answer guide
  • A hierarchical schedule can perform high-volume reduction and gathering on fast intra-node GPU links while moving smaller shards or using fewer constrained exchanges across the inter-node network.
  • The benefit depends on the selected hierarchy: a bandwidth-balanced reduce-scatter/global-all-reduce/all-gather schedule has different participants and network bytes from a leader-based reduce/broadcast schedule.
  • The hierarchy wins only when saved network time exceeds extra local phases, startup, reduction work, endpoint contention, and arrival skew on the measured topology.
Practice

Simulate flat and hierarchical all-reduce

For eight ranks arranged as two four-GPU nodes, simulate a flat ring and one explicitly chosen hierarchical schedule on a small vector with distinct values. Calculate local and network bytes separately.

Evidence to keep: Round-by-round ownership tables, verified final reduction, sent and received bytes per rank and node, dependent round counts, and a route-aware predicted crossover.

Real systems rely on NCCL and related libraries to select and execute these algorithms. Diagnosis requires connecting library choices to topology and application order.

References

Sources and further reading

The lesson explanations synthesize primary papers and official references. Follow the originals for proofs, exact APIs, architecture details, and version-sensitive behavior.

  1. NCCL User Guide
  2. GPUDirect RDMA
  3. PyTorch Distributed