Sub topic 3
The low-rank idea behind LoRA
This chapter assumes you know matrix multiplication and matrix rank, and that you have a high-level picture of a transformer's linear layers. The previous chapter covered why full fine-tuning is expensive. This one explains the observation that makes LoRA possible.
The update is the thing to shrink
Fine-tuning changes a pretrained weight matrix W into W + ΔW. Full fine-tuning learns every entry of ΔW. For a layer with dout × din weights, that is dout × din trainable numbers, plus optimizer state for each of them.
LoRA starts from a hypothesis: for a narrow task, ΔW does not need to be a general matrix. Most of its useful content lies in a few directions, so it is close to a low-rank matrix. Adapting a model to a style or a task seems to move the weights along a small number of directions, not in every direction at once.
What low rank means in practice
A matrix of rank r can be written as the product of two thin matrices. A dout × din matrix of rank at most r equals B × A, where B is dout × r and A is r × din. You store r × (din + dout) numbers instead of din × dout.
import torch
B = torch.randn(64, 4)
A = torch.randn(4, 64)
delta_w = B @ A
print(delta_w.shape)
print(torch.linalg.matrix_rank(delta_w))The product has the shape of a full 64 × 64 matrix, but its rank is 4. LoRA trains B and A and never builds the full ΔW as a trainable object.
How much you save
d_out, d_in, r = 4096, 4096, 8
full = d_out * d_in
lora = r * (d_in + d_out)
print(full, lora, f"{lora / full:.2%}")This prints 16777216 65536 0.39%. At rank 8, one 4096 × 4096 layer needs about 0.4% of the trainable parameters. The saving grows as the layer gets larger and the rank stays small.
The frozen W needs no gradients and no optimizer state. Only A and B do, which is where the memory saving comes from. The saved adapter is also small, because it holds only A and B.
Where the adapter sits
The base path is unchanged. The adapter path runs in parallel, and the two outputs are summed. The next chapter gives the exact formula, including the scaling factor.
Why the starting point matters
LoRA initializes B to zeros and A to small random values. The product B × A is then zero, so the adapted layer starts out identical to the base layer. Training begins from the pretrained model's behavior instead of from a randomly perturbed one.
Common errors in understanding
- Thinking LoRA is a smaller model. The base model keeps its full size. LoRA reduces trainable parameters and optimizer memory, not the memory needed to hold the base weights. QLoRA, covered later, addresses that.
- Assuming a higher rank is always better. Rank caps how expressive the update can be, but the low-rank hypothesis says a small rank is often enough. Past that point, extra rank mostly costs parameters.
- Expecting low rank to work for every task. If the task needs changes that really are high rank, such as teaching a large body of new knowledge, a small adapter can underfit.
- Initializing both matrices to zero. Both gradients would then be zero, because each depends on the other matrix, and training would never start. Only B is zero.
Exercise
A layer has 2048 inputs and 2048 outputs. How many trainable parameters does a rank-16 LoRA adapter add to it, and what fraction of the layer's 2048 × 2048 weights is that?
Show answer
The adapter adds r × (din + dout) = 16 × (2048 + 2048) = 65,536 parameters. The full layer has 2048 × 2048 = 4,194,304 weights, so the adapter is 65,536 / 4,194,304 = 1.5625%, about 1.6%.