Dive into Deep Learning · §10
Attention Mechanisms: Differentiable Content-Based Retrieval
queries, keys & values · scaled dot-product · multi-head & GQA · RoPE & ALiBi · FlashAttention · linear attention · circuits
Motivation
Core Mechanism
Architectural Trade-Offs
Chapter Roadmap
Pedagogical Thread
Every section tests its claims on an executable benchmark and counts exact FLOPs and bytes on a reference GPT-2 small head (d = 768, H = 12, d_h = 64):
| Section | Executable Benchmark / Model | Key Quantitative Takeaway |
|---|---|---|
| §10.1–10.2 | Nadaraya–Watson 1D regression; ToyAligner permutation task |
Bandwidth \sigma and 1/\sqrt{d} act as softmax temperatures preventing gradient saturation |
| §10.3 | “Alice gave Bob the book”; Gaussian copy-both task | One head incurs \ge 1/2 relative MSE; two heads reach \sim 10^{-7} at identical 4d^2 params |
| §10.4 | TinyCharLM on WikiText-103 bytes (n = 128 \to 512) |
RoPE gives lowest in-range perplexity (7.3); slow-pair interpolation repairs 4\times context |
| §10.5–10.6 | Chunked online softmax; fused Flash/FlexAttention up to n = 32\text{K} | Online softmax cuts memory from O(n^2) to O(n); window w = 128 gives > 30\times speedup |
| §10.7 | TinyCharLM on variable-period repeated random patterns |
Two blocks learn in-context induction (\sim 90\% accuracy); one block stalls at \sim 10\% |
Fast track to Chapter 11 (Transformers): Read §10.1–10.4 and the residual-stream opening of §10.7; study §10.5–10.6 alongside KV caching and modern RNNs.