Multi-Query Attention (MQA) cuts incremental decoder latency by more than an order of magnitude by keeping h distinct query heads while collapsing all keys and values into a single shared head. In Noam Shazeer’s 2019 experiments on TPUv2 hardware, sharing one key and value projection across eight query heads slashed per-token greedy decoding cost from 46 microseconds to 3.8 microseconds (a 12.1x decoder speedup) while matching standard multi-head attention on WMT 2014 English-to-German beam-4 translation (28.5 vs 28.4 test BLEU).

The core insight is a roofline shift between training and autoregressive serving. During training on known sequences, Transformer attention batches every query position at once and stays comfortably compute-bound. During generation, producing one token at a time forces the accelerator to stream the entire history of cached keys and values from high-bandwidth memory on every step. MQA became the direct ancestor of modern inference-friendly attention designs, inspiring Grouped-Query Attention in Llama and Gemma and Multi-Head Latent Attention in DeepSeek.

  • 12.1x faster greedy decoding: on a 1,024-sequence batch at length 128 on TPUv2 (8 cores), decoder step time drops from 47 ms (46 us/token) to 3.9 ms (3.8 us/token), and beam-4 search drops from 203 to 32 us/token (6.3x faster).
  • 8x smaller KV-cache reads: removing the h dimension from K and V cuts the memory-access-to-compute ratio’s sequence-dependent n / d term down to n / (d * h).
  • Minimal quality drop vs naive shrinkage: at equal 211M parameters on WMT14 EN-DE, MQA scores 26.5 dev BLEU (vs 26.7 MHA), outperforming equal-cache alternatives that shrink h or d_k (25.8 to 26.2 BLEU).
  • Orthogonal to local windows: combining MQA with a 32-token local decoder window hits 3.3 us/token greedy and 16 us/token beam-4 without losing dev BLEU (26.5).
Multi-head vs. Multi-query attention: where the memory traffic goes
Standard MHA
h query heads each write and reload their own [m, k] keys and [m, v] values.
Cache shape [b, h, m, k] and [b, h, m, v]
Across n decode steps, memory traffic scales as Theta(b n^2 d + n d^2) for Theta(b n d^2) FLOPs, leaving memory-to-compute ratio near n/d + 1/b.
Multi-Query Attention
All h query projections P_q stay independent, while P_k and P_v omit the head axis.
Shared cache [b, m, k] and [b, m, v]
Every query head still computes a distinct attention pattern over the same cached keys and values, dropping ratio to Theta(1/d + n/(d h) + 1/b).

Source: Shazeer (2019), Sections 2.4 and 3.1.

Why incremental decoding hits a memory-bandwidth wall

Shazeer frames the problem cleanly with two tf.einsum complexity derivations. On modern TPUs and GPUs, arithmetic throughput is roughly two orders of magnitude higher than off-chip memory bandwidth, so kernels only run fast when the ratio of bytes moved to arithmetic operations stays far below 1.

During batched training (Section 2.3.1, assuming sequence length m = n <= d and head width k = v = d / h), all n query tokens in a batch of b sequences execute together. Total arithmetic is Theta(b n d^2), while total memory accesses across inputs, logits, weights, and projections sum to O(b n d + b h n^2 + d^2). Dividing memory by compute gives an intensity ratio of O(1/k + 1/(b n)). With k = 128 and large batches, that ratio sits around 0.01, so training finishes fast (13.2 us/token in Table 2).

During autoregressive decoding (Section 2.4.1), data dependencies break that parallelism. Token t depends on token t - 1, so each call processes only one new query vector (x of shape [b, d]) while reloading the entire cached history prev_K and prev_V of shape [b, h, m, k]. Across n generated tokens, arithmetic stays Theta(b n d^2), yet memory accesses explode to Theta(b n^2 d + n d^2): the b n^2 d term comes from reloading K and V at every step, and the n d^2 term comes from reloading the projection matrices P_q, P_k, P_v, P_o.

Dividing memory by compute now produces Theta(n/d + 1/b). When sequence length n approaches model width d, or when batch size b is small, that ratio approaches 1, and the accelerator spends almost its entire time waiting on memory. Even at a huge batch of b = 1,024 sequences (shrinking 1/b below 0.001), the n/d term from reloading K and V still dominates: in Table 2, the MHA decoder spent 46 us/token compared to just 1.7 us/token in the parallel encoder.

Why naive KV-cache fixes fall short

Once you spot the n/d term in Theta(n/d + 1/b), three naive fixes come to mind immediately. Shazeer’s derivations and ablations show why each fails to match MQA.

First, cranking up batch size b only shrinks the 1/b parameter-reloading term. It does nothing to the n/d KV-cache reload term, because every added sequence brings its own [h, m, k] and [h, m, v] state along for the ride. Worse, MHA caches fill device HBM h times faster, capping b before compute units saturate.

Second, shrinking the number of heads h or the head width d_k, d_v in standard MHA reduces K and V to the exact same byte footprint as MQA, yet destroys representation capacity. In Sections 4.1 and 4.2, Shazeer holds total model parameters fixed at 211M on WMT14 EN-DE (and 192M on the Billion-Word Language Model) by widening d_ff to compensate for smaller attention projections. Setting h = 1, d_k = 128 forces the entire layer to compute a single attention distribution (25.8 dev BLEU vs MHA’s 26.7); keeping h = 8 while shrinking d_k = d_v = 16 starves each dot product (25.8 dev BLEU); intermediate h = 2, d_k = 64 (26.2 BLEU) and h = 4, d_k = 32 (26.1 BLEU) still lag MQA (26.5 BLEU). On Billion-Word LM (Table 3), all four shrunk-MHA variants score 30.9 to 31.2 dev perplexity, noticeably worse than MQA’s 30.2 (versus MHA’s 29.9).

