Module 2: Self-Attention Mechanics
1 min read·
What you need first
- Module 1 and its lab (
tinyformer-labatv0.1.0; the lab rebuilds a stand-in if needed) - Tensors, matrix products and softmax (Module 1, Lesson 1)
- After AI Engineering Module 6, Lesson 1, skim section 1 of Lessons 1 and 3
- No GPU, no API key
What you will build
tinyformer/attention.py with seven tests and a three-seed race of 4, 2 and 1 key/value heads, tagged v0.2.0.
Lessons
| File | Title | Minutes | Depth tags |
|---|---|---|---|
01-scaled-dot-product-attention.md | Lesson 1: Scaled dot-product attention, derived and built by hand | 50 | Essential, Recommended |
02-the-scaling-factor.md | Lesson 2: The scaling factor, derived, measured and removed | 45 | Essential, Recommended |
03-multi-head-attention.md | Lesson 3: Multi-head attention, split, joined and switched off | 50 | Essential, Recommended, Optional |
04-masking.md | Lesson 4: Masking, built, tested and broken on purpose | 50 | Essential, Recommended |
05-head-variants-and-attention-cost.md | Lesson 5: Head variants and what attention really costs | 55 | Essential, Recommended, Optional |
Extras
06-module-lab.md: Lab: Package attention and test every claim (60 minutes). The lessons' code as one tested file, plus a seeded race.07-incident-file.md: Incident: A scale factor from the wrong width (35 minutes). Gemma 2 9B's scale from the wrong width (llama.cpp #8444 and #8473, July 2024), caught with a reference-output test.