The short answer

Multi-head attention gives every query head its own key head and value head. Multi-query attention keeps all those query heads, though one key head and one value head now serve every one of them. Grouped-query attention sits between the two, since each group of query heads shares a single key and value pair. Shazeer introduced multi-query attention in 2019. Ainslie and colleagues introduced grouped-query attention in 2023, calling it “an interpolation between multi-head and multi-query attention”. The query head count never moves. Only the key and value head count does.

A decoder produces one token per forward pass. At every pass it reloads the cached keys and values for each earlier position. That reload is the slow step, and it grows with sequence length.

Multi-query attention and grouped-query attention both shrink that cache. Neither one touches the query heads. Both cut how many key and value heads exist, so fewer bytes move per decoding step.

This guide reads the two source papers directly. Shazeer wrote the multi-query paper at Google in 2019. Ainslie and colleagues wrote the grouped-query paper at Google Research in 2023, and their measured tables settle most of the usual arguments.

Three-panel diagram titled MHA, MQA and GQA, each showing eight query head boxes above their key and value heads, with MHA using eight key-value boxes, MQA using one wide shared box and GQA using two boxes each shared by four query heads
Query heads never change across the three schemes. Only the number of key and value heads does.

Why Loading Keys and Values Is the Bottleneck

Attention heads turn up in encoder and decoder stacks alike, as our self-attention vs cross-attention guide sets out. Training runs those heads across the whole sequence at once. Incremental decoding cannot.

Shazeer frames the entire problem as “the ratio of memory access to arithmetic operations”. That ratio matters because accelerators are lopsided. He describes hardware “where the computational capacity can be two orders of magnitude higher than the memory bandwidth”.

During incremental decoding that ratio climbs toward one. Shazeer writes that this ends up “causing memory bandwidth to be a major performance bottleneck on modern computing hardware”. The chip then waits on memory rather than on arithmetic.

So what exactly gets reloaded? The cached keys and values from every earlier position, at every single step. Shazeer’s abstract names the memory-bandwidth cost of repeatedly loading those large tensors as the core problem.

Serial decoding is also the target of speculative decoding, though from another angle. Speculative decoding cuts how many serial steps you run. Multi-query attention cuts the bytes each step must move. The two levers compose cleanly.

How Multi-Head Attention Works

Multi-head attention runs several attention layers side by side. Shazeer’s translation baseline uses eight heads, with key and value dimensions of 128. Every head owns a query projection, a key projection and a value projection.

Because each head owns its keys and values, the cache holds one set per head. That is the largest KV footprint among the three schemes. Nothing at all gets shared.

Queries and keys are also the tensors that positional schemes rewrite, as our RoPE vs absolute positional encoding guide covers. Head structure and position handling stay separate choices, though. Neither one forces the other.

Training this layer is quick, since the whole sequence runs in parallel. Decoding is where the bill arrives. Every step reloads eight sets of keys and values rather than one.

How Multi-Query Attention Works

Multi-query attention changes exactly one thing. The query heads stay untouched, while a single key head and a single value head serve all of them. Ainslie describes a scheme “which uses multiple query heads but single key and value heads”.

Their Figure 2 caption says it in one line. “Multi-query attention shares single key and value heads across all query heads.” Shazeer’s abstract makes the same point, since keys and values are shared across every attention head in his variant.

The KV cache therefore shrinks by the head count. With eight heads, a single set replaces eight sets. Fewer bytes then move at every decoding step.

Notice what did not change here. The number of query heads is identical to the multi-head baseline. Only the key and value head count moved, and that one detail is what most explanations blur.

How Grouped-Query Attention Works

Grouped-query attention is a dial rather than a third mechanism. Ainslie and colleagues define it as “an interpolation between multi-head and multi-query attention with single key and value heads per subgroup of query heads”.

Query heads split into G groups. Each group shares one key head and one value head. GQA-8 therefore names eight groups, so eight key and value pairs survive.

Both endpoints matter here. With one group, grouped-query attention is exactly multi-query attention. With as many groups as query heads, it is exactly multi-head attention. Everything between those endpoints is a tunable middle.

Why add the dial at all? Because “multi-query attention (MQA) can lead to quality degradation and training instability”, in the newer paper’s own words. A gentler cut keeps more of the baseline quality.

You also do not need a fresh training run. The recipe uptrains existing multi-head checkpoints “using 5% of original pre-training compute”. First “The key and value heads are mean-pooled to the appropriate MQA or GQA structure”. Then the model gets pre-trained a little longer.

