AI & ML
What Attention Actually Computes
Queries, keys, values, and a weighted average: the single mechanism at the heart of the transformer.
Introduction
Section titled “Introduction”The transformer, introduced in 2017, replaced recurrence and convolution in sequence modelling with a single mechanism: self-attention. It now underpins large language models, vision models, and much of modern machine learning. Despite the mystique, attention computes something simple — a weighted average, where the weights are learned from the data.
The core concept
Section titled “The core concept”Given a sequence of tokens, self-attention lets each token gather information from every other token. For each token, the model computes three vectors from its embedding: a query (what am I looking for?), a key (what do I contain?), and a value (what do I contribute?). The output for a token is a weighted average of all tokens’ value vectors, where the weight for each pair is determined by how well the first token’s query matches the second token’s key.
How it works
Section titled “How it works”The computation, called scaled dot-product attention, is:
- Score. For each pair of tokens, take the dot product of the query of one and the key of the other. A high score means “this token is relevant to that one”.
- Scale. Divide by the square root of the key dimension. Without this, dot products grow large in high dimensions, the softmax saturates, and gradients vanish.
- Normalise. Apply softmax over the scores so the weights for each token sum to one.
- Mix. Multiply the weights by the value vectors and sum — the weighted average that becomes the token’s new representation.
This is done in parallel for all tokens (hence the transformer’s speed on modern hardware), and repeated in several heads: each attention head learns its own query/key/value projections, so one head might track subject–verb agreement while another tracks long-range references. The heads’ outputs are concatenated and projected back to the model dimension.
Crucially, attention itself has no notion of order — the weighted average is permutation-invariant. Position is injected separately, via positional encodings added to the input embeddings, which is why the model can tell “dog bites man” from “man bites dog”.
Why it matters
Section titled “Why it matters”Attention solves the bottleneck that limited recurrent models: an RNN had to compress an entire sentence into one fixed-size hidden state, so distant context faded. Attention gives every token direct access to every other token in a single step, so the path length between any two positions is one — long-range dependencies are no harder to learn than adjacent ones. The cost is quadratic: attention over a sequence of length n needs n² scores, which is why context windows, not model size alone, dominate the cost of long documents, and why sparse and linear attention variants are an active research area.
Misconceptions
Section titled “Misconceptions”Attention weights are often read as explanations — “the model looked at this word” — but a high weight is not proof of causal influence, and different heads often disagree. Attention is a routing mechanism, not a reasoning trace. It is also not the whole transformer: the feed-forward network applied to each position after attention holds most of the model’s parameters and does much of the actual computation.
Related reading
Section titled “Related reading”References
Section titled “References”- Vaswani et al., “Attention Is All You Need” (NeurIPS 2017)
- The Illustrated Transformer — Jay Alammar
- Transformers — Hugging Face course