Virtual Head Attention

Xibo Ding, Guoxia Wang, Jinle Zeng, Jiabin Yang, Sirui Tao, Chao Yang, Dianhai YuNeurIPS 2026

TL;DR

Designing attention mechanisms for long-context Large Language Models (LLMs) usually feels like a game of whack-a-mole: shrink the memory footprint, and either your compute budget explodes or the model gets noticeably dumber. Virtual Head Attention (VHA) breaks this stalemate using two tiny, linear transformations during training that recover lost expressiveness while slashing memory and compute costs. Best of all, because the math is strictly linear, these extra operations fold neatly into standard model weights at deployment—giving you a leaner, faster, and smarter model with zero custom inference code required.

What Problem Does This Paper Address?

When an LLM generates text, it stores past context in GPU memory as a Key-Value (KV) cache. As context windows stretch to 128K tokens and beyond, this cache becomes a massive memory bottleneck.

To manage this, engineers face an "impossible triangle" between KV cache size, training/prefill compute (FLOPs), and model quality:

  • Grouped-Query Attention (GQA): The industry standard (used in LLaMA 3 and Qwen2) shares single Key-Value pairs across groups of Query heads. Using 8 KV heads (GQA-8) keeps quality high, but the cache is still bulky. Dropping to 2 KV heads (GQA-2) shrinks the cache by $4\times$, but forces too many queries to share the same context, tanking model accuracy.
  • Latent/Factorization Alternatives: Methods like Multi-head Latent Attention (MLA) compress the cache into a latent space, but require complex custom inference kernels and often lose accuracy at smaller token budgets. Conversely, Multi-matrix Factorization Attention (MFA) maintains quality under a tiny cache, but balloons training FLOPs by ~50%.

The authors ask: Can we get the tiny memory footprint of GQA-2, lower our training compute, and actually beat GQA-8 in accuracy—all while keeping standard, off-the-shelf deployment?

Methods

VHA starts with an aggressively pruned foundation: it sets the KV heads to just 2 ($H_{kv}=2$) and cuts the number of physical query heads in half. On its own, this "doubly-compressed" setup degrades accuracy. To rescue it, VHA adds two lightweight linear operations during training:

Figure 1: Overview of Virtual Head Attention. (a) VHA improves the trade-off among KV cache size, training/prefill FLOPs, and model quality by combining Q Premix and Linear Postmix during training, while absorbing both linear modules into standard GQA projections at inference. (b) Schematic of the KV cache–FLOPs–quality impossible triangle faced by existing attention designs. (c) Method comparison at 1.7B and 4B scales, plotting KV cache size against training FLOPs, with bubble area proportional to 8-task average accuracy.
  1. Q Premix (Virtual Query Head Expansion): Before applying rotary position embeddings (RoPE), VHA multiplies the halved physical queries by a learnable, per-KV-group matrix initialized near the identity matrix. This expands the halved physical heads back into the full count of virtual query heads, allowing each KV group to probe different geometric directions and restore attention diversity.
  2. Linear Postmix (Cross-Group Mixing): Because queries are siloed into two KV groups, the attention outputs lack cross-group communication. Postmix applies a low-rank identity-residual transformation ($I + BA^\top$) across the head dimension right after attention, letting heads share features across groups.

The Reparameterization Trick: Crucially, VHA uses no non-linear activation functions in Premix or Postmix. Once training finishes, Q Premix is multiplied directly into the standard Query weight matrix ($W_Q$), and Linear Postmix is absorbed into the Output weight matrix ($W_O$). At inference time, VHA literally becomes a standard GQA-2 model—compatible out-of-the-box with frameworks like vLLM and SGLang.

Datasets

  • Training: Small-scale models (0.6B to 8B-A1B MoE) were pretrained on 50B tokens of FineWeb-Edu. An industrial 30B-A3B MoE model was pretrained on 500B tokens of an internal multi-domain corpus (web text, books, code, multilingual data).
  • Evaluation: Small-scale models were evaluated on 8 zero-shot benchmarks: ARC-C, BoolQ, HellaSwag, OBQA, PIQA, WinoGrande, SciQ, and SIQA. The 30B model used a 12-benchmark suite spanning General Knowledge, Math & STEM, Coding, and Multilingual tasks. Long-context capabilities (up to 128K tokens) were tested on RULER, LongBench-v2, BABILong, InfiniteBench, and HELMET. No new benchmarks were introduced.

