3.2 Self-Attention (Q, K, V)

DATA 443/643 · Week 3 · PDF · Slides

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 2×2 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 X∈ℝT×d,

Q=XWQ,K=XWK,V=XWV.

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 t of QK⊤ is “how much token t wants each position.” Softmax makes that a distribution. Then you mix values, not keys. Mixing keys is a common lab bug.

Query, key, value, then a mix of values

Self versus cross. Self-attention: Q,K,V 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 ht. 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 dk. If the coordinates of q and k are roughly independent with variance 1, the dot product has variance dk. Large dk pushes softmax toward a one-hot. Dividing by dk puts the logits back in a range where softmax still mixes. Skipping the scale, or dividing by dk 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 X to Q,K,V. Scores are QK⊤/dk, softmax over keys, then mix V:

A=softmax(QK⊤dk),Attention(Q,K,V)=AV.

Multi-head: several (WQ,WK,WV) in parallel, each on a slice of the channels (head dimension dk=d/h). Concatenate the head outputs, then a linear WO. Concat is not a residual, and WO is part of attention’s parameter count in note 3.3.

Two heads, concatenate, project

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: softmax([1,0]) still puts mass on the “zero.” −∞ becomes probability 0.

Causal mask zeros out the future


4. How it works, step by step

  1. Form Q,K,V from X (or, in a tiny drill, start from scores S=QK⊤ already).
  2. Scale: divide by dk.
  3. If this is a language model, set illegal future scores to −∞.
  4. Softmax over keys (last dim / each row), not over columns.
  5. Multiply the resulting distribution by V. Output row t is a mix of values.

Row t of A is a distribution over the sequence. Large mass on a name is “this pronoun just looked at that name.”

Attention weights for one head

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 T=4: one head by hand, then a causal mask. Do not start that notebook until the 2×2 arithmetic below has been done once on paper.


5. Mathematical formulas

The layer:

Attention(Q,K,V)=softmax(QK⊤/dk)V,

with Q=XWQ, K=XWK, V=XWV.

Causal mask: before the softmax, illegal (future) entries of QK⊤/dk are set to −∞, so those probabilities become 0.

Multi-head: h heads, each of width dk=d/h; concatenate, then WO. If two heads each emit a 2-D vector and the model width is d=4, WO 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 Q versus K,V changes.
  • A heatmap of A is a debug tool (Lab 3), not just a picture.

Negative.

  • O(T2) scores per head per layer, as note 3.1 already billed.
  • Mixing keys instead of values is a common bug; the softmax weights multiply V.
  • No positions until note 3.3: attention is a set operation unless you add order.
  • Dividing by dk instead of dk, or skipping the scale, peaks or flattens softmax for the wrong reason.
  • Masking with 0 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 2×2 table (including dk), then the causal mask. Play the QKV and softmax stretch of 3Blue1Brown attention (about 4:00–16:00). Lab 3 is T=4; do not start that notebook until this arithmetic is done once by hand.


8. Worked example

T=2, dk=2, skip WQ and take scores S=QK⊤ already:

S=[2002],S2≈[1.41001.41].

Row 1 softmax: e1.41≈4.10, e0=1, so [4.10,1]/5.10≈[0.80,0.20].

Row 2 is the same on the diagonal: A≈[0.800.200.200.80].

Scaled scores to a mix of values

If V=I, then AV=A. 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 [1,0]: token 1 cannot look at token 2.

Without the 2, row 1 would be softmax([2,0])≈[0.88,0.12]: 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 dk instead of dk, or skipping the scale.
  • Masking with 0 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

  1. If two tokens have identical keys, what does the softmax do to their weights?

  2. Why divide by dk?

  3. Write A for T=2 with Q=K=V=I (identity), no mask. What is AV?

  4. Softmax the row [0,2]. Then softmax [0,2]/2. Did the larger entry gain or lose probability after scaling?

  5. Scores [1301], causal mask (upper triangle −∞). Write the masked score matrix, then A (row-wise softmax).

  6. Self-attention versus cross-attention: which matrices come from which sequence?

  7. Two heads each emit a 2-D vector. After concat, what width does WO map back to if the model width is d=4?