skip to content

How does NCCL choose between ring and tree for an all-reduce, and when, if ever, should you set NCCL_ALGO or NCCL_PROTO?

level: seniorimportance: should knowfreq 9%

answer

  1. a cost model per call
  2. steps against log of nodes
  3. chain inside the node, tree across
  4. LL, LL128 and Simple
  5. filed under the guide's debugging variables

basics

~20 s

NCCL models each algorithm and protocol's latency and bandwidth for the message size and detected topology, and picks the fastest: tree often wins small all-reduces across many nodes, ring usually large ones. Override NCCL_ALGO or NCCL_PROTO only to experiment or debug.

solid answer

~50 s

NCCL has no fixed rule: for each collective it estimates time as latency plus size over bandwidth for every enabled algorithm and protocol, using the topology it detected, and takes the cheapest. A ring passes chunks around all n ranks, so it keeps every link busy but its step count grows with n. NCCL's tree runs a chain inside each node and a double binary tree across nodes, so its latency grows with log2 of the node count; NCCL uses it for all-reduce only. That favours tree for small and medium all-reduces on many nodes and ring for large ones. Protocols trade the same way: `LL` has the lowest latency at about half the bandwidth, `LL128` keeps most of the bandwidth where the platform supports it, `Simple` serves large messages. NCCL 2.32's guide files `NCCL_ALGO` and `NCCL_PROTO` under debugging: use them to compare runs or exclude a suspect choice, not as production settings.

go deeper

for a junior

Recall that NCCL has more than one way to run an all-reduce, ring and tree, and picks one automatically for each call.

for a middle

Explain ring's 2(n-1) steps against tree's log-of-nodes latency, and what LL, LL128 and Simple trade between latency and bandwidth.

for a senior

Show how you would prove the tuner wrong: TUNING logs, an nccl-tests sweep with each algorithm forced, and a scoped override such as allreduce:tree rather than a global one.

for a principal

Treat overrides as debt: a forced algorithm or protocol in a launcher outlives the NCCL release it was tuned for, so decide who re-checks it after each upgrade.

