Pratham Patel
· 23 min read

Mathematical Prerequisites for DeepSeek-V2

Building the linear algebra foundations for Multi-head Latent Attention — matrix-vector multiplication, transpose, associativity, non-commutativity, the bilinear identity, linearity over sums, low-rank factorization, and the dot product of concatenated vectors — all derived step by step with one consistent 3-dimensional example.

The DeepSeek-V2 blog derives Multi-head Latent Attention (MLA), an attention mechanism that compresses keys and values into a shared low-rank latent vector and then recovers them through up-projection matrices. The central computational trick — matrix absorption — rewrites the attention formula so that keys and values are never explicitly computed for cached tokens. Every step of that trick relies on basic linear algebra identities: the transpose, associativity, non-commutativity, and linearity of matrix multiplication.

These identities were not needed in any previous blog, so none of our earlier prerequisite posts derived them. This post builds every one from scratch. By the end, you will have all the tools required to follow the matrix absorption derivation and the low-rank compression argument in the DeepSeek-V2 blog.

The dot product was fully derived in Mathematical Prerequisites for the Attention Series. We will use it without re-deriving it.


The Running Example

We work with a 3-dimensional vector and a 2×32 \times 3 matrix throughout. These dimensions are small enough to compute by hand but large enough to show the structure of every operation.

The vector:

x=(213)\mathbf{x} = \begin{pmatrix} 2 \\ -1 \\ 3 \end{pmatrix}

The matrix:

A=(102314)A = \begin{pmatrix} 1 & 0 & -2 \\ 3 & 1 & 4 \end{pmatrix}

In the DeepSeek-V2 blog, x\mathbf{x} plays the role of the hidden state ht\mathbf{h}_t (512-dimensional in practice), and AA plays the role of a projection matrix like WDKVW^{DKV} that compresses the hidden state into a smaller latent vector. Here, AA compresses from 3 dimensions to 2 — in the blog, it compresses from 512 to 128.


1. Matrix-Vector Multiplication

In MLA, every projection — from hidden state to latent vector, from latent vector to keys, from latent vector to values — is a matrix-vector multiplication. We need to know exactly what this operation does and what dimensions it produces.

Matrix-vector multiplication takes a matrix ARm×nA \in \mathbb{R}^{m \times n} and a vector xRn\mathbf{x} \in \mathbb{R}^n and produces a new vector y=AxRm\mathbf{y} = A\mathbf{x} \in \mathbb{R}^m. Each entry of y\mathbf{y} is the dot product of one row of AA with x\mathbf{x}:

yi=j=1nAijxj\boxed{y_i = \sum_{j=1}^{n} A_{ij} \, x_j}

The matrix has mm rows and nn columns. The vector has nn entries. The number of columns of the matrix must equal the number of entries in the vector — this is the dimension compatibility rule. The result has mm entries, one per row of the matrix.

Numerical check

AA is 2×32 \times 3 and x\mathbf{x} is 3×13 \times 1. The inner dimensions match (both 3), so the product is defined and produces a 2×12 \times 1 vector.

Row 1 of AA is (1,0,2)(1, 0, -2). Its dot product with x=(2,1,3)\mathbf{x} = (2, -1, 3) is:

y1=12+0(1)+(2)3=2+06=4y_1 = 1 \cdot 2 + 0 \cdot (-1) + (-2) \cdot 3 = 2 + 0 - 6 = -4

Row 2 of AA is (3,1,4)(3, 1, 4):

y2=32+1(1)+43=61+12=17y_2 = 3 \cdot 2 + 1 \cdot (-1) + 4 \cdot 3 = 6 - 1 + 12 = 17

So:

Ax=(102314)(213)=(417)A\mathbf{x} = \begin{pmatrix} 1 & 0 & -2 \\ 3 & 1 & 4 \end{pmatrix} \begin{pmatrix} 2 \\ -1 \\ 3 \end{pmatrix} = \begin{pmatrix} -4 \\ 17 \end{pmatrix}

We went from a 3-dimensional vector to a 2-dimensional vector. In MLA, this is exactly what the down-projection WDKVW^{DKV} does: it takes the 512-dimensional hidden state and produces a 128-dimensional latent vector.


2. Matrix Transpose

The transpose appears constantly in the DeepSeek-V2 derivations — in attention scores (qTk\mathbf{q}^T \mathbf{k}), in the matrix absorption trick ((WUK)T(W^{UK})^T), and in the key identity that makes absorption work.

The transpose of a matrix ARm×nA \in \mathbb{R}^{m \times n} is the matrix ATRn×mA^T \in \mathbb{R}^{n \times m} obtained by swapping rows and columns:

(AT)ij=Aji\boxed{(A^T)_{ij} = A_{ji}}

Row ii of ATA^T is column ii of AA. Column jj of ATA^T is row jj of AA. The shape flips from m×nm \times n to n×mn \times m.

Numerical check

A=(102314)AT=(130124)A = \begin{pmatrix} 1 & 0 & -2 \\ 3 & 1 & 4 \end{pmatrix} \quad \Longrightarrow \quad A^T = \begin{pmatrix} 1 & 3 \\ 0 & 1 \\ -2 & 4 \end{pmatrix}

