← Writing

DeepSeek-V2: MLA Explained

·2 min read

DeepSeek-V2: MLA Explained

MLA is a novel attention mechanism designed for Transformer-based Large Language Models, aiming to boost inference efficiency by massively compressing how Key-Value (KV) cache is stored and accessed during generation. Conventional Multi-Head Attention (MHA) keeps a heavy cache of keys and values per token, which limits batch sizes and sequence lengths for LLMs. MLA replaces this with a joint, low-rank compression of keys and values (and optionally queries), dramatically reducing memory footprint while maintaining or improving performance.

MLA also introduces decoupled Rotary Position Embedding (RoPE) to address the incompatibility between standard RoPE and low-rank compression.


MLA Full Formulas#

Below are the complete step-by-step formulas of MLA, as defined in the DeepSeek-V2 paper (Appendix C):

1. Query Compression

  • Compress the attention input for queries:

    cQt=WDQhtc_{Q_t} = W_{DQ} h_t

    where cQtc_{Q_t} is the compressed latent vector for queries, hth_t is the input at time tt, and WDQW_{DQ} is the down-projection matrix.

  • Up-project to produce query candidates:

    [qt,1C;…;qt,nhC]=qtC=WUQcQt[q^C_{t,1}; \ldots; q^C_{t,n_h}] = q^C_t = W_{UQ} c_{Q_t}
  • Apply Rotary Position Embedding (RoPE) to decoupled queries:

    [qt,1R;…;qt,nhR]=qtR=RoPE(WQRcQt)[q^R_{t,1}; \ldots; q^R_{t,n_h}] = q^R_t = \text{RoPE}(W_{QR} c_{Q_t})
  • Concatenate compressed and RoPE queries for each head:

    qt,i=[qt,iC;qt,iR]q_{t,i} = [q^C_{t,i}; q^R_{t,i}]

2. Key-Value Joint Compression

  • Compress the attention input for keys and values:

    cKVt=WDKVhtc_{KV_t} = W_{DKV} h_t
  • Up-project keys:

    [kt,1C;…;kt,nhC]=ktC=WUKcKVt[k^C_{t,1}; \ldots; k^C_{t,n_h}] = k^C_t = W_{UK} c_{KV_t}
  • Apply RoPE to the decoupled key:

    ktR=RoPE(WKRht)k^R_t = \text{RoPE}(W_{KR} h_t)
  • Concatenate compressed and RoPE keys for each head:

    kt,i=[kt,iC;ktR]k_{t,i} = [k^C_{t,i}; k^R_t]
  • Up-project values:

    [vt,1C;…;vt,nhC]=vtC=WUVcKVt[v^C_{t,1}; \ldots; v^C_{t,n_h}] = v^C_t = W_{UV} c_{KV_t}

3. Attention Calculation

  • For each head, compute attention output:

    ot,i=∑j=1tSoftmaxj(qt,iTkj,idh+dhR)vj,iCo_{t,i} = \sum_{j=1}^{t} \text{Softmax}_j\left( \frac{q_{t,i}^T k_{j,i}}{\sqrt{d_h + d^R_h}} \right) v^C_{j,i}

    where dhd_h is the per-head dimension, and dhRd^R_h is the decoupled per-head dimension for RoPE.

  • Concatenate all heads and project:

    ut=WO[ot,1;ot,2;…;ot,nh]u_t = W_O [o_{t,1}; o_{t,2}; \ldots; o_{t,n_h}]

    where WOW_O is the output projection matrix.

Caching Strategy

  • During inference, only compressed latent vectors (cKVtc_{KV_t}, ktRk^R_t) need to be cached, greatly reducing memory requirements compared to MHA.

Optimization Note

  • Due to distributive properties in matrix multiplication, some projection matrices (e.g., WUK,WUVW_{UK}, W_{UV}) can be merged with other projections during inference, avoiding redundant computation of keys and values for each query.

Summary Table of MLA vs Other Attention Mechanisms#

MechanismKV Cache Per Token (# elements)Performance
MHA2nhdhl2 n_h d_h lStrong
GQA2ngdhl2 n_g d_h lModerate
MQA2dhl2 d_h lWeak
MLA (Ours)(dc+dhR)l(d_c + d^R_h) l (≈2dhl≈ 2 d_h l)Stronger

Where:

  • nhn_h: # of attention heads
  • dhd_h: per-head dimension
  • ll: # of layers
  • ngn_g: # of groups (GQA)
  • dcd_c: compression dimension (MLA)
  • dhRd^R_h: decoupled query/key dimension (MLA)

For DeepSeek-V2, typically, dc=4dhd_c = 4 d_h, dhR=dh/2d^R_h = d_h / 2, making MLA's cache much leaner.

Source: [DeepSeek-V2 Paper](https://arxiv.org/pdf/2405.04434

Comments