Skip to content
nonconvexcard

The Mathematics of Transformers

Attention derived from scratch, why the scaling is 1/sqrt(d_k), multi-head as a low-rank sum, positional encodings, residual and normalisation placement, and the exact complexity.

Deep LearningGraduate⏱ 1 day15 min readtransformerattentionsoftmaxmulti-headpositional encoding
7 problems →

Attention

Let X∈Rn×dX\in\R^{n\times d} hold nn token representations as rows.

DefinitionScaled dot-product attention
Q=XWQ,K=XWK,V=XWV,WQ,WK∈Rd×dk, WV∈Rd×dv,Q = XW_Q,\quad K = XW_K,\quad V = XW_V, \qquad W_Q,W_K\in\R^{d\times d_k},\ W_V\in\R^{d\times d_v},Attention⁡(Q,K,V)=softmax⁡ ⁣(QK⊤dk)⏟A ∈ Rn×nV,\operatorname{Attention}(Q,K,V) = \underbrace{\operatorname{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)}_{A\ \in\ \R^{n\times n}}V ,

with the softmax applied row-wise. AA is row-stochastic: Aij≥0A_{ij}\ge0 and ∑jAij=1\sum_jA_{ij}=1, so row ii of the output is a convex combination of the value vectors.

PropositionWhy the scaling is 1/sqrt(d_k)

Suppose the entries of qiq_i and kjk_j are independent with mean 00 and variance σ2\sigma^2, as at standard initialisation. Then

E⟨qi,kj⟩=0,Var⁡(⟨qi,kj⟩)=dkσ4,\E\ip{q_i}{k_j} = 0, \qquad \Var\big(\ip{q_i}{k_j}\big) = d_k\sigma^4 ,

so ⟨qi,kj⟩\ip{q_i}{k_j} has standard deviation σ2dk\sigma^2\sqrt{d_k}, growing with dkd_k. Dividing by dk\sqrt{d_k} makes the standard deviation σ2\sigma^2, independent of the head dimension.

PropositionSoftmax saturation kills the gradient

Let a=softmax⁡(s)a = \operatorname{softmax}(s) for logits s∈Rns\in\R^n. Then

∂a∂s=diag⁡(a)−aa⊤,\frac{\partial a}{\partial s} = \diag(a) - aa^\top ,

and for any vv, v⊤(diag⁡(a)−aa⊤)v=Var⁡j∼a(vj)v^\top\big(\diag(a)-aa^\top\big)v = \Var_{j\sim a}(v_j). Hence if aa concentrates on one index, the Jacobian is approximately zero.

Key idea

Combine the two propositions. Without the 1/dk1/\sqrt{d_k} factor, logits would have standard deviation growing like dk\sqrt{d_k}; with dk=64d_k = 64 and σ2=1\sigma^2=1 that is a spread of 88 across positions, which after softmax is near-one-hot. The Jacobian is then a variance of a near-degenerate distribution, i.e. near zero, and no gradient flows through the attention weights. The scaling is not cosmetic: it is what keeps the softmax in its informative regime at initialisation.

PropositionAttention is permutation equivariant

Let PP be an n×nn\times n permutation matrix. Then

Attention⁡((PX)WQ,(PX)WK,(PX)WV)=P⋅Attention⁡(Q,K,V).\operatorname{Attention}\big((PX)W_Q,(PX)W_K,(PX)W_V\big) = P\cdot\operatorname{Attention}(Q,K,V).

Consequently a transformer without positional information computes a function of the input multiset, not the sequence.

This is why positional encodings are required, not an enhancement. The MLP and normalisation sublayers act position-wise and are therefore also permutation equivariant, so nothing else in the architecture breaks the symmetry.

Multi-head attention

MHA⁡(X)=Concat⁡(head1,…,headh)WO,headm=Attention⁡(XWQm,XWKm,XWVm),\operatorname{MHA}(X) = \operatorname{Concat}\big(\mathrm{head}_1,\dots,\mathrm{head}_h\big)W_O, \qquad \mathrm{head}_m = \operatorname{Attention}(XW_Q^m, XW_K^m, XW_V^m),

with dk=dv=d/hd_k = d_v = d/h so that the total parameter count matches a single head of full width.

PropositionMulti-head attention is a sum of low-rank attention maps

Partition WOW_O into blocks WOm∈Rdv×dW_O^m\in\R^{d_v\times d}. Then

