All courses

Pipeline parallelism

Place consecutive layers on different GPUs and stream microbatches through them. Compute the pipeline bubble fraction and explain why more microbatches shrink it.

Builds on: The decoder-only transformer, end to end, Batch size in tokens and gradient accumulation

▶ Watch this lesson Start the whole course

What the animation says

Split the model by layers

A model with thirty-two blocks is too big for one GPU's memory. So split it by layers: blocks one to eight on GPU one, nine to sixteen on GPU two, and so on. The residual stream passes from GPU to GPU, like a baton. But send the whole batch in one piece, and only one GPU works at a time. Three of four sit idle. Seventy-five percent of the hardware, wasted. How would you keep all four busy?

An assembly line of microbatches

Cut the batch into microbatches, as in gradient accumulation. GPU one runs microbatch one, hands its activations to GPU two, and starts microbatch two right away. Each hand-off is a slice of the residual stream: b sequences, T tokens, d numbers each. Soon every stage works on a different microbatch. The backward pass runs in reverse, gradients flowing from GPU four back to GPU one. Each stage sums its gradients, then takes one optimizer step. Here, four stages and four microbatches. Tap the slot where GPU four starts working.

Fill, drain, and the bubble

GPU four starts three slots late. That's the fill. At the end, GPU one finishes three slots early. That's the drain. So the step lasts seven slots, and each GPU is busy in only four. Every stage waits three slots in total. The backward pass mirrors this shape. So the idle share is stages minus one, over microbatches plus stages minus one. Here, three sevenths, about forty-three percent. Keep four stages but use sixteen microbatches. What's the idle share now?

Simulate it, shrink it

Three over nineteen: about sixteen percent. Fill and drain stay fixed by the stages, while useful work grows with microbatches. Let's check by simulating. This grid marks which stage is busy in which slot. Stage s handles microbatch j in slot s plus j. This line marks it busy. One minus the busy share is the idle share. It matches the formula at every size. Drag the slider: find a microbatch count that brings the bubble under ten percent.

The limits, and the rule

So why not use a thousand microbatches? With a fixed batch, more microbatches means tinier ones, and a GPU only runs at full speed on large matrix multiplies. Keep them full-size instead, and each stage must store activations for every microbatch until its backward pass. Memory grows. Also, the end stages carry extra work: the embedding on GPU one, the final norm, unembedding and loss on GPU four. The formula assumes balanced stages. The rule: many more microbatches than stages keeps the bubble small.

Model too big for one GPU: split by layers?

Say a model with $L=32$ blocks doesn't fit in one GPU's memory (a GPU is the chip that runs the matrix multiplies). Put blocks 1–8 on GPU 1, 9–16 on GPU 2, and so on, then pass the residual stream from GPU to GPU. The catch: if a whole batch goes through in one piece, GPU 2 waits while GPU 1 works, and so on. Only 1 of 4 GPUs is busy at a time, so 75% of the hardware is wasted. How do you keep them all busy?

An assembly line of microbatches

Cut the batch into $m$ **microbatches** of $b$ sequences each, as in gradient accumulation. GPU 1 runs microbatch 1 through its blocks and sends the activations to GPU 2. These are a $[b,T,d]$ slice of the residual stream. GPU 1 then starts microbatch 2 right away. Soon every **stage** works on a different microbatch. The backward pass runs in reverse: gradients with respect to the residual stream flow from GPU 4 back to GPU 1. Each stage adds up its weight gradients over all microbatches, then takes one optimizer step.

The bubble fraction

Take $p$ stages and $m$ microbatches. Here $p$ counts stages; it is not the top-p cutoff. Assume each stage takes the same time per microbatch, and call that time one **slot**. The last stage starts $p-1$ slots late (**fill**). The first stage finishes $p-1$ slots early (**drain**). So the forward pass lasts $m+p-1$ slots, and each GPU is busy in only $m$ of them. Stage $s$ waits $s-1$ slots at the start and $p-s$ at the end, so $p-1$ in total. The backward pass has the same shape mirrored. So for the whole step the idle share is $$\text{bubble}=\frac{p-1}{m+p-1}.$$ The fill/drain cost is fixed by $p$, while useful work grows with $m$. More microbatches therefore shrink the bubble toward 0.

4 GPUs, 32 blocks

$p=4$ stages of 8 blocks each. GPU 1 also holds $W_E$. GPU 4 also holds the final norm, $W_U$ and the loss. These end stages run slightly longer, but the formula assumes balanced stages. - $m=1$: $3/4=75\%$ idle, the naive case. - $m=4$: $3/7\approx43\%$. - $m=16$: $3/19\approx16\%$. - $m=64$: $3/67\approx4.5\%$. The limits: with a fixed batch, a large $m$ means tiny microbatches, and a GPU only runs at full speed on large matrix multiplies. If instead you raise $m$ by adding more microbatches of the same size, each stage must keep activations for all $m$ of them until their backward pass. Then memory grows with $m$.

Simulate the schedule, count idle slots

Stage $s$ handles microbatch $j$ in slot $s+j$ (both counted from 0). The simulated idle share matches $(p-1)/(m+p-1)$. The backward pass has the same shape mirrored, so it gives the same fraction.
import numpy as np
def bubble(p, m):
    slots = m + p - 1
    busy = np.zeros((p, slots), bool)
    for s in range(p):          # stage index
        for j in range(m):      # microbatch index
            busy[s, s + j] = True
    return 1 - busy.mean()
for m in [1, 4, 16, 64]:
    print(m, round(bubble(4, m), 3), round(3 / (m + 3), 3))

Remember

Pipeline parallelism puts consecutive blocks on different GPUs and streams microbatches through them like an assembly line. Each step wastes a fill-and-drain **bubble** of $(p-1)/(m+p-1)$. Many more microbatches than stages make it small.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.