Multi-Head Latent Attention
An attention variant that compresses key-value cache storage into a low-rank latent space while keeping distinct query heads.
Multi-head latent attention (MLA) is an attention variant that stores a compact latent key-value representation and reconstructs per-head keys and values when needed, reducing cache size without collapsing to one shared head.
At a glance
Released
May 2024
Authors
DeepSeek-AI
Optimizes
- Kv Cache
- Memory Bandwidth
- Long Context Inference
What It Is
Multi-head latent attention (MLA) is an attention variant derived from multi-head attention. It keeps multiple query heads but projects keys and values into a shared low-rank latent space that is cached during autoregressive decoding.Why It Exists
MLA targets key-value cache size, memory bandwidth during autoregressive decoding, and the cost of serving long contexts when full per-head key-value tensors would otherwise dominate memory.How It Works
Keys and values are down-projected into a latent cache with rank r. During attention, those latent vectors are up-projected back to per-head key and value spaces. Queries remain head-specific, so each head attends against reconstructed key-value pairs derived from the shared latent cache.Compress key-value stateLatent key-value cache (rank r)Expand to per-head K/VkqqkqqVVReconstructed valuesReconstructed keysLatent cacheQueriesExpand to per-head K/V to kExpand to per-head K/V to kExpand to per-head K/V to VExpand to per-head K/V to VCompress key-value state to Latent key-value cache (rank r)Latent key-value cache (rank r) to Expand to per-head K/Vk to qk to qk to qk to q
Math Or Compute Schema
With multiple query heads and latent rank r, MLA caches compact latent vectors and reconstructs per-head keys and values through low-rank projections. The formulas below contrast full multi-head attention against MLA, which routes queries through a shared latent cache.- Query vectors for head i.
- Reconstructed key vectors from latent cache c.
- Reconstructed value vectors from latent cache c.
- Number of query heads.
- Latent rank of the compressed key-value cache.
- Key dimension per head after up-projection.
- Query head index.
- Shared latent key-value cache vector stored per token.
Compared To Nearby Modules
Compared with multi-head attention, MLA stores a compressed latent cache instead of full per-head key-value tensors. Compared with multi-query and grouped-query attention, MLA keeps distinct query heads while compressing key-value state through low-rank projection rather than only sharing heads.| Comparison dimension | Multi-Head Latent Attention | Multi-Head Attention | Multi-Query Attention | Grouped-Query Attention |
|---|---|---|---|---|
| Key-value representation | Low-rank latent key-value vectors (rank r) | H key heads and H value heads | 1 key head and 1 value head | G key heads and G value heads |
| Query-head flexibility | H distinct query heads with latent key-value reconstruction | H independent query and key-value head pairs | H query heads share one key-value pair | H distinct query heads grouped into G shared key-value pairs |
| Cache footprint per token | r-dimensional latent tensors (compressed key-value cache) | 2H tensors (full multi-head attention cache) | 2 tensors (single shared key-value cache) | 2G tensors (G keys plus G values) |