Search

Search papers and lessons

Low-Rank Adaptation (LoRA)

Level 4 · 10 min read

LoRA fine-tunes a large model by learning a small low-rank correction to each weight matrix and leaving the original weights frozen, cutting trainable parameters by orders of magnitude.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Post-training
  • Probability
  • Neural network basics
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Fine-tuning a large model normally updates every weight, which means storing and optimising a full copy of billions of numbers for each new task. LoRA freezes the pretrained weights and learns only a correction to each weight matrix, and it constrains that correction to be low rank. The correction is a product of two thin matrices, so the number of trainable parameters can fall by a factor of hundreds while the model keeps the behaviour it learned in pretraining.

Where you'll see it: in adapting open language models to a new style or domain on a single GPU, in shipping many small task-specific adapters on top of one shared base model, and in the image diffusion community, where LoRA files customise a style or a character.

Intuition

Change the model by a small note

A pretrained model is like a skilled editor. Retraining it from scratch for each new job would be like hiring a new editor each time. Fine-tuning changes the editor's habits directly, which is costly because every habit is a number you must store. LoRA leaves the editor alone and hands over a short sheet of corrections: "when you see this kind of sentence, do this extra adjustment." The sheet is small to write, small to store, and can be swapped out.

In the language of Fine-Tuning, the question is what the update to the weights looks like. Full fine-tuning allows the update to be any matrix of the same shape as the weight. LoRA's bet is that the update needed for a new task does not need that freedom. It can live in a small subspace of directions, which is the same as saying the update has low rank.

Why low rank is plausible

Recall from Matrix Rank that the rank of a matrix counts the independent directions its outputs can reach. A weight matrix of size 4096×40964096 \times 4096 has rank up to 4096. An update of rank 8 can only add changes along 8 directions. That sounds restrictive, but a task usually asks the model to shift a few behaviours, not to rebuild everything. Hu et al. (2021) found that small ranks often matched full fine-tuning on the tasks they tested, and that the useful updates were concentrated in few directions.

There is an earlier clue. Aghajanyan et al. (2020) measured the intrinsic dimension of fine-tuning: they found that a model could be tuned to a task while training only a random projection of a much smaller number of parameters. Low intrinsic dimension says the task itself lives in a small space, and LoRA is a structured way to search there.

The construction is cheap to run. The base weight is never modified during training, so gradients and optimiser state exist only for the small factors. At inference the update can be added into the base weight, so the adapted model runs at the same speed as the original.

flowchart LR
  X[Input x] --> W0[Frozen weight W0]
  X --> A[Down projection A]
  A --> B[Up projection B]
  W0 --> S[Sum]
  B --> S
  S --> Y[Output h]

The math

The update

Let W0∈Rd×kW_0 \in \mathbb{R}^{d \times k} be a pretrained weight matrix in a layer that computes h=W0xh = W_0 x for an input x∈Rkx \in \mathbb{R}^{k}. Full fine-tuning learns an update ΔW\Delta W of the same size, so the layer becomes h=(W0+ΔW)xh = (W_0 + \Delta W)x.

Definition: LoRA update

LoRA constrains the update to a product of two thin matrices,

ΔW=BA,B∈Rd×r,A∈Rr×k,r≪min⁡(d,k),\Delta W = B A, \qquad B \in \mathbb{R}^{d \times r}, \quad A \in \mathbb{R}^{r \times k}, \quad r \ll \min(d, k),

and computes the layer as

h=W0x+αr BAx.h = W_0 x + \frac{\alpha}{r}\, B A x.

The rank rr is a hyperparameter and α\alpha is a fixed scale. Only AA and BB are trained.

Here dd is the output size of the layer, kk its input size, and rr the rank chosen by the practitioner, often between 1 and 64. The factor α/r\alpha / r keeps the size of the update roughly stable as rr changes, so one learning rate can be reused across ranks. The product BABA has rank at most rr, because every column of BABA is a combination of the rr columns of BB. That is the whole constraint.

Initialisation

The two factors start in a specific way. AA is filled with small random values, and BB is filled with zeros. Then at step zero

BA=0⇒h=W0x,BA = 0 \quad\Rightarrow\quad h = W_0 x,

so the adapted model starts exactly where the pretrained model is. Training then moves BB away from zero. If both factors started random, the model would begin with a random perturbation of its learned behaviour, and that perturbation would have to be unlearned first.

Parameter count

A full update has dkdk parameters. The LoRA update has r(d+k)r(d + k). The fraction of trainable parameters is therefore

r(d+k)dk.\frac{r(d+k)}{dk}.

For a square layer d=kd = k this equals 2r/d2r/d. The formula shows why the approach scales: the saving grows as the layer gets wider, and it is controlled entirely by rr.

Low-rank approximation

Why should a rank-rr update be enough? The Eckart–Young theorem gives a precise answer for approximating a fixed matrix. Among all matrices of rank at most rr, the closest one to a given matrix MM (in Frobenius norm) is obtained by keeping its top rr singular values and vectors from the singular value decomposition. The squared Frobenius error of that best rank-rr approximation is the sum of the squared discarded singular values, so the error is small when the spectrum decays fast.

LoRA does not compute an SVD of the update. It learns the factors directly by gradient descent. The theorem says what is possible; the experiments say the required spectrum is usually fast-decaying enough for the task.

10203040506000.010.020.030.04LoRA rank rtrainable fraction of the layer
  • trainable fraction, d = k = 4096
For a 4096 by 4096 layer the trainable fraction is 2r/4096, which is r/2048. Rank 8 trains about 0.4 percent of the layer's parameters.

