# Multi-head latent attention explained: how MLA shrinks the KV cache and why RoPE needs a detour

Long contexts are limited by the KV cache, the memory that stores past keys and values. Multi-head latent attention caches one small latent vector per token instead. This article works through the cache maths, the absorption trick and decoupled RoPE, and shows how current kernels quantise the latent further.

When a language model generates text, it keeps the keys and values of every earlier token in memory so it does not recompute them at each step. This store is the KV cache, and for long contexts it often uses more memory than the model's weights. It also limits speed, because each new token must read the whole cache from memory.

Multi-head latent attention (MLA), introduced by DeepSeek in its DeepSeek-V2 paper in 2024, attacks this problem at its root. Instead of caching full keys and values for every attention head, it caches one small latent vector per token and rebuilds what each head needs on the fly. The idea has since moved into serving kernels, and the newest work quantises that latent vector to 8 or even 4 bits.

This article explains how MLA works, computes the cache sizes against standard alternatives, shows the algebra trick that makes it fast, and explains why positional encoding needs special handling.

## What happened

### DeepSeek releases FlashMLA kernels with FP8 and FP4 latent caches (10 and 30 September 2026)

DeepSeek's FlashMLA repository, its library of attention kernels, released kernels for DeepSeek-V4.1 on 10 September 2026, covering prefill and decoding with FP8 or FP4 KV caches. A 30 September release added kernels for Huawei Ascend 950 NPUs and changed the FP8 and FP4 cache formats, while dropping support for Hopper GPUs and earlier models, including DeepSeek V3, V3.2 and V4.0. The README documents the cache layout per token for V4.1: 528 bytes in the FP8 format and 288 bytes in the FP4 format, for a head dimension of 512. It reports decode kernels reaching up to 1,024 TFLOPS on an NVIDIA B200.

### Research targets 4-bit latent caches (November 2026)

New papers on arXiv in November 2026 focus on the same target. Titles include "NVFP4-MLA: Unified FP4 KV-Cache Storage and Attention Computation for Multi-Head Latent Attention" and "AscMLA: Unlocking FP4 Efficiency for Long-Context MLA Decoding through Asymmetric Scaling". On the serving side, a vLLM pull request titled "FlashMLA mega attention and the NVFP4 compressed KV cache" targets DeepSeek-V4.1. The direction is clear: once MLA shrinks the number of cached values, the next saving comes from storing each value in fewer bits.

## The KV cache problem

In standard multi-head attention (MHA), each layer has n_h heads, each with key and value vectors of size d_h. For every token, every layer caches 2 × n_h × d_h numbers. Take the attention shape described in the DeepSeek-V2 paper: 128 heads of dimension 128, over 60 layers.

- Per token, per layer: 2 × 128 × 128 = 32,768 numbers, or 65,536 bytes in BF16.
- Per token, all layers: 65,536 × 60 = 3.93 MB.
- For a 128K-token context (131,072 tokens): about 515 GB, for a single sequence.

That is clearly impractical. Two older ideas reduce it by sharing heads. Multi-query attention (MQA) uses one key and value head for all query heads. Grouped-query attention (GQA) uses a few shared heads, for example 8. Both save memory but give each query head less distinct information, which can cost quality.

A useful analogy is a library's photocopying policy. MHA keeps a separate copy of every page for every reader. MQA keeps one copy for everyone. MLA keeps a compact summary of each page and has each reader expand it into the version they need, using their own decoder.

## How MLA works

### Compress, cache, expand

For each token with hidden state h, MLA computes a single compressed latent vector:

c = W_DKV · h

with dimension d_c much smaller than n_h × d_h. Only c is cached. When attention runs, each head's key and value are recovered by up-projections:

k_i = W_UK,i · c, v_i = W_UV,i · c

In DeepSeek-V2, d_c = 512, against 128 × 128 = 16,384 for the full set of keys alone. The model learns W_DKV, W_UK and W_UV during training, so the latent is shaped to hold what the heads need. Queries can also be compressed with a similar low-rank projection to save activation memory in training, but that does not affect the cache.

### The absorption trick

Expanding the latent into full keys for every cached token at every step would cost a lot of computation. MLA avoids this with associativity. The attention score between a query q_i and a key k_i is

q_iᵀ k_i = q_iᵀ (W_UK,i c) = (W_UK,iᵀ q_i)ᵀ c

The matrix W_UK,i can be applied once to the query instead of to every cached latent. The model then computes scores directly against the cached c. The same move works on the value side, where W_UV,i can be folded into the output projection. At inference the keys and values are never materialised; attention runs in the latent space.

The result is that, during decoding, MLA behaves like multi-query attention with a single shared key and value per token, of dimension around 512, while keeping per-head expressiveness through the absorbed projections. FlashMLA's sparse kernel interface reflects this: its key-value tensor has a single head, and its value dimension is 512.

