# Beyond One GPU: Tensor, Pipeline and Expert Parallelism, and Split Prefill and Decode

Author: Ishwar Jangid
Canonical URL: https://ishwarj.com/writings/llm-inference-7-beyond-one-gpu/
Updated: 2026-10-11
Source: https://github.com/ishwar6/ishwar-books/blob/main/content/writings/llm-inference-7-beyond-one-gpu.md


Llama 3.1 70B stores its weights in 141 GB. A widely used data-centre GPU, the NVIDIA H100, has 80 GB of memory. The model does not fit, and nothing clever inside one GPU changes that.

So we split the model across several GPUs. The moment we do, a new cost appears that the first six parts of this series could ignore: **the GPUs have to talk to each other**, many times for every single token. How often they talk, how much they send, and how fast the wire between them is, decides almost everything about how a model should be spread out.

This part builds the whole picture from the ground up:

1. **Why one GPU is not enough**: the memory arithmetic for an 8B, a 70B, a 405B and a 671B model, and the speed limit on decode.
2. **The wires**: how fast GPUs can talk inside one server and between servers, and a simple model of what a message costs.
3. **Tensor parallelism**: split every layer across GPUs. We run a real model this way, in two separate processes that only share numbers through a sum, and check it gives the same answer.
4. **Collectives**: all-reduce, all-gather and all-to-all, the few ways a group of GPUs exchange data, with the cost formula worked out.
5. **Pipeline parallelism**: give each GPU a slice of the layers instead.
6. **Expert parallelism** for mixture-of-experts models, with **real routing decisions** recorded from a 64-expert model.
7. **Disaggregated prefill and decode**: run the two phases from [Part 1](https://ishwarj.com/writings/llm-inference-1-prefill-and-decode/) on different GPUs, and move the KV cache between them.
8. **The real systems** that do this today, and the flags you would set.
9. **How to choose** a layout, with worked examples.

> [!NOTE] Three kinds of numbers in this article
> Every number carries its source.
> **Measured:** run on the machine used throughout this series, an Apple M5 Pro (64 GB), with PyTorch. That includes a real tensor-parallel run of Qwen2.5-0.5B across separate processes on the CPU, timings of matrix shards on the Apple GPU (`mps`), and the expert choices of the real OLMoE-1B-7B model on real text. There is no NVIDIA GPU here, so **no multi-GPU speed was measured**. Its timings are used for shapes and ratios, never as H100 numbers.
> **Arithmetic and simulation:** model sizes, bytes and times computed from published shapes and NVIDIA's spec-sheet peaks, plus small simulators in plain Python. Real systems are slower than peak; where an input is an assumption (like the fixed cost of one message), it is named and varied.
> **Reported:** numbers from papers (Megatron-LM, GPipe, the Llama 3 report, DeepSeek-V3, DistServe, Splitwise, Mooncake) and from official documentation, each with its hardware.
> Engine flags were checked in the vLLM and SGLang documentation on 11 October 2026 (vLLM `187a0eb`, SGLang `f9cee8d`).

This part builds on [Part 6](https://ishwarj.com/writings/llm-inference-6-serving-in-production/), which covered serving on one GPU per copy of the model: latency targets, goodput, queueing, capacity planning and routing between copies. Here we look inside a copy that is too big for one GPU.

## 1. Why one GPU is not enough

### Memory: weights plus the KV cache

A GPU must hold two big things while it serves a model:

- the **weights**, read on every forward pass ([Part 1](https://ishwarj.com/writings/llm-inference-1-prefill-and-decode/));
- the **KV cache**, the saved keys and values of every token of every conversation in flight ([Part 2](https://ishwarj.com/writings/llm-inference-2-kv-cache/)).

> [!DEFINITION] HBM (GPU memory)
> High Bandwidth Memory: the stacked memory chips packaged right next to a data-centre GPU. An H100 SXM has 80 GB of it, read at up to 3.35 TB/s. Everything the GPU computes on must come from here.

The weight size is the number of parameters times the bytes per parameter. The KV cache size is the formula from Part 2:

$$
M = N\,b_w \;+\; \underbrace{2\,L\,H_{\mathrm{KV}}\,d_h\,b_{kv}}_{\text{bytes per token}}\;\times\;S\times C
$$

where:

- $$N$$ is the number of parameters and $$b_w$$ the bytes per parameter (2 for BF16, 1 for FP8);
- $$L$$ is the number of layers, $$H_{\mathrm{KV}}$$ the number of key-value heads and $$d_h$$ the size of each head;
- the 2 counts keys and values, and $$b_{kv}$$ is the bytes per stored number (2 for BF16);
- $$S$$ is the number of conversations being served at once and $$C$$ the tokens in each one.

**Worked example: Llama 3.1 70B.** The Llama 3 report gives 80 layers, 8 KV heads and a model width of 8,192 (64 heads of 128). Counting every matrix gives $$N = 70.6$$ billion, so the weights take $$70.6\times10^9\times 2 = 141$$ GB. One token of KV cache is $$2\times80\times8\times128\times2 = 327{,}680$$ bytes, 320 KiB. Serve 32 conversations of 8,192 tokens and the cache is $$320\ \text{KiB}\times32\times8192 = 80$$ GiB. Together that is 227 GB. With about 90% of each 80 GB GPU usable (the rest goes to the CUDA runtime, activations and fragmentation), that needs **four** H100s.

The same arithmetic for the other models:

<figure class="fig"><svg viewBox="0 0 760 300" role="img" aria-label="Weights plus the KV cache of 32 conversations of 8,192 tokens: Llama 3.1 8B 50 GB fits one H100; 70B needs 227 GB, four H100s; 405B in BF16 needs 947 GB, more than one 8-GPU node; DeepSeek-V3 in FP8 needs 689 GB, ten H100s."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="s1" x="190.0" y="12.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="208.0" y="22.0" text-anchor="start">weights</text><rect class="s2" x="266.4" y="12.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="284.4" y="22.0" text-anchor="start">KV cache: 32 chats x 8,192 tokens</text><text class="t-note" x="180.0" y="61.0" text-anchor="end">Llama 3.1 8B</text><text class="t-tick" x="180.0" y="78.0" text-anchor="end">BF16 weights</text><g class="mark"><title>Llama 3.1 8B: weights 16 GB, KV 34 GB</title><rect class="s1" x="190.0" y="48.0" width="6.8" height="22.0" rx="3"/><rect class="s2" x="196.8" y="48.0" width="14.5" height="22.0" rx="3"/></g><text class="t-val" x="219.3" y="64.0" text-anchor="start">50 GB -&gt; 1 x H100</text><text class="t-note" x="180.0" y="119.0" text-anchor="end">Llama 3.1 70B</text><text class="t-tick" x="180.0" y="136.0" text-anchor="end">BF16 weights</text><g class="mark"><title>Llama 3.1 70B: weights 141 GB, KV 86 GB</title><rect class="s1" x="190.0" y="106.0" width="59.6" height="22.0" rx="3"/><rect class="s2" x="249.6" y="106.0" width="36.3" height="22.0" rx="3"/></g><text class="t-val" x="293.9" y="122.0" text-anchor="start">227 GB -&gt; 4 x H100</text><text class="t-note" x="180.0" y="177.0" text-anchor="end">Llama 3.1 405B</text><text class="t-tick" x="180.0" y="194.0" text-anchor="end">BF16 weights</text><g class="mark"><title>Llama 3.1 405B: weights 812 GB, KV 135 GB</title><rect class="s1" x="190.0" y="164.0" width="342.9" height="22.0" rx="3"/><rect class="s2" x="532.9" y="164.0" width="57.1" height="22.0" rx="3"/></g><text class="t-val" x="598.0" y="180.0" text-anchor="start">947 GB -&gt; 14 x H100</text><text class="t-note" x="180.0" y="235.0" text-anchor="end">DeepSeek-V3</text><text class="t-tick" x="180.0" y="252.0" text-anchor="end">FP8 weights</text><g class="mark"><title>DeepSeek-V3: weights 671 GB, KV 18 GB</title><rect class="s1" x="190.0" y="222.0" width="283.4" height="22.0" rx="3"/><rect class="s2" x="473.4" y="222.0" width="7.8" height="22.0" rx="3"/></g><text class="t-val" x="489.2" y="238.0" text-anchor="start">689 GB -&gt; 10 x H100</text><line class="base-line" x1="220.4" y1="34.0" x2="220.4" y2="272.0"/><text class="t-tick" x="220.4" y="288.0" text-anchor="middle">1 x 80 GB (90% usable)</text><line class="base-line" x1="433.3" y1="34.0" x2="433.3" y2="272.0"/><text class="t-tick" x="433.3" y="288.0" text-anchor="middle">8 x 80 GB: one node</text></svg><figcaption>Weights (BF16, or FP8 for DeepSeek-V3) plus the KV cache for 32 conversations of 8,192 tokens each. Arithmetic from the published model shapes; 90% of each 80 GB GPU counted as usable.</figcaption></figure>

[![Terminal output of memory_math.py: Llama 3.1 8B 8.0B parameters, 16 GB weights, 128 KiB per token, 32 GiB of KV for 32 x 8K, 50 GB total, 1 H100; Llama 3.1 70B 70.6B, 141 GB, 320 KiB, 80 GiB, 227 GB, 4 H100s; Llama 3.1 405B 405.9B, 812 GB, 504 KiB, 126 GiB, 947 GB, 14 H100s; DeepSeek-V3 671B, 671 GB in FP8, 68.6 KiB, 17 GiB, 689 GB, 10 H100s. Decode floor table: 8B 4.8 ms on 1 GPU down to 0.3 ms on 16; 70B 21.1 ms on 2 GPUs, 10.5 on 4, 5.3 on 8; 405B 15.1 ms on 16; DeepSeek-V3 active weights 11.0 ms](https://ishwarj.com/img/multigpu/memory_math-run.pngmemory_math-run.png)](/img/multigpu/memory_math-run.png)

Three things stand out.

**The 8B model fits on one GPU with room to spare.** For it, more GPUs mean more copies of the model, not a split model. That is the world of Part 6.

**The 405B model does not even fit on one 8-GPU server.** In BF16 its weights alone are 812 GB, more than the 640 GB of eight H100s. Meta hit exactly this wall:

> [!PAPER] Llama Team, The Llama 3 Herd of Models · Section 6.1 · page 51
> [![Llama 3 report: When using a BF16 number representation for the model parameters, Llama 3 405B does not fit in the GPU memory of a single machine with 8 Nvidia H100 GPUs. To address this issue, we parallelize model inference using BF16 precision across 16 GPUs on two machines. Within each machine, the high NVLink bandwidth](https://ishwarj.com/img/multigpu/paper-llama3-pp1.pngaper-llama3-pp1.png)](/img/multigpu/paper-llama3-pp1.png)
>
> **Context:** the start of the report's inference section, about serving the 405B model.
>
> **What it says:** 405B in BF16 does not fit in one 8-GPU H100 machine, so Meta spread it over 16 GPUs in two machines. We will come back to *how* they split it in Section 5.
>
> **Why it matters:** our arithmetic (812 GB of weights against 640 GB) predicts exactly this, before a single GPU is bought.
>
> [Read Section 6.1](https://arxiv.org/pdf/2407.21783#page=51)

**DeepSeek-V3 is bigger but its cache is smaller.** It has 671 billion parameters, stored in FP8 (one byte each), so 671 GB. But it uses multi-head latent attention (MLA, mentioned in [Part 2](https://ishwarj.com/writings/llm-inference-2-kv-cache/)): each layer caches one compressed vector of 512 numbers plus a 64-number position key, shared by all heads. That is $$61\times(512+64)\times2 = 70{,}272$$ bytes per token, **68.6 KiB, against 504 KiB for Llama 3.1 405B**. Its problem is the weights, not the cache.

> [!DEFINITION] Mixture of experts (MoE)
> A model whose feed-forward layers are split into many smaller "experts". A small router picks a few experts for each token, so each token uses only part of the model. DeepSeek-V3 has 256 routed experts per layer and picks 8, plus one shared expert: 671 billion parameters in total, but only 37 billion are used for any one token.

### Speed: decode is limited by reading the weights

Even when a model fits, there is a second reason to spread it: speed. [Part 1](https://ishwarj.com/writings/llm-inference-1-prefill-and-decode/) showed that a decode step for a small batch is **memory-bound**: the GPU spends its time reading every weight once, not multiplying. That gives a hard floor on the time per token:

$$
t_{\text{step}} \;\ge\; \frac{N\,b_w}{p\,B_{\text{HBM}}}
$$

where:

- $$N\,b_w$$ is the size of the weights in bytes;
- $$p$$ is the number of GPUs the weights are split over, each reading only its own share;
- $$B_{\text{HBM}}$$ is one GPU's memory bandwidth (3.35 TB/s for an H100).

**Worked example.** Llama 3.1 70B on 2 H100s: $$141\ \text{GB} / (2\times3.35\ \text{TB/s}) = 21.1$$ ms, at best 47 tokens per second for one user. On 8 H100s each GPU reads only 17.6 GB, so the floor drops to **5.3 ms**. Spreading the weights does not just make the model fit; it multiplies the memory bandwidth working on each token.

<figure class="fig"><svg viewBox="0 0 760 306" role="img" aria-label="Lowest possible time per decode step at batch 1: reading the weights once, split perfectly over 1 to 16 H100s. 8B: 4.8 ms on one GPU. 70B: 21.1 ms on two, 5.3 ms on eight. 405B: 15.1 ms on sixteen."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><line class="grid" x1="90.0" y1="238.4" x2="700.0" y2="238.4"/><text class="t-tick" x="82.0" y="242.4" text-anchor="end">0.3 ms</text><line class="grid" x1="90.0" y1="203.8" x2="700.0" y2="203.8"/><text class="t-tick" x="82.0" y="207.8" text-anchor="end">1 ms</text><line class="grid" x1="90.0" y1="172.2" x2="700.0" y2="172.2"/><text class="t-tick" x="82.0" y="176.2" text-anchor="end">3 ms</text><line class="grid" x1="90.0" y1="137.7" x2="700.0" y2="137.7"/><text class="t-tick" x="82.0" y="141.7" text-anchor="end">10 ms</text><line class="grid" x1="90.0" y1="106.1" x2="700.0" y2="106.1"/><text class="t-tick" x="82.0" y="110.1" text-anchor="end">30 ms</text><line class="grid" x1="90.0" y1="71.5" x2="700.0" y2="71.5"/><text class="t-tick" x="82.0" y="75.5" text-anchor="end">100 ms</text><text class="t-tick" x="90.0" y="270.0" text-anchor="middle">1 GPU</text><text class="t-tick" x="242.5" y="270.0" text-anchor="middle">2 GPUs</text><text class="t-tick" x="395.0" y="270.0" text-anchor="middle">4 GPUs</text><text class="t-tick" x="547.5" y="270.0" text-anchor="middle">8 GPUs</text><text class="t-tick" x="700.0" y="270.0" text-anchor="middle">16 GPUs</text><line class="axis" x1="90.0" y1="250.0" x2="700.0" y2="250.0"/><polyline class="long" points="90.0,158.8"/><polyline class="l1" points="90.0,158.8 242.5,178.7 395.0,198.6 547.5,218.5 700.0,238.4"/><g class="mark"><title>Llama 3.1 8B on 1 GPUs: 4.8 ms</title><circle class="s1" cx="90.0" cy="158.8" r="4"/></g><g class="mark"><title>Llama 3.1 8B on 2 GPUs: 2.4 ms</title><circle class="s1" cx="242.5" cy="178.7" r="4"/></g><g class="mark"><title>Llama 3.1 8B on 4 GPUs: 1.2 ms</title><circle class="s1" cx="395.0" cy="198.6" r="4"/></g><g class="mark"><title>Llama 3.1 8B on 8 GPUs: 0.6 ms</title><circle class="s1" cx="547.5" cy="218.5" r="4"/></g><g class="mark"><title>Llama 3.1 8B on 16 GPUs: 0.3 ms</title><circle class="s1" cx="700.0" cy="238.4" r="4"/></g><text class="t-val" x="100.0" y="148.8" text-anchor="start">Llama 3.1 8B: 4.8 ms</text><polyline class="long" points="90.0,96.4 242.5,116.3"/><polyline class="l2" points="242.5,116.3 395.0,136.2 547.5,156.1 700.0,176.0"/><g class="mark"><title>Llama 3.1 70B on 2 GPUs: 21.1 ms</title><circle class="s2" cx="242.5" cy="116.3" r="4"/></g><g class="mark"><title>Llama 3.1 70B on 4 GPUs: 10.5 ms</title><circle class="s2" cx="395.0" cy="136.2" r="4"/></g><g class="mark"><title>Llama 3.1 70B on 8 GPUs: 5.3 ms</title><circle class="s2" cx="547.5" cy="156.1" r="4"/></g><g class="mark"><title>Llama 3.1 70B on 16 GPUs: 2.6 ms</title><circle class="s2" cx="700.0" cy="176.0" r="4"/></g><text class="t-val" x="252.5" y="106.3" text-anchor="start">Llama 3.1 70B: 21.1 ms</text><polyline class="long" points="90.0,46.1 242.5,66.0 395.0,85.9 547.5,105.8 700.0,125.7"/><polyline class="l3" points="700.0,125.7"/><g class="mark"><title>Llama 3.1 405B on 16 GPUs: 15.1 ms</title><circle class="s3" cx="700.0" cy="125.7" r="4"/></g><text class="t-val" x="688.0" y="130.7" text-anchor="end">Llama 3.1 405B: 15.1 ms</text><text class="t-tick" x="380.0" y="294.0" text-anchor="middle">Dashed: the weights do not fit there. BF16 weights read once per step at 3.35 TB/s per H100, perfect split.</text></svg><figcaption>The lowest possible time per decode step at batch 1, if the weights are split perfectly and communication were free. Arithmetic: BF16 weights, 3.35 TB/s per H100.</figcaption></figure>

That last condition, "if communication were free", is the whole subject of this article. It is not free.

## 2. The wires between GPUs

### Inside a server and between servers

A typical AI server (NVIDIA calls it a node) has eight GPUs. They are connected to each other in two very different ways, depending on whether the other GPU is in the same box.

> [!DEFINITION] NVLink and NVSwitch
> NVLink is NVIDIA's direct GPU-to-GPU connection. On H100 it carries up to 900 GB/s per GPU, counting both directions. NVSwitch is a switch chip inside the server that connects all eight GPUs' NVLinks, so any GPU can talk to any other at full speed.

> [!DEFINITION] PCIe
> The general-purpose connection between a GPU and the rest of the computer (the CPU, network cards, disks). PCIe Gen5 with 16 lanes carries 128 GB/s, both directions together. GPUs without NVLink must talk to each other over PCIe.

> [!DEFINITION] InfiniBand and RDMA
> InfiniBand is a fast network for connecting servers in a cluster. A 400 Gb/s card (NVIDIA ConnectX-7) moves 50 GB/s each way. RDMA (remote direct memory access) lets one machine write into another's memory without either CPU copying the data; with GPUDirect RDMA the network card reads and writes GPU memory directly. Some clusters use RDMA over fast Ethernet instead (RoCE).

<figure class="fig"><svg viewBox="0 0 760 360" role="img" aria-label="An 8-GPU server. The GPUs talk to each other through NVSwitch at 900 GB/s per GPU. Each GPU also has a 400 Gb/s network card (50 GB/s) for traffic to other servers."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="plane" x="20.0" y="20.0" width="470.0" height="300.0" rx="14"/><text class="t-title" x="255.0" y="44.0" text-anchor="middle">One server (node): 8 GPUs</text><rect class="box-1" x="40.0" y="64.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="85.0" y="86.0" text-anchor="middle">GPU 0</text><text class="t-tick" x="85.0" y="104.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="85.0" y1="114.0" x2="85.0" y2="150.0"/><rect class="box-1" x="150.0" y="64.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="195.0" y="86.0" text-anchor="middle">GPU 1</text><text class="t-tick" x="195.0" y="104.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="195.0" y1="114.0" x2="195.0" y2="150.0"/><rect class="box-1" x="260.0" y="64.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="305.0" y="86.0" text-anchor="middle">GPU 2</text><text class="t-tick" x="305.0" y="104.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="305.0" y1="114.0" x2="305.0" y2="150.0"/><rect class="box-1" x="370.0" y="64.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="415.0" y="86.0" text-anchor="middle">GPU 3</text><text class="t-tick" x="415.0" y="104.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="415.0" y1="114.0" x2="415.0" y2="150.0"/><rect class="box-1" x="40.0" y="236.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="85.0" y="258.0" text-anchor="middle">GPU 4</text><text class="t-tick" x="85.0" y="276.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="85.0" y1="236.0" x2="85.0" y2="198.0"/><rect class="box-1" x="150.0" y="236.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="195.0" y="258.0" text-anchor="middle">GPU 5</text><text class="t-tick" x="195.0" y="276.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="195.0" y1="236.0" x2="195.0" y2="198.0"/><rect class="box-1" x="260.0" y="236.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="305.0" y="258.0" text-anchor="middle">GPU 6</text><text class="t-tick" x="305.0" y="276.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="305.0" y1="236.0" x2="305.0" y2="198.0"/><rect class="box-1" x="370.0" y="236.0" width="90.0" height="50.0" rx="8"/><text class="t-note" x="415.0" y="258.0" text-anchor="middle">GPU 7</text><text class="t-tick" x="415.0" y="276.0" text-anchor="middle">80 GB HBM</text><line class="edge" x1="415.0" y1="236.0" x2="415.0" y2="198.0"/><rect class="box-2" x="40.0" y="150.0" width="420.0" height="48.0" rx="8"/><text class="t-note" x="250.0" y="172.0" text-anchor="middle">NVSwitch: any GPU to any GPU</text><text class="t-tick" x="250.0" y="190.0" text-anchor="middle">900 GB/s per GPU (both directions)</text><rect class="box-3" x="560.0" y="64.0" width="170.0" height="74.0" rx="8"/><text class="t-note" x="645.0" y="92.0" text-anchor="middle">8 network cards</text><text class="t-tick" x="645.0" y="112.0" text-anchor="middle">400 Gb/s each = 50 GB/s</text><text class="t-tick" x="645.0" y="128.0" text-anchor="middle">(InfiniBand or Ethernet)</text><line class="edge" x1="490.0" y1="101.0" x2="556.0" y2="101.0" marker-end="url(#ah7)"/><rect class="box" x="560.0" y="236.0" width="170.0" height="74.0" rx="8"/><text class="t-note" x="645.0" y="264.0" text-anchor="middle">other nodes</text><text class="t-tick" x="645.0" y="284.0" text-anchor="middle">through network switches</text><line class="long" x1="645.0" y1="138.0" x2="645.0" y2="232.0" marker-end="url(#ah7)"/><rect class="box-1" x="520.0" y="160.0" width="0.0" height="0.0" rx="8"/><text class="t-tick" x="660.0" y="190.0" text-anchor="start">about 9x slower</text><text class="t-tick" x="660.0" y="206.0" text-anchor="start">than NVLink</text><text class="t-tick" x="255.0" y="345.0" text-anchor="middle">Inside: NVLink through NVSwitch. Between nodes: one NIC per GPU, over the network.</text></svg><figcaption>One 8-GPU server, as in NVIDIA's DGX H100: NVLink through NVSwitch inside, one 400 Gb/s network card per GPU to the rest of the cluster.</figcaption></figure>

Put the numbers side by side:

<figure class="fig"><svg viewBox="0 0 760 290" role="img" aria-label="Peak bandwidths on a log scale: HBM 3,350 GB/s, NVLink 900 GB/s, PCIe Gen5 128 GB/s, one 400 Gb/s InfiniBand card 50 GB/s, 100 Gb/s Ethernet 12.5 GB/s."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><line class="grid" x1="339.1" y1="18.0" x2="339.1" y2="238.0"/><text class="t-tick" x="339.1" y="254.0" text-anchor="middle">10 GB/s</text><line class="grid" x1="469.1" y1="18.0" x2="469.1" y2="238.0"/><text class="t-tick" x="469.1" y="254.0" text-anchor="middle">100 GB/s</text><line class="grid" x1="599.1" y1="18.0" x2="599.1" y2="238.0"/><text class="t-tick" x="599.1" y="254.0" text-anchor="middle">1,000 GB/s</text><text class="t-note" x="290.0" y="40.0" text-anchor="end">HBM inside one H100</text><g class="mark"><title>HBM inside one H100: 3350 GB/s</title><rect class="s1" x="300.0" y="26.0" width="367.4" height="22.0" rx="3"/></g><text class="t-val" x="675.4" y="42.0" text-anchor="start">3,350</text><text class="t-note" x="290.0" y="84.0" text-anchor="end">NVLink 4 per GPU (both directions)</text><g class="mark"><title>NVLink 4 per GPU (both directions): 900 GB/s</title><rect class="s2" x="300.0" y="70.0" width="293.2" height="22.0" rx="3"/></g><text class="t-val" x="601.2" y="86.0" text-anchor="start">900</text><text class="t-note" x="290.0" y="128.0" text-anchor="end">PCIe Gen5 x16 (both directions)</text><g class="mark"><title>PCIe Gen5 x16 (both directions): 128 GB/s</title><rect class="s3" x="300.0" y="114.0" width="183.1" height="22.0" rx="3"/></g><text class="t-val" x="491.1" y="130.0" text-anchor="start">128</text><text class="t-note" x="290.0" y="172.0" text-anchor="end">InfiniBand NDR, one 400 Gb/s NIC</text><g class="mark"><title>InfiniBand NDR, one 400 Gb/s NIC: 50 GB/s</title><rect class="s4" x="300.0" y="158.0" width="130.0" height="22.0" rx="3"/></g><text class="t-val" x="438.0" y="174.0" text-anchor="start">50</text><text class="t-note" x="290.0" y="216.0" text-anchor="end">Ethernet 100 Gb/s</text><g class="mark"><title>Ethernet 100 Gb/s: 12.5 GB/s</title><rect class="s4" x="300.0" y="202.0" width="51.7" height="22.0" rx="3"/></g><text class="t-val" x="359.7" y="218.0" text-anchor="start">12.5</text><text class="t-tick" x="380.0" y="278.0" text-anchor="middle">Spec-sheet peaks (NVIDIA H100, ConnectX-7). Log scale: each grid line is 10x.</text></svg><figcaption>Peak bandwidth of each link, from NVIDIA's H100 and ConnectX-7 pages. Note the log scale: each step down is several times slower.</figcaption></figure>

| Link | Peak bandwidth | Compared with NVLink |
|---|---|---|
| HBM, inside one H100 | 3,350 GB/s | (the GPU's own memory) |
| NVLink 4, GPU to GPU in a server | 900 GB/s both ways (450 each way) | 1x |
| PCIe Gen5 x16 | 128 GB/s both ways (64 each way) | 7x slower |
| InfiniBand NDR, one 400 Gb/s card | 50 GB/s each way | 9x slower |
| Ethernet, 100 Gb/s | 12.5 GB/s each way | 36x slower |

These are NVIDIA's published peaks (the [H100 page](https://www.nvidia.com/en-us/data-center/h100/) lists "NVIDIA NVLink: 900GB/s" and "PCIe Gen5: 128GB/s"; the [ConnectX-7 page](https://www.nvidia.com/en-us/networking/infiniband-adapters/) lists 400 Gb/s). The newer Blackwell generation doubles NVLink to 1,800 GB/s per GPU and can join 72 GPUs into one NVLink domain (the "NVL72" racks), which moves the line between "inside" and "outside" further out. The shape of the problem stays the same.

Real clusters see less than peak. DeepSeek reports what their H800 cluster (a version of the H100 with reduced NVLink bandwidth, made for export to China) delivers in practice:

> [!PAPER] DeepSeek-AI, DeepSeek-V3 Technical Report · Section 3.2.2 · page 13
> [![DeepSeek-V3 report: in our cluster, cross-node GPUs are fully interconnected with IB, and intra-node communications are handled via NVLink. NVLink offers a bandwidth of 160 GB/s, roughly 3.2 times that of IB (50 GB/s). To effectively leverage the different bandwidths of IB and NVLink, we limit each token to be dispatched to at most 4 nodes, thereby reducing IB traffic.](https://ishwarj.com/img/multigpu/paper-dsv3-links.pngaper-dsv3-links.png)](/img/multigpu/paper-dsv3-links.png)
>
> **Context:** how DeepSeek built the communication kernels for their mixture-of-experts model.
>
> **What it says:** in their cluster, NVLink gives about 160 GB/s and InfiniBand 50 GB/s, so they design the model's routing to send each token to at most 4 servers.
>
> **Why it matters:** the network shaped the model itself. When communication is the bottleneck, people change the algorithm to fit the wires, not the other way round.
>
> [Read Section 3.2.2](https://arxiv.org/pdf/2412.19437#page=13)

### What one message costs: latency plus size over bandwidth

Every message between GPUs costs a fixed amount of time before any data moves (setting up, synchronising, crossing switches), plus time proportional to its size. This is called the **alpha-beta model**:

$$
T(n) = \alpha + \frac{n}{\beta}
$$

where:

- $$n$$ is the message size in bytes;
- $$\alpha$$ (alpha) is the fixed cost per message, in seconds;
- $$\beta$$ (beta) is the bandwidth in bytes per second.

**Worked example.** Send 16 KiB (16,384 bytes) over NVLink at 450 GB/s each way. The size term is $$16{,}384 / (450\times10^9) = 36$$ ns. If the fixed cost is a few microseconds (an assumed figure; NVIDIA does not publish one), it is **about 100 times bigger** than the data term. Small messages are all alpha; large messages are all beta.

NVIDIA does not publish an alpha for NVLink or InfiniBand, and I cannot measure one without the hardware. So I measured the *shape* on what I have: separate processes on the M5 Pro's CPU, exchanging data with `torch.distributed` (the same library NVIDIA GPUs use, but with its CPU backend, called gloo).

<figure class="fig"><svg viewBox="0 0 760 304" role="img" aria-label="Measured all-reduce time against message size on the M5 Pro with 2 and 4 processes: almost flat for small messages (the fixed cost alpha), then growing in proportion to size (the bandwidth term)."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><line class="grid" x1="90.0" y1="245.8" x2="700.0" y2="245.8"/><text class="t-tick" x="82.0" y="249.8" text-anchor="end">100 us</text><line class="grid" x1="90.0" y1="175.3" x2="700.0" y2="175.3"/><text class="t-tick" x="82.0" y="179.3" text-anchor="end">1,000 us</text><line class="grid" x1="90.0" y1="104.7" x2="700.0" y2="104.7"/><text class="t-tick" x="82.0" y="108.7" text-anchor="end">10,000 us</text><line class="grid" x1="90.0" y1="34.1" x2="700.0" y2="34.1"/><text class="t-tick" x="82.0" y="38.1" text-anchor="end">100,000 us</text><text class="t-tick" x="90.0" y="268.0" text-anchor="middle">4 B</text><text class="t-tick" x="293.3" y="268.0" text-anchor="middle">1 KiB</text><text class="t-tick" x="547.5" y="268.0" text-anchor="middle">1 MiB</text><text class="t-tick" x="700.0" y="268.0" text-anchor="middle">64 MiB</text><line class="axis" x1="90.0" y1="250.0" x2="700.0" y2="250.0"/><polyline class="l1" points="90.0,237.6 140.8,226.1 191.7,216.6 242.5,217.0 293.3,217.9 344.2,222.5 395.0,209.1 445.8,203.7 496.7,194.8 547.5,183.6 598.3,146.3 649.2,108.8 700.0,64.5"/><g class="mark"><title>2 processes, 4 B: 131 us</title><circle class="s1" cx="90.0" cy="237.6" r="3.5"/></g><g class="mark"><title>2 processes, 16 B: 191 us</title><circle class="s1" cx="140.8" cy="226.1" r="3.5"/></g><g class="mark"><title>2 processes, 64 B: 259 us</title><circle class="s1" cx="191.7" cy="216.6" r="3.5"/></g><g class="mark"><title>2 processes, 256 B: 256 us</title><circle class="s1" cx="242.5" cy="217.0" r="3.5"/></g><g class="mark"><title>2 processes, 1,024 B: 249 us</title><circle class="s1" cx="293.3" cy="217.9" r="3.5"/></g><g class="mark"><title>2 processes, 4,096 B: 214 us</title><circle class="s1" cx="344.2" cy="222.5" r="3.5"/></g><g class="mark"><title>2 processes, 16,384 B: 332 us</title><circle class="s1" cx="395.0" cy="209.1" r="3.5"/></g><g class="mark"><title>2 processes, 65,536 B: 396 us</title><circle class="s1" cx="445.8" cy="203.7" r="3.5"/></g><g class="mark"><title>2 processes, 262,144 B: 529 us</title><circle class="s1" cx="496.7" cy="194.8" r="3.5"/></g><g class="mark"><title>2 processes, 1,048,576 B: 761 us</title><circle class="s1" cx="547.5" cy="183.6" r="3.5"/></g><g class="mark"><title>2 processes, 4,194,304 B: 2573 us</title><circle class="s1" cx="598.3" cy="146.3" r="3.5"/></g><g class="mark"><title>2 processes, 16,777,216 B: 8761 us</title><circle class="s1" cx="649.2" cy="108.8" r="3.5"/></g><g class="mark"><title>2 processes, 67,108,864 B: 37098 us</title><circle class="s1" cx="700.0" cy="64.5" r="3.5"/></g><polyline class="l2" points="90.0,193.3 140.8,182.7 191.7,164.2 242.5,167.8 293.3,167.3 344.2,165.1 395.0,165.3 445.8,167.1 496.7,163.9 547.5,150.4 598.3,124.4 649.2,82.1 700.0,42.4"/><g class="mark"><title>4 processes, 4 B: 555 us</title><circle class="s2" cx="90.0" cy="193.3" r="3.5"/></g><g class="mark"><title>4 processes, 16 B: 785 us</title><circle class="s2" cx="140.8" cy="182.7" r="3.5"/></g><g class="mark"><title>4 processes, 64 B: 1435 us</title><circle class="s2" cx="191.7" cy="164.2" r="3.5"/></g><g class="mark"><title>4 processes, 256 B: 1276 us</title><circle class="s2" cx="242.5" cy="167.8" r="3.5"/></g><g class="mark"><title>4 processes, 1,024 B: 1297 us</title><circle class="s2" cx="293.3" cy="167.3" r="3.5"/></g><g class="mark"><title>4 processes, 4,096 B: 1394 us</title><circle class="s2" cx="344.2" cy="165.1" r="3.5"/></g><g class="mark"><title>4 processes, 16,384 B: 1382 us</title><circle class="s2" cx="395.0" cy="165.3" r="3.5"/></g><g class="mark"><title>4 processes, 65,536 B: 1304 us</title><circle class="s2" cx="445.8" cy="167.1" r="3.5"/></g><g class="mark"><title>4 processes, 262,144 B: 1447 us</title><circle class="s2" cx="496.7" cy="163.9" r="3.5"/></g><g class="mark"><title>4 processes, 1,048,576 B: 2249 us</title><circle class="s2" cx="547.5" cy="150.4" r="3.5"/></g><g class="mark"><title>4 processes, 4,194,304 B: 5256 us</title><circle class="s2" cx="598.3" cy="124.4" r="3.5"/></g><g class="mark"><title>4 processes, 16,777,216 B: 20922 us</title><circle class="s2" cx="649.2" cy="82.1" r="3.5"/></g><g class="mark"><title>4 processes, 67,108,864 B: 76281 us</title><circle class="s2" cx="700.0" cy="42.4" r="3.5"/></g><rect class="s1" x="110.0" y="24.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="128.0" y="34.0" text-anchor="start">2 processes: alpha 249 us, beta 1.8 GB/s</text><rect class="s2" x="424.0" y="24.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="442.0" y="34.0" text-anchor="start">4 processes: alpha 1276 us, beta 0.9 GB/s</text><text class="t-tick" x="380.0" y="292.0" text-anchor="middle">Measured: torch.distributed all_reduce, gloo backend, CPU processes on the M5 Pro. Flat, then a slope.</text></svg><figcaption>Measured on the M5 Pro: the time of one all-reduce (defined in Section 4) between 2 or 4 CPU processes, against message size. The absolute numbers describe the M5 Pro's shared-memory path, not any GPU link.</figcaption></figure>

[![Terminal output of tp_demo.py part C: gloo all_reduce timings for 2 and 4 processes from 4 bytes to 64 MiB, with the fitted alpha and beta](https://ishwarj.com/img/multigpu/tp_demo_c-run.pngu/tp_demo_c-run.png)](/img/multigpu/tp_demo_c-run.png)

The curve is flat for small messages and then rises in step with size: exactly $$\alpha + n/\beta$$. On the M5 Pro the flat part is about 0.25 ms with 2 processes and 1.3 ms with 4: more participants means more steps and more waiting for the slowest one, a first hint of Section 4. (These alphas are hundreds of times larger than a GPU's, because they are CPU processes; only the shape carries over.) The flat part is why the next sections keep asking two separate questions about every design: **how many messages** per token, and **how many bytes** per message.

## 3. Tensor parallelism: split every layer

### The idea

> [!DEFINITION] Tensor parallelism (TP)
> Every layer's weight matrices are cut into pieces, and each GPU holds one piece of **every** layer. All GPUs work on the same tokens at the same time, each computing part of each layer, and they combine their partial results after each block. "TP=8" means the layers are split eight ways.

Tensor parallelism attacks both problems from Section 1 at once: each GPU stores $$1/p$$ of the weights, and each GPU reads only $$1/p$$ of them per step. The price is communication inside every layer. The trick, from the Megatron-LM paper (Shoeybi et al., 2019), is to cut the matrices so that the communication happens as rarely as possible.

### Splitting the MLP: columns, then rows

A transformer's MLP block is two matrix multiplications with a nonlinearity between them:

$$
Y = \mathrm{GeLU}(XA), \qquad Z = YB
$$

where:

- $$X$$ is the input, one row per token, $$d$$ columns wide (the model width);
- $$A$$ is a $$d\times f$$ weight matrix that widens each token to $$f$$ numbers (the FFN width);
- GeLU is the nonlinear function applied to every number (Llama uses a close cousin, SwiGLU);
- $$B$$ is an $$f\times d$$ matrix that narrows each token back to $$d$$ numbers.

There are two ways to cut $$A$$ in half. Megatron's authors explain why only one of them works well:

> [!PAPER] Shoeybi et al., Megatron-LM · Section 3 · page 4
> [![Megatron-LM: We start by detailing the MLP block. The first part of the block is a GEMM followed by a GeLU nonlinearity, Y = GeLU(XA). One option is to split A along its rows and X along its columns; since GeLU is a nonlinear function, GeLU(X1A1+X2A2) is not GeLU(X1A1)+GeLU(X2A2) and this approach will require a synchronization point before the GeLU function. Another option is to split A along its columns, which allows the GeLU nonlinearity to be independently applied to the output of each partitioned GEMM. Hence, we partition the first GEMM in this column parallel fashion and split the second GEMM along its rows so it takes the output of the GeLU layer directly without requiring any communication. The output of the second GEMM is then reduced across the GPUs. This requires only a single all-reduce operation in the forward pass and a single all-reduce in the backward pass.](https://ishwarj.com/img/multigpu/paper-megatron-mlp.pnger-megatron-mlp.png)](/img/multigpu/paper-megatron-mlp.png)
>
> **Context:** Section 3 of the paper, which introduced the tensor-parallel layout every inference engine uses today.
>
> **What it says:** cut $$A$$ by **columns**, so each GPU computes complete GeLU outputs for its own share of the $$f$$ hidden units; cut $$B$$ by **rows**, so each GPU can multiply its own hidden units straight away. Only at the very end are the partial results added together, once.
>
> **Why it matters:** inference only runs the forward pass, so the MLP costs **exactly one all-reduce** per layer, no matter how many GPUs share it.
>
> [Read Section 3](https://arxiv.org/pdf/1909.08053#page=4)

Written out for two GPUs, with $$A = [A_1, A_2]$$ split by columns and $$B = \begin{bmatrix} B_1 \\ B_2 \end{bmatrix}$$ split by rows:

$$
Z = \mathrm{GeLU}(X[A_1, A_2])\begin{bmatrix} B_1 \\ B_2 \end{bmatrix} = \underbrace{\mathrm{GeLU}(XA_1)\,B_1}_{\text{GPU 0}} + \underbrace{\mathrm{GeLU}(XA_2)\,B_2}_{\text{GPU 1}}
$$

The GeLU is applied to each column of $$XA$$ separately, so it does not care how the columns are grouped. The only cross-GPU step is the final **+**.

<figure class="fig"><svg viewBox="0 0 760 276" role="img" aria-label="Tensor-parallel MLP on two GPUs. Each GPU has the full input X, half the columns of A and the matching half of the rows of B. It computes its part with no communication. One all-reduce adds the two partial outputs."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">Megatron MLP on 2 GPUs:  Y = GeLU(X A),  Z = Y B</text><rect class="box" x="20.0" y="50.0" width="80.0" height="70.0" rx="8"/><text class="t-node" x="60.0" y="82.0" text-anchor="middle">X</text><text class="t-tick" x="60.0" y="104.0" text-anchor="middle">full copy</text><line class="edge" x1="100.0" y1="85.0" x2="136.0" y2="85.0" marker-end="url(#ah7)"/><rect class="box-1" x="140.0" y="50.0" width="130.0" height="70.0" rx="8"/><text class="t-node" x="205.0" y="80.0" text-anchor="middle">A1</text><text class="t-tick" x="205.0" y="104.0" text-anchor="middle">half the columns</text><line class="edge" x1="270.0" y1="85.0" x2="306.0" y2="85.0" marker-end="url(#ah7)"/><rect class="box-1" x="310.0" y="50.0" width="100.0" height="70.0" rx="8"/><text class="t-tick" x="360.0" y="80.0" text-anchor="middle">Y1 = GeLU(X A1)</text><text class="t-tick" x="360.0" y="102.0" text-anchor="middle">no talking</text><line class="edge" x1="410.0" y1="85.0" x2="446.0" y2="85.0" marker-end="url(#ah7)"/><rect class="box-2" x="450.0" y="50.0" width="120.0" height="70.0" rx="8"/><text class="t-node" x="510.0" y="80.0" text-anchor="middle">B1</text><text class="t-tick" x="510.0" y="104.0" text-anchor="middle">half the rows</text><line class="edge" x1="570.0" y1="85.0" x2="618.0" y2="140.0" marker-end="url(#ah7)"/><text class="t-tick" x="16.0" y="85.0" text-anchor="middle"></text><rect class="box" x="20.0" y="170.0" width="80.0" height="70.0" rx="8"/><text class="t-node" x="60.0" y="202.0" text-anchor="middle">X</text><text class="t-tick" x="60.0" y="224.0" text-anchor="middle">full copy</text><line class="edge" x1="100.0" y1="205.0" x2="136.0" y2="205.0" marker-end="url(#ah7)"/><rect class="box-1" x="140.0" y="170.0" width="130.0" height="70.0" rx="8"/><text class="t-node" x="205.0" y="200.0" text-anchor="middle">A2</text><text class="t-tick" x="205.0" y="224.0" text-anchor="middle">half the columns</text><line class="edge" x1="270.0" y1="205.0" x2="306.0" y2="205.0" marker-end="url(#ah7)"/><rect class="box-1" x="310.0" y="170.0" width="100.0" height="70.0" rx="8"/><text class="t-tick" x="360.0" y="200.0" text-anchor="middle">Y2 = GeLU(X A2)</text><text class="t-tick" x="360.0" y="222.0" text-anchor="middle">no talking</text><line class="edge" x1="410.0" y1="205.0" x2="446.0" y2="205.0" marker-end="url(#ah7)"/><rect class="box-2" x="450.0" y="170.0" width="120.0" height="70.0" rx="8"/><text class="t-node" x="510.0" y="200.0" text-anchor="middle">B2</text><text class="t-tick" x="510.0" y="224.0" text-anchor="middle">half the rows</text><line class="edge" x1="570.0" y1="205.0" x2="618.0" y2="160.0" marker-end="url(#ah7)"/><text class="t-tick" x="16.0" y="205.0" text-anchor="middle"></text><rect class="box-3" x="620.0" y="110.0" width="120.0" height="80.0" rx="8"/><text class="t-note" x="680.0" y="140.0" text-anchor="middle">all-reduce</text><text class="t-tick" x="680.0" y="160.0" text-anchor="middle">Z = Y1 B1 + Y2 B2</text><text class="t-tick" x="680.0" y="176.0" text-anchor="middle">every GPU gets Z</text><text class="t-tick" x="255.0" y="135.0" text-anchor="middle">GPU 0 above, GPU 1 below: each holds half of A and half of B</text><text class="t-tick" x="380.0" y="262.0" text-anchor="middle">The only communication: one sum of the partial outputs, the size of the activations (tokens x d).</text></svg><figcaption>The Megatron MLP split on two GPUs. Each GPU computes a complete partial output with no communication; one all-reduce adds them.</figcaption></figure>

### Check it: the sum of the parts equals the whole

This is easy to verify. The first part of `tp_demo.py` builds an MLP with Qwen2.5-0.5B's sizes ($$d = 896$$, $$f = 4{,}864$$), splits it two, four and eight ways, and compares the sum of the pieces with the unsplit result:

```python
full = F.gelu(X @ A) @ B
for p in [2, 4, 8]:
    A_parts = A.chunk(p, dim=1)              # column split: each GPU gets ffn/p output columns
    B_parts = B.chunk(p, dim=0)              # row split: each GPU gets the matching ffn/p rows
    partial = [F.gelu(X @ Ai) @ Bi for Ai, Bi in zip(A_parts, B_parts)]   # no communication needed here
    Y = sum(partial)                         # the all-reduce: add the p partial outputs
    res[p] = float((Y - full).abs().max())
```

`chunk(p, dim=1)` cuts $$A$$ into $$p$$ blocks of columns; `chunk(p, dim=0)` cuts $$B$$ into the matching blocks of rows. Each list entry in `partial` is what one GPU would compute on its own. `sum(partial)` is the all-reduce. Then the wrong way, splitting $$A$$ by rows and applying GeLU before adding:

```python
Xs, As = X.chunk(2, dim=1), A.chunk(2, dim=0)
wrong = sum(F.gelu(Xi @ Ai) for Xi, Ai in zip(Xs, As)) @ B
```

```text
PART A: split matrix multiplies (float64, CPU)
p=2: each part A_i (896, 2432), B_i (2432, 896); max |sum of parts - unsplit| = 1.1e-14
p=4: each part A_i (896, 1216), B_i (1216, 896); max |sum of parts - unsplit| = 1.3e-14
p=8: each part A_i (896, 608), B_i (608, 896); max |sum of parts - unsplit| = 1.2e-14
row-split first matrix, GeLU applied before adding: max error 0.909 (output values are about 0.533 on average) -> needs a sync before GeLU
```

The column-then-row split matches the unsplit result to $$10^{-14}$$, which is just the rounding of 64-bit floats. The row split is wrong by more than the size of the answer itself, because $$\mathrm{GeLU}(a+b) \ne \mathrm{GeLU}(a)+\mathrm{GeLU}(b)$$. To use it you would have to add the halves *before* the GeLU: a second all-reduce per layer.

### Splitting attention: whole heads per GPU

Attention splits even more naturally. Its heads are already independent: head 3 never looks at head 5's numbers until the output projection mixes them. So each GPU takes a whole group of heads, with their query, key and value weights (a column split), and the matching rows of the output projection $$W_O$$ (a row split). Again one all-reduce adds the partial outputs.

<figure class="fig"><svg viewBox="0 0 760 318" role="img" aria-label="Attention split by heads across two GPUs. Each GPU runs its own query heads with its own key-value heads, keeps only those heads in its KV cache, and produces a partial output. An all-reduce sums the partial outputs."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">Attention on 2 GPUs: whole heads per GPU, and each GPU caches only its own heads</text><rect class="plane" x="30.0" y="44.0" width="330.0" height="170.0" rx="12"/><text class="t-note" x="195.0" y="66.0" text-anchor="middle">GPU 0</text><rect class="box-1" x="46.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="79.0" y="105.0" text-anchor="middle">head 0</text><rect class="box-1" x="124.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="157.0" y="105.0" text-anchor="middle">head 1</text><rect class="box-1" x="202.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="235.0" y="105.0" text-anchor="middle">head 2</text><rect class="box-1" x="280.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="313.0" y="105.0" text-anchor="middle">head 3</text><rect class="box-2" x="46.0" y="132.0" width="144.0" height="34.0" rx="6"/><text class="t-tick" x="118.0" y="154.0" text-anchor="middle">KV head 0</text><rect class="box" x="200.0" y="132.0" width="144.0" height="34.0" rx="6"/><text class="t-tick" x="272.0" y="154.0" text-anchor="middle">KV cache: half</text><rect class="box-3" x="46.0" y="176.0" width="298.0" height="30.0" rx="6"/><text class="t-tick" x="195.0" y="196.0" text-anchor="middle">partial output = its heads x its rows of W_o</text><rect class="plane" x="390.0" y="44.0" width="330.0" height="170.0" rx="12"/><text class="t-note" x="555.0" y="66.0" text-anchor="middle">GPU 1</text><rect class="box-1" x="406.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="439.0" y="105.0" text-anchor="middle">head 4</text><rect class="box-1" x="484.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="517.0" y="105.0" text-anchor="middle">head 5</text><rect class="box-1" x="562.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="595.0" y="105.0" text-anchor="middle">head 6</text><rect class="box-1" x="640.0" y="80.0" width="66.0" height="40.0" rx="6"/><text class="t-tick" x="673.0" y="105.0" text-anchor="middle">head 7</text><rect class="box-2" x="406.0" y="132.0" width="144.0" height="34.0" rx="6"/><text class="t-tick" x="478.0" y="154.0" text-anchor="middle">KV head 1</text><rect class="box" x="560.0" y="132.0" width="144.0" height="34.0" rx="6"/><text class="t-tick" x="632.0" y="154.0" text-anchor="middle">KV cache: half</text><rect class="box-3" x="406.0" y="176.0" width="298.0" height="30.0" rx="6"/><text class="t-tick" x="555.0" y="196.0" text-anchor="middle">partial output = its heads x its rows of W_o</text><line class="edge" x1="195.0" y1="214.0" x2="330.0" y2="248.0" marker-end="url(#ah7)"/><line class="edge" x1="555.0" y1="214.0" x2="430.0" y2="248.0" marker-end="url(#ah7)"/><rect class="box-3" x="300.0" y="248.0" width="160.0" height="36.0" rx="8"/><text class="t-note" x="380.0" y="271.0" text-anchor="middle">all-reduce (sum)</text><text class="t-tick" x="380.0" y="306.0" text-anchor="middle">Qwen2.5-0.5B in our demo: 14 query heads and 2 KV heads, so TP=2 gives 7 + 1 per GPU.</text></svg><figcaption>Attention split by heads. Each GPU computes its own heads, keeps only its own key-value heads in its KV cache, and produces a partial output for the all-reduce.</figcaption></figure>

There is a bonus: **each GPU only caches the keys and values of its own heads**. The KV cache is split $$p$$ ways along with the weights, so the room for conversations grows with every GPU you add.

There is also a limit. With grouped-query attention (Part 2), Llama 3.1 70B has 64 query heads but only **8 KV heads**. At TP=8 each GPU gets exactly one KV head. At TP=16 two GPUs would need the same KV head, so it must be copied, and the cache stops shrinking. That is one reason tensor parallelism usually stops at the 8 GPUs of one server.

The paper's own picture shows both blocks. The boxes marked $$f$$ and $$g$$ are where communication happens; in the forward pass $$f$$ does nothing and $$g$$ is the all-reduce:

> [!PAPER] Shoeybi et al., Megatron-LM · Figure 3 · page 4
> [![Megatron-LM Figure 3: Blocks of Transformer with Model Parallelism. (a) MLP: X goes through f, is multiplied by A1 and A2 on two GPUs, GeLU, then by B1 and B2, then g combines them before dropout. (b) Self-attention: queries, keys and values are split by heads into two groups, each with softmax and dropout, then multiplied by B1 and B2 and combined by g. f and g are conjugate: f is an identity operator in the forward pass and all reduce in the backward pass while g is an all reduce in the forward pass and identity in the backward pass.](https://ishwarj.com/img/multigpu/paper-megatron-fig3.pngr-megatron-fig3.png)](/img/multigpu/paper-megatron-fig3.png)
>
> **Context:** Figure 3, the paper's diagram of a tensor-parallel transformer layer on two GPUs.
>
> **What it says:** in the forward pass, each block starts with no communication ($$f$$ is the identity) and ends with one all-reduce ($$g$$).
>
> **Why it matters:** a whole transformer layer costs **two all-reduces** in inference: one after attention and one after the MLP.
>
> [Read Section 3](https://arxiv.org/pdf/1909.08053#page=4)

<figure class="fig"><svg viewBox="0 0 760 178" role="img" aria-label="Each layer runs attention on the local heads, an all-reduce, the local slice of the MLP, and a second all-reduce. Llama 3.1 70B has 160 all-reduces per decode step."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">One transformer layer under tensor parallelism, repeated for every layer</text><rect class="box-1" x="30.0" y="44.0" width="150.0" height="46.0" rx="6"/><text class="t-tick" x="105.0" y="72.0" text-anchor="middle">attention (my heads)</text><rect class="box-3" x="194.0" y="44.0" width="70.0" height="46.0" rx="6"/><text class="t-tick" x="229.0" y="72.0" text-anchor="middle">all-reduce</text><rect class="box-1" x="278.0" y="44.0" width="190.0" height="46.0" rx="6"/><text class="t-tick" x="373.0" y="72.0" text-anchor="middle">MLP (my slice)</text><rect class="box-3" x="482.0" y="44.0" width="70.0" height="46.0" rx="6"/><text class="t-tick" x="517.0" y="72.0" text-anchor="middle">all-reduce</text><text class="t-tick" x="570.0" y="72.0" text-anchor="start">... next layer</text><text class="t-note" x="380.0" y="120.0" text-anchor="middle">Llama 3.1 70B: 80 layers x 2 = 160 all-reduces for every decode step.</text><text class="t-tick" x="380.0" y="142.0" text-anchor="middle">Each carries batch x 8,192 x 2 bytes: 16 KiB at batch 1, 1 MiB at batch 64.</text><text class="t-tick" x="380.0" y="162.0" text-anchor="middle">Every GPU waits at each all-reduce, so its cost adds directly to the time per token.</text></svg><figcaption>Two all-reduces per layer, in every layer, for every forward pass. The GPUs cannot start the next block until the sum is complete.</figcaption></figure>

### A real tensor-parallel run, in two processes

Splitting one MLP is a toy. To be sure the whole recipe works, `tp_demo.py` runs **all 24 layers of Qwen2.5-0.5B** with tensor parallelism across two separate operating-system processes. Each process loads the model, keeps only its own half of every layer, and the two talk only through `torch.distributed.all_reduce`, the same call vLLM and SGLang make on NVIDIA GPUs (here with the CPU backend, gloo, since there is no NVIDIA GPU). Qwen2.5-0.5B has 14 query heads and 2 KV heads, so with TP=2 each process gets 7 query heads and 1 KV head.

Each process slices its share of the weights once, at load time:

```python
qs = slice(rank * self.hq * hd, (rank + 1) * self.hq * hd)     # this rank's query heads
ks = slice(rank * self.hkv * hd, (rank + 1) * self.hkv * hd)   # this rank's KV heads
fs = slice(rank * ffn, (rank + 1) * ffn)                       # this rank's MLP hidden units
self.layers.append(dict(
    wq=g('self_attn.q_proj.weight')[qs], bq=g('self_attn.q_proj.bias')[qs],      # column split
    wk=g('self_attn.k_proj.weight')[ks], bk=g('self_attn.k_proj.bias')[ks],
    wv=g('self_attn.v_proj.weight')[ks], bv=g('self_attn.v_proj.bias')[ks],
    wo=g('self_attn.o_proj.weight')[:, qs],                                        # row split
    wg=g('mlp.gate_proj.weight')[fs], wu=g('mlp.up_proj.weight')[fs],              # column split
    wd=g('mlp.down_proj.weight')[:, fs]))                                          # row split
```

PyTorch stores a linear layer's weight as (output, input), so a **column** split of the maths is a slice of the weight's **first** axis (`[qs]`), and a **row** split is a slice of its second axis (`[:, qs]`). The gate and up projections of Qwen's SwiGLU MLP are both column-split the same way, so their elementwise product stays local.

Then each layer of the forward pass is ordinary code, with exactly two collective calls:

```python
a = F.scaled_dot_product_attention(q, K, V, is_causal=ids.shape[0] > 1)   # my 7 heads only
a = a.transpose(0, 1).reshape(ids.shape[0], -1)
h = h + self.all_reduce(a @ L['wo'].T)       # all-reduce 1 of 2 in this layer
x = rms(h, L['ln2'], c.rms_norm_eps)
m = F.silu(x @ L['wg'].T) * (x @ L['wu'].T)  # my half of the MLP
h = h + self.all_reduce(m @ L['wd'].T)       # all-reduce 2 of 2 in this layer
```

`self.all_reduce` wraps `dist.all_reduce`, which replaces each process's tensor with the sum over both processes, and counts the calls and bytes. Everything outside the two calls (embeddings, normalisation, the residual additions, the output head) is simply done on both processes, which is how Megatron handles them too (it can also split the vocabulary, which this demo skips).

The script compares the last-token logits with the unsplit Hugging Face model and greedily generates 24 tokens both ways:

[![Terminal output of tp_demo.py part B: TP=1 holds 100% of the layer weights and 912 KiB of KV cache, max logit difference 4.9e-05 against Hugging Face, same 24 greedy tokens; TP=2 rank 0 holds 50% of the layer weights and 456 KiB of KV cache, max logit difference 5.0e-05, same 24 tokens; 48 all-reduce calls per token carrying 168 KiB, and the generated continuation](https://ishwarj.com/img/multigpu/tp_demo_b-run.pngu/tp_demo_b-run.png)](/img/multigpu/tp_demo_b-run.png)

The split model gives the **same 24 tokens** as the original, and the logits agree to about $$5\times10^{-5}$$, the same small difference that the unsplit re-implementation has (float32 sums in a different order). Each process holds exactly half of the layer weights and half of the KV cache. And every decode token costs **48 all-reduces**: 24 layers times two.

The timings in that run are not a speed-up: both "GPUs" are processes on one CPU, sharing the same cores and memory, so splitting gives each process half the work but no extra hardware to do it with. This demo checks correctness and counts messages; the next pages estimate speed.

### How many bytes per token?

Each all-reduce carries one activation vector per token: $$d$$ numbers. Per decode step:

$$
\text{calls} = 2L, \qquad \text{bytes per call} = b\times d\times s
$$

where:

- $$L$$ is the number of layers (two all-reduces each);
- $$b$$ is the number of tokens in the step (the batch size during decode);
- $$d$$ is the model width and $$s$$ the bytes per number (2 for BF16).

**Worked example, Qwen2.5-0.5B in the demo:** $$2\times24 = 48$$ calls, each $$1\times896\times4 = 3{,}584$$ bytes in float32, so $$48\times3{,}584 = 172{,}032$$ bytes = 168 KiB per token, exactly the 168.0 KiB the script counted.

**Worked example, Llama 3.1 70B in BF16:** $$2\times80 = 160$$ all-reduces per step. At batch 1 each carries $$8{,}192\times2 = 16$$ KiB; at batch 64, 1 MiB. Compare that with what each GPU reads from its own memory at TP=8: 17.6 GB of weights. The bytes sent are tiny next to the bytes read. But there are **160 separate messages**, and each must finish before the layer can go on. From Section 2: when messages are small, it is their count, through $$\alpha$$, that costs time.

### What each GPU's share costs: a measurement

To see how the work per GPU shrinks, `measure_mps.py` times one real Llama 3.1 8B MLP matrix (14,336 by 4,096, BF16, 117 MB) on the M5 Pro's GPU, whole and cut to the half and quarter that TP=2 and TP=4 would give each GPU:

<figure class="fig"><svg viewBox="0 0 760 374" role="img" aria-label="Measured time to multiply one Llama 3.1 8B MLP matrix by 1 to 4,096 tokens, whole and as the half and quarter shards that TP=2 and TP=4 would give each GPU."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="s1" x="150.0" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="168.0" y="20.0" text-anchor="start">whole matrix</text><rect class="s2" x="262.4" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="280.4" y="20.0" text-anchor="start">TP=2 shard</text><rect class="s3" x="360.4" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="378.4" y="20.0" text-anchor="start">TP=4 shard</text><text class="t-note" x="140.0" y="62.0" text-anchor="end">1 token</text><g class="mark"><title>1 tokens TP=1: 0.879 ms</title><rect class="s1" x="150.0" y="36.0" width="470.0" height="14.0" rx="3"/></g><text class="t-tick" x="626.0" y="48.0" text-anchor="start">0.879 ms</text><g class="mark"><title>1 tokens TP=2: 0.690 ms</title><rect class="s2" x="150.0" y="53.0" width="368.9" height="14.0" rx="3"/></g><text class="t-tick" x="524.9" y="65.0" text-anchor="start">0.690 ms  1.27x</text><g class="mark"><title>1 tokens TP=4: 0.728 ms</title><rect class="s3" x="150.0" y="70.0" width="389.4" height="14.0" rx="3"/></g><text class="t-tick" x="545.4" y="82.0" text-anchor="start">0.728 ms  1.21x</text><text class="t-note" x="140.0" y="126.0" text-anchor="end">16 tokens</text><g class="mark"><title>16 tokens TP=1: 1.388 ms</title><rect class="s1" x="150.0" y="100.0" width="470.0" height="14.0" rx="3"/></g><text class="t-tick" x="626.0" y="112.0" text-anchor="start">1.388 ms</text><g class="mark"><title>16 tokens TP=2: 0.930 ms</title><rect class="s2" x="150.0" y="117.0" width="315.1" height="14.0" rx="3"/></g><text class="t-tick" x="471.1" y="129.0" text-anchor="start">0.930 ms  1.49x</text><g class="mark"><title>16 tokens TP=4: 0.737 ms</title><rect class="s3" x="150.0" y="134.0" width="249.7" height="14.0" rx="3"/></g><text class="t-tick" x="405.7" y="146.0" text-anchor="start">0.737 ms  1.88x</text><text class="t-note" x="140.0" y="190.0" text-anchor="end">128 tokens</text><g class="mark"><title>128 tokens TP=1: 1.546 ms</title><rect class="s1" x="150.0" y="164.0" width="470.0" height="14.0" rx="3"/></g><text class="t-tick" x="626.0" y="176.0" text-anchor="start">1.546 ms</text><g class="mark"><title>128 tokens TP=2: 1.022 ms</title><rect class="s2" x="150.0" y="181.0" width="310.7" height="14.0" rx="3"/></g><text class="t-tick" x="466.7" y="193.0" text-anchor="start">1.022 ms  1.51x</text><g class="mark"><title>128 tokens TP=4: 0.819 ms</title><rect class="s3" x="150.0" y="198.0" width="248.8" height="14.0" rx="3"/></g><text class="t-tick" x="404.8" y="210.0" text-anchor="start">0.819 ms  1.89x</text><text class="t-note" x="140.0" y="254.0" text-anchor="end">1,024 tokens</text><g class="mark"><title>1024 tokens TP=1: 6.441 ms</title><rect class="s1" x="150.0" y="228.0" width="470.0" height="14.0" rx="3"/></g><text class="t-tick" x="626.0" y="240.0" text-anchor="start">6.441 ms</text><g class="mark"><title>1024 tokens TP=2: 3.825 ms</title><rect class="s2" x="150.0" y="245.0" width="279.1" height="14.0" rx="3"/></g><text class="t-tick" x="435.1" y="257.0" text-anchor="start">3.825 ms  1.68x</text><g class="mark"><title>1024 tokens TP=4: 2.023 ms</title><rect class="s3" x="150.0" y="262.0" width="147.6" height="14.0" rx="3"/></g><text class="t-tick" x="303.6" y="274.0" text-anchor="start">2.023 ms  3.18x</text><text class="t-note" x="140.0" y="318.0" text-anchor="end">4,096 tokens</text><g class="mark"><title>4096 tokens TP=1: 32.829 ms</title><rect class="s1" x="150.0" y="292.0" width="470.0" height="14.0" rx="3"/></g><text class="t-tick" x="626.0" y="304.0" text-anchor="start">32.829 ms</text><g class="mark"><title>4096 tokens TP=2: 16.181 ms</title><rect class="s2" x="150.0" y="309.0" width="231.7" height="14.0" rx="3"/></g><text class="t-tick" x="387.7" y="321.0" text-anchor="start">16.181 ms  2.03x</text><g class="mark"><title>4096 tokens TP=4: 9.032 ms</title><rect class="s3" x="150.0" y="326.0" width="129.3" height="14.0" rx="3"/></g><text class="t-tick" x="285.3" y="338.0" text-anchor="start">9.032 ms  3.63x</text><text class="t-tick" x="380.0" y="362.0" text-anchor="middle">Measured on the Apple GPU (mps), BF16, 14,336 x 4,096 matrix. Bars scaled per row.</text></svg><figcaption>Measured on the Apple GPU (mps): one 8B MLP matrix, whole and as TP=2 and TP=4 shards, for 1 to 4,096 tokens. Fastest of 40 runs.</figcaption></figure>

[![Terminal output of measure_mps.py: GPU copy bandwidth, then for 1 to 4,096 tokens the time, TFLOP/s, GB/s and speedup of the whole matrix and of TP=2 and TP=4 shards, then the all-reduce bytes each shard would send](https://ishwarj.com/img/multigpu/measure_mps-run.pngmeasure_mps-run.png)](/img/multigpu/measure_mps-run.png)

Two regimes show up. With **4,096 tokens** the work is arithmetic, and the shards behave as hoped: the half takes half the time (2.03x faster) and the quarter close to a quarter (3.63x). With **one token**, the whole matrix is a 117 MB read at 134 GB/s, 0.88 ms. But the half and quarter shards take 0.69 and 0.73 ms, barely faster. Below a certain size, every GPU operation has a fixed cost of its own (launching the kernel and getting it going), and splitting the work cannot go under that floor. It is the same shape as the alpha of Section 2, inside one GPU. Engines fight it by capturing a whole decode step as one CUDA graph, so that hundreds of small launches cost about one; the principle stays: cutting small work very finely gives diminishing returns, even before any communication.

The last block of the output adds the communication those shards would need: the partial outputs are only 8 KiB per token, which takes nanoseconds on any link. The bytes are not the problem; the number of separate exchanges is. (The copy bandwidth here, 153 GB/s, is lower than the 262 GB/s measured in Part 1 because other work was running at the same time; the fastest of 40 runs is reported for every number.)

### Putting it together: TP on 70B

Now add the communication back. `layout_model.py` estimates one Llama 3.1 70B decode step on H100s as the time to read the weights and the KV cache (or to do the arithmetic, whichever is longer) plus 160 all-reduces:

$$
t_{\text{step}} = \max\!\left(\frac{W + K}{p\,B_{\text{HBM}}},\ \frac{2Nb}{p\,F}\right) + 2L\left(2\alpha + \frac{2(p-1)}{p}\cdot\frac{b\,d\,s}{\beta}\right)
$$

where:

- $$W$$ is the weight bytes and $$K$$ the KV-cache bytes read in the step ($$b$$ sequences of 4,096 tokens each);
- $$F$$ is one GPU's peak BF16 rate (989 TFLOP/s for an H100) and $$2Nb$$ the arithmetic for $$b$$ tokens;
- the last term is $$2L$$ all-reduces, each costing two message delays ($$2\alpha$$) plus its bytes, using the two-step all-reduce explained in Section 4, with $$\beta$$ = 450 GB/s;
- $$\alpha = 5\ \mu$$s is an **assumption** (NVIDIA publishes none); it is varied below.

<figure class="fig"><svg viewBox="0 0 760 264" role="img" aria-label="Modelled decode step for Llama 3.1 70B. At batch 1: 22.9 ms on TP=2, 12.2 ms on TP=4, 6.9 ms on TP=8, where all-reduces are 23% of the step. At batch 64: 19.1 ms on TP=4 and 10.7 ms on TP=8."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="s1" x="170.0" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="188.0" y="20.0" text-anchor="start">read weights + KV (memory)</text><rect class="s3" x="383.2" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="401.2" y="20.0" text-anchor="start">all-reduces (communication)</text><text class="t-note" x="160.0" y="54.0" text-anchor="end">batch 1, TP=2</text><g class="mark"><title>batch 1 TP=2: 21.26 ms memory/compute, 1.61 ms all-reduce</title><rect class="s1" x="170.0" y="40.0" width="390.5" height="22.0" rx="3"/><rect class="s3" x="560.5" y="40.0" width="29.5" height="22.0" rx="3"/></g><text class="t-val" x="598.0" y="56.0" text-anchor="start">22.9 ms  (comm 7%)</text><text class="t-note" x="160.0" y="94.0" text-anchor="end">batch 1, TP=4</text><g class="mark"><title>batch 1 TP=4: 10.63 ms memory/compute, 1.61 ms all-reduce</title><rect class="s1" x="170.0" y="80.0" width="195.3" height="22.0" rx="3"/><rect class="s3" x="365.3" y="80.0" width="29.5" height="22.0" rx="3"/></g><text class="t-val" x="402.8" y="96.0" text-anchor="start">12.2 ms  (comm 13%)</text><text class="t-note" x="160.0" y="134.0" text-anchor="end">batch 1, TP=8</text><g class="mark"><title>batch 1 TP=8: 5.32 ms memory/compute, 1.61 ms all-reduce</title><rect class="s1" x="170.0" y="120.0" width="97.6" height="22.0" rx="3"/><rect class="s3" x="267.6" y="120.0" width="29.6" height="22.0" rx="3"/></g><text class="t-val" x="305.2" y="136.0" text-anchor="start">6.9 ms  (comm 23%)</text><text class="t-note" x="160.0" y="174.0" text-anchor="end">batch 64, TP=4</text><g class="mark"><title>batch 64 TP=4: 16.94 ms memory/compute, 2.16 ms all-reduce</title><rect class="s1" x="170.0" y="160.0" width="311.2" height="22.0" rx="3"/><rect class="s3" x="481.2" y="160.0" width="39.7" height="22.0" rx="3"/></g><text class="t-val" x="528.8" y="176.0" text-anchor="start">19.1 ms  (comm 11%)</text><text class="t-note" x="160.0" y="214.0" text-anchor="end">batch 64, TP=8</text><g class="mark"><title>batch 64 TP=8: 8.47 ms memory/compute, 2.25 ms all-reduce</title><rect class="s1" x="170.0" y="200.0" width="155.6" height="22.0" rx="3"/><rect class="s3" x="325.6" y="200.0" width="41.4" height="22.0" rx="3"/></g><text class="t-val" x="374.9" y="216.0" text-anchor="start">10.7 ms  (comm 21%)</text><text class="t-tick" x="380.0" y="252.0" text-anchor="middle">Roofline model with H100 peaks; all-reduce = 2 x 5 us + bytes / 450 GB/s, 160 per step (alpha assumed).</text></svg><figcaption>Modelled Llama 3.1 70B decode step on H100s, split into reading memory and all-reduces. Peak numbers throughout, so real systems are slower.</figcaption></figure>

[![Terminal output of layout_model.py: tensor parallel scaling for Llama 3.1 70B at batch 1 and 64, the alpha sensitivity, the one-node comparison of TP=8, TP=4 x 2 and TP=2 x 4, the 405B two-node comparison and the pipeline schedule](https://ishwarj.com/img/multigpu/layout_model-run.pngayout_model-run.png)](/img/multigpu/layout_model-run.png)

**Worked example at batch 1.** TP=2: reading 141 GB at $$2\times3.35$$ TB/s takes 21.3 ms (with the cache); 160 all-reduces at $$2\times5\ \mu\text{s}$$ each add 1.6 ms; total **22.9 ms**. TP=8: memory 5.3 ms, all-reduces still **1.6 ms**, total **6.9 ms**. Going from 2 to 8 GPUs cut the memory time by 4x but left the communication exactly where it was, so the step is only 3.3x faster, and communication grew from 7% to 23% of it. With $$\alpha = 2\ \mu$$s the TP=8 step is 6.0 ms; with $$\alpha = 10\ \mu$$s it is 8.5 ms.

This is a general law, and the Google team that scaled PaLM inference put it plainly:

> [!PAPER] Pope et al., Efficiently Scaling Transformer Inference · Section 3.2.1 · page 4
> [![Pope et al.: As we parallelize the computation across more chips, the memory latency and compute latency does decrease, often near-linearly. However, the communication latency remains roughly constant independent of the number of chips used, since the entire activation matrix is aggregated across chips for every pair of matrix multiplications. As the number of chips grows larger, communication becomes a bottleneck.](https://ishwarj.com/img/multigpu/paper-pope-constant.pngr-pope-constant.png)](/img/multigpu/paper-pope-constant.png)
>
> **Context:** the paper's analysis of the Megatron-style "1D weight-stationary" layout for feed-forward layers, on TPU v4 chips.
>
> **What it says:** splitting wider shrinks memory and compute time almost in proportion, but the communication stays about the same size, because every chip must see the whole activation after every pair of matrix multiplications.
>
> **Why it matters:** tensor parallelism has diminishing returns built in. Past a point, more GPUs per copy of the model buy almost nothing, and the GPUs are better used as more copies.
>
> [Read Section 3.2.1](https://arxiv.org/pdf/2211.05102#page=4)

Two practical consequences follow. **Tensor parallelism belongs where $$\alpha$$ is small and links are fast**: inside one NVLink server. And because it needs that, inference engines put real effort into making all-reduce fast: vLLM and TensorRT-LLM ship their own all-reduce kernels for small messages on NVLink, rather than relying only on the general NCCL library. Section 4 shows why that helps.

## 4. Collectives: how a group of GPUs exchanges data

### The four you need

When a group of GPUs exchange data in a fixed pattern, the operation is called a **collective**. NVIDIA's NCCL library (pronounced "nickel") implements them on GPUs; `torch.distributed` calls it. Four collectives cover almost everything in this article.

> [!DEFINITION] All-reduce
> Every GPU starts with a vector of the same length. Every GPU ends with the element-by-element sum of all of them. Tensor parallelism uses it after each attention and MLP block.

> [!DEFINITION] All-gather
> Every GPU starts with one piece. Every GPU ends with all the pieces, in order.

> [!DEFINITION] Reduce-scatter
> Every GPU starts with a full vector. GPU $$r$$ ends with the sum of piece $$r$$ only. An all-reduce is exactly a reduce-scatter followed by an all-gather.

> [!DEFINITION] All-to-all
> Every GPU starts with one piece addressed to each GPU. Piece $$r$$ of every GPU goes to GPU $$r$$: like transposing a table. Expert parallelism uses it to send tokens to their experts.

`collectives.py` runs all four on four simulated GPUs (list entries in NumPy), where rank $$r$$ starts with $$[10r, 10r+1, 10r+2, 10r+3]$$:

<figure class="fig"><svg viewBox="0 0 760 312" role="img" aria-label="Four collectives on four ranks. All-reduce gives everyone the elementwise sum [60, 64, 68, 72]. All-gather gives everyone [0, 11, 22, 33]. Reduce-scatter gives rank r the sum of piece r. All-to-all sends piece r of every rank to rank r, a transpose."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="plane" x="10.0" y="14.0" width="178.0" height="260.0" rx="10"/><text class="t-title" x="99.0" y="38.0" text-anchor="middle">all-reduce</text><text class="t-tick" x="99.0" y="58.0" text-anchor="middle">all get the full sum</text><text class="t-tick" x="24.0" y="95.0" text-anchor="start">r0</text><rect class="box-1" x="48.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="64.0" y="95.0" text-anchor="middle">60</text><rect class="box-1" x="82.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="98.0" y="95.0" text-anchor="middle">64</text><rect class="box-1" x="116.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="132.0" y="95.0" text-anchor="middle">68</text><rect class="box-1" x="150.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="166.0" y="95.0" text-anchor="middle">72</text><text class="t-tick" x="24.0" y="135.0" text-anchor="start">r1</text><rect class="box-1" x="48.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="64.0" y="135.0" text-anchor="middle">60</text><rect class="box-1" x="82.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="98.0" y="135.0" text-anchor="middle">64</text><rect class="box-1" x="116.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="132.0" y="135.0" text-anchor="middle">68</text><rect class="box-1" x="150.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="166.0" y="135.0" text-anchor="middle">72</text><text class="t-tick" x="24.0" y="175.0" text-anchor="start">r2</text><rect class="box-1" x="48.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="64.0" y="175.0" text-anchor="middle">60</text><rect class="box-1" x="82.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="98.0" y="175.0" text-anchor="middle">64</text><rect class="box-1" x="116.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="132.0" y="175.0" text-anchor="middle">68</text><rect class="box-1" x="150.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="166.0" y="175.0" text-anchor="middle">72</text><text class="t-tick" x="24.0" y="215.0" text-anchor="start">r3</text><rect class="box-1" x="48.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="64.0" y="215.0" text-anchor="middle">60</text><rect class="box-1" x="82.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="98.0" y="215.0" text-anchor="middle">64</text><rect class="box-1" x="116.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="132.0" y="215.0" text-anchor="middle">68</text><rect class="box-1" x="150.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="166.0" y="215.0" text-anchor="middle">72</text><text class="t-tick" x="99.0" y="260.0" text-anchor="middle">tensor parallel (Megatron)</text><rect class="plane" x="198.0" y="14.0" width="178.0" height="260.0" rx="10"/><text class="t-title" x="287.0" y="38.0" text-anchor="middle">all-gather</text><text class="t-tick" x="287.0" y="58.0" text-anchor="middle">all get every piece</text><text class="t-tick" x="212.0" y="95.0" text-anchor="start">r0</text><rect class="box-1" x="236.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="252.0" y="95.0" text-anchor="middle">0</text><rect class="box-1" x="270.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="286.0" y="95.0" text-anchor="middle">11</text><rect class="box-1" x="304.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="320.0" y="95.0" text-anchor="middle">22</text><rect class="box-1" x="338.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="354.0" y="95.0" text-anchor="middle">33</text><text class="t-tick" x="212.0" y="135.0" text-anchor="start">r1</text><rect class="box-1" x="236.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="252.0" y="135.0" text-anchor="middle">0</text><rect class="box-1" x="270.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="286.0" y="135.0" text-anchor="middle">11</text><rect class="box-1" x="304.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="320.0" y="135.0" text-anchor="middle">22</text><rect class="box-1" x="338.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="354.0" y="135.0" text-anchor="middle">33</text><text class="t-tick" x="212.0" y="175.0" text-anchor="start">r2</text><rect class="box-1" x="236.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="252.0" y="175.0" text-anchor="middle">0</text><rect class="box-1" x="270.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="286.0" y="175.0" text-anchor="middle">11</text><rect class="box-1" x="304.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="320.0" y="175.0" text-anchor="middle">22</text><rect class="box-1" x="338.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="354.0" y="175.0" text-anchor="middle">33</text><text class="t-tick" x="212.0" y="215.0" text-anchor="start">r3</text><rect class="box-1" x="236.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="252.0" y="215.0" text-anchor="middle">0</text><rect class="box-1" x="270.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="286.0" y="215.0" text-anchor="middle">11</text><rect class="box-1" x="304.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="320.0" y="215.0" text-anchor="middle">22</text><rect class="box-1" x="338.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="354.0" y="215.0" text-anchor="middle">33</text><text class="t-tick" x="287.0" y="260.0" text-anchor="middle">data-parallel attention</text><rect class="plane" x="386.0" y="14.0" width="178.0" height="260.0" rx="10"/><text class="t-title" x="475.0" y="38.0" text-anchor="middle">reduce-scatter</text><text class="t-tick" x="475.0" y="58.0" text-anchor="middle">rank r: sum of piece r</text><text class="t-tick" x="400.0" y="95.0" text-anchor="start">r0</text><rect class="box-1" x="424.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="440.0" y="95.0" text-anchor="middle">60</text><text class="t-tick" x="400.0" y="135.0" text-anchor="start">r1</text><rect class="box-1" x="424.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="440.0" y="135.0" text-anchor="middle">64</text><text class="t-tick" x="400.0" y="175.0" text-anchor="start">r2</text><rect class="box-1" x="424.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="440.0" y="175.0" text-anchor="middle">68</text><text class="t-tick" x="400.0" y="215.0" text-anchor="start">r3</text><rect class="box-1" x="424.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="440.0" y="215.0" text-anchor="middle">72</text><text class="t-tick" x="475.0" y="260.0" text-anchor="middle">half of a ring all-reduce</text><rect class="plane" x="574.0" y="14.0" width="178.0" height="260.0" rx="10"/><text class="t-title" x="663.0" y="38.0" text-anchor="middle">all-to-all</text><text class="t-tick" x="663.0" y="58.0" text-anchor="middle">piece r goes to rank r</text><text class="t-tick" x="588.0" y="95.0" text-anchor="start">r0</text><rect class="box-2" x="612.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="628.0" y="95.0" text-anchor="middle">0</text><rect class="box-1" x="646.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="662.0" y="95.0" text-anchor="middle">10</text><rect class="box-1" x="680.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="696.0" y="95.0" text-anchor="middle">20</text><rect class="box-1" x="714.0" y="76.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="730.0" y="95.0" text-anchor="middle">30</text><text class="t-tick" x="588.0" y="135.0" text-anchor="start">r1</text><rect class="box-1" x="612.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="628.0" y="135.0" text-anchor="middle">1</text><rect class="box-2" x="646.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="662.0" y="135.0" text-anchor="middle">11</text><rect class="box-1" x="680.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="696.0" y="135.0" text-anchor="middle">21</text><rect class="box-1" x="714.0" y="116.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="730.0" y="135.0" text-anchor="middle">31</text><text class="t-tick" x="588.0" y="175.0" text-anchor="start">r2</text><rect class="box-1" x="612.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="628.0" y="175.0" text-anchor="middle">2</text><rect class="box-1" x="646.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="662.0" y="175.0" text-anchor="middle">12</text><rect class="box-2" x="680.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="696.0" y="175.0" text-anchor="middle">22</text><rect class="box-1" x="714.0" y="156.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="730.0" y="175.0" text-anchor="middle">32</text><text class="t-tick" x="588.0" y="215.0" text-anchor="start">r3</text><rect class="box-1" x="612.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="628.0" y="215.0" text-anchor="middle">3</text><rect class="box-1" x="646.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="662.0" y="215.0" text-anchor="middle">13</text><rect class="box-1" x="680.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="696.0" y="215.0" text-anchor="middle">23</text><rect class="box-2" x="714.0" y="196.0" width="32.0" height="28.0" rx="3"/><text class="t-cell" x="730.0" y="215.0" text-anchor="middle">33</text><text class="t-tick" x="663.0" y="260.0" text-anchor="middle">expert parallel (MoE)</text><text class="t-tick" x="380.0" y="298.0" text-anchor="middle">Start: rank r holds [10r, 10r+1, 10r+2, 10r+3]. All-gather shares each rank&#x27;s own piece (piece r).</text></svg><figcaption>The four collectives on four ranks, with the numbers from collectives.py. All-to-all keeps piece r on rank r (highlighted) and swaps the rest.</figcaption></figure>

### Ring all-reduce, step by step

How do $$p$$ GPUs compute a sum without one GPU becoming a bottleneck? The classic answer is the **ring**. Arrange the GPUs in a circle and cut each vector into $$p$$ chunks.

1. **Reduce-scatter, $$p-1$$ steps.** At each step every GPU sends one chunk to its right-hand neighbour, which adds it to its own copy of that chunk. After $$p-1$$ steps, each GPU holds one chunk that contains the complete sum.
2. **All-gather, $$p-1$$ more steps.** Each GPU passes its finished chunk to the right, and the finished chunks travel round the ring until everyone has all of them.

Here is the trace for four ranks holding $$[1,2,3,4]$$ times 1, 2, 3 and 4:

<figure class="fig"><svg viewBox="0 0 760 298" role="img" aria-label="Ring all-reduce on four ranks holding [1,2,3,4] times 1 to 4. Three reduce-scatter steps leave each rank with one fully summed chunk; three all-gather steps spread the sums, so every rank ends with [10, 20, 30, 40]."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-tick" x="116.0" y="26.0" text-anchor="middle">start</text><rect class="box-1" x="74.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="84.0" y="60.0" text-anchor="middle">1</text><rect class="box-1" x="95.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="105.0" y="60.0" text-anchor="middle">2</text><rect class="box-1" x="116.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="126.0" y="60.0" text-anchor="middle">3</text><rect class="box-1" x="137.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="147.0" y="60.0" text-anchor="middle">4</text><rect class="box-1" x="74.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="84.0" y="112.0" text-anchor="middle">2</text><rect class="box-1" x="95.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="105.0" y="112.0" text-anchor="middle">4</text><rect class="box-1" x="116.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="126.0" y="112.0" text-anchor="middle">6</text><rect class="box-1" x="137.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="147.0" y="112.0" text-anchor="middle">8</text><rect class="box-1" x="74.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="84.0" y="164.0" text-anchor="middle">3</text><rect class="box-1" x="95.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="105.0" y="164.0" text-anchor="middle">6</text><rect class="box-1" x="116.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="126.0" y="164.0" text-anchor="middle">9</text><rect class="box-1" x="137.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="147.0" y="164.0" text-anchor="middle">12</text><rect class="box-1" x="74.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="84.0" y="216.0" text-anchor="middle">4</text><rect class="box-1" x="95.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="105.0" y="216.0" text-anchor="middle">8</text><rect class="box-1" x="116.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="126.0" y="216.0" text-anchor="middle">12</text><rect class="box-1" x="137.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="147.0" y="216.0" text-anchor="middle">16</text><text class="t-tick" x="208.0" y="26.0" text-anchor="middle">reduce 1</text><rect class="box" x="166.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="176.0" y="60.0" text-anchor="middle">1</text><rect class="box" x="187.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="197.0" y="60.0" text-anchor="middle">2</text><rect class="box" x="208.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="218.0" y="60.0" text-anchor="middle">3</text><rect class="box" x="229.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="239.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="166.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="176.0" y="112.0" text-anchor="middle">3</text><rect class="box" x="187.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="197.0" y="112.0" text-anchor="middle">4</text><rect class="box" x="208.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="218.0" y="112.0" text-anchor="middle">6</text><rect class="box" x="229.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="239.0" y="112.0" text-anchor="middle">8</text><rect class="box" x="166.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="176.0" y="164.0" text-anchor="middle">3</text><rect class="box" x="187.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="197.0" y="164.0" text-anchor="middle">10</text><rect class="box" x="208.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="218.0" y="164.0" text-anchor="middle">9</text><rect class="box" x="229.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="239.0" y="164.0" text-anchor="middle">12</text><rect class="box" x="166.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="176.0" y="216.0" text-anchor="middle">4</text><rect class="box" x="187.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="197.0" y="216.0" text-anchor="middle">8</text><rect class="box" x="208.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="218.0" y="216.0" text-anchor="middle">21</text><rect class="box" x="229.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="239.0" y="216.0" text-anchor="middle">16</text><text class="t-tick" x="300.0" y="26.0" text-anchor="middle">reduce 2</text><rect class="box" x="258.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="268.0" y="60.0" text-anchor="middle">1</text><rect class="box" x="279.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="289.0" y="60.0" text-anchor="middle">2</text><rect class="box" x="300.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="310.0" y="60.0" text-anchor="middle">24</text><rect class="box" x="321.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="331.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="258.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="268.0" y="112.0" text-anchor="middle">3</text><rect class="box" x="279.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="289.0" y="112.0" text-anchor="middle">4</text><rect class="box" x="300.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="310.0" y="112.0" text-anchor="middle">6</text><rect class="box" x="321.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="331.0" y="112.0" text-anchor="middle">28</text><rect class="box" x="258.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="268.0" y="164.0" text-anchor="middle">6</text><rect class="box" x="279.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="289.0" y="164.0" text-anchor="middle">10</text><rect class="box" x="300.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="310.0" y="164.0" text-anchor="middle">9</text><rect class="box" x="321.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="331.0" y="164.0" text-anchor="middle">12</text><rect class="box" x="258.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="268.0" y="216.0" text-anchor="middle">4</text><rect class="box" x="279.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="289.0" y="216.0" text-anchor="middle">18</text><rect class="box" x="300.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="310.0" y="216.0" text-anchor="middle">21</text><rect class="box" x="321.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="331.0" y="216.0" text-anchor="middle">16</text><text class="t-tick" x="392.0" y="26.0" text-anchor="middle">reduce 3</text><rect class="box" x="350.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="360.0" y="60.0" text-anchor="middle">1</text><rect class="box-3" x="371.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="381.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="392.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="402.0" y="60.0" text-anchor="middle">24</text><rect class="box" x="413.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="423.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="350.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="360.0" y="112.0" text-anchor="middle">3</text><rect class="box" x="371.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="381.0" y="112.0" text-anchor="middle">4</text><rect class="box-3" x="392.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="402.0" y="112.0" text-anchor="middle">30</text><rect class="box" x="413.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="423.0" y="112.0" text-anchor="middle">28</text><rect class="box" x="350.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="360.0" y="164.0" text-anchor="middle">6</text><rect class="box" x="371.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="381.0" y="164.0" text-anchor="middle">10</text><rect class="box" x="392.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="402.0" y="164.0" text-anchor="middle">9</text><rect class="box-3" x="413.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="423.0" y="164.0" text-anchor="middle">40</text><rect class="box-3" x="350.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="360.0" y="216.0" text-anchor="middle">10</text><rect class="box" x="371.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="381.0" y="216.0" text-anchor="middle">18</text><rect class="box" x="392.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="402.0" y="216.0" text-anchor="middle">21</text><rect class="box" x="413.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="423.0" y="216.0" text-anchor="middle">16</text><text class="t-tick" x="484.0" y="26.0" text-anchor="middle">gather 1</text><rect class="box-3" x="442.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="452.0" y="60.0" text-anchor="middle">10</text><rect class="box-3" x="463.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="473.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="484.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="494.0" y="60.0" text-anchor="middle">24</text><rect class="box" x="505.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="515.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="442.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="452.0" y="112.0" text-anchor="middle">3</text><rect class="box-3" x="463.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="473.0" y="112.0" text-anchor="middle">20</text><rect class="box-3" x="484.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="494.0" y="112.0" text-anchor="middle">30</text><rect class="box" x="505.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="515.0" y="112.0" text-anchor="middle">28</text><rect class="box" x="442.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="452.0" y="164.0" text-anchor="middle">6</text><rect class="box" x="463.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="473.0" y="164.0" text-anchor="middle">10</text><rect class="box-3" x="484.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="494.0" y="164.0" text-anchor="middle">30</text><rect class="box-3" x="505.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="515.0" y="164.0" text-anchor="middle">40</text><rect class="box-3" x="442.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="452.0" y="216.0" text-anchor="middle">10</text><rect class="box" x="463.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="473.0" y="216.0" text-anchor="middle">18</text><rect class="box" x="484.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="494.0" y="216.0" text-anchor="middle">21</text><rect class="box-3" x="505.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="515.0" y="216.0" text-anchor="middle">40</text><text class="t-tick" x="576.0" y="26.0" text-anchor="middle">gather 2</text><rect class="box-3" x="534.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="544.0" y="60.0" text-anchor="middle">10</text><rect class="box-3" x="555.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="565.0" y="60.0" text-anchor="middle">20</text><rect class="box" x="576.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="586.0" y="60.0" text-anchor="middle">24</text><rect class="box-3" x="597.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="607.0" y="60.0" text-anchor="middle">40</text><rect class="box-3" x="534.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="544.0" y="112.0" text-anchor="middle">10</text><rect class="box-3" x="555.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="565.0" y="112.0" text-anchor="middle">20</text><rect class="box-3" x="576.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="586.0" y="112.0" text-anchor="middle">30</text><rect class="box" x="597.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="607.0" y="112.0" text-anchor="middle">28</text><rect class="box" x="534.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="544.0" y="164.0" text-anchor="middle">6</text><rect class="box-3" x="555.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="565.0" y="164.0" text-anchor="middle">20</text><rect class="box-3" x="576.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="586.0" y="164.0" text-anchor="middle">30</text><rect class="box-3" x="597.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="607.0" y="164.0" text-anchor="middle">40</text><rect class="box-3" x="534.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="544.0" y="216.0" text-anchor="middle">10</text><rect class="box" x="555.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="565.0" y="216.0" text-anchor="middle">18</text><rect class="box-3" x="576.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="586.0" y="216.0" text-anchor="middle">30</text><rect class="box-3" x="597.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="607.0" y="216.0" text-anchor="middle">40</text><text class="t-tick" x="668.0" y="26.0" text-anchor="middle">gather 3</text><rect class="box-3" x="626.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="636.0" y="60.0" text-anchor="middle">10</text><rect class="box-3" x="647.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="657.0" y="60.0" text-anchor="middle">20</text><rect class="box-3" x="668.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="678.0" y="60.0" text-anchor="middle">30</text><rect class="box-3" x="689.0" y="40.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="699.0" y="60.0" text-anchor="middle">40</text><rect class="box-3" x="626.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="636.0" y="112.0" text-anchor="middle">10</text><rect class="box-3" x="647.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="657.0" y="112.0" text-anchor="middle">20</text><rect class="box-3" x="668.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="678.0" y="112.0" text-anchor="middle">30</text><rect class="box-3" x="689.0" y="92.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="699.0" y="112.0" text-anchor="middle">40</text><rect class="box-3" x="626.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="636.0" y="164.0" text-anchor="middle">10</text><rect class="box-3" x="647.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="657.0" y="164.0" text-anchor="middle">20</text><rect class="box-3" x="668.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="678.0" y="164.0" text-anchor="middle">30</text><rect class="box-3" x="689.0" y="144.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="699.0" y="164.0" text-anchor="middle">40</text><rect class="box-3" x="626.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="636.0" y="216.0" text-anchor="middle">10</text><rect class="box-3" x="647.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="657.0" y="216.0" text-anchor="middle">20</text><rect class="box-3" x="668.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="678.0" y="216.0" text-anchor="middle">30</text><rect class="box-3" x="689.0" y="196.0" width="20.0" height="30.0" rx="3"/><text class="t-cell" x="699.0" y="216.0" text-anchor="middle">40</text><text class="t-note" x="62.0" y="60.0" text-anchor="end">rank 0</text><text class="t-note" x="62.0" y="112.0" text-anchor="end">rank 1</text><text class="t-note" x="62.0" y="164.0" text-anchor="end">rank 2</text><text class="t-note" x="62.0" y="216.0" text-anchor="end">rank 3</text><line class="drop" x1="435.0" y1="34.0" x2="435.0" y2="244.0"/><text class="t-tick" x="258.0" y="258.0" text-anchor="middle">reduce-scatter: add chunks round the ring</text><text class="t-tick" x="580.0" y="258.0" text-anchor="middle">all-gather: pass the sums round</text><text class="t-tick" x="380.0" y="284.0" text-anchor="middle">Highlighted: a chunk that already holds its final sum. After 2(p-1) = 6 steps every rank has [10, 20, 30, 40].</text></svg><figcaption>Ring all-reduce traced by collectives.py. Highlighted cells already hold their final sum. Every rank ends with [10, 20, 30, 40].</figcaption></figure>

[![Terminal output of collectives.py: the ring all-reduce trace with each rank's four numbers after every reduce-scatter and all-gather step, the check that every rank ends with the plain sum, the bytes each rank sends for p = 2, 4 and 8 against the formula 2(p-1)/p, and the other three collectives](https://ishwarj.com/img/multigpu/collectives-run.pngcollectives-run.png)](/img/multigpu/collectives-run.png)

### What the ring costs

Each step moves one chunk, $$n/p$$ bytes, from every GPU at the same time. There are $$2(p-1)$$ steps. So, with the alpha-beta model from Section 2:

$$
T_{\text{ring}} = 2(p-1)\left(\alpha + \frac{n}{p\,\beta}\right) = \underbrace{2(p-1)\,\alpha}_{\text{grows with } p} + \underbrace{\frac{2(p-1)}{p}\cdot\frac{n}{\beta}}_{\text{almost constant}}
$$

where:

- $$p$$ is the number of GPUs and $$n$$ the size of the vector being summed, in bytes;
- $$\alpha$$ is the fixed cost of one step and $$\beta$$ each GPU's sending bandwidth.

The byte term is the famous property of the ring: each GPU sends $$2(p-1)/p$$ times the vector, which is never more than **2x** the vector however many GPUs there are. The simulation counts the bytes and agrees: 1.000, 1.500 and 1.750 times $$n$$ for 2, 4 and 8 ranks. (NCCL's [performance notes](https://github.com/NVIDIA/nccl-tests/blob/master/doc/PERFORMANCE.md) use the same $$2(p-1)/p$$ factor to turn measured times into "bus bandwidth".)

The latency term is the catch. It grows with $$p$$: $$2(p-1) = 14$$ steps on 8 GPUs, and every GPU must wait for its neighbour at every step.

**Worked example: Llama 3.1 70B at batch 1, TP=8, NVLink.** Each all-reduce carries $$n = 16$$ KiB. Bytes: $$\frac{14}{8}\times\frac{16{,}384}{450\times10^9} = 64$$ ns. Latency: $$14\times\alpha$$, so $$28$$ to $$140\ \mu$$s for $$\alpha$$ between 2 and 10 $$\mu$$s. Times 160 all-reduces: **4.5 to 22.4 ms per token**, while the bytes alone would take 0.01 ms. A ring is the wrong shape for tiny messages.

### Fewer steps for small messages

That is why there are other algorithms. With NVSwitch every GPU can reach every other directly, so a GPU can send each peer its chunk in one step (a one-hop reduce-scatter) and then gather the sums in one more step: **two steps, whatever $$p$$ is**. NVIDIA describes this for TensorRT-LLM (their "MultiShot" all-reduce, which uses the switch to multicast):

> "This process is repeated 2N-2 times where N is the number of GPUs working together ... This increases latency, as all GPUs need to stay synchronized at every step of the ring." ([NVIDIA technical blog, *3x Faster AllReduce with NVSwitch and TensorRT-LLM MultiShot*, November 2024](https://developer.nvidia.com/blog/3x-faster-allreduce-with-nvswitch-and-tensorrt-llm-multishot/))

The two-step version costs about

$$
T_{\text{two-step}} \approx 2\alpha + \frac{2(p-1)}{p}\cdot\frac{n}{\beta}
$$

with the same symbols as before. Same bytes, but only two delays. For the 70B example: $$160\times2\times\alpha$$ = **0.65 to 3.2 ms per token**, about 7x less than the ring at TP=8. (NCCL itself also switches algorithm and protocol by message size; the point is that small all-reduces are a latency problem, and good implementations attack the step count.)

| Llama 3.1 70B, all 160 all-reduces of one decode step | Ring | Two-step | Bytes only |
|---|---|---|---|
| TP=2, batch 1, NVLink | 0.65 to 3.21 ms | 0.65 to 3.21 ms | 0.006 ms |
| TP=8, batch 1, NVLink | 4.49 to 22.41 ms | 0.65 to 3.21 ms | 0.010 ms |
| TP=8, batch 64, NVLink | 5.13 to 23.05 ms | 1.29 to 3.85 ms | 0.65 ms |
| TP=8, batch 64, PCIe Gen5 | 9.07 to 26.99 ms | 5.23 to 7.79 ms | 4.59 ms |
| TP=8, batch 64, one 400 Gb/s NIC | 10.35 to 28.27 ms | 6.51 to 9.07 ms | 5.87 ms |

Ranges are for $$\alpha$$ = 2 to 10 $$\mu$$s (assumed); bandwidths are per-direction peaks. Read the table by rows:

- At batch 1 the **bytes never matter**; only the number of steps does.
- At batch 64 the bytes start to count, and on **PCIe or the network** they alone cost 4.6 to 5.9 ms per token, as much as reading the weights. That is why the vLLM documentation tells you to avoid tensor parallelism on GPUs without NVLink (Section 8).

## 5. Pipeline parallelism: split the layers instead

### The idea, from training

> [!DEFINITION] Pipeline parallelism (PP)
> The model's layers are divided into consecutive groups called stages, and each GPU (or group of GPUs) holds one stage. A token's activations flow from stage 1 to stage 2 and onwards, like an assembly line. "PP=2" means two stages.

Pipeline parallelism communicates far less than tensor parallelism. Between two stages it sends the activations once ($$b\times d\times s$$ bytes, point to point), instead of two all-reduces in every layer. The cost is idle time. GPipe (Huang et al., 2019) introduced the standard picture:

> [!PAPER] Huang et al., GPipe · Figure 2 · page 3
> [![GPipe Figure 2: (a) An example neural network with sequential layers is partitioned across four accelerators. (b) The naive model parallelism strategy leads to severe under-utilization due to the sequential dependency of the network: only one device works at a time. (c) Pipeline parallelism divides the input mini-batch into smaller micro-batches, enabling different accelerators to work on different micro-batches simultaneously; the idle region is labelled Bubble.](https://ishwarj.com/img/multigpu/paper-gpipe-fig2.pngaper-gpipe-fig2.png)](/img/multigpu/paper-gpipe-fig2.png)
>
> **Context:** GPipe was a training system; panel (b) shows forward passes F and backward passes B on four devices.
>
> **What it says:** with one batch, only one device works at a time. Cutting the batch into micro-batches lets the devices overlap, leaving a smaller idle "bubble".
>
> **Why it matters:** the same two pictures apply to inference, minus the backward pass.
>
> [Read Section 2](https://arxiv.org/pdf/1811.06965#page=3)

> [!DEFINITION] Micro-batch and bubble
> A micro-batch is a slice of the batch that moves through the pipeline on its own, so that different stages can work on different slices at the same time. The bubble is the time a stage sits idle because no micro-batch has reached it yet, or because the next one is not ready.

GPipe gives the size of the bubble:

> [!PAPER] Huang et al., GPipe · Section 2.3 · page 4
> [![GPipe: As illustrated in Figure 2c, partitioning introduces some idle time per accelerator, which we refer to as the bubble overhead. This bubble time is O((K-1)/(M+K-1)) amortized over the number of micro-steps M. In our experiments, we found the bubble overhead to be negligible when M is at least 4 x K.](https://ishwarj.com/img/multigpu/paper-gpipe-bubble.pnger-gpipe-bubble.png)](/img/multigpu/paper-gpipe-bubble.png)
>
> **Context:** GPipe's performance analysis.
>
> **What it says:** with $$K$$ stages and $$M$$ micro-batches, the idle fraction is about $$\frac{K-1}{M+K-1}$$, small once $$M \ge 4K$$.
>
> [Read Section 2.3](https://arxiv.org/pdf/1811.06965#page=4)

**Worked example.** $$K = 4$$ stages. With $$M = 1$$: $$\frac{3}{4} = 75\%$$ idle. With $$M = 16$$: $$\frac{3}{19} = 16\%$$.

### Pipelines during decode

Decode adds a twist: the next token of a sequence cannot start until its previous token has left the **last** stage. One sequence alone keeps only one stage busy at a time. Several independent groups of sequences (micro-batches) fill the gaps. `layout_model.py` lays out the schedule for 4 stages, each micro-batch generating 3 tokens:

<figure class="fig"><svg viewBox="0 0 760 326" role="img" aria-label="Pipeline schedule on 4 GPUs. With one sequence each GPU is busy one slot in four (25%). With four micro-batches the GPUs are busy 80% of the time, but each sequence still waits 4 slots per token."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-note" x="20.0" y="22.0" text-anchor="start">one sequence (M = 1): each stage works 1 slot in 4: 3 tokens in 12 slots, GPUs busy 25%</text><text class="t-tick" x="84.0" y="46.0" text-anchor="end">GPU 0</text><rect class="box-1" x="92.0" y="30.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="112.0" y="46.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="134.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="176.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="218.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="260.0" y="30.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="280.0" y="46.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="302.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="344.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="386.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="428.0" y="30.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="448.0" y="46.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="470.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="512.0" y="30.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="554.0" y="30.0" width="40.0" height="22.0" rx="3"/><text class="t-tick" x="84.0" y="72.0" text-anchor="end">GPU 1</text><rect class="cell-masked" x="92.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="134.0" y="56.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="154.0" y="72.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="176.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="218.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="260.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="302.0" y="56.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="322.0" y="72.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="344.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="386.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="428.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="470.0" y="56.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="490.0" y="72.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="512.0" y="56.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="554.0" y="56.0" width="40.0" height="22.0" rx="3"/><text class="t-tick" x="84.0" y="98.0" text-anchor="end">GPU 2</text><rect class="cell-masked" x="92.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="134.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="176.0" y="82.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="196.0" y="98.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="218.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="260.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="302.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="344.0" y="82.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="364.0" y="98.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="386.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="428.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="470.0" y="82.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="512.0" y="82.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="532.0" y="98.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="554.0" y="82.0" width="40.0" height="22.0" rx="3"/><text class="t-tick" x="84.0" y="124.0" text-anchor="end">GPU 3</text><rect class="cell-masked" x="92.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="134.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="176.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="218.0" y="108.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="238.0" y="124.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="260.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="302.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="344.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="386.0" y="108.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="406.0" y="124.0" text-anchor="middle">mb0</text><rect class="cell-masked" x="428.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="470.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="512.0" y="108.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="554.0" y="108.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="574.0" y="124.0" text-anchor="middle">mb0</text><text class="t-note" x="20.0" y="172.0" text-anchor="start">four micro-batches in flight (M = 4): 12 tokens in 15 slots, GPUs busy 80%</text><text class="t-tick" x="84.0" y="196.0" text-anchor="end">GPU 0</text><rect class="box-1" x="92.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="112.0" y="196.0" text-anchor="middle">mb0</text><rect class="box-2" x="134.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="154.0" y="196.0" text-anchor="middle">mb1</text><rect class="box-3" x="176.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="196.0" y="196.0" text-anchor="middle">mb2</text><rect class="box-1" x="218.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="238.0" y="196.0" text-anchor="middle">mb3</text><rect class="box-1" x="260.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="280.0" y="196.0" text-anchor="middle">mb0</text><rect class="box-2" x="302.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="322.0" y="196.0" text-anchor="middle">mb1</text><rect class="box-3" x="344.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="364.0" y="196.0" text-anchor="middle">mb2</text><rect class="box-1" x="386.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="406.0" y="196.0" text-anchor="middle">mb3</text><rect class="box-1" x="428.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="448.0" y="196.0" text-anchor="middle">mb0</text><rect class="box-2" x="470.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="490.0" y="196.0" text-anchor="middle">mb1</text><rect class="box-3" x="512.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="532.0" y="196.0" text-anchor="middle">mb2</text><rect class="box-1" x="554.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="574.0" y="196.0" text-anchor="middle">mb3</text><rect class="cell-masked" x="596.0" y="180.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="638.0" y="180.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="680.0" y="180.0" width="40.0" height="22.0" rx="3"/><text class="t-tick" x="84.0" y="222.0" text-anchor="end">GPU 1</text><rect class="cell-masked" x="92.0" y="206.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="134.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="154.0" y="222.0" text-anchor="middle">mb0</text><rect class="box-2" x="176.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="196.0" y="222.0" text-anchor="middle">mb1</text><rect class="box-3" x="218.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="238.0" y="222.0" text-anchor="middle">mb2</text><rect class="box-1" x="260.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="280.0" y="222.0" text-anchor="middle">mb3</text><rect class="box-1" x="302.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="322.0" y="222.0" text-anchor="middle">mb0</text><rect class="box-2" x="344.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="364.0" y="222.0" text-anchor="middle">mb1</text><rect class="box-3" x="386.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="406.0" y="222.0" text-anchor="middle">mb2</text><rect class="box-1" x="428.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="448.0" y="222.0" text-anchor="middle">mb3</text><rect class="box-1" x="470.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="490.0" y="222.0" text-anchor="middle">mb0</text><rect class="box-2" x="512.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="532.0" y="222.0" text-anchor="middle">mb1</text><rect class="box-3" x="554.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="574.0" y="222.0" text-anchor="middle">mb2</text><rect class="box-1" x="596.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="616.0" y="222.0" text-anchor="middle">mb3</text><rect class="cell-masked" x="638.0" y="206.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="680.0" y="206.0" width="40.0" height="22.0" rx="3"/><text class="t-tick" x="84.0" y="248.0" text-anchor="end">GPU 2</text><rect class="cell-masked" x="92.0" y="232.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="134.0" y="232.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="176.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="196.0" y="248.0" text-anchor="middle">mb0</text><rect class="box-2" x="218.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="238.0" y="248.0" text-anchor="middle">mb1</text><rect class="box-3" x="260.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="280.0" y="248.0" text-anchor="middle">mb2</text><rect class="box-1" x="302.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="322.0" y="248.0" text-anchor="middle">mb3</text><rect class="box-1" x="344.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="364.0" y="248.0" text-anchor="middle">mb0</text><rect class="box-2" x="386.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="406.0" y="248.0" text-anchor="middle">mb1</text><rect class="box-3" x="428.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="448.0" y="248.0" text-anchor="middle">mb2</text><rect class="box-1" x="470.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="490.0" y="248.0" text-anchor="middle">mb3</text><rect class="box-1" x="512.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="532.0" y="248.0" text-anchor="middle">mb0</text><rect class="box-2" x="554.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="574.0" y="248.0" text-anchor="middle">mb1</text><rect class="box-3" x="596.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="616.0" y="248.0" text-anchor="middle">mb2</text><rect class="box-1" x="638.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="658.0" y="248.0" text-anchor="middle">mb3</text><rect class="cell-masked" x="680.0" y="232.0" width="40.0" height="22.0" rx="3"/><text class="t-tick" x="84.0" y="274.0" text-anchor="end">GPU 3</text><rect class="cell-masked" x="92.0" y="258.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="134.0" y="258.0" width="40.0" height="22.0" rx="3"/><rect class="cell-masked" x="176.0" y="258.0" width="40.0" height="22.0" rx="3"/><rect class="box-1" x="218.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="238.0" y="274.0" text-anchor="middle">mb0</text><rect class="box-2" x="260.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="280.0" y="274.0" text-anchor="middle">mb1</text><rect class="box-3" x="302.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="322.0" y="274.0" text-anchor="middle">mb2</text><rect class="box-1" x="344.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="364.0" y="274.0" text-anchor="middle">mb3</text><rect class="box-1" x="386.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="406.0" y="274.0" text-anchor="middle">mb0</text><rect class="box-2" x="428.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="448.0" y="274.0" text-anchor="middle">mb1</text><rect class="box-3" x="470.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="490.0" y="274.0" text-anchor="middle">mb2</text><rect class="box-1" x="512.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="532.0" y="274.0" text-anchor="middle">mb3</text><rect class="box-1" x="554.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="574.0" y="274.0" text-anchor="middle">mb0</text><rect class="box-2" x="596.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="616.0" y="274.0" text-anchor="middle">mb1</text><rect class="box-3" x="638.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="658.0" y="274.0" text-anchor="middle">mb2</text><rect class="box-1" x="680.0" y="258.0" width="40.0" height="22.0" rx="3"/><text class="t-cell" x="700.0" y="274.0" text-anchor="middle">mb3</text><text class="t-tick" x="380.0" y="314.0" text-anchor="middle">A token must pass all 4 stages before the next token of the same sequence can start.</text></svg><figcaption>Pipeline schedule from layout_model.py: 4 GPUs, each stage taking one time slot. One sequence keeps each GPU busy a quarter of the time; four micro-batches keep them 80% busy.</figcaption></figure>

The figure shows the two faces of pipelining:

- **Throughput improves** with micro-batches: 12 tokens in 15 slots instead of 3 tokens in 12.
- **Latency does not.** Each token still passes through all 4 stages one after another, each stage running only its quarter of the layers. A pipeline never makes one sequence's token faster than reading all its layers on one GPU would, and it adds a network hop between stages.

Meta's choice for Llama 3 405B, from the same section as the box in Section 1, follows from this:

> [!PAPER] Llama Team, The Llama 3 Herd of Models · Section 6.1 · page 52
> [![Llama 3 report: Within each machine the high NVLink bandwidth enables the use of tensor parallelism. Across nodes, however, connectivity has lower bandwidth and higher latency, so we use pipeline parallelism instead. During training with pipeline parallelism, bubbles are a major efficiency concern. However, they are not an issue during inference, since inference does not involve a backward pass that requires a pipeline flush. Therefore, we use micro-batching to improve inference throughput with pipeline parallelism. We evaluate the effect of using two micro-batches in inference workloads of 4,096 input tokens and 256 output tokens. The additional synchronization points due to micro-batching also increase latency but, overall, micro-batching still leads to a better throughput-latency trade-off.](https://ishwarj.com/img/multigpu/paper-llama3-pp2.pngaper-llama3-pp2.png)](/img/multigpu/paper-llama3-pp2.png)
>
> **Context:** how Meta served Llama 3 405B in BF16 on two 8-GPU machines.
>
> **What it says:** tensor parallelism inside each machine, pipeline parallelism between the two machines, with two micro-batches. Micro-batching raised throughput and also raised latency, and was still worth it.
>
> **Why it matters:** this is the standard recipe for a model too big for one node: TP where the wires are fast, PP where they are slow.
>
> [Read Section 6.1](https://arxiv.org/pdf/2407.21783#page=51)

### Worked example: 405B on two nodes

`layout_model.py` compares two ways to use 16 H100s in two nodes for Llama 3.1 405B:

- **TP=16**: every layer split 16 ways, so 252 all-reduces per step, all crossing InfiniBand ($$\alpha$$ assumed 10 $$\mu$$s, $$\beta$$ = 50 GB/s per GPU).
- **TP=8 x PP=2**: each node holds 63 layers, split 8 ways over NVLink; one hop over InfiniBand per step.

<figure class="fig"><svg viewBox="0 0 760 298" role="img" aria-label="Modelled 405B on two nodes. At batch 32, TP=16 over InfiniBand takes 31.4 ms per token and makes 1,020 tokens per second; TP=8 with PP=2 takes 36.4 ms per token and makes 1,760 tokens per second with two micro-batches in flight."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="22.0" text-anchor="start">Llama 3.1 405B (BF16) on two 8-GPU nodes, modelled</text><text class="t-label" x="350.0" y="44.0" text-anchor="middle">ms per token (latency)</text><text class="t-label" x="620.0" y="44.0" text-anchor="middle">tokens/s (throughput)</text><text class="t-tick" x="270.0" y="72.0" text-anchor="end">batch 1: TP=16 over InfiniBand</text><g class="mark"><title>batch 1: TP=16 over InfiniBand: 20.5 ms</title><rect class="s3" x="280.0" y="58.0" width="95.9" height="18.0" rx="3"/></g><text class="t-val" x="381.9" y="72.0" text-anchor="start">20.5</text><g class="mark"><title>batch 1: TP=16 over InfiniBand: 49 tokens/s</title><rect class="s3" x="520.0" y="58.0" width="4.7" height="18.0" rx="3"/></g><text class="t-val" x="530.7" y="72.0" text-anchor="start">49</text><text class="t-tick" x="270.0" y="106.0" text-anchor="end">batch 1: TP=8 x PP=2, 1 micro-batch</text><g class="mark"><title>batch 1: TP=8 x PP=2, 1 micro-batch: 32.9 ms</title><rect class="s1" x="280.0" y="92.0" width="153.8" height="18.0" rx="3"/></g><text class="t-val" x="439.8" y="106.0" text-anchor="start">32.9</text><g class="mark"><title>batch 1: TP=8 x PP=2, 1 micro-batch: 30 tokens/s</title><rect class="s1" x="520.0" y="92.0" width="2.9" height="18.0" rx="3"/></g><text class="t-val" x="528.9" y="106.0" text-anchor="start">30</text><text class="t-tick" x="270.0" y="140.0" text-anchor="end">batch 1: TP=8 x PP=2, 2 micro-batches</text><g class="mark"><title>batch 1: TP=8 x PP=2, 2 micro-batches: 32.9 ms</title><rect class="s2" x="280.0" y="126.0" width="153.8" height="18.0" rx="3"/></g><text class="t-val" x="439.8" y="140.0" text-anchor="start">32.9</text><g class="mark"><title>batch 1: TP=8 x PP=2, 2 micro-batches: 61 tokens/s</title><rect class="s2" x="520.0" y="126.0" width="5.9" height="18.0" rx="3"/></g><text class="t-val" x="531.9" y="140.0" text-anchor="start">61</text><text class="t-tick" x="270.0" y="186.0" text-anchor="end">batch 32: TP=16 over InfiniBand</text><g class="mark"><title>batch 32: TP=16 over InfiniBand: 31.4 ms</title><rect class="s3" x="280.0" y="172.0" width="146.5" height="18.0" rx="3"/></g><text class="t-val" x="432.5" y="186.0" text-anchor="start">31.4</text><g class="mark"><title>batch 32: TP=16 over InfiniBand: 1,021 tokens/s</title><rect class="s3" x="520.0" y="172.0" width="98.6" height="18.0" rx="3"/></g><text class="t-val" x="624.6" y="186.0" text-anchor="start">1,021</text><text class="t-tick" x="270.0" y="220.0" text-anchor="end">batch 32: TP=8 x PP=2, 1 micro-batch</text><g class="mark"><title>batch 32: TP=8 x PP=2, 1 micro-batch: 36.4 ms</title><rect class="s1" x="280.0" y="206.0" width="170.0" height="18.0" rx="3"/></g><text class="t-val" x="456.0" y="220.0" text-anchor="start">36.4</text><g class="mark"><title>batch 32: TP=8 x PP=2, 1 micro-batch: 879 tokens/s</title><rect class="s1" x="520.0" y="206.0" width="84.9" height="18.0" rx="3"/></g><text class="t-val" x="610.9" y="220.0" text-anchor="start">879</text><text class="t-tick" x="270.0" y="254.0" text-anchor="end">batch 32: TP=8 x PP=2, 2 micro-batches</text><g class="mark"><title>batch 32: TP=8 x PP=2, 2 micro-batches: 36.4 ms</title><rect class="s2" x="280.0" y="240.0" width="170.0" height="18.0" rx="3"/></g><text class="t-val" x="456.0" y="254.0" text-anchor="start">36.4</text><g class="mark"><title>batch 32: TP=8 x PP=2, 2 micro-batches: 1,760 tokens/s</title><rect class="s2" x="520.0" y="240.0" width="170.0" height="18.0" rx="3"/></g><text class="t-val" x="696.0" y="254.0" text-anchor="start">1,760</text><text class="t-tick" x="380.0" y="286.0" text-anchor="middle">Pipeline: no faster per token, but more tokens per second. TP=16: lower latency, many network messages.</text></svg><figcaption>Modelled Llama 3.1 405B decode on two 8-GPU nodes. Pipelining does not lower latency, but with two micro-batches in flight it gives the highest throughput.</figcaption></figure>

| Batch 32 | ms per token | Tokens per second |
|---|---|---|
| TP=16 across both nodes | 31.4 (15.0 of it all-reduce over InfiniBand) | 1,020 |
| TP=8 x PP=2, one micro-batch | 36.4 | 879 |
| TP=8 x PP=2, two micro-batches | 36.4 | 1,760 |

Per token, the pipeline is *slower*: each token reads 406 GB on 8 GPUs (15.1 ms) in the first node, then again in the second, plus the hop. TP=16 reads everything in parallel on 16 GPUs but spends almost half its time in all-reduces over the network. Fill the pipeline with two micro-batches and it delivers **1.7x the throughput** of TP=16 while sending a few kilobytes per token over InfiniBand instead of hundreds of messages. If latency matters most and the network is excellent, TP across nodes can win; for throughput per GPU, the pipeline does. Both conclusions depend on the assumed $$\alpha$$, which is why the code keeps it as a named input.

## 6. Expert parallelism for mixture-of-experts models

### Why MoE models need their own kind of parallelism

A mixture-of-experts layer replaces one big MLP with many small ones. DeepSeek-V3 has, in each of its 58 MoE layers, 256 routed experts (each an MLP with a hidden width of 2,048) plus one shared expert that every token uses. A small router scores the experts for each token and sends the token to its top 8.

You could split every expert with tensor parallelism. But each expert is small, and an all-reduce per expert per layer would be pure overhead. The natural move is the opposite: **keep each expert whole and give different experts to different GPUs.**

> [!DEFINITION] Expert parallelism (EP)
> The experts of each MoE layer are distributed across GPUs, each GPU holding a few whole experts. Tokens are sent to the GPUs that hold their chosen experts and the results are sent back. "EP=32" means the experts are spread over 32 GPUs.

> [!DEFINITION] Data-parallel attention
> The attention part of the model is copied onto every GPU (or every small TP group), and each copy handles different sequences. It is often combined with EP: attention is cheap to copy, especially with MLA's small KV cache, while the experts are too large to copy. SGLang calls this "DP attention"; DeepSeek's report writes it as DP8 or DP80.

Each MoE layer then runs in four stages:

<figure class="fig"><svg viewBox="0 0 760 408" role="img" aria-label="Expert parallelism on four GPUs. Each GPU runs attention for its tokens, the router picks experts, a dispatch all-to-all sends each token to the GPUs holding its experts, the experts compute, and a combine all-to-all sends the outputs back."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="22.0" text-anchor="start">One MoE layer with expert parallelism (EP = 4 GPUs, 2 experts each, top-2)</text><rect class="box" x="30.0" y="40.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="110.0" y="62.0" text-anchor="middle">GPU 0: attention</text><text class="t-tick" x="110.0" y="84.0" text-anchor="middle">router picks 2 experts/token</text><rect class="box-1" x="30.0" y="190.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="110.0" y="214.0" text-anchor="middle">experts 0, 1</text><text class="t-tick" x="110.0" y="236.0" text-anchor="middle">FFN on received tokens</text><rect class="box" x="30.0" y="330.0" width="160.0" height="44.0" rx="8"/><text class="t-tick" x="110.0" y="357.0" text-anchor="middle">weighted sum of 2 outputs</text><rect class="box" x="212.0" y="40.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="292.0" y="62.0" text-anchor="middle">GPU 1: attention</text><text class="t-tick" x="292.0" y="84.0" text-anchor="middle">router picks 2 experts/token</text><rect class="box-1" x="212.0" y="190.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="292.0" y="214.0" text-anchor="middle">experts 2, 3</text><text class="t-tick" x="292.0" y="236.0" text-anchor="middle">FFN on received tokens</text><rect class="box" x="212.0" y="330.0" width="160.0" height="44.0" rx="8"/><text class="t-tick" x="292.0" y="357.0" text-anchor="middle">weighted sum of 2 outputs</text><rect class="box" x="394.0" y="40.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="474.0" y="62.0" text-anchor="middle">GPU 2: attention</text><text class="t-tick" x="474.0" y="84.0" text-anchor="middle">router picks 2 experts/token</text><rect class="box-1" x="394.0" y="190.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="474.0" y="214.0" text-anchor="middle">experts 4, 5</text><text class="t-tick" x="474.0" y="236.0" text-anchor="middle">FFN on received tokens</text><rect class="box" x="394.0" y="330.0" width="160.0" height="44.0" rx="8"/><text class="t-tick" x="474.0" y="357.0" text-anchor="middle">weighted sum of 2 outputs</text><rect class="box" x="576.0" y="40.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="656.0" y="62.0" text-anchor="middle">GPU 3: attention</text><text class="t-tick" x="656.0" y="84.0" text-anchor="middle">router picks 2 experts/token</text><rect class="box-1" x="576.0" y="190.0" width="160.0" height="60.0" rx="8"/><text class="t-note" x="656.0" y="214.0" text-anchor="middle">experts 6, 7</text><text class="t-tick" x="656.0" y="236.0" text-anchor="middle">FFN on received tokens</text><rect class="box" x="576.0" y="330.0" width="160.0" height="44.0" rx="8"/><text class="t-tick" x="656.0" y="357.0" text-anchor="middle">weighted sum of 2 outputs</text><line class="edge" x1="110.0" y1="100.0" x2="462.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="462.0" y1="250.0" x2="110.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="110.0" y1="100.0" x2="644.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="644.0" y1="250.0" x2="110.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="292.0" y1="100.0" x2="106.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="106.0" y1="250.0" x2="292.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="292.0" y1="100.0" x2="652.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="652.0" y1="250.0" x2="292.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="474.0" y1="100.0" x2="296.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="296.0" y1="250.0" x2="474.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="474.0" y1="100.0" x2="478.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="478.0" y1="250.0" x2="474.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="656.0" y1="100.0" x2="122.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="122.0" y1="250.0" x2="656.0" y2="326.0" marker-end="url(#ah7)"/><line class="edge" x1="656.0" y1="100.0" x2="304.0" y2="186.0" marker-end="url(#ah7)"/><line class="drop" x1="304.0" y1="250.0" x2="656.0" y2="326.0" marker-end="url(#ah7)"/><rect class="box-3" x="300.0" y="128.0" width="160.0" height="30.0" rx="6"/><text class="t-note" x="380.0" y="148.0" text-anchor="middle">dispatch: all-to-all</text><rect class="box-3" x="300.0" y="270.0" width="160.0" height="30.0" rx="6"/><text class="t-note" x="380.0" y="290.0" text-anchor="middle">combine: all-to-all</text><text class="t-tick" x="380.0" y="396.0" text-anchor="middle">Two all-to-alls per MoE layer. Where tokens go depends on the router, so traffic is uneven.</text></svg><figcaption>Expert parallelism on four GPUs. Dispatch sends each token's activations to the GPUs holding its chosen experts; combine sends the expert outputs back to be added up.</figcaption></figure>

1. Each GPU runs attention for its own tokens, and the router picks each token's experts.
2. **Dispatch:** an all-to-all sends each token's activations to the GPUs that hold its experts.
3. Each GPU runs its experts on whatever tokens it received.
4. **Combine:** a second all-to-all sends the outputs back, where they are added with the router's weights.

### How many bytes

Each token is sent once per chosen expert, and the answers come back the same way.

$$
\text{bytes per token per MoE layer} = k\,d\,(s_{\text{dispatch}} + s_{\text{combine}})
$$

where:

- $$k$$ is the number of experts chosen per token (8 for DeepSeek-V3);
- $$d$$ is the model width (7,168);
- $$s_{\text{dispatch}}$$ and $$s_{\text{combine}}$$ are the bytes per number on the way out and back: DeepSeek sends 1 byte (FP8) out and 2 bytes (BF16) back.

**Worked example.** $$8\times7{,}168\times(1+2) = 172{,}032$$ bytes per token per layer; over 58 MoE layers, **10.0 MB per token**. A decode batch of 128 tokens per GPU dispatches $$128\times8\times7{,}168 = 7.34$$ MB per layer.

DeepSeek's open-source all-to-all library, [DeepEP](https://github.com/deepseek-ai/DeepEP), published timings for exactly this setting (H800s with 400 Gb/s InfiniBand, 128 tokens per batch, hidden 7,168, top-8, FP8 dispatch, BF16 combine; [README of release v1.2.1](https://github.com/deepseek-ai/DeepEP/tree/v1.2.1)). Our byte count reproduces them: 7.34 MB at their measured 98 GB/s is 75 $$\mu$$s, against the **77 $$\mu$$s** they report for EP8 dispatch; 14.68 MB of combine at 127 GB/s is 116 $$\mu$$s against **114 $$\mu$$s** reported. Across all 58 MoE layers that is **11.1 ms per decode step** at EP8 and 32.1 ms at EP256 (194 and 360 $$\mu$$s per layer), unless it is hidden behind computation. That is why DeepSeek runs two micro-batches and overlaps one's communication with the other's compute.

[![Terminal output of collectives.py section 3 and 4: the all-reduce cost table for Llama 3.1 70B, ring against two-step, and the DeepSeek-V3 all-to-all byte arithmetic: 57,344 B dispatch and 114,688 B combine per token per layer, 10.0 MB per token, 75 and 116 microseconds for a 128-token batch against DeepEP's reported 77 and 114, 11.1 ms per decode step at EP8 and 32.1 ms at EP256](https://ishwarj.com/img/multigpu/collectives_cost-run.pngctives_cost-run.png)](/img/multigpu/collectives_cost-run.png)

### The real problem: load imbalance

All-to-all has a second cost that the bytes do not show. A layer is finished only when **the busiest GPU** is finished. If the router sends twice the average number of tokens to the experts on one GPU, that GPU takes twice as long and every other GPU waits.

How uneven is real routing? Rather than guess, `moe_routing.py` records it. It runs the real **OLMoE-1B-7B** model (64 experts per layer, 8 chosen per token, 16 layers, 6.9 billion parameters) on the M5 Pro's GPU over 12,288 tokens of WikiText and 12,288 tokens of Python code, and saves which 8 experts the router chose for every token in every layer. That is about 3 million real routing decisions. The routing is the measurement; nothing is timed.

The recording is a forward hook on each layer's router, which returns the chosen expert ids as its third output:

```python
for i, layer in enumerate(model.model.layers):
    hooks.append(layer.mlp.gate.register_forward_hook(
        lambda m, inp, out, i=i: buf.setdefault(i, []).append(out[2].cpu())))
with torch.no_grad():
    for s in seqs:
        model(torch.tensor(s, device=dev)[None])
```

Then plain Python asks what those choices would do to expert parallelism. Experts $$0$$ to $$63$$ are placed in order, $$64/G$$ per GPU, and for random batches of tokens we compute the load on the busiest GPU divided by the average load:

```python
counts = np.bincount(picks[l, idx].ravel(), minlength=E)   # tokens sent to each expert in this batch
load = np.zeros(n_gpus)
np.add.at(load, expert_to_gpu, counts)                       # tokens each GPU must process
ratios.append(load.max() / load.mean())                      # 1.00 = perfectly even
```

The same calculation on random routing (each token picks 8 experts uniformly) separates bad luck from real preference.

[![Terminal output of moe_routing.py: for WikiText and Python code, how much more than its fair share the hottest expert gets, the busiest-GPU-over-average ratio for EP = 8, 16 and 64 at batches of 64, 256 and 4,096 tokens against random routing, the effect of balanced placement and redundant experts, and how many hot experts the two kinds of text share](https://ishwarj.com/img/multigpu/moe_routing-run.pngmoe_routing-run.png)](/img/multigpu/moe_routing-run.png)

First, the experts themselves. In a typical layer the busiest expert receives **3.1 times** its fair share of WikiText tokens (2.2x to 4.2x across layers), and on code **6.9 times**; the idlest receive almost nothing.

<figure class="fig"><svg viewBox="0 0 760 392" role="img" aria-label="Measured share of routing slots per expert in OLMoE-1B-7B, sorted from busiest to idlest, for a typical layer and the most uneven layer. A few experts get several times their fair share; some get almost nothing."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-note" x="20.0" y="26.0" text-anchor="start">Layer 8 (a typical layer), WikiText: share of the 8 expert slots per token</text><g class="mark"><title>expert rank 1: 4.86% of visits</title><rect class="s1" x="60.0" y="50.0" width="8.0" height="100.0" rx="1"/></g><g class="mark"><title>expert rank 2: 4.01% of visits</title><rect class="s1" x="70.0" y="67.5" width="8.0" height="82.5" rx="1"/></g><g class="mark"><title>expert rank 3: 3.79% of visits</title><rect class="s1" x="80.0" y="72.0" width="8.0" height="78.0" rx="1"/></g><g class="mark"><title>expert rank 4: 3.13% of visits</title><rect class="s1" x="90.0" y="85.6" width="8.0" height="64.4" rx="1"/></g><g class="mark"><title>expert rank 5: 2.74% of visits</title><rect class="s1" x="100.0" y="93.7" width="8.0" height="56.3" rx="1"/></g><g class="mark"><title>expert rank 6: 2.50% of visits</title><rect class="s1" x="110.0" y="98.5" width="8.0" height="51.5" rx="1"/></g><g class="mark"><title>expert rank 7: 2.43% of visits</title><rect class="s1" x="120.0" y="100.0" width="8.0" height="50.0" rx="1"/></g><g class="mark"><title>expert rank 8: 2.41% of visits</title><rect class="s1" x="130.0" y="100.4" width="8.0" height="49.6" rx="1"/></g><g class="mark"><title>expert rank 9: 2.39% of visits</title><rect class="s1" x="140.0" y="100.8" width="8.0" height="49.2" rx="1"/></g><g class="mark"><title>expert rank 10: 2.34% of visits</title><rect class="s1" x="150.0" y="101.8" width="8.0" height="48.2" rx="1"/></g><g class="mark"><title>expert rank 11: 2.19% of visits</title><rect class="s1" x="160.0" y="104.9" width="8.0" height="45.1" rx="1"/></g><g class="mark"><title>expert rank 12: 2.18% of visits</title><rect class="s1" x="170.0" y="105.2" width="8.0" height="44.8" rx="1"/></g><g class="mark"><title>expert rank 13: 2.16% of visits</title><rect class="s1" x="180.0" y="105.5" width="8.0" height="44.5" rx="1"/></g><g class="mark"><title>expert rank 14: 2.10% of visits</title><rect class="s1" x="190.0" y="106.9" width="8.0" height="43.1" rx="1"/></g><g class="mark"><title>expert rank 15: 2.07% of visits</title><rect class="s1" x="200.0" y="107.5" width="8.0" height="42.5" rx="1"/></g><g class="mark"><title>expert rank 16: 2.01% of visits</title><rect class="s1" x="210.0" y="108.6" width="8.0" height="41.4" rx="1"/></g><g class="mark"><title>expert rank 17: 2.01% of visits</title><rect class="s1" x="220.0" y="108.7" width="8.0" height="41.3" rx="1"/></g><g class="mark"><title>expert rank 18: 1.99% of visits</title><rect class="s1" x="230.0" y="109.0" width="8.0" height="41.0" rx="1"/></g><g class="mark"><title>expert rank 19: 1.95% of visits</title><rect class="s1" x="240.0" y="110.0" width="8.0" height="40.0" rx="1"/></g><g class="mark"><title>expert rank 20: 1.86% of visits</title><rect class="s1" x="250.0" y="111.7" width="8.0" height="38.3" rx="1"/></g><g class="mark"><title>expert rank 21: 1.77% of visits</title><rect class="s1" x="260.0" y="113.5" width="8.0" height="36.5" rx="1"/></g><g class="mark"><title>expert rank 22: 1.75% of visits</title><rect class="s1" x="270.0" y="114.0" width="8.0" height="36.0" rx="1"/></g><g class="mark"><title>expert rank 23: 1.72% of visits</title><rect class="s1" x="280.0" y="114.7" width="8.0" height="35.3" rx="1"/></g><g class="mark"><title>expert rank 24: 1.70% of visits</title><rect class="s1" x="290.0" y="114.9" width="8.0" height="35.1" rx="1"/></g><g class="mark"><title>expert rank 25: 1.70% of visits</title><rect class="s1" x="300.0" y="115.0" width="8.0" height="35.0" rx="1"/></g><g class="mark"><title>expert rank 26: 1.64% of visits</title><rect class="s1" x="310.0" y="116.3" width="8.0" height="33.7" rx="1"/></g><g class="mark"><title>expert rank 27: 1.63% of visits</title><rect class="s1" x="320.0" y="116.4" width="8.0" height="33.6" rx="1"/></g><g class="mark"><title>expert rank 28: 1.63% of visits</title><rect class="s1" x="330.0" y="116.4" width="8.0" height="33.6" rx="1"/></g><g class="mark"><title>expert rank 29: 1.61% of visits</title><rect class="s1" x="340.0" y="116.8" width="8.0" height="33.2" rx="1"/></g><g class="mark"><title>expert rank 30: 1.57% of visits</title><rect class="s1" x="350.0" y="117.8" width="8.0" height="32.2" rx="1"/></g><g class="mark"><title>expert rank 31: 1.56% of visits</title><rect class="s1" x="360.0" y="117.9" width="8.0" height="32.1" rx="1"/></g><g class="mark"><title>expert rank 32: 1.47% of visits</title><rect class="s1" x="370.0" y="119.8" width="8.0" height="30.2" rx="1"/></g><g class="mark"><title>expert rank 33: 1.45% of visits</title><rect class="s1" x="380.0" y="120.2" width="8.0" height="29.8" rx="1"/></g><g class="mark"><title>expert rank 34: 1.42% of visits</title><rect class="s1" x="390.0" y="120.8" width="8.0" height="29.2" rx="1"/></g><g class="mark"><title>expert rank 35: 1.41% of visits</title><rect class="s1" x="400.0" y="120.9" width="8.0" height="29.1" rx="1"/></g><g class="mark"><title>expert rank 36: 1.41% of visits</title><rect class="s1" x="410.0" y="121.0" width="8.0" height="29.0" rx="1"/></g><g class="mark"><title>expert rank 37: 1.40% of visits</title><rect class="s1" x="420.0" y="121.1" width="8.0" height="28.9" rx="1"/></g><g class="mark"><title>expert rank 38: 1.40% of visits</title><rect class="s1" x="430.0" y="121.2" width="8.0" height="28.8" rx="1"/></g><g class="mark"><title>expert rank 39: 1.37% of visits</title><rect class="s1" x="440.0" y="121.9" width="8.0" height="28.1" rx="1"/></g><g class="mark"><title>expert rank 40: 1.36% of visits</title><rect class="s1" x="450.0" y="121.9" width="8.0" height="28.1" rx="1"/></g><g class="mark"><title>expert rank 41: 1.34% of visits</title><rect class="s1" x="460.0" y="122.4" width="8.0" height="27.6" rx="1"/></g><g class="mark"><title>expert rank 42: 1.26% of visits</title><rect class="s1" x="470.0" y="124.0" width="8.0" height="26.0" rx="1"/></g><g class="mark"><title>expert rank 43: 1.25% of visits</title><rect class="s1" x="480.0" y="124.3" width="8.0" height="25.7" rx="1"/></g><g class="mark"><title>expert rank 44: 1.14% of visits</title><rect class="s1" x="490.0" y="126.5" width="8.0" height="23.5" rx="1"/></g><g class="mark"><title>expert rank 45: 1.06% of visits</title><rect class="s1" x="500.0" y="128.2" width="8.0" height="21.8" rx="1"/></g><g class="mark"><title>expert rank 46: 1.03% of visits</title><rect class="s1" x="510.0" y="128.9" width="8.0" height="21.1" rx="1"/></g><g class="mark"><title>expert rank 47: 1.00% of visits</title><rect class="s1" x="520.0" y="129.5" width="8.0" height="20.5" rx="1"/></g><g class="mark"><title>expert rank 48: 0.97% of visits</title><rect class="s1" x="530.0" y="130.1" width="8.0" height="19.9" rx="1"/></g><g class="mark"><title>expert rank 49: 0.94% of visits</title><rect class="s1" x="540.0" y="130.6" width="8.0" height="19.4" rx="1"/></g><g class="mark"><title>expert rank 50: 0.89% of visits</title><rect class="s1" x="550.0" y="131.8" width="8.0" height="18.2" rx="1"/></g><g class="mark"><title>expert rank 51: 0.82% of visits</title><rect class="s1" x="560.0" y="133.1" width="8.0" height="16.9" rx="1"/></g><g class="mark"><title>expert rank 52: 0.80% of visits</title><rect class="s1" x="570.0" y="133.5" width="8.0" height="16.5" rx="1"/></g><g class="mark"><title>expert rank 53: 0.72% of visits</title><rect class="s1" x="580.0" y="135.1" width="8.0" height="14.9" rx="1"/></g><g class="mark"><title>expert rank 54: 0.72% of visits</title><rect class="s1" x="590.0" y="135.1" width="8.0" height="14.9" rx="1"/></g><g class="mark"><title>expert rank 55: 0.66% of visits</title><rect class="s1" x="600.0" y="136.4" width="8.0" height="13.6" rx="1"/></g><g class="mark"><title>expert rank 56: 0.54% of visits</title><rect class="s1" x="610.0" y="138.9" width="8.0" height="11.1" rx="1"/></g><g class="mark"><title>expert rank 57: 0.53% of visits</title><rect class="s1" x="620.0" y="139.1" width="8.0" height="10.9" rx="1"/></g><g class="mark"><title>expert rank 58: 0.48% of visits</title><rect class="s1" x="630.0" y="140.1" width="8.0" height="9.9" rx="1"/></g><g class="mark"><title>expert rank 59: 0.46% of visits</title><rect class="s1" x="640.0" y="140.6" width="8.0" height="9.4" rx="1"/></g><g class="mark"><title>expert rank 60: 0.44% of visits</title><rect class="s1" x="650.0" y="140.9" width="8.0" height="9.1" rx="1"/></g><g class="mark"><title>expert rank 61: 0.32% of visits</title><rect class="s1" x="660.0" y="143.4" width="8.0" height="6.6" rx="1"/></g><g class="mark"><title>expert rank 62: 0.30% of visits</title><rect class="s1" x="670.0" y="143.7" width="8.0" height="6.3" rx="1"/></g><g class="mark"><title>expert rank 63: 0.14% of visits</title><rect class="s1" x="680.0" y="147.2" width="8.0" height="2.8" rx="1"/></g><g class="mark"><title>expert rank 64: 0.06% of visits</title><rect class="s1" x="690.0" y="148.8" width="8.0" height="1.2" rx="1"/></g><line class="base-line" x1="56.0" y1="117.9" x2="704.0" y2="117.9"/><text class="t-tick" x="708.0" y="121.9" text-anchor="start">fair 1/64</text><text class="t-val" x="64.0" y="46.0" text-anchor="start">hottest: 3.1x fair</text><line class="axis" x1="56.0" y1="150.0" x2="704.0" y2="150.0"/><text class="t-note" x="20.0" y="196.0" text-anchor="start">Layer 4 (the most uneven layer), WikiText: share of the 8 expert slots per token</text><g class="mark"><title>expert rank 1: 6.58% of visits</title><rect class="s1" x="60.0" y="220.0" width="8.0" height="100.0" rx="1"/></g><g class="mark"><title>expert rank 2: 5.10% of visits</title><rect class="s1" x="70.0" y="242.6" width="8.0" height="77.4" rx="1"/></g><g class="mark"><title>expert rank 3: 3.06% of visits</title><rect class="s1" x="80.0" y="273.5" width="8.0" height="46.5" rx="1"/></g><g class="mark"><title>expert rank 4: 2.85% of visits</title><rect class="s1" x="90.0" y="276.7" width="8.0" height="43.3" rx="1"/></g><g class="mark"><title>expert rank 5: 2.72% of visits</title><rect class="s1" x="100.0" y="278.7" width="8.0" height="41.3" rx="1"/></g><g class="mark"><title>expert rank 6: 2.69% of visits</title><rect class="s1" x="110.0" y="279.1" width="8.0" height="40.9" rx="1"/></g><g class="mark"><title>expert rank 7: 2.54% of visits</title><rect class="s1" x="120.0" y="281.3" width="8.0" height="38.7" rx="1"/></g><g class="mark"><title>expert rank 8: 2.28% of visits</title><rect class="s1" x="130.0" y="285.3" width="8.0" height="34.7" rx="1"/></g><g class="mark"><title>expert rank 9: 2.23% of visits</title><rect class="s1" x="140.0" y="286.2" width="8.0" height="33.8" rx="1"/></g><g class="mark"><title>expert rank 10: 2.21% of visits</title><rect class="s1" x="150.0" y="286.5" width="8.0" height="33.5" rx="1"/></g><g class="mark"><title>expert rank 11: 2.17% of visits</title><rect class="s1" x="160.0" y="287.1" width="8.0" height="32.9" rx="1"/></g><g class="mark"><title>expert rank 12: 2.13% of visits</title><rect class="s1" x="170.0" y="287.7" width="8.0" height="32.3" rx="1"/></g><g class="mark"><title>expert rank 13: 2.07% of visits</title><rect class="s1" x="180.0" y="288.5" width="8.0" height="31.5" rx="1"/></g><g class="mark"><title>expert rank 14: 1.96% of visits</title><rect class="s1" x="190.0" y="290.3" width="8.0" height="29.7" rx="1"/></g><g class="mark"><title>expert rank 15: 1.90% of visits</title><rect class="s1" x="200.0" y="291.1" width="8.0" height="28.9" rx="1"/></g><g class="mark"><title>expert rank 16: 1.90% of visits</title><rect class="s1" x="210.0" y="291.1" width="8.0" height="28.9" rx="1"/></g><g class="mark"><title>expert rank 17: 1.88% of visits</title><rect class="s1" x="220.0" y="291.4" width="8.0" height="28.6" rx="1"/></g><g class="mark"><title>expert rank 18: 1.85% of visits</title><rect class="s1" x="230.0" y="291.9" width="8.0" height="28.1" rx="1"/></g><g class="mark"><title>expert rank 19: 1.83% of visits</title><rect class="s1" x="240.0" y="292.2" width="8.0" height="27.8" rx="1"/></g><g class="mark"><title>expert rank 20: 1.82% of visits</title><rect class="s1" x="250.0" y="292.4" width="8.0" height="27.6" rx="1"/></g><g class="mark"><title>expert rank 21: 1.79% of visits</title><rect class="s1" x="260.0" y="292.9" width="8.0" height="27.1" rx="1"/></g><g class="mark"><title>expert rank 22: 1.77% of visits</title><rect class="s1" x="270.0" y="293.1" width="8.0" height="26.9" rx="1"/></g><g class="mark"><title>expert rank 23: 1.73% of visits</title><rect class="s1" x="280.0" y="293.7" width="8.0" height="26.3" rx="1"/></g><g class="mark"><title>expert rank 24: 1.72% of visits</title><rect class="s1" x="290.0" y="293.8" width="8.0" height="26.2" rx="1"/></g><g class="mark"><title>expert rank 25: 1.65% of visits</title><rect class="s1" x="300.0" y="294.9" width="8.0" height="25.1" rx="1"/></g><g class="mark"><title>expert rank 26: 1.61% of visits</title><rect class="s1" x="310.0" y="295.5" width="8.0" height="24.5" rx="1"/></g><g class="mark"><title>expert rank 27: 1.61% of visits</title><rect class="s1" x="320.0" y="295.6" width="8.0" height="24.4" rx="1"/></g><g class="mark"><title>expert rank 28: 1.55% of visits</title><rect class="s1" x="330.0" y="296.4" width="8.0" height="23.6" rx="1"/></g><g class="mark"><title>expert rank 29: 1.55% of visits</title><rect class="s1" x="340.0" y="296.4" width="8.0" height="23.6" rx="1"/></g><g class="mark"><title>expert rank 30: 1.53% of visits</title><rect class="s1" x="350.0" y="296.8" width="8.0" height="23.2" rx="1"/></g><g class="mark"><title>expert rank 31: 1.53% of visits</title><rect class="s1" x="360.0" y="296.8" width="8.0" height="23.2" rx="1"/></g><g class="mark"><title>expert rank 32: 1.53% of visits</title><rect class="s1" x="370.0" y="296.8" width="8.0" height="23.2" rx="1"/></g><g class="mark"><title>expert rank 33: 1.52% of visits</title><rect class="s1" x="380.0" y="296.8" width="8.0" height="23.2" rx="1"/></g><g class="mark"><title>expert rank 34: 1.50% of visits</title><rect class="s1" x="390.0" y="297.2" width="8.0" height="22.8" rx="1"/></g><g class="mark"><title>expert rank 35: 1.47% of visits</title><rect class="s1" x="400.0" y="297.6" width="8.0" height="22.4" rx="1"/></g><g class="mark"><title>expert rank 36: 1.46% of visits</title><rect class="s1" x="410.0" y="297.9" width="8.0" height="22.1" rx="1"/></g><g class="mark"><title>expert rank 37: 1.35% of visits</title><rect class="s1" x="420.0" y="299.5" width="8.0" height="20.5" rx="1"/></g><g class="mark"><title>expert rank 38: 1.34% of visits</title><rect class="s1" x="430.0" y="299.6" width="8.0" height="20.4" rx="1"/></g><g class="mark"><title>expert rank 39: 1.30% of visits</title><rect class="s1" x="440.0" y="300.2" width="8.0" height="19.8" rx="1"/></g><g class="mark"><title>expert rank 40: 1.30% of visits</title><rect class="s1" x="450.0" y="300.3" width="8.0" height="19.7" rx="1"/></g><g class="mark"><title>expert rank 41: 1.25% of visits</title><rect class="s1" x="460.0" y="301.0" width="8.0" height="19.0" rx="1"/></g><g class="mark"><title>expert rank 42: 1.22% of visits</title><rect class="s1" x="470.0" y="301.4" width="8.0" height="18.6" rx="1"/></g><g class="mark"><title>expert rank 43: 1.22% of visits</title><rect class="s1" x="480.0" y="301.5" width="8.0" height="18.5" rx="1"/></g><g class="mark"><title>expert rank 44: 1.14% of visits</title><rect class="s1" x="490.0" y="302.6" width="8.0" height="17.4" rx="1"/></g><g class="mark"><title>expert rank 45: 1.14% of visits</title><rect class="s1" x="500.0" y="302.7" width="8.0" height="17.3" rx="1"/></g><g class="mark"><title>expert rank 46: 1.05% of visits</title><rect class="s1" x="510.0" y="304.0" width="8.0" height="16.0" rx="1"/></g><g class="mark"><title>expert rank 47: 1.04% of visits</title><rect class="s1" x="520.0" y="304.1" width="8.0" height="15.9" rx="1"/></g><g class="mark"><title>expert rank 48: 1.01% of visits</title><rect class="s1" x="530.0" y="304.6" width="8.0" height="15.4" rx="1"/></g><g class="mark"><title>expert rank 49: 0.92% of visits</title><rect class="s1" x="540.0" y="306.0" width="8.0" height="14.0" rx="1"/></g><g class="mark"><title>expert rank 50: 0.92% of visits</title><rect class="s1" x="550.0" y="306.1" width="8.0" height="13.9" rx="1"/></g><g class="mark"><title>expert rank 51: 0.90% of visits</title><rect class="s1" x="560.0" y="306.4" width="8.0" height="13.6" rx="1"/></g><g class="mark"><title>expert rank 52: 0.88% of visits</title><rect class="s1" x="570.0" y="306.7" width="8.0" height="13.3" rx="1"/></g><g class="mark"><title>expert rank 53: 0.78% of visits</title><rect class="s1" x="580.0" y="308.1" width="8.0" height="11.9" rx="1"/></g><g class="mark"><title>expert rank 54: 0.72% of visits</title><rect class="s1" x="590.0" y="309.1" width="8.0" height="10.9" rx="1"/></g><g class="mark"><title>expert rank 55: 0.71% of visits</title><rect class="s1" x="600.0" y="309.2" width="8.0" height="10.8" rx="1"/></g><g class="mark"><title>expert rank 56: 0.67% of visits</title><rect class="s1" x="610.0" y="309.8" width="8.0" height="10.2" rx="1"/></g><g class="mark"><title>expert rank 57: 0.54% of visits</title><rect class="s1" x="620.0" y="311.8" width="8.0" height="8.2" rx="1"/></g><g class="mark"><title>expert rank 58: 0.35% of visits</title><rect class="s1" x="630.0" y="314.7" width="8.0" height="5.3" rx="1"/></g><g class="mark"><title>expert rank 59: 0.24% of visits</title><rect class="s1" x="640.0" y="316.4" width="8.0" height="3.6" rx="1"/></g><g class="mark"><title>expert rank 60: 0.19% of visits</title><rect class="s1" x="650.0" y="317.1" width="8.0" height="2.9" rx="1"/></g><g class="mark"><title>expert rank 61: 0.16% of visits</title><rect class="s1" x="660.0" y="317.6" width="8.0" height="2.4" rx="1"/></g><g class="mark"><title>expert rank 62: 0.14% of visits</title><rect class="s1" x="670.0" y="317.8" width="8.0" height="2.2" rx="1"/></g><g class="mark"><title>expert rank 63: 0.13% of visits</title><rect class="s1" x="680.0" y="318.0" width="8.0" height="2.0" rx="1"/></g><g class="mark"><title>expert rank 64: 0.10% of visits</title><rect class="s1" x="690.0" y="318.5" width="8.0" height="1.5" rx="1"/></g><line class="base-line" x1="56.0" y1="296.3" x2="704.0" y2="296.3"/><text class="t-tick" x="708.0" y="300.3" text-anchor="start">fair 1/64</text><text class="t-val" x="64.0" y="216.0" text-anchor="start">hottest: 4.2x fair</text><line class="axis" x1="56.0" y1="320.0" x2="704.0" y2="320.0"/><text class="t-tick" x="380.0" y="380.0" text-anchor="middle">Measured: OLMoE-1B-7B (64 experts, top-8) routing 12,288 WikiText tokens. Experts sorted by load.</text></svg><figcaption>Measured: share of all expert slots each of the 64 experts receives in one layer of OLMoE-1B-7B, sorted. The dashed line is a perfectly fair 1/64.</figcaption></figure>

Then what that does to GPUs:

<figure class="fig"><svg viewBox="0 0 760 350" role="img" aria-label="Load on the busiest GPU divided by the average, OLMoE routing on WikiText. With 4,096-token batches random routing would be nearly even (1.02 to 1.10) but real routing gives 1.32 at EP=8, 1.57 at EP=16 and 3.18 at EP=64."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="s2" x="170.0" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="188.0" y="20.0" text-anchor="start">random routing (bad luck only)</text><rect class="s1" x="412.0" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="430.0" y="20.0" text-anchor="start">real routing, WikiText (measured)</text><text class="t-note" x="160.0" y="54.0" text-anchor="end">EP=8, 64 tokens</text><g class="mark"><title>EP=8 batch 64: random 1.17</title><rect class="s2" x="170.0" y="36.0" width="166.1" height="15.0" rx="3"/></g><g class="mark"><title>EP=8 batch 64: real 1.35</title><rect class="s1" x="170.0" y="54.0" width="191.6" height="15.0" rx="3"/></g><text class="t-tick" x="342.1" y="48.0" text-anchor="start">1.17</text><text class="t-val" x="367.6" y="66.0" text-anchor="start">1.35x</text><text class="t-note" x="160.0" y="100.0" text-anchor="end">EP=8, 4,096 tokens</text><g class="mark"><title>EP=8 batch 4096: random 1.02</title><rect class="s2" x="170.0" y="82.0" width="144.5" height="15.0" rx="3"/></g><g class="mark"><title>EP=8 batch 4096: real 1.32</title><rect class="s1" x="170.0" y="100.0" width="186.4" height="15.0" rx="3"/></g><text class="t-tick" x="320.5" y="94.0" text-anchor="start">1.02</text><text class="t-val" x="362.4" y="112.0" text-anchor="start">1.32x</text><text class="t-note" x="160.0" y="156.0" text-anchor="end">EP=16, 64 tokens</text><g class="mark"><title>EP=16 batch 64: random 1.30</title><rect class="s2" x="170.0" y="138.0" width="184.4" height="15.0" rx="3"/></g><g class="mark"><title>EP=16 batch 64: real 1.64</title><rect class="s1" x="170.0" y="156.0" width="231.6" height="15.0" rx="3"/></g><text class="t-tick" x="360.4" y="150.0" text-anchor="start">1.30</text><text class="t-val" x="407.6" y="168.0" text-anchor="start">1.64x</text><text class="t-note" x="160.0" y="202.0" text-anchor="end">EP=16, 4,096 tokens</text><g class="mark"><title>EP=16 batch 4096: random 1.04</title><rect class="s2" x="170.0" y="184.0" width="146.9" height="15.0" rx="3"/></g><g class="mark"><title>EP=16 batch 4096: real 1.57</title><rect class="s1" x="170.0" y="202.0" width="222.7" height="15.0" rx="3"/></g><text class="t-tick" x="322.9" y="196.0" text-anchor="start">1.04</text><text class="t-val" x="398.7" y="214.0" text-anchor="start">1.57x</text><text class="t-note" x="160.0" y="258.0" text-anchor="end">EP=64, 64 tokens</text><g class="mark"><title>EP=64 batch 64: random 1.85</title><rect class="s2" x="170.0" y="240.0" width="261.5" height="15.0" rx="3"/></g><g class="mark"><title>EP=64 batch 64: real 3.32</title><rect class="s1" x="170.0" y="258.0" width="470.0" height="15.0" rx="3"/></g><text class="t-tick" x="437.5" y="252.0" text-anchor="start">1.85</text><text class="t-val" x="646.0" y="270.0" text-anchor="start">3.32x</text><text class="t-note" x="160.0" y="304.0" text-anchor="end">EP=64, 4,096 tokens</text><g class="mark"><title>EP=64 batch 4096: random 1.10</title><rect class="s2" x="170.0" y="286.0" width="155.7" height="15.0" rx="3"/></g><g class="mark"><title>EP=64 batch 4096: real 3.18</title><rect class="s1" x="170.0" y="304.0" width="449.5" height="15.0" rx="3"/></g><text class="t-tick" x="331.7" y="298.0" text-anchor="start">1.10</text><text class="t-val" x="625.5" y="316.0" text-anchor="start">3.18x</text><text class="t-tick" x="380.0" y="338.0" text-anchor="middle">Busiest GPU / average GPU. 1.00 would be perfect. The whole layer waits for the busiest GPU.</text></svg><figcaption>Busiest GPU divided by the average GPU, from OLMoE's real routing on WikiText against random routing. Experts placed in order, 64/EP per GPU.</figcaption></figure>

| Busiest GPU / average, batches of 4,096 tokens | EP=8 | EP=16 | EP=64 |
|---|---|---|---|
| Random routing (chance only) | 1.02 | 1.04 | 1.10 |
| Real routing, WikiText | 1.32 | 1.57 | 3.18 |
| Real routing, Python code | 1.80 | 2.61 | 6.77 |

Three lessons:

- **Big batches do not save you.** Random imbalance fades as batches grow (1.17 at 64 tokens, 1.02 at 4,096 for EP=8). Real imbalance does not (1.35 and 1.32): it comes from the router's genuine preferences, not from small numbers.
- **Wider EP makes it worse.** At EP=64 each GPU holds one expert, so the busiest GPU is simply the hottest expert: 3.2x the average on WikiText, 6.8x on code. Two thirds or more of the GPU time in the layer is waiting.
- **The hot experts depend on the traffic.** WikiText and code share a median of **1** of their 8 hottest experts per layer. A placement tuned on yesterday's chat traffic can be wrong for today's coding traffic.

(OLMoE was trained with a load-balancing loss, like most MoE models, and DeepSeek-V3 adds its own auxiliary-loss-free balancing. Training reduces the skew, but as these numbers show, it does not remove it at serving time, when the mix of text is whatever users send.)

### The fix: place experts by load, and copy the hot ones

DeepSeek's report describes what they do in production:

> [!PAPER] DeepSeek-AI, DeepSeek-V3 Technical Report · Section 3.4.1 · page 19
> [![DeepSeek-V3 report, 3.4.1 Prefilling: The minimum deployment unit of the prefilling stage consists of 4 nodes with 32 GPUs. The attention part employs 4-way Tensor Parallelism (TP4) with Sequence Parallelism (SP), combined with 8-way Data Parallelism (DP8). Its small TP size of 4 limits the overhead of TP communication. For the MoE part, we use 32-way Expert Parallelism (EP32), which ensures that each expert processes a sufficiently large batch size. To achieve load balancing among different experts, we introduce a deployment strategy of redundant experts, which duplicates high-load experts and deploys them redundantly. The high-load experts are detected based on statistics collected during the online deployment and are adjusted periodically (e.g., every 10 minutes). For the deployment of DeepSeek-V3, we set 32 redundant experts for the prefilling stage. For each GPU, besides the original 8 experts it hosts, it will also host one additional redundant expert.](https://ishwarj.com/img/multigpu/paper-dsv3-prefill.pnger-dsv3-prefill.png)](/img/multigpu/paper-dsv3-prefill.png)
>
> **Context:** Section 3.4, "Inference and Deployment", which describes how DeepSeek serves the model.
>
> **What it says:** attention runs with small tensor parallelism (TP4) copied 8 ways (DP8); the experts are spread 32 ways (EP32). The busiest experts are found from live statistics and **copied**, with the set refreshed about every 10 minutes; every GPU holds its 8 experts plus one extra copy.
>
> **Why it matters:** this is the production answer to the table above: measure the load, then copy hot experts so their tokens can be split.
>
> [Read Section 3.4](https://arxiv.org/pdf/2412.19437#page=18)

`moe_routing.py` tries the same two ideas on OLMoE's routing. It learns from the **first half** of the recorded tokens and is tested on the **second half**, just as a server must use past load to plan for future load:

1. **Placement by load:** put the heaviest experts first, each on the least-loaded GPU that still has room.
2. **Redundant copies:** each GPU gets a few spare slots; repeatedly copy the busiest expert on the busiest GPU to the least-loaded GPU with a free slot (keeping a copy only if it does not create a new busiest GPU), and split that expert's tokens evenly between its copies.

<figure class="fig"><svg viewBox="0 0 760 334" role="img" aria-label="Busiest GPU over average for OLMoE routing on WikiText with experts placed in order, placed by past load, and with extra copies of the hottest experts."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="s2" x="170.0" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="188.0" y="20.0" text-anchor="start">experts in order</text><rect class="s3" x="311.2" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="329.2" y="20.0" text-anchor="start">placed by past load</text><rect class="s1" x="474.0" y="10.0" width="12.0" height="12.0" rx="3"/><text class="t-tick" x="492.0" y="20.0" text-anchor="start">plus redundant copies</text><text class="t-note" x="160.0" y="68.0" text-anchor="end">EP=8, 8 copies</text><g class="mark"><title>EP8+8 default: 1.34</title><rect class="s2" x="170.0" y="38.0" width="188.7" height="16.0" rx="3"/></g><text class="t-tick" x="364.7" y="51.0" text-anchor="start">1.34</text><g class="mark"><title>EP8+8 balanced: 1.28</title><rect class="s3" x="170.0" y="57.0" width="179.9" height="16.0" rx="3"/></g><text class="t-tick" x="355.9" y="70.0" text-anchor="start">1.28</text><g class="mark"><title>EP8+8 redundant: 1.29</title><rect class="s1" x="170.0" y="76.0" width="181.4" height="16.0" rx="3"/></g><text class="t-tick" x="357.4" y="89.0" text-anchor="start">1.29</text><text class="t-note" x="160.0" y="138.0" text-anchor="end">EP=8, 16 copies</text><g class="mark"><title>EP8+16 default: 1.34</title><rect class="s2" x="170.0" y="108.0" width="188.6" height="16.0" rx="3"/></g><text class="t-tick" x="364.6" y="121.0" text-anchor="start">1.34</text><g class="mark"><title>EP8+16 balanced: 1.28</title><rect class="s3" x="170.0" y="127.0" width="180.0" height="16.0" rx="3"/></g><text class="t-tick" x="356.0" y="140.0" text-anchor="start">1.28</text><g class="mark"><title>EP8+16 redundant: 1.29</title><rect class="s1" x="170.0" y="146.0" width="181.8" height="16.0" rx="3"/></g><text class="t-tick" x="357.8" y="159.0" text-anchor="start">1.29</text><text class="t-note" x="160.0" y="208.0" text-anchor="end">EP=16, 16 copies</text><g class="mark"><title>EP16+16 default: 1.58</title><rect class="s2" x="170.0" y="178.0" width="222.8" height="16.0" rx="3"/></g><text class="t-tick" x="398.8" y="191.0" text-anchor="start">1.58</text><g class="mark"><title>EP16+16 balanced: 1.47</title><rect class="s3" x="170.0" y="197.0" width="207.8" height="16.0" rx="3"/></g><text class="t-tick" x="383.8" y="210.0" text-anchor="start">1.47</text><g class="mark"><title>EP16+16 redundant: 1.47</title><rect class="s1" x="170.0" y="216.0" width="207.0" height="16.0" rx="3"/></g><text class="t-tick" x="383.0" y="229.0" text-anchor="start">1.47</text><text class="t-note" x="160.0" y="278.0" text-anchor="end">EP=64, 64 copies</text><g class="mark"><title>EP64+64 default: 3.19</title><rect class="s2" x="170.0" y="248.0" width="450.0" height="16.0" rx="3"/></g><text class="t-tick" x="626.0" y="261.0" text-anchor="start">3.19</text><g class="mark"><title>EP64+64 balanced: 3.19</title><rect class="s3" x="170.0" y="267.0" width="450.0" height="16.0" rx="3"/></g><text class="t-tick" x="626.0" y="280.0" text-anchor="start">3.19</text><g class="mark"><title>EP64+64 redundant: 2.77</title><rect class="s1" x="170.0" y="286.0" width="390.1" height="16.0" rx="3"/></g><text class="t-tick" x="566.1" y="299.0" text-anchor="start">2.77</text><text class="t-tick" x="380.0" y="322.0" text-anchor="middle">Simulated on measured routing: placement learned on the first half of the text, tested on the second half.</text></svg><figcaption>Simulated on measured routing (WikiText, batches of 4,096 tokens): experts in order, placed by past load, and with redundant copies. Learned on the first half of the text, tested on the second.</figcaption></figure>

| Busiest / average, tested on unseen tokens | In order | Placed by load | Plus redundant copies |
|---|---|---|---|
| WikiText, EP=8 (8 copies) | 1.34 | 1.28 | 1.29 |
| WikiText, EP=16 (16 copies) | 1.58 | 1.47 | 1.47 |
| WikiText, EP=64 (64 copies) | 3.19 | 3.19 | 2.77 |
| Code, EP=8 (8 copies) | 1.81 | 1.12 | 1.12 |
| Code, EP=16 (16 copies) | 2.62 | 1.74 | 1.37 |
| Code, EP=64 (64 copies) | 6.75 | 6.75 | 1.67 |

When a GPU holds several experts, **placing them by load** does most of the work (code at EP=8: 1.81 to 1.12). When a GPU holds only one expert, placement cannot help at all, and **only copies help** (code at EP=64: 6.75 to 1.67). The leftover imbalance on WikiText comes from the traffic changing between the two halves of the text, which is exactly why DeepSeek refreshes its choice every 10 minutes and is "exploring a dynamic redundancy strategy" (same section) that re-plans for every batch.

### DeepSeek-V3 in production

Putting the pieces together, here is how DeepSeek serves V3, with prefill and decode on **separate** groups of machines (Section 7 explains why):

<figure class="fig"><svg viewBox="0 0 760 364" role="img" aria-label="DeepSeek-V3 deployment from its technical report. Prefill units of 4 nodes (32 GPUs) use TP4 with DP8 for attention and EP32 for experts. Decode units of 40 nodes (320 GPUs) use TP4 with DP80 for attention and EP320, one expert per GPU."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="22.0" text-anchor="start">DeepSeek-V3 serving on H800s (technical report, Section 3.4)</text><rect class="plane" x="20.0" y="40.0" width="720.0" height="130.0" rx="12"/><text class="t-note" x="36.0" y="66.0" text-anchor="start">Prefill unit: 4 nodes, 32 GPUs</text><rect class="box-1" x="40.0" y="84.0" width="60.0" height="10.0" rx="2"/><rect class="box-1" x="110.0" y="84.0" width="60.0" height="10.0" rx="2"/><rect class="box-1" x="180.0" y="84.0" width="60.0" height="10.0" rx="2"/><rect class="box-1" x="250.0" y="84.0" width="60.0" height="10.0" rx="2"/><text class="t-tick" x="36.0" y="132.0" text-anchor="start">attention: TP4 + SP, DP8</text><text class="t-tick" x="36.0" y="150.0" text-anchor="start">MoE: EP32, 8 experts + 1 redundant per GPU</text><text class="t-tick" x="724.0" y="66.0" text-anchor="end">two micro-batches overlap compute and all-to-all</text><rect class="plane" x="20.0" y="190.0" width="720.0" height="130.0" rx="12"/><text class="t-note" x="36.0" y="216.0" text-anchor="start">Decode unit: 40 nodes, 320 GPUs</text><rect class="box-1" x="40.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="56.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="72.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="88.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="104.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="120.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="136.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="152.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="168.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="184.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="200.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="216.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="232.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="248.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="264.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="280.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="296.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="312.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="328.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="344.0" y="234.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="40.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="56.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="72.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="88.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="104.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="120.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="136.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="152.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="168.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="184.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="200.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="216.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="232.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="248.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="264.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="280.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="296.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="312.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="328.0" y="248.0" width="12.0" height="10.0" rx="2"/><rect class="box-1" x="344.0" y="248.0" width="12.0" height="10.0" rx="2"/><text class="t-tick" x="36.0" y="282.0" text-anchor="start">attention: TP4 + SP, DP80</text><text class="t-tick" x="36.0" y="300.0" text-anchor="start">MoE: EP320, 1 expert per GPU (64 GPUs for redundant + shared)</text><text class="t-tick" x="724.0" y="216.0" text-anchor="end">point-to-point IB transfers, IBGDA</text><text class="t-tick" x="380.0" y="352.0" text-anchor="middle">Prefill and decode run on separate units: disaggregation plus very wide expert parallelism.</text></svg><figcaption>DeepSeek-V3's deployment units as described in its technical report. Prefill uses 32 GPUs per unit; decode uses 320, with one expert per GPU.</figcaption></figure>

> [!PAPER] DeepSeek-AI, DeepSeek-V3 Technical Report · Section 3.4.2 · page 19
> [![DeepSeek-V3 report, 3.4.2 Decoding: During decoding, we treat the shared expert as a routed one, so each token will select 9 experts. The minimum deployment unit of the decoding stage consists of 40 nodes with 320 GPUs. The attention part employs TP4 with SP, combined with DP80, while the MoE part uses EP320. For the MoE part, each GPU hosts only one expert, and 64 GPUs are responsible for hosting redundant experts and shared experts. All-to-all communication of the dispatch and combine parts is performed via direct point-to-point transfers over IB to achieve low latency. Additionally, we leverage the IBGDA technology to further minimize latency and enhance communication efficiency.](https://ishwarj.com/img/multigpu/paper-dsv3-decode.pngper-dsv3-decode.png)](/img/multigpu/paper-dsv3-decode.png)
>
> **Context:** the decoding half of Section 3.4.
>
> **What it says:** decode uses a much wider unit than prefill, 320 GPUs, with **one expert per GPU** and 64 GPUs for copies and the shared expert. Tokens move by direct point-to-point InfiniBand writes, issued by the GPU itself (IBGDA), to keep latency low.
>
> **Why it matters:** decode processes few tokens per step, so it needs many GPUs' worth of memory bandwidth and very low-latency all-to-all. One expert per GPU is exactly the case where our simulation says only redundant copies can fix imbalance.
>
> [Read Section 3.4.2](https://arxiv.org/pdf/2412.19437#page=19)

DeepSeek later published what this looks like in daily operation. Their [inference system overview](https://github.com/deepseek-ai/open-infra-index/blob/main/202502OpenSourceWeek/day_6_one_more_thing_deepseekV3R1_inference_system_overview.md) (February 2025) describes a slightly different production layout than the paper, **EP32 for prefill over 4 nodes and EP144 for decode over 18 nodes**, each with 32 redundant experts, and reports the results over one day:

[![DeepSeek inference system overview: Prefilling Phase [Routed Expert EP32, MLA/Shared Expert DP32]: Each deployment unit spans 4 nodes with 32 redundant routed experts, where each GPU handles 9 routed experts and 1 shared expert. Decoding Phase [Routed Expert EP144, MLA/Shared Expert DP144]: Each deployment unit spans 18 nodes with 32 redundant routed experts, where each GPU manages 2 routed experts and 1 shared expert.](https://ishwarj.com/img/multigpu/deepseek-day6-units.pngseek-day6-units.png)](/img/multigpu/deepseek-day6-units.png)

[![DeepSeek inference system overview statistics: total input tokens 608B, of which 342B tokens (56.3%) hit the on-disk KV cache; total output tokens 168B; average output speed 20 to 22 tokens per second; each H800 node delivers an average throughput of about 73.7k tokens/s input including cache hits during prefilling or about 14.8k tokens/s output during decoding.](https://ishwarj.com/img/multigpu/deepseek-day6-stats.pngseek-day6-stats.png)](/img/multigpu/deepseek-day6-stats.png)

Each H800 node delivered **about 73.7 thousand input tokens per second** in prefill (including cache hits) or **about 14.8 thousand output tokens per second** in decode. Prefill nodes move five times more tokens, which is the compute-bound against memory-bound gap of Part 1 showing up at cluster scale, and one more reason to give the two phases different machines.

## 7. Disaggregated prefill and decode

### Why the two phases get in each other's way

[Part 1](https://ishwarj.com/writings/llm-inference-1-prefill-and-decode/) showed that prefill and decode are different kinds of work. Prefill processes thousands of prompt tokens at once and is limited by arithmetic (compute-bound). Decode produces one token per sequence per step and is limited by reading memory (memory-bound). [Part 3](https://ishwarj.com/writings/llm-inference-3-vllm/) showed what happens when one GPU does both: a long prompt arriving in the middle of other users' answers either **stalls** those answers while it is prefilled, or, with chunked prefill, is cut into slices that **slow every step a little** for longer.

> [!DEFINITION] Disaggregated serving
> Prefill and decode run on different GPUs (often different machines). A prefill instance processes the prompt and produces the first token and the KV cache; the KV cache is then transferred to a decode instance, which generates the rest of the answer. Also called prefill/decode (PD) disaggregation or phase splitting.

The cost model of the simulator below makes the interference concrete. A user is one of 32 sequences being decoded on an H100 running Llama 3.1 8B when another user's 2,000-token prompt arrives:

<figure class="fig"><svg viewBox="0 0 760 222" role="img" aria-label="Token timeline for a user who is decoding while another user&#x27;s 2,000-token prompt arrives. Prefill-first stalls the user for the whole prefill; chunked prefill slows several steps a little; on a dedicated decode GPU the steps are unaffected."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="22.0" text-anchor="start">What one user sees while a 2,000-token prompt arrives (cost model of disagg_sim.py)</text><text class="t-note" x="20.0" y="60.0" text-anchor="start">colocated, prefill first</text><rect class="box-1" x="250.0" y="44.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="266.5" y="60.0" text-anchor="middle">tok</text><rect class="box-1" x="283.1" y="44.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="299.6" y="60.0" text-anchor="middle">tok</text><rect class="waste" x="316.1" y="44.0" width="194.7" height="24.0" rx="3"/><text class="t-cell" x="414.5" y="60.0" text-anchor="middle">prefill 68 ms: no tokens</text><rect class="box-1" x="512.8" y="44.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="529.4" y="60.0" text-anchor="middle">tok</text><rect class="box-1" x="545.9" y="44.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="562.4" y="60.0" text-anchor="middle">tok</text><text class="t-note" x="20.0" y="106.0" text-anchor="start">colocated, chunked (512)</text><rect class="box-1" x="250.0" y="90.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="266.5" y="106.0" text-anchor="middle">tok</text><rect class="box-1" x="283.1" y="90.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="299.6" y="106.0" text-anchor="middle">tok</text><rect class="box-2" x="316.1" y="90.0" width="53.9" height="24.0" rx="3"/><rect class="box-2" x="372.0" y="90.0" width="53.9" height="24.0" rx="3"/><rect class="box-2" x="428.0" y="90.0" width="53.9" height="24.0" rx="3"/><rect class="box-2" x="483.9" y="90.0" width="53.9" height="24.0" rx="3"/><rect class="box-1" x="539.8" y="90.0" width="31.1" height="24.0" rx="3"/><text class="t-note" x="20.0" y="152.0" text-anchor="start">disaggregated: decode GPU</text><rect class="box-1" x="250.0" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="266.5" y="152.0" text-anchor="middle">tok</text><rect class="box-1" x="283.1" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="299.6" y="152.0" text-anchor="middle">tok</text><rect class="box-1" x="316.1" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="332.7" y="152.0" text-anchor="middle">tok</text><rect class="box-1" x="349.2" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="365.7" y="152.0" text-anchor="middle">tok</text><rect class="box-1" x="382.2" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="398.8" y="152.0" text-anchor="middle">tok</text><rect class="box-1" x="415.3" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="431.8" y="152.0" text-anchor="middle">tok</text><rect class="box-1" x="448.4" y="136.0" width="31.1" height="24.0" rx="3"/><text class="t-cell" x="464.9" y="152.0" text-anchor="middle">tok</text><text class="t-tick" x="250.0" y="186.0" text-anchor="start">decode step 11.4 ms (batch 32);  with a 512-token chunk 19.3 ms;  whole prefill 68 ms</text><text class="t-tick" x="380.0" y="210.0" text-anchor="middle">The prompt belongs to another user; ours is one of the 32 sequences already decoding.</text></svg><figcaption>What one decoding user sees when someone else's long prompt arrives, from the simulator's cost model (roofline with assumed efficiencies, Llama 3.1 8B on an H100).</figcaption></figure>

With prefill first, the user's stream freezes for the whole 67.8 ms prefill, six normal steps long. With 512-token chunks, four of the user's steps grow from 11.4 ms to 19.3 ms. On a decode-only GPU nothing happens at all.

### The papers: Splitwise and DistServe

Two papers in 2024 made the case for splitting the phases onto separate GPUs. **Splitwise** (Patel et al., Microsoft and the University of Washington) started from production traces and the observation that decode does not need the newest, most compute-heavy GPUs; it reported clusters with "up to 1.4x higher throughput at 20% lower cost". **DistServe** (Zhong et al., OSDI 2024) framed the goal as **goodput**, defined in [Part 6](https://ishwarj.com/writings/llm-inference-6-serving-in-production/): the highest request rate at which a target share of requests still meets both the time-to-first-token (TTFT) and time-per-output-token (TPOT) objectives. Its Figure 1 is the clearest picture of the problem:

> [!PAPER] Zhong et al., DistServe · Figure 1 · page 1
> [![DistServe Figure 1: Performance when serving an LLM with 13B parameters under a synthetic workload with input length 512 and output length 64 on one NVIDIA 80GB A100. Upper: P90 time-to-first-token for existing systems against a prefill-only system, with the SLA line crossed at about 1.6 and 5.6 requests per second. Lower: P90 time-per-output-token for existing systems against a decode-only system, with the SLA crossed at about 1.6 and 10 requests per second.](https://ishwarj.com/img/multigpu/paper-distserve-fig1.png-distserve-fig1.png)](/img/multigpu/paper-distserve-fig1.png)
>
> **Context:** the first figure of the paper, a 13B model on one A100 with 512-token prompts and 64-token answers.
>
> **What it says:** a GPU doing both phases meets the latency objectives up to about 1.6 requests per second. A GPU doing only prefill handles 5.6, and one doing only decode handles 10.
>
> [Read the introduction](https://arxiv.org/pdf/2401.09670#page=1)

> [!PAPER] Zhong et al., DistServe · Section 1 · page 2
> [![DistServe: Under the SLO attainment of 90%, the maximum achievable goodput on a single A100 GPU, constrained by the more stringent one of TTFT and TPOT requirements, is about 1.6 requests per second. The performance contrasts sharply when each phase is served independently on a separate GPU, which achieve per-GPU goodput of 5.6 rps for the prefill phase and 10 rps for decoding. Ideally, by allocating 2 GPUs for prefill and 1 GPU for decoding, we can effectively serve the model with an overall goodput of 10 rps, or equally 3.3 rps per GPU, which is 2.1x higher than existing systems.](https://ishwarj.com/img/multigpu/paper-distserve-goodput.pngstserve-goodput.png)](/img/multigpu/paper-distserve-goodput.png)
>
> **What it says:** two prefill GPUs (2 x 5.6 = 11.2 rps of prefill) feeding one decode GPU (10 rps) serve 10 rps on 3 GPUs: 3.3 per GPU, **2.1x** the 1.6 of a GPU doing both.
>
> **Why it matters:** the gain comes from two things together: no interference, and the freedom to choose the **ratio** of prefill to decode GPUs (and, in the full paper, a different parallel layout for each). Across their workloads the authors report up to "7.4x more requests or 12.6x tighter SLO" than the systems they compared with.
>
> [Read Section 1](https://arxiv.org/pdf/2401.09670#page=2)

Notice what the baseline was: "existing systems" in early 2024 meant vLLM without chunked prefill. That matters for our own simulation below.

### The cost: moving the KV cache

Splitting the phases adds one job: the prompt's KV cache, built on the prefill GPU, must be copied to the decode GPU before the second token. Its size is the bytes-per-token of Section 1 times the prompt length, and its transfer time is size over bandwidth:

$$
t_{\text{transfer}} = \frac{2\,L\,H_{\mathrm{KV}}\,d_h\,b_{kv}\times T}{\beta}
$$

where:

- $$T$$ is the number of prompt tokens and the numerator is the KV cache in bytes (Section 1);
- $$\beta$$ is the bandwidth of the path between the two GPUs.

DistServe works one example; our script reproduces it first, to check the formula:

> [!PAPER] Zhong et al., DistServe · Section 3.3 · page 6
> [![DistServe: Communication overhead. Transferring KV caches from prefill to decoding instances incurs notable overheads. For example, the KV cache size of a single 512-token request on OPT-66B is approximately 1.13GB. Assuming an average arrival rate of 10 rps, we need to transfer 11.3GB data per second, or equivalently 90Gbps bandwidth to render the overhead invisible. While many modern GPU clusters for LLMs are equipped with InfiniBand (e.g., 800 Gbps), in cases where cross-node bandwidth is limited, DistServe relies on the commonly available intra-node NVLINK, where the peak bandwidth between A100 GPUs is 600 GB/s, again rendering the transmission overhead negligible.](https://ishwarj.com/img/multigpu/paper-distserve-kv.pnger-distserve-kv.png)](/img/multigpu/paper-distserve-kv.png)
>
> **What it says:** 1.13 GB per 512-token request for OPT-66B; at 10 requests per second that is about 90 Gbps of sustained traffic, easy for InfiniBand or NVLink, hard for ordinary networks.
>
> [Read Section 3.3](https://arxiv.org/pdf/2401.09670#page=6)

**Worked example (check).** OPT-66B has 64 layers and full multi-head attention with width 9,216, so $$2\times64\times9{,}216\times2 = 2{,}359{,}296$$ bytes per token; times 512 tokens is $$1{,}207{,}959{,}552$$ bytes = **1.125 GiB**, and at 10 per second, 90 Gibit/s. The paper's 1.13GB and 90Gbps are the same numbers counted in powers of two.

**Worked example (today).** Llama 3.1 70B, an 8,192-token prompt: $$320\ \text{KiB}\times8{,}192 = 2.5$$ GiB. Over one 400 Gb/s card (50 GB/s): **54 ms**. Over NVLink in the same server: 6 ms. If the prefill side runs TP=8 and the decode side runs TP=8, each GPU holds one eighth of the cache and can send it over **its own** network card, so the 8 cards together move it in 6.7 ms.

<figure class="fig"><svg viewBox="0 0 760 438" role="img" aria-label="Time to move the KV cache of one 8,192-token prompt compared with the time to prefill it. Over NVLink or one NIC per GPU it is a few milliseconds; over one 400 Gb/s card it is 12 to 54 ms; over 100 Gb/s Ethernet 46 to 215 ms."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><line class="grid" x1="260.0" y1="26.0" x2="260.0" y2="386.0"/><text class="t-tick" x="260.0" y="402.0" text-anchor="middle">1 ms</text><line class="grid" x1="403.3" y1="26.0" x2="403.3" y2="386.0"/><text class="t-tick" x="403.3" y="402.0" text-anchor="middle">10 ms</text><line class="grid" x1="546.7" y1="26.0" x2="546.7" y2="386.0"/><text class="t-tick" x="546.7" y="402.0" text-anchor="middle">100 ms</text><line class="grid" x1="690.0" y1="26.0" x2="690.0" y2="386.0"/><text class="t-tick" x="690.0" y="402.0" text-anchor="middle">1,000 ms</text><text class="t-note" x="20.0" y="40.0" text-anchor="start">Llama 3.1 8B (1 GPU): 1.00 GiB of KV</text><text class="t-tick" x="252.0" y="60.0" text-anchor="end">prefill itself (~)</text><g class="mark"><title>prefill itself (~): 301.6 ms</title><rect class="s3" x="260.0" y="48.0" width="355.4" height="15.0" rx="3"/></g><text class="t-tick" x="621.4" y="60.0" text-anchor="start">301.6</text><text class="t-tick" x="252.0" y="79.0" text-anchor="end">NVLink (same node)</text><g class="mark"><title>NVLink (same node): 2.4 ms</title><rect class="s1" x="260.0" y="67.0" width="54.1" height="15.0" rx="3"/></g><text class="t-tick" x="320.1" y="79.0" text-anchor="start">2.4</text><text class="t-tick" x="252.0" y="98.0" text-anchor="end">8 x 400G NICs</text><g class="mark"><title>8 x 400G NICs: 2.7 ms</title><rect class="s1" x="260.0" y="86.0" width="61.5" height="15.0" rx="3"/></g><text class="t-tick" x="327.5" y="98.0" text-anchor="start">2.7</text><text class="t-tick" x="252.0" y="117.0" text-anchor="end">1 x 400G NIC</text><g class="mark"><title>1 x 400G NIC: 21.5 ms</title><rect class="s1" x="260.0" y="105.0" width="190.9" height="15.0" rx="3"/></g><text class="t-tick" x="456.9" y="117.0" text-anchor="start">21.5</text><text class="t-tick" x="252.0" y="136.0" text-anchor="end">100G Ethernet</text><g class="mark"><title>100G Ethernet: 85.9 ms</title><rect class="s1" x="260.0" y="124.0" width="277.2" height="15.0" rx="3"/></g><text class="t-tick" x="543.2" y="136.0" text-anchor="start">85.9</text><text class="t-note" x="20.0" y="160.0" text-anchor="start">Llama 3.1 70B (TP=8): 2.50 GiB of KV</text><text class="t-tick" x="252.0" y="180.0" text-anchor="end">prefill itself (~)</text><g class="mark"><title>prefill itself (~): 314.4 ms</title><rect class="s3" x="260.0" y="168.0" width="358.0" height="15.0" rx="3"/></g><text class="t-tick" x="624.0" y="180.0" text-anchor="start">314.4</text><text class="t-tick" x="252.0" y="199.0" text-anchor="end">NVLink (same node)</text><g class="mark"><title>NVLink (same node): 6.0 ms</title><rect class="s1" x="260.0" y="187.0" width="111.2" height="15.0" rx="3"/></g><text class="t-tick" x="377.2" y="199.0" text-anchor="start">6.0</text><text class="t-tick" x="252.0" y="218.0" text-anchor="end">8 x 400G NICs</text><g class="mark"><title>8 x 400G NICs: 6.7 ms</title><rect class="s1" x="260.0" y="206.0" width="118.5" height="15.0" rx="3"/></g><text class="t-tick" x="384.5" y="218.0" text-anchor="start">6.7</text><text class="t-tick" x="252.0" y="237.0" text-anchor="end">1 x 400G NIC</text><g class="mark"><title>1 x 400G NIC: 53.7 ms</title><rect class="s1" x="260.0" y="225.0" width="247.9" height="15.0" rx="3"/></g><text class="t-tick" x="513.9" y="237.0" text-anchor="start">53.7</text><text class="t-tick" x="252.0" y="256.0" text-anchor="end">100G Ethernet</text><g class="mark"><title>100G Ethernet: 214.7 ms</title><rect class="s1" x="260.0" y="244.0" width="334.2" height="15.0" rx="3"/></g><text class="t-tick" x="600.2" y="256.0" text-anchor="start">214.7</text><text class="t-note" x="20.0" y="280.0" text-anchor="start">DeepSeek-V3 MLA (32 GPUs): 0.54 GiB of KV</text><text class="t-tick" x="252.0" y="300.0" text-anchor="end">prefill itself (~)</text><g class="mark"><title>prefill itself (~): 42.0 ms</title><rect class="s3" x="260.0" y="288.0" width="232.7" height="15.0" rx="3"/></g><text class="t-tick" x="498.7" y="300.0" text-anchor="start">42.0</text><text class="t-tick" x="252.0" y="319.0" text-anchor="end">NVLink (same node)</text><g class="mark"><title>NVLink (same node): 1.3 ms</title><rect class="s1" x="260.0" y="307.0" width="15.3" height="15.0" rx="3"/></g><text class="t-tick" x="281.3" y="319.0" text-anchor="start">1.3</text><text class="t-tick" x="252.0" y="338.0" text-anchor="end">8 x 400G NICs</text><g class="mark"><title>8 x 400G NICs: 1.4 ms</title><rect class="s1" x="260.0" y="326.0" width="22.7" height="15.0" rx="3"/></g><text class="t-tick" x="288.7" y="338.0" text-anchor="start">1.4</text><text class="t-tick" x="252.0" y="357.0" text-anchor="end">1 x 400G NIC</text><g class="mark"><title>1 x 400G NIC: 11.5 ms</title><rect class="s1" x="260.0" y="345.0" width="152.1" height="15.0" rx="3"/></g><text class="t-tick" x="418.1" y="357.0" text-anchor="start">11.5</text><text class="t-tick" x="252.0" y="376.0" text-anchor="end">100G Ethernet</text><g class="mark"><title>100G Ethernet: 46.1 ms</title><rect class="s1" x="260.0" y="364.0" width="238.4" height="15.0" rx="3"/></g><text class="t-tick" x="504.4" y="376.0" text-anchor="start">46.1</text><text class="t-tick" x="380.0" y="426.0" text-anchor="middle">Arithmetic: BF16 cache, peak link bandwidth, prefill at 50% of H100 peak FLOP/s. Log scale.</text></svg><figcaption>Moving the KV cache of one 8,192-token prompt, against the time to prefill it. Arithmetic with peak link bandwidths; prefill assumed at 50% of H100 peak.</figcaption></figure>

[![Terminal output of kv_transfer.py: the DistServe check (1.125 GiB, 90 Gibit/s), then KV size, prefill time and transfer time over NVLink, eight 400G NICs, one 400G NIC and 100G Ethernet for Llama 3.1 8B, Llama 3.1 70B and DeepSeek-V3 at 1,024, 8,192 and 32,768 tokens, and the extra delay before the second token when sending at the end or layer by layer](https://ishwarj.com/img/multigpu/kv_transfer-run.pngkv_transfer-run.png)](/img/multigpu/kv_transfer-run.png)

Two things make this cheaper than it looks. First, MLA: DeepSeek-V3's 8,192-token cache is only 0.54 GiB, a fifth of 70B's. Second, the transfer does not have to wait for the prefill to finish. Splitwise sends each layer's KV as soon as that layer is done:

> [!PAPER] Patel et al., Splitwise · Section IV-C · page 7
> [![Splitwise: The time required for the transfer depends on the size of the KV cache and on the bandwidth of the interconnect between the prompt and the token machines. Even when using fast InfiniBand links, the transfer overhead for large prompt sizes could become a significant fraction of the TBT. In Splitwise, we optimize the KV-cache transfer by overlapping it with the computation in the prompt phase. As each layer in the LLM gets calculated in the prompt machine, the KV cache corresponding to that layer is also generated. At the end of each layer, we trigger an asynchronous transfer of the KV-cache for that layer while the prompt computation continues to the next layer.](https://ishwarj.com/img/multigpu/paper-splitwise-layerwise.pngtwise-layerwise.png)](/img/multigpu/paper-splitwise-layerwise.png)
>
> **Context:** Splitwise's "prompt machine" does prefill and its "token machine" does decode; TBT is time between tokens.
>
> **What it says:** each layer's KV leaves while the next layer is still computing, so most of the transfer hides behind the prefill.
>
> [Read Section IV-C](https://arxiv.org/pdf/2311.18677#page=7)

With layer-by-layer sending, what is left after the prefill is roughly the larger of one layer's share and the part of the transfer that did not fit under the computation:

$$
t_{\text{exposed}} \approx \max\!\left(\frac{t_{\text{transfer}}}{L},\ t_{\text{transfer}} - t_{\text{prefill}}\cdot\frac{L-1}{L}\right)
$$

where $$L$$ is the number of layers and $$t_{\text{prefill}}$$ the prefill time. For the 70B example over one card: the transfer (54 ms) is far shorter than the prefill (about 314 ms at the assumed efficiency), so only one layer's share is left: $$54/80 = 0.67$$ ms.

<figure class="fig"><svg viewBox="0 0 760 174" role="img" aria-label="Sending the KV cache only after prefill adds 54 ms before the second token for a 70B model over one 400 Gb/s card. Sending each layer as soon as it is ready hides almost all of it."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="22.0" text-anchor="start">Llama 3.1 70B, 8,192-token prompt, KV over one 400 Gb/s NIC (54 ms of transfer)</text><text class="t-note" x="20.0" y="62.0" text-anchor="start">send at the end</text><rect class="box-2" x="170.0" y="46.0" width="371.2" height="24.0" rx="3"/><text class="t-cell" x="355.6" y="63.0" text-anchor="middle">prefill 314 ms</text><rect class="waste" x="541.2" y="46.0" width="63.4" height="24.0" rx="3"/><text class="t-cell" x="572.9" y="63.0" text-anchor="middle">+54</text><rect class="box-1" x="604.6" y="46.0" width="22.0" height="24.0" rx="3"/><text class="t-tick" x="630.6" y="63.0" text-anchor="start">2nd token</text><text class="t-note" x="20.0" y="112.0" text-anchor="start">layer by layer</text><rect class="box-2" x="170.0" y="96.0" width="371.2" height="24.0" rx="3"/><text class="t-cell" x="355.6" y="113.0" text-anchor="middle">prefill 314 ms</text><rect class="s1" x="206.3" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="243.4" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="280.6" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="317.7" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="354.8" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="391.9" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="429.1" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="466.2" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="503.3" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="s1" x="540.4" y="124.0" width="0.6" height="10.0" rx="2"/><rect class="box-1" x="542.0" y="96.0" width="22.0" height="24.0" rx="3"/><text class="t-tick" x="568.0" y="113.0" text-anchor="start">2nd token (+0.7 ms)</text><text class="t-tick" x="380.0" y="160.0" text-anchor="middle">Each layer&#x27;s KV leaves while the next layers compute, so only the last layer&#x27;s share is left at the end.</text></svg><figcaption>The same 2.5 GiB transfer sent after the prefill or layer by layer. Arithmetic from kv_transfer.py.</figcaption></figure>

Splitwise measured the effect on real hardware:

> [!PAPER] Patel et al., Splitwise · Section VI-A · page 9
> [![Splitwise: End-to-end impact. The latency impact of serially transferring the KV-cache grows up to 3% of the E2E with large prompts. However, Splitwise only incurs 0.8% of E2E. In a user-facing inference, the only visible impact of KV-cache transfer overhead is the latency for the second token. Splitwise adds a 16.5% latency to the second token, as compared to the 64% overhead from a serialized transfer. Overall, the transfer impact in Splitwise is hardly perceivable even in a user-facing inference.](https://ishwarj.com/img/multigpu/paper-splitwise-result.pngplitwise-result.png)](/img/multigpu/paper-splitwise-result.png)
>
> **What it says:** layer-wise transfer cut the cost from up to 3% of end-to-end latency to 0.8%, and the delay of the second token from +64% to +16.5%.
>
> [Read Section VI-A](https://arxiv.org/pdf/2311.18677#page=9)

### Mooncake: build the system around the KV cache

Moonshot AI's **Mooncake**, the serving platform behind their Kimi assistant, takes the idea one step further. If KV caches are going to travel between machines anyway, treat them as the central object: keep them in a pool spread across the cluster's spare CPU memory and SSDs, and schedule every request by where its cache already is.

> [!PAPER] Qin et al., Mooncake · Figure 1 · page 2
> [![Mooncake Figure 1, Mooncake Architecture: a KVCache-centric Conductor with a cache-aware prefill scheduler, a KVCache balance scheduler and a load-balance decoding scheduler; a prefill pool of instances with chunked prefill and paged KV cache in GPU memory; a distributed KVCache pool in CPU memory, DRAM and SSD connected by RDMA; and a decoding pool. The prefill stage optimises for maximum cache reuse subject to the TTFT SLO and MFU; the decoding stage optimises for maximum throughput subject to the TBT SLO.](https://ishwarj.com/img/multigpu/paper-mooncake-fig1.pngr-mooncake-fig1.png)](/img/multigpu/paper-mooncake-fig1.png)
>
> **Context:** the overview figure of the paper.
>
> **What it says:** a global scheduler ("Conductor") picks a prefill instance that already holds as much of the prompt's KV as possible, sends the result over RDMA to a decode instance with room, and balances the KV pool across machines.
>
> **Why it matters:** disaggregation and prefix caching (Parts 3 and 5) combine: the KV cache becomes a cluster-wide resource. Mooncake reports up to 525% more throughput in simulated long-context scenarios and 75% more requests handled under real Kimi workloads.
>
> [Read the paper](https://arxiv.org/abs/2407.00079)

DeepSeek's production numbers from Section 6 show the same pattern at scale: 56.3% of their input tokens hit an on-disk KV cache.

### A simulation: when is it worth it?

The papers report big wins against the systems of their time. Engines have improved since, especially with chunked prefill. So `disagg_sim.py` compares the options on equal hardware, under clearly stated assumptions:

- **4 H100s serving Llama 3.1 8B.** Each iteration costs $$\max(\text{bytes}/(0.7 \times 3.35\ \text{TB/s}),\ \text{FLOPs}/(0.5\times989\ \text{TFLOP/s})) + 1$$ ms, with attention FLOPs counted for prompts. The 70% and 50% efficiencies and the 1 ms overhead are assumptions.
- **Layouts:** four colocated GPUs with prefill first; four colocated GPUs with 512-token chunked prefill; and disaggregated 1+3, 2+2 and 3+1 prefill and decode GPUs, with the KV cache moved over NVLink.
- **Workloads:** "chat" with 1,000 to 3,000-token prompts and 100 to 400-token answers; "long" with 8,000 to 16,000-token prompts and 50 to 200-token answers. Poisson arrivals for 120 simulated seconds.
- **SLOs:** loose (TTFT 1 s, TPOT 40 ms), tight (TTFT 0.5 s, TPOT 15 ms) and strict (TTFT 1 s, TPOT 12 ms). Goodput is the highest rate at which 90% of requests meet both.

The heart of the simulator is the cost of one iteration and the three kinds of iteration a GPU can run:

```python
def iter_time(decode_reqs, prefill_tokens, extra_flops=0.0):
    kv = sum(r['ctx'] for r in decode_reqs) * KV_TOK          # every running sequence's cache is read
    toks = len(decode_reqs) + prefill_tokens
    return max((W_BYTES + kv) / HBM, (2 * P * toks + extra_flops) / PEAK) + OVH
```

A prefill-first GPU runs a whole-prompt iteration whenever prompts are waiting, so everyone decoding waits; a chunked GPU adds up to 512 prompt tokens to every decode iteration; a disaggregated prefill GPU only prefills, then schedules a hand-off event $$T \times 128\ \text{KiB} / 450\ \text{GB/s}$$ later, when the request joins the least-loaded decode GPU.

<figure class="fig"><svg viewBox="0 0 760 340" role="img" aria-label="Simulated share of requests meeting the SLO as load rises on 4 GPUs. Chat workload with a tight SLO: chunked prefill keeps 90% up to 28 requests per second, 2P+2D up to 26, prefill-first 16. Long prompts with a strict SLO: 2P+2D reaches 3 requests per second, chunked and 3P+1D 2, prefill-first 1."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-note" x="205.0" y="24.0" text-anchor="middle">chat, tight SLO (0.5 s, 15 ms)</text><line class="grid" x1="60.0" y1="250.0" x2="350.0" y2="250.0"/><text class="t-tick" x="54.0" y="254.0" text-anchor="end">0%</text><line class="grid" x1="60.0" y1="150.0" x2="350.0" y2="150.0"/><text class="t-tick" x="54.0" y="154.0" text-anchor="end">50%</text><line class="base-line" x1="60.0" y1="70.0" x2="350.0" y2="70.0"/><text class="t-tick" x="54.0" y="74.0" text-anchor="end">90%</text><line class="grid" x1="60.0" y1="50.0" x2="350.0" y2="50.0"/><text class="t-tick" x="54.0" y="54.0" text-anchor="end">100%</text><text class="t-tick" x="92.2" y="268.0" text-anchor="middle">8</text><text class="t-tick" x="135.2" y="268.0" text-anchor="middle">16</text><text class="t-tick" x="178.1" y="268.0" text-anchor="middle">24</text><text class="t-tick" x="221.1" y="268.0" text-anchor="middle">32</text><text class="t-tick" x="264.1" y="268.0" text-anchor="middle">40</text><text class="t-tick" x="307.0" y="268.0" text-anchor="middle">48</text><text class="t-tick" x="350.0" y="268.0" text-anchor="middle">56</text><text class="t-tick" x="205.0" y="286.0" text-anchor="middle">requests per second;  goodput (90% met):</text><polyline class="l3" points="60.0,50.0 70.7,50.0 81.5,50.0 92.2,50.0 103.0,50.0 113.7,50.0 124.4,50.2 135.2,56.7 145.9,89.5 156.7,150.2 167.4,213.1 178.1,237.5 188.9,244.2 199.6,246.3 210.4,247.5 221.1,248.5 231.9,250.0 242.6,250.0 253.3,250.0 264.1,250.0 274.8,250.0 285.6,250.0 296.3,250.0 307.0,250.0 317.8,250.0 328.5,250.0 339.3,250.0 350.0,250.0"/><rect class="s3" x="60.0" y="297.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="76.0" y="306.0" text-anchor="start">prefill first: 16 req/s</text><polyline class="l1" points="60.0,50.0 70.7,50.0 81.5,50.0 92.2,50.0 103.0,50.0 113.7,50.0 124.4,50.0 135.2,50.0 145.9,50.0 156.7,50.0 167.4,50.0 178.1,50.0 188.9,50.8 199.6,56.1 210.4,75.7 221.1,113.1 231.9,169.1 242.6,211.6 253.3,235.3 264.1,242.6 274.8,244.6 285.6,244.9 296.3,246.0 307.0,247.4 317.8,248.6 328.5,249.9 339.3,250.0 350.0,250.0"/><rect class="s1" x="210.0" y="297.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="226.0" y="306.0" text-anchor="start">chunked: 28 req/s</text><polyline class="l2" points="60.0,50.0 70.7,50.0 81.5,50.0 92.2,50.0 103.0,50.0 113.7,50.0 124.4,50.0 135.2,50.0 145.9,50.0 156.7,50.1 167.4,50.4 178.1,52.0 188.9,65.8 199.6,109.5 210.4,199.3 221.1,239.9 231.9,243.2 242.6,244.6 253.3,246.4 264.1,247.8 274.8,248.3 285.6,248.5 296.3,248.7 307.0,248.8 317.8,248.9 328.5,248.9 339.3,249.0 350.0,249.1"/><rect class="s2" x="60.0" y="315.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="76.0" y="324.0" text-anchor="start">2P + 2D: 26 req/s</text><polyline class="l4" points="60.0,50.0 70.7,50.0 81.5,50.0 92.2,50.0 103.0,50.0 113.7,50.0 124.4,68.9 135.2,159.7 145.9,217.7 156.7,243.8 167.4,246.2 178.1,247.3 188.9,247.7 199.6,248.1 210.4,248.5 221.1,248.8 231.9,249.0 242.6,249.2 253.3,249.4 264.1,249.5 274.8,249.6 285.6,249.6 296.3,249.6 307.0,249.7 317.8,249.7 328.5,249.7 339.3,249.7 350.0,249.7"/><rect class="s4" x="210.0" y="315.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="226.0" y="324.0" text-anchor="start">3P + 1D: 14 req/s</text><text class="t-note" x="565.0" y="24.0" text-anchor="middle">long prompts, strict SLO (1 s, 12 ms)</text><line class="grid" x1="420.0" y1="250.0" x2="710.0" y2="250.0"/><line class="grid" x1="420.0" y1="150.0" x2="710.0" y2="150.0"/><line class="base-line" x1="420.0" y1="70.0" x2="710.0" y2="70.0"/><text class="t-tick" x="414.0" y="74.0" text-anchor="end">90%</text><line class="grid" x1="420.0" y1="50.0" x2="710.0" y2="50.0"/><text class="t-tick" x="420.0" y="268.0" text-anchor="middle">1</text><text class="t-tick" x="464.6" y="268.0" text-anchor="middle">3</text><text class="t-tick" x="509.2" y="268.0" text-anchor="middle">5</text><text class="t-tick" x="553.8" y="268.0" text-anchor="middle">7</text><text class="t-tick" x="598.5" y="268.0" text-anchor="middle">9</text><text class="t-tick" x="643.1" y="268.0" text-anchor="middle">11</text><text class="t-tick" x="687.7" y="268.0" text-anchor="middle">13</text><text class="t-tick" x="565.0" y="286.0" text-anchor="middle">requests per second;  goodput (90% met):</text><polyline class="l3" points="420.0,51.8 442.3,77.2 464.6,148.3 486.9,200.8 509.2,232.2 531.5,246.9 553.8,249.8 576.2,250.0 598.5,250.0 620.8,250.0 643.1,250.0 665.4,250.0 687.7,250.0 710.0,250.0"/><rect class="s3" x="420.0" y="297.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="436.0" y="306.0" text-anchor="start">prefill first: 1 req/s</text><polyline class="l1" points="420.0,50.0 442.3,56.0 464.6,77.8 486.9,139.5 509.2,192.4 531.5,227.9 553.8,243.8 576.2,249.2 598.5,250.0 620.8,250.0 643.1,250.0 665.4,250.0 687.7,250.0 710.0,250.0"/><rect class="s1" x="570.0" y="297.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="586.0" y="306.0" text-anchor="start">chunked: 2 req/s</text><polyline class="l2" points="420.0,50.0 442.3,51.7 464.6,67.8 486.9,160.5 509.2,242.8 531.5,245.2 553.8,246.7 576.2,247.7 598.5,248.7 620.8,249.0 643.1,249.1 665.4,249.5 687.7,249.5 710.0,249.5"/><rect class="s2" x="420.0" y="315.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="436.0" y="324.0" text-anchor="start">2P + 2D: 3 req/s</text><polyline class="l4" points="420.0,50.0 442.3,50.9 464.6,86.7 486.9,160.1 509.2,219.8 531.5,243.3 553.8,249.3 576.2,249.8 598.5,250.0 620.8,250.0 643.1,250.0 665.4,250.0 687.7,250.0 710.0,250.0"/><rect class="s4" x="570.0" y="315.0" width="10.0" height="10.0" rx="2"/><text class="t-tick" x="586.0" y="324.0" text-anchor="start">3P + 1D: 2 req/s</text></svg><figcaption>Simulated share of requests meeting the SLO as load grows, on 4 GPUs. Left: chat workload, tight SLO. Right: long-prompt workload, strict SLO. Goodput is where a curve crosses 90%.</figcaption></figure>

[![Terminal output of disagg_sim.py: for the chat and long workloads, the goodput of each layout under the loose, tight and strict SLOs, with p90 TTFT, p90 TPOT, p99 gap between tokens and SLO attainment at several request rates](https://ishwarj.com/img/multigpu/disagg_sim-run.png/disagg_sim-run.png)](/img/multigpu/disagg_sim-run.png)

| Goodput, requests/s on 4 GPUs (simulated) | Chat, loose | Chat, tight | Chat, strict | Long, loose | Long, strict |
|---|---|---|---|---|---|
| Colocated, prefill first | 30 | 16 | 10 | 4 | 1 |
| Colocated, chunked prefill | **48** | **28** | 18 | **6** | 2 |
| Disaggregated 1P + 3D | 12 | 10 | 12 | 1 | 1 |
| Disaggregated 2P + 2D | 28 | 26 | **20** | 3 | **3** |
| Disaggregated 3P + 1D | 26 | 14 | 8 | 5 | 2 |

(No layout meets the tight SLO on the long workload: a 12,000-token prompt alone takes about half a second to prefill on one GPU.)

What the simulation says, honestly:

- **Disaggregation removes the stalls.** In the chat workload at 16 requests per second, prefill-first has a 99th-percentile gap between tokens of 99 ms (worst 236 ms); chunked prefill 19 ms; 2P + 2D 12 ms. On long prompts, prefill-first freezes some users for up to 1.4 seconds.
- **But on a handful of GPUs, chunked prefill usually wins on goodput.** With only 4 GPUs, the split must be 1+3, 2+2 or 3+1, and every ratio wastes some capacity. Chunked prefill uses all four GPUs for whatever work exists. Under the loose and tight SLOs it serves the most requests on both workloads.
- **Disaggregation wins when the time-per-token target is strict.** Under the strict 12 ms TPOT, 2P + 2D serves 20 chat requests per second against 18, and 3 long-prompt requests against 2, because chunked steps that carry prompt slices are slower than pure decode steps.
- **The ratio is everything.** The same four GPUs serve 12, 28 or 26 chat requests per second (loose SLO) depending on the split. Production systems pick and adjust the ratio continuously; DistServe searches for it automatically.

The vLLM documentation states the same conclusion in one line:

> [!PAPER] vLLM documentation · Disaggregated Prefilling (experimental) · Why disaggregated prefilling?
> [![vLLM docs: Why disaggregated prefilling? Two main reasons. Tuning time-to-first-token and inter-token-latency separately: disaggregated prefilling puts prefill and decode phase of LLM inference inside different vLLM instances, which gives you the flexibility to assign different parallel strategies (e.g. tp and pp) to tune TTFT without affecting ITL, or to tune ITL without affecting TTFT. Controlling tail ITL: without disaggregated prefilling, vLLM may insert some prefill jobs during the decoding of one request, which results in higher tail latency; chunked prefill with a proper chunk size also can achieve the same goal, but in practice it is hard to figure out the correct chunk size value. Note: Disaggregated prefill DOES NOT improve throughput.](https://ishwarj.com/img/multigpu/vllm-disagg-doc.pngvllm-disagg-doc.png)](/img/multigpu/vllm-disagg-doc.png)
>
> **Context:** the feature page of vLLM's disaggregated prefill, still marked experimental.
>
> **What it says:** the two reasons to disaggregate are tuning TTFT and inter-token latency separately (including different parallelism per phase) and controlling the tail of inter-token latency. It does not raise throughput.
>
> [Read the page](https://docs.vllm.ai/en/latest/features/disagg_prefill/)

So disaggregation is worth it when: prompts are long; the per-token latency target is strict; the deployment is large enough to choose the prefill-to-decode ratio finely (DeepSeek runs thousands of GPUs); the two phases benefit from different parallel layouts (DeepSeek's EP32 prefill against EP144 or EP320 decode); and there is fast networking for the KV cache. For one or two servers of a mid-sized dense model, chunked prefill on every GPU is the simpler and often better choice.

## 8. Real systems today

Everything above is available as flags in the open-source engines. Here is how each idea maps to them, checked against the current documentation (11 October 2026). Flags change between releases, so check the docs for your version.

### vLLM

The vLLM docs give the same rule of thumb this article arrived at:

> [!PAPER] vLLM documentation · Parallelism and Scaling
> [![vLLM docs: To choose a distributed inference strategy for a single-model replica: Single GPU (no distributed inference) if the model fits on a single GPU. Single-node multi-GPU using tensor parallel inference if the model is too large for a single GPU but fits on a single node with multiple GPUs, for example tensor_parallel_size=4 on a node with 4 GPUs. Multi-node multi-GPU using tensor parallel and pipeline parallel inference if the model is too large for a single node: set tensor_parallel_size to the number of GPUs per node and pipeline_parallel_size to the number of nodes, for example 8 and 2 for 2 nodes with 8 GPUs. Edge case: if the GPUs on the node do not have NVLINK interconnect (e.g. L40S), leverage pipeline parallelism instead of tensor parallelism for higher throughput and lower communication overhead.](https://ishwarj.com/img/multigpu/vllm-parallelism-doc.pngparallelism-doc.png)](/img/multigpu/vllm-parallelism-doc.png)
>
> **Context:** the guide for choosing a layout for one copy of a model.
>
> **What it says:** one GPU if it fits; tensor parallel inside a node; tensor parallel inside nodes and pipeline parallel across them; and pipeline instead of tensor parallel on GPUs without NVLink.
>
> **Why it matters:** each rule is one of our results. TP needs fast links (Section 4); PP sends little across slow links (Section 5).
>
> [Read the page](https://docs.vllm.ai/en/latest/serving/parallelism_scaling/)

| Idea | vLLM flag | Note |
|---|---|---|
| Tensor parallel | `--tensor-parallel-size 8` | Inside one NVLink node |
| Pipeline parallel | `--pipeline-parallel-size 2` | Across nodes, with TP inside each |
| Data parallel (copies) | `--data-parallel-size 8` | With EP: attention copied, experts spread |
| Expert parallel | `--enable-expert-parallel` | EP size = TP size x DP size |
| All-to-all kernels | `--all2all-backend deepep_low_latency` | Also `deepep_high_throughput`, `allgather_reducescatter` (default) |
| Expert load balancing | `--enable-eplb`, `--eplb-config '{...}'` | Redundant experts, as in Section 6 |
| Disaggregated prefill | `--kv-transfer-config '{"kv_connector":"NixlConnector","kv_role":"kv_both"}'` | Connectors include NIXL, Mooncake, LMCache; marked experimental |

Typical launches, from the documentation:

```bash
# Llama 3.1 70B on one 8-GPU node
vllm serve meta-llama/Llama-3.1-70B-Instruct --tensor-parallel-size 8

# A model too big for one node: TP inside each of 2 nodes, PP across them (after joining the nodes as the docs describe)
vllm serve <model> --tensor-parallel-size 8 --pipeline-parallel-size 2

# DeepSeek-V3 on one node: attention copied 8 ways, experts spread over 8 GPUs
vllm serve deepseek-ai/DeepSeek-V3-0324 --tensor-parallel-size 1 --data-parallel-size 8 --enable-expert-parallel
```

The expert-parallel page lists the all-to-all backends, split by the two phases exactly as DeepSeek splits them:

[![vLLM docs: vLLM provides multiple communication backends for EP, selected with --all2all-backend: allgather_reducescatter, the default, standard all2all using allgather and reducescatter primitives, general purpose; deepep_high_throughput for multi-node prefill, grouped GEMM with continuous layout, for prefill-dominated high-throughput workloads; deepep_low_latency for multi-node decode, CUDA graph support and masked layout, for decode-dominated low-latency workloads.](https://ishwarj.com/img/multigpu/vllm-ep-doc.pnggpu/vllm-ep-doc.png)](/img/multigpu/vllm-ep-doc.png)

### SGLang

| Idea | SGLang flag | Note |
|---|---|---|
| Tensor parallel | `--tp-size` (or `--tensor-parallel-size`) | |
| Pipeline parallel | `--pp-size` | |
| Data parallel copies | `--dp-size` | |
| Data-parallel attention | `--attn-dp-size` | Replaces the older `--enable-dp-attention`, now deprecated |
| Expert parallel | `--ep-size` | |
| All-to-all kernels | `--moe-a2a-backend deepep` | Also mooncake, nixl, flashinfer and others |
| Expert load balancing | `--enable-eplb` | |
| Disaggregation | `--disaggregation-mode prefill` or `decode` | Transfer with `--disaggregation-transfer-backend mooncake` (default) or `nixl` |

SGLang's PD disaggregation page explains the motivation in the terms of Section 7, including a problem specific to data-parallel attention:

[![SGLang docs: Large Language Model inference comprises two distinct phases: Prefill and Decode. The Prefill phase is computation-intensive, processing the entire input sequence, while the Decode phase is memory-intensive, managing the KV cache for token generation. Traditionally these phases are handled within a unified engine, where combined scheduling of prefill and decode batches introduces inefficiencies. Issues with unified scheduling: 1. Prefill interruption: incoming prefill batches frequently interrupt ongoing decode batches, causing substantial delays in token generation. 2. DP attention imbalance: in data-parallel attention, one DP worker may process a prefill batch while another handles a decode batch simultaneously, leading to increased decode latency. PD Disaggregation resolves these by separating the two stages, enabling tailored optimizations for each. Currently, we support Mooncake and NIXL as the transfer engine.](https://ishwarj.com/img/multigpu/sglang-pd-doc.pngu/sglang-pd-doc.png)](/img/multigpu/sglang-pd-doc.png)

A single-node disaggregated setup from that page (Llama 3.1 8B, one GPU each, Mooncake transfer) is three commands:

```bash
python -m sglang.launch_server --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode prefill --port 30000 --disaggregation-ib-device mlx5_roce0
python -m sglang.launch_server --model-path meta-llama/Llama-3.1-8B-Instruct \
  --disaggregation-mode decode --port 30001 --base-gpu-id 1 --disaggregation-ib-device mlx5_roce0
python -m sglang_router.launch_router --pd-disaggregation \
  --prefill http://127.0.0.1:30000 --decode http://127.0.0.1:30001 --host 0.0.0.0 --port 8000
```

The router receives each request, sends it to a prefill server, and has the decode server pick up the KV cache over RDMA (the `mlx5_roce0` device is the RDMA network card). The page's DeepSeek-V3 example uses two prefill nodes with `--tp-size 16 --attn-dp-size 8 --moe-a2a-backend deepep`: attention in data-parallel groups, experts over all 16 GPUs with DeepEP.

### The orchestration layer: Dynamo and llm-d

Running disaggregation and wide expert parallelism in production needs more than one engine process: routers that know where each KV cache lives, a way to move KV between machines, and autoscaling of the prefill and decode pools separately. Two open-source projects package this around the engines:

- **[NVIDIA Dynamo](https://github.com/ai-dynamo/dynamo)** describes itself as "the open-source, datacenter-scale inference stack". It runs on top of SGLang, TensorRT-LLM or vLLM ("it doesn't replace" them) and adds disaggregated prefill and decode pools that scale independently, KV-aware routing ("based on worker load and KV cache overlap"), and a KV block manager that offloads cache from GPU to CPU, SSD and remote storage. Its transfer library, [NIXL](https://github.com/ai-dynamo/nixl) (NVIDIA Inference Xfer Library), is also one of vLLM's KV connectors and SGLang's transfer backends. Latest release at the time of writing: v1.5.1, 7 October 2026.
- **[llm-d](https://github.com/llm-d/llm-d)**, a Cloud Native Computing Foundation sandbox project started by Red Hat, Google Cloud, IBM Research, CoreWeave and NVIDIA, is "a high-performance distributed inference serving stack optimized for production deployments on Kubernetes". Its documented ["well-lit paths"](https://llm-d.ai/docs/well-lit-paths) include prefix-cache-aware routing, prefill/decode disaggregation, and "wide expert parallelism" for large MoE models. Latest release: v0.10.0, 29 September 2026.

Both build on vLLM (and in Dynamo's case SGLang and TensorRT-LLM as well); neither replaces the parallelism inside an engine. They decide which engine instance a request goes to and where its KV cache moves, the cluster-level version of the routing in [Part 6](https://ishwarj.com/writings/llm-inference-6-serving-in-production/).

## 9. Choosing a layout

### A decision guide

<figure class="fig"><svg viewBox="0 0 760 352" role="img" aria-label="Decision guide: one GPU per replica if it fits; otherwise tensor parallel inside a node; tensor plus pipeline parallel across nodes; expert parallel for mixture-of-experts; disaggregation for long prompts with strict per-token latency at scale."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><rect class="box" x="150.0" y="4.0" width="460.0" height="30.0" rx="6"/><text class="t-note" x="380.0" y="24.0" text-anchor="middle">Do weights + KV for your traffic fit on one GPU?</text><line class="edge" x1="300.0" y1="34.0" x2="160.0" y2="70.0" marker-end="url(#ah7)"/><line class="edge" x1="460.0" y1="34.0" x2="600.0" y2="70.0" marker-end="url(#ah7)"/><rect class="box-1" x="20.0" y="72.0" width="280.0" height="52.0" rx="6"/><text class="t-note" x="160.0" y="94.0" text-anchor="middle">Yes: one GPU per replica</text><text class="t-tick" x="160.0" y="114.0" text-anchor="middle">add replicas for more load (Part 6)</text><rect class="box" x="460.0" y="72.0" width="280.0" height="30.0" rx="6"/><text class="t-note" x="600.0" y="92.0" text-anchor="middle">No: does it fit on one node?</text><line class="edge" x1="560.0" y1="102.0" x2="470.0" y2="140.0" marker-end="url(#ah7)"/><line class="edge" x1="640.0" y1="102.0" x2="690.0" y2="140.0" marker-end="url(#ah7)"/><rect class="box-1" x="320.0" y="142.0" width="300.0" height="52.0" rx="6"/><text class="t-note" x="470.0" y="164.0" text-anchor="middle">Yes: tensor parallel inside the node</text><text class="t-tick" x="470.0" y="184.0" text-anchor="middle">compare TP sizes by KV room and latency</text><rect class="box-2" x="630.0" y="142.0" width="120.0" height="52.0" rx="6"/><text class="t-note" x="690.0" y="164.0" text-anchor="middle">No: TP in node</text><text class="t-tick" x="690.0" y="184.0" text-anchor="middle">+ PP across</text><rect class="box-3" x="20.0" y="222.0" width="720.0" height="40.0" rx="6"/><text class="t-note" x="380.0" y="247.0" text-anchor="middle">Mixture of experts? Spread the experts with EP; run attention data-parallel.</text><rect class="box-3" x="20.0" y="274.0" width="720.0" height="40.0" rx="6"/><text class="t-note" x="380.0" y="299.0" text-anchor="middle">Long prompts, strict time per token, many GPUs? Split prefill and decode onto separate GPUs.</text><text class="t-tick" x="380.0" y="340.0" text-anchor="middle">Then measure: the right layout is the one that meets your SLO at the lowest cost per token.</text></svg><figcaption>A first decision guide. Every branch is a rule from Sections 1 to 7; the last step is always to measure.</figcaption></figure>

### Worked example 1: Llama 3.1 8B

Weights 16 GB, 128 KiB of KV per token. One H100 holds the model plus $$(72 - 16)\ \text{GB} / 128\ \text{KiB} \approx 427{,}000$$ tokens of KV cache: 52 conversations of 8,192 tokens. The decode floor is 4.8 ms per step on one GPU. **Use one GPU per copy, and add copies for more traffic.** Tensor parallelism would only add all-reduces to a model that already fits; at TP=2 the step floor halves to 2.4 ms, which is worth it only if a per-token latency target cannot be met otherwise. Disaggregation, by our simulation, does not raise goodput at this size unless the per-token target is very strict. Everything else about scaling this model is in [Part 6](https://ishwarj.com/writings/llm-inference-6-serving-in-production/).

### Worked example 2: Llama 3.1 70B on one 8-GPU node

The weights (141 GB) need at least two GPUs, and realistically more to leave room for KV. With 8 GPUs, three layouts are possible: one copy at TP=8, two copies at TP=4, or four copies at TP=2. The model in `layout_model.py` compares them for 4,096-token conversations:

<figure class="fig"><svg viewBox="0 0 760 298" role="img" aria-label="Modelled latency against throughput for Llama 3.1 70B on one 8-GPU node. One TP=8 replica gives lower latency and, because it has room for more sequences, the highest throughput; two TP=4 replicas give more throughput at equal batch per replica but run out of KV space at 109 sequences each."><defs><marker id="ah7" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker></defs><line class="grid" x1="90.0" y1="250.0" x2="90.0" y2="30.0"/><text class="t-tick" x="90.0" y="268.0" text-anchor="middle">0</text><line class="grid" x1="186.8" y1="250.0" x2="186.8" y2="30.0"/><text class="t-tick" x="186.8" y="268.0" text-anchor="middle">2,000</text><line class="grid" x1="283.5" y1="250.0" x2="283.5" y2="30.0"/><text class="t-tick" x="283.5" y="268.0" text-anchor="middle">4,000</text><line class="grid" x1="380.3" y1="250.0" x2="380.3" y2="30.0"/><text class="t-tick" x="380.3" y="268.0" text-anchor="middle">6,000</text><line class="grid" x1="477.1" y1="250.0" x2="477.1" y2="30.0"/><text class="t-tick" x="477.1" y="268.0" text-anchor="middle">8,000</text><line class="grid" x1="573.8" y1="250.0" x2="573.8" y2="30.0"/><text class="t-tick" x="573.8" y="268.0" text-anchor="middle">10,000</text><line class="grid" x1="670.6" y1="250.0" x2="670.6" y2="30.0"/><text class="t-tick" x="670.6" y="268.0" text-anchor="middle">12,000</text><text class="t-tick" x="82.0" y="254.0" text-anchor="end">0 ms</text><text class="t-tick" x="82.0" y="210.7" text-anchor="end">5 ms</text><text class="t-tick" x="82.0" y="167.3" text-anchor="end">10 ms</text><text class="t-tick" x="82.0" y="124.0" text-anchor="end">15 ms</text><text class="t-tick" x="82.0" y="80.6" text-anchor="end">20 ms</text><line class="axis" x1="90.0" y1="250.0" x2="690.0" y2="250.0"/><text class="t-tick" x="390.0" y="286.0" text-anchor="middle">tokens per second for the whole node</text><polyline class="l1" points="97.0,190.0 103.9,189.4 117.2,188.4 142.7,186.3 188.9,182.1 266.1,173.8 378.8,157.1 514.8,123.6 645.6,56.7"/><g class="mark"><title>TP=8 x 1, batch 1 per replica: 6.9 ms, 144 tok/s</title><circle class="s1" cx="97.0" cy="190.0" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 2 per replica: 7.0 ms, 286 tok/s</title><circle class="s1" cx="103.9" cy="189.4" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 4 per replica: 7.1 ms, 563 tok/s</title><circle class="s1" cx="117.2" cy="188.4" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 8 per replica: 7.3 ms, 1,089 tok/s</title><circle class="s1" cx="142.7" cy="186.3" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 16 per replica: 7.8 ms, 2,044 tok/s</title><circle class="s1" cx="188.9" cy="182.1" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 32 per replica: 8.8 ms, 3,639 tok/s</title><circle class="s1" cx="266.1" cy="173.8" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 64 per replica: 10.7 ms, 5,969 tok/s</title><circle class="s1" cx="378.8" cy="157.1" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 128 per replica: 14.6 ms, 8,779 tok/s</title><circle class="s1" cx="514.8" cy="123.6" r="3.5"/></g><g class="mark"><title>TP=8 x 1, batch 256 per replica: 22.3 ms, 11,482 tok/s</title><circle class="s1" cx="645.6" cy="56.7" r="3.5"/></g><text class="t-tick" x="639.6" y="46.7" text-anchor="end">TP=8 x 1 (max 324/replica)</text><polyline class="l2" points="97.9,143.9 105.7,143.0 120.8,141.1 149.5,137.3 201.6,129.7 288.3,114.6 414.3,84.4"/><g class="mark"><title>TP=4 x 2, batch 1 per replica: 12.2 ms, 163 tok/s</title><circle class="s2" cx="97.9" cy="143.9" r="3.5"/></g><g class="mark"><title>TP=4 x 2, batch 2 per replica: 12.3 ms, 324 tok/s</title><circle class="s2" cx="105.7" cy="143.0" r="3.5"/></g><g class="mark"><title>TP=4 x 2, batch 4 per replica: 12.6 ms, 637 tok/s</title><circle class="s2" cx="120.8" cy="141.1" r="3.5"/></g><g class="mark"><title>TP=4 x 2, batch 8 per replica: 13.0 ms, 1,231 tok/s</title><circle class="s2" cx="149.5" cy="137.3" r="3.5"/></g><g class="mark"><title>TP=4 x 2, batch 16 per replica: 13.9 ms, 2,307 tok/s</title><circle class="s2" cx="201.6" cy="129.7" r="3.5"/></g><g class="mark"><title>TP=4 x 2, batch 32 per replica: 15.6 ms, 4,099 tok/s</title><circle class="s2" cx="288.3" cy="114.6" r="3.5"/></g><g class="mark"><title>TP=4 x 2, batch 64 per replica: 19.1 ms, 6,702 tok/s</title><circle class="s2" cx="414.3" cy="84.4" r="3.5"/></g><text class="t-tick" x="408.3" y="74.4" text-anchor="end">TP=4 x 2 (max 109/replica)</text><polyline class="l3" points="98.5,51.8 106.8,50.0"/><g class="mark"><title>TP=2 x 4, batch 1 per replica: 22.9 ms, 175 tok/s</title><circle class="s3" cx="98.5" cy="51.8" r="3.5"/></g><g class="mark"><title>TP=2 x 4, batch 2 per replica: 23.1 ms, 347 tok/s</title><circle class="s3" cx="106.8" cy="50.0" r="3.5"/></g><text class="t-tick" x="116.8" y="54.0" text-anchor="start">TP=2 x 4 (max 2/replica)</text></svg><figcaption>Modelled Llama 3.1 70B on one 8-GPU H100 node: time per token against tokens per second for the whole node, as the batch per copy grows. Each curve ends where that layout runs out of KV-cache memory.</figcaption></figure>

| Layout | KV room (4,096-token conversations) | Batch 1: ms per token | Batch 64 per copy: ms per token, node tokens/s | Most the node can do |
|---|---|---|---|---|
| TP=8, 1 copy | 324 | 6.9 | 10.7 ms, 5,969 (batch 128: 14.6 ms, 8,779) | 11,482 tokens/s at 22.3 ms (batch 256) |
| TP=4, 2 copies | 109 each, 218 total | 12.2 | 19.1 ms, 6,702 | 6,702 tokens/s at 19.1 ms (batch 64) |
| TP=2, 4 copies | 2 each | 22.9 | (does not fit) | 347 tokens/s |

**Worked numbers for TP=4.** Each copy has $$4\times72 = 288$$ GB usable; the weights take 141 GB, leaving 147 GB, which at $$320\ \text{KiB}\times4{,}096 = 1.25$$ GiB per conversation holds 109 conversations. For TP=8 the free memory is $$576 - 141 = 435$$ GB: **324** conversations, three times as many, because the weights are stored once instead of twice.

You may have heard "prefer more copies over wider tensor parallelism". For a 70B model on 80 GB GPUs this model says the opposite. At the same 128 sequences in flight on the node, one TP=8 copy makes **8,779 tokens per second at 14.6 ms per token**, while two TP=4 copies (64 each) make **6,702 at 19.1 ms**. Decode is memory-bound, and two copies read the 141 GB of weights twice per step where one copy reads them once. The extra all-reduces of TP=8 cost less than that second read. TP=8 also has three times the KV room, so it can go on to 11,482 tokens per second at batch 256, and it gives the lowest latency for a single user (6.9 against 12.2 ms). TP=2 barely fits the weights and is useless.

More copies do win in other conditions: when the weights are a small part of each step's memory traffic (long contexts, where the KV cache dominates), when the links are slow (no NVLink, so all-reduce is expensive), and for reasons a roofline ignores, such as isolating failures and scheduling requests independently. So the lesson is a method, not a rule: **count the KV room for each layout first, then compare latency and throughput at the batch sizes your traffic needs**, and confirm with a benchmark as in Part 6.

### Worked example 3: a large MoE across nodes

DeepSeek-V3 in FP8 needs 671 GB for weights: at least nine 80 GB GPUs, so more than one node. Its attention is small and its KV cache tiny (68.6 KiB per token), so copying attention is cheap; its experts are huge, so spreading them is the only option. The layouts from Section 6 follow:

- **Small deployment (2 nodes, 16 GPUs):** attention in data-parallel groups, experts spread over all 16 GPUs with EP, DeepEP for the all-to-all. This is SGLang's documented two-node example.
- **Large deployment (dozens of nodes):** separate prefill and decode units, with wider EP for decode (EP144 to EP320 in DeepSeek's case), redundant copies of hot experts, and two micro-batches to hide the all-to-all.

The all-to-all cost from Section 6 sets the limits. At about 11 ms of dispatch and combine per decode step at EP8 (more at wider EP), the communication must be overlapped with computation, and the network matters as much as the GPUs. That is why DeepSeek limits each token to 4 nodes, and why the decode units are so large: many GPUs reading their own experts in parallel is what makes a 37-billion-active-parameter step fast.

## 10. Summary

### The whole part, on one page

| Question | Answer | Where the number comes from |
|---|---|---|
| Why more than one GPU? | 70B needs 227 GB with KV for 32 x 8K conversations; 405B BF16 weights (812 GB) exceed one 8-GPU node | Arithmetic from published shapes |
| What does spreading buy? | Each GPU reads 1/p of the weights: 70B decode floor 21.1 ms on 2 GPUs, 5.3 ms on 8 | Arithmetic, H100 peak bandwidth |
| How fast are the wires? | HBM 3,350, NVLink 900, PCIe 128, one 400G NIC 50 GB/s | NVIDIA spec pages |
| What is a message's cost? | $$\alpha + n/\beta$$: small messages cost a fixed delay | Measured shape on the M5 Pro; NVIDIA publishes no alpha |
| Tensor parallelism | Columns then rows: 2 all-reduces per layer; exact (same 24 tokens, logits within $$5\times10^{-5}$$) | Measured: 2-process Qwen2.5-0.5B run |
| Its cost | 160 all-reduces per 70B step; 23% of a TP=8 step at batch 1 | Model, $$\alpha$$ assumed 5 $$\mu$$s |
| Ring all-reduce | Bytes $$\frac{2(p-1)}{p}n$$, but $$2(p-1)$$ steps; two-step algorithms cut latency 7x at TP=8 | Simulation and formula |
| Pipeline parallelism | Little traffic, no latency gain; with micro-batches 1.7x the throughput of TP=16 across two nodes | Model, 405B |
| Expert parallelism | 10 MB of all-to-all per token for DeepSeek-V3; real routing makes the busiest GPU 1.3x to 6.8x the average | DeepEP report checked by arithmetic; measured OLMoE routing |
| Fixing MoE imbalance | Place by load (1.81 to 1.12 at EP=8 on code); copy hot experts (6.75 to 1.67 at EP=64) | Simulation on measured routing |
| Disaggregation | Moves 2.5 GiB per 8K-token 70B prompt (54 ms on one NIC, under 1 ms exposed layer by layer); wins under strict TPOT and at scale, not by default | Arithmetic; simulation |

Return to where we started. A 70B model does not fit on one GPU, so it is cut into pieces that must talk. Inside a server, where talking is cheap, every layer is split (tensor parallelism) and the GPUs add up their partial results 160 times per token. Between servers, where talking is slow, the model is cut into stages (pipeline parallelism) that pass one message per step. Mixture-of-experts models send each token to the GPUs holding its experts (expert parallelism), and the real difficulty is that some experts are far more popular than others. Finally, the two phases of every request can live on different GPUs (disaggregation), at the price of moving the KV cache, which pays off for long prompts, strict per-token targets and large fleets. In every case the design follows one question: **how often must the GPUs talk, and how fast is the wire?**

### The series so far

| Part | Topic | The one idea |
|---|---|---|
| [1](https://ishwarj.com/writings/llm-inference-1-prefill-and-decode/) | Prefill and decode | Decode is limited by reading the weights, not by arithmetic |
| [2](https://ishwarj.com/writings/llm-inference-2-kv-cache/) | The KV cache | Saving keys and values avoids recomputation, and their size limits the batch |
| [3](https://ishwarj.com/writings/llm-inference-3-vllm/) | vLLM | Paged memory, continuous batching and chunked prefill keep one GPU busy |
| [4](https://ishwarj.com/writings/llm-inference-4-speculative-decoding/) | Speculative decoding | Checking several guessed tokens costs about as much as writing one |
| [5](https://ishwarj.com/writings/llm-inference-5-sglang-vs-vllm/) | SGLang and vLLM | Reuse the saved state of shared prompt beginnings |
| [6](https://ishwarj.com/writings/llm-inference-6-serving-in-production/) | Serving in production | Measure goodput against SLOs, plan capacity, route between copies |
| 7 | Beyond one GPU | Split the model where the wires are fast; the cost of talking decides the layout |

## Try it yourself

All the code is in [`code/multigpu`](https://github.com/ishwar6/ishwar-books/tree/main/code/multigpu). Nothing needs an NVIDIA GPU.

```bash
python code/multigpu/memory_math.py     # Section 1: memory and the decode floor
python code/multigpu/tp_demo.py         # Section 3: split MLP, 2-process Qwen2.5-0.5B, all-reduce timings
python code/multigpu/measure_mps.py     # Section 3: shard timings (needs an Apple GPU; edit dev for CUDA)
python code/multigpu/collectives.py     # Section 4: ring trace, collectives, cost tables, MoE bytes
python code/multigpu/layout_model.py    # Sections 3, 5 and 9: layouts for 70B and 405B
python code/multigpu/moe_routing.py     # Section 6: records OLMoE routing (14 GB download), then simulates EP
python code/multigpu/kv_transfer.py     # Section 7: KV transfer arithmetic
python code/multigpu/disagg_sim.py      # Section 7: colocated vs disaggregated goodput
```

If you have a machine with two or more NVIDIA GPUs, change the backend in `tp_demo.py` from `gloo` to `nccl` and put each rank's tensors on `cuda:<rank>`; the same code then runs real tensor parallelism over NVLink or PCIe, and the all-reduce timings in part C become your own $$\alpha$$ and $$\beta$$.

## References

**Papers**

1. M. Shoeybi et al. [*Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism*](https://arxiv.org/abs/1909.08053). arXiv 1909.08053, 2019.
2. Y. Huang et al. [*GPipe: Efficient Training of Giant Neural Networks using Pipeline Parallelism*](https://arxiv.org/abs/1811.06965). NeurIPS 2019.
3. R. Pope et al. [*Efficiently Scaling Transformer Inference*](https://arxiv.org/abs/2211.05102). MLSys 2023.
4. Llama Team, AI @ Meta. [*The Llama 3 Herd of Models*](https://arxiv.org/abs/2407.21783). 2024. Section 6, Inference.
5. DeepSeek-AI. [*DeepSeek-V3 Technical Report*](https://arxiv.org/abs/2412.19437). 2024. Sections 3.2.2 and 3.4.
6. N. Shazeer et al. [*Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer*](https://arxiv.org/abs/1701.06538). ICLR 2017.
7. D. Lepikhin et al. [*GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding*](https://arxiv.org/abs/2006.16668). 2020.
8. P. Patel et al. [*Splitwise: Efficient Generative LLM Inference Using Phase Splitting*](https://arxiv.org/abs/2311.18677). ISCA 2024.
9. Y. Zhong et al. [*DistServe: Disaggregating Prefill and Decoding for Goodput-optimized Large Language Model Serving*](https://arxiv.org/abs/2401.09670). OSDI 2024.
10. R. Qin et al. [*Mooncake: A KVCache-centric Disaggregated Architecture for LLM Serving*](https://arxiv.org/abs/2407.00079). 2024.
11. A. Agrawal et al. [*SARATHI: Efficient LLM Inference by Piggybacking Decodes with Chunked Prefills*](https://arxiv.org/abs/2308.16369). 2023.
12. N. Muennighoff et al. [*OLMoE: Open Mixture-of-Experts Language Models*](https://arxiv.org/abs/2409.02060). 2024. The model used in Section 6: [OLMoE-1B-7B-0924](https://huggingface.co/allenai/OLMoE-1B-7B-0924).

**Documentation, specifications and engineering posts**

13. NVIDIA. [H100](https://www.nvidia.com/en-us/data-center/h100/), [H200](https://www.nvidia.com/en-us/data-center/h200/), [NVLink and NVLink Switch](https://www.nvidia.com/en-us/data-center/nvlink/), [DGX H100 user guide](https://docs.nvidia.com/dgx/dgxh100-user-guide/introduction-to-dgxh100.html), [InfiniBand adapters (ConnectX-7)](https://www.nvidia.com/en-us/networking/infiniband-adapters/).
14. NVIDIA. [*3x Faster AllReduce with NVSwitch and TensorRT-LLM MultiShot*](https://developer.nvidia.com/blog/3x-faster-allreduce-with-nvswitch-and-tensorrt-llm-multishot/). Technical blog, November 2024.
15. NVIDIA. [nccl-tests performance notes](https://github.com/NVIDIA/nccl-tests/blob/master/doc/PERFORMANCE.md) (algorithm and bus bandwidth).
16. vLLM. [Parallelism and Scaling](https://docs.vllm.ai/en/latest/serving/parallelism_scaling/), [Expert Parallel Deployment](https://docs.vllm.ai/en/latest/serving/expert_parallel_deployment/), [Disaggregated Prefilling](https://docs.vllm.ai/en/latest/features/disagg_prefill/).
17. SGLang. [Server Arguments](https://docs.sglang.io/docs/advanced_features/server_arguments), [PD Disaggregation](https://docs.sglang.io/docs/advanced_features/pd_disaggregation).
18. DeepSeek. [DeepEP](https://github.com/deepseek-ai/DeepEP) (performance tables in the [v1.2.1 README](https://github.com/deepseek-ai/DeepEP/tree/v1.2.1)); [DeepSeek-V3/R1 inference system overview](https://github.com/deepseek-ai/open-infra-index/blob/main/202502OpenSourceWeek/day_6_one_more_thing_deepseekV3R1_inference_system_overview.md), February 2025.
19. [NVIDIA Dynamo](https://github.com/ai-dynamo/dynamo) and [documentation](https://docs.nvidia.com/dynamo/); [llm-d](https://github.com/llm-d/llm-d) and its [well-lit paths](https://llm-d.ai/docs/well-lit-paths).

**Companion results**

20. [Memory arithmetic](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/memory_math.json), [tensor-parallel demo](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/tp_demo.json), [shard timings](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/measure_mps.json), [collectives](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/collectives.json), [layouts](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/layout_model.json), [MoE routing](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/moe_routing.json), [KV transfer](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/kv_transfer.json), [disaggregation](https://github.com/ishwar6/ishwar-books/blob/main/code/multigpu/results/disagg_sim.json).

