CourseTransformers: From Attention to Trained, Served and Interpreted Models · Module 2: Self-Attention Mechanics · part 12 of 111
Part 12 · Module 2: Self-Attention Mechanics

Module 2: Self-Attention Mechanics

1 min read·

What you need first

  • Module 1 and its lab (tinyformer-lab at v0.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

FileTitleMinutesDepth tags
01-scaled-dot-product-attention.mdLesson 1: Scaled dot-product attention, derived and built by hand50Essential, Recommended
02-the-scaling-factor.mdLesson 2: The scaling factor, derived, measured and removed45Essential, Recommended
03-multi-head-attention.mdLesson 3: Multi-head attention, split, joined and switched off50Essential, Recommended, Optional
04-masking.mdLesson 4: Masking, built, tested and broken on purpose50Essential, Recommended
05-head-variants-and-attention-cost.mdLesson 5: Head variants and what attention really costs55Essential, 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.