Phase 05/Moving bytes between GPUs and nodes/NCCL and GPU communication
PHASE 05 · Moving bytes between GPUs and nodes

NCCL and GPU communication

0%
8 h5 subtopics3 lessons · 2 required
In plain English

When GPUs share work they must constantly exchange numbers, and NCCL is the library that does the exchanging as fast as the wires allow. It knows the shape of the machine: which GPUs are joined by NVLink (a fast private link between GPUs in the same box), which only share the slower PCIe connection every expansion card uses, and which sit in another computer entirely; and it picks the best route and pattern for each message. Picture a group of friends passing notes around a table. If everyone needs the total of all the numbers, you can pass notes in a ring so each person adds their part, or form a tree so the result arrives faster. NCCL automates that choice. The limits of the wires, not the GPUs, often decide how fast a distributed model runs, which is why engineers obsess over bandwidth and latency numbers.

Why it matters

Every TP all-reduce and PP send in a serving engine goes through NCCL. Being able to estimate collective time from link bandwidth, read NCCL_DEBUG logs, and recognise a mis-configured network separates a guess from a diagnosis.

Hiring signal

Skills this topic builds, with the number of the 46 analysed job postings that ask for each. Companies asking: Anthropic, Baseten, Cerebras, Cohere, CoreWeave, Crusoe, Databricks, ElevenLabs, Fireworks, Liquid AI, Modal, nvidia, OpenAI, Perplexity, Thinking Machines, Together AI, xAI.See all skills →

Learning objectives

  • Name the main collectives (all-reduce, all-gather, reduce-scatter, broadcast, all-to-all) and state what each rank holds afterward.
  • Compute ring all-reduce time from tensor size, rank count, and link bandwidth, and distinguish latency-bound from bandwidth-bound regimes.
  • Read nvidia-smi topo -m and NCCL_DEBUG=INFO output to identify the transport in use.
  • Benchmark collectives and compare measured bus bandwidth with the hardware ceiling.

Key concepts

NCCL
NVIDIA Collective Communications Library; implements topology-aware collectives for GPUs over NVLink, PCIe, and network.
Ring versus tree algorithm
Ring is bandwidth-optimal for large tensors; tree has lower latency for small tensors and many ranks.
Bus bandwidth (busbw)
nccl-tests metric that normalises algorithm bandwidth so it can be compared directly with link speed.
NVLink / NVSwitch
Direct GPU-to-GPU links inside a node; 900 GB/s bidirectional per H100 with NVSwitch.
GPUDirect RDMA
Lets the network card read GPU memory directly, skipping a CPU copy, for inter-node transfers over InfiniBand or RoCE.
Latency floor
Fixed per-collective cost (tens of microseconds) that dominates decode-step collectives whose tensors are only kilobytes.
Reduce-scatter and all-gather
The two halves of an all-reduce; used separately in sequence parallelism and ZeRO-style sharding.

Subtopics

  1. 1. Collective operations

    Semantics of broadcast, reduce, all-reduce, all-gather, reduce-scatter, all-to-all; which parallelism uses which.

  2. 2. Ring all-reduce from first principles

    Reduce-scatter plus all-gather in 2(n-1) steps; each rank sends 2(n-1)/n of the data; time = latency * steps + bytes / bandwidth.

  3. 3. Hardware topology and transports

    NVLink, NVSwitch, PCIe switches, InfiniBand, RoCE; how NCCL discovers topology and chooses SHM, P2P, or NET transports.

  4. 4. Network bottlenecks in inference

    Small-tensor latency floor in decode, bandwidth saturation in prefill, head-of-line blocking from stragglers.

  5. 5. Debugging and tuning

    NCCL_DEBUG, NCCL_ALGO, NCCL_PROTO, NCCL_IB_HCA, NCCL_SOCKET_IFNAME; common hang and timeout causes.

Resources

Hands-on lessons

L1

Derive and plot all-reduce cost

CPU · your Macnotebook90 minrequired

