Module 7 · AI infrastructureLesson 87 of 887 minAdvanced
Distributed model inference
When a model is too big or too slow for one GPU, split it across several, either by slicing every layer or by giving each GPU a group of layers.
Play "Tensor parallel" and count the GPU-to-GPU messages per layer. Then play "Pipeline parallel" and watch two batches move through the stages.
A 70B model in 16-bit needs about 140 GB for weights alone, more than one 80 GB GPU. Even when a model fits, splitting it can make each token faster. There are a few ways to split the work.
Tensor parallelism
Every layer's big matrix multiplications are split across GPUs. Each GPU holds a slice of every layer and computes part of the result; then the GPUs combine their parts (an all-reduce) before the next layer.
- All GPUs work on every token, so latency per token goes down.
- Needs communication after every layer, so it needs very fast links (NVLink within a machine).
- Common: tensor parallel across the 4 or 8 GPUs of one machine.
Pipeline parallelism
The layers are divided into stages: GPU 1 runs layers 1 to 20, GPU 2 layers 21 to 40, and so on. Activations flow from stage to stage.
- Little communication: just passing activations between neighbours, so it works across machines.
- Each token still passes through every stage in turn, so it doesn't reduce latency by itself.
- Bubbles: stages sit idle while the pipeline fills or drains. Splitting work into micro-batches keeps stages busier.
Other kinds of parallelism
- Data parallelism (replicas): full copies of the model serve different users. The simplest way to scale throughput.
- Expert parallelism: in mixture-of-experts models, different experts live on different GPUs and tokens are routed to them.
- Sequence / context parallelism: very long contexts are split across GPUs.
Putting it together
A large deployment might use tensor parallel 8 within each machine, pipeline parallel 2 across two machines, and 20 such replicas behind a router. Serving engines like vLLM, TensorRT-LLM and SGLang handle the splitting; your job is choosing the layout that meets latency and cost targets.