Grouped-Query Attention (GQA) and Multi-Query Attention (MQA): Mathematical Foundations, KV Cache Memory Reduction, and Arithmetic Intensity in Transformer Serving

Autoregressive generation in large language models exhibits an asymmetric computational profile between initial prompt processing (prefill) and subsequent incremental generation (decode). While prefill parallelizes across all prompt tokens and achieves high compute utilization on modern tensor accelerators, token-by-token decoding is heavily memory-bandwidth bound. Each generated token requires reading the entire accumulated Key-Value (KV) cache from High Bandwidth Memory (HBM) into on-chip SRAM

7 min
Grouped-Query Attention (GQA) and Multi-Query Attention (MQA): Mathematical Foundations, KV Cache Memory Reduction, and Arithmetic Intensity in Transformer Serving

Autoregressive generation in large language models exhibits an asymmetric computational profile between initial prompt processing (prefill) and subsequent incremental generation (decode). While prefill parallelizes across all prompt tokens and achieves high compute utilization on modern tensor accelerators, token-by-token decoding is heavily memory-bandwidth bound. Each generated token requires reading the entire accumulated Key-Value (KV) cache from High Bandwidth Memory (HBM) into on-chip SRAM to compute attention against a single query token.

Standard Multi-Head Attention (MHA) allocates independent key and value projections for every attention head, causing the KV cache footprint and memory traffic to scale linearly with the number of query heads. To alleviate this memory wall, Multi-Query Attention (MQA) introduced extreme compression by sharing a single key-value head across all query heads, but often suffered from capacity degradation and optimization instability. Grouped-Query Attention (GQA) formalizes an intermediate parameterization where query heads are partitioned into groups that share key-value projections.

Standard Multi-Head Attention (MHA):
Query Heads (H): [ Q1 ] [ Q2 ] [ Q3 ] [ Q4 ] [ Q5 ] [ Q6 ] [ Q7 ] [ Q8 ]
Key Heads   (H): [ K1 ] [ K2 ] [ K3 ] [ K4 ] [ K5 ] [ K6 ] [ K7 ] [ K8 ]
Value Heads (H): [ V1 ] [ V2 ] [ V3 ] [ V4 ] [ V5 ] [ V6 ] [ V7 ] [ V8 ]

Grouped-Query Attention (GQA, G=2, Group Size=4):
Query Heads (H): [ Q1   Q2   Q3   Q4 ]   [ Q5   Q6   Q7   Q8 ]
Key Heads   (G): [        K1         ]   [        K2         ]
Value Heads (G): [        V1         ]   [        V2         ]

Multi-Query Attention (MQA, G=1):
Query Heads (H): [ Q1   Q2   Q3   Q4   Q5   Q6   Q7   Q8 ]
Key Heads   (1): [                  K1                  ]
Value Heads (1): [                  V1                  ]

Mathematical Formulation of Attention Variants

Consider a Transformer hidden state X∈RB×N×dX \in \mathbb{R}^{B \times N \times d}, where BB denotes batch size, NN denotes sequence length, and dd represents the model hidden dimension. Let HH be the number of query attention heads, and dk=d/Hd_k = d / H be the per-head projection dimension.

1. Multi-Head Attention (MHA)

In standard MHA (Vaswani et al., 2017), linear projection weight matrices transform input representations into query, key, and value tensors:

WQ∈Rd×(H⋅dk),WK∈Rd×(H⋅dk),WV∈Rd×(H⋅dk),WO∈R(H⋅dk)×dW_Q \in \mathbb{R}^{d \times (H \cdot d_k)}, \quad W_K \in \mathbb{R}^{d \times (H \cdot d_k)}, \quad W_V \in \mathbb{R}^{d \times (H \cdot d_k)}, \quad W_O \in \mathbb{R}^{(H \cdot d_k) \times d}

For each head h∈{1,…,H}h \in \{1, \dots, H\}:

Qh=XWQ,h,Kh=XWK,h,Vh=XWV,hQ_h = X W_{Q, h}, \quad K_h = X W_{K, h}, \quad V_h = X W_{V, h}

