AI State of the Art
SOTA/Advanced Attention Mechanisms/25 min
Section 73

Multi-head Latent Attention (MLA)

Listen as a podcast →

A story: the cloakroom that runs out of space

Picture a large theater. At every show, the audience arrives with coats, bags, umbrellas. The cloakroom has to hold everything for the whole performance, and the fuller the house, the more it overflows. At some point there's simply no room left: it's the cloakroom, not the hall, that caps how many people you can admit.

A language model generating text lives exactly this problem. With every new word it produces, it must remember everything it has already read and written. That working memory is called the KV cache, and it (not raw compute) is what saturates first on long contexts.

Multi-head Latent Attention (MLA) is a clever way to tidy that cloakroom: instead of keeping each coat separately, you compress them into a small shared locker, then reconstruct them only when needed. The cloakroom becomes tiny, the show stays just as good.

Refresher: what the KV cache is, and why it hurts

When a Transformer reads text, each token produces three vectors per attention head: a query (Q, "what I'm looking for"), a key (K, "what I offer") and a value (V, "the information I carry"). Attention compares the current token's query against the keys of all previous tokens, then blends in their values.

The key point: to generate the next word, you don't recompute the K and V of tokens already processed, you cache them. That's the KV cache. The query is never stored: the current token's query is used once and never again, while the keys and values of the whole past are re-read at every step, you cache what gets re-read, not what is written once (the full explanation). The catch: the cache's size grows linearly with context length and with the number of heads. Over 100,000 tokens and dozens of layers, that's tens of gigabytes of GPU memory. It is the real bottleneck of inference.

Three families of attention have tried to attack this cost:

  • MHA (Multi-Head Attention), every head has its own K and V: maximum quality, huge cache.
  • MQA (Multi-Query Attention), all heads share a single K/V pair: tiny cache, but quality can suffer.
  • GQA (Grouped Query Attention), a middle ground: a few K/V groups (typically 8) shared across heads.
What MQA and GQA have in common

MQA and GQA shrink the cache by cutting the number of K/V heads: you keep fewer copies. It works, but it's a head-on trade-off, fewer K/V heads means potentially less nuance. MLA takes a different road: keep the equivalent of all heads, but in compressed form.

The MLA idea: compress, then decompress

MLA was introduced with DeepSeek-V2 (2024) and carried over into DeepSeek-V3. The intuition fits in one sentence: don't cache the full K and V, cache a small latent vector that summarizes them, and rebuild K and V on the fly when you compute attention.

DeepSeek-V2

The underlying observation is the same as in image compression: the K and V across all heads carry a lot of redundancy. You can project them into a low-dimensional ("latent") subspace without losing much. This is a low-rank compression: replace one large matrix with the product of two small ones.

The intuition: a summary, not a copy

Rather than writing down every word spoken in a meeting (the full K and V), you take a compact summary (the latent vector). When you need a specific detail, you expand just the useful part of the summary. The notebook stays thin, yet the information is recoverable.

How it works, step by step

1. The "down" projection: compress

For each token, instead of computing keys and values directly, you first compute a single compressed latent vector ctc_t, of dimension dcd_c far smaller than the combined K/V size across all heads:

ct=WDKV htc_t = W^{DKV} \, h_t

where hth_t is the representation of token tt (the vector entering the attention layer) and WDKVW^{DKV} is the compression matrix (down-projection), a learned parameter, fixed at training time, the same for all texts. The latent vector ctc_t, on the other hand, is a result, recomputed for each token of the current text. It is this vector ctc_t, and only this one, that gets cached.

2. The "up" projection: decompress

When it's time to compute attention, you reconstruct each head's keys and values from the latent, via decompression matrices (up-projection):

Kt=WUK ctVt=WUV ctK_t = W^{UK} \, c_t \qquad V_t = W^{UV} \, c_t

You thus recover full-dimensional K and V, head by head (just like MHA) but without ever having stored them. Only the small ctc_t takes up memory.

Comparison between Multi-head Latent Attention and standard Multi-head Attention: in MLA the input goes through a compression matrix producing compressed keys and values that are later decompressed, whereas MHA produces keys and values directly.
Left, MLA: the input is compressed by W_dkv into a compressed key/value pair, then expanded again by W_uk and W_uv. Right, standard MHA produces Key and Value directly. It is the compressed version, and only that, which is carried from one token to the next.Source : Sebastian Raschka, LLMs from Scratch

3. What is actually cached

The contrast is striking:

  • MHA caches, per token and per layer, the K and V of all heads.
  • MLA caches only ctc_t (plus a small position component, see below).

Because dcd_c is much smaller than the heads' combined dimension, the cache shrinks dramatically, typically in proportions comparable to, or better than, an aggressive GQA, while keeping quality close to full MHA.

Figure comparing MHA, GQA, MQA and MLA: hatched blocks mark what is cached during inference. MHA caches one key/value pair per head, GQA one per group, MQA a single shared pair, and MLA a single compressed latent vector.
The figure from the DeepSeek-V2 paper. Hatched blocks are what actually sits in the cache: a lot for MHA, very little for MQA, and for MLA a single compressed latent vector from which keys and values are re-projected.Source : DeepSeek-V2 paper (figure 3), via Lior Sinai
An efficiency trick: folding the matrices

MLA's up-projections can be mathematically absorbed into the query and output matrices. In practice you often don't have to explicitly decompress K and V at every step: you can reorganize the computation to work directly on the latent. This prevents decompression from eating up the memory savings in compute time.

The subtle point: RoPE breaks compression

Here is the technical wrinkle that makes MLA non-trivial. Modern LLMs encode word position with RoPE (Rotary Position Embedding, dedicated lesson): queries and keys are given a rotation whose angle depends on the token's position.

