← All labs

Attention microscope

How do Q, K, V, the mask and softmax produce one output?

Back to journey scene 4
Four toy tokens, three input numbers each, one head with d_head = 2. Change one token's input and follow the effect through every step. The recorded mode shows real Qwen3-8B-Base attention probabilities, with only the controls that recording supports.

Inputs

Toy calculation
X: one row per token (after RMSNorm, in a real block)
rowx0x1x2
A1.000.000.50
B0.001.000.50
C1.001.000.00
D0.50-1.001.00

1. Projections

Q = XW_q, K = XW_k, V = XW_v
Q (queries)
rowq0q1
A1.250.25
B0.251.25
C1.001.00
D1.00-0.50
K (keys)
rowk0k1
A1.500.25
B0.500.75
C1.001.50
D1.50-1.25
V (values)
rowv0v1
A1.250.25
B0.251.25
C1.001.00
D1.00-0.50

2–4. Scores, scaling, mask, shares

rows = queries, columns = keys
Q·K (raw scores)
rowABCD
A1.940.811.631.56
B0.691.062.13-1.19
C1.751.252.500.25
D1.380.130.252.13
÷ √2, future masked 🔒
rowABCD
A1.37🔒🔒🔒
B0.490.75🔒🔒
C1.240.881.77🔒
D0.970.090.181.50
softmax: shares per row
rowABCD
A100%🔒🔒🔒
B43%57%🔒🔒
C29%21%50%🔒
D28%12%13%48%

Query D shares: A 28.1%, B 11.6%, C 12.7%, D 47.7% (before your edits: A 28.1%, B 11.6%, C 12.7%, D 47.7%). Each row adds up to 100%.

5. Weighted sum of values

Toy calculation

out(D) = 0.28·V_A + 0.12·V_B + 0.13·V_C + 0.48·V_D

output for D
0.9800.101

Dashed outline: before your edits. Filled bar: now.

Predict first

Which positions may token C attend to?

Predict first

Token D gives 70% of its attention to A. Does that prove A caused the model's next prediction?

Grouped-query attention in Qwen
Qwen3-8B has 32 query heads but only 8 key/value heads: each K/V head is shared by 4 query heads, which makes the KV cache 4 times smaller. Each query head still has its own pattern.