Multi-Head Latent Attention: Low-Rank KV Compression, Decoupled RoPE, and Matrix Absorption
Multi-Head Latent Attention (MLA), introduced in DeepSeek-V2, addresses the KV cache bottleneck that has constrained long-context LLM inference. Rather than reducing the number of heads as in Grouped-Query Attention (GQA) or Multi-Query Attention (MQA), MLA compresses keys and values into a shared low-rank latent representation, caches that compressed form, and reconstructs full K and V matrices at use time. A decoupled rotary position embedding (RoPE) preserves positional information without preventing compression, and an "absorb" transformation folds the up-projection and output-projection matrices into the query weights so inference never materializes the decompressed K and V. This article walks through the mathematics of standard MHA, the MLA modifications, and the resulting inference economics.
Standard Multi-Head Attention and the KV Cache Problem
In standard Multi-Head Attention (MHA), a hidden state at position is projected to queries, keys, and values for each of heads:
where and is the head dimension. The attention score for head at step against all prior positions is
and the head output is . The concatenated output is then projected by .
During autoregressive generation, each layer must store and for all and all heads . For a model with layers, heads, head dimension , and context length , the KV cache size is floats — in total. DeepSeek-V2 () would require roughly 488 GB of KV cache under MHA, making long-context serving impractical.
MLA: Low-Rank Key-Value Joint Compression
MLA replaces the separate and projections with a single down-projection to a shared latent vector (where ), followed by up-projections per head:
Only is stored in the cache — a single vector of size per token per layer. For DeepSeek-V2, versus for full K/V, yielding a 32× reduction in cached elements per token (from 32,768 to 1,024 floats per layer per token). The authors report a 93.3% KV cache reduction compared to their 67B dense MHA baseline.
Queries are handled symmetrically with a separate down-projection and per-head up-projections , though the query latent is not cached since queries are only needed at the current step.
Decoupled Rotary Position Embedding
Standard RoPE applies a position-dependent rotation to queries and keys:
In MLA, if RoPE were applied after the up-projection, the rotated would depend on the full , breaking the ability to cache only . MLA therefore applies RoPE to a dedicated decoupled set of query and key projections that bypass the compression:
where and with typically much smaller than (DeepSeek-V2 uses ). The RoPE rotation is applied to these low-dimensional vectors, and the positional attention score component is computed separately:
The full attention score combines content and positional components:
Because is projected directly from (not from the compressed ), it can be RoPE-rotated without requiring decompression. Only and are cached, adding floats per token per layer — still a 57× reduction versus full MHA.
Matrix Absorption: Eliminating Decompression at Inference
The content attention score can be rewritten by substituting the up-projections:
Define . Then
During inference, is computed once per step. The matrix can be pre-computed and fused into the query projection:
so that the score becomes — a single matrix-vector product against the cached . The full K matrix is never materialized.
Similarly, the output aggregation can absorb the value up-projection and output projection. The head output is
The concatenated output is multiplied by . Defining (where selects the columns corresponding to head ), the final output is
All up-projections and the output projection are absorbed into modified query and output matrices. At inference time, the model only reads the compressed latents and from cache — no decompression step occurs.
Inference Economics
| Configuration | KV Cache per Token per Layer | 128K Context (60 layers) | |---------------|------------------------------|---------------------------| | MHA (DeepSeek 67B) | floats | ~488 GB | | GQA (8 groups) | floats | ~30 GB | | MLA (DeepSeek-V2) | floats | ~8.6 GB |
MLA achieves a 57× reduction versus MHA and a 3.5× reduction versus 8-group GQA. The compute cost of the absorb operations is negligible: the pre-computed and add one small matrix multiply per head per layer per token, which is dominated by the attention softmax and the linear projections that must run regardless.
Training Considerations
During training, MLA uses the same forward pass but caches the full and for gradient computation, since the absorb transformation is not applied until inference. The decoupled RoPE projections add a small parameter overhead (~0.5% of attention params). DeepSeek-V2 reports that MLA matches or exceeds MHA quality while enabling 128K context at a fraction of the inference memory.
Sources
- DeepSeek-AI, "DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model," arXiv:2405.04434, §2.1.2, Appendix D.2.
- Lior Sinai, "DeepSeek's Multi-Head Latent Attention," https://liorsinai.github.io/machine-learning/2025/02/22/mla.html
- Chris McCormick, "The Inner Workings of Multihead Latent Attention (MLA)," https://mccormickml.com/2025/04/26/inner-workings-of-mla
- Sebastian Raschka, "Multi-Head Latent Attention (MLA)," https://sebastianraschka.com/llms-from-scratch/ch04/05_mla
- Vizuara, "Decoding Multi-Head Latent Attention (Part 1): The KV Cache Compression," https://vizuara.substack.com/p/decoding-multi-head-latent-attention