AA is 2×32 \times 3. ATA^T is 3×23 \times 2. Entry (1,2)(1,2) of ATA^T is 33, which is entry (2,1)(2,1) of AA. Entry (3,1)(3,1) of ATA^T is 2-2, which is entry (1,3)(1,3) of AA. Every entry checks out.

Transpose of a vector

A column vector xRn\mathbf{x} \in \mathbb{R}^n is a n×1n \times 1 matrix. Its transpose xT\mathbf{x}^T is a 1×n1 \times n row vector. This is why the attention score qTk\mathbf{q}^T \mathbf{k} is a scalar: qT\mathbf{q}^T is 1×d1 \times d and k\mathbf{k} is d×1d \times 1, so the product is 1×11 \times 1.

The dot product as a matrix product

In the attention blogs, we write the dot product as qTk\mathbf{q}^T \mathbf{k}. This is exactly the matrix product of a 1×d1 \times d row vector with a d×1d \times 1 column vector:

qTk=i=1dqiki\mathbf{q}^T \mathbf{k} = \sum_{i=1}^{d} q_i k_i

This is the same dot product formula from the attention prerequisites, but written in matrix notation. The two notations are interchangeable.


3. Matrix-Matrix Multiplication

Before we can derive associativity, we need the general rule for multiplying two matrices.

Given ARm×nA \in \mathbb{R}^{m \times n} and BRn×pB \in \mathbb{R}^{n \times p}, their product C=ABRm×pC = AB \in \mathbb{R}^{m \times p} has entries:

Cij=k=1nAikBkj\boxed{C_{ij} = \sum_{k=1}^{n} A_{ik} \, B_{kj}}

Each entry CijC_{ij} is the dot product of row ii of AA with column jj of BB. The inner dimensions must match: AA has nn columns, BB has nn rows.

Numerical check

We need a second matrix to multiply with ATA^T. Let us define:

B=(5123)B = \begin{pmatrix} 5 & -1 \\ 2 & 3 \end{pmatrix}

Then ATBA^T B has ATR3×2A^T \in \mathbb{R}^{3 \times 2} and BR2×2B \in \mathbb{R}^{2 \times 2}, so ATBR3×2A^T B \in \mathbb{R}^{3 \times 2}.

Entry (1,1)(1,1): row 1 of ATA^T is (1,3)(1, 3), column 1 of BB is (5,2)(5, 2). Dot product: 15+32=5+6=111 \cdot 5 + 3 \cdot 2 = 5 + 6 = 11.

Entry (1,2)(1,2): row 1 of ATA^T is (1,3)(1, 3), column 2 of BB is (1,3)(-1, 3). Dot product: 1(1)+33=1+9=81 \cdot (-1) + 3 \cdot 3 = -1 + 9 = 8.

Entry (2,1)(2,1): row 2 of ATA^T is (0,1)(0, 1), column 1 of BB is (5,2)(5, 2). Dot product: 05+12=20 \cdot 5 + 1 \cdot 2 = 2.

Entry (2,2)(2,2): row 2 of ATA^T is (0,1)(0, 1), column 2 of BB is (1,3)(-1, 3). Dot product: 0(1)+13=30 \cdot (-1) + 1 \cdot 3 = 3.

Entry (3,1)(3,1): row 3 of ATA^T is (2,4)(-2, 4), column 1 of BB is (5,2)(5, 2). Dot product: (2)5+42=10+8=2(-2) \cdot 5 + 4 \cdot 2 = -10 + 8 = -2.

Entry (3,2)(3,2): row 3 of ATA^T is (2,4)(-2, 4), column 2 of BB is (1,3)(-1, 3). Dot product: (2)(1)+43=2+12=14(-2) \cdot (-1) + 4 \cdot 3 = 2 + 12 = 14.

ATB=(11823214)A^T B = \begin{pmatrix} 11 & 8 \\ 2 & 3 \\ -2 & 14 \end{pmatrix}

This is 3×23 \times 2, matching our dimension prediction. We will use this result when we verify associativity below.


4. Associativity of Matrix Multiplication

This is the property that makes the matrix absorption trick in MLA work. In the DeepSeek-V2 blog, we need to rewrite (WiUK)T(WiUQctQ)(W_i^{UK})^T (W_i^{UQ} \mathbf{c}_t^Q) as ((WiUK)TWiUQ)ctQ((W_i^{UK})^T W_i^{UQ}) \mathbf{c}_t^Q. This is only valid if matrix multiplication is associative.

Associativity states that for any matrices AA, BB, CC of compatible dimensions:

(AB)C=A(BC)\boxed{(AB)C = A(BC)}

We can group the multiplication in either order and get the same result. This means we can precompute ABAB as a single matrix and then multiply by CC, or we can first compute BCBC and then multiply by AA. The answer is identical.

Proof

Let ARm×nA \in \mathbb{R}^{m \times n}, BRn×pB \in \mathbb{R}^{n \times p}, CRp×qC \in \mathbb{R}^{p \times q}. Both (AB)C(AB)C and A(BC)A(BC) are in Rm×q\mathbb{R}^{m \times q}. We show that entry (i,j)(i, j) is the same on both sides.

