Multi-Head Subspaces: Why and How Heads Divide d_model
Agent-Assisted Engineering Check
Prerequisites
Day 43-44 scaled dot-product attention and causal masking.
Learning Objective
Explain the representational necessity of multi-head attention, define linear projections W^Q, W^K, W^V, W^O, and verify parameter ledgers for tiny and standard configurations.
Instructions
Required work: explain why a single attention head compresses multiple distinct relationship types (e.g., syntactic dependency vs semantic topic) into a single average. Define MultiHead(Q,K,V) = Concat(head_1, ..., head_h) W^O, where each head_i = Attention(Q W_i^Q, K W_i^K, V W_i^V). Let d_model = 4, h = 2, so d_k = d_v = 2. Derive the parameter ledger: W^Q, W^K, W^V, W^O each have shape (4, 4) = 16 parameters (total 64 without bias, or 80 with 4x4 biases). Then compute the ledger for GPT-2 Small: d_model = 768, h = 12, d_k = 64 (4 * 768*768 = 2,359,296 weights, + 4*768 = 3,072 biases, total 2,362,368).
Practice & Prediction Log
1. Why can a single attention head not simultaneously focus on positional proximity and long-range syntactic agreement?
2. Write the parameter ledger for d_model = 4, h = 2 with and without biases.
3. Write the exact parameter ledger for GPT-2 Small attention layer (d_model = 768, h = 12).
A single head produces one scalar attention weight per pair of tokens. Multiple heads provide h independent scalar weights per token pair, allowing independent subspace representations.
A single head can only compute one convex combination of values per token; if it attends to adjacent words, it cannot simultaneously attend to distant subjects. For d_model=4, h=2: four (4,4) matrices = 64 weights; with biases (+16) total is 80. For GPT-2 Small: W^Q, W^K, W^V, W^O are four (768,768) matrices = 4 * 589,824 = 2,359,296 weights; 4 bias vectors of length 768 = 3,072; total attention parameters = 2,362,368.
Precise conceptual explanation of subspace routing and error-free parameter accounting for both tiny and GPT-2 scales.
Authoritative Source Resources & Provenance
Attention Is All You Need
Relevant Section / Range: Section 3.2.2: Multi-Head Attention architecture, linear projections, and parameter equations
Verified Version & Date: NeurIPS 2017 / arXiv:1706.03762v7 (Verified: 2026-10-30)
The foundational primary literature establishing scaled dot-product attention, multi-head projection mechanisms, and autoregressive causal masking.
Linear algebra, matrix multiplication, softmax probability distributions
Complete original algebraic derivations, matrix examples, variance proofs, and tensor diagrams are self-contained in the curriculum text without external document dependency.
Compared Candidate Sources & Selection Analysis (3)
Pioneered additive attention in RNNs, but Vaswani scaled dot-product attention replaced additive alignment in modern LLM architectures due to parallel matrix multiplication speed.
Introduces relative positional biases into attention; deferred to subsequent positional embedding modules after absolute scaled attention is thoroughly mastered.
Selected jointly as the primary code reference: translates Section 3.2 of the Vaswani paper into minimal, idiomatic PyTorch tensor operations.
The Annotated Transformer
Relevant Section / Range: MultiHeadedAttention module implementation: linear projections and subspace splitting
Verified Version & Date: Updated 2022 edition (Verified: 2026-10-30)
Widely used pedagogical commentary that pairs every paragraph of the original Vaswani paper with an executable, testable code cell.
Intermediate Python, PyTorch broadcasting and tensor slicing semantics
The curriculum provides standalone hand-worked numerical tables, explicit causal mask verification scripts, and offline analytical proofs.
Compared Candidate Sources & Selection Analysis (3)
Spectacular visual diagrams and intuitive color-coding, but lacks executable tensor code and formal numerical shape validation.
Rigorous academic slides and video lectures, but access to assignment grading is restricted to Stanford enrolled students.
The original primary paper; Annotated Transformer annotates it directly.