A Complete Guide to LLM Attention Mechanism Variants
This article provides a structured overview of the main attention variants used in large language models. It traces attention back to its origins in encoder-decoder RNNs, explains how standard Multi-Head Attention (MHA) works and why its KV cache becomes a bottleneck, and then examines Multi-Query Attention (MQA), Grouped-Query Attention (GQA), sliding window attention, and sparse or block attention. Each variant is presented in terms of the specific constraint it addresses, followed by practical guidance on choosing among them based on memory, quality, context length, and deployment needs.
1. Why Attention Deserves Another Look
Self-attention lets every token in a sequence “see” the other visible tokens and assign weights based on relevance, producing context-aware representations. Multi-Head Attention (MHA) is the standard form of this idea in the Transformer: it runs multiple subspaces in parallel, each with its own projections, then concatenates and fuses the head outputs into a richer semantic representation.
As context length and model size grow, however, the memory and bandwidth cost of standard MHA quickly becomes a bottleneck. This has led to a series of redesigns aimed at reducing KV cache and computation without a significant loss in quality. This article takes a neutral view and abstracts the common problems and solution paths shared by these variants.
2. A Brief History: Why Attention Was Invented
Attention predates the Transformer. Its direct background was the encoder-decoder recurrent neural network: the encoder compressed the source sentence token by token into a sequence of hidden states, and the decoder generated the target sentence from them. When key information sat far from the current position in the input, the fixed-size hidden state became an information bottleneck. Attention broke this limit by “looking back at the full sequence on demand,” and paved the way for later parallel architectures.
3. How Standard Multi-Head Attention (MHA) Works
- Each head has independent query, key, and value projections, learning different relational patterns.
- The head outputs are concatenated and passed through an output projection to form a unified representation.
- During inference, the keys and values of all heads must be cached (KV Cache), and memory grows linearly with the number of heads and the sequence length.
Illustrative pseudocode:
# Simplified illustration, not a specific framework APIdef mha(x, Wq, Wk, Wv, Wo, n_heads):Q, K, V = proj(x, Wq), proj(x, Wk), proj(x, Wv)heads = [scaled_dot(Q_i, K_i, V_i) for i in range(n_heads)]return proj(concat(heads), Wo)
4. Key Variants and the Problems They Solve
Multi-Query Attention (MQA)
All heads share a single set of keys and values, compressing the KV cache to 1/n_heads of the original and significantly reducing bandwidth pressure, at the possible cost of some expressive power.Grouped-Query Attention (GQA)
A compromise between MHA and MQA: the heads are divided into several groups, and keys and values are shared within each group. It balances quality and efficiency and has become a common choice in many open-weight models.Sliding Window Attention
Each token attends only to neighbors within a local window, reducing complexity from quadratic to linear and suiting long-text and streaming scenarios; the trade-off is that more layers are needed to expand the effective receptive field.Sparse and Block Attention
Fixed or learnable sparse connections reduce computation, with clear gains on very long sequences, but the implementation and tuning are more complex.
5. Selection Guidance and Engineering Practice
- For the lowest inference memory: prioritize MQA or GQA with a small number of groups.
- For a balance of quality and cost: GQA is usually a safe starting point.
- For very long context or streaming generation: combine sliding window and block strategies.
- On the deployment side, general cloud capabilities such as KV cache quantization, paged caching, operator fusion, and monitoring and alerting can improve throughput and stability.
6. Conclusion
Attention variants do not replace one another; they are engineering trade-offs for different constraints. Understanding where each variant saves and where it loses is what allows you to make explainable, reproducible choices among quality, latency, and cost.
