Skip to content
AI & Machine Learning Medium #gqa#mqa#attention

GQA & MQA: Sharing K/V Heads

MHA: heads each have K and V. MQA: one shared K/V. GQA: groups share. Quality vs cache size.

A free, animated gqa & mqa: sharing k/v heads you can read here or embed on any website, from Scrollchart.

GQA & MQA: Sharing K/V Heads

GQA & MQA: Sharing K/V HeadsReducing KV heads from 8 to 2 (GQA) or 1 (MQA) cuts cache proportionally with minimal quality lossMHAMulti-Head AttentionGQAGrouped-Query AttentionMQAMulti-Query AttentionQKVQ1Q2Q3Q4Q5Q6Q7Q8KVKVKVKVKVKVKVKV8 KVQ1Q2Q3Q4Q5Q6Q7Q8KVKV2 KVQ1Q2Q3Q4Q5Q6Q7Q8KV1 KVKV cache size (per layer, relative to MHA)MHA1x (baseline)GQA0.25xMQA0.125xQuality vs cache tradeoffLlama-2 70B family (approx MMLU)MHA69.8GQA-868.9MQA67.1GQA (G=8, used in Llama-3 and Mistral) cuts cache 4x vs MHA. MQA (G=1, used in Falcon) cuts 8x with a steeper quality dip. Both share the same forward pass cost as MHA.

Three layouts of Q, K, V heads. MHA (32 each), MQA (32 Q, 1 KV), GQA (32 Q, 8 KV groups). Cache size and downstream eval scores plotted across the three.

Good for

  • KV cache optimisation and long-context serving architecture articles
  • Llama-2 / Mistral / Falcon architecture deep dives comparing attention variants
  • Inference efficiency content on fitting large batches within GPU memory budgets

Source & accuracy

This gqa & mqa: sharing k/v heads is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Sharing K/V heads to reduce cache size

Multi-Head Attention (MHA) gives each head its own key and value projections. With 8 heads, you compute 8 independent K-V caches during generation. Multi-Query Attention (MQA) uses a single shared K-V, reducing cache size by 8x. Grouped-Query Attention (GQA) is a middle ground: two heads share one K-V pair.

The KV cache size is proportional to sequence length and the number of K-V heads. Sharing K-V dramatically reduces memory overhead, enabling longer sequences or larger batch sizes on the same hardware.

Quality trade-off and recovery

MQA sacrifices some quality compared to MHA; the single shared K-V is a bottleneck. GQA reduces cache size less but usually matches MHA quality more closely. For smaller models, the quality gap is acceptable. For large models, GQA is often preferred.

Fine-tuning a pretrained MHA model to use MQA can recover some of the quality gap, but it requires retraining and is only partially effective.

When to use each variant

MHA is the standard for training and small models. MQA is useful for inference on hardware with limited memory (mobile, edge). GQA is increasingly popular for large models, offering a good balance: significant cache reduction with minimal quality loss. Models like Llama 2 use GQA for this reason.

Embed this diagram

Add this animated gqa & mqa: sharing k/v heads to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated gqa & mqa: sharing k/v heads for any website.
Who uses it
AI/ML blogs.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="gqa-mqa" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "GQA & MQA: Sharing K/V Heads" for my website?
Scrollchart provides "GQA & MQA: Sharing K/V Heads" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a gqa & mqa: sharing k/v heads in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.