Navigation
📐 Foundations· 14 min read

Attention Variants

From Multi-Head Attention to GQA, MLA, linear attention, and state-space models — how attention architectures evolve to meet infrastructure constraints.

mhamqagqamlalinear-attentionmambassm

The original multi-head attention (MHA) works well but is expensive — especially at inference time where KV cache memory scales with head count. This chapter traces the evolution of attention variants, each designed to solve a specific infrastructure bottleneck.


1. The KV Cache Problem

During autoregressive generation, the model stores Key and Value tensors for all previous tokens (the KV cache) to avoid recomputation. Per token per layer:

KV cache per token=2×Nkv×dk×bytes_per_element\text{KV cache per token} = 2 \times N_{kv} \times d_k \times \text{bytes\_per\_element}

For LLaMA-2-7B with MHA (Nkv=32,dk=128N_{kv}=32, d_k=128, FP16):

  • Per token per layer: 2×32×128×2=162 \times 32 \times 128 \times 2 = 16 KB
  • Per token (32 layers): 32×1632 \times 16 KB = 512 KB per token
  • For 4K context: 4096×5124096 \times 512 KB = 2 GB of KV cache alone

At 128K context, that’s 64 GB — most of an H100’s memory consumed by KV cache for a single request. Every attention variant below exists to reduce this cost.


2. Multi-Query Attention (MQA)

Idea: All Q heads share a single K and V head.

Nh Q heads,1 K head,1 V headN_h \text{ Q heads}, \quad 1 \text{ K head}, \quad 1 \text{ V head}

KV cache reduction: Nh×N_h \times smaller (e.g., 32× for 32 heads).

Tradeoff: Quality degrades because all Q heads share a single K/V representation. Different heads can no longer learn independent attention patterns (e.g., one for syntax, another for semantics). Used by PaLM, Falcon, but largely superseded by GQA.


3. Grouped-Query Attention (GQA)

MHA vs GQA vs MQA

Idea: A middle ground — group Q heads and have each group share one K/V head.

Nh Q heads,Nkv K/V heads(Nkv<Nh)N_h \text{ Q heads}, \quad N_{kv} \text{ K/V heads} \quad (N_{kv} < N_h)

Each group of Nh/NkvN_h / N_{kv} Q heads shares one K/V head.

ConfigQ headsKV headsGroup sizeKV cache reduction
MHA646411× (baseline)
GQA-864888×
MQA6416464×

Used by: LLaMA-2-70B (8 KV heads), Mistral-7B (8 KV heads), LLaMA-3 (8 KV heads).

Quality vs MHA: Negligible difference in practice — the KV cache savings far outweigh any quality loss.


4. Multi-Latent Attention (MLA)

Idea (DeepSeek-V2/V3): Instead of storing full K and V, compress them into a low-dimensional latent vector and reconstruct on the fly.

ct=htWDKV(compress: d→dc)\mathbf{c}_t = \mathbf{h}_t \mathbf{W}^{DKV} \qquad \text{(compress: } d \to d_c \text{)} Kt=ctWUK,Vt=ctWUV(reconstruct during attention)\mathbf{K}_t = \mathbf{c}_t \mathbf{W}^{UK}, \qquad \mathbf{V}_t = \mathbf{c}_t \mathbf{W}^{UV} \qquad \text{(reconstruct during attention)}

Only the compressed ct\mathbf{c}_t (dimension dc≪dd_c \ll d) is stored in KV cache.

KV cache reduction: With dc=512d_c = 512 vs Nkv×dk=8×128=1024N_{kv} \times d_k = 8 \times 128 = 1024 (GQA-8), MLA achieves ~2× further reduction on top of GQA.

Key insight for infrastructure: The reconstruction adds compute (extra matrix multiplies during attention), but eliminates memory — trading compute for memory. At long contexts where KV cache is the bottleneck, this is a winning trade.


5. Linear Attention

Standard attention is O(S2)O(S^2) in both compute and memory (due to the S×SS \times S attention matrix). Linear attention avoids materializing this matrix:

Standard:softmax(QK⊤)VO(S2d)\text{Standard}: \quad \text{softmax}(QK^\top)V \qquad O(S^2d) Linear:ϕ(Q)(ϕ(K)⊤V)O(Sd2)\text{Linear}: \quad \phi(Q)(\phi(K)^\top V) \qquad O(Sd^2)

By changing the order of operations (compute K⊤VK^\top V first, which is d×dd \times d, then multiply by QQ), complexity drops from O(S2d)O(S^2d) to O(Sd2)O(Sd^2).

Tradeoff: Linear attention approximates softmax attention — quality is generally lower, especially for tasks requiring precise token-to-token attention patterns. No production LLM uses pure linear attention, but hybrid architectures are emerging.


6. State-Space Models (Mamba)

Idea: Replace attention entirely with a recurrence that processes tokens sequentially with a fixed-size state:

ht=Aˉht−1+Bˉxt,yt=Chth_t = \bar{A} h_{t-1} + \bar{B} x_t, \qquad y_t = C h_t

Where Aˉ,Bˉ\bar{A}, \bar{B} are discretized state transition matrices and hth_t is a fixed-size hidden state.

Advantages:

  • O(S)O(S) compute and memory (linear in sequence length)
  • No KV cache — fixed state size regardless of context length
  • Efficient parallel training via the “parallel scan” algorithm

Disadvantages:

  • Cannot do exact “lookup” attention (e.g., “find the name mentioned in paragraph 3”)
  • Quality gap vs Transformers on retrieval-heavy tasks

Hybrid architectures (Jamba, Zamba) interleave Mamba layers with attention layers. Typical ratio: 1 attention layer per 4-8 Mamba layers. Attention handles retrieval-heavy operations (“find the name in paragraph 3”) while Mamba handles bulk sequential processing. Jamba (AI21, 2024) uses a 1:7 ratio with 52B parameters, achieving competitive quality with much lower KV cache than a pure Transformer.

Infrastructure Implications

Attention Variants Comparison

ArchitectureMemory (inference)Compute (inference)Best for
MHAO(S)O(S) KV cacheO(S2)O(S^2) per stepQuality-critical, short context
GQAO(S/g)O(S/g) KV cacheO(S2)O(S^2) per stepProduction LLMs (LLaMA, Mistral)
MLAO(S⋅dc/d)O(S \cdot d_c/d) KV cacheO(S2)O(S^2) + reconstructVery long context (DeepSeek)
Linear AttnO(d2)O(d^2) stateO(d2)O(d^2) per stepStreaming, infinite context
SSM (Mamba)O(d)O(d) stateO(d)O(d) per stepVery long context, streaming

Self-Check

  • Calculate KV cache size for MHA, GQA, and MLA for a given model and context length
  • Explain why GQA has minimal quality loss compared to MHA
  • Explain MLA’s “compress then reconstruct” approach and the compute-memory tradeoff
  • Explain why linear attention changes the order of operations
  • Explain why SSMs have O(1) memory per token but struggle with retrieval tasks
  • Explain why hybrid architectures (Mamba + Attention) exist

References