Left side. (AB)C(AB)C means: first compute D=ABD = AB, where Dik=l=1nAilBlkD_{ik} = \sum_{l=1}^{n} A_{il} B_{lk}. Then compute (DC)ij=k=1pDikCkj(DC)_{ij} = \sum_{k=1}^{p} D_{ik} C_{kj}. Substituting:

((AB)C)ij=k=1p(l=1nAilBlk)Ckj((AB)C)_{ij} = \sum_{k=1}^{p} \left( \sum_{l=1}^{n} A_{il} B_{lk} \right) C_{kj}

We distribute CkjC_{kj} into the inner sum:

=k=1pl=1nAilBlkCkj= \sum_{k=1}^{p} \sum_{l=1}^{n} A_{il} B_{lk} C_{kj}

Right side. A(BC)A(BC) means: first compute E=BCE = BC, where Elj=k=1pBlkCkjE_{lj} = \sum_{k=1}^{p} B_{lk} C_{kj}. Then compute (AE)ij=l=1nAilElj(AE)_{ij} = \sum_{l=1}^{n} A_{il} E_{lj}. Substituting:

(A(BC))ij=l=1nAil(k=1pBlkCkj)=l=1nk=1pAilBlkCkj(A(BC))_{ij} = \sum_{l=1}^{n} A_{il} \left( \sum_{k=1}^{p} B_{lk} C_{kj} \right) = \sum_{l=1}^{n} \sum_{k=1}^{p} A_{il} B_{lk} C_{kj}

Both sides equal k=1pl=1nAilBlkCkj\sum_{k=1}^{p} \sum_{l=1}^{n} A_{il} B_{lk} C_{kj}. The double sum is a finite sum of real numbers, and the order of summation does not matter (this is commutativity of addition). The two expressions are identical. \square

Numerical check

We verify (ATB)x=AT(Bx)(A^T B) \mathbf{x} = A^T (B \mathbf{x}) using our running example. Here ATR3×2A^T \in \mathbb{R}^{3 \times 2}, BR2×2B \in \mathbb{R}^{2 \times 2}, and we need a 2-dimensional vector. Let us define:

z=(43)\mathbf{z} = \begin{pmatrix} 4 \\ -3 \end{pmatrix}

Left side: (ATB)z(A^T B) \mathbf{z}. We already computed ATB=(11823214)A^T B = \begin{pmatrix} 11 & 8 \\ 2 & 3 \\ -2 & 14 \end{pmatrix}. Multiply by z\mathbf{z}:

(ATB)z=(114+8(3)24+3(3)(2)4+14(3))=(442489842)=(20150)(A^T B) \mathbf{z} = \begin{pmatrix} 11 \cdot 4 + 8 \cdot (-3) \\ 2 \cdot 4 + 3 \cdot (-3) \\ (-2) \cdot 4 + 14 \cdot (-3) \end{pmatrix} = \begin{pmatrix} 44 - 24 \\ 8 - 9 \\ -8 - 42 \end{pmatrix} = \begin{pmatrix} 20 \\ -1 \\ -50 \end{pmatrix}

Right side: AT(Bz)A^T (B \mathbf{z}). First compute BzB\mathbf{z}:

Bz=(5123)(43)=(54+(1)(3)24+3(3))=(20+389)=(231)B \mathbf{z} = \begin{pmatrix} 5 & -1 \\ 2 & 3 \end{pmatrix} \begin{pmatrix} 4 \\ -3 \end{pmatrix} = \begin{pmatrix} 5 \cdot 4 + (-1)(-3) \\ 2 \cdot 4 + 3 \cdot (-3) \end{pmatrix} = \begin{pmatrix} 20 + 3 \\ 8 - 9 \end{pmatrix} = \begin{pmatrix} 23 \\ -1 \end{pmatrix}

Then multiply by ATA^T:

AT(Bz)=(130124)(231)=(123+3(1)023+1(1)(2)23+4(1))=(20150)A^T (B\mathbf{z}) = \begin{pmatrix} 1 & 3 \\ 0 & 1 \\ -2 & 4 \end{pmatrix} \begin{pmatrix} 23 \\ -1 \end{pmatrix} = \begin{pmatrix} 1 \cdot 23 + 3 \cdot (-1) \\ 0 \cdot 23 + 1 \cdot (-1) \\ (-2) \cdot 23 + 4 \cdot (-1) \end{pmatrix} = \begin{pmatrix} 20 \\ -1 \\ -50 \end{pmatrix}

Both sides give (20,1,50)(20, -1, -50). Associativity holds.

In MLA, this is exactly how the matrix absorption trick works. The attention score involves (WiUK)T(W_i^{UK})^T applied to the query, then dotted with the cached latent. By associativity, we can precompute W~iQ=(WiUK)TWiUQ\tilde{W}_i^Q = (W_i^{UK})^T W_i^{UQ} once, and then apply the combined matrix to each query — avoiding the per-token up-projection entirely.


5. Non-Commutativity of Matrix Multiplication

