Discover: fine-tuning a giant with a sliver of its parameters
GPT-3 has 175 billion parameters. Fine-tuning all of them for a new task produces a new 175-billion-parameter model, and a company with a hundred tasks would have to store a hundred of them. In 2021 a team at Microsoft proposed a way out called LoRA, low-rank adaptation. Freeze every pretrained weight matrix , and learn only a correction (read “delta W”), written as a product of two thin matrices:
Spotted in the wild
Here is , is and is , where is tiny. The paper uses values like for matrices with , and reports that even or can be enough. On GPT-3 that cuts the number of trainable parameters by a factor of about 10,000, while performing on par with full fine-tuning on the benchmarks they tested.
That sounds too good to be true, so check the arithmetic yourself.
A weight matrix is . LoRA writes its update as with and . How many numbers do and hold together?
is a full-size matrix, but it can't be any matrix. Its columns are all combinations of just columns of , so it only ever reaches an -dimensional slice of the space. That number, the true dimension of what a matrix can produce, is its rank. To see why LoRA works, you need to know what a matrix keeps, what it throws away, and how the two are related.
Learn: the column space, everything A can reach
Let be an matrix, so it takes inputs to outputs . Its column space is the set of every output it can produce:
Read it as “C of A is the set of all A x, for x in R n”. Since is a combination of the columns of weighted by the entries of (Chamber 4), the column space is exactly the span of the columns (Chamber 3). It's a subspace of : it contains , and sums and multiples of outputs are outputs again.
This answers the question Chamber 5 kept circling. has a solution exactly when is in . Take
Two independent columns span a plane in : the plane where the third entry is the sum of the first two. A right-hand side on that plane has a solution, and one off it has none.
Let . Which vector lies in the column space ?
To find a basis for in general, row reduce and look at where the pivots land: the pivot columns of the original form a basis. Careful: of , not of its echelon form. Row operations change the columns themselves (and so the column space), but they never change which combinations of columns add up to zero, since those combinations are the solutions of , and row operations never change solutions. So a column that depends on earlier ones in the echelon form depended on the same ones, with the same weights, in .
Learn: the null space, everything A destroys
The null space is the set of inputs that sends to zero:
Read it as “N of A, the set of x such that A x equals zero”. It's a subspace of , the input side: if and , then and . It's also called the kernel, written . The null space is what the transformation destroys. Two inputs that differ by a null-space vector , say and , give exactly the same output, because . A layer in a neural network literally cannot tell them apart.
Finding the null space is Chamber 5's elimination with . Here it is in full for
- 1
Reduce to reduced row echelon form
One operation does it: gives
The pivots are in columns 1 and 3, so and are pivot variables and and are free variables.
- 2
Write the pivot variables in terms of the free ones
says and , so
- 3
One special solution per free variable
Set one free variable to and the others to . With you get . With you get . Check the second in the original : and .
- 4
Every solution is a combination of them
Any solution has some values and , and the pivot variables follow, so . The null space is the plane inside .
This also completes the story of Chamber 5's “infinitely many solutions”. If is any one solution of (a particular solution), then every solution is with , because , and the difference of two solutions always lands in the null space. The line of solutions in Chamber 5's workbench was a copy of the null space, shifted away from the origin.
Learn: rank
The rank of is the number of pivots elimination finds:
Each pivot column contributes one independent direction to the column space, so the rank is the dimension of everything can reach. Two facts make rank the single most useful number about a matrix.
Row rank equals column rank. The rows of span a subspace of , the row space . Row operations only replace rows by combinations of rows, reversibly, so they never change the row space. In the echelon form, the non-zero rows hold one pivot each and are independent, so the row space has dimension too: the rank also counts the independent rows. That holds even for a matrix. So among those columns, at most can be independent.
The rank is at most , since there is at most one pivot per row and at most one per column. A matrix that reaches this limit has full rank:
- Full column rank (): every column has a pivot, there are no free variables, and . Then has at most one solution.
- Full row rank (): every row has a pivot, so no row can turn into . Then has at least one solution, for every .
- Square and full rank (): both at once. That's exactly an invertible matrix from Chamber 5.
- “the column space of A”Every output , that is, every combination of the columns of . A subspace of . Also written , or .
- “the null space of A”Every input with : what destroys. A subspace of . Also called the kernel, .
- “the row space of A”Every combination of the rows of (the column space of ). A subspace of .
- “the left null space of A”Every with , equivalently . A subspace of .
- “the rank of A”The number of pivots, which is also the number of independent columns and the number of independent rows. Some books write .
- “the dimension of V”The number of vectors in any basis of the subspace (Chamber 3). The dimension of the null space is sometimes called the nullity.
- “u v transpose”The outer product of and : the matrix with entries . It has rank 1 when and are non-zero.
- “r is much less than the smaller of d and k”LoRA's condition on the rank of its update: far smaller than either side of the weight matrix.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the column space of A” | Every output , that is, every combination of the columns of . A subspace of . Also written , or . | ||
| “the null space of A” | Every input with : what destroys. A subspace of . Also called the kernel, . | ||
| “the row space of A” | Every combination of the rows of (the column space of ). A subspace of . | ||
| “the left null space of A” | Every with , equivalently . A subspace of . | ||
| “the rank of A” | The number of pivots, which is also the number of independent columns and the number of independent rows. Some books write . | ||
| “the dimension of V” | The number of vectors in any basis of the subspace (Chamber 3). The dimension of the null space is sometimes called the nullity. | ||
| “u v transpose” | The outer product of and : the matrix with entries . It has rank 1 when and are non-zero. | ||
| “r is much less than the smaller of d and k” | LoRA's condition on the rank of its update: far smaller than either side of the weight matrix. |
Now put the definitions to work. The matrices in the lab range from to . Some are honest, and some hide a dependency between their rows. Find each rank by eliminating in your head or by spotting the dependency.
Interactive lab
Rank detective
Matrix 1 · 2 × 2
What is its rank?
Clues to look for
- Is one row a multiple of another? Then elimination turns it into a zero row.
- Is one row the sum (or another combination) of the others? Same story, better hidden.
- The rank can never exceed the number of rows, nor the number of columns.
- A square matrix with a non-zero determinant has full rank.
Learn: rank–nullity and the four subspaces
In the null space example, had columns: were pivot columns and were free, and the null space came out 2-dimensional. That's no coincidence.
Theorem (rank–nullity). For any matrix ,
Proof. Let be the reduced row echelon form of . Row operations don't change the solutions of (Chamber 5), so , and the rank is the number of pivots in . Each of the columns of is either a pivot column or a free column, never both, so there are exactly free columns. It remains to show that , and we do it by exhibiting a basis with vectors.
For each free column , let be the special solution: set , every other free variable to , and solve for the pivot variables. (Each row of has a single pivot, so it expresses one pivot variable in terms of the free ones, and the solution exists and is unique.) We check the two properties a basis needs.
- They span . Take any and form . The vector is in the null space (a combination of null-space vectors), and it has the same free entries as . The pivot entries of a null-space vector are determined by its free entries, so .
- They're independent. In , look at the entry in free position . Only has a there, and every other special solution has a , so that entry is . If the combination is the zero vector, every is .
So the special solutions form a basis of , and .
The theorem is an accounting identity for input directions. Each of the dimensions of the input is either kept, contributing to the output (a pivot), or destroyed, lost to the null space (a free variable). Nothing is lost without a trace and nothing is double-counted.
is a matrix of rank . What is ?
Apply the theorem to as well and you have all four fundamental subspaces, as Gilbert Strang calls them:
| Subspace | Name | Lives in | Dimension |
|---|---|---|---|
| column space | |||
| left null space | |||
| row space | |||
| null space |
There's a hidden geometry here. says every row of has dot product with (Chamber 2), so every null-space vector is perpendicular to the whole row space. The input space splits into two perpendicular pieces, of dimensions and , and the output space does the same. Chamber 7 builds least squares on exactly this picture.
Learn: rank one, and low-rank factorisation
The simplest non-zero matrices are outer products. For and , the matrix (read “u v transpose”) has entries :
Column is , so every column is a multiple of , and every row is a multiple of . When and are non-zero the rank is exactly (you'll prove it below). Nine entries, but six numbers describe them completely: an rank-one matrix needs only numbers instead of .
What is the rank of ?
Chamber 4's outer product view of multiplication says that a product is a sum of outer products. If has columns and has rows , then
Each term has rank at most , and the rank of a sum is at most the sum of the ranks (the columns of are combinations of the columns of and ). So . More generally, a product can never have more rank than either of its factors:
You'll assemble the proof in the last part of this chamber. The converse holds too: any matrix of rank can be written as with columns in , for instance with its pivot columns as and the non-zero rows of its reduced echelon form as . Low rank and a thin factorisation are the same thing.
Now derive LoRA's savings. The full update has entries. The factored update has entries in and in :
For GPT-3's with , that's per adapted matrix. The price is the constraint . LoRA's authors bet that it's a price worth paying: that the change a pretrained model needs for a new task has a low “intrinsic rank”. They took the idea from work on intrinsic dimension, including Li et al. (2018), the paper from Chamber 3. The experiments say the bet pays off, at least for the tasks they tried.
Read beyond the course
Pick at least one. The first two deepen the linear algebra; the last two take you into LoRA itself.
Book · free online · ~30 min
Mathematics for Machine LearningMarc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Section 2.6.2, Rank; Section 2.7.3, Image and Kernel
The same ideas in the language of linear mappings: the column space becomes the image and the null space the kernel, and Theorem 2.24 is rank–nullity. Worth reading now to get used to the notation , and , which you'll meet in papers.
Interactive · free online · ~35 min
Immersive Linear AlgebraJacob Ström, Kalle Åström & Tomas Akenine-Möller · Chapter 8, Rank
Interactive figures of null spaces and column spaces in 3D, which you can rotate. The chapter also proves that rank equals the number of pivots and that row rank equals column rank, and it states rank–nullity as the “dimension theorem”.
Tool · free online · ~15 min
microsoft/LoRA: loralib/layers.pyEdward J. Hu and colleagues (official implementation) · class Linear
The paper's equation as about sixty lines of PyTorch. Find the shapes of lora_A () and lora_B (), the scaling of , the zero initialisation of lora_B, and the train method, which merges into the frozen weight for inference. One small surprise: the code initialises lora_A with a uniform Kaiming initialisation rather than the Gaussian the paper describes, and a comment admits the difference. Reading code alongside a paper often turns up details like this.
Paper · free online · ~40 min
Intrinsic Dimensionality Explains the Effectiveness of Language Model Fine-TuningArmen Aghajanyan, Sonal Gupta & Luke Zettlemoyer (ACL 2021)
One of the two papers that inspired LoRA's low-rank hypothesis. It shows that fine-tuning a large language model in a random low-dimensional subspace of its parameters can get close to full fine-tuning. Read the abstract, the introduction and the first figure to see the evidence LoRA built on.
Papers and lectures
LoRA is one of the most widely used ideas in modern machine learning, and its core is this chamber's linear algebra. Read the abstract and look at Figure 1, which draws , and side by side. Then read Section 4.1 in full: it's one page, containing the equation you'll decode below, the initialisation, and the argument that merging adds no inference cost. Skim Section 4.2, which says which weight matrices to adapt. Then jump to Section 7.2, What is the optimal rank r for LoRA?, where Table 6 shows that is already competitive on some tasks. Skip Section 3 (on other adaptation methods), the experiment tables in Section 5 and the appendices for now.
LoRA: Low-Rank Adaptation of Large Language ModelsEdward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen · ICLR, 2022Freeze the pretrained weights and train a rank- update beside each chosen matrix. It became the standard way to fine-tune large models on modest hardware.
Decode the paper · Eq. (3), Section 4.1
LoRA: Low-Rank Adaptation of Large Language ModelsEdward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen · ICLR, 2022
LoRA fine-tunes a pretrained network without changing its weights. For each chosen weight matrix it learns a low-rank correction, and this equation is the layer's new forward pass. Match each symbol to its meaning.
Options
Watch
The geometric view of this chamber and the last. Watch for the moment a transformation squashes space onto a line: the line is the column space, the rank is its dimension, and the vectors that land on the origin form the null space.
Gilbert Strang's lecture on the four subspaces from MIT's 18.06. He works out the dimension of each subspace and how to find a basis for it, which is exactly the reasoning of this chamber, done at the blackboard.
Your turn
Start by matching the four subspaces, then translate the notation into NumPy.
Match · Object ↔ What it is
The four fundamental subspaces
is an matrix of rank . Match each object to its description.
Options
Match · Maths ↔ NumPy
From notation to NumPy
Match each piece of notation to the NumPy that computes it. u, v and x are 1-D arrays; A, B and W0 are 2-D arrays of compatible shapes.
Options
Proofs
Put the first proof in order, leaving out the lines that don't belong. It's the rank bound that makes LoRA's updates low-rank. Then write the second yourself.
Proof puzzle
A product can't have more rank than its factors
Claim
For any and ,
Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.
Your proof
- Pick the first line below.
Available lines
Prove it yourself
Outer products have rank one
Claim
Let and be non-zero vectors. Then the outer product has rank exactly .
Your typeset proof appears here.
Code it up
Count LoRA's parameters on GPT-3, classify every small 0/1 matrix by rank, and uncover the low rank hiding in a distance matrix.
Problem 16·Warm-up
Apply the paper: LoRA's parameter budget
Most of LoRA's GPT-3 experiments adapt only the attention's query and value projections, and , in every layer. GPT-3 175B has layers and model width , and each of and is a matrix.
Each adapted matrix gets its own trainable pair and . With rank , how many trainable parameters does LoRA add in total?
Problem 17·Standard
The ranks of all 0/1 matrices
Consider every matrix whose nine entries are each or . There are of them.
How many of them have rank exactly 2? Rank means the usual rank over the real numbers, as in this chamber: the number of pivots that ordinary Gaussian elimination produces.
Problem 18·Challenge
Distance matrices hide low rank
Given points , the squared distance matrix has entries
For each dimension , take the points with coordinates
and let be the rank of the resulting matrix . Find .
Key takeaways
- The column space is everything can reach, the span of its columns; is solvable exactly when .
- The null space is everything destroys; it's spanned by one special solution per free variable, and all solutions of are one particular solution plus the null space.
- Rank is the number of pivots: the dimension of the column space and of the row space, at most .
- Rank–nullity: . Every input direction is either kept or destroyed.
- Low rank means a thin factorisation: a rank- matrix is a sum of outer products, , stored with numbers, and .
- LoRA trains with , betting that fine-tuning needs only a low-rank change, and cuts trainable parameters by orders of magnitude.
Checkpoint
Prove it to the labyrinth
Answer every question to clear this chamber. First-try answers earn the most XP.
A linear layer maps 512-dimensional inputs to 256-dimensional outputs. What is the smallest possible dimension of its null space ?
Which vector is in the null space of ?
In LoRA, with and . What can you say for certain about ?
is a matrix of rank . What is the dimension of its left null space ?
The columns of are linearly independent, so . What does that tell you about ?
is and is . What is the largest possible rank of the matrix ?
For a square weight matrix, what fraction of the full update's entries does a rank- LoRA update need to train?
End of the chamber
Clear this chamber
- Questions in this chamber (0/11 solved)Next unsolved
- Bonus: Rank detective (+40 XP)
- Bonus: Problem 16: Apply the paper: LoRA's parameter budget (+20 XP)
- Bonus: Problem 17: The ranks of all 0/1 matrices (+35 XP)
- Bonus: Problem 18: Distance matrices hide low rank (+50 XP)
- Bonus: Proof: A product can't have more rank than its factors (+25 XP)
- Bonus: Proof: Outer products have rank one (+40 XP)
- Bonus: Decode the paper (+30 XP)
- Bonus: Match: The four fundamental subspaces (+25 XP)
- Bonus: Match: From notation to NumPy (+25 XP)