Skip to main content

Flash Attention vs MHA vs GQA vs MQA: Comparing Attention Mechanisms

Compare head sharing with explicit projection dimensions and KV-cache bytes. Separate exact memory ratios from measured quality and latency, and see why FlashAttention is an independent choice.

A decoder can keep many query heads while sharing fewer key and value heads. Multi-head attention (MHA), grouped-query attention (GQA), and multi-query attention (MQA) choose how much K/V to share. This changes the learned projections and the size of the KV cache.

FlashAttention answers a separate question: how should the selected attention operation run on the GPU? It tiles and fuses computation to reduce transfers to high-bandwidth memory (HBM), without approximating softmax attention. A supported FlashAttention implementation can run MHA, GQA or MQA. Head sharing and an IO-aware kernel can therefore be combined. FlashAttention paper.

Compare the Variants

Move the slider from eight K/V heads to one. The eight query heads stay in place while the wiring and compact cache payload change. This is the same teaching configuration as the GQA explorer: model width 1,024, head width 128, 32 layers, 4,096 cached tokens, one sequence and two bytes per element.

Share K/V heads, keep every query head

4 query heads share each K/V head.

Hq = 8 query heads0123456701Hkv = 2 key heads + 2 value heads

Every query head still scores T = 4,096 cached keys: Hq × T = 32,768 pairs. Sharing K/V does not share attention weights or remove query heads.

D = 1024 model width · d = 128 per head · Hq = 8 · Hkv = 2

B = 1 sequence · L = 32 layers · T = 4,096 tokens · b = 2 bytes (FP16/BF16)

KV payload = 2 × B × L × T × Hkv × d × b. Excludes weights, workspace, allocator overhead and replicated or expanded K/V.

Read the outcome in two parts. Fewer K/V heads mean fewer K/V projection weights and cached rows. Every query head still computes its own attention scores and output, so sharing does not reduce the number of query-key score pairs by the cache ratio. The figure calculates dimensions and bytes; it does not predict model quality or measured speed.

Detailed Comparison

Architecture Differences

Keep model width and head width distinct:

SymbolMeaningFigure value
DModel width, the input and output hidden dimension1,024
dWidth of each query, key and value head128
HqNumber of query heads8
HkvNumber of key heads and number of value heads8, 4, 2 or 1
BNumber of equal-length sequences in the batch1
LNumber of attention layers32
TCached tokens per sequence4,096
bBytes per cached element2, for FP16 or BF16

The figure uses H_q d = D, but the general formulas below do not require that equality. Each input row is projected to H_q d query features and H_{kv} d key and value features:

FeatureMHAGQAMQA
Query headsHqHqHq
K/V headsHkv = Hq1 < Hkv < HqHkv = 1
Queries per K/V head1Hq / HkvHq
Q projection shapeD × (Hq d)D × (Hq d)D × (Hq d)
K and V shapes, eachD × (Hq d)D × (Hkv d)D × d

For this evenly grouped layout, Hkv divides Hq. Counting Q, K and V weights per layer, excluding biases gives:

P_{QKV} = D H_q d + 2 D H_{kv} d = D(H_q + 2H_{kv})d

The output projection maps the concatenated query-head outputs back to model width. Count it separately:

P_O = (H_q d)D

MHA has P_{QKV} = 3D H_q d, which becomes 3D^2 when H_q d = D. GQA and MQA reduce the K/V portion; the Q and output projections remain the same in this comparison. These counts do not include feed-forward layers, embeddings or other model weights.

Memory Footprint

A compact dense cache stores both K and V for every cached token at every layer:

M_{KV} = 2 B L T H_{kv} d b \quad \text{bytes}

The factor 2 is for K and V, not the element size. Using the figure's B = 1, L = 32, T = 4,096, d = 128 and b = 2:

VariantHkvPayload bytesPayload MiBFraction of MHA
MHA8536,870,912512100%
GQA with 4 K/V heads4268,435,45625650%
GQA with 2 K/V heads2134,217,72812825%
MQA167,108,8646412.5%

One MiB is 2^{20} bytes. At fixed B, L, T, d and b, the payload fraction is exactly H_{kv}/H_q. A label such as “GQA-8” specifies eight K/V heads; its ratio also depends on Hq. With 32 query heads it is 25% of MHA; with 64 it is 12.5%.

These are tensor payloads, not total GPU memory. Weights, attention workspace, allocator padding and metadata add memory. Tensor-parallel replication or materializing K/V copies for every query head can also change the stored amount. For variable sequence lengths, count their cached tokens individually; windowed caches use the tokens actually retained.

Mathematical Formulations