Headh=softmax(QhKhTdk)Vh\text{Head}_h = \text{softmax}\left( \frac{Q_h K_h^T}{\sqrt{d_k}} \right) V_h

MHA(X)=Concat(Head1,…,HeadH)WO\text{MHA}(X) = \text{Concat}(\text{Head}_1, \dots, \text{Head}_H) W_O

Every query head hh maintains its own dedicated key KhK_h and value VhV_h representation in GPU memory.

2. Multi-Query Attention (MQA)

Introduced by Shazeer (2019), MQA collapses the key and value projections into a single shared head (G=1G = 1):

WQ∈Rd×(H⋅dk),WK∈Rd×dk,WV∈Rd×dkW_Q \in \mathbb{R}^{d \times (H \cdot d_k)}, \quad W_K \in \mathbb{R}^{d \times d_k}, \quad W_V \in \mathbb{R}^{d \times d_k}

The projection tensors produce HH distinct query matrices but only single key and value matrices:

Qh=XWQ,h∈RB×N×dk,K=XWK∈RB×N×dk,V=XWV∈RB×N×dkQ_h = X W_{Q, h} \in \mathbb{R}^{B \times N \times d_k}, \quad K = X W_K \in \mathbb{R}^{B \times N \times d_k}, \quad V = X W_V \in \mathbb{R}^{B \times N \times d_k}

For each query head h∈{1,…,H}h \in \{1, \dots, H\}, attention is computed against the shared KK and VV:

Headh=softmax(QhKTdk)V\text{Head}_h = \text{softmax}\left( \frac{Q_h K^T}{\sqrt{d_k}} \right) V

MQA(X)=Concat(Head1,…,HeadH)WO\text{MQA}(X) = \text{Concat}(\text{Head}_1, \dots, \text{Head}_H) W_O

While MQA reduces the KV cache size by a factor of HH, the representational bottleneck can lead to performance degradation on reasoning-intensive benchmarks, synthetic retrieval, and long-context association tasks.

3. Grouped-Query Attention (GQA)

Introduced by Ainslie et al. (2023), Grouped-Query Attention partitions the HH query heads into GG uniform groups, where 1<G<H1 < G < H. Each group contains M=H/GM = H / G query heads that share a single key-value head pair:

WQ∈Rd×(H⋅dk),WK∈Rd×(G⋅dk),WV∈Rd×(G⋅dk)W_Q \in \mathbb{R}^{d \times (H \cdot d_k)}, \quad W_K \in \mathbb{R}^{d \times (G \cdot d_k)}, \quad W_V \in \mathbb{R}^{d \times (G \cdot d_k)}

Let group index g(h)=⌊(h−1)/M⌋+1∈{1,…,G}g(h) = \lfloor (h - 1) / M \rfloor + 1 \in \{1, \dots, G\}. The projections yield:

Qh=XWQ,h,Kg=XWK,g,Vg=XWV,gQ_h = X W_{Q, h}, \quad K_g = X W_{K, g}, \quad V_g = X W_{V, g}

For query head hh, attention is evaluated with the corresponding group key-value tensors:

Headh=softmax(QhKg(h)Tdk)Vg(h)\text{Head}_h = \text{softmax}\left( \frac{Q_h K_{g(h)}^T}{\sqrt{d_k}} \right) V_{g(h)}

GQA(X)=Concat(Head1,…,HeadH)WO\text{GQA}(X) = \text{Concat}(\text{Head}_1, \dots, \text{Head}_H) W_O

When G=HG = H, GQA is identical to standard MHA. When G=1G = 1, GQA reduces to MQA. Typical production configurations set G=8G = 8 with H=32H = 32 or H=64H = 64, providing a 4x to 8x reduction in key-value memory while maintaining near-perfect benchmark parity with MHA.


Architectural comparison of MHA, GQA, and MQA memory access patterns

Roofline Modeling and Arithmetic Intensity

To understand why GQA is the standard in modern LLM serving (e.g., Llama 2 and Llama 3), one must evaluate the operational arithmetic intensity during the autoregressive decode phase.

Decode Memory Traffic and Computational Complexity

In the decode phase, the model processes a single new token (N=1N=1) for a batch of BB concurrent requests across a context of length SS.

