


Transformers power modern language models, but explanations often jump from a colorful attention diagram to dense matrix notation. This series takes the middle route: clear intuition, enough mathematics to verify the mechanism, and practical implications for model architecture and GPU inference. It assumes familiarity with programming, arrays and basic algebra, not previous experience building Transformers.
Reading path. Parts 1–4 build one attention calculation. Parts 5–8 assemble a Transformer block, including positional information and feed-forward networks (FFNs). Parts 9–13 connect the math to memory-efficient GPU execution and text generation. Parts 14–15 close with training, implementation and evidence-based performance analysis. Each post can be published separately; equations, diagrams and examples are selected and simplified from the accompanying technical masterclass rather than copied wholesale.
Editorial conventions. denotes token positions, the number of hidden features per token, Query/Key width, batch size and attention-head count. Dimensions are usually written as rows × columns. Approximate costs are analytic estimates, not measurements of a particular GPU.
When one token needs information from other tokens, two different questions arise: Which positions are worth listening to? And what information should be taken from them? Self-attention separates these jobs with three learned projections: Query (Q), Key (K) and Value (V).
The database analogy is useful here. A Query resembles a search request, a Key resembles the index entry used for comparison, and a Value resembles the record content. But unlike a normal lookup returning one exact row, attention usually mixes information from many permissible positions.
Let contain the hidden-state vectors entering an attention layer. For token positions of model width , has shape if we omit the batch dimension. Three learned weight matrices produce new views of those same token vectors:
These are not three copies of . They are distinct trained transformations, so the representation that is useful for matching can differ from the representation that is useful for carrying information. In an illustrative small model, each projection might return vectors of width 2 even when the model hidden-state width is 4.
We often use these roles as a memory aid: a Query asks, a Key is compared, and a Value contributes to the output. That is a description of the computation, not evidence that each vector has a human-readable linguistic meaning.
For one Query and one Key , the dot product is
A larger dot product indicates greater learned numerical compatibility under the current projections. The score is not yet a probability. It can be negative, greater than one, or affected by vector magnitude. Later, softmax converts a Query’s scores across Keys into nonnegative weights that sum to one.
For Query token and Key token
, with shared feature width , the unscaled score is
The index runs over features, whereas
and
identify token positions. Confusing those two axes is a common cause of shape errors.
Suppose there are three Query positions and four Key positions, each with two comparison features. Then has shape , has shape , and transposing changes its shape to . Their product is
The output has three rows, one for each Query, and four columns, one for each candidate Key. The inner dimension of two matches because both vectors occupy the same comparison space. Transposition only swaps axes: it cannot make a two-feature Query compatible with a three-feature Key.

Queries and Keys must have compatible widths because they participate in a dot product. Values enter a different operation. If the attention-weight matrix is and there are four Values of width 5, multiplying the weights by produces . Each Query receives a five-feature output.
This distinction is easy to lose in diagrams where Q, K and V happen to have identical shapes. The Value width is an architectural choice; it is not logically required to equal .
Imagine choosing expert reviewers for a technical document. The attributes used to decide whose opinion is relevant need not be the same as the information each reviewer contributes. Attention learns this separation: Queries and Keys determine weights, while Values provide the quantities mixed by those weights.
In a trained model, all these representations are distributed, context-dependent vectors. We should not assert that one Query means “question” or one head means “grammar”. Instead, the computation is precise: compare Q with K, transform scores into weights, then weight V.
Takeaway. Attention has two stages that must not be conflated. creates a matrix of compatibility scores across token positions. Only after scaling and softmax do those scores become weights for mixing the Value vectors.
Next: Part 3 asks why these dot-product scores are divided by the square root of the head width.