


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.
A common Transformer explanation stops just as it becomes useful. It shows attention as a box, adds an FFN box beside it, and calls the result a Transformer. But how does a token actually travel through those boxes? We can trace one complete, deliberately tiny decoder block while keeping tensor shapes and numerical roles visible.
Our source masterclass calculates a three-token, two-feature Pre-LN decoder block and verifies its outputs with a deterministic NumPy implementation. Here we will preserve the important transitions without reproducing every multiplication in that longer derivation.
Take one sequence with three token positions: batch size , token count , model width . There is one Query head and one Key/Value head, with . The FFN temporarily expands to three features. This is much smaller than a production model, but the types of operations are the same.
The block input is a hidden-state tensor, not raw token IDs:
Our notation suppresses the leading batch dimension for clarity. The model uses RMSNorm with , a purposely large value chosen only to make the hand calculations easy to inspect. The Query and Key projections are identity matrices in this toy case; the Value projection doubles the second feature. Trained models need not use these special weights.
Apply RMSNorm separately to each token row to obtain . The first row has mean square , giving . The third row becomes approximately .
Then project the normalized states:
Every projection preserves the three positions. Query and Key have the same comparison width, so creates a score matrix. The causal mask forbids future positions, and stable softmax normalises each permitted row.
The third Query can consult all three Keys. Its scaled scores in the source example are approximately . Softmax gives weights .
Multiplying those weights by the three Value vectors gives the third contextual attention mixture:
The learned output projection mixes features and changes this to . Now comes the first residual addition: the original input row is added to the projected attention result, producing
The distinction matters: the residual adds the projected attention output, not the attention probability row and not the raw Value tensor.
A second RMSNorm takes the updated residual , producing . For the third token the normalised row is approximately . The FFN applies an expansion matrix, an activation and a contraction matrix. In this example, the activation is ReLU: negative intermediate features are set to zero and positive ones pass through.
The FFN uses the same learned weights at every token position, but it computes each row independently. The cross-token communication already happened in attention. Finally, a second residual addition produces , preserving the original number of tokens and the external feature width.

The source implementation reports the final hidden-state matrix as
There are still three rows and two columns. The entries are learned-feature coordinates after one block, not three generated words. A real decoder ordinarily runs additional blocks and possibly a final normalizer, then applies a vocabulary projection to form logits, from which a token-selection policy can generate text.
The source checks an equally important causal property: change only the third input token and the first two output rows must remain unchanged. That is a more useful correctness test for causal information flow than merely checking that each softmax row sums to one.
An encoder with bidirectional self-attention generally permits a token to use left and right context. A causal decoder cannot read future positions. In encoder-decoder designs, cross-attention draws Queries from decoder states while Keys and Values come from encoder output states. Those are different rules about vector sources and visibility, even though the same underlying dot-product operation appears.
Takeaway. A complete Pre-LN block is a sequence of two updates: normalize → attention → output projection → residual, then normalize → FFN → residual. Its result is another set of hidden states, not yet a token prediction.
Next: Part 8 expands the FFN itself, explaining why it widens, activates and contracts each token vector.