Multi-Query Attention › Multi-Query Attention: Extreme Sharing
Language AI HandbookAll chapters Multi-Query Attention › Multi-Query Attention: Extreme SharingOne section · Sections in this chapter3 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Multi-Query Attention, introduced by Noam Shazeer in 2019, addresses the KV cache problem through aggressive parameter sharing. The core insight is simple: instead of having separate key and value projections (one per head), use just one shared key and one shared value projection across all heads.
Multi-Query Attention maintains multiple query heads but shares a single key head and a single value head across all of them. Each query head computes attention using the same keys and values, reducing the KV cache size by a factor of (the number of heads).
The MQA Formulation
To truly understand Multi-Query Attention, we need to first build intuition about what multi-head attention does and why each component exists. Think of attention as a retrieval system: queries ask questions, keys advertise what information is available, and values contain the actual content to retrieve. Multi-head attention runs multiple such retrieval systems in parallel, each learning to find different kinds of relationships.
Step 1: The Standard Multi-Head Attention Baseline
In standard multi-head attention, each head transforms the input through its own learned projection matrices. Given an input sequence , we compute the three head-specific projection outputs:
where:
- : the input sequence with tokens, each represented as a -dimensional vector
- : the query projection matrix for head
- : the key projection matrix for head
- : the value projection matrix for head
- : the dimension of queries and keys per head (typically )
- : the dimension of values per head (often equal to )
The subscript on each projection matrix is the key detail here. It tells us that every head learns its own transformation. Head 1 might learn to project tokens in a way that emphasizes syntactic features, while head 2 might emphasize semantic similarity. This specialization is what makes multi-head attention so powerful: the model can simultaneously attend to different types of relationships.
But this flexibility comes at a cost. During inference, we must cache the keys and values for each head separately. With 32 heads, we store 32 different matrices and 32 different matrices. This is where the memory bottleneck arises.
Step 2: The MQA Insight: Sharing Keys and Values
MQA asks a provocative question: do we really need different keys and values for each head? The hypothesis is that while different heads benefit from asking different questions (different queries), they might be able to share a common "index" (keys) and "content store" (values).
Mathematically, MQA removes the subscript from the key and value projections:
Notice the key change:
- : still has subscript , meaning each head retains its own query projection
- and : no subscript, meaning a single projection shared across all heads
The resulting matrices have dimensions:
- : unique query matrix for head
- : one shared key matrix for all heads
- : one shared value matrix for all heads
This is a simple but effective change. Different heads can still "ask different questions" because each projects the input into its own query space. But they all look up answers from the same key-value store. It's like having 32 researchers who each have their own research questions but share access to a single library catalog and book collection.
Step 3: Computing Attention with Shared KV
Each head now computes attention using its unique queries against the shared keys and values:
Let's trace through what happens in this formula:
-
Compute attention scores: produces an matrix where entry measures how much token at position should attend to position . Because is unique to head , each head produces different attention patterns.
-
Scale the scores: Dividing by prevents the dot products from growing too large as dimension increases. Without scaling, the softmax would produce very peaked distributions, making gradients vanish during training.
-
Normalize to weights: The softmax converts each row of scores into a probability distribution that sums to 1. These are the attention weights.
-
Aggregate values: Multiplying by computes a weighted sum of value vectors. All heads aggregate from the same , but with different weights.
The key insight is that expressiveness comes from two sources: the diversity of attention patterns (which heads preserve through unique queries) and the diversity of retrieved content (which MQA sacrifices through shared values). Empirically, the diversity of attention patterns matters more than the diversity of values for most tasks.
To illustrate this, let's visualize how different query heads can produce distinct attention patterns even when sharing the same keys and values.




Each head learns a different way to "ask questions" of the shared key-value store. Head 1 might focus on recent context for local coherence, Head 2 might anchor to the beginning of the sequence, Head 3 might aggregate broadly, and Head 4 might pick up periodic patterns. The shared keys and values don't limit this diversity; they simply mean all heads retrieve from the same "library."
Step 4: Combining Head Outputs
Finally, we concatenate all head outputs and project back to the model dimension:
where:
- : the output from attention head , containing each head's weighted aggregation
- : concatenation along the feature dimension
- : the output projection that mixes information across heads
- : the total number of attention heads
This final projection is unchanged from standard MHA. Each head contributes its perspective, and the output projection learns to combine them into a coherent representation. The key benefit of MQA is that we achieve this with much less memory overhead during inference.
Visualizing the Difference
The architectural difference between MHA and MQA becomes clear when we visualize the projection structure.

