


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.
Attention is the famous part of a Transformer, but an attention output alone is not a complete block. Modern models also use normalization to control feature magnitudes and residual connections to preserve and update the running representation. These two mechanisms are not decorative extras. Their position in the computation changes the actual forward values and the gradient paths available during training.
Suppose a software system starts with state and computes a correction . Instead of discarding the original state, it can return That is the intuition behind a residual connection:
The sum is coordinate-wise. It requires to have the same shape as . Attention therefore applies an output projection to turn concatenated head outputs back to before adding them to the residual stream. The FFN also contracts its output back to the model width for the same reason.
In deep networks, a residual gives information and gradients a direct additive route across the sublayer. It does not solve every optimisation problem or guarantee that arbitrarily deep models will train, but it changes the architecture from “replace everything at each layer” to “learn useful updates.”
For a standard Transformer token-wise normalizer, the statistics are calculated across features within one token. If the input tensor is , normalization preserves that shape. It does not average across all tokens, silently inspect future tokens, or change the sequence length.
LayerNorm first subtracts the feature mean , then divides by a measure of feature spread. With scale vector , optional shift , and stabiliser :
Here means elementwise multiplication. The variance is calculated with division by feature count rather than the sample-statistics correction. The small positive helps prevent division by zero.
RMSNorm is different: it does not subtract the mean. It divides by the root mean square, then applies a learned scale:
RMSNorm can therefore retain nonzero mean direction information that LayerNorm removes. They are related mechanisms, not interchangeable spellings for one operation.
Let a token vector be , choose deliberately to make its influence visible, and take , .
For LayerNorm, the mean is . Centering gives , whose population-style variance is 1. Dividing by yields approximately .
For RMSNorm, the mean square is . Dividing the original vector by gives approximately . These are visibly different outputs even before learned scales or biases are applied. The exaggerated is for teaching, not a recommended production setting.

In a pre-normalization (Pre-LN) decoder block, the first normalizer prepares the attention input. The attention update is then added to the unnormalized residual stream. A second normalizer prepares the FFN input, and the FFN update is added to its own residual stream:
In this notation, the Attention function includes the learned Q/K/V projections, the attention operation and the output projection. Post-LN places normalization after each residual addition. The order affects both numerical outputs and backpropagation; you cannot swap the order without changing the model.

A simple derivative illustrates the benefit of a residual route. If , the local derivative contains an identity term:
where is the identity matrix and is the Jacobian, the matrix collecting local partial derivatives of . In a Pre-LN block, this direct additive path bypasses the sublayer’s internal normalization and transformations. In Post-LN, the output normaliser acts on the sum, so its Jacobian also affects that route.
This is one reason normalization placement matters for deep-network optimization. It is not proof that one design always wins under all settings. Different architectures and training schemes choose different compromises.
Takeaway. Residuals preserve a model-width information stream while sublayers compute updates. LayerNorm and RMSNorm control token-feature statistics in different ways. Pre-LN and Post-LN differ in calculation order and gradient flow, not merely in diagram aesthetics.