Worked examples

Example
One attention projection, rank 8

Take a square projection with d=k=4096d = k = 4096, and set r=8r = 8 and α=16\alpha = 16.

  1. Full update: dk=4096×4096=16,777,216dk = 4096 \times 4096 = 16{,}777{,}216 parameters.
  2. LoRA update: r(d+k)=8×8192=65,536r(d + k) = 8 \times 8192 = 65{,}536 parameters.
  3. Ratio: 16,777,216/65,536=25616{,}777{,}216 / 65{,}536 = 256. LoRA trains 256 times fewer parameters for this layer.
  4. Scale factor: α/r=16/8=2\alpha / r = 16 / 8 = 2, so the layer computes h=W0x+2BAxh = W_0 x + 2 B A x.
  5. Start of training: B=0B = 0 gives h=W0xh = W_0 x, the pretrained output exactly.

Interpretation: the layer's output changes only through a rank-8 term, and the optimiser tracks only 65,536 numbers for it instead of 16.8 million. Repeating this for every projection in a model keeps the total small in the same proportion.

Example
A tiny rank-1 update by hand

Take d=2d = 2, k=2k = 2, r=1r = 1. Let B=(10)B = \begin{pmatrix} 1 \\ 0 \end{pmatrix} and A=(0.50.5)A = \begin{pmatrix} 0.5 & 0.5 \end{pmatrix}.

  1. BA=(10)(0.50.5)=(0.50.500)BA = \begin{pmatrix} 1 \\ 0 \end{pmatrix}\begin{pmatrix} 0.5 & 0.5 \end{pmatrix} = \begin{pmatrix} 0.5 & 0.5 \\ 0 & 0 \end{pmatrix}.
  2. The rank is 1, since the second row is zero and the first row is nonzero.
  3. Apply to x=(2,4)x = (2, 4): Ax=0.5⋅2+0.5⋅4=3Ax = 0.5 \cdot 2 + 0.5 \cdot 4 = 3.
  4. Then B(Ax)=(3,0)B(Ax) = (3, 0). With α/r=1\alpha / r = 1, the correction to hh is (3,0)(3, 0).
  5. The full 2×22 \times 2 update has 4 entries, but the LoRA form uses 1⋅(2+2)=41 \cdot (2 + 2) = 4 numbers. At this size the saving is zero; it appears only when dd and kk are large.

Interpretation: the correction always points along the single direction BB, however xx varies. Step 5 is a useful reminder that low rank pays off only at scale.

Tradeoffs and failure modes

LoRA is cheap, but the constraint is real. The table covers the usual decisions.

SituationWhat goes wrongBetter choice
The new task needs behaviour far from pretrainingRank 8 cannot express the change and the loss plateausRaise rr, or fine-tune more layers
Only attention projections are adaptedSome tasks respond better to the feed-forward layersAdapt the feed-forward projections too
Learning rate copied from full fine-tuningAdapter learns too slowly or too unstablyTune the rate separately; it is often larger
Many adapters loaded at onceMemory and merging logic get complicatedMerge one adapter into the base, or batch the adapters explicitly
Base model quantised to low precisionFrozen weights limit accuracyUse a quantised LoRA variant, which keeps the adapter in higher precision

The method also changes what the model can forget. Because the base is frozen, the original capabilities are largely preserved, which is an advantage. It is also a limit: if the new task requires rewriting a capability, the low-rank correction may be the wrong tool.

History

Adapter layers came first. Houlsby et al. (2019) inserted small trainable bottleneck modules between the layers of a frozen pretrained model, and showed that they matched full fine-tuning on many tasks with a small fraction of the parameters. Aghajanyan et al. (2020) then measured how many parameters a task really needs, the intrinsic dimension, and found it was often small.

Hu et al. (2021) at Microsoft proposed LoRA. Its advantages over adapter layers were that it adds no extra depth at inference, because the update can be merged into the base weights, and that it applies directly to the weight matrices that already exist. It caught on quickly in open-source language model work, where a single GPU could fine-tune a model that was too large to copy in full for each user. Its use then spread to image generation, which is why many shared LoRA files now customise diffusion models.

Check yourself

QuestionRecall: what does the initialisation B=0B = 0 accomplish at the start of training?Show answer

It makes the product BABA zero, so the layer output is exactly W0xW_0 x. The adapted model begins identical to the pretrained model, and training moves it away from that point in small steps.

QuestionCompute: a layer has d=1024d = 1024 and k=4096k = 4096. How many parameters does a rank-16 LoRA update train, and how many does the full update have?Show answer

LoRA trains r(d+k)=16×5120=81,920r(d + k) = 16 \times 5120 = 81{,}920 parameters. The full update has dk=1024×4096=4,194,304dk = 1024 \times 4096 = 4{,}194{,}304. The LoRA count is about 2 percent of the full count.

QuestionWhy: the scale is α/r\alpha / r rather than a fixed constant. What problem does that solve?Show answer

The size of the update BABA tends to grow with the rank, because more terms add together. Dividing by rr keeps the effect of the correction roughly stable as rr changes, so the same learning rate and α\alpha can be used when a practitioner tries a different rank.

QuestionApplied: you adapt a model to a new domain and find the loss stops improving at rank 4 but drops at rank 64. What do you conclude, and what would you check next?Show answer

The task needs more directions of change than rank 4 provides, so the rank is a bottleneck. Raise the rank, which increases the trainable count proportionally. Also check that the learning rate is appropriate for the chosen rank and that the adapted layers include the projections that matter for this task. Rank 64 is still far fewer parameters than full fine-tuning.

Sources