In the DeepSeek-V2 blog, the RoPE incompatibility problem rests on one fact: matrix multiplication does not commute. We cannot swap the order of multiplication and expect the same result. This is why RoPE (a rotation matrix applied after the up-projection) cannot be “pushed inside” the up-projection.

Non-commutativity means that in general, ABBAAB \neq BA. This is true even when both products are defined (i.e., when AA and BB are both square matrices of the same size).

Numerical check

We use two 2×22 \times 2 matrices to demonstrate. Let:

P=(1201),R=(0110)P = \begin{pmatrix} 1 & 2 \\ 0 & 1 \end{pmatrix}, \qquad R = \begin{pmatrix} 0 & -1 \\ 1 & 0 \end{pmatrix}

RR is a 90-degree rotation matrix (exactly the kind of operation RoPE performs). Let us compute both PRPR and RPRP.

PR=(10+211(1)+2000+110(1)+10)=(2110)PR = \begin{pmatrix} 1 \cdot 0 + 2 \cdot 1 & 1 \cdot (-1) + 2 \cdot 0 \\ 0 \cdot 0 + 1 \cdot 1 & 0 \cdot (-1) + 1 \cdot 0 \end{pmatrix} = \begin{pmatrix} 2 & -1 \\ 1 & 0 \end{pmatrix} RP=(01+(1)002+(1)111+0012+01)=(0112)RP = \begin{pmatrix} 0 \cdot 1 + (-1) \cdot 0 & 0 \cdot 2 + (-1) \cdot 1 \\ 1 \cdot 1 + 0 \cdot 0 & 1 \cdot 2 + 0 \cdot 1 \end{pmatrix} = \begin{pmatrix} 0 & -1 \\ 1 & 2 \end{pmatrix}

PRRPPR \neq RP. The (1,1)(1,1) entry is 2 versus 0. The (2,2)(2,2) entry is 0 versus 2. The order matters.

This is precisely the problem with RoPE and MLA. The up-projection WUKW^{UK} and the RoPE rotation RtR_t (which depends on position tt) do not commute. So Rt(WUKct)WUK(Rtct)R_t(W^{UK} \mathbf{c}_t) \neq W^{UK}(R_t \mathbf{c}_t) in general. We cannot apply RoPE to the cached latent vector and then up-project — we would get the wrong answer. That is why DeepSeek-V2 decouples RoPE into a separate set of queries and keys.


6. The Bilinear Identity (Transpose Trick)

This is the identity that enables the key absorption in Section 5.1 of the DeepSeek-V2 blog. The blog rewrites (qt,iC)TWiUKcjKV(\mathbf{q}^C_{t,i})^T W_i^{UK} \mathbf{c}_j^{KV} as ((WiUK)Tqt,iC)TcjKV((W_i^{UK})^T \mathbf{q}^C_{t,i})^T \mathbf{c}_j^{KV}. That is this identity.

For any vector aRm\mathbf{a} \in \mathbb{R}^m, matrix MRm×nM \in \mathbb{R}^{m \times n}, and vector bRn\mathbf{b} \in \mathbb{R}^n:

aTMb=(MTa)Tb\boxed{\mathbf{a}^T M \mathbf{b} = (M^T \mathbf{a})^T \mathbf{b}}

Derivation

Start with the left side. aTMb\mathbf{a}^T M \mathbf{b} is a scalar (a 1×11 \times 1 matrix). The transpose of a scalar is itself:

aTMb=(aTMb)T\mathbf{a}^T M \mathbf{b} = (\mathbf{a}^T M \mathbf{b})^T

Now we apply the transpose of a product rule. For any matrices XX and YY of compatible dimensions, (XY)T=YTXT(XY)^T = Y^T X^T. Let us prove this rule before using it.

Detour: Transpose of a Product

We claim (XY)T=YTXT(XY)^T = Y^T X^T for any XRm×nX \in \mathbb{R}^{m \times n} and YRn×pY \in \mathbb{R}^{n \times p}. Both sides are in Rp×m\mathbb{R}^{p \times m}. Entry (j,i)(j, i) of (XY)T(XY)^T equals entry (i,j)(i, j) of XYXY:

((XY)T)ji=(XY)ij=k=1nXikYkj((XY)^T)_{ji} = (XY)_{ij} = \sum_{k=1}^{n} X_{ik} Y_{kj}

Entry (j,i)(j, i) of YTXTY^T X^T:

(YTXT)ji=k=1n(YT)jk(XT)ki=k=1nYkjXik(Y^T X^T)_{ji} = \sum_{k=1}^{n} (Y^T)_{jk} (X^T)_{ki} = \sum_{k=1}^{n} Y_{kj} X_{ik}

Each term in the sum is XikYkjX_{ik} Y_{kj} (by commutativity of real number multiplication). So the two sums are identical. \square

Completing the derivation

We apply the transpose-of-a-product rule to aTMb\mathbf{a}^T M \mathbf{b}. Grouping as (aTM)b(\mathbf{a}^T M) \mathbf{b}, we have two factors: aTM\mathbf{a}^T M (a 1×n1 \times n row vector) and b\mathbf{b} (an n×1n \times 1 column vector). Their product is 1×11 \times 1. Taking the transpose:

