Distributed serving, engines and hardware
- Production
- 45 min
- Lesson 5 of 7 in Inference engineering
- Last verified
Select text in the lesson first to keep it with your note. With the keyboard, move focus here and press the button: the note goes with the section you are reading.
In this module
On this page
Before you start
- Completed:Scheduling and decoding
You will be able to
- Choose between data, tensor, pipeline and expert parallelism for a model and an interconnect, and state the communication each one adds.
- Compute weights per GPU, the KV budget and the weights-only decode floor for a candidate layout, and decide whether it serves.
- Explain why a mixture-of-experts model costs compute by active parameters but memory and bandwidth by total parameters as the batch grows.
- Explain how one resident base model can serve many LoRA adapters, and size an adapter relative to the base.
- Choose an engine by stated criteria with dated facts, match a hardware attribute (capacity, bandwidth, compute, interconnect) to a workload, and place self-hosting against a hosted API.
The model has grown too big for one machine. Two plans: split each layer across machines side by side, or give each machine a few layers to run one after the other.
Either way the halves have to talk to each other. Ananya traces the cable between the two machines, then traces it again to be sure.
“Fast link or slow link,” she says. “Which one are we buying?”
Concept
When a model, plus its cache, does not fit on one GPU, or one GPU cannot meet the latency target, the work is split across several. Splitting always adds communication, and communication is slower than local memory. That is the link in the story. The rule that follows is the one to remember: use the smallest degree of parallelism that fits and meets the target, then scale out with replicas.
This lesson covers the four ways to split, the arithmetic for choosing a layout, why mixture-of-experts and multi-adapter serving change the memory picture, and how to choose among engines and hardware when both change every few months. The engine and hardware facts below are dated 2026-10-21; the principles outlast them.
Why It Matters
“How would you serve a 400-billion-parameter model?” and “why is adding GPUs not making it faster?” are standard questions, and the answer is a layout with numbers: weights per GPU, cache budget, step floor, and the wire. Wrong layouts waste money quietly. An eight-GPU tensor-parallel group spread across a slow network can be slower than four GPUs with 8-bit weights.
Mental Model
Splitting a model is like cutting a recipe across several kitchens. Cut each dish in half and cook both halves at once (tensor parallelismTensor parallelismSplitting each layer's weight matrices across several GPUs, so every GPU holds a slice of every layer and they exchange partial results at each layer. It lets a model too large for one GPU run, and it needs fast links between the GPUs.Open the glossary entry) and the cooks must swap notes after every step, so the kitchens must be next door. Give each kitchen a stage of the recipe (pipeline parallelismPipeline parallelismSplitting a model by layers, so different GPUs hold different whole layers and each passes its output to the next. It also lets a model too large for one GPU run, usually with less communication between GPUs than tensor parallelism needs.Open the glossary entry) and the notes cross only at the handoff, but a dish still passes through every kitchen in turn. Copy the whole kitchen (data parallelism) and no notes are needed, but each kitchen needs all the ingredients. A mixture-of-experts recipe has a hundred specialists and each dish needs only a few, but all hundred must be in the building.
The parallelism toolbox
| Strategy | What is split | Communication | Use when | Cost |
|---|---|---|---|---|
| Data parallel (replicas) | nothing; the whole model is copied | none per request, only a router | more requests per second | memory for each copy |
| Tensor parallel (TP) | each layer’s matrices across GPUs | all-reduces inside every layer, every token | the model does not fit, or lower latency from aggregate bandwidth | needs a fast interconnect; diminishing returns |
| Pipeline parallel (PP) | consecutive groups of layers on different GPUs | activations passed between stages | the model spans several nodes, or the interconnect is slow | pipeline bubbles; a token still crosses every stage |
| Expert parallel (EP) | the experts of a mixture-of-experts model | all-to-all routing of tokens to experts | serving a large mixture-of-experts model | load imbalance; network-heavy |
vLLM’s guidance (accessed 2026-10-21) matches the rule above: if the model fits on one GPU, distributed inference is probably unnecessary; if it fits on one node, use tensor parallelism across that node’s GPUs; if not, combine tensor parallelism within each node and pipeline parallelism across nodes. It says efficient tensor parallelism across nodes needs fast links such as InfiniBand, and, in its discussion of uneven GPU splits, that GPUs without NVLink (its example is the L40S) can use pipeline parallelism instead of tensor parallelism for higher throughput. For mixture-of-experts it supports data-parallel attention with expert or tensor-parallel expert layers.
What tensor parallelism costs
Megatron-LM (Shoeybi et al., 2019) introduced the intra-layer split most engines use: each layer’s matrices are divided across GPUs so each computes part of the product. In the standard split, a forward pass needs two all-reduces per layer, one after attention and one after the feed-forward block. For a 126-layer model that is 252 all-reduces for every generated token.
Each all-reduce sums a small activation: at hidden size 16,384 and a batch of 32 in 16-bit values, about 1 MB. These messages are small, so cost is dominated by latency, not bandwidth. If each took about 10 microseconds (an invented figure for illustration), 252 of them add about 2.5 ms to every step, on top of a weights floor that, as computed below, is about 15 ms. That is roughly a sixth. Over a slower network the same count costs far more, which is why TP belongs inside a node’s high-speed links.
| Quantity (illustrative) | Value |
|---|---|
| Layers | 126 |
| All-reduces per layer | 2 |
| All-reduces per generated token | 252 |
| Assumed time per all-reduce | 10 µs |
| Added time per step | about 2.5 ms |
Pipeline parallelism sends activations only between stages, so it tolerates slower links. It does not lower the time for a single token to cross the model, because the token still passes every stage in turn. It helps throughput and capacity, at the price of pipeline bubbles when stages sit idle.
A worked layout: a 405-billion-parameter dense model
Llama 3 405B (accessed 2026-10-21): 126 layers, 8 KV heads, head dimension 128. Cache per token in 16-bit values: bytes (504 KiB). Weights: parameters, so 810 GB in BF16 or 405 GB in 8-bit. Assume 90 percent usable memory and 4 GB of overhead per GPU (illustrative).
| Layout | Memory | Weights | KV budget | Sequences of 8,192 tokens | Weights-only step floor |
|---|---|---|---|---|---|
| 8 × H100 (80 GB), BF16 | 640 GB | 810 GB | none: does not fit | 0 | n/a |
| 8 × H100, 8-bit weights, TP = 8 | 640 GB | 405 GB | about 32 | ||
| 8 × H200 (141 GB), BF16, TP = 8 | 1,128 GB | 810 GB | about 40 |
H100 figures are 80 GB and 3.35 TB/s; H200 figures are 141 GB and 4.8 TB/s, from NVIDIA’s pages, with the step floor using aggregate bandwidth and ignoring the cache read and communication from earlier lessons. The BF16 model does not fit on one H100 node at all, so the choice is between 8-bit weights and a larger-memory GPU. Neither answer is free: the first costs quantization risk (lesson 3), the second costs money. Scale requests per second by adding replicas of the chosen group.
Mixture-of-experts serving
In a mixture-of-expertsMixture of expertsA model whose layers hold many expert blocks, with a router sending each token to a few of them. Compute per token follows the active parameters; memory follows the total.Open the glossary entry (MoE) model a router sends each token to a few experts, so active parameters are far fewer than total parameters. Mixtral 8x7B has 47B parameters of which 13B are active per token (Jiang et al., 2024); DeepSeek-V3 has 671B total and 37B activated (DeepSeek-AI, 2024).
Compute per token follows the active parameters. Memory follows the total, and so does the bandwidth bill once the batch grows. Suppose routing is uniform and independent, with each token choosing 2 of 8 experts as in Mixtral. The chance that a given expert is touched by at least one of tokens in a layer is:
| Batch | Share of experts read in a layer |
|---|---|
| 1 | 25 percent |
| 4 | 68 percent |
| 8 | 90 percent |
| 16 | 99 percent |
So a step at batch 1 reads about the active weights (), while a step at batch 16 reads nearly all of them (), over three times as much, for sixteen tokens instead of one. This is an idealisation: real routers are skewed and shared layers add a fixed amount, but the shape is right. MoE saves compute, not memory, and its decode advantage depends on batch size.
Consequences. The whole model must be resident, so it needs many GPUs. Experts are spread with expert parallelismExpert parallelismSpreading the experts of a mixture-of-experts model across GPUs and routing each token to the GPU that holds its expert with an all-to-all exchange. Uneven routing makes the busiest GPU the straggler.Open the glossary entry, which routes tokens to experts with an all-to-all exchange, network-heavy and sensitive to load imbalance: a hot expert makes its GPU the straggler, which shows up as p99 latency spikes while p50 looks fine. Mitigations include replicating hot experts, load-balancing in routing, and routing-aware batching. Large deployments often combine expert parallelism for experts, data or tensor parallelism for attention, and prefill and decode disaggregation (lesson 4).
Many fine-tunes, one base model: multi-LoRA
Serving a separate full model per customer fine-tune is ruinous. A LoRALoRALow-rank adaptation: freeze the base weights and train a small low-rank update, a tiny fraction of the parameters. Adapters are small, swappable and mergeable.Open the glossary entry adapter is a small low-rank change to selected matrices, so one base model can stay resident while adapters are loaded as needed.
Size, with illustrative choices on Llama 3 8B dimensions (hidden size 4,096, KV projection width 1,024, 32 layers): a rank-16 adapter on the query, key, value and output projections has parameters for query and for output, and for key and for value, so 425,984 per layer and about 13.6 million over 32 layers. In 16-bit values that is 27 MB, about 0.17 percent of the 16 GB base. Hundreds of such adapters fit in the memory of one extra base copy.
S-LoRA (Sheng et al., 2023) keeps adapters in host memory, moves those needed by running requests to the GPU, and uses one paged pool for adapters and KV tensors; it reports up to 4 times the throughput of HuggingFace PEFT and vLLM with naive LoRA support, in its experiments. In vLLM (accessed 2026-10-21), you enable LoRA, the client selects an adapter by passing its name as the model, and adapter requests run alongside base-model requests, with batching across adapters depending on the configured max_loras. It can load adapters at runtime, which its documentation flags as a security risk.
Costs to plan for: adapter load latency, cache pressure, per-adapter prefix caches (an adapter changes the keys and values, lesson 2), and a maximum rank setting that wastes memory if set too high. How adapters are trained is covered in the fine-tuning lesson linked below; here an adapter is a serving cost.
Choosing an engine
Do not choose from a leaderboard. Choose from criteria: does it support your model, quantization, adapters and structured output; how does it perform on your prompt and output length distribution at your target latency; how mature are its metrics, health checks and upgrades; how locked in are you to hardware?
| Engine | Strengths (as documented, 2026-10-21) | Watch-outs |
|---|---|---|
| vLLM | PagedAttention and continuous batching (Kwon et al.), prefix caching, LoRA, speculative decoding, a broad quantization table, OpenAI-compatible server | moves fast; tune flags per workload |
| SGLang | RadixAttention for KV reuse (Zheng et al.); its README presents a serving framework for language, vision-language and diffusion models and lists NVIDIA, AMD, TPU and other hardware | smaller ecosystem than vLLM |
| TensorRT-LLM | documented for NVIDIA GPUs; in-flight batching, paged KV cache, FP8 on H100 and later, FP4 on B200, EAGLE, MTP and n-gram speculative decoding, PyTorch-native runtime | tied to NVIDIA hardware |
| Text Generation Inference | simple Hugging Face deployment | the repository states it is in maintenance mode and was archived on 2026-03-21, recommending vLLM and SGLang |
| llama.cpp, MLX and similar local engines | CPU, Apple Silicon and edge | built for local use, not datacenter concurrency |
The TGI row is a reminder that engine status changes. Check the repository before you build on one. A defensible default: vLLM or SGLang for flexibility, and TensorRT-LLM when squeezing NVIDIA hardware at stable scale. Whichever you pick, verify claims by benchmarking your own traffic (lesson 6).
Hardware: read four numbers
For each accelerator, ask for memory capacity, memory bandwidth, low-precision compute and interconnect bandwidth. The mapping is the roofline again.
- Capacity decides what fits: weights plus cache.
- Bandwidth decides decode speed.
- Compute decides prefill and large-batch speed.
- Interconnect decides how well TP and EP scale.
| Number to read | What it decides |
|---|---|
| Memory capacity | What fits: weights plus cache |
| Memory bandwidth | Decode speed |
| Low-precision compute | Prefill and large-batch speed |
| Interconnect bandwidth | How well tensor and expert parallelism scale |
From NVIDIA’s pages (accessed 2026-10-21): H100 SXM has 80 GB and 3.35 TB/s; H200 has 141 GB and 4.8 TB/s with NVLink at 900 GB/s; the DGX B200 system page lists 1,440 GB total and 64 TB/s across the system, with FP8 figures marked as sparse (halve them for a dense estimate). Compare same-basis numbers across vendors, and check availability and price, which in practice often decide.
Encoder-style models such as embedding and reranker models have no decode loop and no growing cache. Their batches are compute-bound, so they benefit from batching by length and from running on separate pools, so an indexing burst does not starve chat.
Self-host or use an API
LLM inference and serving gave the break-even method. The two additions from this module: use the cost formula from lesson 1 at your measured throughput and a realistic utilization, and price the layout, not one GPU. Illustrative: at 2.50 dollars per GPU hour and 2,500 output tokens per second, cost is about 0.28 dollars per million tokens at full load and about 0.93 dollars at 30 percent utilization (invented price and throughput). Add engineering and on-call, and compare quality, not only price. A common hybrid sends steady high-volume traffic to your fleet and spikes and the long tail to an API.
Real-World Application
Start from the model’s weights and cache per token, pick the smallest number of GPUs that leaves a useful KV budget for your context length, and prefer quantization over more GPUs when the wire is slow. Keep TP inside a fast-link domain. Scale throughput with replicas, and put a router in front. For a mixture-of-experts model, plan for the whole parameter count in memory and watch p99 for expert imbalance. Pick the engine by running your own traffic, and re-check its status when you upgrade.
Interview Perspective
Expect: “TP versus PP versus DP: what is split and what is communicated?”, “Lay out a 405B dense model on H100 nodes”, “Your MoE deployment has p99 spikes while p50 is fine. What do you check?”, “50 million tokens a day with data-residency limits: self-host or API?”. A strong answer gives weights, cache budget, step floor and the interconnect assumption, then picks the layout.
Common mistakes: “just add GPUs with tensor parallelism”; saying MoE is cheap in memory because few experts are active; choosing an engine from a benchmark that does not match your length distribution; proposing TP across nodes without checking the interconnect; costing self-hosting at full utilization.
Provenance: Common industry pattern and Generated practice. These are standard serving topics for ML-infrastructure and system-design rounds; none is verified for a named company. Engine and hardware details are dated and will change.
Practice Questions
- Basic: TP versus PP versus DP: what is split and what is communicated?
- Intermediate: Lay out a 405B-class dense model on H100 nodes with 8 GPUs and NVLink each; justify the parallelism and quantization.
- Advanced: Your MoE deployment shows p99 spikes while p50 is fine. What do you check?
- Product-Company: 50 million tokens a day with data-residency limits. Self-host or API? Show the break-even and a hybrid option.
Activity
Pick a dense model and a node of GPUs you could rent. Compute weights, KV bytes per token, the KV budget with stated overhead, how many sequences of your context length fit, and the weights-only step floor. Then decide the smallest layout that meets a 40 ms inter-token target, and say whether quantizing the weights would let you use fewer GPUs.
Homework
Write a one-page serving design for a 70B dense assistant with a 40 ms inter-token target: hardware, parallelism, quantization, engine, how you scale, what you monitor, and a fallback. Include the memory and step-time arithmetic with every assumption stated.
Quick Revision
- Use the smallest parallelism that fits and meets the target; scale out with replicas.
- TP: all-reduces inside every layer, so it needs fast links. PP: activations between stages, tolerates slow links, does not shorten a token’s path. EP: all-to-all, imbalance risk.
- Layout arithmetic: weights per GPU, KV budget, weights-only step floor, then the cache and wire.
- MoE: compute by active parameters, memory and bandwidth by total as the batch grows.
- One base model plus small adapters serves many fine-tunes; adapters are tens of MB.
- Choose engines by criteria and your own traffic; check status and dates.
- Hardware: capacity, bandwidth, compute, interconnect map to fit, decode, prefill, scaling.
Check yourself
- Walk-through: Serve a 70B dense model for an internal assistant with a 40 ms inter-token target. Pick hardware, parallelism, quantization and engine, and say how you scale.
- Variation: Repeat for a large mixture-of-experts model.
- Common misconception: “More GPUs per replica always means lower latency.” Communication can cancel the gain.
Related
Resources & Further Reading
Checked sources to go deeper. The essential ones come first in each group.
Official documentation
Speculative Decoding (opens in a new tab)
Speculative decoding is the next engine feature to read about once layouts are chosen; the page says it targets inter-token latency in memory-bound, low-to-medium load workloads.
Further reading
LLM Inference Handbook (opens in a new tab)
A broader tour of serving topics that can be read next to the vendor documents above.
Research papers
A peer-reviewed case for separating prefill and decode onto different GPUs, which extends the layout choices in this lesson.
More for this module: Inference engineering resources
Practice
Test what you just read. Quizzes and cards are grouped by module: Inference engineering.
Check yourself15 questions on what this lesson set out to teach
Checking answers needs JavaScript. You can still read every question and option below.
Keyboard: arrow keys move between options. Space or Enter selects the focused option.