Transformers from the Weights Up/Part 6 of 20
Transformers from the Weights Up, Part 6: Training: cross-entropy, backprop, AdamW
Training is forward, cross-entropy on the next token, one backward sweep, and an AdamW step. That is where the 6N FLOPs and the 16 bytes per parameter come from.
4 Nov 2026
- 1
Forward
Tokens [B, T] → logits [B, T, 128256]. Same computation as inference, except every position is scored, not just the last, and intermediate activations are kept for the backward pass.
- 2
Loss
Targets are the inputs shifted left by one: position t is graded on token t+1 (image 8). Average −log p(target) over all B·T positions.
- 3
Backward
Reverse-mode autodiff from the scalar loss: one sweep yields ∂L/∂θ for all 8,030,261,248 parameters, at about twice the forward cost.
- 4
Update
AdamW turns each gradient into a step using its running mean m and mean square v, then applies decoupled weight decay. Zero the gradients and repeat.
Cross-entropy on next-token targets
For one position with logits z ∈ ℝ^V and target id y, the loss is the negative log of the probability the model gave the right token. Expanding the softmax gives a form that is also how it is computed stably (log-sum-exp with the max subtracted):
That gradient is the whole story at the top of the network: the predicted distribution minus the one-hot target. Every wrong token is pushed down in proportion to the probability it stole, the right token is pushed up by 1 − pᵧ, and the entries always sum to zero, because softmax can only move probability around. Over Llama-3-8B's vocabulary it is a [128256] vector per position, or [B, T, 128256] per batch.
- Step
- 0
- Loss ℓ = −ln p(mat)
- 1.7918 nats
- Perplexity e^ℓ
- 6.000
From the logits the error enters the weights. With final hidden states H [T, 4096] and G = P − Y [T, 128256], lm_head gets dW_U = Gᵀ H ([128256, 4096], the same shape as the tensor) and the residual stream gets dH = G W_U ([T, 4096]), which is then carried down through all 32 blocks. The same mechanism trains Lesson 5's memories: for FFN slot i, the gradient on its value column is dW_down[:, i] = Σₜ aᵢ(xₜ) · δₜ, where δₜ is the error arriving at the FFN output. Only slots that fired are edited, in proportion to how hard they fired. Gradient descent writes key–value memories a tiny rank-T update at a time; ROME just does one on purpose.
Backprop is reverse-mode autodiff
The forward pass is a directed acyclic graph of primitive ops (matmul, add, SiLU, RMSNorm, softmax). The chain rule says the derivative of the scalar loss with respect to any node is the sum, over the node's consumers, of the consumer's gradient times the local derivative. Reverse mode evaluates that from the loss backwards, so one sweep gives the gradient for every input. Forward-mode differentiation would need one pass per parameter: 8 billion of them.
Backprop through a matmul: where 6N comes from
Apply the multiply rule to a whole matrix. For Y = XW with X [T, K], W [K, N] and upstream gradient dY [T, N], matching shapes forces the answer:
Each is a matmul with the same three dimensions as the forward product, so each costs the same 2TKN FLOPs. Forward 2, backward 4: Lesson 4's 6N FLOPs per token, now derived rather than asserted. dW sums over all T tokens in the batch, which is why bigger batches give a less noisy gradient for the same weight read. For Llama-3-8B, 6 × 8.03B ≈ 4.8 × 10¹⁰ FLOPs per training token, ignoring attention's context-length term.
- This product
- 2 × 4,096 × 4,096 × 14,336 = 0.481 TFLOP
- Forward + backward for this layer
- 1.443 TFLOP = 6 × 58.72M weights × 4,096 tokens
A useful sanity check on the rule: the Llama 3 paper reports its 405B flagship used 3.8 × 10²⁵ training FLOPs on 15.6T tokens. 6 × 405 × 10⁹ × 15.6 × 10¹² = 3.79 × 10²⁵. The same rule gives the 8B, on its "about 15T" tokens, ≈ 7.2 × 10²³ (Lesson 4). The paper also reports 38–43% BF16 model-FLOPs utilization for the 405B on H100s; at 40% of the H100 SXM's 989 TFLOP/s, 7.2 × 10²³ FLOPs is roughly 5 × 10⁵ GPU-hours. That last figure is our estimate, not a reported number.
AdamW: the update rule and its state
Plain SGD steps θ ← θ − ηg. Adam (Kingma and Ba, 2014) keeps two exponential moving averages per parameter, a mean m and an uncentered variance v, and divides one by the square root of the other. AdamW (Loshchilov and Hutter, 2017) changes one line: weight decay is applied directly to θ instead of being added to g.
| Piece | What it does | Typical value |
|---|---|---|
| m (first moment) | Momentum: averages gradient direction over ~1/(1 − β₁) steps, cancelling noise and oscillation | β₁ = 0.9 (≈ 10 steps) |
| v (second moment) | Per-parameter RMS gradient. Dividing by √v makes the step size roughly η for every weight, whatever its gradient scale | β₂ = 0.95 is common for LLMs (GPT-3 used it); the Adam paper's default is 0.999 |
| Bias correction | m and v start at 0 and would be too small early; dividing by 1 − βᵗ fixes it. At t = 1, m̂ = g and v̂ = g², so the first step is η·sign(g) | automatic |
| ε | Guards the division when v ≈ 0 | 1e-8 (Adam paper default) |
| Decoupled decay λ | Shrinks every weight by ηλθ per step, uniformly. Added to g instead, it would be divided by √v̂ and weights with large gradients would barely be regularized | Llama 3's scaling-law runs: decay = 0.1 × the learning rate at each step |
- SGD (η = 0.45)
- step 0: (-7.00, -2.20), loss 2.17e+1
- SGD + momentum 0.9
- step 0: (-7.00, -2.20), loss 2.17e+1
- Adam (η = 0.25)
- step 0: (-7.00, -2.20), loss 2.17e+1
Training memory: 16 bytes per parameter
Serving Llama-3-8B in bf16 needs 2 bytes per parameter: 16.06 GB. Training it with mixed-precision AdamW keeps five copies' worth of state for every weight, and none of it can be streamed away because every step touches all of it.
| Tensor | Precision | Bytes/param | Llama-3-8B |
|---|---|---|---|
| Weights used in forward/backward | bf16 | 2 | 16.06 GB |
| Gradients | bf16 | 2 | 16.06 GB |
| Master copy of weights (what AdamW updates) | fp32 | 4 | 32.12 GB |
| Adam m | fp32 | 4 | 32.12 GB |
| Adam v | fp32 | 4 | 32.12 GB |
| Total model states | 16 | 128.48 GB |
Why an fp32 master copy? Adam moves each weight by roughly η per step. Late in a cosine schedule η is 10⁻⁵ or smaller, while a bf16 weight near 0.01 has only 8 significant bits (Lesson 1's bf16 panel), so neighbouring representable values are about 6 × 10⁻⁵ apart: the update would round to zero. The update is applied in fp32 and the bf16 copy is re-cast from it. Llama 3 goes further and accumulates gradients across micro-batches and reduces them across data-parallel workers in FP32, for numerical stability.
- Training, mixed-precision AdamW
- 16 bytes/param × 8.03B = 128.5 GB
- Per GPU
- 128.5 GB: does not fit
- vs serving in bf16
- 8× the 16.06 GB inference footprint
Lesson 6: key numbers
- 11.76
- nats of loss at uniform initln 128,256; perplexity 128,256
- p − y
- ∂ℓ/∂logits[128256] per position, sums to 0
- 6N
- training FLOPs per token2N forward + 4N backward; ≈ 4.8e10 for 8B
- ≈ 8N
- with activation checkpointingone recomputed forward
- 16 B
- per param, mixed-precision AdamW2 + 2 + 4 + 4 + 4 (ZeRO's 2Ψ + 2Ψ + 12Ψ)
- 128.5 GB
- Llama-3-8B model statesvs 16.06 GB to serve in bf16
- 3 × 10⁻⁴
- Llama 3 8B peak learning rateLlama 3 paper, Table 3
- 0.9 / 0.95
- common β₁ / β₂ for LLMsAdam paper default β₂ = 0.999
Lesson 6: self-check
1. A freshly initialized Llama-3-8B gives near-uniform next-token probabilities. What training loss should the first steps report?
2. At one position the target token has p = 0.7 and another token has p = 0.2. What are the gradients on those two logits?
3. Why is the backward pass of a linear layer about twice the forward cost?
4. Two weights have gradients of 1e-6 and 10 on the very first step. How far does Adam move each?
5. Why does AdamW apply weight decay to θ directly instead of adding λθ to the gradient?
6. You want to fully fine-tune Llama-3-8B with mixed-precision AdamW on 80 GB GPUs. What is the floor on GPU count from model states alone?
7. Gradients for an FFN value column vᵢ (Lesson 5) are Σₜ aᵢ(xₜ)·δₜ. What does that say about which memories a batch edits?
Lesson 6: resources
- The spelled-out intro to neural networks and backpropagation: building microgradYouTube · Andrej Karpathy
- Adam: A Method for Stochastic OptimizationKingma & Ba, 2014 (arXiv)
- Decoupled Weight Decay Regularization (AdamW)Loshchilov & Hutter, 2017 (arXiv)
- ZeRO: Memory Optimizations Toward Training Trillion Parameter ModelsRajbhandari et al., 2019 (arXiv)
- The Llama 3 Herd of ModelsLlama Team, Meta, 2024 (arXiv)