Prove that a transformer encoder layer without positional encodings is permutation-equivariant: permuting the input tokens permutes the outputs in the same way. How do you show that a decoder with a causal mask and no positional encodings is not, and that it can still recover position information?

Sequence models and transformers, Attention
Level L5 Expert. Length Long. Nature Th+Mat Both.

What's being asked

Whether you can prove equivariance with permutation matrices and explain how the causal mask breaks it.

Key idea

For a permutation matrix P, softmax(PSP⊤)=P softmax(S) P⊤, so Attn(PX)=P Attn(X). A causal mask M is not preserved by P, so equivariance fails.

Steps

  1. Let X∈Rn×d hold the token vectors, Q=XWQ, K=XWK, V=XWV, S=QK⊤/dk and Attn(X)=softmax(S)V. A permuted input is PX.
  2. Then Q′=PQ, K′=PK, V′=PV and S′=PQK⊤P⊤/dk=PSP⊤.
  3. Softmax acts on each row independently and permuting columns just reorders entries within a row, so softmax(PSP⊤)=P softmax(S) P⊤.
  4. Output: P softmax(S) P⊤PV=P softmax(S)V, using P⊤P=I. So Attn(PX)=P Attn(X).
  5. The feed-forward network, layer norm and residual connection act on each token separately, so they are equivariant too. Hence the whole layer is, and the model cannot tell word order without positional encodings.
  6. Causal mask: S′=PSP⊤+M, but PSP⊤ with the original mask M is not the same as P(S+M)P⊤, since PMP⊤≠M in general. The proof breaks.
  7. Position leak: suppose the first token is a special BOS whose value vector has a coordinate equal to 1, and all other tokens have 0 there. If the scores are all equal, position i attends uniformly to i tokens, so that coordinate of the output is 1/i. The model can read off the position from it.

Common mistakes

How long your answer should be

One page; 8 minutes.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: FlashAttention computes softmax attention one block of keys at a time, without storing the full score matrix. Derive the online softmax recurrence that keeps a running maximum m and a running sum ℓ, and test it on the scores (1,3,2,5) split into the blocks (1,3) and (2,5) with values (1,2,3,4)?
Next in Sequence models and transformers: In speculative decoding, a small draft model with next-token distribution q proposes a token x∼q, and the large target model with distribution p accepts it with probability min⁡(1,p(x)/q(x)). If it is rejected, a token is drawn from the residual ∝max⁡(0,p−q). Show that the output token is distributed exactly as p. For p=(0.5,0.3,0.2) and q=(0.2,0.5,0.3), find the acceptance probability and the expected number of tokens per target pass when k=4 tokens are drafted?