Skip to content
nonconvexcard

The Memory Arithmetic of Training

Sixteen bytes per trainable parameter, the activation bill, and why checkpointing every √L layers is exactly the right thing to do.

Fine-TuningAdvanced⏱ 1 hour9 min readmemoryadammixed precisiongradient checkpointingzero
3 problems →

Persistent state

PropositionSixteen bytes per trainable parameter

Mixed-precision training with Adam stores, per trainable parameter: BF16 weights (2 bytes), a BF16 gradient (2), an FP32 master weight (4), and FP32 first and second moment estimates (4 each) —

16 bytes per trainable parameter.16 \text{ bytes per trainable parameter}.

Assumptions

Adam or AdamW with the standard two-moment state; BF16 or FP16 compute with an FP32 master copy, the usual mixed-precision recipe. Variants change the constant and not the structure: plain SGD with momentum needs 4 bytes of state instead of 8, 8-bit optimizers quantize the moments to 1 byte each, and pure FP32 training drops the master copy while doubling the others.

CorollaryWhere LoRA's memory saving actually comes from

Freezing a base model of PP parameters and training an adapter of Pa≪PP_a \ll P reduces persistent state from 16P16P to 2P+16Pa2P + 16P_a bytes — a saving of 14P−16Pa≈14P14P - 16P_a \approx 14P — and leaves activation memory unchanged.

Activations

PropositionActivation memory scales with batch, sequence and depth

For a transformer with LL layers, width dd, batch BB and sequence length SS, the activations retained for backpropagation occupy

Mact=Θ(L B S d) bytes,M_{\text{act}} = \Theta\big(L\,B\,S\,d\big) \text{ bytes},

with a constant depending on how many intermediate tensors per layer are kept, and an additional Θ(L B S2 nheads)\Theta(L\,B\,S^2\,n_{\text{heads}}) term if attention probability matrices are materialised.

Note that PP does not appear: activation memory is independent of parameter count and depends instead on the shape of the input. This is why the same model trains fine at S=512S=512 and fails at S=8192S = 8192.

Checkpointing

Gradient checkpointing stores activations only at selected layer boundaries and recomputes the rest during the backward pass.

TheoremOptimal uniform checkpointing uses √L segments

Partition LL layers into consecutive segments of kk layers. Store the activation at each segment boundary, and during backpropagation recompute a segment's interior from its stored boundary. Peak activation storage, in units of one layer's activations, is

m(k)=Lk+k,m(k) = \frac{L}{k} + k ,

minimised over real k > 0 at k⋆=Lk^\star = \sqrt L with m(k⋆)=2Lm(k^\star) = 2\sqrt L, at the cost of one additional forward pass.

Key idea

The trade is Θ(L)\Theta(L) memory for Θ(L)\Theta(\sqrt L) memory at the price of roughly 33%33\% more compute — one extra forward pass against the usual forward-plus-backward cost of about three forward passes. For L=32L = 32 this is 232≈112\sqrt{32} \approx 11 layers' worth of activations instead of 32, a 2.8×2.8\times reduction. For L=80L = 80 it is 8.9×8.9\times against 8080 — the deeper the model, the better the deal.

What this does not say

Every count here is of tensors the algorithm requires. Real allocators add fragmentation, workspace for fused kernels, communication buffers, and framework overhead that together commonly add tens of percent. Use these formulas to decide whether a configuration is plausible, then measure.

References

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Problems (3)

All problems