Goal · A notebook that models ring and tree all-reduce time for a range of tensor sizes and link types and identifies the latency/bandwidth crossover.

  1. 1Write time_ring(bytes, n, bw_GBps, latency_us) = 2*(n-1)*latency + 2*(n-1)/n * bytes / bw.
  2. 2Write time_tree(bytes, n, bw, latency) using 2*log2(n) steps and bytes/bw per step as an approximation.
  3. 3Parameterise three links with NVLink (300 GB/s, 10 us), PCIe 4 x16 (25 GB/s, 20 us), and 100 GbE (12 GB/s, 50 us).
  4. 4Sweep tensor size from 1 KB to 1 GB on a log axis for n in {2, 4, 8} and plot time per link.
  5. 5Compute the per-decode-step collective bytes for Llama-3-70B TP=4 at batch 1 (2 * 80 layers * d_model * 2 bytes) and mark that point on the plot.
  6. 6Write a paragraph on which regime decode sits in and what that means for TP over Ethernet.
Deliverable
allreduce_cost.ipynb with the plot and the marked decode-step point.
Check
For a 1 GB tensor ring beats tree on every link; for 16 KB tensors tree beats ring at n=8; decode-step collectives on 100 GbE are latency-bound (more than 80 percent of time is latency).
L2

Implement ring all-reduce by hand on gloo

CPU · your Macscript120 minrequired

Goal · Implement reduce-scatter plus all-gather with only dist.send/dist.recv on 4 CPU processes and verify it equals dist.all_reduce.

  1. 1Create ring_allreduce.py; init gloo; split the input tensor into world_size chunks.
  2. 2Implement the reduce-scatter phase with world_size-1 steps where rank r sends chunk (r-step) to r+1 and receives from r-1, accumulating.
  3. 3Implement the all-gather phase with another world_size-1 steps that circulate the fully reduced chunks.
  4. 4Compare the result with torch.distributed.all_reduce on a copy using torch.equal (integers) and allclose (floats).
  5. 5Count bytes sent per rank and confirm 2(n-1)/n of the tensor size; time 100 iterations for 1 MB and 100 MB tensors with 2 and 4 processes.
  6. 6Launch with torchrun --nproc_per_node=4 ring_allreduce.py.
Deliverable
ring_allreduce.py and a table of (n, tensor_MB, bytes_sent_per_rank, time_ms).
Check
Result equals the built-in all_reduce; bytes per rank are exactly 2(n-1)/n times tensor size; the per-byte time for 100 MB is lower than for 1 MB (bandwidth regime).
L3

Run nccl-tests on a two-GPU instance

Rented multi-GPUcli90 minoptional