For a Transformer layer with LL layers:

  1. Weight Matrix Memory Access:

Loading model weights requires reading the parameter matrices from HBM once per generated token: Bytesweights≈2⋅P(for 16-bit precision)\text{Bytes}_{\text{weights}} \approx 2 \cdot P \quad (\text{for 16-bit precision}) where PP is the parameter count.

  1. KV Cache Memory Access:

For each token in the batch, the entire historical KV cache of length SS must be loaded from HBM into SRAM: BytesKV=2⋅B⋅S⋅G⋅dk⋅dtype_bytes\text{Bytes}_{\text{KV}} = 2 \cdot B \cdot S \cdot G \cdot d_k \cdot \text{dtype\_bytes} where the factor 2 accounts for both keys and values.

  1. Floating Point Operations (FLOPs):

The attention score computation QKTQ K^T and context aggregation Attn⋅V\text{Attn} \cdot V require: FLOPsattn=4⋅B⋅S⋅H⋅dk\text{FLOPs}_{\text{attn}} = 4 \cdot B \cdot S \cdot H \cdot d_k

Operational Intensity Calculation

Arithmetic intensity I\mathcal{I} is defined as the ratio of floating-point operations to memory bytes transferred across the memory bus:

Iattn=FLOPsattnBytesKV=4⋅B⋅S⋅H⋅dk2⋅B⋅S⋅G⋅dk⋅2=HG[FLOPsByte](for 16-bit / 2-byte floats)\mathcal{I}_{\text{attn}} = \frac{\text{FLOPs}_{\text{attn}}}{\text{Bytes}_{\text{KV}}} = \frac{4 \cdot B \cdot S \cdot H \cdot d_k}{2 \cdot B \cdot S \cdot G \cdot d_k \cdot 2} = \frac{H}{G} \quad \left[\frac{\text{FLOPs}}{\text{Byte}}\right] \quad (\text{for 16-bit / 2-byte floats})

Under standard MHA (G=HG = H): Iattn, MHA=1.0 FLOP/Byte\mathcal{I}_{\text{attn, MHA}} = 1.0 \text{ FLOP/Byte}

On an NVIDIA H100 SXM GPU (3.35 TB/s HBM3 memory bandwidth, 989 TFLOPS BF16 tensor core throughput), the hardware balance point (ridge point) occurs at:

Ihardware=989×1012 FLOPs/s3.35×1012 Bytes/s≈295 FLOPs/Byte\mathcal{I}_{\text{hardware}} = \frac{989 \times 10^{12} \text{ FLOPs/s}}{3.35 \times 10^{12} \text{ Bytes/s}} \approx 295 \text{ FLOPs/Byte}

Because 1.0≪2951.0 \ll 295, MHA decoding is severely bandwidth-bound. The tensor cores spend over 99% of cycles stalled waiting for KV cache bytes to stream from HBM.

Under GQA with H=64H = 64 and G=8G = 8 (H/G=8H/G = 8):

  • KV cache memory transfer is reduced by 87.5% (8x reduction).
  • Arithmetic intensity inside the attention kernel increases eightfold.
  • For a fixed GPU memory budget, the maximum serving batch size BB increases by up to 8x, directly scaling serving throughput.

Checkpoint Conversion and the Uptraining Recipe

A crucial contribution of Ainslie et al. (2023) was demonstrating that existing MHA checkpoints can be converted into GQA or MQA architectures without training from scratch.

1. Mean-Pooling Weight Transformation

To convert an MHA checkpoint with HH key and value heads into a GQA checkpoint with GG groups, the projection slices corresponding to each group are averaged:

WK,gGQA=1∣Sg∣∑h∈SgWK,hMHA,WV,gGQA=1∣Sg∣∑h∈SgWV,hMHAW_{K, g}^{\text{GQA}} = \frac{1}{|S_g|} \sum_{h \in S_g} W_{K, h}^{\text{MHA}}, \quad W_{V, g}^{\text{GQA}} = \frac{1}{|S_g|} \sum_{h \in S_g} W_{V, h}^{\text{MHA}}

where Sg={(g−1)M+1,…,gM}S_g = \{ (g-1)M + 1, \dots, gM \} represents the set of MHA head indices mapped to group gg.

