AI SYSTEMS7 MIN READ4 INTERACTIVE LABS

How Self-Attention Routes Meaning Across a Sentence

Scaled dot-product attention, why dividing by dk\sqrt{d_k} saves gradients, multi-head specialization, and positional encoding.

01

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.

Context-Aware Meaning Resolver
Same input ID for "bank"
She4%
deposited34%
the3%
check44%
at3%
the4%
bank8%
Output vector direction for "bank"Post-attention blend
Finance
92%
River
8%
Strong attention on "check" (44%) and "deposited" (34%) pulls "bank" toward financial institution.
02

Queries, Keys, and Values: a differentiable lookup table

Every attention layer projects each token vector xix_i into three vectors using learned matrices WQ,WK,WVW_Q, W_K, W_V:

  • Query (qi=xiWQq_i = x_i W_Q): What token ii is searching for.
  • Key (kj=xjWKk_j = x_j W_K): What information token jj advertises.
  • Value (vj=xjWVv_j = x_j W_V): The payload sent when a Query matches a Key.

Alignment is scored via the dot product qi⋅kjq_i \cdot k_j, normalized with softmax, and summed over VV. Caching KK and VV across turns gives you the KV cache .

Query · Key Dot Product Scorer
Query "it" angle: 22°
Key TokenSoftmax Attention Weight
server
66% (score 3.2)
crashed
31% (score 2.5)
quickly
3% (score 0.0)
Aligned Query and Key yield a high dot product, routing "server" into the Value payload for "it".
Attention⁡(Q,K,V)=softmax⁡ ⁣(QK⊤dk)V\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\!\left(\frac{Q K^{\top}}{\sqrt{d_k}}\right) V
03

Why that tiny dk\sqrt{d_k} denominator matters

Summing dkd_k unit-variance products pushes dot-product variance to Var⁡(q⋅k)=dk\operatorname{Var}(q \cdot k) = d_k. At dk=128d_k = 128, scores swing widely, saturating softmax near 100% on one token and starving gradients. Dividing by dk\sqrt{d_k} restores unit variance.

Softmax Saturation Lab (1 / √d_k)
SMOOTH · HEALTHY GRADIENTS
check
57.3%
deposited
28.5%
she
9.5%
the
4.7%
Dividing by √d_k keeps variance at 1.0 regardless of dimension, so softmax stays smooth.
04

One head is not enough: Multi-Head Attention

A single softmax blurs multiple relationships together. Multi-Head Attention runs hh parallel heads (MultiHead⁡(Q,K,V)=Concat⁡(head1,…,headh)WO\operatorname{MultiHead}(Q, K, V) = \operatorname{Concat}(\text{head}_1, \dots, \text{head}_h) W_O) so each head can specialize in subject-verb, object, pronoun, or local syntax links.

Multi-Head Attention Specialization
Subject → Verb
Thepos 0
engineerAttended
whopos 2
fixedpos 3
thepos 4
bugpos 5
deployedQuery
thepos 7
patchpos 8
"deployed" attends back to "engineer" to know who performed the action.
05

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 QQ and KK by position angle before scoring, encoding relative offset (i−j)(i - j) alongside the causal mask . Next, see how that attention budget behaves at 100K+ tokens in Context Engineering .