AI State of the Art
Fundamentals/The Transformer: The Architecture That Changed Everything/25 min
Section 30

Self-Attention: Query, Key, Value in Detail

Listen as a podcast →

The story: understanding who does what in a sentence

Read this sentence: "The cat that was sleeping on the couch woke up."

Your brain, reading "woke up", must answer a crucial question: "Who woke up?" Instantly, you know it's the cat, not the couch, even though "couch" is the word right before "woke up". To understand this link, your brain had to "jump" over four intermediate words and identify the correct grammatical subject.

How does it do it? It looks at all the words in the sentence simultaneously and evaluates: "Which one has the most semantic relationship with 'woke up'?" The answer is "cat", because it's a living being that can wake up, unlike a couch.

Self-attention does exactly this work. For each word in a sequence, the mechanism looks at all the other words and computes: "Which one concerns me the most? Which one helps me understand my role and meaning in this sentence?"

This is a fundamental break from older architectures. RNNs (Recurrent Neural Networks) read the sentence word by word, from left to right, trying to compress all past context into a single vector. For the information from the word "cat" to reach the word "woke up", it had to traverse the entire intermediate chain, losing information at each step. Self-attention, on the other hand, establishes a direct connection between any pair of words, regardless of distance.

Q, K, V: three views of the same text

Let's take a concrete example: the word "cat" in our sentence. We want to determine which other words it should pay attention to.

The central idea is to create three different perspectives of the same word, each playing a distinct role:

  1. Query (Q) - The question: "What is this word looking for? What does it need to be understood?" For "cat", the query might encode "I'm a subject, I'm looking for my verb".
  2. Key (K) - The label: "Here's what I offer as context to others." For "woke up", the key might encode "I'm a verb that needs an animate subject".
  3. Value (V) - The content: "Here's the semantic information I transmit if I'm selected." It's the rich representation of the word that will actually be shared.

The most intuitive analogy: imagine a library. The Query is your search ("I'm looking for a book about astronomy"). The Key is the label on each book on the shelf ("astrophysics", "cooking", "novel"). The Value is the actual content of the book you read once you've found it. You compare your search (Q) with each label (K), and you read the content (V) of the most relevant books.

In the Transformer code, these three views are obtained through simple matrix multiplications:

Q=X⋅WQ,K=X⋅WK,V=X⋅WVQ = X \cdot W_Q, \quad K = X \cdot W_K, \quad V = X \cdot W_V

The same input vector X is projected three times with three different weight matrices, producing three distinct representations. These matrices WQW_Q, WKW_K and WVW_V are learned during training: the network discovers on its own what "questions" to ask and what "labels" to create.

Diagram showing two words, Thinking and Machines, with their embeddings x1 and x2 in green, then the query vectors q1 and q2 in purple, key vectors k1 and k2 in orange, and value vectors v1 and v2 in blue, each obtained by multiplying the embedding by a weight matrix W_Q, W_K or W_V.
Each word starts as a single vector (in green) and comes out as three: its query, its key and its value. The three weight matrices W_Q, W_K and W_V are shared by every word in the sentence, and they are exactly what the model learns.Source : Jay Alammar, The Illustrated Transformer
Self vs Cross-attention

"Self"-attention: Q, K, V come from the same text (a word looks at its neighbors). "Cross"-attention: Q comes from one text, K and V from another (a translator looks at the source text).

How it works: step by step

Step 1: The three projections

Each word is a vector. You multiply it by three learned matrices (W_Q, W_K, W_V) to get three versions of the word:

Q=x⋅WQ(the question)Q = x \cdot W_Q \quad \text{(the question)} K=x⋅WK(the label)K = x \cdot W_K \quad \text{(the label)} V=x⋅WV(the information)V = x \cdot W_V \quad \text{(the information)}

Each word is transformed into three versions of itself, each optimized for a specific role in the attention mechanism.

Step 2: Finding similarity

Now, we compare the Query of each word with the Key of every other word. The similarity measure used is the dot product: if two vectors point in the same direction, their dot product is large (high compatibility). If they're orthogonal, the product is zero (no relationship).

scores=Q⋅KT(n×n matrix)\text{scores} = Q \cdot K^T \quad \text{(} n \times n \text{ matrix)}

The result is a square n×nn \times n matrix (where nn is the number of words). Each cell (i,j)(i, j) contains the compatibility score between word ii's query and word jj's key. In other words: "how much should word ii pay attention to word jj?"

Step 3: Scaling (really important!)