The code below confirms the identity numerically:

```python
import numpy as np
rng = np.random.default_rng(0)
d, d_c, d_h = 256, 32, 16
W_dkv = rng.normal(size=(d_c, d))      # compress
W_uk = rng.normal(size=(d_h, d_c))     # up-project to one head's key
W_q = rng.normal(size=(d_h, d))
h_query, h_past = rng.normal(size=d), rng.normal(size=d)

c = W_dkv @ h_past                     # this is all that is cached
k = W_uk @ c
q = W_q @ h_query
print(np.allclose(q @ k, (W_uk.T @ q) @ c))   # True: score computed from the latent
```

## Why RoPE needs a detour

Most modern models use rotary position embedding (RoPE), which rotates queries and keys by angles that depend on their positions. The attention score then depends on the relative distance between tokens. The rotation sits between the query and the key:

q_iᵀ R(t − s) k_i = q_iᵀ R(t − s) W_UK,i c

Now the matrix between q and c includes R(t − s), which changes with every pair of positions. W_UK,i can no longer be absorbed into the query once, because the combined matrix differs for every cached token. Absorption breaks.

DeepSeek's solution is decoupled RoPE. Each token's key gets two parts: a content part derived from the latent c with no rotation, which can be absorbed, and a small extra part of dimension d_R that carries the position and is rotated with RoPE. Queries get a matching rotated part. The score is the sum of the two dot products. Only the small rotated key part, shared across heads, must be cached in addition to c. In DeepSeek-V2, d_R = 64.

So the cache per token per layer is d_c + d_R = 512 + 64 = 576 numbers. The DeepSeek-V2 paper describes this as equal to GQA with only 2.25 groups, which matches the arithmetic: 576 ÷ (2 × 128) = 2.25. For DeepSeek-V4.1, the FlashMLA README describes a head dimension of 512 in which 64 RoPE dimensions are stored together with the rest and quantised like them.

## The numbers side by side

Using 128 heads of dimension 128, 60 layers and BF16 values:

| Attention type | Numbers cached per token per layer | Bytes per token, all layers | 128K-token context | Reduction vs MHA |
|---|---|---|---|---|
| MHA | 32,768 | 3.93 MB | 515 GB | 1× |
| GQA, 8 groups | 2,048 | 246 KB | 32.2 GB | 16× |
| MLA (512 + 64) | 576 | 69 KB | 9.06 GB | 57× |
| MQA | 256 | 31 KB | 4.03 GB | 128× |

MLA's cache sits between GQA and MQA in size. The DeepSeek-V2 paper reports that MLA nonetheless achieves better quality than standard MHA in its comparisons, which is the main reason the design spread: it is smaller than GQA-8 without the quality cost of MQA.

### Quantising the latent

The latest step stores the latent in fewer bits. FlashMLA's documented formats for DeepSeek-V4.1 are:

- **FP8:** 512 bytes of FP8 E4M3 values for the 512 dimensions, plus 16 bytes of E8M0 scales, one per 32 values: 528 bytes per token.
- **FP4:** 256 bytes holding 512 E2M1 values, two per byte, plus 32 bytes of FP8 E4M3 scales, one per 16 values: 288 bytes per token.

Compared with a BF16 latent of 576 values (1,152 bytes), the FP8 layout is 46 percent of the size and the FP4 layout is about 25 percent. The FP4 layout uses the same block structure as NVIDIA's NVFP4 format: 16 values per block with an FP8 scale.

## What it means in practice

For serving, MLA changes the bottleneck. A smaller cache means more concurrent sequences fit on one GPU, longer contexts are affordable, and each decoding step reads fewer bytes. Because absorbed MLA decoding behaves like MQA with a wide head, it has higher arithmetic intensity than MHA decoding: many query heads reuse the same cached latent. That is why specialised kernels such as FlashMLA can reach high compute utilisation in decoding, where standard attention is usually memory-bound.

There are costs. MLA must be designed into the model before pre-training; you cannot switch an existing GQA model to MLA without further training. Kernels are specialised, and FlashMLA's September release shows how quickly formats and hardware support can change. Tensor parallelism is also awkward, because the single shared latent cannot be split across GPUs by head as easily as MHA's per-head caches, which is why research such as tensor-parallel latent attention exists.

The [KV cache simulation](/ai-simulations/kv-cache) shows how cache size grows with context and batch, and the [attention simulation](/ai-simulations/attention) shows how scores are computed. For rotary embeddings, see the [positional encoding simulation](/ai-simulations/positional-encoding).

## What to do now

