← ALL AI GUIDES/ TRANSFORMERS / CONTEXT

How transformer attention works

Queries, keys and values: a useful calculation, not mind-reading.

THE SHORT ANSWER

Attention lets a position calculate weighted combinations of information from other allowed positions. The weights depend on learned query, key and value projections, plus the model's masking rules.

Choose your level of detail
How it works · Your choice is saved across lessons

A query compares with allowed keys; softmax converts comparisons into weights; each weight scales a value vector. Multiple heads perform different learned comparisons.

01 / EXPLANATION

Why attention was important

A sequence model needs to use earlier context when predicting new text. Transformer attention provides a way for each position to compare itself with other positions and combine relevant information, instead of using only a single rolling summary of what came before.

A transformer block contains more than attention: residual connections, normalization and feed-forward computation are also essential. Showing attention alone is a guided look at one mechanism, not a complete model.

02 / EXPLANATION

Queries, keys and values

Every position is transformed into a query vector Q, a key vector K and a value vector V. A query is compared with keys using dot products. Dividing these scores by the square root of the key dimension helps control their scale.

Softmax turns the scores into nonnegative weights that sum to one across allowed keys. The weighted sum of value vectors becomes the information passed forward by that attention head. An attention percentage is a mathematical weight, not a ground-truth explanation of why the model answered something.

03 / EXPLANATION

Why a causal model cannot look ahead

A GPT-2-style language model predicts the next token using earlier tokens. A causal mask prevents a position from attending to tokens that occur later in the sequence. In a heatmap, the prohibited region is therefore masked.

Multi-head attention uses several learned sets of projections. Different heads can encode different relationships, but they should not be assigned fixed human roles from a colorful heatmap alone.

04 / EXPLANATION

Toy attention versus a real model's activations

Our transformer X-ray animates a simplified attention matrix with computed, invented teaching weights. It can use real token text as labels, but those highlighted values are not extracted from DistilGPT-2. The demo is designed to teach causal masking and normalization.

Real pretrained attention requires reading the model's intermediate tensors and respecting its architecture and quantized runtime. Until that extraction is implemented and tested, the educational X-ray must be treated as a simulation.

WORKED EXAMPLE / CHECK THE LIMITS

A masked matrix

Three token positions: A, B, C

At position B, causal attention may combine information from A and B but not future position C. Its two allowed weights must sum to 100%. The particular percentages are learned-model dependent.

What this does not prove: This example describes valid causal attention behavior; it does not report the actual attention weights of DistilGPT-2.

Open the attention X-ray ↗
QUESTIONS PEOPLE ASK

Questions and clear answers

Is attention the same as understanding?

No. Attention is a mathematical operation for weighting representations, not proof of human-like understanding.

Can GPT-2 attend to future tokens?

Not while making an autoregressive prediction. A causal mask excludes positions later than the current token.

Are the website's colorful attention weights real GPT-2 values?

No. The X-ray uses illustrative weights and says so explicitly. Real tokenization and next-token probabilities are separate actual-model features.

Research & verificationScaled dot-product attentionPeer reviewed
Peer reviewedSite demo: not independently audited
Attention Is All You Need ↗Vaswani and colleagues, NeurIPS 2017, 2017

Publication review: Peer-reviewed machine-learning conference paper.

Independent verification: Our query/key numbers are invented teaching values. The attention implementation has unit tests but has not been independently reviewed.

Peer review of a paper does not certify this demo.