Attention Mechanism
The attention mechanism allows neural networks to dynamically focus on different parts of the input when producing each part of the output, enabling models to capture dependencies regardless of distance in the sequence.
What It Really Means
Consider the sentence: "The animal didn't cross the street because it was too tired." What does "it" refer to? A human instantly knows "it" refers to "the animal" — not "the street." For a neural network to make this connection, it needs a mechanism to relate distant tokens in a sequence.
Attention provides this mechanism. For each token in a sequence, attention computes a weighted sum over all other tokens, where the weights reflect how relevant each other token is. When processing "it," the attention mechanism assigns high weight to "animal" and low weight to "street," effectively resolving the reference.
Before attention, sequence models (RNNs, LSTMs) compressed all past information into a single fixed-size hidden state. This created an information bottleneck — by the time the model processed the 50th token, information about the 1st token had largely decayed. Attention allows direct connections between any two positions, regardless of distance.
The attention mechanism is the core innovation in the transformer architecture and the reason modern LLMs can process contexts of 128K+ tokens. Understanding attention is prerequisite to understanding everything in modern AI.
How It Works in Practice
Scaled Dot-Product Attention
The fundamental operation. Given three matrices:
- Q (Query): What information am I looking for? (derived from the current token)
- K (Key): What information do I contain? (derived from all tokens)
- V (Value): What information will I provide? (derived from all tokens)
Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) * V*
Step by step for a single query token:
- Compute dot product of the query with all keys → raw relevance scores
- Scale by 1/sqrt(d_k) → prevents softmax from saturating with large dimensions
- Apply softmax → normalize scores to sum to 1 (attention weights)
- Multiply weights by values → weighted sum of information from all tokens
Multi-Head Attention
A single attention head captures one type of relationship. Multi-head attention runs h attention heads in parallel, each with different learned projections. This allows the model to simultaneously attend to:
- Syntactic relationships ("The" → "cat" — article-noun agreement)
- Semantic relationships ("sat" → "cat" — who performed the action)
- Positional relationships ("on" → "mat" — prepositional phrase)
Each head operates on a subspace of dimension d_k = d_model / h. The outputs are concatenated and projected back to the full dimension.
Self-Attention vs Cross-Attention
Self-attention: Q, K, V all come from the same sequence. Each token attends to all tokens in the same sequence. Used in both encoder and decoder.
Cross-attention: Q comes from one sequence (decoder), K and V come from another (encoder output). Used in encoder-decoder models for tasks like translation.
Causal (Masked) Self-Attention: In decoder-only models (GPT, Llama), each token can only attend to tokens before it (and itself). Future tokens are masked out to prevent information leakage during training.
Concrete Example
Input: "Paris is the capital of France"
When processing "capital" at attention head 3:
- High attention to "Paris" (0.35) — the city being described
- High attention to "France" (0.30) — the country it is capital of
- Moderate attention to "the" (0.15) — grammatical context
- Low attention to "is" (0.10) — verb connection
- Low attention to "of" (0.10) — preposition
This attention pattern encodes the relational knowledge: capital(France) = Paris.
Implementation