All courses

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

▶ Watch this lesson Start the whole course

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.

Why won't a 7B model train on an 80 GB GPU?

A 7B-parameter model in bf16 needs only about 14 GB to generate text. Yet training that same model on a single 80 GB GPU runs out of memory before the first step finishes. Where does all that memory go? Which parts grow with model size, and which grow with batch and context? And which part can you trade compute to shrink?

Two piles: parameter state and activations

**Pile 1: per-parameter state.** Every weight carries its bf16 value, a bf16 gradient, an fp32 master copy and Adam's two fp32 moments. This pile scales with the parameter count and lasts the whole run. **Pile 2: activations.** Backprop needs the forward pass's intermediate values, such as the input to each matmul. Each one is kept until the backward pass reaches its layer. This pile scales with tokens per batch ($B\cdot T$) × width $d$ × depth $L$.

The memory formula, in bytes

$$\text{Mem}_{\text{train}} \approx 16N + c\,B\,T\,d\,L$$ The 16 bytes per parameter break down as 2 (bf16 weights) + 2 (bf16 grads) + 4 (fp32 master) + 4 + 4 (Adam's two moments). Strictly, every parameter costs 16 bytes, embeddings included. Using the non-embedding count $N$ slightly underestimates, by $16Vd$ for untied embeddings. The constant $c \approx 34$ is bytes per token, per unit of width, per block, in bf16. It covers every saved tensor inside one block: QKV inputs, FFN hidden values, norm inputs. It does not count stored $T\times T$ attention patterns, which add more on top.

Activation checkpointing: recompute, don't store

With checkpointing, the forward pass keeps only each block's input $r^{(\ell-1)}$ and discards everything inside the block. During the backward pass, it reruns that one block's forward pass from the saved input, uses the rebuilt values, then frees them. Activation memory drops to about $2BTdL$ (one bf16 block input, 2 bytes per number, for each of the $L$ blocks) plus $cBTd$ (one block's activations while it is being recomputed). The price is roughly one extra forward pass. Backward costs about twice forward, so total compute goes from about 3 to 4 forward-equivalents, or about +33%.

Worked example: a 1.2B model

Take $d=2048$, $L=24$, so $N\approx12\cdot24\cdot2048^2\approx1.2\times10^9$. - **States:** $16\times1.2\times10^9\approx19.3$ GB. - **Activations** at $B=8$, $T=2048$: $BTdL = 8\cdot2048\cdot2048\cdot24\approx8.05\times10^8$. Times 34 gives ≈27.4 GB. - Total ≈47 GB. **With checkpointing:** $2\times8.05\times10^8\approx1.6$ GB of saved inputs, plus one block's activations ($27.4/24\approx1.1$ GB) ≈ 2.7 GB. That is a 10× drop, not 24×. The total falls to ≈22 GB.

Back to 7B: what checkpointing can't fix

For a 7B model ($d=4096$, $L=32$, $B=1$, $T=4096$), parameter state alone is 112 GB, more than the whole GPU. Checkpointing cuts activations from about 18 GB to about 1.6 GB, but it never touches the $16N$ pile. The usual fix for that pile is spreading it across several GPUs, covered later.
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

Training memory ≈ $16N$ bytes of parameter state (weights, grads, fp32 master, two Adam moments) plus activations ≈ $c\,BTdL$. Activation checkpointing keeps only each block's input and recomputes the rest during backward. Activations shrink from $c\,BTdL$ to about $2BTdL + cBTd$. That is roughly 10× at typical depths, and never more than $c/2\approx17$×. It costs about 33% more compute and leaves $16N$ untouched.

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.