Attention Mechanism
Reading the whole room, selectively
Attention lets each token look at all other tokens and selectively pull information — no fixed window. It is the idea that makes LLMs work, and the reason transformers won.
What you will learn
- Describe queries, keys and values
- Explain why attention replaces fixed windows
- Trade attention's power against its cost
What is this?
Attention computes, for each token, a weighted mix of every other token's value, where weights come from matching a query against each key. It lets the model 'pay attention to' the relevant context regardless of distance — the crucial upgrade over fixed-window RNNs. Tokens attend to the whole sequence; that selective mixing is how meaning flows in transformers.
Why do you need it?
- Any token can influence any other in one step — long-range dependencies are native.
- Unlike recurrence, attention processes all positions in parallel, which is why GPUs love transformers.
- The pattern is data-driven: the model learns WHAT to attend to for each task.
- Nothing before it handled 'need to look far away' and 'still train fast' at once.
Mental model
A book club discussing a chapter
Attention
Every member (token) gets a say relevant to their interest. The query is your question, keys are each member's area of knowledge, values are what each member can contribute. You weigh contributions by how well the question matches their expertise.
How it works
- Each token builds three vectors: query (Q), key (K), value (V), via learned projections.
- Score = Q · K across all tokens; softmax normalizes the scores.
- Output = weighted sum of all values using those weights.
- High score = that token's value flows strongly into this token's representation.
- Repeated over layers, attention composes deep-around references, not local ones.
One attention head on 3 tokens.
Examples
Attention arithmetic in plain Python
import mathimport numpy as np tokens = np.array([[["it"], ["was"], ["it"]], [[0.0], [0], [0]]])[0, :, 0] # dummy tokensx = np.array([[1.0, 0], [0.1, 0.9], [0.2, 0.8]])# pretend projections = identity for this demoq = x[0]; k = x; v = xscores = np.array([np.dot(q, ki) for ki in k])weights = np.exp(scores - max(scores)); weights /= weights.sum()out = np.dot(weights, v)print("weights:", np.round(weights, 2))print("output :", np.round(out, 2))The query 'it' settles an attention split — weights decide from which token the meaning arrives.
In production
- LLMs rely on attention for summarization/translation/retrieval-heavy prompts.
- Attention masks control causal vs bidirectional reading (see causal masking).
- Attention's O(N^2) cost is THE scaling constraint in serving long contexts — KV caching exists to tame it.
Best practices
- Remember attention cost grows with sequence length — budget context accordingly.
- Rely on the framework's fused attention kernels; never reimplement manually.
- Understand that attention tells you WHERE it looks, not WHY (interpretability caution).
Common mistakes
- Treating attention weights as a reliable explanation of model reasoning.
- Queries without keys/values in the value mixing — attention without the V projection is just weighting.
- Assuming all attention is contextual knowledge — much of it is positional/biweekly priors.
Common issues
Bring-your-own attention is slowNaive O(N^2) implementation
Use fused/causal kernels or FlashAttention-style paths from the framework.
Model ignores distant tokens entirelyAttention learned a local bias or context too long
Evaluate on truncated context; consider windowed/relative attention.
Debugging tips
- Tokens don't influence each other? Check causal mask interfering or attention head collapsed.
- Long sequences slow — expect squared attention cost; switch to efficient variants if scaling.
- An empty softmax (all extreme negatives) → logit scaling problem, usually numeric.
Interview checkpoint
Quick quiz
Which vectors does a token emit?
Attention lets distant tokens directly influence each other.