Overview
Why does a Transformer know that "he" in "The boss told the employee that he must work overtime" refers to the employee, not the boss? This post unpacks the coreference-resolution intuition layer by layer, using it as a springboard to explain the attention mechanism in detail.
Key points
- Coreference is hard for machines. Rule-based methods (Hobbs, 1978) use nearest-noun heuristics and break easily; ML methods need heavy feature engineering; RNN/LSTMs lose dependencies beyond roughly 10–15 tokens. Pre-2017 accuracy on complex coreference stalled around 70–80%.
- Attention drops the sequential assumption. Instead of carrying hidden state forward, every token directly "sees" every other token by computing pairwise scores. *Attention Is All You Need* (Vaswani et al., 2017, arXiv:1706.03762) reframes language understanding as a selection problem rather than a memory problem.
- Q/K/V as "look for / be / carry." Each token is projected through three learned matrices (W_Q, W_K, W_V) into a Query ("what I want"), Key ("what I am"), and Value ("what I contribute"). Attention(Q, K, V) = softmax(QK^T / √d_k) V.
- QK^T computes matching scores ("lane widths") between tokens.
- Softmax turns raw scores into a probability budget that sums to 1.
- The weighted sum of V vectors is the actual output—attention is not just "looking" but "moving" semantic content into the current token's representation.
- Why divide by √d_k. When d_k is large (64, 128), dot products grow in variance, pushing softmax into near-one-hot outputs ("exclusive" attention) and starving gradients. Dividing by √d_k normalizes the distribution.
- Multi-head attention captures parallel patterns. h independent Q/K/V projections let different heads specialize—e.g., syntactic coreference, semantic associations, sentiment polarity, logical connectors. Anthropic's *A Mathematical Framework for Transformer Circuits* (2021) shows these heads are interpretable. GPT-3 has 96 layers × 96 heads = 9,216 heads; deeper layers (49–96) handle abstract reasoning, document structure, and author intent, not just local coreference.
- Causal mask enforces left-to-right generation. Autoregressive models must not see future tokens during training, so the upper-triangular portion of the attention matrix is set to −∞ (which softmax maps to 0). nanoGPT implements this with
torch.triu(..., diagonal=1)andmasked_fill(mask, float('-inf')). - Attention is not the whole Transformer. A full block also requires token embeddings, positional encoding, the feed-forward network (FFN) for per-token nonlinear transformation, residual connections for gradient flow, and layer normalization for training stability. The post stresses: attention chooses *where* to look; FFNs do the actual understanding; training data determines *what* gets understood. Without the embedding tables and learned W matrices, attention alone is meaningless.
- A useful mental model. Attention answers the question: "In this sentence, who should I pay attention to?" The answer is the attention-weight vector.
- Vaswani et al. (2017). *Attention Is All You Need.* arXiv:1706.03762
- Devlin et al. (2019). *BERT.* arXiv:1810.04805
- Brown et al. (2020). *Language Models are Few-Shot Learners* (GPT-3). arXiv:2005.14165
- Elhage et al. (2021). *A Mathematical Framework for Transformer Circuits.* https://transformer-circuits.pub/2021/framework/
- Karpathy (2022). *nanoGPT.* https://github.com/karpathy/nanoGPT
- Hobbs (1978). *Resolving Pronoun References.* Lingua.
- Stanford CS224n materials on coreference resolution.