Why attention needs information about token order

← Back to Blog & Articles

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 nn token representations stacked into a matrix π—βˆˆβ„nΓ—dmodel\mathbf{X} \in \mathbb{R}^{n \times d_{\text{model}}}, with row ii equal to the (as yet position-free) embedding of the ii-th token. Attention computes query, key, and value matrices via learned projections 𝐖Q,𝐖K,𝐖V\mathbf{W}_Q, \mathbf{W}_K, \mathbf{W}_V,

𝐐=𝐗𝐖Q,𝐊=𝐗𝐖K,𝐕=𝐗𝐖V, \mathbf{Q} = \mathbf{X}\mathbf{W}_Q, \quad \mathbf{K} = \mathbf{X}\mathbf{W}_K, \quad \mathbf{V} = \mathbf{X}\mathbf{W}_V,

and produces an output

Attn(𝐗)=softmax(𝐐𝐊⊀dk)𝐕. \text{Attn}(\mathbf{X}) = \text{softmax}\!\left(\frac{\mathbf{Q}\mathbf{K}^\top}{\sqrt{d_k}}\right)\mathbf{V}.

Let 𝐏∈{0,1}nΓ—n\mathbf{P} \in \{0,1\}^{n \times n} be a permutation matrix representing some reordering Οƒ\sigma of the sequence, so that 𝐗′=𝐏𝐗\mathbf{X}' = \mathbf{P}\mathbf{X} has row ii equal to row Οƒ(i)\sigma(i) of 𝐗\mathbf{X}. Because 𝐐,𝐊,𝐕\mathbf{Q}, \mathbf{K}, \mathbf{V} are each obtained from 𝐗\mathbf{X} by an independent linear map applied row-wise, permuting the rows of 𝐗\mathbf{X} permutes the rows of each of them identically: 𝐐′=𝐏𝐐\mathbf{Q}' = \mathbf{P}\mathbf{Q}, πŠβ€²=𝐏𝐊\mathbf{K}' = \mathbf{P}\mathbf{K}, 𝐕′=𝐏𝐕\mathbf{V}' = \mathbf{P}\mathbf{V}. The pre-softmax score matrix transforms as

πβ€²πŠβ€²βŠ€=𝐏𝐐𝐊⊀𝐏⊀=𝐏𝐌𝐏⊀,𝐌:=𝐐𝐊⊀. \mathbf{Q}'\mathbf{K}'^\top = \mathbf{P}\mathbf{Q}\mathbf{K}^\top\mathbf{P}^\top = \mathbf{P}\mathbf{M}\mathbf{P}^\top, \qquad \mathbf{M} := \mathbf{Q}\mathbf{K}^\top.

Writing out entries, (𝐏𝐌𝐏⊀)ij=MΟƒ(i)Οƒ(j)(\mathbf{P}\mathbf{M}\mathbf{P}^\top)_{ij} = M_{\sigma(i)\sigma(j)}: row ii of 𝐏𝐌𝐏⊀\mathbf{P}\mathbf{M}\mathbf{P}^\top is row Οƒ(i)\sigma(i) of 𝐌\mathbf{M} with its columns permuted by Οƒ\sigma. 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:

softmax(𝐏𝐌𝐏⊀)=𝐏softmax(𝐌)𝐏⊀. \text{softmax}(\mathbf{P}\mathbf{M}\mathbf{P}^\top) = \mathbf{P}\,\text{softmax}(\mathbf{M})\,\mathbf{P}^\top .

Multiplying through by 𝐕′=𝐏𝐕\mathbf{V}' = \mathbf{P}\mathbf{V} and using 𝐏⊀𝐏=𝐈\mathbf{P}^\top\mathbf{P} = \mathbf{I} (permutation matrices are orthogonal) gives the central result:

Attn(𝐏𝐗)=𝐏Attn(𝐗).\begin{equation} \text{Attn}(\mathbf{P}\mathbf{X}) = \mathbf{P}\,\text{Attn}(\mathbf{X}). \end{equation}

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 ℝ2\mathbb{R}^2:

𝐱dog=(10),𝐱bites=(01),𝐱man=(11). \mathbf{x}_{\text{dog}} = \begin{pmatrix}1\\0\end{pmatrix}, \qquad \mathbf{x}_{\text{bites}} = \begin{pmatrix}0\\1\end{pmatrix}, \qquad \mathbf{x}_{\text{man}} = \begin{pmatrix}1\\1\end{pmatrix}.

Take 𝐖Q=𝐖K=𝐖V=𝐈2\mathbf{W}_Q = \mathbf{W}_K = \mathbf{W}_V = \mathbf{I}_2 for simplicity, so query, key, and value vectors equal the token embeddings themselves, and drop the 1/dk1/\sqrt{d_k} 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:

πͺbites⋅𝐀dog=0,πͺbites⋅𝐀bites=1,πͺbites⋅𝐀man=1. \mathbf{q}_{\text{bites}} \cdot \mathbf{k}_{\text{dog}} = 0, \qquad \mathbf{q}_{\text{bites}} \cdot \mathbf{k}_{\text{bites}} = 1, \qquad \mathbf{q}_{\text{bites}} \cdot \mathbf{k}_{\text{man}} = 1.

Softmax of [0,1,1] gives weights β‰ˆ[0.1554,0.4223,0.4223]\approx [0.1554,\ 0.4223,\ 0.4223] (in dog, bites, man order), so the attention output for β€œbites” is

0.1554(10)+0.4223(01)+0.4223(11)=(0.57770.8446). 0.1554\begin{pmatrix}1\\0\end{pmatrix} + 0.4223\begin{pmatrix}0\\1\end{pmatrix} + 0.4223\begin{pmatrix}1\\1\end{pmatrix} = \begin{pmatrix}0.5777\\0.8446\end{pmatrix}.

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 πͺbites⋅𝐀dog\mathbf{q}_{\text{bites}} \cdot \mathbf{k}_{\text{dog}} and πͺbites⋅𝐀man\mathbf{q}_{\text{bites}} \cdot \mathbf{k}_{\text{man}} 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.

Scroll to Top