Introduction
Welcome back! In our previous lesson, we demystified the KV-cache, a critical inference optimization that transforms the quadratic complexity of autoregressive generation into a linear one. We saw how it dramatically speeds up token generation by avoiding redundant computations. However, we also concluded by identifying its main trade-off: a significant memory footprint, which grows linearly with the sequence length.
Today's lesson directly addresses that memory bottleneck. You will learn how modern LLMs like Llama 3 and Mistral manage this trade-off through architectural innovations in the attention mechanism itself. This is a perfect example of how architectural design and inference optimization are deeply intertwined—a key insight for an applied AI engineer.
This lesson covers the learning outcome: Describe Grouped-Query Attention (GQA) and Multi-Query Attention (MQA) as optimizations for the standard multi-head attention. We'll explore how these variants reduce the memory cost of the KV-cache while trying to preserve the powerful expressiveness of Multi-Head Attention.
The Problem: KV-Cache Memory Consumption in Multi-Head Attention
Let's start with a quick refresher on standard Multi-Head Attention (MHA). In the original Transformer architecture, each attention "head" has its own set of Query (Q), Key (K), and Value (V) projection matrices. This means the number of Q heads is equal to the number of K/V heads.
Recall the formula for the KV-cache size:
The key variable for today's lesson is , the number of attention heads. In MHA, this term represents the number of Q heads, K heads, and V heads, as they are all equal. Because the cache only stores Keys and Values, its size is directly proportional to the number of K and V heads.
The core idea behind MQA and GQA is simple: what if we could reduce the number of K and V heads without reducing the number of Q heads? This would shrink the KV-cache without crippling the model's ability to "ask" diverse questions via its multiple query heads.
Let's watch a short video that provides a high-level overview of MHA and introduces its more efficient variants, MQA and GQA.
Multi-Head Attention (MHA), Multi-Query Attention (MQA), Grouped Query Attention (GQA) Explained
This video from DataMListic provides a concise overview of the three attention mechanisms we'll be discussing. It will give you a solid framework for understanding their differences.
Watch the entire video (from 00:35 to 07:04). It quickly recaps MHA's memory bottleneck during inference, then explains the architectural changes in MQA and GQA, and concludes by comparing their performance trade-offs.
A Spectrum of Attention Mechanisms
As the video explained, MHA, GQA, and MQA exist on a spectrum of efficiency versus representational power. The key difference lies in how many Key and Value heads are shared across the Query heads.
Let's use a diagram to make this crystal clear.

Let's break down the two main optimizations based on this visual:
-
Multi-Query Attention (MQA): The Extreme Optimization
- Architecture: All Query heads share a single Key head and a single Value head.
- Impact on KV-Cache: This is the most aggressive memory-saving technique. If a model has 32 attention heads, MQA reduces the size of the K and V tensors in the cache by a factor of 32 compared to MHA. This drastically lowers memory bandwidth requirements during inference.
- Trade-off: The cost is a potential degradation in model quality. Since all queries must retrieve information from the same, single representation of keys and values, the model's capacity can be limited. It's like having 32 experts (queries) who are all forced to consult the same single, summarized reference book (the single K/V head).
-
Grouped-Query Attention (GQA): The Balanced Approach
- Architecture: This is the pragmatic middle ground. The Query heads are divided into a smaller number of groups. Within each group, the Query heads share a common Key and Value head.
- Example: A model like Llama 3 8B has 32 query heads but only 8 KV heads. This means the query heads are organized into 8 groups, with 4 query heads per group sharing one K/V head.
- Impact on KV-Cache: The cache size is reduced by a factor of
(Number of Q heads) / (Number of KV heads). In the Llama 3 8B example, this is a 4x reduction (32 / 8). - Trade-off: GQA has been empirically shown to offer a fantastic balance. It achieves nearly the same performance as MHA while capturing most of the speed and memory benefits of MQA. This is why it has become the de-facto standard in many modern LLMs.
From Architecture to PyTorch: The Engineer's View
Given your background, it's valuable to see how this architectural change translates into code. The implementation difference is subtle but powerful.
The following reading walks through the motivation and provides a full PyTorch implementation of GQA, comparing it directly to MHA.
Understanding Grouped-Query Attention: A Practical Guide
Let's dive into the implementation details. This article provides an excellent, practical guide to GQA with PyTorch code. It will connect the high-level architectural concepts to the tensor manipulations you're familiar with.
Please read the following sections: 'The Memory Problem in Large Language Models': This will quickly reinforce the motivation we've discussed. 'Grouped-Query Attention: The Best of Both Worlds': Focus on the 'PyTorch Implementation' subsection. Pay close attention to two key details: The k_proj and v_proj layers project to a smaller dimension (self.kv_dim). The use of K.repeat_interleave(...) to 'broadcast' the smaller number of K heads to match the number of Q heads. 'Side-by-Side Comparison' -> 'Reshaping': This section gives a clean code diff that perfectly illustrates the implementation difference between MHA and GQA.
The key implementation takeaway is the repeat_interleave operation. This is where the "sharing" happens. Instead of computing num_query_heads unique K/V tensors, we compute fewer (num_kv_heads) and then efficiently copy them to allow all num_query_heads to perform their attention calculation. This small change in the forward pass leads to massive savings in memory and bandwidth during inference.
Test your understanding!
A new model is announced with the following specs:
- Number of query heads: 64
- Number of KV heads: 8
- What attention mechanism is this model using?
- How many query heads share a single KV head?
- By what factor is the KV-cache size reduced compared to an MHA version of the same model?
- If this model were to use MQA instead, what would the
num_kv_headsbe?
Show answer
- Since the number of KV heads (8) is greater than 1 but less than the number of query heads (64), the model is using Grouped-Query Attention (GQA).
- The number of query heads per group is
num_query_heads / num_kv_heads= 64 / 8 = 8. So, 8 query heads share a single KV head. - The reduction factor is also
num_query_heads / num_kv_heads, which is 8x. The KV-cache will be 8 times smaller than it would be in an MHA equivalent. - In Multi-Query Attention (MQA), all query heads share a single KV head. Therefore,
num_kv_headswould be 1.
Conclusion
You have now learned about two critical optimizations to the standard attention mechanism that are essential for deploying modern LLMs efficiently. While they may seem like small architectural tweaks, their impact on inference performance—especially latency and memory usage—is enormous.
Key Takeaways:
- The Motivation: Standard Multi-Head Attention (MHA) creates a large KV-cache during inference, which becomes a memory and bandwidth bottleneck.
- Multi-Query Attention (MQA): An aggressive optimization where all query heads share a single K/V head. This provides maximum memory savings but can degrade model quality.
- Grouped-Query Attention (GQA): A balanced approach where groups of query heads share K/V heads. It offers a "best of both worlds" compromise, significantly reducing the KV-cache size with minimal impact on performance.
- The Mechanism: The core implementation detail involves projecting to fewer K/V dimensions and then using an operation like
repeat_interleaveto align the K/V tensors with the query tensors for the attention computation. - Practical Relevance: GQA is a key feature in leading models like Llama 3 and Mistral, enabling them to handle long contexts efficiently.
What's Next?
We have now examined several key optimizations related to the attention sub-layer of the Transformer block (RoPE, KV-caching, GQA/MQA). In the next lesson, we will shift our focus to the other major sub-layer: the Position-wise Feed-Forward Network (FFN). We will explore the different activation functions used in modern LLMs, such as SwiGLU and GeLU, and understand how they contribute to model performance.