The problem: if a key is meant to be rebuilt on the fly via Kt=WUKctK_t = W^{UK} c_t, but must then receive a rotation that is different for every position, then you can no longer cleanly pre-fold the matrices, nor cache an already-"positioned" latent. The position-dependent rotation interferes with the low-rank compression.

The fix: decoupled RoPE

DeepSeek solves this by splitting the dimensions into two sets:

  1. A position-free part: most dimensions, compressed into the latent ctc_t and rebuilt by up-projection. They carry no position information.
  2. A small position-dedicated part: a few extra key dimensions that do receive RoPE. These are computed and cached separately.
Ktfinal=[  WUKct⏟content, compressed  ;  RoPE(WKRht)⏟position, kept apart  ]K_t^{\text{final}} = [\; \underbrace{W^{UK} c_t}_{\text{content, compressed}} \; ; \; \underbrace{\text{RoPE}(W^{KR} h_t)}_{\text{position, kept apart}} \;]

Reading the formula: token tt's final key is two pieces laid end to end. The first, WUKctW^{UK} c_t, is the content, rebuilt from the compressed latent. The second is the position: a small dedicated learned matrix, WKRW^{KR}, produces a few key dimensions from hth_t, and RoPE's rotation is applied to those alone. At attention time you concatenate the two. Result: compression stays intact for most of the vector, while position lives in a small dedicated channel that supports RoPE without breaking the rest. The extra cache for this positional part is minimal.

Detailed MLA architecture from the DeepSeek-V2 paper: the input hidden state produces a latent for keys and values, which is cached, while a small separate component receives RoPE and is concatenated to the rebuilt keys before attention is computed.
Decoupled RoPE in detail. The hatched blocks are what gets cached: the key/value latent (right) and the small positional component k_t^R, which receives RoPE separately. The two are concatenated just before attention.Source : DeepSeek-V2 paper (figure 3), via CSDN blog
Why this detail matters

Without decoupled RoPE, MLA would either be incompatible with standard positional encoding, or be forced to give up the matrix-absorption trick (and thus lose part of its advantage). Decoupling is precisely the ingredient that makes MLA both RoPE-compatible and memory-efficient.

A forward-looking variant: NoPE

Since position causes so much trouble, an obvious question arises: what if we did without it? That's the idea behind NoPE (No Positional Encoding).

The observation, surprising at first: a causal decoder (one that can only look at past tokens, never the future) already holds implicit position information. The mere fact that token 5 "sees" 5 tokens while token 50 sees 50 is enough for the network to infer an order, no explicit position injection required. Some work shows causal decoders can run without explicit positional encoding, sometimes with better generalization to lengths never seen during training (explicit encodings tend to "break down" outside their training range).

The link to MLA: since RoPE is exactly the part that complicates compression and generalizes poorly at very long context, one research direction is to use less RoPE, or none at all, leaning on implicit position. Recent architectures thus mix layers with no positional encoding and layers with a bit of RoPE, to combine long-context robustness with the ability to model precise positions.

What to remember about NoPE

NoPE doesn't mean "no notion of position whatsoever", it's the idea that the causal mask already carries part of that information, so you can lighten (or drop) explicit positional encoding, especially to generalize better to very long contexts.

Comparison table: MHA / MQA / GQA / MLA

MechanismWhat is cachedCache sizeQuality
MHAK and V of all headsLargest (baseline)Maximum (baseline)
MQAA single shared K/V pairSmallestGood, but can lag on demanding tasks
GQAA few K/V groups (often 8)Reduced (roughly ÷8 vs MHA)Near-MHA
MLAA compressed latent vector (+ small RoPE channel)Strongly reduced, comparable to or better than GQAClose to full MHA, often above GQA at equal cache

DeepSeek's argument is precisely this: at a memory budget comparable to GQA, MLA tends to preserve quality better, because it doesn't throw heads away, it compresses the information of all heads and reconstructs it.

Why it matters

The KV cache is the factor that decides how many users you can serve in parallel, and up to what context length, on a given piece of hardware. Cutting that cache directly cuts inference cost and unlocks longer contexts without changing GPUs. MLA is one of the first techniques to deliver that reduction without the usual quality trade-off of "fewer heads" approaches, which explains its adoption in frontier models and the interest it draws, including from an intellectual-property standpoint (it is a clearly identifiable, differentiating architectural optimization).

Key takeaways

  1. The KV cache is the model's working memory at inference; it grows with context and becomes the main memory bottleneck.
  2. MLA compresses the keys and values of all heads into a small latent vector (down-projection), then rebuilds them at compute time (up-projection); only the latent is cached.
  3. Unlike MQA/GQA, which reduce the number of K/V heads, MLA keeps the information of all heads but in compressed form, hence quality close to MHA at a strongly reduced cache.
  4. RoPE breaks compression: the position-dependent rotation prevents pre-folding and caching a "positioned" latent. Decoupled RoPE isolates position in a small dedicated channel, leaving the rest compressed.
  5. NoPE reminds us that a causal decoder holds implicit position; one can therefore lighten or drop RoPE, notably to generalize better to long context, hence MLA + less-RoPE combinations.
  6. Introduced with DeepSeek-V2, reused in DeepSeek-V3, MLA offers an excellent memory/quality trade-off and illustrates a deeper trend: optimize inference by compression rather than by removal.
Check your understanding0 / 5

1. During generation, what does MLA actually cache for each token?

2. In the formula c_t = W_DKV h_t, what is the difference in nature between the matrix W_DKV and the vector c_t?

3. How does MLA's strategy differ from MQA's and GQA's?

4. Why does RoPE cause trouble for MLA, and how does DeepSeek solve it?

5. What does the NoPE (No Positional Encoding) idea actually mean?