Generative AI
LoRA (Low-Rank Adaptation)
The product of two thin matrices has the same shape as the frozen base, but it can express only as many independent update directions as the bottleneck allows.
The base and target are 8 by 8. A dense update trains all 64 entries.
- Base weights
- Target and applied updates
Columns in the layer. The wider fixture adds zero columns to the target and keeps its independent directions.
One trainable entry for every base weight.
For an 8 × 16 layer and rank 2, how many entries are in the two factors combined?
Change the layer width. A dense update has the same number of rows and columns as the base weights.
Check this stage
Check your understanding
LoRA freezes the original weights and learns an additive update through two thin matrices. Use rank to change what the update can represent, then use alpha to change how much of that update is applied.
What changes when the base freezes?
A dense update can change every entry in a weight matrix. LoRA keeps those original entries fixed and adds a second path. The base still computes and can pass gradients to earlier layers. The optimiser does not update through the adapter. Once the adapter is enabled, the same input can produce a different output even though the base weights have not changed.
How do two thin matrices make a full-size update?
For a layer with outputs and inputs, the update must be , like . The adapter uses with shape and with shape . Reading from right to left, first maps the input to coordinates and expands them back to outputs. The product fills a matrix, but its rank is at most .
Count the entries that can change: the two factors need , whereas a dense update needs . For an eight by eight layer, rank three needs 48 entries compared with 64 for a dense update. Rank four already costs the same as the dense update. Widen the input in the lab and watch where the advantage disappears.
What do rank and alpha change?
The lab uses a synthetic target with three independent directions. Its truncated singular value decomposition finds the best fit the current rank allows, measured by Best residual. That percentage measures the size of the leftover difference relative to the original target. It does not measure training loss or task performance.
The Applied residual compares the target with . Set alpha equal to rank to apply the fitted product at unit scale. At rank one, two directions are still missing. At rank three, all three fit. With rank held at three, set alpha to zero. The best fit is unchanged, but the applied update is zero and the applied residual rises to 100%. Rank zero also turns the adapter off; the lab skips the division in that case.
Open Under the hood to multiply one row of by one column of . Add the signed products, apply the scale, and add that result to the corresponding frozen weight. This is the same arithmetic at every cell of the applied update.
Does LoRA make the model smaller?
The base matrix keeps its original shape. A small adapter can reduce trainable parameters and their optimiser state, and the factors can be saved separately. If you merge the update into the base, the merged matrix still has the same dimensions and can use the original matrix multiplication.
This lab solves a matrix approximation to isolate rank and scale. Real LoRA learns the factors from task data; the best rank and resulting model quality depend on the task. The LoRA paper explains the original method and the LoRA and QLoRA concept shows how low-rank training differs from quantising the base.
Stage recap
Dense update
Change the layer width. A dense update has the same number of rows and columns as the base weights.
Freeze the base
The base still computes Wx. Its weights receive no updates, but the layer output can change when an adapter is added.
Factor the update
A × B has the target shape at every rank. B compresses a column input first, then A expands it. Try rank at unit scale here; Rank sweep unlocks alpha.
Rank sweep
Set alpha equal to rank to isolate capacity. Then turn alpha to zero: the best fit stays put while the applied update vanishes.
Reference
- Base weights
- W stays fixed during adapter training. It still contributes Wx to the output.
- Rank and shape
- For d outputs and k inputs, A has d × r entries and B has r × k. B acts on x first.
- Trainable entries
- The two factors need r(d + k) entries. A dense update needs dk.
- Alpha scaling
- At positive rank, the product is scaled by alpha divided by rank. Rank zero is the off case.
- Relative residual
- The Frobenius norm of the difference from the target, divided by the target norm. The best fit uses truncated SVD at unit scale.
Break it on purpose
A bottleneck that is too narrow leaves part of the target update out, even when the adapter is applied at unit scale.