((aTM)b)T=bT(aTM)T((\mathbf{a}^T M) \mathbf{b})^T = \mathbf{b}^T (\mathbf{a}^T M)^T

Now apply the transpose-of-a-product rule to aTM\mathbf{a}^T M:

(aTM)T=MT(aT)T=MTa(\mathbf{a}^T M)^T = M^T (\mathbf{a}^T)^T = M^T \mathbf{a}

Here we used the fact that (aT)T=a(\mathbf{a}^T)^T = \mathbf{a} — transposing twice returns the original.

Substituting back:

aTMb=bT(MTa)\mathbf{a}^T M \mathbf{b} = \mathbf{b}^T (M^T \mathbf{a})

But bT(MTa)\mathbf{b}^T (M^T \mathbf{a}) is the dot product of b\mathbf{b} and MTaM^T \mathbf{a}, which equals (MTa)Tb(M^T \mathbf{a})^T \mathbf{b} (dot products are commutative: uTv=vTu\mathbf{u}^T \mathbf{v} = \mathbf{v}^T \mathbf{u}). Therefore:

aTMb=(MTa)Tb\mathbf{a}^T M \mathbf{b} = (M^T \mathbf{a})^T \mathbf{b} \quad \square

Interpretation

The left side, aTMb\mathbf{a}^T M \mathbf{b}, says: “Apply MM to b\mathbf{b}, then dot with a\mathbf{a}.” The right side, (MTa)Tb(M^T \mathbf{a})^T \mathbf{b}, says: “Apply MTM^T to a\mathbf{a}, then dot with b\mathbf{b}.” The two give the same scalar. The matrix can be “moved” from one side to the other, at the cost of transposing it.

In MLA, this means the up-projection WiUKW_i^{UK} that would normally be applied to every cached key can instead be applied (transposed) to the single current query. We move the work from the TT-many cached tokens (expensive) to the one current token (cheap).

Numerical check

Let a=Ax=(4,17)T\mathbf{a} = A\mathbf{x} = (-4, 17)^T (computed in Section 1), M=B=(5123)M = B = \begin{pmatrix} 5 & -1 \\ 2 & 3 \end{pmatrix}, and b=z=(4,3)T\mathbf{b} = \mathbf{z} = (4, -3)^T.

Left side: aTMb\mathbf{a}^T M \mathbf{b}.

First, Mb=Bz=(23,1)TM \mathbf{b} = B \mathbf{z} = (23, -1)^T (computed in Section 4).

Then aT(Mb)=(4)23+17(1)=9217=109\mathbf{a}^T (M\mathbf{b}) = (-4) \cdot 23 + 17 \cdot (-1) = -92 - 17 = -109.

Right side: (MTa)Tb(M^T \mathbf{a})^T \mathbf{b}.

MT=BT=(5213)M^T = B^T = \begin{pmatrix} 5 & 2 \\ -1 & 3 \end{pmatrix}.

MTa=(5(4)+217(1)(4)+317)=(20+344+51)=(1455)M^T \mathbf{a} = \begin{pmatrix} 5 \cdot (-4) + 2 \cdot 17 \\ (-1)(-4) + 3 \cdot 17 \end{pmatrix} = \begin{pmatrix} -20 + 34 \\ 4 + 51 \end{pmatrix} = \begin{pmatrix} 14 \\ 55 \end{pmatrix}.

(MTa)Tb=144+55(3)=56165=109(M^T \mathbf{a})^T \mathbf{b} = 14 \cdot 4 + 55 \cdot (-3) = 56 - 165 = -109.

Both sides give 109-109. The identity holds.


7. Linearity of Matrix Multiplication

In Section 5.2 of the DeepSeek-V2 blog, we pull the value up-projection matrix WiUVW_i^{UV} out of a weighted sum: jαj(WiUVcj)=WiUVjαjcj\sum_j \alpha_j (W_i^{UV} \mathbf{c}_j) = W_i^{UV} \sum_j \alpha_j \mathbf{c}_j. This is linearity.

Linearity of matrix multiplication means that for any matrix WW, vectors x1,x2,,xT\mathbf{x}_1, \mathbf{x}_2, \ldots, \mathbf{x}_T of compatible dimension, and scalars α1,α2,,αT\alpha_1, \alpha_2, \ldots, \alpha_T:

W(j=1Tαjxj)=j=1Tαj(Wxj)\boxed{W \left( \sum_{j=1}^{T} \alpha_j \mathbf{x}_j \right) = \sum_{j=1}^{T} \alpha_j (W \mathbf{x}_j)}

Derivation

It suffices to prove two properties and then combine them.

Distributivity over addition. For any matrix WRm×nW \in \mathbb{R}^{m \times n} and vectors u,vRn\mathbf{u}, \mathbf{v} \in \mathbb{R}^n:

(W(u+v))i=k=1nWik(uk+vk)=k=1nWikuk+k=1nWikvk=(Wu)i+(Wv)i(W(\mathbf{u} + \mathbf{v}))_i = \sum_{k=1}^{n} W_{ik}(u_k + v_k) = \sum_{k=1}^{n} W_{ik} u_k + \sum_{k=1}^{n} W_{ik} v_k = (W\mathbf{u})_i + (W\mathbf{v})_i

