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.
We work with a 3-dimensional vector and a 2×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=2−13
The matrix:
A=(1301−24)
In the DeepSeek-V2 blog, x plays the role of the hidden state ht (512-dimensional in practice), and A plays the role of a projection matrix like WDKV that compresses the hidden state into a smaller latent vector. Here, A 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 A∈Rm×n and a vector x∈Rn and produces a new vector y=Ax∈Rm. Each entry of y is the dot product of one row of A with x:
yi=j=1∑nAijxj
The matrix has m rows and n columns. The vector has n 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 m entries, one per row of the matrix.
Numerical check
A is 2×3 and x is 3×1. The inner dimensions match (both 3), so the product is defined and produces a 2×1 vector.
Row 1 of A is (1,0,−2). Its dot product with x=(2,−1,3) is:
y1=1⋅2+0⋅(−1)+(−2)⋅3=2+0−6=−4
Row 2 of A is (3,1,4):
y2=3⋅2+1⋅(−1)+4⋅3=6−1+12=17
So:
Ax=(1301−24)2−13=(−417)
We went from a 3-dimensional vector to a 2-dimensional vector. In MLA, this is exactly what the down-projection WDKV 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), in the matrix absorption trick ((WUK)T), and in the key identity that makes absorption work.
The transpose of a matrix A∈Rm×n is the matrix AT∈Rn×m obtained by swapping rows and columns:
(AT)ij=Aji
Row i of AT is column i of A. Column j of AT is row j of A. The shape flips from m×n to n×m.
Numerical check
A=(1301−24)⟹AT=10−2314
A is 2×3. AT is 3×2. Entry (1,2) of AT is 3, which is entry (2,1) of A. Entry (3,1) of AT is −2, which is entry (1,3) of A. Every entry checks out.
Transpose of a vector
A column vector x∈Rn is a n×1 matrix. Its transpose xT is a 1×n row vector. This is why the attention score qTk is a scalar: qT is 1×d and k is d×1, so the product is 1×1.
The dot product as a matrix product
In the attention blogs, we write the dot product as qTk. This is exactly the matrix product of a 1×d row vector with a d×1 column vector:
qTk=i=1∑dqiki
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 A∈Rm×n and B∈Rn×p, their product C=AB∈Rm×p has entries:
Cij=k=1∑nAikBkj
Each entry Cij is the dot product of row i of A with column j of B. The inner dimensions must match: A has n columns, B has n rows.
Numerical check
We need a second matrix to multiply with AT. Let us define:
B=(52−13)
Then ATB has AT∈R3×2 and B∈R2×2, so ATB∈R3×2.
Entry (1,1): row 1 of AT is (1,3), column 1 of B is (5,2). Dot product: 1⋅5+3⋅2=5+6=11.
Entry (1,2): row 1 of AT is (1,3), column 2 of B is (−1,3). Dot product: 1⋅(−1)+3⋅3=−1+9=8.
Entry (2,1): row 2 of AT is (0,1), column 1 of B is (5,2). Dot product: 0⋅5+1⋅2=2.
Entry (2,2): row 2 of AT is (0,1), column 2 of B is (−1,3). Dot product: 0⋅(−1)+1⋅3=3.
Entry (3,1): row 3 of AT is (−2,4), column 1 of B is (5,2). Dot product: (−2)⋅5+4⋅2=−10+8=−2.
Entry (3,2): row 3 of AT is (−2,4), column 2 of B is (−1,3). Dot product: (−2)⋅(−1)+4⋅3=2+12=14.
ATB=112−28314
This is 3×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) as ((WiUK)TWiUQ)ctQ. This is only valid if matrix multiplication is associative.
Associativity states that for any matrices A, B, C of compatible dimensions:
(AB)C=A(BC)
We can group the multiplication in either order and get the same result. This means we can precompute AB as a single matrix and then multiply by C, or we can first compute BC and then multiply by A. The answer is identical.
Proof
Let A∈Rm×n, B∈Rn×p, C∈Rp×q. Both (AB)C and A(BC) are in Rm×q. We show that entry (i,j) is the same on both sides.
Left side.(AB)C means: first compute D=AB, where Dik=∑l=1nAilBlk. Then compute (DC)ij=∑k=1pDikCkj. Substituting:
((AB)C)ij=k=1∑p(l=1∑nAilBlk)Ckj
We distribute Ckj into the inner sum:
=k=1∑pl=1∑nAilBlkCkj
Right side.A(BC) means: first compute E=BC, where Elj=∑k=1pBlkCkj. Then compute (AE)ij=∑l=1nAilElj. Substituting:
Both sides equal ∑k=1p∑l=1nAilBlkCkj. 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. □
Numerical check
We verify (ATB)x=AT(Bx) using our running example. Here AT∈R3×2, B∈R2×2, and we need a 2-dimensional vector. Let us define:
z=(4−3)
Left side: (ATB)z. We already computed ATB=112−28314. Multiply by z:
In MLA, this is exactly how the matrix absorption trick works. The attention score involves (WiUK)T applied to the query, then dotted with the cached latent. By associativity, we can precompute W~iQ=(WiUK)TWiUQ 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, AB=BA. This is true even when both products are defined (i.e., when A and B are both square matrices of the same size).
Numerical check
We use two 2×2 matrices to demonstrate. Let:
P=(1021),R=(01−10)
R is a 90-degree rotation matrix (exactly the kind of operation RoPE performs). Let us compute both PR and RP.
PR=RP. The (1,1) entry is 2 versus 0. The (2,2) entry is 0 versus 2. The order matters.
This is precisely the problem with RoPE and MLA. The up-projection WUK and the RoPE rotation Rt (which depends on position t) do not commute. So Rt(WUKct)=WUK(Rtct) 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 as ((WiUK)Tqt,iC)TcjKV. That is this identity.
For any vector a∈Rm, matrix M∈Rm×n, and vector b∈Rn:
aTMb=(MTa)Tb
Derivation
Start with the left side. aTMb is a scalar (a 1×1 matrix). The transpose of a scalar is itself:
aTMb=(aTMb)T
Now we apply the transpose of a product rule. For any matrices X and Y of compatible dimensions, (XY)T=YTXT. Let us prove this rule before using it.
Detour: Transpose of a Product
We claim (XY)T=YTXT for any X∈Rm×n and Y∈Rn×p. Both sides are in Rp×m. Entry (j,i) of (XY)T equals entry (i,j) of XY:
((XY)T)ji=(XY)ij=k=1∑nXikYkj
Entry (j,i) of YTXT:
(YTXT)ji=k=1∑n(YT)jk(XT)ki=k=1∑nYkjXik
Each term in the sum is XikYkj (by commutativity of real number multiplication). So the two sums are identical. □
Completing the derivation
We apply the transpose-of-a-product rule to aTMb. Grouping as (aTM)b, we have two factors: aTM (a 1×n row vector) and b (an n×1 column vector). Their product is 1×1. Taking the transpose:
((aTM)b)T=bT(aTM)T
Now apply the transpose-of-a-product rule to aTM:
(aTM)T=MT(aT)T=MTa
Here we used the fact that (aT)T=a — transposing twice returns the original.
Substituting back:
aTMb=bT(MTa)
But bT(MTa) is the dot product of b and MTa, which equals (MTa)Tb (dot products are commutative: uTv=vTu). Therefore:
aTMb=(MTa)Tb□
Interpretation
The left side, aTMb, says: “Apply M to b, then dot with a.” The right side, (MTa)Tb, says: “Apply MT to a, then dot with 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 WiUK 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 T-many cached tokens (expensive) to the one current token (cheap).
Numerical check
Let a=Ax=(−4,17)T (computed in Section 1), M=B=(52−13), and b=z=(4,−3)T.
In Section 5.2 of the DeepSeek-V2 blog, we pull the value up-projection matrix WiUV out of a weighted sum: ∑jαj(WiUVcj)=WiUV∑jαjcj. This is linearity.
Linearity of matrix multiplication means that for any matrix W, vectors x1,x2,…,xT of compatible dimension, and scalars α1,α2,…,αT:
W(j=1∑Tαjxj)=j=1∑Tαj(Wxj)
Derivation
It suffices to prove two properties and then combine them.
Distributivity over addition. For any matrix W∈Rm×n and vectors u,v∈Rn:
The first equality uses the definition of matrix-vector multiplication. The second uses distributivity of real number multiplication over addition: a(b+c)=ab+ac. Since this holds for every entry i, we have W(u+v)=Wu+Wv.
Compatibility with scalar multiplication. For any scalar α:
(W(αu))i=k=1∑nWik(αuk)=αk=1∑nWikuk=α(Wu)i
The second equality uses the fact that α is a common factor in every term of the sum. So W(αu)=α(Wu).
Combining. Apply these two rules repeatedly to the sum ∑jαjxj:
W(j=1∑Tαjxj)=j=1∑TW(αjxj)=j=1∑Tαj(Wxj)□
The first equality uses distributivity over addition (applied T−1 times). The second uses compatibility with scalar multiplication (applied T times).
Numerical check
Let W=A=(1301−24). Define two 3-dimensional vectors:
In MLA, this identity lets us compute the weighted sum of cached latent vectors ∑jαjcjKV first, and then apply the output projection once — instead of applying the up-projection to each of the T 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 W∈Rm×n has a low-rank factorization if it can be written as:
W=UV
where U∈Rm×r and V∈Rr×n with r<min(m,n). The number r is called the rank of the factorization (or an upper bound on the rank of W).
What this means
Without factorization, W has m×n entries. With the factorization, we store U (m×r entries) and V (r×n entries), for a total of r(m+n) entries. When r≪m and r≪n, this is much less than mn.
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), the intermediate vector c=Vx lives in Rr. If r is small, this intermediate vector is a compressed representation of the input.
Numerical check
In our running example, A∈R2×3 maps from 3 dimensions to 2. We can factor A as the product of a 2×1 matrix U and a 1×3 matrix V… but only if A has rank 1. Let us check. The rows of A are (1,0,−2) and (3,1,4). Is (3,1,4) a scalar multiple of (1,0,−2)? That would require 3/1=1/0, which is undefined. So the rows are linearly independent, and A 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=2−13(10−2)=2−13000−42−6
Here U=(2,−1,3)T∈R3×1 and V=(1,0,−2)∈R1×3. This is a 3×3 matrix with 9 entries, but it is fully determined by the 3+3=6 entries of U and V.
When we compute Wx, we can first compute the intermediate scalar c=Vx=1⋅2+0⋅(−1)+(−2)⋅3=−4, and then compute U⋅c=(−4)⋅(2,−1,3)T=(−8,4,−12)T.
Both methods give (−8,4,−12)T. The intermediate representation was a single scalar c=−4. In MLA, V plays the role of WDKV (down-projection), c plays the role of the latent vector ctKV, and U plays the role of WUK or WUV (up-projection). The crucial point: we only need to cache c, not the full output Wx.
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] and ki=[kiC;kiR]. Their dot product splits into two independent terms. We derive why.
The concatenation of a∈Rm and b∈Rn is the vector [a;b]∈Rm+n formed by stacking b below a:
[a;b]=(a1,…,am,b1,…,bn)T
The dot product of two concatenated vectors, [a;b] and [c;d] (with a,c∈Rm and b,d∈Rn), is:
[a;b]T[c;d]=i=1∑maici+j=1∑nbjdj
The first sum is aTc and the second is bTd. Therefore:
[a;b]T[c;d]=aTc+bTd
Derivation
By the definition of the dot product:
[a;b]T[c;d]=k=1∑m+n([a;b])k⋅([c;d])k
The first m entries of [a;b] are a1,…,am and the last n entries are b1,…,bn. Similarly for [c;d]. We split the sum at k=m:
=k=1∑makck+k=1∑nbkdk=aTc+bTd□
The second equality uses the definition of the dot product applied to each pair.
Numerical check
Let a=(2,−1)T, b=(3,) (a 1-dimensional vector), c=(0,4)T, d=(−2,).
Direct computation.[a;b]=(2,−1,3)T and [c;d]=(0,4,−2)T.
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
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. Matrix-matrix multiplication extends the row-dot-column rule to two matrices. Associativity says (AB)C=A(BC) — this lets us precompute combined projection matrices in MLA. Non-commutativity says AB=BA in general — this is why RoPE cannot be pushed inside the up-projection. The bilinear identityaTMb=(MTa)Tb 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=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.