Key Insights and Ideas

  • Mathematically "redundant" layers change how models learn: Algebraically, multiplying two linear layers during training expresses the exact same set of functions as a single standard matrix. Yet training them separately (like RepVGG or LoRA) couples gradients across heads and imposes low-rank constraints, creating a favorable optimization landscape and implicit regularization that standard GQA cannot find on its own.
  • Non-linearity actively hurts: Adding SiLU activations to Premix or Postmix actually degraded benchmark accuracy by 0.67%–0.83%, while also ruining the ability to fold weights at inference. Keeping it linear is a rare win-win.
  • True functional complementarity: Inspecting trained weights revealed that Premix and Postmix aren't just adding extra parameters—they do distinct jobs. Premix learns specialized, group-specific query directions, while Postmix builds sparse cross-group bridges to redistribute information across heads. Neither module closes the compression gap alone.

Key Results

VHA establishes a new Pareto frontier across all five tested model scales:

Table 1: Pretraining accuracy across four model scales. We report zero-shot accuracy (%) on eight standard multiple-choice benchmarks. CACHE is KV cache per layer per token in BF16 (KB). Bold marks the best per scale per column; ties are bolded jointly. VHA achieves the highest average accuracy at every scale while using the smallest KV cache. Per-method full configurations and benchmark citations are in Appendix D; per-method training-FLOPs comparison is in Table 5.
  • Smaller Cache & Fewer FLOPs, Higher Quality: Across 0.6B, 1.7B, 4B dense, and 8B-A1B MoE models, VHA beats the GQA baseline at every scale (+0.05% to +0.81% average accuracy), while cutting the KV cache by $4\times$ (dense) and $2\times$ (MoE) down to 1.00 KB/layer/token. Simultaneously, halving the physical query projection cuts training/prefill FLOPs by 6.5% to 8.1% (e.g., -7.9% at 1.7B, compared to MFA's +49.8% FLOPs penalty).
  • Rescuing Doubly-Compressed Shapes: At 1.7B, naively shrinking GQA-8 to VHA's (8, 2, 2) head shape drops accuracy by -1.71%. Adding Premix and Postmix recovers the entire deficit and surpasses GQA-8 by +0.41%.
  • Industrial & Long-Context Scale: On the 30B-A3B MoE model (500B tokens), VHA achieves 50.36% average accuracy, beating MLA (49.66%) and GQA-4 (47.76%) with the smallest cache. On a 10B-A1B MoE model tested from 8K to 128K context, VHA scores 84.4 on RULER versus 73.0 for GQA (+11.4 points), while using half the KV heads in full-attention layers.

Limitations and Future Work

Not discussed in the paper. (Note: The provided pages contain the main text and references without a dedicated limitations or future work section).

Summary updated

Abstract

The design of attention mechanisms in long-context large language models faces an ``impossible triangle'' among KV cache size, training and prefill FLOPs, and model quality. Grouped-Query Attention (GQA) achieves strong quality but incurs high KV cache overhead; alternative approaches such as Multi-head Latent Attention (MLA) and Multi-matrix Factorization Attention (MFA) can compress the cache, but MLA suffers from quality loss while MFA substantially increases training and prefill compute. We propose Virtual Head Attention (VHA), which simultaneously improves all three vertices through two lightweight linear operations: Q Premix applies a near-identity transformation to queries within each KV group to recover virtual head diversity from halved physical query heads, and Linear Postmix fuses inter-head features via a low-rank $I + AB^\top$ residual structure. Since VHA introduces no nonlinear operations, Premix can be folded into the query projection and Postmix into the output projection at inference time, reducing the model to a standard GQA-2 architecture that directly reuses all existing GQA inference optimizations. With only two KV heads, the group size in large models readily reaches 64, fully exploiting Tensor Core parallelism during decoding. Across four scales (0.6B, 1.7B, 4B dense, and 8B-A1B MoE) with 4K-context 50B-token pretraining, VHA consistently surpasses the standard GQA baseline at every scale ($+$0.05\% to $+$0.81\%), while reducing KV cache by $4\times$ vs.\ GQA-8 at dense scales and $2\times$ vs.\ GQA-4 at the MoE scale, and lowering training and prefill FLOPs by 7.9\% (1.7B, $S{=}4096$). On an industrial 30B-A3B MoE model trained on 500B tokens at 8K context, VHA outperforms both MLA and GQA-4 by $+$0.70\,/\,$+$2.60\,\% on a 12-benchmark base-model evaluation suite; in a separate matched 10B-A1B MoE comparison after long-context mid-training, VHA attains an average RULER score of 84.4 over 8K--128K versus 73.0 for GQA, while using two rather than four KV heads in the full-attention layers. The source code is available at \url{https://github.com/PaddlePaddle/VHA}.
BibTeX
@inproceedings{ding2026virtual,
  author = {Xibo Ding and Guoxia Wang and Jinle Zeng and Jiabin Yang and Sirui Tao and Chao Yang and Dianhai Yu},
  title = {{Virtual Head Attention}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=6chRc0Xfxr},
}
Figure