Theory ofIntelligence
Index
Transformer Architecture & LLMsBook Index: Letter S

Self-Attention: The Foundation of Modern Transformers

How scaled dot-product attention allows every token to dynamically query and weight all other tokens in a sequence, eliminating the sequential bottlenecks of RNNs.

functionsMathematical Representation
Attention(Q,K,V)=softmax(QK⊤dk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

1. The Core Bottleneck Before Transformers

Before 2017, natural language processing relied on Recurrent Neural Networks (RNNs) and LSTMs. These models processed text sequentially, one word at a time:

ht=f(ht−1,xt)h_t = f(h_{t-1}, x_t)

This sequential processing suffered from two crippling limitations:

  1. Vanishing Context: By the time the RNN reaches word 500, earlier context is compressed and forgotten.
  2. Sequential Bottleneck: You cannot process word tt until word t−1t-1 finishes. GPUs could not train in parallel across the sequence length.

The Transformer, introduced by Vaswani et al. in "Attention Is All You Need" (2017), replaced recurrence completely with Self-Attention.


2. The Retrieval Intuition: Query, Key, and Value

Self-attention works like an intelligent database lookup:

  • Query (QQ): What a token is looking for.
  • Key (KK): What a token contains / offers.
  • Value (VV): The actual semantic information passed along if a Query matches a Key.

Consider the sentence:

lightbulb

"The bank of the river was steep."

When the model processes the ambiguous word "bank":

  • Its Query asks: "Am I a financial institution or a geographic landform?"
  • The Key of "river" responds: "I am a body of flowing water."
  • The dot product between QbankQ_{\text{bank}} and KriverK_{\text{river}} produces a high similarity score.
  • As a result, the Value VriverV_{\text{river}} is blended into the representation of "bank", disambiguating its meaning to geographical riverbank.

3. The Mathematical Mechanics

For an input sequence embedded into matrix X∈RT×dmodel\mathbf{X} \in \mathbb{R}^{T \times d_{\text{model}}}, we project using learned weight matrices WQ,WK,WV\mathbf{W}_Q, \mathbf{W}_K, \mathbf{W}_V:

Q=XWQ,K=XWK,V=XWVQ = \mathbf{X}\mathbf{W}_Q, \quad K = \mathbf{X}\mathbf{W}_K, \quad V = \mathbf{X}\mathbf{W}_V

Step 1: Compatibility Scores

Compute the pairwise dot products between all Queries and Keys: S=QK⊤S = Q K^\top

Step 2: Scaling by dk\sqrt{d_k}

For large vector dimensions dkd_k, dot products grow large in magnitude, pushing softmax into regions with vanishingly small gradients. Dividing by dk\sqrt{d_k} stabilizes the variance.

Step 3: Softmax Normalization

A=softmax(QK⊤dk)\mathbf{A} = \text{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}\right) The attention matrix A∈RT×T\mathbf{A} \in \mathbb{R}^{T \times T} contains probability weights summing to 1.01.0 across each row.

Step 4: Weighted Aggregation

Output=AV\text{Output} = \mathbf{A} V Each output token representation is a context-aware convex combination of all Value vectors in the sequence.