
Multi-Head, Grouped-Query, and Multi-Head Latent Attention
An examination of Multi-Head Attention, Grouped-Query Attention, and Multi-Head Latent Attention variants, focusing on KV cache size optimization and inference speed.
In large language model (LLM) inference, the bottleneck is rarely compute; it is memory bandwidth. As models scale, the time spent waiting for data to move from High Bandwidth Memory (HBM) to the compute units dominates latency. This constraint has driven the evolution of attention mechanisms from standard multi head attention (MHA) to Grouped-Query Attention (GQA) and, most recently, Multi-Head Latent Attention (MLA). These variants are not merely academic exercises; they are engineering solutions to the KV cache explosion.
Part 2 of the Transformers Under the Hood series.
The Baseline: Multi-Head Attention (MHA)
Standard MHA, introduced by Vaswani et al., divides the query, key, and value vectors into independent heads. Each head computes attention independently. During training, all parameters are updated. During inference, the model generates tokens autoregressively. For each new token, the model must compute the attention weights against all previous tokens.
The critical artifact here is the KV cache. To avoid recomputing keys and values for every new token, the model stores the key and value vectors for all previous positions in memory. If a model has dimensions, heads, and a context window of tokens, the KV cache size is approximately:
where and is the number of transformer layers.
For a 70B parameter model with 64 heads and a 32K context window, using 16-bit precision, the KV cache alone can consume over 100GB of VRAM. This creates a severe limitation: the available context window is constrained by the GPU’s memory, not the model’s architecture. As grows, the memory footprint grows linearly, eventually forcing a reduction in batch size or context length.
The First Optimization: Grouped-Query Attention (GQA)
Grouped-Query Attention, proposed by Ainslie et al., "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints" (2023), addresses the KV cache size by sharing key and value heads across multiple query heads. Instead of separate key/value projections, GQA uses groups (). Each group shares a single key and value vector for all query heads within that group.
Mechanistically, GQA computes:
- (standard projection)
- where the output is reshaped to heads.
- Attention is computed between each query head and the shared key/value pairs for its group.
The KV cache size is reduced by a factor of . If and , the memory usage drops by 8x. This allows for significantly larger batch sizes or longer context windows on the same hardware.
However, GQA introduces a precision trade-off. By sharing keys and values, the model loses the ability to represent fine-grained attention patterns that distinct heads might capture. In practice, GQA with larger group counts (i.e., closer to ) can approach MHA performance, but extreme grouping (small , such as ) can lead to degradation in perplexity and reasoning tasks. The optimal group size is often determined empirically, balancing memory savings against accuracy.
The Frontier: Multi-Head Latent Attention (MLA)
Multi-Head Latent Attention, introduced by DeepSeek-AI in "DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model" (2024), takes a different approach. Instead of reducing the number of heads, MLA compresses the dimensionality of the KV cache itself. The core insight is that the KV cache contains redundant information that can be compressed using low-rank factorization.
MLA works by:
- Compression: The high-dimensional KV states are compressed into a low-dimensional "latent" vector using a projection matrix .
- Decoding: During attention computation, the latent vector is projected back to the full KV dimension using decoding matrices and directly within the attention kernel matmuls, avoiding intermediate memory materialization.
Mathematically, instead of storing , MLA stores , where . The attention mechanism then effectively computes:
This results in a massive reduction in KV cache size, often by an order of magnitude compared to MHA. Unlike GQA, which shares heads, MLA compresses the data itself, preserving the number of heads and their individual expressiveness. This allows MLA to maintain higher accuracy than GQA at equivalent memory footprints, particularly for long-context tasks.
The trade-off is increased computational complexity during the decoding step. The multiplication by and adds FLOPs to each attention step. However, since memory bandwidth is the primary bottleneck, this computational overhead is often acceptable if it significantly reduces memory transfers.
Comparative Analysis
| Feature | MHA | GQA | MLA |
|---|---|---|---|
| KV Cache Size | High () | Medium () | Very Low () |
| Compute Overhead | Low | Low | Moderate (Decoding matrices) |
| Accuracy | Highest | Good (depends on ) | High (preserves head diversity) |
| Implementation | Standard | Simple modification | Requires rank decomposition |
MHA remains the gold standard for accuracy but is impractical for long contexts due to memory constraints. GQA offers a practical balance, widely adopted in open-source models like Llama 3 and Mistral, providing significant memory savings with minimal accuracy loss, a key goal of llm inference optimization. MLA represents the next step, offering aggressive compression while maintaining head diversity, making it ideal for ultra-long context applications.
Conclusion
The evolution from MHA to GQA to MLA reflects the industry’s shift from pure model capacity to system efficiency. For ML engineers, understanding these mechanisms is crucial for deploying LLMs at scale. GQA is likely to remain the default for general-purpose models due to its simplicity and effectiveness. MLA, however, is poised to become the standard for specialized long-context applications, where memory bandwidth is the ultimate limiting factor. As hardware evolves, the interplay between memory capacity and compute speed will continue to drive innovations in attention architectures.
Related posts
PagedAttention vs RadixAttention: Solving KV Cache Fragmentation and Redundancy
Compare PagedAttention and RadixAttention for LLM inference. Learn how these techniques optimize KV cache memory fragmentation and improve sharing efficiency.
Speculative Decoding: 2x Throughput for Free?
An examination of speculative decoding techniques that offer up to 2x throughput improvements for large language models by leveraging draft models and acceptance rates.
Quantizing the KV Cache Without Wrecking Quality
Explore techniques for quantizing the KV cache to reduce memory usage and improve throughput for long context windows in LLMs.