LoRA addresses the main problem of fine-tuning large models: storing and updating billions of weights for every task is expensive. Instead, the base model stays unchanged, and everything learned during fine-tuning is two small matrices for each selected one. Hu and co-authors proposed the method in 2021; today it is one of the common ways of adapting open models to a task or a style.
How it works
Full fine-tuning changes every weight of the model. The LoRA hypothesis: when a model adapts to a new task, the change in its weights has a low “intrinsic rank”, so it can be compressed into the product of two narrow matrices. For a weight matrix W0, an update is added in the form of B·A, where B has the size d×r, A — r×k, and the rank r is much smaller than the dimensions of the matrix.
W0·x + ΔW·x = W0·x + B·A·xDuring training, W0 is frozen and only A and B receive gradients. Matrix A is initialised at random and matrix B with zeros, so at the start of training the adapter changes nothing. The update is scaled by α/r, where α (alpha) is a constant. In practice, LoRA is added to the attention matrices of a transformer (Wq, Wk, Wv, Wo).
Savings in numbers
The method’s paper gives this calculation for GPT-3 175B: with rank r=4 on the attention query and value matrices, the fine-tuning checkpoint shrinks roughly 10,000-fold — from 350 GB to 35 MB. Training memory drops from 1.2 TB to 350 GB, because frozen weights need no optimiser states or gradients.
| What | Full fine-tuning | LoRA |
|---|---|---|
| Trainable weights | all the weights of the model | the A and B matrices of the adapters, usually under one percent of the total |
| Training memory | high: weights + gradients + optimiser states | lower: gradients and optimiser only for the adapters |
| Task checkpoint | a copy of the whole model | tens of megabytes of adapters |
| Switching task | load another copy of the model | swap the adapters on top of a single base model |
| Inference slowdown | none | none: the adapter can be merged into the base model’s weights |
Rank and merging into the weights
The main hyperparameter is the rank r: the higher it is, the more trainable parameters there are and the more expressive the adapter. In the method’s paper, on GPT-3 even r=1 and r=4 gave good results; in practice values from single digits to hundreds are taken — for example, 8, 16 or 64. The second parameter is the scale α. An adapter is trained for a particular base model, so published adapters come with an indication of which model they belong to.
After training, the update B·A can be merged into the base weights: W = W0 + B·A — then the adapter disappears and the model runs without any slowdown. To switch task, B·A is subtracted and a new adapter added. Adapters are small, which makes them easy to publish: the base model with open weights is downloaded once, and the task-specific fine-tunes are files of tens of megabytes.
Sources
the term is written from these pagesVerified 9 Nov