LoRA freezes a big model and learns each weight change as two thin matrices, cutting trainable numbers about 10,000 times on GPT-3 175B.
A big language model has billions of numbers. To teach it a new job, the usual method (fine-tuning) changes all of them and saves a full new copy for every job. For a model with 175 billion numbers, that is hundreds of gigabytes per job. LoRA is a way to teach the same job while changing and storing only a tiny extra piece.
LoRA: Low-Rank Adaptation of Large Language Models. Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen (Microsoft). arXiv, June 2021 (version 2, October 2021). arxiv.org/abs/2106.09685
Freeze the original model, and learn the change to each weight matrix as the product of two thin matrices.
The authors guess that the change needed for a new job is simple, so it can be written with far fewer numbers than the original matrix has. The paper tests this guess and finds it holds well on the tasks they tried.
A weight matrix is a grid of numbers. A layer of the model multiplies its input vector by this grid to get an output: . A big model is mostly many such grids. In the paper, is a frozen pre-trained grid with rows and columns.
The rank of a grid is how many truly independent rows it has. A rank-1 grid is built from one pattern: every row is just a multiple of one row. Toy example (made up to show the idea):
Full fine-tuning learns a change with the same size as . LoRA instead forces the change to be a product of two thin grids (paper, section 4.1):
Here is the rank you choose, usually a small number. The layer computes . Both parts get the same input and their outputs are added. Only and are trained. never changes.
Before training, is filled with random numbers and with zeros, so is zero and the model starts exactly as the original. The change is also multiplied by , where is a constant. The paper says this scaling reduces the need to retune settings when you change .
A grid has numbers. and together have .
Real-size example (our arithmetic): take and .
For GPT-3 175B (96 layers, ) the paper adapts two matrices per layer with . Our check: per matrix, times 2 matrices, times 96 layers, equals 18,874,368. The paper calls this its 18M budget.
A Transformer attention block has four weight matrices: (query), (key), (value) and (output). It also has two in its feed-forward part. The paper adapts only the attention ones, and in most experiments only and . It left the feed-forward matrices to future work.
With the number budget fixed at 18M on GPT-3, the paper compares ways to spend it (Table 5):
| Adapted matrices | Rank | WikiSQL accuracy (%) | MultiNLI accuracy (%) |
|---|---|---|---|
| only | 8 | 70.4 | 91.0 |
| only | 8 | 70.0 | 90.8 |
| only | 8 | 73.0 | 91.0 |
| , | 4 | 73.7 | 91.3 |
| , , , | 2 | 73.7 | 91.7 |
Spreading a small rank over more matrices beat putting a larger rank in one. The paper's own words are that adapting more matrices is preferable to adapting a single type with a larger rank.
The paper builds on earlier work (Aghajanyan et al., 2020) showing that pre-trained models can still learn well when training is squeezed into a small subspace. The authors guess the weight change has a low "intrinsic rank" too. Their evidence is Table 6: on GPT-3, ranks from 1 to 64 give almost the same accuracy when both and are adapted (WikiSQL 73.4 at , 73.8 at , 73.5 at ). They also found that the main directions learned at and overlap strongly. This is evidence, not proof, and it was measured on a few tasks.
and are both . So after training you can compute once and use as an ordinary weight matrix. The model has the same shape and the same speed as before. To switch jobs, subtract and add a different .
Toy check with the earlier numbers: for , , then . Multiplying by the merged grid gives the same .
Compared with fully fine-tuning GPT-3 175B (abstract and Table 4):
| GPT-3 175B method | Trainable numbers | WikiSQL (%) | MNLI-m (%) | SAMSum R1/R2/RL |
|---|---|---|---|---|
| Full fine-tuning | 175,255.8M | 73.8 | 89.5 | 52.0/28.0/44.5 |
| Adapters (largest) | 40.1M | 73.2 | 91.5 | 53.2/29.0/45.1 |
| LoRA | 4.7M | 73.4 | 91.7 | 53.8/29.8/45.9 |
| LoRA | 37.7M | 74.0 | 91.6 | 53.4/29.2/45.1 |
LoRA matches or beats full fine-tuning on these three tasks. The paper also reports on-par or better results on RoBERTa, DeBERTa and GPT-2. The WikiSQL numbers vary by about plus or minus 0.5% between runs, so small gaps there mean little.