GPU memory for training
Estimate about 16 bytes per parameter for mixed-precision Adam (weights, gradients, fp32 master copy, two moments) plus activations ∝ B·T·d·L. Explain how activation checkpointing trades extra compute for less memory.
Builds on: The Adam optimizer, Mixed-precision training, Counting transformer parameters
What the animation says
The 80 GB puzzle
A seven billion parameter model, stored in bf16, takes about fourteen gigabytes. That's two bytes per weight, and it generates text happily on one GPU. Now try to train that same model on an eighty gigabyte GPU. It runs out of memory before the first training step even finishes. So training must keep more for every weight. How many bytes per parameter?
Pile 1: sixteen bytes per weight
Sixteen bytes. Let's stack them up for a single weight. Two bytes for the bf16 weight, and two for its bf16 gradient. Four for an fp32 master copy, the one the optimizer actually updates. Then four and four for Adam's two moments, also in fp32. This pile scales with the parameter count, and it lives for the whole run. Seven billion times sixteen is a hundred and twelve gigabytes. More than the whole GPU.
Pile 2: activations
The second pile comes from backprop. The backward pass needs values from the forward pass, like the input to each matmul. Each one is kept until the backward pass reaches its layer. So this pile grows with tokens per batch, model width, and depth: about thirty-four bytes for each, per block. A 1.2 billion parameter model: nineteen gigabytes of state, twenty-seven of activations. Checkpointing keeps only each block's input. With twenty-four blocks, how much do activations shrink?
Checkpointing: recompute, don't store
About ten times. Here's why it's not twenty-four. Forward keeps only each block's input: two bytes per number, about one point six gigabytes for all twenty-four blocks. In backward, each block reruns its forward from that input, uses the values, then frees them. That one live block adds about one point one gigabytes. Total, about two point seven, down from twenty-seven. The price is one extra forward pass: compute goes from three forward-equivalents to four, about a third more. Total memory falls from forty-seven gigabytes to about twenty-two.
Back to 7B
Back to seven billion parameters, one sequence of four thousand tokens. Without checkpointing, activations take about eighteen gigabytes. With checkpointing, they drop to about one point six. Yet the GPU still overflows. Something checkpointing can't touch. Tap the pile that still breaks the eighty gigabyte budget.
Two piles: parameter state and activations
The memory formula, in bytes
Activation checkpointing: recompute, don't store
Worked example: a 1.2B model
Back to 7B: what checkpointing can't fix
def train_mem_gb(N, B, T, d, L, ckpt=False, c=34):
states = 16 * N # weights, grads, master, m, v
if ckpt:
acts = 2*B*T*d*L + c*B*T*d # block inputs + one live block
else:
acts = c*B*T*d*L
return round(states/1e9, 1), round(acts/1e9, 2)
print(train_mem_gb(7e9, 1, 4096, 4096, 32)) # (112.0, 18.25)
print(train_mem_gb(7e9, 1, 4096, 4096, 32, ckpt=True)) # (112.0, 1.64)
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