AIForum.

What Is Attention in a Transformer? A Practical Explanation

On this page

THE SHORT ANSWER

Attention is the operation that lets every token in a transformer gather information from the other tokens in its context: it scores each token against the others, turns those scores into weights that sum to 1, and returns a weighted average of what each token offers. This explainer walks through the four steps of scaled dot-product attention, works a hand-checkable example, and shows how masks, temperature, and multi-head setups change what you see.
Quick facts

Attention is the operation that lets each token in a transformer gather information from the other tokens in its context. For every token it computes a set of weights over all tokens, then takes a weighted average of what those tokens offer. That is the entire idea. Multiple heads, causal masks, and positional rotations are machinery built around it.

This explainer is aimed at technically curious beginners and intermediate practitioners who want to reason about model behaviour, memory use, and debugging rather than derive the maths from scratch. It follows the scaled dot-product formulation attributed to Vaswani et al. (2017) and uses a public interactive lab, the ExplAIn Attention Visualizer, as a concrete reference point you can run yourself.

The four steps of scaled dot-product attention

The canonical definition is:

Attention(Q, K, V) = softmax(Q·Kᵀ / √d_k) · V

Read that formula as a pipeline with four stages.

  1. Project. Each embedding row x is multiplied by three learned matrices. The query q = x·W_Q is what a token is looking for. The key k = x·W_K is what it advertises. The value v = x·W_V is what it hands over if it is chosen. The same input token produces all three; the difference is entirely in which projection matrix is applied.
  2. Score. The dot product q_i·k_j measures how well token i‘s query matches token j‘s key. Dividing by √d_k keeps the variance of those scores roughly constant as the key dimension grows. Without that scaling, softmax saturates: one weight goes to nearly 1, the rest to nearly 0, and gradients vanish.
  3. Normalise. Softmax turns each row of raw scores into weights that are positive and sum to 1. After this step, the row is a probability distribution over the context.
  4. Mix. The output for token i is Σ_j a_ij · v_j, the weighted average of every value vector. The result has the same shape as a value vector, so it can be added back to the token’s own representation and passed onward through the network.

Two properties matter in practice. First, the operation is permutation-invariant on its own: without positional information, shuffling the tokens shuffles the outputs identically. Second, every token attends to every other token unless something prevents it, which is where masks come in.

A hand-checkable example

The following numbers are illustrative, chosen to make the arithmetic visible rather than taken from any model.

Take a three-token context and suppose the scaled scores for one query row are [2.0, 0.5, -1.0]. Exponentiating gives roughly 7.389, 1.649, and 0.368, for a total of about 9.406. Dividing each by that total gives attention weights of approximately 0.786, 0.175, and 0.039. They are positive and sum to 1, as required.

If the three value vectors are v₁, v₂, and v₃, the output for this token is 0.786·v₁ + 0.175·v₂ + 0.039·v₃. The token mostly copies the first value, keeps a little of the second, and almost ignores the third. Nothing else happens at this stage — no nonlinearity, no learned gate. Attention is a weighted average with learned weights.

You can reproduce this by hand in the ExplAIn lab, which computes real queries, keys, and values on embeddings small enough to read, and exposes the maths panel next to the visualisation. Note that the lab’s preset weights are hand-built rather than learned, so each pattern has an explanation you can check.

Reading attention patterns: previous-token heads, masks, and temperature

The previous-token head

A useful worked pattern is the head that attends to the token immediately before the current one. Position p is stored as (sin pω, cos pω). A rotation by −ω is a linear map, so W_Q can turn position i into position i − 1. The dot product with key j is then proportional to cos((i − 1 − j)ω), which peaks when j = i − 1. In other words, a single query projection can implement a fixed offset lookup, and the softmax sharpens that peak into a near-one-hot row.

Causal masking

Decoder-style language models must not let a token see the future. Applying a causal mask sets the scores above the diagonal to negative infinity before softmax, so the upper triangle of the attention matrix disappears and each row sums over only the current and previous tokens. In the ExplAIn lab you can toggle the mask and watch that triangle vanish, which is the clearest way to see what masking actually does to the numbers.

Temperature

Temperature divides the scores before softmax. At 0.1, rows collapse toward one-hot: the top match takes almost all the weight. At 3, rows flatten toward uniform: every token contributes roughly equally. This is the same knob you meet in sampling settings, applied here inside attention. It is a useful diagnostic because it separates two questions — which token scores highest, and how decisively that preference is expressed.

Untrained heads

Adding a random head to the visualiser shows what attention looks like before training. The patterns are diffuse and uninterpretable. The structured behaviours people write about, such as previous-token or induction heads, are learned, not architectural guarantees.

