arXiv:2610.17959cs.CLOct 2026

Manuscript Offprint

arXiv:2610.17959

Virtual Head Attention

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

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⊤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× vs. GQA-8 at dense scales and 2× 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 https://github.com/PaddlePaddle/VHA.

1 Introduction

Large language models are rapidly scaling toward longer contexts, with context windows expanding from 2K tokens to 128K and beyond [Grattafiori et al., 2024, Yang et al., 2024]. As sequence lengths grow, the attention mechanism [Vaswani et al., 2017] becomes a central efficiency bottleneck in both inference and training. During inference, the key-value (KV) cache grows linearly with sequence length, consuming substantial GPU memory and constraining both serving concurrency and generation latency. During training and prefill, the FLOPs of attention computation scale quadratically with sequence length. Since the design of the attention mechanism jointly determines KV cache size, computational cost, and model expressiveness, it is a critical factor governing the overall efficiency and quality of large language models.

∗^{*}Equal contribution. Work done at Baidu Inc.

†^{\dagger}Corresponding author.

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

The design of existing attention mechanisms faces three mutually constraining objectives, forming an “impossible triangle”: KV cache size, training and prefill FLOPs, and model quality. Consider the widely adopted Grouped-Query Attention (GQA) [Ainslie et al., 2023]: GQA with 8 KV heads (GQA-8) achieves strong quality, but incurs a KV cache of 4 KB per layer per token (in BF16). Directly reducing the number of KV heads to 2 (GQA-2) compresses the cache to 1 KB, but dramatically enlarges the group size so that far more query heads must share the same key-value pairs, causing attention diversity to drop sharply and resulting in a 1.00% quality degradation at the 1.7B scale. Multi-head Latent Attention (MLA) [Liu et al., 2024] applies low-rank compression to reduce KV cache to 1.12 KB, but yields the lowest quality across multiple model scales. Multi-matrix Factorization Attention (MFA) [Hu et al., 2025] reduces the cache to 1 KB by enlarging the head dimension, achieving quality close to GQA-8, but at the cost of a 49.8% increase in training and prefill FLOPs (1.7B, S=4096). No existing approach achieves a satisfactory balance across all three dimensions.

We propose Virtual Head Attention (VHA), which simultaneously improves all three vertices of the impossible triangle through two lightweight linear operations. The core idea of VHA is to halve the number of physical query heads while setting the number of KV heads to only 2. Q Premix then applies a near-identity linear transformation to queries within each KV group, expanding the reduced physical query heads into virtual query heads to recover attention diversity. Linear Postmix applies a low-rank identity-residual transformation (I+AB⊤,r=Hq′/2,e.g.,r=4 at 1.7B)(I + AB^\top, r = H_q' / 2, \text{e.g.}, r = 4 \text{ at } 1.7\text{B}) on the attention output, fusing cross-group inter-head features. Since VHA introduces no nonlinear operations, Premix can be directly folded into the query projection matrix and Postmix into the output projection matrix at inference time, reducing the model to a standard GQA-2 architecture that reuses all existing GQA inference optimizations in mainstream serving frameworks such as vLLM [Kwon et al., 2023] and SGLang [Zheng et al., 2024]. Moreover, with only 2 KV heads, the group size in large models readily reaches 64, fully exploiting Tensor Core parallelism during the decoding phase.

Across four model 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× vs. GQA-8 at dense scales and 2× 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, demonstrating scalability to industrial model scale. A separate matched 10B-A1B MoE comparison further extends this advantage to 128K context under more aggressive KV compression. Our main contributions are as follows:

  1. We propose VHA, employing Q Premix and Linear Postmix, two lightweight linear operations that simultaneously reduce KV cache and training/prefill FLOPs while improving model quality.

  2. VHA uses only linear operations and can be fully absorbed into a standard GQA-2 model at inference time, reusing existing serving frameworks such as vLLM and SGLang with zero additional overhead.

  3. Direct inspection of the trained Premix and Postmix matrices reveals two complementary structures (group-conditioned query specialization and structured low-rank cross-head communication) that jointly close the gap of the doubly-compressed configuration; component ablations confirm that neither module is sufficient on its own.

  4. Comprehensive evaluations across five scales (0.6B to 30B-A3B MoE) against MHA, GQA, MFA, and MLA establish VHA as the best attention mechanism that is Pareto-optimal on KV cache, training/prefill FLOPs, and downstream quality simultaneously; matched long-context evaluations through 128K further validate this advantage under aggressive KV compression.