The first equality uses the definition of matrix-vector multiplication. The second uses distributivity of real number multiplication over addition: a(b+c)=ab+aca(b + c) = ab + ac. Since this holds for every entry ii, we have W(u+v)=Wu+WvW(\mathbf{u} + \mathbf{v}) = W\mathbf{u} + W\mathbf{v}.

Compatibility with scalar multiplication. For any scalar α\alpha:

(W(αu))i=k=1nWik(αuk)=αk=1nWikuk=α(Wu)i(W(\alpha \mathbf{u}))_i = \sum_{k=1}^{n} W_{ik}(\alpha u_k) = \alpha \sum_{k=1}^{n} W_{ik} u_k = \alpha (W\mathbf{u})_i

The second equality uses the fact that α\alpha is a common factor in every term of the sum. So W(αu)=α(Wu)W(\alpha \mathbf{u}) = \alpha (W \mathbf{u}).

Combining. Apply these two rules repeatedly to the sum jαjxj\sum_j \alpha_j \mathbf{x}_j:

W ⁣(j=1Tαjxj)=j=1TW(αjxj)=j=1Tαj(Wxj)W\!\left(\sum_{j=1}^{T} \alpha_j \mathbf{x}_j\right) = \sum_{j=1}^{T} W(\alpha_j \mathbf{x}_j) = \sum_{j=1}^{T} \alpha_j (W \mathbf{x}_j) \quad \square

The first equality uses distributivity over addition (applied T1T - 1 times). The second uses compatibility with scalar multiplication (applied TT times).

Numerical check

Let W=A=(102314)W = A = \begin{pmatrix} 1 & 0 & -2 \\ 3 & 1 & 4 \end{pmatrix}. Define two 3-dimensional vectors:

x1=(213),x2=(101)\mathbf{x}_1 = \begin{pmatrix} 2 \\ -1 \\ 3 \end{pmatrix}, \qquad \mathbf{x}_2 = \begin{pmatrix} 1 \\ 0 \\ -1 \end{pmatrix}

with weights α1=0.6\alpha_1 = 0.6 and α2=0.4\alpha_2 = 0.4.

Left side: W(α1x1+α2x2)W(\alpha_1 \mathbf{x}_1 + \alpha_2 \mathbf{x}_2).

First, α1x1+α2x2=0.6(2,1,3)+0.4(1,0,1)=(1.2,0.6,1.8)+(0.4,0,0.4)=(1.6,0.6,1.4)\alpha_1 \mathbf{x}_1 + \alpha_2 \mathbf{x}_2 = 0.6 \cdot (2, -1, 3) + 0.4 \cdot (1, 0, -1) = (1.2, -0.6, 1.8) + (0.4, 0, -0.4) = (1.6, -0.6, 1.4).

Then W(1.6,0.6,1.4)TW \cdot (1.6, -0.6, 1.4)^T:

y1=11.6+0(0.6)+(2)1.4=1.62.8=1.2y_1 = 1 \cdot 1.6 + 0 \cdot (-0.6) + (-2) \cdot 1.4 = 1.6 - 2.8 = -1.2 y2=31.6+1(0.6)+41.4=4.80.6+5.6=9.8y_2 = 3 \cdot 1.6 + 1 \cdot (-0.6) + 4 \cdot 1.4 = 4.8 - 0.6 + 5.6 = 9.8

Left side: (1.2,9.8)(-1.2, 9.8).

Right side: α1(Wx1)+α2(Wx2)\alpha_1 (W \mathbf{x}_1) + \alpha_2 (W \mathbf{x}_2).

We know Wx1=(4,17)W \mathbf{x}_1 = (-4, 17) from Section 1.

Wx2=(11+00+(2)(1)31+10+4(1))=(31)W \mathbf{x}_2 = \begin{pmatrix} 1 \cdot 1 + 0 \cdot 0 + (-2)(-1) \\ 3 \cdot 1 + 1 \cdot 0 + 4 \cdot (-1) \end{pmatrix} = \begin{pmatrix} 3 \\ -1 \end{pmatrix}.

α1(Wx1)+α2(Wx2)=0.6(4,17)+0.4(3,1)=(2.4,10.2)+(1.2,0.4)=(1.2,9.8)\alpha_1 (W\mathbf{x}_1) + \alpha_2 (W\mathbf{x}_2) = 0.6 \cdot (-4, 17) + 0.4 \cdot (3, -1) = (-2.4, 10.2) + (1.2, -0.4) = (-1.2, 9.8).

Both sides give (1.2,9.8)(-1.2, 9.8).

In MLA, this identity lets us compute the weighted sum of cached latent vectors jαjcjKV\sum_j \alpha_j \mathbf{c}_j^{KV} first, and then apply the output projection once — instead of applying the up-projection to each of the TT cached vectors individually.


8. Low-Rank Factorization

This is the structural idea behind MLA’s KV compression. The term “low-rank” appears in Section 2.3 of the DeepSeek-V2 blog. We need to understand what it means.

A matrix WRm×nW \in \mathbb{R}^{m \times n} has a low-rank factorization if it can be written as:

