Md. Asif Uddin
    Problem I.6.B05

    What the backward pass costs, in arithmetic and in memory

    complexity▲▲△

    GFLOP to two decimals, GB to three, at 4 bytes per fp32 value.

    STATEMENT

    Account for both resources a backward pass consumes. Show that the arithmetic is a small multiple of the forward pass and that the memory is not, find the batch size at which stored activations overtake parameters, and quantify what checkpointing buys.

    GIVEN

    A stack of L=48L = 48 square affine layers of width d=1024d = 1024, in fp32. Two working regimes: a batch of 32 rows, and a batch of 32 sequences of length 512, which presents 32×512=16 38432 \times 512 = 16\,384 rows to every layer.

    FIND

    Forward and backward FLOPs and their ratio; activation memory against parameter memory in both regimes; the batch at which the two are equal; and the memory and arithmetic under L\sqrt{L} checkpointing.

    STRATEGY

    Count matrix products, since everything else is lower order. Forward, each layer does one. Backward, by (I.6.3) and (I.6.4), each layer does two — one to send the adjoint onward and one to form the weight gradient. For memory, count what the backward pass will read and cannot recompute for free.

    SOLUTION

    Step 1 — arithmetic. A product of a B×dB \times d matrix with a d×dd \times d matrix costs 2Bd22Bd^2 floating-point operations, counting a multiply and an add for each of the Bd2Bd^2 pairs. Forward:

    F=L⋅2Bd2F = L \cdot 2Bd^2

    Backward does two such products per layer, so 2F2F, and the step costs 3F3F:

    regimeforwardbackwardtotal / forward
    batch 323.223.22 GFLOP6.446.44 GFLOP3.0×3.0\times
    batch 32×51232 \times 5121649.271649.27 GFLOP3298.533298.53 GFLOP3.0×3.0\times

    The ratio is 3 in both regimes and would be 3 for any BB, dd and LL: it is a property of the graph, not of the size. This is the cheap gradient principle (I.6.T1) at work, and the constant here — three — sits comfortably inside the c≤4c \le 4 the theorem allows.

    Step 2 — memory. The parameters are Ld2=50 331 648Ld^2 = 50\,331\,648 values, 0.2010.201 GB. The activations kept for the backward pass are LBdLBd by (I.6.7):

    regimeactivationsparametersratio
    batch 320.0060.006 GB0.2010.201 GB0.03×0.03\times
    batch 32×51232 \times 5123.2213.221 GB0.2010.201 GB16.00×16.00\times

    The same model, the same weights, the same arithmetic per row — and the memory picture inverts completely. At batch 32 the parameters dominate by thirty times; at batch 16 384 the activations dominate by sixteen.

    Step 3 — the crossover. Setting LBd=Ld2LBd = Ld^2 gives

    B=d=1024B = d = 1024

    The layer count cancels, and so does everything else. Activations overtake parameters when the batch reaches the width, which for this model is 1024 rows — a number a single sequence of moderate length already exceeds. That is why the second regime is the normal one and the first is the exception.

    Step 4 — checkpointing. Keep every L\sqrt{L}-th activation and recompute the rest during the backward pass. With L=48L = 48, 48≈7\sqrt{48} \approx 7: hold 7 segment boundaries, plus the 6 tensors of whichever segment is currently being recomputed, so 13 tensors instead of 48.

    3.221 GB⟶0.872 GB,3.7× less3.221\ \text{GB} \longrightarrow 0.872\ \text{GB}, \qquad 3.7\times \text{ less}

    The price is recomputing each segment’s forward pass once, which adds one forward pass to a step that cost three:

    2F+2FF+2F=43=1.33×\frac{2F + 2F}{F + 2F} = \frac{4}{3} = 1.33\times

    A third more arithmetic for nearly four times less memory. On a device where memory is the binding constraint and arithmetic is not — which describes most training — that is not a close decision.

    Answer

    Arithmetic: backward is exactly twice forward, so a step costs 3×3\times a forward pass, independent of BB, dd and LL.

    Memory: activations are LBdLBd and parameters Ld2Ld^2, so they are equal when B=d=1024B = d = 1024. At batch 32 activations are 0.03×0.03\times the parameters; at batch 32×51232 \times 512 they are 16×16\times — 3.2213.221 GB against 0.2010.201 GB.

    Checkpointing at 7 segments holds 13 tensors instead of 48: 3.2213.221 GB becomes 0.8720.872 GB, a 3.7×3.7\times reduction, for 1.33×1.33\times the arithmetic.

    Check — sanity

    The FLOP ratio is exactly 3 in both regimes. It has to be — it is a count of matrix products per layer, one forward and two back, and no dimension enters it. A measured ratio far from 3 on real hardware is a statement about memory bandwidth and kernel efficiency, not about the arithmetic.

    The crossover has no free parameters. LBd=Ld2LBd = Ld^2 reduces to B=dB = d with LL cancelling, so the answer cannot depend on depth. A deeper model has more activations and more parameters, in the same proportion.

    Checkpointed memory is near 2L2\sqrt{L}. 248≈13.92\sqrt{48} \approx 13.9 against the 13 tensors counted, and 48/13=3.748/13 = 3.7 matches the measured reduction. The L\sqrt{L} of (I.6.7) is where boundary storage and segment storage balance; away from it, one term or the other grows.

    Where this breaks

    Every number here counts one thing each and ignores the rest of the bill. The gradient buffer is another copy of the parameters, and Adam adds two more, so the parameter side of the ledger is nearer 4×0.2014 \times 0.201 GB than 0.2010.201 GB before any activation is stored — a point I.7 makes properly. Workspace for the matrix kernels, fragmentation and the framework’s own overhead are all real and none is counted.

    The FLOP count is also not a time. It assumes every operation costs the same, which on a device with a hundred times more arithmetic throughput than memory bandwidth is badly wrong: the backward pass touches more memory per FLOP than the forward pass and is routinely slower than the 2×2\times its arithmetic suggests. The 3×3\times is a lower bound on the time ratio and an exact statement about the arithmetic, and confusing the two is how a training run comes in over its estimate.

    Finally, checkpointing assumes recomputation is deterministic. A segment containing dropout, or any other sampling, recomputes a different forward pass unless the random state is stored and restored — and the gradient then belongs to neither the first forward pass nor the second.

    Variation

    Recompute the crossover for a model whose layers are not square: width dd in and 4d4d out, as a transformer’s feed-forward block is. The parameter count gains a factor of 4 and the stored activation gains one too. Does the crossover move?

    Draws on