Chapter 7 (Attention: Mathematical Foundations) will derive scaled dot-product attention in full.
the companion book (Attention: Mathematical Foundations) will derive scaled dot-product attention in full. What matters here is a single structural fact about that mechanism that must be dealt with before attention is even introduced, because it determines what has to be added to the embeddings in the first place: attention, considered purely as a function of the token content vectors it is given, has no notion of sequence order at all.
Consider a single self-attention layer acting on a sequence of token representations stacked into a matrix , with row equal to the (as yet position-free) embedding of the -th token. Attention computes query, key, and value matrices via learned projections ,
and produces an output
Let be a permutation matrix representing some reordering of the sequence, so that has row equal to row of . Because are each obtained from by an independent linear map applied row-wise, permuting the rows of permutes the rows of each of them identically: , , . The pre-softmax score matrix transforms as
Writing out entries, : row of is row of with its columns permuted by . Because softmax is applied independently to each row and normalizes over the entries within that row regardless of what order they are listed in, permuting the columns of a row before applying softmax simply permutes the output weights the same way:
Multiplying through by and using (permutation matrices are orthogonal) gives the central result:
Self-attention is permutation equivariant: reordering the input rows reorders the output rows in exactly the same way, but the value computed for a given token is untouched by where that token sits in the sequence. The output attached to a token depends only on which other tokens are present β the multiset of keys and values available to attend over β and not on their arrangement. A layer built purely from linear projections, dot products, softmax, and weighted sums simply has no mechanism, no term anywhere in the computation, that reads off βthis is the third tokenβ versus βthis is the fifth token.β Contrast this with a recurrent network, where the hidden state is produced by iterating a step function left to right, so order is baked into the computation itself; attention has no such iteration, which is exactly what makes it parallelizable across positions β and exactly why order must be supplied as data rather than emerging from the architecture.
A small worked example makes this concrete. Take three tokens with content-only embeddings (no position information added) in :
Take for simplicity, so query, key, and value vectors equal the token embeddings themselves, and drop the scaling (it will not change the argument). Compute the attention output for the query token βbites,β attending over all three tokens including itself. The three raw scores are dot products between βbitesβ and each tokenβs key:
Softmax of gives weights (in dog, bites, man order), so the attention output for βbitesβ is
Nothing about this computation refers to whether the sentence is βdog bites manβ or βman bites dog.β Both are permutations of the same multiset of three tokens; the dot products and take the same values regardless of which token physically precedes the other in the row ordering, since dot products depend only on the pair of content vectors involved, not their row indices. The representation the model builds for βbitesβ β and, downstream, its prediction for what continues the sentence β is therefore identical in both orderings, despite the two sentences having opposite meanings (who is doing the biting). This is the concrete failure mode that positional encoding exists to prevent: without it, a Transformer literally cannot distinguish subject from object, βbeforeβ from βafter,β or any order-dependent relationship, because its core computational primitive was built to ignore order by construction.
Continue with the book
Explore the full treatment, worked examples, and exercises in LLMs Derived.
Companion article for LLMs Derived, published by Choral Software. Article text and original examples Β© 2026 Choral Software. All rights reserved.