MHA vs MQA vs GQA: Comparison Table

Three-column infographic comparing MHA, MQA and GQA across six rows covering key and value heads, KV cache size, attention FLOPs, decode speed, quality and measured T5-XXL inference time
MHA vs MQA vs GQA at a glance: what each scheme shares, what it costs and what it measured on T5-XXL.

The table below sets the three schemes side by side, row by row. Every value traces back to one of the two papers named above.

AspectMHA (multi-head)MQA (multi-query)GQA (grouped-query)
Query heads per layerEight, when following Shazeer’s baselineStill eight, exactly as in MHAStill eight, since query heads never change
Key and value heads per layerOne pair per query head, namely eightOne pair in total, rather than eightOne pair per group, so eight for GQA-8
What gets sharedNothing, since every head owns its ownKeys and values, across all query headsKeys and values, though only inside a group
KV cache size per tokenLargest, when compared across the threeSmallest, namely one set instead of eightBetween the two, since G sets survive
Attention arithmetic per tokenThe baseline amount, still the referenceEssentially unchanged, rather than reducedEssentially unchanged as well
What the real bottleneck isMemory bandwidth, when decoding seriallyMemory bandwidth too, though far less of itMemory bandwidth again, tuned by group count
Decoder inference speed46 microseconds per token, also 203 with beam-43.8 microseconds, while beam-4 drops to 32Not in Shazeer’s table, since GQA came later
Training speed13.2 microseconds, generally untouched13.0 microseconds, so barely differentNot reported in the 2019 tables either
Quality against the MHA baselineThe baseline itself, namely the reference row“slightly worse than the baseline”, per Shazeer“quality close to multi-head attention”, when uptrained
Training stabilityStable, still the reference caseAinslie report “quality degradation and training instability”Introduced partly because of that instability
A special case of another schemeYes, since GQA with eight groups is MHAYes, since GQA with one group is MQANo, rather it is the general form of both
How to get one from an MHA checkpointAlready the starting point, so nothing to doMean-pool the key and value heads, then uptrainMean-pool into groups, then uptrain likewise
Paper and yearThe baseline in both papers, 2019 and 2023Shazeer, arXiv 1911.02150, 2019Ainslie and colleagues, arXiv 2305.13245, 2023
Measured T5 inference time0.37s at Large, though 1.51s at XXL0.24s at XXL, still the fastest row0.28s at XXL, so very close to MQA
Measured T5 average score46.0 at Large, while XXL reaches 47.246.6 at XXL, above MHA-Large47.1 at XXL, nearly matching MHA-XXL
Dev ln(PPL) on WMT14 EN-DE1.424, namely the baseline row1.439, so slightly worse than MHANot measured, since the paper came later
Where it turns up todayNamed by Ainslie as common, including T5 and LLaMANamed by Ainslie as already used in PaLMProposed when MHA quality and MQA speed both matter

One row above deserves a second look, namely attention arithmetic per token. All three schemes compute roughly the same amount. The next section shows why that matters so much.

The Arithmetic Barely Changes

Horizontal bar chart of TPUv2-microseconds per output token showing training at 13.2 for multi-head and 13.0 for multi-query, annotated almost identical, against decoder inference at 46 and 3.8, annotated about 12 times faster
Training barely moves from 13.2 to 13.0 microseconds, while decoder inference falls from 46 to 3.8.

People often describe multi-query attention as a way to make attention cheaper to compute. That description gets the trade backward. The attention computation per token is essentially unchanged.

What actually falls is the number of key and value bytes reloaded at each decoding step. This is a KV-cache story, not a FLOPs story. Shazeer’s framing is “the ratio of memory access to arithmetic operations”, never the arithmetic alone.

His Table 2 proves the point better than any argument could. It reports TPUv2-microseconds per output token, at sequence length 128.

Attention typeTrainingInference, enc + decBeam-4 search, enc + dec
multi-head13.21.7 + 462.0 + 203
multi-query13.01.5 + 3.81.6 + 32
multi-head local13.21.7 + 231.9 + 47
multi-query local13.01.5 + 3.31.6 + 16

Read the two leftmost number columns together. Training moves from 13.2 to 13.0 microseconds, which is essentially nothing. Decoder inference moves from 46 to 3.8 microseconds, which is enormous.

Same model, same arithmetic, two completely different outcomes. Training runs in parallel across the sequence, so bandwidth barely constrains it. Decoding runs serially, so bandwidth dominates it.