Goal · Measure all-reduce busbw across sizes on a real two-GPU node and compare it with the link speed printed by nvidia-smi topo -m.

  1. 1Rent a 2-GPU instance; run nvidia-smi topo -m and record the link type between GPU 0 and GPU 1 (NV#, PIX, PHB, SYS).
  2. 2Clone and build nccl-tests with make MPI=0; run ./build/all_reduce_perf -b 8 -e 1G -f 2 -g 2.
  3. 3Export the table and plot busbw versus size; find the size at which busbw reaches 80 percent of its plateau.
  4. 4Re-run with NCCL_ALGO=Ring and NCCL_ALGO=Tree and NCCL_DEBUG=INFO; note the chosen transport and algorithm lines.
  5. 5Overlay the measured curve on your l1 model and adjust latency and bandwidth parameters until they match.
Deliverable
A markdown report with the topo output, the busbw plot, and the fitted (latency, bandwidth) pair.
Check
Plateau busbw is within 30 percent of the nominal link bandwidth for the detected link type; small sizes (under 64 KB) show busbw under 10 percent of plateau.
Fallback
On the Mac, run the l2 ring_allreduce.py and also time torch.distributed.all_reduce on gloo for sizes 8 B to 256 MB with 2 and 4 processes; produce the same busbw-versus-size plot (busbw = bytes * 2(n-1)/n / time) and fit latency and bandwidth for the loopback interface.

Build your own

Build your own ring all-reduce

PythonCPU · your Mac~7 h

Reduce-scatter plus all-gather written with only point-to-point sends on gloo ranks, an alpha-beta cost model fitted to your own measurements, and a benchmark that prints algbw and busbw like nccl-tests.

The real thing: github.com/NVIDIA/nccl ↗

How you know a stage passes

Write tests/test_allreduce.py first. Tests launch `torchrun --nproc_per_node=4 ring.py --op <name> --size <n>` via subprocess with a 60 second timeout (a deadlock is a failure), then compare the tensors each rank saved with dist.all_reduce run in the same process. The cost model is a pure function tested against hand-computed values, and the benchmark CLI's output is parsed by a test. Run `pytest -q`.

Stages, in order. Each one leaves you with something that runs.0/6
  1. 01a ring of point-to-point messages
    Each rank splits its tensor into world chunks and, for world - 1 steps, sends one chunk to rank + 1 and receives one from rank - 1 using isend and irecv so no rank blocks on itself.
    test · After the loop every rank has received every other rank's chunk in the expected order; 4 ranks finish a 1 MB tensor without deadlock inside the timeout.
  2. 02reduce-scatter
    The ring accumulates so that after world - 1 steps rank r holds the fully reduced chunk r.
    test · Each rank's chunk equals the matching slice of dist.all_reduce on a 1,048,576-element float32 tensor within atol 1e-6 at world 2 and 4.
  3. 03all-gather and the complete ring all-reduce
    A second ring pass distributes the reduced chunks; ring_all_reduce(tensor) returns the full sum in place.
    test · Results equal dist.all_reduce for sizes from 1 KB to 64 MB; bytes sent per rank, counted in your send wrapper, equal 2 * (world - 1) / world * tensor bytes.
  4. 04the alpha-beta model fitted to measurements
    time(N) = 2 (w - 1) alpha + 2 (w - 1) / w * N / beta; alpha and beta are fitted by least squares to your ring's timings on sizes 1 KB to 16 MB.
    test · The fitted model predicts held-out sizes 32 MB and 64 MB within 25 percent; the latency-bandwidth crossover size (where the two terms are equal) is printed and lands between 64 KB and 4 MB on loopback.
  5. 05a tree alternative for small messages
    A recursive-halving reduce followed by recursive-doubling broadcast (or reduce-to-root plus broadcast) with log2(w) steps per phase.
    test · Tree beats ring on median time for 1 KB messages at 4 ranks and ring beats tree at 64 MB; the table of both across sizes is in the README.
  6. 06a benchmark in nccl-tests format
    `python bench.py -b 1K -e 64M -f 2` prints size, count, time, algbw and busbw columns for your ring, your tree and dist.all_reduce.
    test · The output parses into 17 rows; your ring's busbw at 64 MB is within 2x of gloo's built-in all-reduce; the README explains algbw versus busbw with your own numbers.

Knowledge check

QIn a ring all-reduce with 8 ranks, what fraction of the tensor does each rank send?

2 * 7/8 = 1.75 times the tensor size.

QWhy do decode-step collectives rarely saturate bandwidth?

Per-token activations are only kilobytes, so each collective is dominated by fixed latency rather than bytes over the wire.

QWhat does nvidia-smi topo -m output SYS mean for a pair of GPUs?

Traffic must cross the CPU interconnect between sockets; it is the slowest intra-node path, far below NVLink.

QWhich two collectives compose an all-reduce?

Reduce-scatter followed by all-gather.

QWhat is GPUDirect RDMA and why does it matter across nodes?

It lets the NIC read and write GPU memory directly, avoiding a host-memory copy and lowering latency and CPU load for inter-node collectives.

Completion

  • All 5 subtopics marked done
  • All 2 required lessons done
  • Lesson l1 notebook plot produced with the decode-step point marked.
  • Lesson l2 hand-rolled ring all-reduce matches the built-in and the byte count formula.
  • Can read a NCCL_DEBUG=INFO log and name the transport and algorithm.
Topic progress is derived; it reaches 100% when both checks above pass.Next recommended topic →