MHA⁡(X)=∑m=1hAmX WVmWOm⏟rank ≤ dv=d/h,\operatorname{MHA}(X) = \sum_{m=1}^h A^m X\,\underbrace{W_V^mW_O^m}_{\text{rank}\ \le\ d_v = d/h},

where Am=softmax⁡(XWQm(WKm)⊤X⊤/dk)A^m = \operatorname{softmax}\big(XW_Q^m(W_K^m)^\top X^\top/\sqrt{d_k}\big). Each head contributes an attention pattern AmA^m composed with a rank-≤d/h\le d/h value transformation, and each logit matrix is determined by the rank-≤d/h\le d/h matrix WQm(WKm)⊤W_Q^m(W_K^m)^\top.

Key idea

The interesting quantity is not WQW_Q and WKW_K separately but their product WQKm=WQm(WKm)⊤∈Rd×dW_{QK}^m = W_Q^m(W_K^m)^\top\in\R^{d\times d}, of rank at most d/hd/h — and likewise WVOm=WVmWOmW_{VO}^m = W_V^mW_O^m. A head is fully specified by these two low-rank matrices, which is the observation underlying the circuit-level analyses in the interpretability literature, and also the observation that makes LoRA natural: if the operative objects are already low rank, a low-rank update is an appropriate parameterisation.

Multiple heads are therefore a rank allocation choice: hh heads of rank d/hd/h each rather than one map of rank dd. The total parameter count is the same; what changes is that each head is forced to attend according to a single low-dimensional similarity structure, and different heads can specialise.

Positional information

DefinitionSinusoidal positional encoding
PE(p)2m=sin⁡ ⁣(p100002m/d),PE(p)2m+1=cos⁡ ⁣(p100002m/d),\mathrm{PE}(p)_{2m} = \sin\!\left(\frac{p}{10000^{2m/d}}\right), \qquad \mathrm{PE}(p)_{2m+1} = \cos\!\left(\frac{p}{10000^{2m/d}}\right),

added to the token embedding at position pp. Because

(sin⁡(ω(p+Δ))cos⁡(ω(p+Δ)))=(cos⁡ωΔsin⁡ωΔ−sin⁡ωΔcos⁡ωΔ)(sin⁡ωpcos⁡ωp),\begin{pmatrix}\sin(\omega(p+\Delta))\\\cos(\omega(p+\Delta))\end{pmatrix} = \begin{pmatrix}\cos\omega\Delta & \sin\omega\Delta\\ -\sin\omega\Delta & \cos\omega\Delta\end{pmatrix} \begin{pmatrix}\sin\omega p\\\cos\omega p\end{pmatrix},

a shift by Δ\Delta acts as a fixed linear map on each frequency pair — so relative position is linearly available to the model.

PropositionRotary embeddings make the logit depend only on relative position