We divide each score by dk\sqrt{d_k}, where dkd_k is the dimension of the key vectors. This technical detail is crucial and often poorly explained.

Here's the problem: when you compute a dot product between two vectors of dimension dkd_k, you're adding dkd_k terms. The larger the dimension, the larger the sum in absolute value. Concretely, if dk=64d_k = 64, the dot products will have a variance on the order of 64. Some scores will be very large (positive or negative). When you apply the softmax in the next step, these extreme values will produce near-binary distributions (99.99% on one word, 0.01% on the others), which crushes the gradients and makes learning nearly impossible.

scores=scoresdk\text{scores} = \frac{\text{scores}}{\sqrt{d_k}}

Dividing by 64=8\sqrt{64} = 8 brings the variance back to 1, producing well-distributed softmax outputs and healthy gradients. It's a simple gesture, but without it, the Transformer simply wouldn't work.

Technical corner: variance

This is a classic statistical normalization. If dkd_k independent random variables with variance 1 are summed, the sum has a variance of dkd_k. Dividing by dk\sqrt{d_k} brings the variance back to 1. The authors of the "Attention Is All You Need" paper explicitly mentioned this problem and proposed this solution.

Step 4: Converting to probabilities

Apply softmax to convert the scores into probabilities (between 0 and 1, summing to 1):

attention_weights=softmax(scores)\text{attention\_weights} = \text{softmax}(\text{scores})

Now, for each word, you have a probability distribution: "what percentage of attention will I give to each other word?"

Step 5: Gathering the answers

Compute a weighted average of all the Values (V) using the attention weights:

output=attention_weights⋅V\text{output} = \text{attention\_weights} \cdot V

Result: for each word, you get a representation that combines all the other words, weighted by their relative importance.

The complete magic formula

Attention(Q,K,V)=softmax ⁣(Q⋅KTdk)⋅V\text{Attention}(Q, K, V) = \text{softmax}\!\left(\frac{Q \cdot K^T}{\sqrt{d_k}}\right) \cdot V

That's the entire algorithm, in a single line. Three linear projections, a similarity measure, scaling normalization, a conversion to probabilities via softmax, and a weighted average of values. It's this elegance that makes the Transformer powerful: a relatively simple mechanism, but one that gives the model the ability to connect any elements of a sequence to each other.

Vertical table walking through the attention computation for the phrase Thinking Machines: embedding row, queries row, keys row, values row, then the scores q1 dot k1 equals 112 and q1 dot k2 equals 96, the division by 8 giving 14 and 12, the softmax giving 0.88 and 0.12, the values scaled by those weights, and finally the sum z1.
The five steps unrolled on a tiny two-word example. Follow the Thinking column top to bottom: a raw score of 112, divided by 8 (the square root of d_k) it becomes 14, softmax turns it into 0.88, and the output z1 is the sum of the values weighted by 0.88 and 0.12. Here the word Thinking mostly attends to itself.Source : Jay Alammar, The Illustrated Transformer

Visualizing: seeing where the model looks

The attention matrix (the weights after softmax, before the weighted average) is a fascinating interpretability tool. It shows literally where the model looks when it processes each word. Researchers turn them into heatmaps to visualize them, which allows you to "see" the model's reasoning.

Let's take the example of the sentence "The cat eats the mouse". Here is the typical shape of the attention paid by the word "eats". Note that the split below is illustrative only, it depends entirely on which model and which head you look at; what matters is the ordering, not any particular figure:

  • a large share to the word "cat" (the subject performing the action)
  • a comparable share to the word "mouse" (the object receiving the action)
  • a smaller share to function words ("the")
  • the remainder on itself
A 4 by 4 heatmap titled QKV Attention Weights. Rows are the querying words (The, quick, fox, jumps) and columns are the words being attended to. Each cell shows a weight between 0.14 and 0.37, the darkest cell being fox attending to fox at 0.37.
This is what the attention matrix actually looks like after the softmax. Each row is a word asking its question, each column a word answering, and the numbers in any given row always add up to 1: that is how this word spreads its attention over the whole sentence.Source : Query, Key, Value: The Foundation of Transformer Attention, M. Brenndoerfer

What's remarkable is that nobody ever taught the model grammar. It has never seen explicit syntactic rules. By simply training to predict the next word on billions of texts, it discovered on its own that verbs must pay attention to their subjects and objects. This is an example of emergent behavior: a complex capability that spontaneously emerges from a simple training objective.

The problem: it's very expensive

