Linear algebra for LLMsPart 8: Gradients through matrices
Training cost
6 min
6N FLOPs per token
Forward: one multiply-add per weight per token (Part 2). Backward: each matmul becomes two of the same size, one for the input gradient and one for the weight gradient (two decks ago). Attention over the context adds a little more; is the standard estimate.
Write
A whole run
is the number of training tokens. A 7B model on 2 trillion tokens takes about FLOPs.
Write
Into GPU-hours
Assume each GPU sustains 400 TFLOP/s of useful work, a plausible fraction of a modern accelerator’s BF16 peak. Real runs vary a lot with utilization, so treat this as an order of magnitude.
Write
Memory per parameter
| What | Bytes per parameter |
|---|---|
| BF16 weights | 2 |
| BF16 gradients | 2 |
| FP32 master weights | 4 |
| Adam’s two FP32 moments | 8 |
| total | 16 |
Standard mixed-precision training with Adam keeps about 16 bytes per parameter, before activations: 7B parameters need about 112 GB just for this. That’s why training is spread across many GPUs.
Write
✏️ Your turn
Size a run
How many training FLOPs? How much memory for weights, gradients and Adam state?
Work it out on paper, then go to the next card to check.
Answer
Inference is cheaper
In a transformer
Generating a token takes only the forward pass, FLOPs, and no gradients or optimizer state: weights (and the KV cache) are all that must sit in memory. Part 9 shows why decoding is limited by reading those weights, not by the FLOPs.
📌 Recap
Training cost in one card
- FLOPs per training token; for a run.
- Time / (GPUs × sustained FLOP/s).
- Mixed-precision Adam: about 16 bytes per parameter before activations.
Write