W=UV\boxed{W = U V}

where URm×rU \in \mathbb{R}^{m \times r} and VRr×nV \in \mathbb{R}^{r \times n} with r<min(m,n)r < \min(m, n). The number rr is called the rank of the factorization (or an upper bound on the rank of WW).

What this means

Without factorization, WW has m×nm \times n entries. With the factorization, we store UU (m×rm \times r entries) and VV (r×nr \times n entries), for a total of r(m+n)r(m + n) entries. When rmr \ll m and rnr \ll n, this is much less than mnmn.

But the key insight for MLA is not about storage of the weight matrices — it is about the intermediate representation. When we compute Wx=U(Vx)W\mathbf{x} = U(V\mathbf{x}), the intermediate vector c=Vx\mathbf{c} = V\mathbf{x} lives in Rr\mathbb{R}^r. If rr is small, this intermediate vector is a compressed representation of the input.

Numerical check

In our running example, AR2×3A \in \mathbb{R}^{2 \times 3} maps from 3 dimensions to 2. We can factor AA as the product of a 2×12 \times 1 matrix UU and a 1×31 \times 3 matrix VV… but only if AA has rank 1. Let us check. The rows of AA are (1,0,2)(1, 0, -2) and (3,1,4)(3, 1, 4). Is (3,1,4)(3, 1, 4) a scalar multiple of (1,0,2)(1, 0, -2)? That would require 3/1=1/03/1 = 1/0, which is undefined. So the rows are linearly independent, and AA has rank 2 — it cannot be factored through a 1-dimensional intermediate.

Instead, let us construct a rank-1 example that mirrors MLA’s structure. Define:

W=(213)(102)=(204102306)W = \begin{pmatrix} 2 \\ -1 \\ 3 \end{pmatrix} \begin{pmatrix} 1 & 0 & -2 \end{pmatrix} = \begin{pmatrix} 2 & 0 & -4 \\ -1 & 0 & 2 \\ 3 & 0 & -6 \end{pmatrix}

Here U=(2,1,3)TR3×1U = (2, -1, 3)^T \in \mathbb{R}^{3 \times 1} and V=(1,0,2)R1×3V = (1, 0, -2) \in \mathbb{R}^{1 \times 3}. This is a 3×33 \times 3 matrix with 9 entries, but it is fully determined by the 3+3=63 + 3 = 6 entries of UU and VV.

When we compute WxW \mathbf{x}, we can first compute the intermediate scalar c=Vx=12+0(1)+(2)3=4c = V\mathbf{x} = 1 \cdot 2 + 0 \cdot (-1) + (-2) \cdot 3 = -4, and then compute Uc=(4)(2,1,3)T=(8,4,12)TU \cdot c = (-4) \cdot (2, -1, 3)^T = (-8, 4, -12)^T.

Alternatively, compute WxW\mathbf{x} directly:

Wx=(22+0(1)+(4)3(1)2+0(1)+2332+0(1)+(6)3)=(8412)W\mathbf{x} = \begin{pmatrix} 2 \cdot 2 + 0 \cdot (-1) + (-4) \cdot 3 \\ (-1) \cdot 2 + 0 \cdot (-1) + 2 \cdot 3 \\ 3 \cdot 2 + 0 \cdot (-1) + (-6) \cdot 3 \end{pmatrix} = \begin{pmatrix} -8 \\ 4 \\ -12 \end{pmatrix}

Both methods give (8,4,12)T(-8, 4, -12)^T. The intermediate representation was a single scalar c=4c = -4. In MLA, VV plays the role of WDKVW^{DKV} (down-projection), cc plays the role of the latent vector ctKV\mathbf{c}_t^{KV}, and UU plays the role of WUKW^{UK} or WUVW^{UV} (up-projection). The crucial point: we only need to cache cc, not the full output WxW\mathbf{x}.


9. Dot Product of Concatenated Vectors

In Section 4.3 of the DeepSeek-V2 blog, the full query and key are formed by concatenating a content part and a RoPE part: qi=[qiC;qiR]\mathbf{q}_i = [\mathbf{q}_i^C; \mathbf{q}_i^R] and ki=[kiC;kiR]\mathbf{k}_i = [\mathbf{k}_i^C; \mathbf{k}_i^R]. Their dot product splits into two independent terms. We derive why.

The concatenation of aRm\mathbf{a} \in \mathbb{R}^m and bRn\mathbf{b} \in \mathbb{R}^n is the vector [a;b]Rm+n[\mathbf{a}; \mathbf{b}] \in \mathbb{R}^{m+n} formed by stacking b\mathbf{b} below a\mathbf{a}:

[a;b]=(a1,,am,b1,,bn)T[\mathbf{a}; \mathbf{b}] = (a_1, \ldots, a_m, b_1, \ldots, b_n)^T

The dot product of two concatenated vectors, [a;b][\mathbf{a}; \mathbf{b}] and [c;d][\mathbf{c}; \mathbf{d}] (with a,cRm\mathbf{a}, \mathbf{c} \in \mathbb{R}^m and b,dRn\mathbf{b}, \mathbf{d} \in \mathbb{R}^n), is:

[a;b]T[c;d]=i=1maici+j=1nbjdj[\mathbf{a}; \mathbf{b}]^T [\mathbf{c}; \mathbf{d}] = \sum_{i=1}^{m} a_i c_i + \sum_{j=1}^{n} b_j d_j

The first sum is aTc\mathbf{a}^T \mathbf{c} and the second is bTd\mathbf{b}^T \mathbf{d}. Therefore:

[a;b]T[c;d]=aTc+bTd\boxed{[\mathbf{a}; \mathbf{b}]^T [\mathbf{c}; \mathbf{d}] = \mathbf{a}^T \mathbf{c} + \mathbf{b}^T \mathbf{d}}

Derivation

By the definition of the dot product:

[a;b]T[c;d]=k=1m+n([a;b])k([c;d])k[\mathbf{a}; \mathbf{b}]^T [\mathbf{c}; \mathbf{d}] = \sum_{k=1}^{m+n} ([\mathbf{a}; \mathbf{b}])_k \cdot ([\mathbf{c}; \mathbf{d}])_k

The first mm entries of [a;b][\mathbf{a}; \mathbf{b}] are a1,,ama_1, \ldots, a_m and the last nn entries are b1,,bnb_1, \ldots, b_n. Similarly for [c;d][\mathbf{c}; \mathbf{d}]. We split the sum at k=mk = m:

=k=1makck+k=1nbkdk=aTc+bTd= \sum_{k=1}^{m} a_k c_k + \sum_{k=1}^{n} b_k d_k = \mathbf{a}^T \mathbf{c} + \mathbf{b}^T \mathbf{d} \quad \square

The second equality uses the definition of the dot product applied to each pair.

Numerical check

Let a=(2,1)T\mathbf{a} = (2, -1)^T, b=(3,)\mathbf{b} = (3,) (a 1-dimensional vector), c=(0,4)T\mathbf{c} = (0, 4)^T, d=(2,)\mathbf{d} = (-2,).

Direct computation. [a;b]=(2,1,3)T[\mathbf{a}; \mathbf{b}] = (2, -1, 3)^T and [c;d]=(0,4,2)T[\mathbf{c}; \mathbf{d}] = (0, 4, -2)^T.

[a;b]T[c;d]=20+(1)4+3(2)=046=10[\mathbf{a}; \mathbf{b}]^T [\mathbf{c}; \mathbf{d}] = 2 \cdot 0 + (-1) \cdot 4 + 3 \cdot (-2) = 0 - 4 - 6 = -10

Split computation. aTc=20+(1)4=4\mathbf{a}^T \mathbf{c} = 2 \cdot 0 + (-1) \cdot 4 = -4. bTd=3(2)=6\mathbf{b}^T \mathbf{d} = 3 \cdot (-2) = -6. Sum: 4+(6)=10-4 + (-6) = -10.

Both give 10-10.

Interpretation

The dot product of concatenated vectors decomposes cleanly into independent terms. No cross-talk: the content entries of the query only interact with the content entries of the key, and the RoPE entries only interact with the RoPE entries. This is why the DeepSeek-V2 blog writes:

qt,iTkj,i=(qt,iC)Tkj,iC+(qt,iR)TkjR\mathbf{q}_{t,i}^T \mathbf{k}_{j,i} = (\mathbf{q}_{t,i}^C)^T \mathbf{k}_{j,i}^C + (\mathbf{q}_{t,i}^R)^T \mathbf{k}_j^R

The content similarity and the positional similarity contribute additively. They can be computed and understood independently.


Summary

We built eight linear algebra tools. Matrix-vector multiplication takes a matrix and a vector and produces a new vector whose dimension equals the number of rows of the matrix — this is how every projection in MLA works. The transpose swaps rows and columns, and the transpose of a product reverses the order: (XY)T=YTXT(XY)^T = Y^T X^T. Matrix-matrix multiplication extends the row-dot-column rule to two matrices. Associativity says (AB)C=A(BC)(AB)C = A(BC) — this lets us precompute combined projection matrices in MLA. Non-commutativity says ABBAAB \neq BA in general — this is why RoPE cannot be pushed inside the up-projection. The bilinear identity aTMb=(MTa)Tb\mathbf{a}^T M \mathbf{b} = (M^T \mathbf{a})^T \mathbf{b} moves a matrix from one side of a dot product to the other — this is the heart of the key absorption trick. Linearity lets us pull a matrix out of a weighted sum — this is how the value absorption works. Low-rank factorization writes W=UVW = UV with a small intermediate dimension — this is the compression that makes MLA’s cache so small. And the dot product of concatenated vectors decomposes into independent terms — this is why decoupled RoPE works.

With these tools in hand, we are ready for the DeepSeek-V2 blog, where we put them all together to derive Multi-head Latent Attention and DeepSeekMoE from scratch.


Previous: Grouped-Query Attention: Fewer KV Heads, Same Quality
Next: DeepSeek-V2 from Scratch: Multi-head Latent Attention and DeepSeekMoE

Enjoyed this post?

Subscribe to get notified when I publish new posts. No spam, unsubscribe anytime.