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
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.
A bottleneck of width r
Rank bound and parameter count
Rank 1 by hand, then at scale
Check it in numpy
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
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.
Bytewise
All courses