Multi-Head Attention: Evolution and Engineering Practices
This article traces the evolution from single-head self-attention to multi-head attention (MHA), explains why adding heads improves representation and training stability, and examines the real engineering bottlenecks of KV cache memory and bandwidth. It also compares optimization paths such as GQA, MQA, chunked and streaming attention, low-precision caches, and prefix reuse, and offers practical guidance on tuning head counts and avoiding common misconceptions.
1. From Single-Head Self-Attention to Multi-Head: Defining the Problem
The core idea of self-attention is to let every token in a sequence “read” other visible tokens based on relevance, then aggregate what it reads into a new context representation through weighted summation. A single-head implementation is functionally complete, but it has a structural limitation: one softmax distribution can express only one relevance pattern, so the model is forced to trade off among signals such as syntactic dependencies, coreference, and local adjacency.
Multi-head attention (MHA) addresses this by opening multiple subspaces in parallel. Each head has its own learned projections and forms its own set of Q/K/V; the head outputs are then concatenated and passed through a single output projection. The skeleton looks like this:
# Pseudocode: forward pass of multi-head attentionQ = X @ Wq # [B, S, D]K = X @ WkV = X @ WvQ, K, V = split_heads(Q, K, V, H) # [B, H, S, D/H]A = softmax(Q @ K.transpose(-1, -2) / sqrt(D // H))O = concat_heads(A @ V) # [B, S, D]Y = O @ Wo
2. Why More Heads Help
First, representation subspaces are decoupled. Different heads can attend to dependencies at different distances and granularities, which is effectively running several weaker attention hypotheses in parallel within the same layer.
Second, optimization becomes more stable. Splitting a single sharp distribution into several smoother ones reduces the chance that attention collapses onto a few positions early in training.
Third, heads complement positional encodings and residual connections. Because head outputs are concatenated and projected together, the information bottleneck inside the layer is not narrowed too aggressively.
It is worth stressing that more heads are not always better. When the per-head dimension D/H becomes too small, the discriminative power of the dot product drops, attention drifts toward a uniform distribution, and the benefit turns negative.
3. The Engineering Cost: Memory and Bandwidth Are the Real Bottlenecks
During inference, the dominant cost is not the matrix multiplications but reading and writing the KV cache. The cache size is roughly:
bytes ≈ 2 × layers × heads × seq_len × (D/heads) × batch × dtype_size
The factor of 2 accounts for K and V. With the total dimension fixed, changing the number of heads does not change the total cache size, but it does significantly change memory access fragmentation and kernel efficiency. Sequence length and layer count are linear scaling factors, so long-context scenarios hit the memory ceiling very easily.
4. Optimization Paths and Selection Guidance
- Grouped-query attention (GQA): multiple query heads share one group of K/V, shrinking the cache by the number of groups. It is a common middle ground between quality and cost.
- Multi-query attention (MQA): all query heads share a single group of K/V. This is the most cache-efficient option, but expressiveness drops noticeably.
- Chunked and streaming attention: compute block by block with online normalization, reducing memory usage from O(S²) to O(S).
- Low-precision cache: store KV at a lower bit width, with accuracy loss under long contexts to be evaluated.
- Prefix reuse: caches for fixed prefixes such as system prompts can be shared across requests, cutting redundant computation significantly.
For selection, a practical order works well: fix the total dimension and layer count, then sweep head counts on a small validation set; measure time-to-first-token and throughput; finally, calculate cache usage at the target context length and leave a safety margin. If the deployment runs on a container platform or elastic compute, pair monitoring and alerting with logging services to continuously track tail latency percentiles, so averages do not hide long-tail behavior.
5. Common Misconceptions
One is treating parameter count as the only metric while ignoring the memory access bottleneck. Another is expanding context length without evaluating cache growth. A third is assuming multi-head is always better than fewer heads without ablation evidence. The safer engineering practice is to treat the attention structure as a tunable hyperparameter and tune it jointly with batch size, precision, and parallelism strategy.