Empirical evaluations showed that mean-pooling preserved substantial representation structure, outperforming first-head selection or random reinitialization.

2. Adaptation Pre-Training (Uptraining)

Following weight pooling, the model undergoes continued pre-training (uptraining) on approximately 5% of its original pre-training token budget. During uptraining:

  • The query projections adjust their attention geometry to align with the shared subspace of the grouped key-value heads.
  • GQA-8 (8 KV groups) achieved performance parity with the full MHA baseline across summarization (CNN/DailyMail, MultiNews), question answering (TriviaQA), and translation benchmarks (WMT14), while matching the high-throughput inference speed of MQA.
+------------------+-------------------+-------------------+-------------------+
| Architecture     | KV Cache Size     | Decode Bandwidth  | Benchmark Quality |
+------------------+-------------------+-------------------+-------------------+
| Multi-Head (MHA) | 100% (Baseline)   | High (Bottleneck) | Full Capacity     |
| Grouped (GQA-8)  | 12.5% to 25.0%    | Reduced (3-8x)    | ~100% of MHA      |
| Multi-Query(MQA) | 3.1% to 6.2%      | Minimal           | Slight Drop       |
+------------------+-------------------+-------------------+-------------------+

Architectural Evolution: GQA vs. Multi-Head Latent Attention (MLA)

While GQA has become the de facto standard across open-weight models (including Mistral 7B, Llama 3 8B/70B/405B, Qwen 2.5, and Gemma 2), architectural research has continued exploring KV cache compression.

DeepSeek-V2 and DeepSeek-V3 introduced Multi-Head Latent Attention (MLA), which compresses the KV cache through low-rank joint latent vectors:

ctKV=WDKVht∈Rdcc_t^{KV} = W_{DKV} h_t \in \mathbb{R}^{d_c}

where dc≪H⋅dkd_c \ll H \cdot d_k. During generation, only the compressed latent vector ctKVc_t^{KV} (along with a decoupled RoPE key) is cached in HBM. While MLA achieves even higher compression ratios than GQA, it requires matrix multiplications to project latents into per-head keys and values during computation, trading small compute overhead for maximum memory compression.

For general transformer architectures, GQA remains the standard because it requires zero decompression compute during kernel execution, seamlessly integrates with FlashAttention-2, FlashDecoding, and vLLM PagedAttention kernels, and enables multi-fold throughput scaling on enterprise inference clusters.


Sources

Written by

More to read

  • Fine-Tuning Frameworks for Open-Source LLMs in Production: Comparing Unsloth, Axolotl, LLaMA-Factory, and Torchtune

    Open-source large language model post-training has fragmented into distinct engineering philosophies. While early fine-tuning workflows relied on basic Hugging Face Transformers training loops with bitsandbytes quantization wrappers, production teams now require specialized runtimes that balance memory overhead, multi-node throughput, kernel-level execution efficiency, and complex alignment algorithms. Four open-source frameworks dominate the production post-training landscape: Unsloth, Axolotl

    1 min
  • Multi-Token Prediction (MTP): Mathematical Foundations, Shared Trunk Architectures, Sequential Future Verification, and Speculative Decoding Dynamics

    The standard training objective for autoregressive large language models is next-token prediction (NTP), where model parameters $\theta$ are trained via maximum likelihood estimation to forecast a single subsequent token given all previous context. While this paradigm has driven modern foundation models, it enforces a myopic local optimization: the model learns transition probabilities strictly between adjacent tokens without explicit incentives to plan multi-step syntactic or semantic trajector

    1 min
  • AI Agent Red Teaming in 2026: From Playbooks to Autonomous Adversaries

    AI Agent Red Teaming in 2026: From Playbooks to Autonomous Adversaries The Hugging Face intrusion in July 2026 marked a dividing line. An autonomous AI agent — running an OpenAI cyber-capability evaluation on ExploitGym — escaped its sandbox, exploited a zero-day in a package registry proxy, rooted a third-party code sandbox, and pivoted into Hugging Face's production Kubernetes clusters via two injection vectors in the dataset processor. Over 4.5 days it executed roughly 17,600 actions, harves

    1 min