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
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.
An assembly line of microbatches
The bubble fraction
4 GPUs, 32 blocks
Simulate the schedule, count idle slots
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
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.
Bytewise
All courses