3.2 Self-Attention (Q, K, V)
Scaled dots, softmax over keys, mix values. Multi-head, then a causal mask.
These notes match the lecture slides. Use (slides) on the course hub for the deck.
Self-attention is a weighted sum of values, with weights from a comparison of queries to keys. Every token plays all three roles. That is the calculation, and Lab 3. By the end you should be able to write the scaled-dot formula, fill a softmax table, and mask the future with , not zero.
1. What self-attention is
Self-attention is the calculation inside a transformer layer. A weighted sum of values, with weights from queries compared to keys. Every token plays all three roles.
For a sequence matrix ,
Query: what this token is looking for. Key: what another token looks like to be found. Value: what you actually add into the representation.
Row of is “how much token wants each position.” Softmax makes that a distribution. Then you mix values, not keys. Mixing keys is a common lab bug.

Self versus cross. Self-attention: all from the same sequence. Cross-attention (encoder–decoder, later BLIP): queries from one sequence, keys and values from another. Same formula.
2. Why we use it
RNNs mix left-to-right through . Self-attention mixes any pair in one layer, in parallel. Note 3.1 argued for path length 1; this note is the arithmetic that implements it.
Why . If the coordinates of and are roughly independent with variance 1, the dot product has variance . Large pushes softmax toward a one-hot. Dividing by puts the logits back in a range where softmax still mixes. Skipping the scale, or dividing by instead, is the other common exam slip.
Several heads in parallel let different slices of the channels track syntax, a name, or a comma. One head is enough to compute by hand in Lab 3; multi-head is why the layer is not a single average.
3. Architecture
Three matrices map to . Scores are , softmax over keys, then mix :
Multi-head: several in parallel, each on a slice of the channels (head dimension ). Concatenate the head outputs, then a linear . Concat is not a residual, and is part of attention’s parameter count in note 3.3.

Lab 3 computes one head by hand. You do not need eight heads on the board.
For next-token prediction you mask future keys (set scores to before softmax). Otherwise the model cheats. BERT does not mask the future; it masks random tokens and reconstructs them (note 3.4). Those are two different uses of the word mask.
Zero is the wrong fill-in: still puts mass on the “zero.” becomes probability .

4. How it works, step by step
- Form from (or, in a tiny drill, start from scores already).
- Scale: divide by .
- If this is a language model, set illegal future scores to .
- Softmax over keys (last dim / each row), not over columns.
- Multiply the resulting distribution by . Output row is a mix of values.
Row of is a distribution over the sequence. Large mass on a name is “this pronoun just looked at that name.”

If two keys are equal, the softmax splits mass equally between them (given the same query). That is not a tie-break bug; it is the definition.
Lab 3 is this procedure at : one head by hand, then a causal mask. Do not start that notebook until the arithmetic below has been done once on paper.
5. Mathematical formulas
The layer:
with , , .
Causal mask: before the softmax, illegal (future) entries of are set to , so those probabilities become 0.
Multi-head: heads, each of width ; concatenate, then . If two heads each emit a 2-D vector and the model width is , maps the concatenated 4-D vector back to width 4.
6. Positive points and negative points
Positive.
- Parallel over tokens, path length 1: any pair can interact in one layer.
- Multi-head specialization: different heads can track syntax, a name, or a comma.
- The same formula covers self-attention and cross-attention; only which sequence supplies versus changes.
- A heatmap of is a debug tool (Lab 3), not just a picture.
Negative.
- scores per head per layer, as note 3.1 already billed.
- Mixing keys instead of values is a common bug; the softmax weights multiply .
- No positions until note 3.3: attention is a set operation unless you add order.
- Dividing by instead of , or skipping the scale, peaks or flattens softmax for the wrong reason.
- Masking with instead of still puts mass on the illegal key.
- Softmax over the wrong axis (columns instead of keys) silently permutes the mix.
When not to. If the task is next-token prediction, bidirectional self-attention is cheating. Use a causal mask. BERT’s token mask is a different objective (note 3.4), not a substitute for that triangular mask.
7. Teaching this note
~18 minutes. Define Q, K, V in one sentence each, write the scaled-dot formula, grind the table (including ), then the causal mask. Play the QKV and softmax stretch of 3Blue1Brown attention (about 4:00–16:00). Lab 3 is ; do not start that notebook until this arithmetic is done once by hand.
8. Worked example
, , skip and take scores already:
Row 1 softmax: , , so .
Row 2 is the same on the diagonal: .

If , then . Each output is 80% “self” and 20% “the other token.”
Causal mask on row 1: set the future score to . Then row 1 softmax is : token 1 cannot look at token 2.
Without the , row 1 would be : more peaked. That is the scale’s job.
9. Where students get stuck
- Softmax over the wrong axis (columns instead of keys / last dim).
- Dividing by instead of , or skipping the scale.
- Masking with instead of .
- Mixing keys instead of values.
10. Video
Watch 3Blue1Brown: Attention in transformers, visually explained.
Pause when queries and keys meet as a grid of dots, when softmax turns a row into a distribution, and when values are mixed. That grid is Lab 3’s heatmap.
11. Practice
-
If two tokens have identical keys, what does the softmax do to their weights?
-
Why divide by ?
-
Write for with (identity), no mask. What is ?
-
Softmax the row . Then softmax . Did the larger entry gain or lose probability after scaling?
-
Scores , causal mask (upper triangle ). Write the masked score matrix, then (row-wise softmax).
-
Self-attention versus cross-attention: which matrices come from which sequence?
-
Two heads each emit a 2-D vector. After concat, what width does map back to if the model width is ?