All courses

Rank and low-rank factorization

Explain why a product AB with inner dimension r has rank at most r. Compute how factoring a d×d weight matrix into d×r and r×d factors cuts parameters from d² to 2dr.

Builds on: Weights, parameters and layers

▶ Watch this lesson Start the whole course

What the animation says

Do we need all d² numbers?

A transformer layer holds weight matrices that are four thousand ninety-six by four thousand ninety-six. That's about sixteen point eight million numbers, for one matrix. But many useful transformations only push vectors along a few directions. The number of directions a matrix really uses is called its rank. If the rank is small, we can store the matrix with far fewer numbers. Let's see how, and what it costs.

A bottleneck of width r

Split the matrix into two pieces: a tall thin one, then a short wide one. An input row of d numbers goes through the first piece and gets squeezed to just r numbers. The second piece expands those r numbers back to d. So the output is always a weighted mix of the same r rows, the rows of the second piece. Now take d equals eight and r equals two, and feed in a thousand random inputs. How many independent directions can the outputs span?

Rank: count the directions

Here's the check in numpy. Random pieces with inner size two, multiplied together. Matrix rank says two. And the thousand outputs on the next lines? Still rank two. Rank is the number of independent rows, which always equals the number of independent columns. It's also the dimension of all the outputs the matrix can produce. Squeezing through r numbers means the product's rank is at most r, never more.

Rank 1 by hand

Smallest case: d is three, r is one. The tall piece is the column one, two, three. The wide piece is the row one, zero, two. Each row of the product is the row one, zero, two, scaled by one entry of the column. Row one is one, zero, two. Row two is two, zero, four. Row three has a gap. Tap the number that fills it.

Count the parameters

That rank-one matrix has nine entries, but its two pieces hold only three plus three: six numbers. In general, a d by d matrix stores d squared numbers. The two pieces store d times r, twice: two d r. At scale, d is four thousand ninety-six and r is eight. Sixteen point eight million versus sixty-five thousand five hundred thirty-six. That's two hundred fifty-six times fewer: d divided by two r. Now keep d at four thousand ninety-six, but let r be two thousand forty-eight. Does factoring still save anything?

The price, and what to remember

No saving at all: two d r matches d squared exactly when r is half of d. So factoring only pays when r is smaller than d over two. And there's a price. The pieces can only build matrices of rank r or less. If the matrix you need has higher rank, you only get an approximation, good only if it's close to rank r. Remember: a bottleneck of width r caps the rank at r, and costs two d r numbers instead of d squared.

Does a weight matrix need all d² numbers?

A $d\times d$ weight matrix with $d=4096$ stores about 16.8 million numbers. But many useful transformations only move vectors along a few directions. Can we store such a matrix with far fewer numbers, and what exactly do we give up? **Rank** measures how many directions a matrix really uses. **Low-rank factorization** exploits a small rank. Later this lets you adapt a huge model cheaply.

A bottleneck of width r

Write $W = W_1W_2$ with $W_1$ of shape $d\times r$ and $W_2$ of shape $r\times d$. Then $uW = (uW_1)W_2$ for an input row $u$. First squeeze $u$ down to $r$ numbers, then expand back to $d$. Whatever $u$ was, the output is those $r$ numbers times the $r$ rows of $W_2$, a weighted sum of just $r$ fixed vectors. So every output lies in a slice of $d$-dimensional space with at most $r$ dimensions.

Rank bound and parameter count

**Rank** = number of linearly independent rows (always equal to the number of independent columns) = dimension of the space of all outputs $uW$. For $W_1\in\mathbb{R}^{d\times r}$ and $W_2\in\mathbb{R}^{r\times d}$: $$\text{rank}(W_1W_2)\le\min(\text{rank}\,W_1,\text{rank}\,W_2)\le r.$$ Parameters: $d^2$ stored directly, versus $dr+rd=2dr$ factored. This saves whenever $r<d/2$. The price is that only matrices of rank $\le r$ can be represented exactly.

Rank 1 by hand, then at scale

Take $d=3, r=1$: $W_1=\begin{bmatrix}1\\2\\3\end{bmatrix}$, $W_2=[1\ 0\ 2]$. Then $W=W_1W_2=\begin{bmatrix}1&0&2\\2&0&4\\3&0&6\end{bmatrix}$. Every row is a multiple of $[1,0,2]$, so the rank is 1. It takes $3+3=6$ numbers instead of 9. At scale, with $d=4096, r=8$: $d^2=16{,}777{,}216$ versus $2dr=65{,}536$. That is 256× fewer, since $d/(2r)=256$.

Check it in numpy

Random factors with inner dimension 2 give a rank-2 product. Even 1000 different input rows only produce outputs spanning 2 directions.
import numpy as np
d, r = 8, 2
W1 = np.random.randn(d, r)
W2 = np.random.randn(r, d)
W = W1 @ W2
print(np.linalg.matrix_rank(W))        # 2
print(W.size, W1.size + W2.size)       # 64 32
U = np.random.randn(1000, d)           # 1000 input rows
print(np.linalg.matrix_rank(U @ W))    # still 2

What to remember

Passing through an inner dimension $r$ is a bottleneck. Every output is a weighted sum of $r$ vectors, so $\text{rank}(W_1W_2)\le r$. Factoring a $d\times d$ matrix into $d\times r$ and $r\times d$ pieces costs $2dr$ instead of $d^2$ parameters, a $d/(2r)$× saving. It only works if the matrix you need has rank at most $r$, or close to it.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.