## Two separate choices: algorithm and protocol For every collective NCCL picks an **algorithm**, the pattern in which ranks pass data, and a **protocol**, the way each chunk is written and signalled. In NCCL 2.32, `NCCL_ALGO` accepts `Ring`, `Tree`, `CollnetDirect`, `CollnetChain`, `NVLS`, `NVLSTree` and `PAT`; `NVLS` and `NVLSTree` offload reductions to NVLink SHARP on NVSwitch systems, and the CollNet variants use in-network reduction. `PAT` serves all-gather and reduce-scatter only, so on an InfiniBand or RoCE cluster without NVLink SHARP or in-network reduction, an all-reduce comes down to ring against tree. `NCCL_PROTO` accepts `LL`, `LL128` and `Simple`. ## How the ring works and what it costs A **ring** orders the n ranks in a cycle. An all-reduce runs as a reduce-scatter pass then an all-gather pass, each rank sending one chunk to its neighbour per step, for **2(n-1) steps**. Every link carries data at every step, so for large buffers the ring is bandwidth-optimal. The cost is latency: on four 8-GPU nodes an all-reduce takes 62 steps, 6 of which cross between nodes, and each step pays a fixed overhead whatever the message size. ## NCCL's tree NCCL's **tree** is hierarchical: a chain through the GPUs inside each node, and a **double binary tree** across nodes, the second a mirror of the first, so that the nodes that are leaves in one tree carry the interior work in the other and the load is shared. Its latency model in NCCL 2.32 is roughly two passes of (GPUs per node - 1) intra-node hops plus log2(nodes) inter-node hops: for four 8-GPU nodes, 2 x (7 + 2) hop latencies instead of the ring's 62 steps. Per-rank volume is the same `2(n-1)/n` of the buffer as the ring; the win is fewer latency-bound steps. The tree serves **all-reduce only**; other collectives use ring and the remaining algorithms. ## Protocols | Protocol | What it does | Best for | |---|---|---| | `LL` | Writes small units with a flag beside the data, so receivers poll memory instead of waiting on a separate signal | Small messages; about half the bandwidth | | `LL128` | The same idea in 128-byte lines, about 120 of them payload | Medium and large messages, only on platforms that support it | | `Simple` | Plain chunks with separate synchronization | Large messages, full bandwidth, highest latency | ## How NCCL decides, and how to see the decision NCCL detects the topology at startup (NVLink, PCIe switches, adapters, NUMA) and builds rings and trees over it. For each call it evaluates every allowed algorithm and protocol pair with its cost model and picks the fastest, so one job can run tree with `LL` for a 1 MB all-reduce and ring with `Simple` for a 1 GB one. To see it: - `NCCL_DEBUG=INFO` with `NCCL_DEBUG_SUBSYS=TUNING` logs each collective's byte count with the algorithm and protocol chosen. - `NCCL_DEBUG_SUBSYS=GRAPH` shows topology detection and graph search; `NCCL_TOPO_DUMP_FILE` writes the detected topology as XML. - An nccl-tests sweep, `all_reduce_perf -b 8 -e 8G -f 2`, run once as is and once each with ring and tree forced, shows where the curves cross. ## When to set them yourself 1. **Experiments**: forcing each algorithm in a benchmark to check the tuner's choice on your fabric. 2. **Suspected bugs**: excluding one algorithm or protocol, for example `allreduce:^tree` or `^LL128`, while you confirm a fault. 3. **Never as a standing production setting.** NCCL's guide warns that forced values stop NCCL from choosing the best setting as releases change, and that enabling `LL128` on platforms that do not support it can corrupt data. ## The syntax change in 2.24 Since NCCL 2.24 both variables take per-collective lists such as `"ring,collnetdirect;allreduce:tree,collnetdirect;broadcast:ring"`, and NCCL no longer falls back to ring implicitly: if a collective has no valid algorithm left, NCCL fails. A global `NCCL_ALGO=tree` therefore leaves all-gather with nothing to run; `allreduce:tree` scopes the override to all-reduce and leaves the other collectives on automatic selection. A leading `^` inverts a list, so `allreduce:^tree` keeps every algorithm except tree for all-reduce, and `NCCL_PROTO` takes the same per-collective form. Parsing is strict as well: since 2.24 an unexpected token in either variable makes NCCL warn and fail, so a typo in a launch script stops the job.

  • You set NCCL_ALGO=tree for the whole job and the all-gathers now fail. Why?
    Since NCCL 2.24, `NCCL_ALGO` no longer falls back to ring implicitly: a collective with no valid algorithm in the list fails. NCCL's tree serves all-reduce only, so all-gather is left with nothing. Scope the override with `allreduce:tree`, or exclude instead of forcing with `allreduce:^tree`.
  • How do you see which algorithm and protocol NCCL actually used for a given message size?
    Run with `NCCL_DEBUG=INFO` and `NCCL_DEBUG_SUBSYS=TUNING`: NCCL logs each collective's byte count with the algorithm and protocol it picked. Pair that with an nccl-tests sweep such as `all_reduce_perf -b 8 -e 8G -f 2`, repeated with ring and with tree forced, and compare where the curves cross.

A ring is a bucket brigade around a circle: every pair of hands is busy, so throughput is high, but each bucket passes every person, so adding people adds delay. A tree is a phone tree: a message reaches a large group in a few rounds, which matters most when the message is short.

  • Ring always beats tree because it keeps every link busy.
  • Tree all-reduce sends fewer bytes per rank than ring.
  • Forcing LL128 everywhere is a safe bandwidth win.
  • NCCL_ALGO=tree switches every collective to the tree algorithm.
  • NCCL picks one algorithm for the whole job at startup.

Assumes NCCL 2.32: NCCL_ALGO accepts Ring, Tree, CollnetDirect, CollnetChain, NVLS, NVLSTree and PAT, and since 2.24 takes per-collective lists with no implicit fallback to ring.