Q, K, V & Causal Attention During Inference
Attention is the only place in the model where positions exchange information. Every other computation in the block, the projections, the MLP, the norms, treats each of the six positions as its own private 4,096-wide problem; attention is where " France" gets to matter to " is". This lesson gives the three letters their meaning, adds the one rule generation imposes, and reads a real attention pattern captured from the canonical prompt.
The arithmetic is not new. The lesson on attention math taught the equation as pure math and the transformer architecture refresher placed the projections structurally; what was missing both times, on purpose, is what the letters are FOR, and what changes when the input is being generated left to right rather than handed over whole.
Query, key, value
Inside the block, each position's vector is multiplied by the three projection matrices from the transformer architecture refresher, producing three different summaries of the same position. The query is the summary used when this position is doing the looking: a representation of what it currently needs from the context. The key is the summary shown to others: what this position can be found BY. The value is what the position hands over once selected: the content that actually flows if attention lands here.
The equation then reads as intent instead of notation. QKᵀ scores every position's query against every position's key, one dot product per pair, the similarity reading from attention math doing its job: the [6, 6] table is six questions scored against six offers. Softmax per row turns each position's scores into weights, and multiplying by V spends those weights, the weighted-sum reading: each position leaves attention carrying a blend of other positions' values, mixed in the proportions its own query chose.
Nothing more exotic is involved: three learned projections and the two dot-product readings you have had since inference prerequisites, arranged so that positions can select from one another.
The mask
Generation imposes one rule the pure equation does not have: a position may look only backward. Ask why " of" is forbidden from seeing " France", when both sat in the same tensor during the same pass and " France" was right there. Because the model produces text left to right, and the position that once held " of" must behave the same way when " France" has not been generated yet. A model whose every position could consult the future during training and prompting would be useless in the loop that actually produces text, where the future does not exist.
The mechanism is blunt. Before the row-wise softmax, every score from a position to any LATER position is overwritten with negative infinity. The lesson on attention math promised that softmax never returns an exact zero, and that promise held for every score a dot product can produce; negative infinity is not such a score, and e^−∞ is exactly zero. Masking is the one way a cell is truly emptied rather than merely starved. The [6, 6] table keeps only its lower triangle, and each row's softmax renormalizes over what remains: position 0 sees only itself, position 3 sees positions 0 through 3, and the last position is the only one that sees the whole prompt.
A real pattern
Here is attention actually happening: one head's weight table from the real forward pass of the canonical prompt, captured this session.
Two readings from the data are worth making explicit. Every row leans hard on the first column: each position sends a large share of its attention to the begin-of-text token, 0.93 from "The" in the early head, 0.28 even from " is" in the deeper one. Attention piling onto the first position, whatever it contains, is a documented habit of transformer language models, and it will matter again when later modules manage attention's memory.
The two heads then disagree instructively. In the layer-0 head, weight drains toward the front of the sequence: " is" gives 0.23 to "The" and only 0.054 to " France", its immediate predecessor. The layer-15 head has opinions about structure instead, " is" sending 0.295 to " of" and " France" sending 0.31 to the same word. Each is one head of 32, every one running the same masked equation on its own 128-wide slice and free to learn its own habit, with the 8 shared key and value groups from the transformer architecture refresher serving four query heads apiece.
During generation
Inference adds one structural fact with long consequences. When a seventh position joins the sequence, its query scores against the keys of every position up to and including itself, and collects from their values; the heads' blends then pass through the block's output projection to rejoin the stream. Attention's backward look means every earlier position's keys and values are needed again at every future step, for as long as the sequence lives.
Our trace obliged by brute force, recomputing every position's keys and values from scratch on every pass. Hold that thought through the next three lessons: the generation-loop lesson will make the repetition impossible to ignore, and what to do about it is the door the module on prefill, decode, and KV memory opens on.
The next lesson covers the rest of the block: the per-position machinery where most of the parameters, and most of the bytes, actually live.