Attention

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

From Fixed-Size State Bottlenecks to Direct Lookup

Motivation

  • The Recurrent Bottleneck: A model of width d = 768 (GPT-2 small) reading n = 1{,}024 tokens squeezes 1{,}024 \times 768 \approx 786{,}000 numbers of history into 768 floats across an O(n)-step gradient chain.
  • Content-Based Retrieval: Keep one representation at every position and retrieve relevant context on demand.
  • Differentiable Soft Lookup: Replace discontinuous database matching with a softmax-weighted average of stored values, trainable end to end by backpropagation.

The Core Primitive: Queries, Keys, and Values

Core Mechanism

  • Three Distinct Roles: Each lookup compares a query \mathbf{q} \in \mathbb{R}^d (what the current position seeks) against m keys \mathbf{k}_i \in \mathbb{R}^d (what each candidate position offers).
  • Convex Value Mixing: Compatibility scores a(\mathbf{q}, \mathbf{k}_i) pass through a softmax to form nonnegative weights \alpha_i summing to 1: \textrm{Attention}(\mathbf{q}, \mathcal{D}) = \sum_{i=1}^m \alpha(\mathbf{q}, \mathbf{k}_i)\,\mathbf{v}_i
  • Parallel Heads: One lookup forms an attention head; running H heads side by side lets a token retrieve multiple arguments at once.

Soft lookup scores a query against keys and returns a convex combination of values.

Three Challenges Created by All-Pairs Lookup

Architectural Trade-Offs

  • 1. Order Blindness: A weighted sum \sum_i \alpha_i \mathbf{v}_i is permutation equivariant and ignores word order, requiring absolute tables, RoPE, or ALiBi (§10.4).
  • 2. Quadratic Cost: Scoring all pairs of n tokens costs \Theta(n^2 d) FLOPs and O(n^2) naive memory (64\text{ GiB} at n=128\text{K}), motivating FlashAttention (§10.5), windows, and linear attention (§10.6).
  • 3. Mechanistic Opacity: Attention weights show where a head looks, not what function it computes across the residual stream via QK/OV circuits (§10.7).

Architecture and Roadmap of Chapter 10

Chapter Roadmap

  • Part 1: Build the Layer (§§10.1–10.3): Soft key–value lookup, Nadaraya–Watson kernel pooling, 1/\sqrt{d} scaled dot-product scoring, boolean mask algebra, and multi-head & grouped-query attention.
  • Part 2: Sequence Order (§10.4): Permutation equivariance, sinusoidal rotations, RoPE, ALiBi, and zero-shot context extension.
  • Part 3 & Part 4: Scale & Circuits (§§10.5–10.7): Blockwise online softmax (FlashAttention), sliding windows, linear attention recurrences, and two-layer induction heads.

Running Benchmarks and Study Guide

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.