FoundationsChecking saved investigationReading browser-local route memory before showing a continuation.

Foundation Lab

Grouped Query Attention (GQA)

Used in LLaMA 2, Mistral—critical for efficient long-context inference

Concept 80 of 100EfficiencyPhase 2
#80GQAEfficiency
key equation\text{KV cache} = \frac{h}{g} \times \text{MHA cache}

Selected Foundation Object

Keep the equation fixed; move through the evidence.

Concept 80 of 100GQAEfficiency / Phase 2: Architecture fundamentals
Current question

Not all heads need unique K, V—sharing works surprisingly well

\text{KV cache} = \frac{h}{g} \times \text{MHA cache}
PredictionCommit before the demo.

Ask what should change when the equation is manipulated, then let the visualization test that expectation.

EvidenceCompare local witness and source.

Use the runnable panel, the key equation, and canonical papers as separate forms of evidence for the same object.

InvariantName what survives notation changes.

The useful learning product is the reusable mechanism you can carry into another model, paper, or engineering tradeoff.

Next moveContinue through the atlas.

Use prerequisites, dependents, and semantic links to repair the next gap without leaving the object behind.

Why It Matters for Modern Models

  • Used in LLaMA 2, Mistral—critical for efficient long-context inference
  • Reduces KV cache without significant quality loss
  • Enables running larger context windows on consumer hardware

What Tutorials Skip

What is still poorly explained in textbooks and papers:

  • Not all heads need unique K, V—sharing works surprisingly well
  • GQA with g=h is MHA; g=1 is MQA; g in between is the sweet spot
  • Speedup comes from smaller memory reads, not fewer FLOPs

Interactive Visualization

Core Math (Optional Deep Dive)

If you want intuition first, start with the key equation and the visualization. Come back here for the full walkthrough.

Key Equation
KV cache=hg×MHA cache\text{KV cache} = \frac{h}{g} \times \text{MHA cache}

Multi-Head Attention: Each head has own Q, K, V.
Multi-Query Attention: All heads share K, V; each has own Q.
Grouped Query Attention: Groups of heads share K, V:

GQA:headi=Attention(XWiQ,XWg(i)K,XWg(i)V)\text{GQA}: \text{head}_i = \text{Attention}(XW^Q_i, XW^K_{g(i)}, XW^V_{g(i)})

where heads in group gg share the same K, V projections.

KV cache savings: Memory reduced by factor hg\frac{h}{g} (h heads, g groups).

Canonical Papers

GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints

Ainslie et al.2023EMNLP
Read paper →

Connections

Next Moves

Choose the next question to carry this object forward.