Third, windowed local attention alone helps (46 down to 23 us/token greedy in Table 2), yet discards long-range context whenever a token needs information past the 32-token window, and still loads h copies of that window on every step. Because MQA removes the h axis from K and V while leaving P_q ([h, d, k]) and P_o ([h, d, v]) untouched, it keeps h distinct ways to query the context and combines orthogonally with local windows (3.3 us/token).

Why obvious alternatives to Multi-Query Attention underperform
Larger batch b

Shrinks 1/b weight-read overhead but leaves the n/d KV-cache reload bottleneck untouched while exhausting HBM capacity h times sooner.

Fewer heads h=1..4

Matches MQA cache size at equal 211M parameters, but drops WMT14 dev BLEU by 0.5 to 0.9 points and hurts Billion-Word perplexity by ~1.0.

Narrower d_k=16

Preserves 8 query heads yet starves key/value width, falling to 25.8 dev BLEU (worse than MQA’s 26.5) and 30.9 LM perplexity.

Data from Shazeer (2019), Tables 1, 2, and 3.

Compare five KV-budget strategies at equal parameter count

Consider Shazeer’s 6-layer translation model (d_model = 1024, h = 8, d_k = d_v = 128, 211M parameters total) generating n = 128 tokens across b = 1,024 sequences in bfloat16/float32-equivalent tensor shapes. Under standard MHA, every layer caches 8 key heads and 8 value heads of width 128, forcing n/d = 128/1024 = 0.125 memory-to-compute ratio from KV reloads alone and stalling each decoder step at 47 ms. MQA keeps all 8 query heads (P_q shape [8, 1024, 128]), collapses P_k and P_v to [1024, 128], and reinvests the saved weights into widening d_ff from 4,096 to 5,440. The n/(d * h) cache term drops 8x to 0.0156, and each decoder step finishes in 3.9 ms.

Switch through the five tabs below to compare all five configurations tested at matched 211M (WMT14) and 192M (Billion-Word) parameter budgets.

Five attention budgets at matched parameter counts

Select a configuration (or use Left and Right arrow keys) to compare KV-cache scaling, WMT14 EN-DE BLEU, Billion-Word perplexity, and TPUv2 decode latency.

Standard Multi-Head Attention (h=8, d_k=128, d_ff=4096)

8 KV heads

Highest raw quality across dev metrics, but memory-bandwidth bound during incremental decoding.

Why it stalls
Reloads [b, 8, m, 128] keys and values at every step; memory-to-compute ratio Theta(n/d + 1/b) pins TPUv2 decode at 46 us/token.
WMT14 EN-DE
Dev ln(PPL) 1.424, dev BLEU 26.7, test BLEU 27.7 (greedy) / 28.4 (beam-4).
Billion-Word LM
Dev perplexity 29.9 (d_ff = 8192, 192M total params).
Decode latency
46 us/token greedy (47 ms/step); 203 us/token beam-4 search; 13.2 us/token training.

Source: Shazeer (2019), Tables 1, 2, and 3.

Without clicking through the tabs, the empirical hierarchy across Tables 1, 2, and 3 is clear:

  • MHA baseline (h=8, d_k=128) sets the quality ceiling (26.7 dev BLEU, 29.9 LM PPL), but stalls incremental decoding at 46 us/token greedy and 203 us/token beam-4.
  • Shrinking head count (h=1..4) or head width (d_k=16..64) achieves the same cache reduction at the cost of 0.5 to 0.9 BLEU and 0.7 to 1.0 LM perplexity beyond MQA.
  • Multi-Query Attention recovers nearly all MHA quality (26.5 dev BLEU, 28.5 beam-4 test BLEU, 30.2 LM PPL) while delivering 12.1x greedy and 6.3x beam-4 decoder speedups.
  • MQA + local window (32 tokens) cuts beam-4 latency further to 16 us/token while matching global MQA’s 26.5 dev BLEU on short translation sentences.

Caveats and how MQA evolved into GQA and MLA

Three caveats keep Shazeer’s 2019 numbers in proper engineering perspective.

First, the experimental scale and sequence lengths were small. Every run used a 6-layer ~200M-parameter model at sequence length 128 to 256 on TPUv2, and Shazeer padded KV caches to fixed maximum length 128 (or 32 for local) because XLA required static shapes. Modern serving engines avoid padding waste with PagedAttention and fuse attention loops with FlashAttention-2, meaning actual end-to-end speedups depend heavily on sequence length, batch concurrency, and whether decode is cache-bound or weight-bound.

Second, MQA is not a zero-cost drop-in for pretrained MHA checkpoints. Shazeer trained from scratch and widened d_ff from 4,096 to 5,440 (and 8,192 to 9,088) to keep parameter counts equal. Dropping 7 of 8 KV heads on an existing MHA model without continued training destroys attention alignment, which is why Grouped-Query Attention introduced mean-pooling uptraining over 5% of the pretraining budget.

Third, a single KV head creates quality and tensor-parallelism friction at 70B+ scale. When serving a large model across 8 tensor-parallel GPUs, 8 KV groups let each GPU hold one shard cleanly; 1 shared KV head forces every rank to duplicate the single KV head or communicate across ranks. That distributed systems friction, alongside slight quality degradation on large frontier models, pushed most modern labs toward GQA (4 to 8 KV heads) or Multi-Head Latent Attention, which compresses all heads into a shared low-rank latent instead of picking just one head.

For inference and architecture engineers today, Shazeer’s 9-page paper remains required reading because it proved the underlying principle: query diversity is far more precious than key-value diversity, and the true bottleneck in autoregressive Transformer serving is memory-bandwidth traffic from reloading KV state, not arithmetic FLOPs.