Inference Engineering

Lesson 13 of 14

Tensor and pipeline parallelism

Llama 3.1 70B needs 141 GB of memory just to hold its numbers, and the biggest common AI chip, an H100, has 80 GB. This lesson shows the two ways to split one model across several chips, what each split costs in chatter between them, and why one stays inside a single machine while the other can stretch across many.

One model, two GPUs, two ways to cut it

Llama 3.1 70B on two NVIDIA H100s, one user, with real numbers. Height is memory, to scale. It plays by itself; press a stage to replay it, or drag to look around.

01 · Too big for one

The model does not fit

A model is a stack of layers. Each is mostly a table of numbers, its weights. To write one token (a token is a piece of a word, about three-quarters of one on average), the GPU reads every layer’s weights from its memory. Llama 3.1 70B has 80 layers and 141 GB of weights in 16 bits.

An H100 has 80 GB, and a server holds 10% back so it never runs out mid-reply. No setting fixes that: the model has to be split across GPUs. Two just hold it, with room for 8,827 tokens of notes, the KV cache from lesson 4. Four leave about 51 times as much.

One GPU's memory, as the model is split wider

  1. 1 GPU141 GB does not fit
  2. 2 GPUs70.6 GB room for 8,827 tokens
  3. 4 GPUs35.3 GB room for 448,280 tokens
  4. 8 GPUs17.6 GB room for 1,327,186 tokens

80.0 GB: one H100

  • Weights, this GPU's share
  • Room for notes (KV cache)
  • Held back, 10%
  • Does not fit

02 · Split by layers

A pipeline takes turns

The first way gives each GPU a run of whole layers: GPU 1 the first quarter, GPU 2 the next, and so on. This is pipeline parallelism. A token climbs one GPU’s layers, then is handed to the next in one small message.

The GPUs take turns. One user’s token visits them one after another, so it comes out no sooner than on one giant GPU: 24 tokens a second, while three of four GPUs wait. They can work on other users’ tokens meanwhile, so four GPUs write 96 tokens a second in total, but nobody gets theirs faster.

Four GPUs, a quarter of the layers each

Each chat
All chats together
Each GPU busy

03 · Split every layer

Faster, but the GPUs must talk

The second way cuts every layer into pieces, one per GPU. This is tensor parallelism. All the GPUs read their piece of the same layer at once, so one user’s tokens come out faster: 88 a second on four GPUs.

Each GPU ends up holding only part of each answer. Twice per layer the GPUs add their parts together in an exchange called an all-reduce: 160 per token. Each takes microseconds, but they add up. With eight GPUs, talking is 30% of the time.

One token, every layer split across more GPUs

  1. 1 GPUif it fit24 tokens/s41.6 ms a token
  2. 2 GPUstalking 2%47 tokens/s21.1 ms a token
  3. 4 GPUstalking 9%88 tokens/s11.4 ms a token
  4. 8 GPUstalking 30%134 tokens/s7.45 ms a token
  • The token's trip: each GPU reads its share of every layer
  • Of which swapping messages, 160 times a token

04 · Where each one goes

Tensor inside a server, pipeline between

Inside one server, a machine with usually eight GPUs, the GPUs are wired together by NVLink, fast enough for constant exchanges. Between servers the wire is InfiniBand, 9 times slower. A tensor split slows down badly over it; a pipeline, which talks once per hand-off, barely notices.

So for the customer: four H100s in one server, every layer split. Large deployments combine both: tensor inside each server, pipeline between servers.

8 GPUs, 64 chats, two kinds of wire

  1. Split every layertensor
  2. Split by layerspipeline

0%share of each token's time spent talking100%

05 · Playground

Split it yourself

Start with two GPUs and add chats until the notes no longer fit. Then switch to a pipeline and compare one chat with eight.

Serve Llama 3.1 70B yourself

16-bit weights on 80 GB H100s, chats of 1,024 tokens

How to split

Memory on each GPU

  • Weights
  • Notes for these chats
  • Held back, 10%
  • Does not fit
Each chat
All chats together
Time spent talking

06 · Glossary

The vocabulary, in one place

Token
The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
Layer
One stage of the model. Every token passes through all of them in order; Llama 3.1 70B has 80.
Weights
The tables of numbers inside each layer, read in full for every token. 141 GB for Llama 3.1 70B in 16 bits.
GPU memory
The chip's own storage, 80 GB on an H100. The weights and the notes have to fit in it, less a 10% safety margin.
KV cache
The notes the model keeps for every token of every conversation, so it never reruns earlier text (lesson 4).
Pipeline parallelism
Splitting a model by layers: each GPU holds a run of whole layers, and a token passes from one GPU to the next.
Tensor parallelism
Splitting every layer: each GPU holds a piece of each layer, and all GPUs work on every layer at once.
All-reduce
The exchange in which GPUs add up their partial answers so each has the total. A tensor split does 160 per token for Llama 3.1 70B.
Server
One machine, usually with eight GPUs. Inside it the GPUs are wired directly to each other.
NVLink
The direct wiring between GPUs in one server: 450 GB/s each way per H100.
InfiniBand
The network between servers: 50 GB/s each way per H100, 9 times slower than NVLink.

You can now

  • Compute how much of a model's weights and KV cache each GPU holds under a tensor or pipeline split, and how many tokens of KV cache fit in the memory left over.
  • Explain why tensor parallelism makes one user's tokens faster but pauses to combine results twice per layer, while pipeline parallelism talks rarely but gives one user no speedup and needs other users' requests to keep its GPUs busy.
  • Decide when to use each split: tensor parallelism inside one NVLink server (usually up to 8 GPUs), even for batched serving, and pipeline parallelism across servers where the link is too slow for constant all-reduces.

Try it in the Serving Lab: Serve the 70B model on the fewest H100s that meet the targets.

Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.