For a query head i, head sharing changes which K and V tensors it reads:

\operatorname{head}_i = \operatorname{Attention}(Q_i, K_{g(i)}, V_{g(i)})

MHA: Separate K/V per Query

g(i) = i \qquad H_{kv} = H_q

Each query head has a separate learned K/V projection. There is no sharing between query heads.

GQA: Share within Groups

g(i) = \left\lfloor \frac{i H_{kv}}{H_q} \right\rfloor \qquad 0 \leq i < H_q

With eight queries and two K/V heads, queries 0–3 read K/V head 0 and queries 4–7 read K/V head 1. The index is zero-based. Queries in a group share keys and values, but each query produces its own softmax weights and mixed value vector.

MQA: One Shared K/V Head

g(i) = 0 \qquad H_{kv} = 1

All query heads read the same K/V history. This is the maximum-sharing endpoint of the same axis.

For one new query token attending to T cached keys, each of Hq query heads still scores T keys:

\text{decode score pairs per sequence and layer} = H_q T

In the figure that is 8 × 4,096 = 32,768 score pairs at every slider position. Sharing can save K/V projection work and memory traffic while leaving this count unchanged. Actual latency depends on how the kernel reuses the shared data.

Production Model Configurations

These examples describe named model releases, not recommended settings to apply to any checkpoint:

ReleaseHead-sharing architectureQuery / K/V headsOther detail
Llama 2 7BMHA32 / 32The original model card marks GQA absent
Llama 2 70BGQA64 / 8The paper describes eight K/V projections
Mistral 7B v0.1GQA32 / 8Configured sliding window of 4,096 tokens
Falcon 40BGQA by this definition128 / 8Published config uses new_decoder_architecture: true

Sources: Llama 2 model card, Llama 2 architecture discussion, Mistral 7B v0.1 config, and Falcon 40B config. The Llama query counts follow the original Llama architecture table and the architecture carried into Llama 2; the Llama 2 sources identify its head-sharing change.

Read configuration fields alongside the model implementation. A family name or an informal “multi-query” label is insufficient to determine how many K/V heads a particular release stores.

Optimization Combinations

The mechanisms operate on different parts of attention:

ChoiceWhat it changesWhat it preserves
MHA, GQA or MQALearned Q/K/V projection layout and K/V sharingThe selected number of query heads
FlashAttentionTiling, fusion and transfers between HBM and on-chip memoryThe mathematical attention operation and selected mask
Sliding-window attentionWhich past positions each query may attend toThe chosen head-sharing layout
RoPEPosition-dependent rotations of queries and keysThe chosen head-sharing layout

FlashAttention's “exact” means it computes softmax attention without a sparsity or kernel approximation. Floating-point roundoff can differ with the order of operations; it does not imply bitwise-identical outputs. It also does not shrink the persistent KV cache by the Hkv/Hq ratio. That ratio comes from the architecture, and the implementation must store the shared K/V compactly to realize it. FlashAttention paper.

Quick Decision Matrix

Start from the available checkpoints and workload, then measure the quantity that matters:

ConstraintWhat to check
KV memory budgetCalculate payload for the checkpoint's heads, dtype, retained tokens and concurrency; measure total allocated memory
Task qualityEvaluate the candidate checkpoints on the same task and data; head count alone does not predict quality
Decode latencyBenchmark the serving kernel at the intended batch and context lengths
Prefill throughputBenchmark the attention implementation and mask on representative prompt lengths
Multiple GPUsCheck K/V sharding and replication across the tensor-parallel layout

There is no universal “GQA-8 default” or “MQA is lowest latency” rule. In the Llama 2 paper's Appendix A.2.1, the authors compare trained attention variants and discuss serving across eight tensor-parallel GPUs. Replicating a single MQA K/V head across those GPUs can make its aggregate cache as large as eight-head GQA. This illustrates why logical payload ratios alone do not determine deployment speed or memory.

Best Practices

  1. Read the checkpoint's architecture first. Changing Hkv changes learned projection shapes. Converting a pretrained MHA checkpoint to GQA or MQA requires an architecture conversion and training or uptraining, followed by evaluation; it is not a drop-in inference setting. The GQA paper studies such conversion through uptraining.
  2. Separate architecture from kernel choice. Choose a supported attention kernel for that checkpoint's head layout, dtype and mask. A FlashAttention option changes execution, not the learned number of K/V heads.
  3. Keep comparisons controlled. Report the model release, task metric, batch size, prompt and decode lengths, dtype, GPU and serving implementation with any quality, latency or throughput claim.

If you found this explanation helpful, consider sharing it with others.

Mastodon