All courses

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

▶ Watch this lesson Start the whole course

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.

Can tokens be processed one at a time?

Imagine a model that transforms each token's vector **on its own**, with any function as deep and large as you like. It then predicts the next token from position $t$'s result. In *river bank* vs *investment bank*, the vector for *bank* is the same row of $W_E$ both times. What could such a model predict differently in the two cases? And what is the most flexible way to let positions share information?

Context has to travel between positions

Per-token processing is a pure function of one token: the same input row always gives the same output, whatever surrounds it. So the guess after *bank* would be identical in every sentence. Adding the token's position wouldn't fix this. Position says where the token sits, not what surrounds it. Context must flow **between** positions. Where it should come from varies: the subject may be 2 or 20 tokens back. So the content itself should decide the routing. Each position scores earlier positions for relevance and takes a weighted average of what they offer.

Per-token map vs content-weighted mixing

Per-token: $y_t = f(e_t)$, where $e_t$ is row $x_t$ of $W_E$. So $y_t$ depends on $x_t$ only, never on $x_{<t}$. Mixing: $$y_t=\sum_{s\le t} a_{ts}\,v_s,\quad a_{ts}\ge 0,\ \sum_{s\le t}a_{ts}=1$$ Here $v_s$ is what position $s$ offers, and $a_{ts}$ is the weight $t$ puts on $s$. Fixed numbers for $a_{ts}$ would route by position only. Attention computes $a_{ts}$ from the contents at $t$ and $s$, so the same parameters route differently in each sentence.

Fixed weights vs content-based weights

At *it*, the model should fetch the animal. Values are 2D, and coordinate 2 means 'is an animal': $v_{cat}=[0,1]$, every other word $[1,0]$. For simplicity, *it* puts weight only on the three earlier words, not on itself. A: *cat sat down it*. B: *then a cat it*. Fixed weights $[0.9,0.05,0.05]$: A gives $[0.1,0.9]$ ✓. B gives $0.9[1,0]+0.05[1,0]+0.05[0,1]=[0.95,0.05]$ ✗. Content-based weights are computed from the words themselves and put 0.9 on whichever word is an animal. B: $0.05[1,0]+0.05[1,0]+0.9[0,1]=[0.1,0.9]$ ✓

Same token, different context

Token 3 comes last in both inputs, after a different earlier token. The per-token function (tanh is just some nonlinearity) returns identical outputs. The content-weighted average does not. The match scores are plain dot products of embeddings, a stand-in for learned scoring. Later positions get score −∞, so each position looks back only. Exponentiating makes scores positive (−∞ becomes 0), and dividing by the row total makes each row sum to 1. Subtracting the row max first avoids overflow and doesn't change the weights.
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

Processing each position alone maps the same token to the same output, so it cannot use context. A weighted average over earlier positions, with weights computed from content, lets one fixed set of parameters fetch information from any distance in a sentence of any length. The same mechanism can copy one position almost exactly or blend many.

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.