Let RpR_p be the block-diagonal rotation acting on coordinate pairs (2m,2m+1)(2m,2m+1) by angle θmp\theta_mp, and define q~p=Rpqp\tilde q_p = R_pq_p, k~p′=Rp′kp′\tilde k_{p'} = R_{p'}k_{p'}. Then

⟨q~p,k~p′⟩=⟨qp,Rp′−p kp′⟩,\ip{\tilde q_p}{\tilde k_{p'}} = \ip{q_p}{R_{p'-p}\,k_{p'}} ,

so the attention logit depends on pp and p′p' only through p′−pp'-p.

This is why rotary embeddings (RoPE) are now standard: relative position is built into the inner product exactly, rather than being linearly recoverable as with additive sinusoids, and the construction extends to unseen positions.

Residual stream, normalisation and the MLP block

A pre-norm transformer layer is

X←X+MHA⁡(LN⁡(X)),X←X+MLP⁡(LN⁡(X)),X \leftarrow X + \operatorname{MHA}\big(\operatorname{LN}(X)\big), \qquad X \leftarrow X + \operatorname{MLP}\big(\operatorname{LN}(X)\big),

with MLP⁡(x)=W2 ρ(W1x)\operatorname{MLP}(x) = W_2\,\rho(W_1x), W1∈R4d×dW_1\in\R^{4d\times d}, W2∈Rd×4dW_2\in\R^{d\times4d}.

The additive form means XX is a residual stream that every sublayer reads from and writes to, with Jacobian I+∂F/∂XI + \partial F/\partial X — the same identity-path mechanism as the LSTM cell state, with the gate fixed at one.

Empirical observationPre-norm versus post-norm

Empirical observation — a regularity seen in experiments. No theorem on this page establishes it in general.

The original transformer used post-norm, X←LN⁡(X+F(X))X\leftarrow\operatorname{LN}(X+F(X)), and required a learning-rate warmup to train at all beyond a modest depth. Pre-norm trains stably without warmup and is now universal for large models.

What this does not say

Xiong et al. give a partial account: in post-norm the expected gradient norm at initialisation grows with depth, so the first steps are too large and destroy the initialisation; warmup masks this by starting small. In pre-norm the gradient norm is roughly depth-independent. Their analysis is at initialisation and under simplifying assumptions, so it explains the need for warmup rather than everything about the two variants' behaviour throughout training. The practical choice is settled; the theory is suggestive rather than complete.

HeuristicAttention as a kernel smoother

Heuristic — a reasoning shortcut that predicts the right answer in the regimes discussed here. It is not a proof and can fail outside them.

Write the attention output for query ii as

oi=∑jexp⁡(⟨qi,kj⟩/dk)∑j′exp⁡(⟨qi,kj′⟩/dk) vj=∑jκ(qi,kj)vj∑jκ(qi,kj),κ(q,k)=e⟨q,k⟩/dk.o_i = \sum_j \frac{\exp\big(\ip{q_i}{k_j}/\sqrt{d_k}\big)}{\sum_{j'}\exp\big(\ip{q_i}{k_{j'}}/\sqrt{d_k}\big)}\,v_j = \frac{\sum_j \kappa(q_i,k_j)v_j}{\sum_j\kappa(q_i,k_j)}, \qquad \kappa(q,k) = e^{\ip qk/\sqrt{d_k}} .

This is exactly a Nadaraya–Watson kernel-weighted average with kernel κ\kappa.

What this does not say

The analogy is genuinely useful: it explains why attention is a smoother, why temperature (the 1/dk1/\sqrt{d_k}) controls bandwidth, and it motivates the linear-attention family, which replaces κ\kappa by a factorisable ⟨ϕ(q),ϕ(k)⟩\ip{\phi(q)}{\phi(k)} and thereby reduces the cost from O(n2d)O(n^2d) to O(nd2)O(nd^2).

But κ(q,k)=e⟨q,k⟩/dk\kappa(q,k) = e^{\ip qk/\sqrt{d_k}} is not symmetric in the sense a kernel smoother assumes — queries and keys come from different projections of the same input, so κ\kappa is a similarity between two different representations of tokens, not a distance-based kernel on a single space. It is positive definite as a function of (q,k)(q,k) pairs via the exponential of a linear kernel, but the Nadaraya–Watson theory that gives rates for kernel smoothers assumes a fixed kernel and i.i.d. data, neither of which holds. Treat the analogy as a source of architectural ideas, not as a route to guarantees.

Complexity

PropositionComplexity of a transformer layer

For sequence length nn, model width dd, and MLP expansion factor 44:

ComponentFLOPsMemory (activations)
Q,K,VQ,K,V projections3nd23nd^23nd3nd
Logits QK⊤QK^\topn2dn^2dn2n^2 (per head, hn2hn^2 total)
AVAVn2dn^2dndnd
Output projectionnd2nd^2ndnd
MLP8nd28nd^24nd4nd
TotalΘ(nd2+n2d)\Theta(nd^2 + n^2d)Θ(nd+hn2)\Theta(nd + hn^2)

Attention dominates the MLP when n≳dn \gtrsim d.

Key idea

The crossover at n≈dn\approx d is worth internalising. For a model with d=4096d = 4096 and context n=2048n = 2048, the MLP dominates and attention is not the bottleneck. At n=128,000n = 128{,}000 attention dominates by a factor of 3030. This is why long-context work is about attention specifically, and why the O(n2)O(n^2) memory for the attention matrix is often the harder constraint than the FLOPs — FlashAttention's contribution is to compute exact attention without ever materialising the n×nn\times n matrix, by tiling and recomputation, reducing memory to O(n)O(n) at no accuracy cost.

Problems

Why attention divides by the square root of d_kProof · Intermediate · 30 min The softmax Jacobian as a variance, and saturationProof · Intermediate · 30 min Attention is permutation equivariantProof · Intermediate · 30 min Multi-head attention is a sum of low-rank mapsProof · Advanced · 30 min Rotary embeddings encode relative position exactlyProof · Advanced · 30 min When does attention dominate the MLP?Numerical · Beginner · 30 min Implement multi-head attention from scratchCoding · Intermediate · 3 hours

References

These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.

Results stated on this page

Problems (7)

All problems

Papers that use this mathematics