Beam search tells the same story again. The decoder cost falls from 203 microseconds to 32. Encoder cost hardly budges, moving from 2.0 to 1.6, because the encoder was never the bandwidth problem.

What Multi-Query Attention Actually Costs

None of this comes free. Shazeer concedes that his models “incur only minor quality degradation from the baseline”. The 2023 paper is blunter, warning that “multi-query attention (MQA) can lead to quality degradation and training instability”.

Be fair to multi-query attention, though, because the lazy framing misses the real claim. Shazeer’s argument is comparative rather than absolute. If you want a smaller KV footprint, the obvious alternative is shrinking the heads themselves.

Attention typehdk, dvdffln(PPL) devBLEU devBLEU test, beam 1 / 4
multi-head812840961.42426.727.7 / 28.4
multi-query812854401.43926.527.5 / 28.5
multi-head local812840961.42726.627.5 / 28.3
multi-query local812854401.43726.527.6 / 28.2
multi-head112867841.51825.8
multi-head26467841.48026.226.8 / 27.9
multi-head43267841.48826.1
multi-head81667841.51325.8

The bottom four rows are the control group. Each one shrinks the head count or the head dimensions instead. Every single one scores worse than multi-query attention on dev perplexity.

That is the whole point of the table. Shazeer writes that “the multi-query attention model seems to be slightly worse than the baseline, but much closer than any of the alternatives involving decreasing h, dk and dv”. The language-model benchmark repeats the pattern, where multi-query reaches 30.2 dev perplexity against the multi-head baseline at 29.9. Every naive shrinking variant lands between 30.9 and 31.2.

Shazeer restates the finding plainly for that benchmark. “The multi-query attention model was slightly worse than the baseline, but significantly better than any of the alternatives involving decreasing h, dk and dv.”

Now for the number that gets misread most often. Shazeer notes that multi-query “actually had the highest BLEU score (28.5) with beam-4 decoding”. Read that against the rest of its own row, though. Dev ln(PPL) is worse, at 1.439 against 1.424. Dev BLEU is worse too, at 26.5 against 26.7.

So do not read 28.5 as multi-query beating multi-head. The paper’s own summary calls it “slightly worse than the baseline”. One test number landing 0.1 higher is noise, rather than a quality gain. Multi-query attention is not better than multi-head attention here.

What GQA Measured

Ainslie and colleagues measured T5 models with all three schemes. They uptrained T5-XXL checkpoints at proportion alpha = 0.05, which took approximately 600 TPUv3 chip-days. Their headline table reports inference time in seconds, alongside an average over all datasets.

ModelInference time (s)Average
MHA-Large0.3746.0
MHA-XXL1.5147.2
MQA-XXL0.2446.6
GQA-8-XXL0.2847.1

Look at MHA-Large against the two XXL variants below it. MQA-XXL runs at 0.24 seconds and scores 46.6. GQA-8-XXL runs at 0.28 seconds and scores 47.1. MHA-Large is slower, at 0.37 seconds, while scoring lower at 46.0.

That comparison is the strongest single result in either paper. A bigger model with fewer key and value heads beats a smaller model with full multi-head attention. It wins on speed and on quality at the same time.

Grouped-query attention then closes most of the remaining gap. GQA-8-XXL scores 47.1 against 47.2 for MHA-XXL, while running roughly five times quicker. The abstract summarises it well, since “uptrained GQA achieves quality close to multi-head attention with comparable speed to MQA”.

One more finding cuts against a common assumption. Bigger models do not suffer relatively more from the KV cache. Ainslie and colleagues note that “larger models suffer relatively less from memory bandwidth overhead from attention”, because the cache grows with model dimension while FLOPs and parameters grow with its square.

Which One You Meet in Practice

Plain multi-head attention remains the thing you meet in most textbooks and most older checkpoints. Ainslie name T5 and LLaMA as examples that did not use multi-query attention. Multi-head attention is also the baseline every comparison starts from.

Multi-query attention shows up where decode latency dominates everything else. Ainslie name PaLM as a model that already used it. The KV cache is smallest there, so long contexts and large batches both get cheaper.

Grouped-query attention is the setting you reach for when neither extreme fits. It gives you a knob rather than a choice between two fixed points. Pick more groups if quality slips; pick fewer if memory is tight.

Decoder-only inference is exactly where all this bites hardest, the family our BERT vs GPT guide contrasts with encoder stacks. Scaling the model up changes the calculus again, as our Mixture of Experts vs dense models guide explains. Sparse routing moves parameters off the critical path, while grouped-query attention moves cache bytes off it.

