Distributed inference
When one machine is not enough, the model is split over many GPUs, and the GPUs must wait for each other. Tensor parallelism waits twice in every layer, so it belongs inside one machine with fast links. Pipeline parallelism talks little and can cross machines. Mixture of experts models add expert parallelism, and big systems often run prefill and decode on separate GPUs. A specialist counts these waits, picks the layout, and plans for failures.
Author: bezzad
Contents
The problem: “16 GPUs must be faster than 8”
The shop now serves the 70B model in BF16 on one machine with 8 GPUs, connected with NVLink, using tensor parallelism of 8. It works, but a manager wants each answer to stream faster. A teammate says: “Let’s use two machines and tensor parallelism of 16. Twice the GPUs, so about twice as fast.”
After the change, each token is slower than before, and the cost doubled. Then business customers start sending long catalogs, and normal chats stutter on both machines.
The GPU memory lesson showed the basic ways to split a model. This lesson goes deeper: what the GPUs actually wait for, how big systems are laid out, and how they fail.
What tensor parallelism really costs
In the common layout (often called the Megatron style):
- Attention is split by heads. Our 70B model has 64 query heads and 8 KV heads. With 8 GPUs, each GPU runs 8 query heads and 1 KV head.
- The MLP is split by columns, then by rows. Each GPU does its part of the two big matrix multiplications.
- Two all-reduces per layer. After attention and after the MLP, the GPUs add up their partial results, so every GPU has the full answer. With 80 layers, that is 160 all-reduces in every decode step.
At decode time, each all-reduce carries little data: the batch size × the hidden size × 2 bytes. For 32 running chats and a hidden size of 8,192, that is about 512 KB. So the cost is mostly the fixed wait of each all-reduce, not the bandwidth.
A rough worksheet (the numbers are only for showing the shape; measure your own):
def decode_step_ms(weights_gb, gpus, mem_tb_s, waits, wait_us):
read = weights_gb / gpus / (mem_tb_s * 1000) * 1000 # ms
talk = waits * wait_us / 1000 # ms
return read + talk
# 70B in BF16: 140 GB of weights, 160 all-reduces per step
print(decode_step_ms(140, 8, 3.0, 160, 8)) # one machine: ~7.1 ms
print(decode_step_ms(140, 16, 3.0, 160, 40)) # two machines: ~9.3 ms
- More GPUs make the reading part smaller. Each GPU reads only its share of the weights.
- But the waits do not get smaller. There are still 160 of them, and between machines each one is many times slower than over NVLink.
- So past some point, more tensor parallelism makes tokens slower. It also needs bigger batches to use all GPUs well.
Limits you hit with tensor parallelism
- Heads must split evenly. The tensor parallel size must divide the number of attention heads. Most engines also want it to divide the KV heads, or they copy KV heads.
- KV heads stop shrinking. Our model has 8 KV heads. With tensor parallelism of 16, each KV head is stored on two GPUs. The KV cache per GPU stops getting smaller, so we gain little room for more chats.
- Stay inside one fast domain. Use tensor parallelism only between GPUs with fast links (NVLink inside a machine, or a larger NVLink system). Over PCIe or a normal network, expect poor scaling.
Pipeline parallelism across machines
If the model does not fit in one machine, split it by layers across machines, and keep tensor parallelism inside each machine. For two machines with 8 GPUs each: tensor parallelism 8, pipeline parallelism 2.
- Little traffic between machines. Only one hand-off per step at the border: about the same 512 KB, once, instead of 160 waits.
- No gain in latency for one request. A token must go through machine 1, then machine 2.
- Bubbles. While machine 2 works on a batch, machine 1 has nothing to do, unless the engine keeps several batches in flight (micro-batches). So pipeline parallelism needs enough traffic.
- Balance the stages. If one stage has more layers or more work, the other stage waits for it.
| Layout | Use it when | Watch out for |
|---|---|---|
| Replicas (data parallel) | The model fits on one GPU or one machine | Nothing shared; simplest; scale by traffic |
| Tensor parallel inside a machine | The model does not fit on one GPU, or you need lower latency | Waits in every layer; needs NVLink |
| Pipeline parallel across machines | The model does not fit on one machine | Bubbles; latency does not improve |
| Expert parallel | Mixture of experts models | All-to-all traffic; hot experts |
Expert parallelism for mixture of experts models
A mixture of experts (MoE) model has many expert MLPs in each layer, but each token uses only a few of them. The experts can be placed on different GPUs: this is expert parallelism.
- Two all-to-all steps per MoE layer. Each token is sent to the GPUs that hold its experts, and the results are sent back.
- Hot experts. If many tokens choose the same expert, its GPU becomes the slow one, and everyone waits. Engines balance this, for example by copying popular experts.
- A common pattern. Attention runs with data parallelism or tensor parallelism, and the expert layers run with expert parallelism across many GPUs. The exact options depend on the engine.
- Memory still follows the total size. All experts must be in GPU memory somewhere.
Separate prefill and decode
Prefill and decode want different things. Prefill is compute-bound and wants a fast first token. Decode is memory-bound and wants big batches and steady tokens. When a 90,000-token catalog runs its prefill on the same GPUs, every chat on them stutters.
- Each pool is tuned for its own job. Different parallel layouts, batch sizes, even GPU types.
- No more stutter from long prompts. Decode GPUs never run a big prefill.
- The cost: moving the KV cache. For our 70B model the cache is about 320 KB per token. A 4,000-token prompt is about 1.3 GB. Over a 400 Gb/s link (about 50 GB/s) that is about 26 ms, added to the time to first token.
- It is worth it at large scale. For a few replicas, chunked prefill on the same GPUs is often enough and much simpler.
Running it in production
- One GPU fails, the whole group stops. With tensor parallelism 8 and pipeline parallelism 2, one bad GPU takes down 16 GPUs. Keep spare capacity and fast health checks.
- Start time is long. Loading 140 GB of weights to many GPUs takes minutes. Autoscaling must start early, and fast weight loading matters.
- Placement matters. In Kubernetes, the GPUs of one tensor parallel group must be on one machine with NVLink. Pipeline stages need a fast network between them.
- Same version everywhere. All GPUs of a group must run the same engine, driver and communication library versions.
- Watch the waits. Profile the time spent in communication, not only GPU use. A GPU that waits for others still looks busy.
A config in the vLLM style (the exact flags and the multi-machine setup depend on the version):
# One machine, 8 GPUs with NVLink
vllm serve shop-assistant-70b --tensor-parallel-size 8
# Two machines: tensor parallel inside, pipeline parallel across
vllm serve shop-assistant-70b \
--tensor-parallel-size 8 \
--pipeline-parallel-size 2
Common mistakes
| Mistake | Result | Right way |
|---|---|---|
| Tensor parallelism across machines | 160 slow waits per step; slower tokens. | Tensor parallel inside a machine, pipeline parallel across. |
| “Twice the GPUs, twice the speed.” | Cost doubles, speed does not. | Count the waits; measure tokens per second per user. |
| Tensor parallel size above the KV head count | KV heads are copied; little extra room for chats. | Keep it at or below the KV heads, or plan for the copies. |
| Pipeline parallelism with little traffic | Bubbles; GPUs sit idle. | Use it only to fit the model, with enough requests in flight. |
| Separate prefill and decode for a small system | Hard to run, little gain. | Start with chunked prefill; split when scale needs it. |
| Looking only at GPU use | Waiting GPUs look busy. | Profile communication time. |
Summary in six lines
- Tensor parallelism waits twice in every layer: 160 waits per step for 80 layers.
- Keep tensor parallelism inside one fast NVLink domain; the KV heads limit how far it helps.
- Pipeline parallelism across machines talks little, but adds bubbles and does not lower latency.
- MoE models add expert parallelism, with all-to-all traffic and hot experts.
- Separate prefill and decode pools stop long prompts from hurting chats, at the cost of moving the KV cache.
- A bigger group fails together and starts slowly; plan spares, placement and versions.