Self-Attention
You’ll learn how self-attention lets each token look at the other tokens and decide what matters. You’ll use the Query, Key, Value idea to understand scaling, causal masking, and the difference between self-attention and cross-attention.
The Problem: Words Are Islands
After the embedding layer, each word in a sentence becomes a vector. But that vector is the same regardless of context. “Bank” gets the same vector whether the sentence is about a river or about money. “It” gets the same vector whether it refers to a cat or a mat.
This is a real problem. Suppose the model is trying to predict the next word after “The cat sat on the mat because it was ___.” To predict well, it needs to know what “it” refers to. But “it” is just a fixed vector that means “generic pronoun.” The model has no way to figure out which earlier words matter.
Seeing the Problem
Play 3 quick rounds below: watch how changing a single word at the end of the sentence completely changes which noun "it" must attend to:
Same pronoun, same position, completely different referent. The model needs a mechanism that lets each word look at the other words and update its representation based on what it finds relevant. That mechanism is called self-attention.
Why “self”? Because the words are attending to other words in the same sentence. Later we’ll see cross-attention, where tokens from one sequence (like a French translation) attend to a different sequence (the English original). Self-attention is the simpler, more fundamental case.
What Is “Attention”?
Step through the animation below. It shows the three things attention does for a single word:
How Attention Works
Step 1 / 4The word "it" has no idea what it refers to yet.
That’s all attention is: look at every word, score relevance, blend the winners. Each word’s representation stops being about that word in isolation and starts carrying context from the words it paid attention to.
The Math, in Three Small Steps
Take the sentence “I love cats.” Each word starts as a vector (from the embedding layer). Right now each vector only knows about its own word. “cats” has no idea that someone loves it. We want to produce a new vector for “cats” that blends in information from the relevant words. Three operations get us there:
The Three Operations Behind Attention
How similar are two vectors? Multiply matching dimensions, add them up. Higher total = more similar.
That’s the entire recipe: dot product to score, softmax to normalize, weighted sum to blend. But we used the raw embedding vectors directly. The model needs a way to learn which aspects of each vector to compare. That’s what the Q, K, and V projections do.
The Q, K, V Framework
Each token gets three different learned roles: a question it asks, a description it advertises, and information it carries. The analogy that makes this click: a library search.
Attention as a Library Search
Step 1 / 5Think of attention like searching a library. You walk in with a question. Each book has a title (so you can judge relevance) and contents (the actual information).
Why Three Separate Projections?
Why not just use the raw embeddings for everything? Toggle between the two modes to see what goes wrong:
Why Separate Q, K, V?
Raw embeddings compare surface similarity, not meaning. "mat" scores higher than "cat" because their vectors happen to be close, even though "tired" should connect to "cat." The model has no way to learn what aspects to compare.
But how do the matrices “know” they’re Q, K, V? They don’t. , , and all start as random numbers. What makes them different is where they sit in the formula: ‘s output is the left side of the dot product (the one asking), ‘s output is the right side (the one being compared against), and ‘s output feeds into the weighted sum (the one providing information). Because each matrix occupies a different position, backpropagation sends each one different gradients, and they specialize over training. The “query, key, value” labels describe the architectural role, not something the matrices inherently possess.
Step-by-Step: The Math with Real Numbers
Let’s trace through self-attention with actual numbers. Click “Next” to advance through each computation step:
Self-Attention Step by Step
How It Works, Step by Step
Here’s what self-attention does in plain language (matching the steps above):
| Step | What Happens | Plain English |
|---|---|---|
| 1 | Start with embeddings | Each token is a vector that knows only about its own word |
| 2 | Three weight matrices | The model has learned weights W_Q, W_K, W_V that extract different roles from each embedding |
| 3 | Multiply to get Q, K, V | Each embedding × W_Q = its query (what am I looking for?), × W_K = its key (what do I offer?), × W_V = its value (what will I share?) |
| 4 | Score all pairs | Compare every query against every key (dot product + scale) to get relevance scores |
| 5 | Softmax | Turn scores into weights that sum to 1 (like percentages) |
| 6 | Weighted blend | Each token collects a blend of all values, weighted by relevance |
| 7 | Output | Each token’s vector is now context-aware, carrying information from the words that mattered to it |
Why does comparing queries and keys measure relevance? Each token’s query encodes what it’s looking for (“something alive that can feel tired”), and each token’s key encodes what it offers (“I’m alive, I can feel things”). When a query and key point in the same direction, the match score is high. When they’re unrelated, the score is low or negative.
What Did We Just Build?
Step through the animation below to see how self-attention fits into the bigger picture: from static embeddings to context-aware representations, stacked layers, and finally next-word prediction.
What Did We Just Build?
1 / 4After the embedding layer, each word is a fixed vector. The same word always gets the same vector, no matter what sentence it appears in.
Self-attention is one building block. Multi-Head Attention runs multiple self-attention operations in parallel for different perspectives. The full transformer architecture shows how all the pieces fit together.
Why Scale the Scores?
You may have noticed the ”÷ √d_k” in step 4 of the visualization above. Each attention score is a dot product, a sum of many small numbers (one per dimension). The more dimensions, the bigger the total. With 64 dimensions, scores can easily reach values like 20 or 30.
Why is that a problem?
Large scores push softmax into saturation, where outputs become nearly one-hot:
| Without Scaling (64 dimensions) | With Scaling | |
|---|---|---|
| Raw scores | [30, 25, -10, 5] | [3.75, 3.125, -1.25, 0.625] |
| After softmax | [0.993, 0.007, 0.000, 0.000] | [0.63, 0.34, 0.004, 0.03] |
| Attention | Almost all on one token | Spread across tokens |
| Gradients | Near-zero (vanishing) | Healthy range |
When softmax saturates, the model puts nearly all its attention on a single token and ignores everything else. Worse, the gradients become near-zero, so the network can’t learn to fix this. Dividing by the square root of the dimension keeps scores in a reasonable range where softmax can spread attention across multiple tokens.
Interactive: Explore Attention Patterns
Play with the attention visualizer below. Try different sentences and see how tokens attend to each other:
Self-Attention Visualizer
Things to try:
- Increase temperature to see how it softens the attention distribution
- Click a token to see its complete attention pattern
- Try sentences with ambiguous words like “bank” to see context effects
Causal (Masked) Self-Attention
For language generation (GPT, LLaMA), we need a critical constraint: each token can only attend to itself and the tokens before it. It can’t see the future.
Think of it like writing an exam. You can only use information you’ve already seen, not answers that haven’t been revealed yet.
The Mask
We block future positions by setting their scores to an extremely negative number before softmax, so they get zero attention weight:
| “The" | "cat" | "sat" | "on" | |
|---|---|---|---|---|
| "The” | can see | blocked | blocked | blocked |
| ”cat” | can see | can see | blocked | blocked |
| ”sat” | can see | can see | can see | blocked |
| ”on” | can see | can see | can see | can see |
Each row shows what a token can attend to. The lower-left triangle is visible; the upper-right is blocked.
Notice how the upper-right triangle of the attention matrix is zeroed out:
Self-Attention Visualizer
When to Mask
| Architecture | Masking | Why |
|---|---|---|
| BERT (encoder) | No mask | Understanding needs full context |
| GPT (decoder) | Causal mask | Can’t see future during generation |
| T5 encoder | No mask | Input is fully visible |
| T5 decoder | Causal mask | Output is generated left-to-right |
Self-Attention vs. Cross-Attention
In self-attention, Q, K, and V all come from the same sequence. But there’s a variant called cross-attention where Q comes from one sequence and K, V come from another. This is how translation models let the decoder “read” the encoder’s output.
| Aspect | Self-Attention | Cross-Attention |
|---|---|---|
| Q, K, V source | All from same sequence | Q from one, K/V from another |
| Used in | Every transformer block | Encoder-decoder models only |
| Purpose | Tokens communicate within a sequence | Decoder reads from encoder |
| Example | ”cat” attends to “sat” in same sentence | French “chat” attends to English “cat” |
Self-attention is the fundamental building block. We’ll explore cross-attention in detail later in Seq2Seq Attention →, where we trace the historical origin of the attention idea.
The Cost of Full Context
Self-attention’s power comes from comparing every token to every other token. But that power has a cost. Drag the slider and watch the attention matrix grow:
Attention Matrix Size
What to look for: Start at 4 tokens and slowly drag to 64. The grid area grows much faster than the slider moves. At 4 tokens, there are 16 comparisons. At 8 tokens, there are 64 (not 32). At 64 tokens, there are 4,096. Every time you double the sequence length, the number of comparisons quadruples.
Think of it like seating at a round table. If 4 people need to introduce themselves to everyone else, that’s manageable. But double the guests to 8, and the introductions don’t just double, they quadruple. With 64 guests, you’d need 4,096 introductions. That’s the pattern: the work grows as the square of the input size.
For real models, this gets serious. A 128,000-token context window means over 16 billion entries in the attention matrix, per head, per layer. This is why researchers developed techniques like Flash Attention (exact attention with better memory access patterns), sliding window attention (attend only to nearby tokens), and sparse attention (skip most of the matrix). The quadratic nature is fundamental to the mechanism, but clever engineering works around the worst of the cost.
Quick Summary
| Concept | What Happens | Intuition |
|---|---|---|
| Self-attention | Every token computes relevance scores with every other token | Each word gets to “look around” and gather context from the whole sentence |
| Q, K, V projections | Each token is projected into three separate roles | Like a library: your question (Q), book titles (K), and book contents (V) are different things |
| Score scaling | Divide scores by the square root of the key dimension | Keeps softmax from becoming overconfident and killing gradients |
| Causal masking | Block future positions so tokens can only see the past | Like writing an exam: you can only use information already revealed |
| Quadratic cost | tokens require comparisons: double the length, quadruple the work | This is both the power (full context) and the limitation (expensive for long sequences) |
- I can explain why self-attention is needed, and how it solves the “which word does ‘it’ refer to?” problem
- I understand the Query, Key, Value framework using the library analogy
- I know why separate Q, K, V projections are needed instead of using raw embeddings
- I can explain in plain language why scaling scores matters and what breaks without it
- I understand how causal masking works and when it’s used (decoders) vs. not used (encoders)
- I can describe the difference between self-attention and cross-attention
Next: Multi-Head Attention
A single attention head gives one perspective. But language is multi-faceted, and we need multiple perspectives simultaneously. That’s Multi-Head Attention →