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:
- 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".
- 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".
- 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:
The same input vector X is projected three times with three different weight matrices, producing three distinct representations. These matrices , and are learned during training: the network discovers on its own what "questions" to ask and what "labels" to create.

"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:
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).
The result is a square matrix (where is the number of words). Each cell contains the compatibility score between word 's query and word 's key. In other words: "how much should word pay attention to word ?"
Step 3: Scaling (really important!)
We divide each score by , where 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 , you're adding terms. The larger the dimension, the larger the sum in absolute value. Concretely, if , 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.
Dividing by 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.
This is a classic statistical normalization. If independent random variables with variance 1 are summed, the sum has a variance of . Dividing by 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):
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:
Result: for each word, you get a representation that combines all the other words, weighted by their relative importance.
The complete magic formula
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.

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

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:
The notation, read "big O", does not give a number of operations but the way that number grows as the sequence gets longer. : double the length, double the work. : 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 , 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.
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:
| Word | Can see |
|---|---|
| Word 1 | itself |
| Word 2 | word 1, word 2 |
| Word 3 | word 1, word 2, word 3 |
| Word 4 | word 1, word 2, word 3, word 4 |
In practice, the implementation is elegant: we set in the attention scores corresponding to future tokens (the upper-right part of the matrix). After softmax, , 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
- Q, K, V = three different perspectives of the same word
- Dot product Q . K = similarity measure
- Scaling by sqrt(d_k) = stabilizes training and variance
- Softmax = converts scores to probabilities
- Aggregation = weighted average of all answers
- complexity = the current bottleneck: double the length of the text and the work quadruples
- Causal mask = prevents cheating by looking at the future
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?