Multi-Head Latent Attention: Low-Rank KV Compression, Decoupled RoPE, and Matrix Absorption

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 t

5 min
Multi-Head Latent Attention: Low-Rank KV Compression, Decoupled RoPE, and Matrix Absorption

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 htRdh_t \in \mathbb{R}^d at position tt is projected to queries, keys, and values for each of HH heads:

qt,i=WiQht,kt,i=WiKht,vt,i=WiVhtq_{t,i} = W_i^Q h_t, \quad k_{t,i} = W_i^K h_t, \quad v_{t,i} = W_i^V h_t

where WiQ,WiK,WiVRdh×dW_i^Q, W_i^K, W_i^V \in \mathbb{R}^{d_h \times d} and dh=d/Hd_h = d / H is the head dimension. The attention score for head ii at step tt against all prior positions sts \le t is

αt,s,i=qt,iks,idh\alpha_{t,s,i} = \frac{q_{t,i}^\top k_{s,i}}{\sqrt{d_h}}

and the head output is ot,i=stsoftmaxs(αt,s,i)vs,io_{t,i} = \sum_{s \le t} \text{softmax}_s(\alpha_{t,s,i}) v_{s,i}. The concatenated output ot=[ot,1;;ot,H]o_t = [o_{t,1}; \dots; o_{t,H}] is then projected by WORd×dW^O \in \mathbb{R}^{d \times d}.

During autoregressive generation, each layer must store ks,ik_{s,i} and vs,iv_{s,i} for all sts \le t and all heads ii. For a model with LL layers, HH heads, head dimension dhd_h, and context length TT, the KV cache size is 2LHdhT2 L H d_h T floats — 2LdT2 L d T in total. DeepSeek-V2 (L=60,H=128,dh=128,T=128KL=60, H=128, d_h=128, T=128\text{K}) 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 WiKW_i^K and WiVW_i^V projections with a single down-projection to a shared latent vector ctKVRdcc_t^{KV} \in \mathbb{R}^{d_c} (where dcHdhd_c \ll H d_h), followed by up-projections per head:

ctKV=WDKVht,WDKVRdc×dc_t^{KV} = W^{DKV} h_t, \quad W^{DKV} \in \mathbb{R}^{d_c \times d}

kt,i=WiUKctKV,vt,i=WiUVctKV,WiUK,WiUVRdh×dck_{t,i} = W_i^{UK} c_t^{KV}, \quad v_{t,i} = W_i^{UV} c_t^{KV}, \quad W_i^{UK}, W_i^{UV} \in \mathbb{R}^{d_h \times d_c}

Only ctKVc_t^{KV} is stored in the cache — a single vector of size dcd_c per token per layer. For DeepSeek-V2, dc=512d_c = 512 versus Hdh=16,384H d_h = 16,384 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 WDQRdc×dW^{DQ} \in \mathbb{R}^{d_c' \times d} and per-head up-projections WiUQRdh×dcW_i^{UQ} \in \mathbb{R}^{d_h \times d_c'}, though the query latent ctQc_t^Q 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:

qt,iRoPE(qt,i,t),kt,iRoPE(kt,i,t)q_{t,i} \leftarrow \text{RoPE}(q_{t,i}, t), \quad k_{t,i} \leftarrow \text{RoPE}(k_{t,i}, t)

In MLA, if RoPE were applied after the up-projection, the rotated kt,ik_{t,i} would depend on the full WiUKctKVW_i^{UK} c_t^{KV}, breaking the ability to cache only ctKVc_t^{KV}. MLA therefore applies RoPE to a dedicated decoupled set of query and key projections that bypass the compression:

qt,iR=WiQRht,ktKR=WKVRhtq_{t,i}^R = W_i^{QR} h_t, \quad k_t^{KR} = W^{KV_R} h_t

where WiQRRdR×dW_i^{QR} \in \mathbb{R}^{d_R \times d} and WKVRRdR×dW^{KV_R} \in \mathbb{R}^{d_R \times d} with dRd_R typically much smaller than dhd_h (DeepSeek-V2 uses dR=64d_R=64). The RoPE rotation is applied to these low-dimensional vectors, and the positional attention score component is computed separately:

βt,s,i=(qt,iR)RoPE(ksKR,st)dR\beta_{t,s,i} = \frac{(q_{t,i}^R)^\top \text{RoPE}(k_s^{KR}, s-t)}{\sqrt{d_R}}

The full attention score combines content and positional components:

αt,s,i=qt,iks,idh+(qt,iR)RoPE(ksKR,st)dR\alpha_{t,s,i} = \frac{q_{t,i}^\top k_{s,i}}{\sqrt{d_h}} + \frac{(q_{t,i}^R)^\top \text{RoPE}(k_s^{KR}, s-t)}{\sqrt{d_R}}

Because ksKRk_s^{KR} is projected directly from hsh_s (not from the compressed csKVc_s^{KV}), it can be RoPE-rotated without requiring decompression. Only csKVc_s^{KV} and ksKRk_s^{KR} are cached, adding dc+dR=576d_c + d_R = 576 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:

qt,iks,i=(WiUQctQ)(WiUKcsKV)=(ctQ)(WiUQWiUK)csKVq_{t,i}^\top k_{s,i} = (W_i^{UQ} c_t^Q)^\top (W_i^{UK} c_s^{KV}) = (c_t^Q)^\top (W_i^{UQ \top} W_i^{UK}) c_s^{KV}

Define W^iUK=WiUQWiUKRdc×dc\hat{W}_i^{UK} = W_i^{UQ \top} W_i^{UK} \in \mathbb{R}^{d_c \times d_c}. Then

αt,s,icontent=(ctQ)W^iUKcsKVdh\alpha_{t,s,i}^{\text{content}} = \frac{(c_t^Q)^\top \hat{W}_i^{UK} c_s^{KV}}{\sqrt{d_h}}

During inference, ctQc_t^Q is computed once per step. The matrix W^iUK\hat{W}_i^{UK} can be pre-computed and fused into the query projection:

W~iQ=W^iUKWiUQRdc×dc\tilde{W}_i^Q = \hat{W}_i^{UK \top} W_i^{UQ \top} \in \mathbb{R}^{d_c \times d_c'}

so that the score becomes (ctQ)W~iQcsKV/dh(c_t^Q)^\top \tilde{W}_i^Q c_s^{KV} / \sqrt{d_h} — a single matrix-vector product against the cached csKVc_s^{KV}. The full K matrix is never materialized.

Similarly, the output aggregation can absorb the value up-projection and output projection. The head output is

ot,i=ssoftmaxs(αt,s,i)vs,i=ssoftmaxs(αt,s,i)WiUVcsKVo_{t,i} = \sum_s \text{softmax}_s(\alpha_{t,s,i}) v_{s,i} = \sum_s \text{softmax}_s(\alpha_{t,s,i}) W_i^{UV} c_s^{KV}

The concatenated output ot=[ot,1;;ot,H]o_t = [o_{t,1}; \dots; o_{t,H}] is multiplied by WOW^O. Defining WiOUV=W:,iOWiUVRd×dcW_i^{OUV} = W^O_{:, i} W_i^{UV} \in \mathbb{R}^{d \times d_c} (where W:,iOW^O_{:, i} selects the columns corresponding to head ii), the final output is

yt=issoftmaxs(αt,s,i)WiOUVcsKVy_t = \sum_i \sum_s \text{softmax}_s(\alpha_{t,s,i}) W_i^{OUV} c_s^{KV}

All up-projections WiUK,WiUVW_i^{UK}, W_i^{UV} and the output projection WOW^O are absorbed into modified query and output matrices. At inference time, the model only reads the compressed latents csKVc_s^{KV} and ksKRk_s^{KR} from cache — no decompression step occurs.

Inference Economics

| Configuration | KV Cache per Token per Layer | 128K Context (60 layers) | |---------------|------------------------------|---------------------------| | MHA (DeepSeek 67B) | 2Hdh=32,7682 H d_h = 32,768 floats | ~488 GB | | GQA (8 groups) | 2×8×128=2,0482 \times 8 \times 128 = 2,048 floats | ~30 GB | | MLA (DeepSeek-V2) | dc+dR=576d_c + d_R = 576 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 W~iQ\tilde{W}_i^Q and WiOUVW_i^{OUV} 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 kt,ik_{t,i} and vt,iv_{t,i} 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

Written by

More to read

  • Agent Memory Frameworks in Production: Comparing Mem0, Letta, Zep Graphiti, and Cognee — Architecture, Entity Extraction, Temporal Graph Indexing, and Serving Economics

    Large language model agents deployed in production environments face a fundamental architectural bottleneck: LLM context windows are stateless, ephemeral, and computationally expensive. While context window capacities have expanded to 1M+ tokens in modern frontier models, naive context stuffing (re-injecting unpruned conversational history on every turn) creates severe operational failure modes: quadratic attention compute overhead, high latency, rapid KV cache invalidation, and severe context d

    1 min
  • Sentence Transformers 6.0 Adds MultiVectorEncoder for ColBERT Late-Interaction Training

    Hugging Face has released Sentence Transformers v6.0, adding native architecture and training workflows for multi-vector late-interaction retrieval models. The update introduces MultiVectorEncoder, bringing ColBERT-style token-level representations directly into the library alongside existing dense embedding, sparse embedding, and cross-encoder reranker classes. While traditional dense retrieval compresses an entire document into a single fixed-dimension vector, multi-vector models preserve ind

    1 min
  • Amazon Acquires DuckLabs to Integrate DuckDB into AWS Analytics and AI Agent Workflows

    Amazon has entered into a definitive agreement to acquire DuckLabs, the Amsterdam-based company behind the open-source columnar database DuckDB. The acquisition brings the DuckLabs development team into Amazon Web Services (AWS), where they will operate as a wholly owned subsidiary starting in early September. Financial terms of the transaction were not disclosed. DuckDB creators and DuckLabs co-founders Hannes Mühleisen and Mark Raasveldt will continue leading the team from Amsterdam, maintain

    1 min