**From Attention Analogies to Architecture: Why Transformers Work the Way They Do**
The concepts of keys, queries, values, and dot‑product attention are often explained with analogies—like questions and conversations. But these analogies only go so far. The deeper reason these components exist is rooted in design constraints: the need for flexible, parallelizable architectures that can attend to information across long sequences without collapsing past inputs into a fixed memory.
In this article, we strip away the analogies and rebuild the Transformer from first principles. We’ll see why its core ingredients—queries, keys, values, attention heads, and the MLP—are not arbitrary, but emerge naturally from requirements like symmetry breaking, parameter efficiency, and hardware-friendly computation.
—
### Why Fixed Memory Fails
Recurrent Neural Networks (RNNs) process sequences step by step, compressing all past information into a fixed hidden state. This compression causes problems: details get overwritten, and long-range dependencies degrade.
One solution is attention mechanisms, which let each step directly access all previous steps. While effective, RNN‑style attention still suffers from sequential computation, limiting training speed on GPUs.
The breakthrough in “Attention Is All You Need” was to remove recurrence entirely and rely solely on attention—enabling massive parallelism and better scalability.
—
### Transformers and Dynamic Weights
Without recurrence, the architecture needs a way to decide how inputs interact at every position. These interactions are governed by weight matrices that must adapt to variable sequence lengths. The solution is to make weights functions of the inputs themselves.
To avoid symmetry—where different positions learn identical behavior—weights must depend on both:
– The source input (what is being attended to), and
– The target unit (what is being computed).
This insight leads to the familiar **query** (target) and **key** (source) framework.
—
### Keys, Queries, and Values Emerge Naturally
Instead of hard‑coding attention weights, the Transformer uses a parameterized function—specifically, the dot product between queries and keys—to determine relevance. The value vectors represent the actual information to be aggregated.
To keep computation feasible:
– **Keys and queries** are projected into a lower-dimensional space, reducing cache size.
– **Multiple attention heads** (different sets of projections) allow the model to attend to information from different subspaces.
Softmax normalization ensures attention weights are sparse and interpretable, focusing on a few relevant tokens rather than spreading influence too thin.
—
### The Transformer MLP: The Other Key–Value Store
After attention, the Transformer feeds its outputs through a feed‑forward network (FFN). While often called the MLP, this block is more than a simple nonlinearity.
The final layer of the MLP (often denoted **W₂**) acts as a second key–value store:
– It allows each position to write rich, high‑dimensional value vectors into the residual stream.
– It decouples the number of model parameters from the width of the residual stream, enabling larger internal representations without increasing output dimensionality.
The earlier linear projection (**W₁**) expands the representation internally so the MLP can model more complex patterns before projecting back down.
—
### Recap: How the Transformer Was “Invented” Step by Step
1. **Eliminate fixed memory** → use attention over all past inputs.
2. **Enable parallelism** → remove recurrence, rely on residual connections and layer-wise computation.
3. **Break symmetry** → make attention weights functions of both query and key.
4. **Use efficient interaction** → dot product attention balances expressiveness and compute cost.
5. **Reduce cache size** → project keys and queries into smaller subspaces.
6. **Control sparsity** → apply softmax to attention scores.
7. **Down‑project values** → compress cached values to save memory, then up‑project later.
8. **Expand feature space** → use MLPs with separate output weights to write diverse patterns back into the residual stream.
—
### Why Transformers Aren’t Inevitable
Despite their success, Transformers have a major weakness: attention computation scales quadratically with sequence length. Sparse or linear approximations can help, but they often run slower on GPUs due to memory bottlenecks.
More importantly, Transformers are tailored to GPU architectures. As hardware evolves—or as we design systems that are not GPU‑bound—entirely different architectures may outperform them. The “inevitable” architecture is often just the one optimized for today’s hardware.
—
### Frequently Asked Questions (FAQ)
**Q: Why do we need both keys and queries? Can’t we just use one?**
A: Keys represent the information being retrieved; queries represent what we’re looking for. Separating them lets each target unit attend selectively to different sources, breaking symmetry and enabling diverse behaviors across heads.
**Q: What do the value vectors actually do?**
A: Values are the information that gets aggregated. Once attention scores determine which keys are relevant, the corresponding values are blended together to form the output for that position.
**Q: Why is softmax used in attention?**
A: Softmax turns raw dot‑product scores into a probability distribution, emphasizing the most relevant tokens while suppressing others. This yields sparse, interpretable attention patterns.
**Q: Can we remove the MLP and just use attention?**
A: Not without losing expressive power. The MLP introduces nonlinear interactions and allows the model to write arbitrary vectors into the residual stream, rather than being constrained to one‑to‑one mappings.
**Q: Will Transformers be replaced?**
A: Likely yes. While extremely effective, Transformers are hardware‑dependent and quadratic in cost. Future architectures may surpass them, especially if they break free from GPU‑centric design assumptions.
—
### Conclusion
The Transformer architecture is not a product of analogies alone, but of concrete design pressures: parallelism, symmetry breaking, parameter efficiency, and hardware constraints. By revisiting these foundations, we see that keys, queries, values, and attention heads emerge as natural solutions—and the MLP acts as an essential complementary memory system.
Understanding this structure not only clarifies how Transformers work today, but also highlights where the limits of current designs lie—and where the next breakthrough might come from.