The downside is severe. To compute attention, each word must be compared with every other word. The complexity therefore grows with the square of the sequence length:

  • 100 words = 10,000 comparisons (trivial)
  • 1,000 words = 1 million comparisons (manageable)
  • 10,000 words = 100 million comparisons (expensive)
  • 100,000 words = 10 billion comparisons (enormous)

Formally:

Old RNN:O(n)(linear - fast but sequential)\text{Old RNN} : O(n) \quad \text{(linear - fast but sequential)} Transformer:O(n2)(quadratic - expensive but parallelizable)\text{Transformer} : O(n^2) \quad \text{(quadratic - expensive but parallelizable)}

The O(⋅)O(\cdot) notation, read "big O", does not give a number of operations but the way that number grows as the sequence gets longer. O(n)O(n): double the length, double the work. O(n2)O(n^2): double the length, quadruple the work. It is this difference in regime, not some multiplicative constant, that decides what is feasible.

Run the arithmetic on a context of one million tokens, which several models now advertise. The number of token pairs to compare is n2=1012n^2 = 10^{12}, that is a trillion pairs, and that is for a single head of a single layer. Which is exactly why no production system ever materializes a 1-million-by-1-million attention matrix: the table would not fit in the memory of any graphics card in existence. Very long contexts all rest on workarounds.

This is a very active research area, covered in detail later in the course:

  • Flash Attention reorganizes the computation so the full matrix is never written to memory, by exploiting the GPU memory hierarchy: SRAM (Static Random-Access Memory), a tiny but very fast memory sitting right next to the compute units, and HBM (High Bandwidth Memory), the card's main memory, far larger but far slower to reach. The whole gain lies in avoiding round trips to the latter.
  • Mamba and SSM-type models (State Space Models) offer linear alternatives to attention.
  • Linear attention approximates the softmax to escape the quadratic cost.

For now, none of these approaches has completely replaced standard attention, which remains the dominant standard.

The context length limit

Quadratic complexity makes very long sequences expensive. The context windows advertised by recent models run into the millions of tokens, but those are marketing figures that shift month to month, and not one of them rests on dense attention applied across the whole window: there is always an optimization behind it (Flash Attention, sliding-window attention, sparse attention). Compute cost remains the limiting factor.

Masked attention: preventing cheating

When you train an autoregressive model like GPT (which generates text word by word), there's a fundamental problem: during training, the model sees the entire sentence at once, but it must not be able to "cheat" by looking at future words it's supposed to predict.

The analogy: imagine an exam where the questions and answers are on the same sheet. If the student can read the answer to question 6 before answering question 5, the exam no longer measures anything. You need to mask the future answers.

Solution: the causal mask. We impose an invisible barrier in the attention matrix:

WordCan see
Word 1itself
Word 2word 1, word 2
Word 3word 1, word 2, word 3
Word 4word 1, word 2, word 3, word 4

In practice, the implementation is elegant: we set −∞-\infty in the attention scores corresponding to future tokens (the upper-right part of the matrix). After softmax, e−∞=0e^{-\infty} = 0, so these words receive exactly zero attention. No information from the future can leak to the past.

At inference (text generation), the causal mask is naturally respected: the model generates one word at a time, and each new word can only see already-generated words. It's the consistency between training (with mask) and inference (sequential generation) that makes the model work correctly in both cases.

This asymmetry has one major practical consequence, developed in the multi-head attention lesson: at each generation step the model needs only its own query, but the keys and values of every preceding token. That is the entire reason K and V are cached and Q never is.

Why this matters

Self-attention is the beating heart of the Transformer, and by extension of all modern language models. It's the mechanism that allows an AI to understand that "cat" and "woke up" are linked, even if they're separated by ten words. Without attention, each word would be processed in isolation or with context degraded by distance. It's attention that gives Transformers their ability to capture long-range relationships, understand grammatical structure, and model the complex dependencies of natural language.

Key takeaways

  1. Q, K, V = three different perspectives of the same word
  2. Dot product Q . K = similarity measure
  3. Scaling by sqrt(d_k) = stabilizes training and variance
  4. Softmax = converts scores to probabilities
  5. Aggregation = weighted average of all answers
  6. O(n2)O(n^2) complexity = the current bottleneck: double the length of the text and the work quadruples
  7. Causal mask = prevents cheating by looking at the future
Check your understanding0 / 4

1. In the library analogy, what role does a word's Key play?

2. Why are attention scores divided by the square root of d_k before the softmax?

3. A model advertises a 1-million-token context window. What can you conclude about its attention?

4. How is the causal mask actually implemented?