Linear algebra for LLMsPart 10: Capstone and reference
Llama 3 8B, end to end
14 min
The spec sheet
Everything on the following cards follows from these seven numbers (from the model’s config and Meta’s model card). Copy them at the top of a fresh page; you’ll use every one.
Write
1. Token ids → vectors
Each id picks one row of the embedding table (Part 1). No arithmetic, just a lookup. This vector starts the residual stream (Part 6).
2. Normalize, then Q, K, V
RMSNorm divides each row by its root mean square and applies a gain (Part 6). Then three matrix products. and are narrower because of GQA: 8 key/value heads of 128 instead of 32 (Part 5).
Write
3. Split into heads, rotate
view then transpose(1, 2), never a bare reshape (Part 4). RoPE then rotates each pair of entries in and by an angle that depends on the position (Part 6), so the scores will depend on relative position. Each K/V head serves 4 query heads.
4. Scores, mask, softmax
A dot product for every (query, key) pair in every head, divided by so the scores don’t saturate the softmax (Part 1). The causal mask puts above the diagonal, and the softmax runs along each row (Part 5).
Write
5. Mix values, merge heads, project
Each output row is a weighted average of value rows (Part 5). Transpose back, merge the heads into one 4096-wide row, multiply by , and add to the residual stream.
6. The MLP
Normalize again, widen to 14,336 twice, gate one with the other elementwise, and project back down (SwiGLU, Part 6). Add to the residual stream. That’s one block; there are 32.
Write
7. Logits and the next token
A final norm, then one score per vocabulary token. When generating, only the last row matters: its softmax is the distribution the next token is sampled from. In training, every row is scored against the actual next token with cross-entropy (Part 8).
✏️ Your turn
Count it
Total parameters? (You did this in Part 6.)
Work it out on paper, then go to the next card to check.
Answer: 8.03 billion
Exactly 8,030,261,248, counting the final norm.
✏️ Your turn
Memory to serve one full-length sequence
Weights plus KV cache, in GB?
Work it out on paper, then go to the next card to check.
Answer: about 17.1 GB
Fits on one 80 GB GPU with plenty of room for a batch: each extra full-length sequence adds only 1 GiB of cache, thanks to GQA. Without GQA (32 K/V heads) it would be 4 GiB each.
✏️ Your turn
Decode speed at full context
What’s the most tokens per second for that one sequence?
Work it out on paper, then go to the next card to check.
Answer: about 196
Every token reads all the weights and the whole cache once (Part 9). Batching 32 sequences reads the weights once for all of them, so total throughput rises far more than the per-sequence speed falls.
Prefill: compute, not memory
Processing an 8,192-token prompt: the weight matmuls cost per token. Scores and mixing each cost per layer, on top. At full context, attention adds about 27% (half of it is masked, but simple kernels compute it anyway); at 1,000 tokens, about 3%. About FLOPs in all: at 989 TFLOP/s, at least 0.17 s.
Write
✏️ Your turn
Training compute
Estimate the training FLOPs. Meta reports 1.3 million H100 GPU-hours; what sustained FLOP/s per GPU does that imply?
Work it out on paper, then go to the next card to check.
Answer
About 150 TFLOP/s of counted work per GPU, roughly 15% of the dense BF16 peak. leaves out attention and everything that isn’t a training step, and Meta’s token count is “over 15T”, so read this as an order-of-magnitude check, not a utilization figure.
✏️ Your turn
A LoRA adapter on the MLP
Part 7 put adapters on and . Now adapt the three MLP matrices instead. How many trainable parameters? What fraction of the model?
Work it out on paper, then go to the next card to check.
Answer: 28.3 million, 0.35%
per adapted matrix, the same for since only the sum of the sides matters. About 4× Part 7’s attention-only adapter, still tiny: 57 MB in BF16, small enough to keep one per customer and swap at serving time.
📌 Recap
The whole model on one page
- Lookup → 32 × (norm, QKV, RoPE, scores, mask, softmax, mix, , add; norm, SwiGLU, add) → norm → logits.
- 8.03B parameters; 16 GB in BF16; KV 128 KiB per token.
- Decode ≈ bandwidth / bytes per token; prefill ≈ ; training ≈ .
- LoRA: per matrix.
Write
Sources
- Meta Llama 3 model card (github.com/meta-llama/llama3, MODEL_CARD.md): 8B and 70B, context 8k, GQA in all models, pretrained on over 15 trillion tokens, 1.3M H100-80GB GPU-hours for 8B.
- Meta-Llama-3-8B
config.json: hidden size 4,096, 32 layers, 32 attention heads, 8 key/value heads, intermediate size 14,336, vocabulary 128,256. - NVIDIA H100 datasheet: 3.35 TB/s (SXM), about 989 TFLOPS dense BF16.