Transformers, Explained: From Token Vectors to GPU Inference

About this series

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.  TT denotes token positions, dmodeld_{model} the number of hidden features per token, dkd_k Query/Key width, BB batch size and HH attention-head count. Dimensions are usually written as rows × columns. Approximate costs are analytic estimates, not measurements of a particular GPU.

  1. Part 1 – Why a Word Needs Its Neighbors
  2. Part 2 – Q, K and V: The Matching Engine
  3. Part 3 – Why Divide Attention Scores by dk\sqrt{d_k}?
  4. Part 4 – Softmax, Causal Masks and a Three-Token Walkthrough
  5. Part 5 – Multiple Heads and Positional Information
  6. Part 6 – Normalization and Residual Connections ← You are here
  7. Part 7 – Trace One Complete Transformer Block
  8. Part 8 – The Feed-Forward Network Is Not Just Another Matrix
  9. Part 9 – The GPU Cost of Attention
  10. Part 10 – FlashAttention Without the Mystery
  11. Part 11 – Why Predicting the Next Token Is a Learning Problem
  12. Part 12 – Prefill, Decode and the First Generated Token
  13. Part 13 – KV Cache, Grouped-Query Attention and PagedAttention
  14. Part 14 – What Gradients and Distributed GPUs Really Change
  15. Part 15 – Build, Verify and Profile Your Own Attention

Overview

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.

Residual connections: update rather than replace

Suppose a software system starts with state xx and computes a correction f(x)f\left(x\right). Instead of discarding the original state, it can return x+f(x).x+f\left(x\right). That is the intuition behind a residual connection:

y=x+F(x).y=x+F\left(x\right).

The sum is coordinate-wise. It requires F(x)F\left(x\right) to have the same shape as xx. Attention therefore applies an output projection to turn concatenated head outputs back to dmodeld_{model} 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.”

What exactly does a normalizer normalize?

For a standard Transformer token-wise normalizer, the statistics are calculated across features within one token. If the input tensor is B×T×dmodelB\times T\times d_{model}, 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 μ\mu, then divides by a measure of feature spread. With scale vector γ\gamma, optional shift β\beta, and stabiliser ϵ\epsilon:

LN(x)=γ⊙x−μσ2+ϵ+β.LN\left(x\right)=\gamma\odot\frac{x-\mu}{\sqrt{\sigma^2+\epsilon}}+\beta.

Here ⊙\odot means elementwise multiplication. The variance σ2\sigma^2 is calculated with division by feature count rather than the sample-statistics correction. The small positive ϵ\epsilon 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(x)=γ⊙xmean(x2)+ϵ.RMSNorm\left(x\right)=\gamma\odot\frac{x}{\sqrt{mean\left(x^2\right)+\epsilon}}.

RMSNorm can therefore retain nonzero mean direction information that LayerNorm removes. They are related mechanisms, not interchangeable spellings for one operation.

Compare both with one short example

Let a token vector be x=[1,3]x = \left[1,3\right], choose ϵ=1\epsilon = 1 deliberately to make its influence visible, and take γ=[1,1]\gamma=\left[1,1\right], β=[0,0]\beta=\left[0,0\right].

For LayerNorm, the mean is (1+3)/2=2\left(1+3\right)/2=2. Centering gives [−1,1]\left[-1,1\right], whose population-style variance is 1. Dividing by 1+1=2\sqrt{1+1}=\sqrt2 yields approximately [−0.7071,07071]\left[-0.7071,07071\right].

For RMSNorm, the mean square is (12+32)/2=5\left(1^2+3^2\right)/2=5. Dividing the original vector by 5+1=6\sqrt{5+1}=\sqrt6 gives approximately [0.4082,1.2247]\left[0.4082,1.2247\right]. These are visibly different outputs even before learned scales or biases are applied. The exaggerated ϵ=1\epsilon=1 is for teaching, not a recommended production setting.

Illustration · Normalization compared using identical input features: LayerNorm subtracts the mean; RMSNorm does not.

Pre-LN versus Post-LN is a genuine design choice

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.

Illustration · Residual paths, normalization, attention and the feed-forward network within a Transformer block.

Why the gradient path matters

A simple derivative illustrates the benefit of a residual route. If y=x+F(x)y=x+F\left(x\right), the local derivative contains an identity term:

∂y∂x=I+JF(x),\frac{\partial y}{\partial x}=I+J_F\left(x\right),

where II is the identity matrix and JFJ_F is the Jacobian, the matrix collecting local partial derivatives of FF. 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.