Where multi-head attention fits

A single attention operation produces one weighting pattern per layer. Transformers instead run several heads in parallel, each with its own W_Q, W_K, and W_V, then concatenate and project the results. This lets one layer track several relationships at once — a positional offset in one head, a syntactic dependency in another — rather than forcing a single compromise. The ExplAIn lab treats multi-head as a first-class technique alongside self-attention and softmax, which is a reasonable mental model: heads are parallel instances of the same four-step pipeline.

Self-attention is the case where queries, keys, and values all come from the same sequence. Google’s machine learning documentation describes self-attention as the mechanism that allows a model to weigh the importance of different words in relation to each other, and places it inside the encoder–decoder transformer architecture used for tasks such as translation. Cross-attention, where queries come from one sequence and keys and values from another, is the same operation with different inputs.

What this means for running models locally

Attention compares every token with every other token, so the work and the intermediate score matrix grow with the square of the sequence length. That is why long contexts get expensive quickly, and why context length is often the binding constraint on a local setup rather than raw parameter count.

A second practical consequence is the key–value cache. During generation, the keys and values for already-processed tokens do not change, so runtimes store them and compute attention only for the new token. The cache is what makes token-by-token generation tractable, and it is also a large, growing memory consumer: its size scales with the number of layers, the number of key–value heads, the head dimension, the sequence length, and the precision it is stored in. When a local model fits in memory at short context but fails at long context, the KV cache is usually the reason, not the weights. Treat any memory estimate that only counts weight size as incomplete.

Limits and common confusions

Attention weights are not an explanation of a model’s output. A high weight means information from that token was mixed into the representation at that layer and head; it does not establish that the token caused the final prediction, and later layers can overwrite or ignore what an earlier head contributed. Use attention maps to build hypotheses about behaviour, then test them by intervening — masking a token, ablating a head, or changing the input — rather than treating the heatmap as a verdict.

Two other confusions are worth naming. Attention is not memory in the retrieval sense; it is a within-context mixing operation, and nothing persists between calls. And attention is not the only place computation happens: the feed-forward sublayers in each block carry a large share of the parameters and do much of the per-token transformation. Attention decides where information comes from; the rest of the block decides what to do with it.

A concrete next step

The fastest way to make this durable is to run the four experiments in the ExplAIn lab in order: follow the previous-token head along a sentence with the arrow keys; switch the causal mask on and watch the upper triangle disappear; drag temperature to 0.1 and then to 3 and watch rows move from one-hot to uniform; then add a random head to see untrained attention. Each one maps directly onto a section above, and the maths panel lets you check the numbers rather than trust the picture.

If you are working on a local deployment, a natural follow-up is to measure how your runtime’s memory use changes as you raise the context length, so you can see the KV cache growing rather than inferring it. Share your setup, the context lengths you tested, and the numbers you observed in the community — concrete reports are more useful to other readers than general advice.

FAQ

Why divide the dot products by √d_k?

The dot product of two vectors grows in magnitude as the key dimension d_k grows. Left unscaled, the scores become large, softmax saturates into a near-one-hot row, and gradients vanish. Dividing by √d_k keeps the variance of the scores roughly constant.

Do attention weights explain why a model produced an answer?

Not on their own. A weight shows that information from a token was mixed into a representation at a particular layer and head. It does not prove the token caused the final prediction, because later layers can override earlier contributions. Treat attention maps as hypotheses to test by intervention.

What does temperature do inside attention?

Temperature divides the scores before softmax. A low value such as 0.1 pushes each row toward one-hot, so the best match dominates. A high value such as 3 flattens rows toward uniform, spreading weight across tokens.

Why does long context need so much memory?

Two effects compound. Attention work grows with the square of sequence length, and the KV cache that stores keys and values for processed tokens grows linearly with sequence length while also scaling with layer count, key–value head count, head dimension, and storage precision.

References

  1. https://www.explain-ai.org/attention
  2. https://developers.google.com/machine-learning/crash-course/llm/transformers

Related terms

Query (Q)
The projection of a token's embedding that expresses what that token is looking for in the context.
Key (K)
The projection that expresses what a token advertises to others; matched against queries by dot product.
Value (V)
The projection that carries the content a token hands over when it receives attention weight.
Softmax
The function that turns a row of raw scores into positive weights summing to 1.
Causal mask
A mask that blocks attention to future tokens by setting upper-triangle scores to negative infinity before softmax.
Multi-head attention
Running several attention operations in parallel with separate projection matrices, then concatenating and projecting the outputs.
KV cache
Stored keys and values for already-processed tokens, reused during generation so attention is computed only for the new token.

Leave a Reply

Your email address will not be published. Required fields are marked *