This post is written for engineers who already know the Transformer basics but want to understand why it was designed this way.
There is a common misconception when people first encounter the Transformer architecture. It usually goes something like this:
“Self-Attention is just calculating the similarity between tokens.”
Intuitively, this makes sense. If you look at the Self-Attention mechanism, you see dot products between vectors. If the dot product is high (meaning the vectors are similar), that token gets more weight.
It sounds a lot like a recommendation system: “People who watched this movie also liked…” So, many engineers nod their heads, think, “Ah, it just groups similar words together,” and move on.
But you shouldn’t stop there. Today, we are going to go one step deeper.
Our inquiry starts with a simple question: If Attention is just about calculating similarity between tokens, why do we need to complicate things by creating three separate vectors — Query, Key, and Value?
If you dig into this, you realize that Attention isn’t just a similarity metric; it is a routing system that guides how information flows inside the Transformer. Furthermore, you will begin to understand how Attention and the MLP (Multi-Layer Perceptron) collaborate to produce the final output.
I might be getting ahead of myself. Let’s break this down step by step.
1. What Self-Attention Actually Does: Ask, Search, Retrieve
Let’s describe the job of Self-Attention using plain English instead of math.
“Attention is an operation where a Query asks a question, selects relevant items from a Key space (indexing), and then mixes the corresponding Values (content) to move that information to a new location.”
That’s a bit dense. Let’s break it down into 4 distinct phases:
- Question Generation (Query): The current token asks, “I need to know who the subject of this sentence is.”
- Index Search (Key): Other tokens hold up their tags (Keys) saying, “I’m a noun,” or “I’m a pronoun.”
- Matching & Selection (Score): We compare the question against the tags. If they are a good match, we give them a high score.
- Information Retrieval (Value & Aggregation): We grab the actual content (Value) from the high-scoring tokens and merge that information into the current token.
When viewed this way, Attention looks a lot like a database lookup.
The crucial part here isn’t just finding similar things; it’s that information moves. In step 4, we take information (Value) from other tokens and transport it to the current token.
It’s not just verifying, “How much do you look like me?” It is a information gathering process: “I need that piece of data over there, so I’m going to copy it and paste it here.”
2. The Roles of Q, K, and V: Everyone Has a Job
The Transformer converts a single token into three distinct vectors: Q, K, and V.
One of the most brilliant aspects of the “Attention is All You Need” paper was the decision to split the input vector into these three separate spaces.
Why make it so complex? If the goal is just relevance, couldn’t we just compare the embeddings directly?
Let’s use a library analogy to explain why this separation is necessary.
- Q (Query): Your Search Term
You walk up to the librarian and ask, “Find me books on the American Civil War.”
This is the Query. It represents the nature of the information the current token needs to find. - K (Key): The Label on the Book Spine
Books in the library have labels or metadata that describe them: “History,” “USA,” “1860s,” “Lincoln.”
This is the Key. It acts as an identifier designed to be caught by a search tool (Query). - V (Value): The Actual Content of the Book
Once you match the label (Key), the knowledge you actually read and absorb comes from the pages inside.
This is the Value. It is the substantial content that will be passed on to other tokens.
From this perspective, the design philosophy of Self-Attention becomes clear:
- It is not merely measuring similarity.
- It is closer to a search engine.
- To make that search accurate and efficient, the data used for searching (Key) is separated from the data being retrieved (Value).
3. “If K is a summary of V, can’t we just search using V?”
You might ask: “We generate K from the input token, and V from the same input token. If K is just a representative summary of V, can’t we skip making K and just use V for the search?”
That is a sharp question.
The answer is no. Doing so would destroy the model’s performance because of Conflicting Optimization Goals.
K (for searching) and V (for content) require fundamentally opposite vector characteristics:
- Goal of K: Keys need to be distinct. To prevent confusion, their features must be sharp. For example, the Key for “Banana” needs to scream “Fruit” and “Yellow” so it can be easily found. It needs to emphasize distinguishing features and discard noise.
- Goal of V: Values need to be rich. Since this information will be used as calculation material in the next layer, it needs to preserve context, part of speech, semantic nuance, and more. It’s better to be dense and rich than simple and sharp.
If we forced (K=V), a single vector would have to be both a “sharp search index” and a “rich information container” simultaneously. It’s like trying to find food that is incredibly delicious but also perfectly healthy — possible, but very difficult.
Put another way, it is like writing the entire text of the book on the cover.
The cover should act like a cover, and the content should act like content. They have different roles. By separating them, the Transformer gains a much more sophisticated ability to understand language.
4. “Then can we combine Q and K since they are both for search?”
“Okay, I get why V is separate. But Q and K are both used for matching. Can’t we just use one vector for both?”
Another great question. In fact, some early recommendation systems used exactly this approach (symmetric dot products).
However, language has a critical feature called Asymmetry. Just like human relationships, relationships between words are not always mutual.
Take this sentence for example:
“Tom went to the store because he was hungry.”
Let’s look at Tom and he.
- When Query is
heand Key isTom: To understand the sentence,heneeds to know that it refers toTom. - When Query is
Tomand Key ishe: To understand the sentence,Tomgenerally has less need to know thathefollows it later.
In other words, he is very interested in Tom, but Tom might not be as interested in he.
What happens if Q and K are not separated?
Score(he, Tom) would be mathematically identical to Score(Tom, he). The model would be forced to assume that the interest between two words is always equal.
By separating Q and K, we get four different vectors: Q(he), K(he), Q(Tom), K(Tom). Consequently, Score(Q(he), K(Tom)) can be completely different from Score(Q(Tom), K(he)).
This allows the Transformer to model Directed Graphs, where A references B strongly, but B ignores A. This is crucial for parsing grammatical structures and logical flow.
5. Why Attention is Not Just Similarity
Let’s circle back to the original misunderstanding. Why isn’t Attention just similarity?
5.1. Softmax: A Winner-Takes-All Competition
The Self-Attention formula includes a Softmax operation.
Softmax is fundamentally about relative evaluation. No matter what the absolute input values are, it forces the sum to be 1.
This means Attention isn’t asking “How similar are you?” (absolute value). It is asking “Among all these candidates, which one is the most relevant to my current Query?”
It is a process of routing through competition, not just measuring distance.
5.2. Aggregating and Moving
The result of a standard similarity check is usually a single number (scalar): “We are 0.7 similar.”
However, the result of Attention is a Vector. Multiple selected Values are mixed (Weighted Sum) to create a brand new vector.
Essentially, information from various tokens is blended and physically moved to a new location in the sequence.
6. Attention (Routing) vs. MLP (Computation)
If you rip open a Transformer block, it has two main components:
- Multi-Head Attention
- Feed-Forward Network (MLP)
Because the paper is titled “Attention is All You Need,” people tend to obsess over the Attention mechanism and treat the MLP as just a “pass-through” layer.
However, recent research shows they play very different, distinct roles.
6.1. Attention: The Logistics Hub
The Attention layer is, at its core, a Linear Combination. It adds and subtracts Values. It usually does not apply non-linear transformations to the content of itself during the aggregation (though the scoring is non-linear).
The job of Attention is “Information Movement.”
- “Move the ‘past tense’ information from the start of the sentence to the verb.”
- “Copy the gender info from ‘Tom’ and paste it to ‘He’.”
Attention acts as the logistics manager or router of the model. It decides where information should go.
6.2. MLP: The Brain
On the other hand, the MLP performs non-linear transformations (using activation functions like GELU or Swish). Unlike Attention, the MLP works on each token individually, without looking at others.
The job of the MLP is “Processing and Inference.”
- Based on the info brought in by Attention (“Subject is 3rd person singular” + “Tense is past”), the MLP calculates: “Okay, I need to conjugate the verb this way.”
- If Attention brings “Bank” and “Finance” together, the MLP processes this to realize: “This is a financial institution, not the side of a river.”
Some researchers argue that the factual knowledge the model learns (e.g., “The capital of France is Paris”) is largely stored in the weights of the MLP layers.
Further Reading:
- Knowledge Neurons in Pretrained Transformers
- Locating and Editing Factual Associations in GPT
- Transformer Feed-Forward Layers Are Key-Value Memories
6.3. Transformer as a System
From a systems engineering perspective, here is how the Transformer operates:
- Attention: Looks around the room and gathers necessary context. (Routing)
- MLP: Takes that gathered info, thinks about it alone, and extracts knowledge or logic. (Computation)
- Residual Connection: Adds this result to the existing information and passes it to the next layer.
This cycle repeats dozens of times. What starts as simple word vectors gradually evolves into a high-dimensional representation capable of understanding context, logic, and complex reasoning.
7. Conclusion: Don’t Get Lost in the Math
We’ve explored why Q, K, and V are separated and how Attention and MLP divide the labor. To summarize:
- Attention is Routing, not just Similarity.
It is a sophisticated logistics system divided into Query (Question), Key (Address), and Value (Cargo). - Splitting Q, K, and V is Essential.
The vector optimized for searching is different from the vector optimized for carrying content. - Attention and MLP Collaborate.
Attention delivers the right information to the right place; MLP analyzes that information to derive meaning.
Some say Transformer is just a giant pile of matrix multiplications. While true mathematically, structurally it is a brilliant system design that decouples “Information Retrieval” from “Information Processing.”
Understanding this perspective makes it much easier to interpret attention maps or debug models.