For an exam or an interview, lead with the head counts rather than the speedups. Say how many query heads each scheme keeps. Then say how many key and value heads it keeps. Those two numbers separate all three schemes immediately.

Interview Questions

All three keep the same number of query heads. Multi-head attention gives each query head its own key and value head. Multi-query attention shares one key and value head across every query head. Grouped-query attention shares one pair per group, so it interpolates between the other two.

Because training is parallel across the sequence, so memory bandwidth rarely binds it. Decoding is serial, so bandwidth binds it hard. Shazeer’s Table 2 shows training moving from 13.2 to 13.0 microseconds. Decoder inference moves from 46 to 3.8 microseconds over the same pair of models.

No. Multi-query did post the “highest BLEU score (28.5) with beam-4 decoding” in Table 1. Dev ln(PPL) still went the other way, at 1.439 against 1.424, and dev BLEU fell from 26.7 to 26.5. Shazeer’s own summary calls multi-query “slightly worse than the baseline”, so treat the 28.5 as noise.

GQA-1 means a single group, which makes it identical to multi-query attention. GQA with as many groups as query heads is identical to multi-head attention. That is why the paper calls grouped-query attention “an interpolation between multi-head and multi-query attention”. It is a dial, rather than a third mechanism.

You uptrain it, since full pre-training is unnecessary. In the paper “The key and value heads are mean-pooled to the appropriate MQA or GQA structure”. The converted checkpoint then trains a little longer, “using 5% of original pre-training compute”. At alpha = 0.05 that came to roughly 600 TPUv3 chip-days.

Frequently Asked Questions

They differ only in how many key and value heads they keep. Multi-head attention keeps one pair per query head. Multi-query attention keeps one pair in total, shared across every query head. Grouped-query attention keeps one pair per group of query heads, so it lands between the other two.

No, the attention computation per token is essentially unchanged. What falls is the volume of key and value bytes reloaded at each decoding step. Shazeer’s Table 2 shows training barely moving, from 13.2 to 13.0 microseconds, while decoder inference drops from 46 to 3.8 microseconds.

Slightly, yes. Shazeer reports models that “incur only minor quality degradation from the baseline”, and dev ln(PPL) sits at 1.439 against 1.424. His real claim is comparative, since multi-query stays “much closer than any of the alternatives involving decreasing h, dk and dv”. Naive head shrinking does worse.

Not really, since it generalises both of the others. Ainslie and colleagues call it “an interpolation between multi-head and multi-query attention with single key and value heads per subgroup of query heads”. One group gives you multi-query attention exactly. As many groups as query heads gives you multi-head attention exactly.

No, and this is the detail most explanations blur. The query head count is identical across all three schemes. Only the key and value head count changes, which is why Ainslie describe multi-query attention as using “multiple query heads but single key and value heads”.

Yes, and that is the first contribution of the GQA paper. The recipe uptrains multi-head checkpoints “using 5% of original pre-training compute”. During conversion “The key and value heads are mean-pooled to the appropriate MQA or GQA structure”. No training run from scratch is needed.

Because “multi-query attention (MQA) can lead to quality degradation and training instability”, as the GQA paper puts it. A gentler cut keeps more baseline quality while giving up little speed. On T5-XXL, GQA-8 scores 47.1 at 0.28 seconds against 46.6 at 0.24 seconds for multi-query.

Relatively, they do not. Ainslie and colleagues write that “larger models suffer relatively less from memory bandwidth overhead from attention”. The cache grows with model dimension, while FLOPs and parameters grow with its square. The absolute cache still gets bigger, though its share of the total cost shrinks.

Wrapping Up

The three schemes differ along one axis only. Query heads stay constant, while the key and value head count drops from one per head, to one per group, to one in total.

Keep three measured facts past the acronyms. Training time barely moves, from 13.2 to 13.0 microseconds, though decoder inference falls from 46 to 3.8. Multi-query attention is slightly worse than the baseline, yet much better than naively shrinking heads. Grouped-query attention recovers most of that gap, since GQA-8-XXL scores 47.1 against 47.2 for MHA-XXL.

Related reading on DiffStudy:

Whatsapp-color Created with Sketch.

By Arun Kumar

Full Stack Developer with a BE in Computer Science, working with React, Next.js, Node.js, MongoDB, and AI/ML tools. Founder of DiffStudy — built to help CS students ace GATE and university exams, and keep developers up to date across AI, cloud, system design, web development, and every field of computer science. Every article is written from real hands-on experience, not just theory.

Leave a Reply

Your email address will not be published. Required fields are marked *