


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.
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.
One attention head produces one set of Query-Key comparisons and one Value mixture for each token. Why have several heads? Because a single set of learned projections is not obliged to capture every useful relationship. Multiple heads provide multiple learned comparison spaces that can be combined into a richer representation.
It is common to hear that one head “finds grammar” and another “finds factual relations.” That may be an intuitive metaphor, but it is not a definition of multi-head attention. Each head has its own learned projections, and what it does emerges from training. Different heads can become redundant, correlated or specialized; no head is guaranteed a neat human label.
In a conventional arrangement, model width is divided among Query heads. For example, and yields head width if the architecture partitions width equally.
Each head calculates attention independently, returning Value features for each token. The outputs are then concatenated along the feature axis, not added as probability matrices. A learned output projection mixes the concatenated features back to model width, allowing a residual connection to add the result to the original hidden state.

Suppose a token’s hidden vector has width 8. Two heads each produce four output features, so concatenation produces eight features. The output projection can then map . For a sequence of length , each head returns a Value mixture; concatenation returns .
The head outputs are not averaged simply because there are two heads. Nor do we need two GPUs: a head is a logical numerical operation, not a hardware processor.
Changing head count at fixed total projected width does not automatically increase Q/K/V projection parameters. With heads each outputting Query features, the combined Query weight count is . This cancellation is a useful antidote to the misconception that twice as many heads always means twice as many weights.
Many serving architectures distinguish the number of Query heads from the number of Key/Value heads . In conventional multi-head attention (MHA), the counts are equal. In multi-query attention (MQA), many Query heads share one Key/Value head. Grouped-query attention (GQA) uses fewer Key/Value heads than Query heads, with Query heads grouped around those shared K/V representations.
With 32 Query heads and 8 Key/Value heads, a KV cache needs roughly one-quarter as many cached head entries as 32-KV-head MHA, when all other cache dimensions are fixed. That improves serving-memory requirements, though it does not mean Query-head computations disappear. Part 13 derives the byte formula.
Plain content-only self-attention has no intrinsic sequence-order information. If you permute token rows and apply the same operations without positional signals or order-sensitive masking, the outputs permute correspondingly. That is called permutation equivariance. To distinguish “the dog chased the cat” from “the cat chased the dog,” models need positional handling in addition to token content.
Some approaches add learned or fixed positional vectors. Rotary Position Embeddings (RoPE) instead rotate pairs of Query and Key coordinates using angles associated with their positions. A 2D rotation preserves vector length, but changes its alignment with another vector. Thus token position influences the Query-Key dot product.
For rotation matrix , a Query at position and Key at can satisfy
The key result is that the pairwise comparison contains the position difference . Production RoPE uses many coordinate pairs and frequencies, not merely one rotation of 90 degrees. RoPE does not itself impose causal visibility or guarantee unlimited context extrapolation.
Positional information answers how tokens are located relative to one another. A causal mask answers which positions a Query is allowed to consult. They do different jobs. A model can use RoPE with a causal mask, but neither mechanism should be mistaken for the other.
Takeaway. Multiple heads create separate learned attention views and an output projection combines them; positional methods help the model distinguish token order. GQA modifies Key/Value sharing primarily to improve inference memory, a topic we will revisit after understanding decoding.
Next: Part 6 supplies the missing architecture glue: normalization, residual connections and why their placement matters.