Core

Self-attention from first principles: Q, K, V

Self-attention lets each token build a context-aware representation by choosing what to read from other positions.

Updated

1

Concept

The embedding of a token starts as a context-free lookup. The token “bank” receives the same initial vector in “river bank” and “bank account.” A useful language model needs a representation that changes after reading the surrounding sequence. Self-attention is the Transformer operation that performs this contextual mixing: each position decides how much information to gather from other positions.

Start with a sequence represented as a matrix XX. Each row is one token position, and each row contains that position’s current features. The attention layer learns three projection matrices, WQW_Q, WKW_K, and WVW_V. Multiplying XX by them creates three new matrices:

Q=XWQ,K=XWK,V=XWVQ = XW_Q,\qquad K = XW_K,\qquad V = XW_V

The names describe roles, not three different input sequences. Queries express what each receiving position is looking for. Keys describe how each available position should be matched. Values contain the information that can be transferred. Every position creates all three from its current representation.

For one receiving position, take its query and compute a dot product with every key. A larger dot product means the vectors point in more compatible directions. Collecting every pair at once gives the score matrix QKQK^\top. The row for one query contains its compatibility with every key. The Transformer scales those scores by the square root of the key dimension and normalizes each row with softmax:

Attention(Q,K,V)=softmax(QKdk)V\operatorname{Attention}(Q,K,V) = \operatorname{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

The softmax values are attention weights. They are non-negative and each row sums to one. Multiplying those weights by VV produces, for every query position, a weighted sum of value vectors. This is the crucial separation: keys decide where to read, while values determine what is read. If keys and values had to be identical, addressing and payload would be forced into one representation.

Consider “The chef added salt because the soup tasted bland.” The query at “bland” may match the keys for “soup” and “tasted” more strongly than the key for “chef.” Its new representation then mixes value information from those positions. In another sentence, the same word starts from the same embedding but receives different context. Self-attention turns a static token identity into a contextual state.

The word self means queries, keys, and values come from the same sequence. In cross-attention, queries come from one sequence while keys and values come from another, such as a decoder reading encoder states. The same addressing mechanism serves both cases.

Decoder-only language models add a causal mask. When predicting the token at position ii, that position must not read future tokens. Before softmax, scores for forbidden positions are replaced by a very negative value, which makes their normalized weight effectively zero. Without the mask during training, the model could peek at the answer it is supposed to predict.

One attention calculation is not asked to capture every relationship. Transformers use multiple heads: separate learned projections perform attention in parallel, and their outputs are concatenated and projected again. Different heads can specialize in different useful patterns, although a clean human interpretation is not guaranteed. The full block also includes residual connections, normalization, and a feed-forward network. Attention is a routing operation inside that larger system, not the entire Transformer.

Self-attention has two notable computational properties. First, all pairwise scores can be computed with matrix operations, so training processes positions in parallel instead of stepping through a recurrent state one token at a time. Second, the score matrix has a pair for every pair of positions, giving conventional attention quadratic growth in sequence length. Efficient kernels such as FlashAttention reduce memory traffic without changing the exact mathematical result, while sparse and linear variants alter the computation more substantially.

Attention weights are useful to inspect, but they are not a complete explanation of model reasoning. A high weight shows that one head transferred value information along an edge at one layer. Residual streams, other heads, feed-forward blocks, and later layers can amplify, transform, or cancel that contribution. “The model attended to this token” is evidence about routing, not a proof of causal importance by itself.

The durable mental model is a differentiable lookup system. Every position writes a query, exposes a key, and offers a value. Similarity between queries and keys produces addresses; softmax turns addresses into mixing weights; the weighted values become context. Because the projections are learned end to end, the model discovers what questions, labels, and payloads are useful for predicting language.

2

Explain it like I am five

Picture a busy newsroom. Every reporter writes a question on a card: that is the query. Each source wears a label describing what information they can help with: that is the key. The reporter compares the question with every label and assigns attention accordingly. What the chosen source actually says is the value. A source can have a label that matches the question strongly while delivering a separate payload. Self-attention repeats this tiny newsroom for every token, in parallel.

3

Teach it back

Using one concrete sentence, explain the distinct jobs of queries, keys, and values in self-attention, then explain why the output is context-dependent.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

In “The animal did not cross the street because it was tired,” the position for “it” forms a query describing what it needs. Every position exposes a key used for matching and a value containing information to retrieve. The query may score the key for “animal” more strongly than the key for “street.” A softmax turns scores into weights, and the output is a weighted sum of values. Because those weights depend on all tokens in this sentence, the representation of “it” changes with its context.

4

Check your understanding

1. What is compared to produce raw attention scores?
Answer and explanation

A query vector with key vectors — Scaled dot products between a position's query and the available keys produce compatibility scores.

2. What is combined after softmax produces attention weights?
Answer and explanation

The value vectors — The attention output is a weighted sum of value vectors; keys participate in addressing, while values carry the retrieved content.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Ashish Vaswani et al. (2017). Attention Is All You Need.
  2. Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio (2015). Neural Machine Translation by Jointly Learning to Align and Translate.