Core
Self-attention from first principles: Q, K, V
Self-attention lets each token build a context-aware representation — worked through with the real projection shapes of Qwen3.8-27B, where 24 heads of width 256 make the Q projection non-square.
Updated
01 · Concept
Concept
The embedding of a token is a context-free lookup. The token “bank” receives the same initial vector in “river bank” and “bank account,” yet a useful model must treat those two occurrences differently. That is the concrete problem: turn a static token identity into a representation that depends on the surrounding sequence. Self-attention is the operation that does it — each position decides how much information to gather from every other allowed position. The positions themselves are tokenizer outputs from lesson 1.2; in Qwen3.8-27B each one is a vector of 5120 numbers, the model’s hidden size.
Represent the sequence as a matrix : one row per token position, 5120 features per row. The attention layer learns three projection matrices, , , and , producing
The names describe roles, not different inputs. Queries express what each receiving position is looking for. Keys describe how each available position should be matched. Values contain the information that can be transferred. Every position creates all three from its current representation.
- q_proj (queries + gate)5120 → 12288
- split per head24 × (256 query + 256 gate)
- k_proj · v_proj5120 → 1024 each
- sigmoid(gate) · o_proj6144 → 5120
Now the worked example, with real shapes. A reasonable first guess — and the classic wrong one — is that each projection maps the hidden size to itself: , a square matrix. Qwen3.8-27B’s configuration refutes it. The model’s Gated Attention layers use 24 query heads, each of dimension 256. The query projection must produce all of them, so the query state is head count times head dimension:
That 6144 is the width of the query state, and it is genuinely non-square — wider than a square matrix would be. The weight matrix that produces it is wider still. These are gated attention layers (attn_output_gate in the config), and the gate is fused into the same projection: q_proj emits head count times head dimension twice over, . The split is per head, not down the middle — the output is viewed as 24 heads of , and each head’s last axis is cut into 256 query values and 256 gate values. Gather the query halves and you have the 6144-wide query state; gather the gate halves and you have a second 6144 that never enters the score matrix. Instead, once attention has run, its output is scaled elementwise by and only then projected. So on disk the tensor is ; only half of it ever becomes a query. Keys and values are narrower still: the model has only 4 KV heads (a grouped-query design covered in lesson 7.6), so
and both the key and value projections are . After the heads compute their outputs and are concatenated back to width 6144, an output projection maps so the result fits the residual stream again. Four rectangles, no squares: (queries and gate together), , , . Note that ‘s input is 6144 wide, not 12288: what reaches it is the concatenated, gate-scaled attention output, which happens to share the query state’s width. The gate is applied elementwise just before , so it scales what receives without widening it.
With shapes settled, the mechanism: for one receiving position, take its query and compute a dot product with every key. A larger dot product means more compatible directions. All pairs at once form the score matrix . Scale by the square root of the head dimension (the subject of lesson 4.3) and normalize each row with softmax:
The softmax outputs are attention weights: non-negative, each row summing to one. Multiplying by gives every query position a weighted sum of value vectors. This is the crucial separation — keys decide where to read, values determine what is read. If keys and values were forced to be the same vectors, addressing and payload would share one representation and each would compromise the other.
Consider “The chef added salt because the soup tasted bland.” The query at “bland” may match the keys for “soup” and “tasted” more strongly than the key for “chef.” Its new representation then mixes value information from those positions. In another sentence the same token starts from the same embedding but ends somewhere different. That is the contextual mixing we set out to build.
The word self means queries, keys, and values come from one sequence. Decoder-only models like Qwen3.8-27B add a causal mask — position must not read positions after — which lesson 4.4 develops. And one attention calculation is not asked to capture every relationship: the projections are split into the 24 parallel heads mentioned above, the subject of lesson 4.5. The full block adds residual connections, normalization, and a feed-forward network; attention is the routing operation inside that larger system.
Two computational properties matter later. First, all pairwise scores are matrix operations, so training processes positions in parallel. Second, the score matrix has an entry for every pair of positions, giving quadratic growth in sequence length — the bill lesson 4.3 quantifies at this model’s real context length and track 7 works to reduce.
Attention weights are evidence about routing, not proof of reasoning. A high weight shows one head transferred value information along one edge at one layer; residual streams, other heads, and later layers can amplify or cancel it. The durable mental model is a differentiable lookup: every position writes a question, wears a label, and offers a payload, and the projections that define all three are learned end to end.
02 · Analogy
Analogy
Picture a busy newsroom. Every reporter writes a question on a card: that is the query. Each source wears a label describing what information they can help with: that is the key. The reporter compares the question with every label and assigns attention accordingly. What the chosen source actually says is the value. A source can have a label that matches the question strongly while delivering a separate payload. Self-attention repeats this tiny newsroom for every token, in parallel.
03 · Teach it back
Teach it back
Using Qwen3.8-27B's real dimensions, explain the jobs of the Q, K, V, and output projections and why none of them is a square matrix.
Compare with a model answer
Each token's 5120-dimensional hidden state is projected three ways. The query projection produces 24 query heads of dimension 256, so the query state is 24 times 256 = 6144 wide, not 5120. Because these are gated attention layers, the same matrix also emits a 6144-wide gate, so the weight itself is 5120 to 12288 and is split per head into 256 query and 256 gate values each; only the query halves enter attention, and sigmoid of the gate scales the attention output before the output projection. The key and value projections map 5120 to 1024 because only 4 KV heads of dimension 256 exist: 4 times 256 is 1024. Queries express what a position is looking for, keys describe how a position can be matched, values carry the retrievable content. After attention mixes values, the output projection maps the 6144-wide concatenation back to 5120 so the result can be added to the residual stream. The widths are set by head count times head dimension, so nothing forces them to equal the hidden size.
04 · Check your understanding
Check your understanding
Complete the teach-back and answer the quiz correctly to finish this lesson.
◎ · Evidence marker
Sources
- Ashish Vaswani et al. (2017). Attention Is All You Need.
- Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio (2015). Neural Machine Translation by Jointly Learning to Align and Translate.
- Qwen Team (2026). Qwen3.8-27B Model Card.