AI Grimoire
Sheet
statusstandard
difficultyintroductory
timeO(L·B·T·d)
memoryO(L·B·T·d)
described2022
revisedtoday

Activation Memory Accounting

Backpropagation needs the forward pass’s intermediates. There are a lot of them, they scale with batch and sequence rather than with parameters, and one term is quadratic.

[memory][training][core]Current standard

Standing

Current standardPart of the default recipe. Present in most frontier models trained today, and the thing a new design departs from rather than argues for.

The arithmetic every memory technique on this branch is trying to reduce. Worth doing on paper before reaching for any of them.

judged as of 2026-09 · what the labels mean

Theory

Training memory has four occupants. Parameters, gradients and optimiser state scale with NN and are fixed for a given model. Activations scale with BTB \cdot T and are the term you control.

They exist because backpropagation is the chain rule: computing L/W\partial \mathcal{L} / \partial W for a layer requires that layer’s input, so every input consumed on the way forward must survive until the backward pass reaches it.

Mlayer=sbh(34+5ash)M_{\text{layer}} = s\,b\,h\left(34 + 5\,\frac{a \cdot s}{h}\right)
eq. 1 — Korthikanti et al., per layer, in bytes for 16-bit

with ss sequence length, bb batch, hh hidden size, aa heads. Two terms, and the second is the one that matters: it carries s2s^2, because the attention score matrix is [b,a,s,s][b, a, s, s] and is stored per layer.

Where the 34 comes from

Worth unpacking once, because it explains which techniques pay.

KeptBytes per token
layernorm input, ×24h
QKV input2h
Q, K, V6h
attention output2h
attention projection input2h
FFN input2h
FFN hidden, ×4 width8h
GELU input8h
dropout masks2h

Nothing is individually large. There are simply about ten of them, and then LL layers of it.

The quadratic term

5as2b5 a s^2 b per layer. At s=4096s = 4096, a=32a = 32, b=4b = 4 that is 2.7 GB per layer, against roughly 0.6 GB for everything else — so the score matrices are most of the activation memory and all of the scaling problem.

FlashAttention deletes the term. It computes attention in tiles and never writes the s×ss \times s matrix to HBM, storing only the log-sum-exp statistics needed to recompute it on the backward pass. Memory becomes linear in ss.

This is why the entry begins here rather than with recomputation: everything else on this branch is trading arithmetic for memory at some cost, and this one is free.

M34LsbhM \approx 34\, L\, s\, b\, h
eq. 2 — what remains once the quadratic term is gone

Implementation

python
def activation_memory(
    layers: int, hidden: int, heads: int, seq: int, batch: int,
    flash: bool = True, checkpointing: str = "none",
) -> dict[str, float]:
    """Bytes of stored activations. Korthikanti et al. eq. 2, in GB."""
    linear = 34 * seq * batch * hidden
    quadratic = 0 if flash else 5 * heads * seq**2 * batch

    per_layer = linear + quadratic
    total = {
        "none": layers * per_layer,
        # Full: only the block input survives; 2·s·b·h per layer.
        "full": layers * 2 * seq * batch * hidden,
        # Selective: recompute the attention block, keep the rest.
        "selective": layers * (linear - 11 * seq * batch * hidden) + layers * quadratic * 0,
    }[checkpointing]

    return {"gb": total / 1e9, "per_layer_mb": per_layer / 1e6,
            "quadratic_share": quadratic / per_layer if per_layer else 0}

What each lever does

Having the formula makes the options comparable rather than a list of tricks.

Batch size divides everything, and micro-batching with gradient accumulation buys the reduction without changing the optimiser’s view of the batch.

Sequence length divides the linear term and quarters the quadratic one, if you still have it.

Recomputation trades the 34 for a repeated forward pass; selective recomputation targets the largest contributors and pays about a tenth of the compute for most of the saving.

Offloading moves them off the device entirely, bounded by PCIe rather than by arithmetic.

Notably absent: ZeRO, which shards parameters, gradients and optimiser state and does nothing at all for activations. The two address different occupants and are routinely confused.

Weights + optimiser
112 GB
Activations, no tricks
≈ 45 GB
Attention term share
≈ 60%
7B model, batch 4, seq 4096, bf16

Related

References

[1]Korthikanti et al. — Reducing Activation Recomputation in Large Transformer Models (2022)arXiv:2205.05198
[2]Rajbhandari et al. — ZeRO: Memory Optimizations Toward Training Trillion Parameter Models (2019)arXiv:1910.02054
[3]Dao et al. — FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (2022)arXiv:2205.14135