Summary

This is the resource accounting lecture: how to predict, before running anything, how much memory a model needs and how long a computation will take. Percy Liang works bottom-up from tensors and floating point formats through FLOP counting, arithmetic intensity, and roofline analysis, then applies all of it to the memory and compute cost of a training step. einops is introduced along the way as the notation used for tensor operations in the course. The lecture ends with two standard memory-reduction techniques, gradient accumulation and activation checkpointing.

Key Takeaways

  • Two motivating back-of-the-envelope calculations: training a 70B model on 15T tokens with 1024 H100s at 0.5 MFU takes about 143 days via the 6ND formula; and with AdamW at 2+2+4+4 = 12 bytes per parameter, 8 H100s with 80GB each fit roughly a 53B-parameter model before counting activations.
  • Precision: fp32 spends 8 bits on the exponent, fp16 only 5, which causes underflow, overflow, and NaNs in training. bf16 keeps fp32’s dynamic range in 16 bits by moving bits from mantissa to exponent, trading resolution that deep learning does not need. Mixed precision uses bf16 for parameters, activations, and gradients with fp32 optimizer state. fp8 is standardized, and nvfp4 gets to 4 bits by scaling per block rather than per value — Nemotron 3 Super was trained in fp4.
  • FLOPs (work done) and FLOP/s (hardware speed) are distinct, and vendor spec sheets quote the sparsity number, so dense figures are half the headline — about 989 TFLOP/s bf16 for an H100.
  • A B×D by D×K matmul costs 2·B·D·K FLOPs, which equals 2 × tokens × parameters. The backward pass costs exactly twice the forward pass because it computes gradients both with respect to the input and with respect to the parameters, giving the familiar 6ND total. This approximation holds for transformers as long as the context length is not large enough for the quadratic attention term to dominate.
  • MFU is actual FLOP/s over spec FLOP/s. About 0.5 is a good result for a real model, a bare matmul might reach 0.8, and 0.1 means something is wrong.
  • Arithmetic intensity is FLOPs per byte moved, and it is compared against the accelerator’s own intensity — roughly 295 FLOPs per byte for an H100. ReLU is about 0.25, GELU about 5, a dot product about 0.5, and a matrix-vector product barely above that, so all are memory bound; matrix-matrix multiplication is about n/3 and is compute bound. This is why large matrices and large batches matter: below the accelerator’s intensity, shrinking the work does not make it faster.
  • The same analysis explains why inference is hard: decoding generates one token at a time, which is a matrix-vector product and therefore memory bound, while training processes a whole sequence at once and stays compute bound.
  • Training memory breaks down as parameters and gradients at 2 bytes each in bf16, activations at 2·B·D·L, and fp32 optimizer state — 4 bytes per parameter for AdaGrad’s second moments, 8 for Adam’s two moments. Optimizer state is rarely a speed bottleneck but caps the model size that fits.
  • Gradient accumulation splits a batch into micro-batches and sums gradients before stepping, which keeps activation memory bounded at large effective batch sizes. Activation checkpointing recomputes activations in the backward pass instead of storing them; checkpointing every √L layers balances √L activation memory against √L recompute overhead.
  • einops replaces positional indexing with named dimensions: einsum is a generalized matmul where unmentioned dimensions are summed out, reduce generalizes sum/mean/max/min, and rearrange splits or merges dimensions. It removes the transpose bookkeeping that makes backward-pass derivations error-prone, and it is sugar over the same primitives rather than a speedup.

Extracted Notes

  • None yet.

Parent Source