Injecting order: absolute position embeddings
Show that unmasked attention is permutation-equivariant, so it cannot see word order; the causal mask breaks this only weakly. Add a learned or sinusoidal position vector to each token embedding, and explain the downsides: tied to fixed positions and poor extrapolation beyond the trained length.
Builds on: Scaled dot-product attention, Causal masking
What the animation says
Same words, different news
Dog bites man. Man bites dog. Same three words, very different news. Attention scores each pair of tokens with a dot product of their two vectors. Nothing in that score mentions where either token sits. Here is dog in the first sentence, and the output vector it gets. Predict: in man bites dog, what output does dog get?
Shuffle in, shuffle out
Same vector. Each of dog's scores depends only on two vectors, and none of them changed. Reorder the input rows, and the rows and columns of the score grid reorder the same way. Each row of weights is only reshuffled, so every output follows its token to the new spot. Shuffle in, same shuffle out. That's permutation equivariance. Unmasked attention sees a set, not a sequence. This code checks it on random numbers. The last line prints True.
The mask sees a bag
Now add the causal mask: each position sees only itself and earlier tokens. The mask stays fixed when tokens move, so the clean shuffle rule breaks. But look at the last position. It learns which tokens came before, not their order. The earlier tokens reach it as an unordered bag. Tap the reordering that leaves the last output exactly the same.
Stamp the position in
The fix: stamp each token with where it sits. Each input row becomes the token's embedding plus a position vector for its slot. That vector is either learned, one row per position, or fixed sine and cosine waves. With four dimensions, one pair turns one radian per step, the other a hundredth of a radian. Now dog at position one enters differently from dog at position two. Say training used 512 positions. Predict: what happens at position 600?
Where absolute breaks
A learned table simply has no row six hundred. Sinusoids can compute one, but the slow pair lands at six radians, past the roughly five point one seen in training. There's a deeper cost too. The cat at positions three and four looks different from the cat at three hundred and three hundred one. So each pattern must be learned largely separately at every location. What matters is the distance between tokens. That's where we go next.
Attention sees a set, not a sequence
Equivariance, and the absolute fix
Check both claims numerically
import numpy as np; rng = np.random.default_rng(0)
T, d = 4, 8 # one head, d_head = d
X = rng.normal(size=(T, d)); Wq, Wk, Wv = rng.normal(size=(3, d, d))
def attn(X, mask=False):
S = (X @ Wq) @ (X @ Wk).T / np.sqrt(d)
if mask: S = S + np.triu(np.full((T, T), -np.inf), 1)
A = np.exp(S - S.max(1, keepdims=True)); A /= A.sum(1, keepdims=True)
return A @ (X @ Wv)
perm = [2, 0, 3, 1]
print(np.allclose(attn(X[perm]), attn(X)[perm])) # True
p3 = [1, 0, 2, 3] # swap first two tokens, keep last
print(np.allclose(attn(X[p3], True)[-1], attn(X, True)[-1])) # True
Sinusoids with d = 4, and where they fail
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses