Why positions must exchange information
Explain that per-token processing alone cannot use context. Explain why a content-dependent weighted average over other positions is a flexible way to route information between them.
Builds on: Token embeddings as a lookup table, Attention in plain words
What the animation says
Same token, same output
Take two phrases: river bank, and investment bank. First, each word becomes a row of the embedding table. Bank is the same row both times. Now imagine a model that transforms each token's vector on its own, with a function as deep and large as you like. Same row in, same output out. It's a pure function of one token. So the guess for what follows bank can't differ between the two phrases. Suppose we also add bank's position to its vector. Would that let the two guesses differ?
Context must travel
Position says where a token sits, not what surrounds it. Both banks sit second, so nothing changes. The difference lives in the other positions: river, or investment. That information has to flow between positions. Where it should come from varies. The word you need might be two tokens back, or twenty. So let the content decide the routing. Each position scores earlier positions for relevance. Then it takes a weighted average of what they offer. The weights are never negative and add up to one.
Fixed weights
A tiny test. At the word it, the model should fetch the animal. Each word offers two numbers; the second means 'is an animal'. Cat offers zero, one; every other word, one, zero. It looks only at the three earlier words. Try fixed weights by position: point nine, point oh five, point oh five. Sentence A: cat sat down it. Point nine lands on cat, giving point one, point nine. An animal. Good. Now sentence B: then a cat it. Same fixed weights. What does it get?
Content decides the weights
Fixed weights put point nine on then, so it gets point nine five, point oh five. Wrong. Fixed numbers route by position only. But the cat moved. Content-based weights are computed from the words themselves. They put point nine on whichever word is an animal. In B that's cat: point nine times zero, one, plus a little from the others, gives point one, point nine. Right again. Your turn. Sentence C: a dog barked it. Dog is an animal too. Tap where content-based weights put point nine.
Proof in code
In NumPy: the per-token function takes each token's row and passes it through tanh. The mix function scores pairs of positions with dot products. Later positions get minus infinity, so each looks back only. Exponentiate and divide by the row total: weights that sum to one. Then average. Two inputs end in the same token, after different earlier tokens. Per-token: the last outputs are identical. Mix: they differ. Per-token can't use context. Content-weighted mixing can, at any distance, copying one position or blending many.
Context has to travel between positions
Per-token map vs content-weighted mixing
Fixed weights vs content-based weights
Same token, different context
import numpy as np
rng = np.random.default_rng(0)
W_E, W = rng.normal(size=(5, 4)), rng.normal(size=(4, 4)) # V=5, d=4
def per_token(ids): return np.tanh(W_E[ids] @ W)
def mix(ids):
X = W_E[ids]; S = X @ X.T + np.triu(np.full((len(ids),) * 2, -np.inf), 1) # -inf on later positions
A = np.exp(S - S.max(1, keepdims=True))
A /= A.sum(1, keepdims=True) # nonnegative weights, each row sums to 1
return A @ X
a, b = [1, 3], [2, 3] # same last token, different earlier token
print(per_token(a)[-1] - per_token(b)[-1]) # all zeros
print(mix(a)[-1] - mix(b)[-1]) # nonzero
What to 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