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:
This sequential processing suffered from two crippling limitations:
- Vanishing Context: By the time the RNN reaches word 500, earlier context is compressed and forgotten.
- Sequential Bottleneck: You cannot process word until word 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 (): What a token is looking for.
- Key (): What a token contains / offers.
- Value (): 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 and produces a high similarity score.
- As a result, the Value is blended into the representation of "bank", disambiguating its meaning to geographical riverbank.
3. The Mathematical Mechanics
For an input sequence embedded into matrix , we project using learned weight matrices :
Step 1: Compatibility Scores
Compute the pairwise dot products between all Queries and Keys:
Step 2: Scaling by
For large vector dimensions , dot products grow large in magnitude, pushing softmax into regions with vanishingly small gradients. Dividing by stabilizes the variance.
Step 3: Softmax Normalization
The attention matrix contains probability weights summing to across each row.
Step 4: Weighted Aggregation
Each output token representation is a context-aware convex combination of all Value vectors in the sequence.