Lesson 13: Tensor and pipeline parallelism · Workbook
Tensor and pipeline parallelism: the workbook
The full working behind lesson 13. How a matrix multiply splits across GPUs, what an all-reduce costs, decode time and memory per GPU under a tensor split, the pipeline and its bubble, and seven exercises to check yourself.
The two cuts at the sushi counter
A language model is a stack of layers, stages that each rework the text a little and pass it up to the next. Llama 3.1 70B has 80 of them. Most of each layer is a large 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 (the chip that runs the model) reads every layer’s weights from its memory. Draw that stack as a tower, too tall for one GPU: a tower can be cut two ways, down or across.
Back to lesson 6’s sushi counter, where the chef now works from lesson 1’s rulebook: 80 thick chapters, one per step, read cover to cover for every piece. The rulebook is the weights, each piece is one token, and each chef is a GPU. The rulebook has outgrown one chef’s shelf: you hire four chefs and split it four ways, one part each.
Cut every chapter into four. Each chef keeps a quarter of every chapter. All four chefs work on each piece at once, each reading only their quarter, so every step takes about a quarter of the time. The catch: twice in every step, halfway through and at the end, the chefs must add their partial answers together before anyone can go on. This is tensor parallelism. For Llama 3.1 70B that is 160 exchanges per token. Each exchange is small, but 160 pauses per piece add up. They stay short only if the chefs stand side by side: GPUs in the same server (one machine, usually with eight GPUs) wired directly to each other. There, one user gets about 88 tokens a second from four GPUs.
Give each chef a quarter of the chapters. The first chef keeps the first quarter of the chapters, the next chef the next quarter, and the piece moves down the line. This is pipeline parallelism. Handing a piece on is cheap: three hand-offs per token instead of 160 exchanges. But the piece visits the chefs one after another, each reading whole chapters, so it takes as long as with one chef who had room for everything: about 24 tokens a second. Meanwhile three of the four chefs stand idle. The line only earns its keep when several pieces move along it at once, one per chef. They must be other diners’ pieces: each of one diner’s pieces depends on the one before. Then every chef is busy and four times as many pieces go out, but each diner waits just as long. The sliced counter can serve several diners at once too, from one read of its rulebook, and inside one server it wins on both counts: faster pieces, and more of them.
Which cut goes where
Both cuts give each GPU the same share of the weights. What differs is how often the GPUs must talk. Tensor parallelism talks constantly, so it stays inside one server. Pipeline parallelism talks rarely, so it can stretch between servers over a slower network.
So for the customer: put the 70B model on several GPUs in one server and slice every layer. Two H100s can just hold the weights, but they leave little room for the KV cache from lesson 4, the notes each conversation keeps. Four leave many times more. (Lesson 10’s 8-bit weights, 72.7 GB, fit one H100 but not the 72.0 GB left after the 10% reserve: no room for notes; and this customer requires 16 bits.)
Why split at all
A model has to fit in GPU memory before it can run at all. Llama 3.1 70B has 70.6 billion parameters. In BF16, at 2 bytes each, the weights alone take 141 GB. An H100 has 80 GB. There is no batch size or kernel trick that fixes this: the model has to be split across GPUs.
There is a second reason to split, even when a model fits. The roofline lesson showed that single-user decode is limited by how fast a GPU can read its weights. Four GPUs, each reading a quarter of the weights, have four times the memory bandwidth between them. Short of reading fewer bytes (quantization, lesson 10) or checking several tokens per read (speculative decoding, lesson 11), splitting is the way to make one user’s tokens come faster once a single GPU is already reading at full speed: it is the only way to add memory bandwidth.
There are two standard ways to split. Tensor parallelism cuts every layer into pieces and gives each GPU a piece of every layer. Pipeline parallelism gives each GPU a run of whole layers. Both divide the memory the same way. They differ in how often the GPUs must talk, and that decides where each one is used.
Splitting a matrix multiply
Start with the MLP block of one layer, where most of a transformer’s weights live. A token’s hidden vector (length ) is multiplied by a first weight matrix (), passed through an elementwise activation, then multiplied by a second matrix (). For Llama 3.1 70B, = 8,192 and = 28,672. (Llama actually has two first-stage matrices, a gate and an up projection, combined elementwise. Both are split the same way, so one is enough to show the idea.)
There are two ways to cut across GPUs. Cutting it by rows forces you to add up the partial results before the activation, since the activation is not linear. (With a row cut, GPU gets the matching slice of the input and computes , a full-length intermediate vector that is only a partial sum. The true intermediate is , and the activation of a sum is not the sum of the activations, so the GPUs would have to all-reduce before the activation.) Cutting it by columns does not: GPU holds columns , computes its own slice of the intermediate vector, and applies the activation to that slice alone. Now cut by rows, along the same axis, so GPU ‘s rows match exactly the slice it already has. Each GPU finishes the block with no communication at all:
Each term in the sum is a full-length vector computed on one GPU. The sum is the only step that needs the other GPUs. Adding a vector across GPUs, and leaving the result on all of them, is a collective operation called an all-reduce.
This is the layout from Megatron-LM (Shoeybi et al., 2019): the first matrix split by columns, the second by rows, and no communication between them. Attention splits the same way along its heads. Each GPU computes the query, key and value projections for its share of the heads, runs attention for those heads locally, and multiplies by its rows of the output projection. That gives one all-reduce after attention and one after the MLP: two per layer in the forward pass, the count the paper gives.
Step through the figure. The top plate is , the bottom plate is , and the colors mark which GPU holds what.
Drag to rotate, scroll to zoom
- Each GPU holds, per layer
- MLP weights per GPU, per layer
- Each all-reduce (batch 1)
So for Llama 3.1 70B, with 80 layers, generating one token takes 160 all-reduces. Each one carries one hidden vector per sequence in the batch: numbers at 2 bytes, 16,384 bytes at batch 1. The weights never move. Only activations do.
What an all-reduce costs
The standard way to run an all-reduce among GPUs is a ring. Each GPU splits its vector into chunks and passes chunks to its neighbor. In the first steps (a reduce-scatter), each chunk travels around the ring collecting the sum. In the next steps (an all-gather), the finished chunks travel around again so everyone has all of them. Each GPU sends times the message size. Patarasuk and Yuan (2009) proved that, when the GPUs themselves do the adding over point-to-point links, no all-reduce algorithm can do with less: some GPU must send at least that much. The ring is bandwidth-optimal under that condition. (The H100’s NVSwitch can get around this bound with in-network reduction, NVLink SHARP, which does the adding inside the switch.)
Each of the steps also pays a fixed start-up cost : launching the transfer, synchronizing, and waiting for the first bytes to arrive. With a message of bytes and a per-direction link bandwidth :
Inside a server, GPUs talk over NVLink, direct GPU-to-GPU links. In an 8-GPU H100 server these run through NVSwitch chips, so every GPU can reach every other at full NVLink speed; that group of 8 is one NVLink domain. Between servers, traffic goes over a network such as InfiniBand.
The bandwidth here needs care. NVIDIA lists the H100’s fourth-generation NVLink as 900 GB/s per GPU. Its Hopper architecture post explains the number: 18 links, each carrying 25 GB/s in each direction. So the 900 GB/s headline counts both directions added together. In a ring, each GPU sends to one neighbor while receiving from another, so the number that limits it is the one-direction figure: 450 GB/s (18 links × 25 GB/s). Between servers, a DGX H100 has eight 400 Gb/s InfiniBand cards for its eight GPUs, which is 50 GB/s per GPU in each direction, 9 times less.
Now put in the decode numbers. At batch 1 with 8 GPUs, each GPU sends 28,672 bytes per all-reduce, which takes 64 nanoseconds over NVLink. That is almost nothing. The fixed cost is what matters: 14 ring steps, and each one costs something on the order of microseconds. That per-step latency depends on the library and kernel and is not on any datasheet, so this lesson assumes = 1 µs and lets you change it in the next figure. At that value one all-reduce takes 14.1 µs, and 160 of them take 2.25 ms per token.
This is why inference libraries do not use a plain ring for small messages. NVIDIA’s TensorRT-LLM MultiShot all-reduce, for example, uses the NVSwitch to finish in two communication steps whatever the number of GPUs, instead of the ring’s . The bandwidth term is already near its floor at decode; the latency term is the one worth attacking.
Decode with tensor parallelism
Put the two halves together. With tensor parallelism of degree , each GPU reads of the weights and of the KV cache (the cache splits by KV head, since each GPU owns its heads). The GPUs read at the same time, so their bandwidths add. Then the step pays for the all-reduces, which cannot overlap with the matmuls on either side because each matmul needs the previous all-reduce’s result. For a model of weight bytes and layers:
The first term shrinks as . The second grows with : more ring steps, and the bytes term creeps up toward twice the message. Pope et al. (2022) analyse this layout with bandwidth only: memory and compute time fall almost linearly with the chip count, while the bytes term stays roughly constant, so communication becomes the bottleneck. Adding the per-step latency makes it worse: the latency term grows with , so communication does not just stop shrinking, it grows.
Each H100 reads HBM at 3.35 TB/s. Decode looks up only one row of the embedding table, so its 2.10 GB is not read per token. For Llama 3.1 70B on H100s, batch 1, 1,024 tokens of context, with = 1 µs:
| GPUs | HBM read time per GPU | All-reduces | Step floor | Comm. share |
|---|---|---|---|---|
| 1 (does not fit) | 41.6 ms | none | 41.6 ms | 0% |
| 2 | 20.8 ms | 326 µs | 21.1 ms | 2% |
| 4 | 10.4 ms | 969 µs | 11.4 ms | 9% |
| 8 | 5.20 ms | 2.25 ms | 7.45 ms | 30% |
Going from 4 to 8 GPUs halves the reads but cuts the step only from 11.4 ms to 7.45 ms, about 88 to 134 tokens per second for one user. At 8 GPUs, communication is 30% of each step. With 16 GPUs it would be a larger share still, and those GPUs would sit in two servers, connected by the slower link. This is why tensor parallelism usually stops at the size of one NVLink domain, 8 GPUs in a standard H100 server.
How much of that is real? The communication share rests on the assumed . As a check on its size: NVIDIA reports that in an 8-way tensor-parallel decode of Gemma 2 on 8 H100s, all-reduces took about 23% of decode latency. Different model, different kernels, same order of magnitude.
The chart below draws the table. Raise the batch to see the bytes term grow (the message is one vector per sequence), switch the link to InfiniBand to see what tensor parallelism across servers would cost, and change to see how much the conclusion depends on it.
- Reading weights and KV cache (memory-bound)
- Matmuls, when compute-bound
- All-reduce: fixed latency per step
- All-reduce: bytes over the link
Memory per GPU
Splitting the weights ways splits their memory ways. The KV cache splits too, because each GPU stores keys and values only for its own heads. Llama 3.1 70B has 8 KV heads (it uses grouped-query attention, covered in lesson 5), so the cache divides cleanly up to 8 GPUs. Past that, GPUs would have to share heads, and the cache stops shrinking.
What is left after the weights is room for the KV cache, which sets how many tokens, across all requests, a server can hold:
| GPUs | Weights per GPU | KV per token per GPU | Tokens of KV that fit |
|---|---|---|---|
| 1 | 141 GB | 327,680 B | 0 |
| 2 | 70.6 GB | 163,840 B | 8,827 |
| 4 | 35.3 GB | 81,920 B | 448,280 |
| 8 | 17.6 GB | 40,960 B | 1,327,186 |
These hold back 10% of each GPU’s 80 GB, as in lesson 4, and ignore activations. Two GPUs barely fit the weights and leave room for about 8,827 tokens, enough for 1 conversation of 8,192 tokens. Four GPUs leave about 51 times as much. The room freed by extra GPUs grows faster than the GPU count, because the weights take a smaller share of each one. That extra KV room is what lets a server run large batches, which is where its throughput comes from (lesson 6).
Pipeline parallelism
The other way to split is by layer. With pipeline parallelism of degree , the first GPU holds the first layers, the second GPU the next , and so on. Each group of layers is a stage. A token’s hidden vector goes through stage 1, is sent to stage 2, and so on to the end. Memory per GPU divides by , exactly as with tensor parallelism.
The communication is much lighter. Between two stages, one GPU sends one hidden vector per sequence to the next GPU, once per token, with no all-reduce. Llama 3.1 70B split into 4 stages needs 3 point-to-point sends per token, against 160 all-reduces with 4-way tensor parallelism. That is why pipeline parallelism is the usual choice across servers, where the link is 9 times slower: it sends little data, rarely.
The cost is latency. The stages run one after another, so a single request gets no speedup. Each stage reads a quarter of the weights, but the token must visit all four in turn, so the full set of weights is still read in sequence. With 4 stages and one batch in flight, the time per token is 41.6 ms, no better than one GPU, and each GPU is busy only 25% of the time. With 4-way tensor parallelism, the same 4 GPUs give 11.4 ms.
The idle time is called the bubble, a name from GPipe (Huang et al., 2019), which introduced this kind of pipeline for training. GPipe splits each batch into micro-batches so that, while stage 2 works on micro-batch 1, stage 1 can start on micro-batch 2. With stages, the paper gives the idle fraction as of order
With 4 stages and one micro-batch that is 75% idle; with 16 micro-batches, 16%. The authors found the overhead negligible in their experiments once .
Inference changes the picture in one way. Training pays the bubble every batch, as the pipeline fills and drains. A decode server can keep running, but a batch’s next token cannot start until its previous token leaves the last stage. So the server needs at least independent batches cycling through the pipeline to keep every stage busy. With 4 batches of one sequence each, our model gives 96 tokens per second in total, 4 times the single-batch rate, but each sequence still waits 41.6 ms between its tokens. Pipeline parallelism needs several batches in flight just to keep its GPUs busy, and it is no speedup for any one user. Its advantage is how little it sends, which matters when the link is slow.
Large deployments combine the two: tensor parallelism inside each server, where NVLink makes frequent all-reduces affordable, and pipeline parallelism between servers, where only a few sends per token cross the slower network.
What the model leaves out
The numbers above are floors, like every number in this course, and the parallel case adds its own gaps.
- The latency is assumed. Measure it with your library’s all-reduce benchmark (for example NVIDIA’s nccl-tests) at the message sizes you will actually send, from a few kilobytes to a few megabytes.
- Libraries do better than a ring for small messages. Two-step all-reduces and fused all-reduce kernels attack the latency term. Some systems also overlap the communication of one micro-batch with the compute of another.
- Small matrices run less efficiently. With 8-way tensor parallelism, each GPU’s share of a matmul is 8 times smaller, and small kernels reach a smaller fraction of peak bandwidth, as the roofline lesson’s measurement showed.
- The embedding and output layers are split too and add a little communication, which we left out.
Check yourself
Every answer below comes from the same functions as the tables and charts.
Exercise 1. Store Llama 3.1 70B's weights in FP8 (1 byte each, with the embedding and output layer left in 16 bits as in lesson 10) and keep the KV cache in BF16. Does it fit on one 80 GB H100, with 10% held back? How does its KV room compare with two GPUs holding BF16 weights? Enter the one-GPU FP8 KV room.
Show the worked answer
The weights take 72.7 GB: they fit in the 80 GB, but not in the 72.0 GB left after the reserve, so there is room for 0 tokens of KV cache. One H100 cannot serve it. Two GPUs with BF16 weights leave room for 8,827 tokens, since each GPU stores only half of every token’s keys and values. Quantization (lesson 10) and parallelism are both ways to make a model fit, with different side effects: FP8 also halves the bytes read per token, while splitting adds communication.
Exercise 2. With 8-way tensor parallelism over NVLink (450 GB/s each direction) and a batch of 64, how many bytes does each GPU send per all-reduce, and how long does the bytes term take per token? Enter the per-token bytes term.
Show the worked answer
The message is 64 × 8,192 × 2 = 1,048,576 bytes. In a ring each GPU sends 2 × 7/8 of that, 1,835,008 bytes, taking 4.08 µs. Times 160 all-reduces per token gives 652 µs per step, against 2.24 ms for the latency term at α = 1 µs. Even at batch 64, latency is the larger part.
Exercise 3. Now run the same 8-way tensor parallelism, batch 64, across servers over 400 Gb/s InfiniBand instead of NVLink. What happens to the step? Enter the per-token bytes term.
Show the worked answer
The link is 9 times slower, so the bytes term grows from 652 µs to 5.87 ms per token, and the step from 8.88 ms to 14.1 ms. Communication goes from 33% to 58% of the step. This keeps the same α for both links; a hop across the network has a higher fixed cost in practice, which makes it worse.
Exercise 4. You have 4 H100s for Llama 3.1 70B. Compare 4-way tensor parallelism with a 4-stage pipeline at batch 1: time per token for one user, and total tokens per second if the pipeline has 4 batches of one sequence in flight.
Show the answer
Tensor parallelism: 11.4 ms per token, about 88 tokens/s. Pipeline: 41.6 ms per token for one user, since the token visits the stages one after another. With 4 batches in flight, the pipeline produces about 96 tokens/s in total, but each sequence still gets one token every 41.6 ms. For a fair comparison, give 4-way tensor parallelism the same 4 sequences as one batch of 4: 11.5 ms per token, about 349 tokens/s, more than the pipeline on both counts. Pipeline parallelism needs several batches in flight just to keep its GPUs busy. Its advantage is not throughput but how little it sends, which matters when the link is slow. Inside one server, tensor parallelism wins.
Exercise 5. Why not 16-way tensor parallelism for Llama 3.1 70B? Consider the KV cache and the link.
Show the answer
Two reasons. The model has 8 KV heads, so beyond 8 GPUs the KV cache per token per GPU stays at 40,960 bytes: heads get copied, not split. And 16 H100s span two servers, so the all-reduces cross InfiniBand. Our model gives 7.50 ms per token at batch 1, against 7.45 ms with 8 GPUs over NVLink: twice the GPUs and no faster. At batch 1 the bytes term barely matters; the extra ring steps eat the gain.
Exercise 6. From lesson 1: an H100 reads its memory at 3.35 TB/s. Llama 3.1 70B's matrix weights take 139 GB in BF16. If one GPU could hold them all, what is the shortest time to read them once, the floor for one decode step for one user? Enter milliseconds.
Show the worked answer
41.5 ms: 139 GB ÷ 3.35 TB/s. A pipeline’s time per token is about the same, 41.6 ms, because its stages read their shares one after another (and the KV cache besides). With the tensor split each of 4 GPUs reads a quarter at the same time, so the reading drops to 10.4 ms per token (a quarter of the weights plus a quarter of the KV cache, which at 1,024 tokens adds only about 25.0 µs), and the all-reduces add 969 µs.
Exercise 7. From lesson 4: Llama 3.1 70B has 80 layers, 8 KV heads and a head dimension of 128. How many bytes of KV cache does one token need in BF16? Enter bytes.
Show the worked answer
327,680 bytes: 2 × 80 × 8 × 128 × 2. The tensor split divides it by KV head, so with 8 GPUs each one stores 40,960 bytes per token, the last row of the memory table above. A pipeline divides it by layer instead, and arrives at the same share per GPU.
Sources
- Shoeybi, Patwary, Puri, LeGresley, Casper, Catanzaro (2019). Megatron-LM: Training multi-billion parameter language models using model parallelism. arXiv:1909.08053. Section 3: split the first MLP matrix by columns and the second by rows; attention split by heads; two all-reduces per layer in the forward pass
- Huang, Cheng, Bapna, Firat, Chen, Chen, Lee, Ngiam, Le, Wu, Chen (2019). GPipe: Efficient training of giant neural networks using pipeline parallelism. NeurIPS 2019. Bubble overhead O((K−1)/(M+K−1)); found negligible when M ≥ 4K. Also arXiv:1811.06965
- Pope, Douglas, Chowdhery, Devlin, Bradbury, Levskaya, Heek, Xiao, Agrawal, Dean (2022). Efficiently scaling transformer inference. arXiv:2211.05102. Section 3.2: with the Megatron layout, weight-loading and compute time fall with the chip count while communication time stays roughly constant
- Patarasuk, Yuan (2009). Bandwidth optimal all-reduce algorithms for clusters of workstations. Journal of Parallel and Distributed Computing 69(2), 117–124. Lower bound: some process must send at least 2(N−1)/N × the data; a ring meets it
- NVIDIA (2022). NVIDIA Hopper architecture in-depth. H100: 18 fourth-generation NVLink links, each 25 GB/s in each direction, 900 GB/s total
- NVIDIA NVLink and NVSwitch product page. Fourth generation: 900 GB/s per GPU, 18 links
- NVIDIA DGX H100/H200 user guide, introduction. 8 H100 GPUs; eight ConnectX-7 InfiniBand cards at up to 400 Gb/s each
- NVIDIA (2024). 3x faster AllReduce with NVSwitch and TensorRT-LLM MultiShot. Ring all-reduce takes 2N−2 steps; MultiShot uses two regardless of GPU count
- NVIDIA (2025). Optimizing for low-latency communication in inference workloads with JAX and XLA. 8-way tensor-parallel Gemma 2 decode on 8 H100s: all-reduce was about 23% of decode latency
- Llama 3.1 70B model card (Meta). d_model 8192, 80 layers, 64 query heads, 8 KV heads, MLP width 28672