1. **Compute your cache budget.** Multiply cached numbers per token per layer by layers, bytes per value, context length and concurrent sequences, and compare it with GPU memory after weights.
2. **Check the attention type of candidate models.** When choosing an open model for long contexts, note whether it uses MHA, GQA or MLA, because the cache cost can differ by an order of magnitude.
3. **Use engines with native MLA kernels.** Serve MLA models with an engine that implements absorbed attention, or you lose much of the benefit by materialising keys and values.
4. **Test FP8 latent caches first.** Where kernels support it, an FP8 latent cache halves memory with a small, measurable accuracy cost; evaluate long-context tasks before moving to FP4.
5. **Pin kernel and cache format versions.** Cache formats change between kernel releases, so record the exact version used with each deployment.
6. **Evaluate at your real context length.** Quantisation errors in the cache accumulate over long sequences, so test at the lengths you serve, not only on short prompts.
7. **Plan parallelism around the latent.** For multi-GPU serving, check how your engine shards MLA, since head-wise tensor parallelism does not apply directly to a shared latent.

## Risks and open questions

**Hardware lock-in.** The fastest MLA kernels target specific GPU generations, and FlashMLA's latest release dropped Hopper support for new formats. Teams on older hardware may need older versions.

**FP4 cache accuracy is still being studied.** The November 2026 papers on FP4 latent caches exist because 4-bit caches introduce errors that need careful scaling; long-context quality under FP4 is not yet settled.

**Retrofitting is costly.** Converting existing models to MLA requires additional training, and published results for retrofitted models are fewer than for models trained with MLA from the start.

**Sparse attention changes the picture.** DeepSeek's newer kernels combine MLA with token-level sparse attention, which reads only selected cached tokens. The interaction between sparsity and quantised latents is a further variable to evaluate.

**Watch next:** whether other model families adopt latent attention, how FP4 latent caches perform on long-context benchmarks, and how serving engines handle MLA across many GPUs.

## Frequently asked questions

### What does MLA cache instead of keys and values?

It caches one compressed latent vector per token per layer, plus a small rotated key part for position information. Keys and values for each head are recovered from the latent through learned projections, or, with absorption, never explicitly rebuilt at all.

### Is MLA the same as low-rank compression of the KV cache?

It is a learned low-rank factorisation, but trained into the model from the start rather than applied afterwards. Because the projections are learned jointly with the rest of the model, the latent holds what the heads actually need.

### Why can't RoPE be applied to the latent directly?

RoPE inserts a position-dependent rotation between query and key. That rotation changes for every pair of positions, so the key up-projection cannot be folded into the query once, and absorption fails. Decoupled RoPE puts position information in a separate small key part.

### How much smaller is MLA's cache than standard attention?

With the DeepSeek-V2 shape, 128 heads of dimension 128, MLA caches 576 numbers per token per layer against 32,768 for MHA, about 57 times fewer. Quantising the latent to FP8 or FP4 shrinks it further.

### Can I use MLA with an existing model?

Not without further training. MLA changes the attention projections, so a model must be trained or substantially re-trained with it, and serving needs kernels that support it.

## Summary

Multi-head latent attention replaces per-head keys and values in the KV cache with one compressed latent vector per token, recovered for each head through learned projections. The absorption trick folds those projections into the query and output, so attention runs directly on the latent and decoding behaves like multi-query attention with a wide head. RoPE breaks absorption, so MLA adds a small decoupled key part for positions, giving 576 cached numbers per token per layer in DeepSeek-V2 against 32,768 for standard attention. Current kernels push further by storing the latent in FP8 or FP4, at 528 and 288 bytes per token in FlashMLA's DeepSeek-V4.1 formats. For more, see the [KV cache](/ai-simulations/kv-cache), [attention mechanism](/tools/attention-mechanism) and [FlashAttention](/tools/flashattention) pages and the [inside an LLM topic](/topics/generative-ai-llms/inside-an-llm).

## Sources

- [arXiv: DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model](https://arxiv.org/abs/2405.04434)
- [GitHub: DeepSeek FlashMLA README](https://github.com/deepseek-ai/FlashMLA)
- [arXiv: Fast Transformer Decoding: One Write-Head is All You Need](https://arxiv.org/abs/1911.02150)
- [arXiv: GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints](https://arxiv.org/abs/2305.13245)
- [arXiv: RoFormer: Enhanced Transformer with Rotary Position Embedding](https://arxiv.org/abs/2104.09864)
- [arXiv: NVFP4-MLA: Unified FP4 KV-Cache Storage and Attention Computation for Multi-Head Latent Attention](https://arxiv.org/abs/2611.18972)
- [arXiv: AscMLA: Unlocking FP4 Efficiency for Long-Context MLA Decoding through Asymmetric Scaling](https://arxiv.org/abs/2611.26106)
- [GitHub: vLLM pull request 56935, FlashMLA mega attention and the NVFP4 compressed KV cache](https://github.com/vllm-project/vllm/pull/56935)
- [arXiv: TPLA: Tensor Parallel Latent Attention for Efficient Disaggregated Prefill and Decode Inference](https://arxiv.org/abs/2508.15881)
