How Self-Attention Routes Meaning Across a Sentence
Scaled dot-product attention, why dividing by saves gradients, multi-head specialization, and positional encoding.
A token has no fixed meaning until it looks around
After tokenization , the word ” bank” enters the embedding table as the exact same vector whether it describes a check or a river. Self-attention lets every token inspect its neighbors and blend their context into a fresh representation.
Queries, Keys, and Values: a differentiable lookup table
Every attention layer projects each token vector into three vectors using learned matrices :
- Query (): What token is searching for.
- Key (): What information token advertises.
- Value (): The payload sent when a Query matches a Key.
Alignment is scored via the dot product , normalized with softmax, and summed over . Caching and across turns gives you the KV cache .
Why that tiny denominator matters
Summing unit-variance products pushes dot-product variance to . At , scores swing widely, saturating softmax near 100% on one token and starving gradients. Dividing by restores unit variance.
One head is not enough: Multi-Head Attention
A single softmax blurs multiple relationships together. Multi-Head Attention runs parallel heads () so each head can specialize in subject-verb, object, pronoun, or local syntax links.
Attention is order-blind without positional encoding
Without position signals, dot products treat text like an unordered bag of words. RoPE (Rotary Positional Embeddings) rotates and by position angle before scoring, encoding relative offset alongside the causal mask . Next, see how that attention budget behaves at 100K+ tokens in Context Engineering .