Search

Search papers and lessons

LoRA: Low-Rank Adaptation of Large Language Models

Edward J. Hu, Yelong Shen, Phillip Wallis et al. · 2021

LoRA freezes a pretrained model and trains only a pair of small low-rank matrices per layer, matching full fine-tuning on GPT-3 while storing a fraction of the parameters per task.

On this page

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

TL;DR

Adapting a large pretrained language model to a task usually means updating every weight, which produces a full copy of the model for each task. LoRA keeps the pretrained weights frozen and learns the change to each chosen weight matrix as the product of two thin matrices. Only those thin matrices are trained and stored.

On GPT-3 175B the paper reports results on par with or better than full fine-tuning on three tasks, with 4.7 million trainable parameters instead of 175 billion. After training, the thin matrices can be merged into the frozen weights, so inference costs the same as the original model.

Where you'll see it: parameter-efficient fine-tuning of open language and image models, where one base model is shared and many small adapter files are swapped in per task.

The problem

Full fine-tuning updates all the parameters of a pretrained model. That is fine for a small model, but for GPT-3 with 175 billion parameters each task needs its own 175-billion-parameter copy. Storing, loading and serving many such copies is expensive. Training them also needs optimizer state for every parameter, which for Transformer models trained with Adam takes a large share of GPU memory.

Earlier fixes trade off against quality. Adapter layers insert small modules into the network, but they add depth and so add inference latency. Prompt and prefix methods shrink the usable input length. Methods that train only some existing parameters, such as BitFit, often fall short of full fine-tuning. The paper wants something that is cheap to store, cheap to train, adds no latency, and matches full fine-tuning in quality.

Key idea

The authors guess that the change a model needs for a task has low intrinsic rank. The full update to a weight matrix may be a large matrix, but the useful part could be captured by a few directions. If that is true, the update can be written as a product of two thin matrices, and only those two need training.

flowchart LR
  X["Input x"] --> W0["Frozen W0 x"]
  X --> A["A: r by k, trained"]
  A --> B["B: d by r, trained"]
  B --> S["Scale by alpha over r"]
  W0 --> SUM["Sum"]
  S --> SUM
  SUM --> H["Output h"]

The frozen path computes the original layer. The side path is a narrow bottleneck of width r. The paper's name, low-rank adaptation, refers to that bottleneck.

The method

Low-rank update

Take a pretrained weight matrix W0∈Rd×kW_0 \in \mathbb{R}^{d \times k}. Full fine-tuning would learn an update ΔW\Delta W of the same size, which is dkdk numbers. LoRA constrains the update to

W0+ΔW=W0+BA,B∈Rd×r,A∈Rr×k,W_0 + \Delta W = W_0 + BA, \qquad B \in \mathbb{R}^{d \times r}, \quad A \in \mathbb{R}^{r \times k},

where the rank rr is much smaller than min⁡(d,k)\min(d, k). During training W0W_0 gets no gradient. Only AA and BB are updated. The forward pass for an input xx is

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

Here α\alpha is a constant that does not change with rr, so the scale α/r\alpha / r shrinks as the rank grows. The paper sets α\alpha to the first rank it tries and does not tune it, which reduces the need to retune hyperparameters when rr changes. The paper argues that with Adam, tuning α\alpha is roughly the same as tuning the learning rate when the initialization is scaled to match.

Initialization

AA is initialized from a random Gaussian and BB is set to zero. The product BABA is therefore zero at the start, so the model begins exactly at the pretrained function. Training then moves BB away from zero.

Parameter count

For a single layer, full fine-tuning trains dkdk parameters, and LoRA trains r(d+k)r(d + k). The saving is largest when dd and kk are large and rr is small. Take d=k=12288d = k = 12288, which the paper cites as the width of GPT-3 175B. Full update: 12288×12288=150,994,94412288 \times 12288 = 150{,}994{,}944 parameters. LoRA with r=2r = 2: 2×(12288+12288)=49,1522 \times (12288 + 12288) = 49{,}152 parameters. The ratio is 150,994,944/49,152=3072150{,}994{,}944 / 49{,}152 = 3072, which equals d/(2r)d / (2r).

Over the whole of GPT-3 175B (96 layers), adapting WqW_q and WvW_v at r=1r = 1 gives 2×96×1×(12288+12288)≈4.72 \times 96 \times 1 \times (12288 + 12288) \approx 4.7M trainable parameters, which is the "LoRA 4.7M" row in the results below. At r=4r = 4 the same recipe gives about 18.9M parameters, which is the 35 MB checkpoint quoted next (the paper rounds this to 18M).

Why no latency

LoRA is linear, so once training finishes the product BABA can be added to W0W_0:

W=W0+αrBA.W = W_0 + \frac{\alpha}{r} BA.

Inference then runs the single merged matrix, and the cost matches the original model. Adapter layers cannot do this, because they add sequential computation. Switching tasks means subtracting one BABA and adding another, which is a small and quick operation.

Where LoRA is applied

In a Transformer, the paper adapts only the attention projections WqW_q, WkW_k, WvW_v and WoW_o, and freezes the MLP blocks. It treats WqW_q as a single dmodel×dmodeld_{model} \times d_{model} matrix even though it is split into heads. The authors leave adapting the MLP, LayerNorm and biases to future work. In the experiments, the default is to adapt the query and value projections.

Practical benefits and limits

The paper reports that for a large Transformer trained with Adam, LoRA reduces VRAM use by up to two thirds when rr is much smaller than dmodeld_{model}, because frozen weights need no optimizer state. On GPT-3 175B, training VRAM falls from 1.2 TB to 350 GB. With r=4r = 4 on the query and value projections, the checkpoint shrinks from 350 GB to 35 MB. The paper also reports about a 25 percent training speedup on GPT-3 compared with full fine-tuning.

The limit is batching across tasks. If the adapters are merged into WW, a single batch cannot mix inputs for different tasks. The paper notes that unmerged LoRA modules can be chosen per sample when latency is less important.

Results

Numbers below are from the paper's tables.

RoBERTa and DeBERTa on GLUE (Table 2). LoRA is compared with full fine-tuning and several parameter-efficient baselines. Average GLUE score: RoBERTa-base 87.2 with LoRA (0.3M parameters) against 86.4 for full fine-tuning (125M); RoBERTa-large 89.0 (0.8M) against 88.9 (355M); DeBERTa XXL 91.3 (4.7M) against 91.1 (1.5B). LoRA does not win every task, for example RoBERTa-large full fine-tuning is better on SST-2 and QQP.

GPT-2 on the E2E natural language generation challenge (Table 3). On GPT-2 medium, LoRA with 0.35M parameters scores 70.4 BLEU against 68.2 for full fine-tuning (354.92M). On GPT-2 large, LoRA with 0.77M parameters scores 70.4 BLEU against 68.5.

GPT-3 175B. The paper's Table 4 gives the comparison that matters most:

MethodTrainable parametersWikiSQL accuracyMNLI-m accuracySAMSum R1/R2/RL
Full fine-tuning175,255.8M73.889.552.0/28.0/44.5
BitFit14.2M71.391.051.3/27.4/43.5
Prefix (layer)20.2M70.189.550.8/27.3/43.5
Adapter (H)40.1M73.291.553.2/29.0/45.1
LoRA4.7M73.491.753.8/29.8/45.9
LoRA37.7M74.091.653.4/29.2/45.1

The 4.7M model has about 37,000 times fewer trainable parameters than full fine-tuning (175,255.8M / 4.7M). The abstract instead says "10,000 times", and the introduction ties that figure to the checkpoint size at r=4r = 4 (350 GB to 35 MB), so the two numbers come from different configurations and are both consistent with the tables. Going from 4.7M to 37.7M parameters changes the scores by less than a point and not always upward (MNLI-m and SAMSum drop slightly).

Which matrices to adapt (Table 5). With a fixed budget of 18 million parameters on GPT-3, adapting both WqW_q and WvW_v at rank 4 gives 73.7 on WikiSQL, against 70.4 for WqW_q alone and 70.0 for WkW_k alone at rank 8. Single matrices WvW_v (73.0) and WoW_o (73.2) are close. Adapting all four attention matrices at rank 2 also gets 73.7 on WikiSQL and the best MultiNLI score (91.7).

How small can rr be. Table 6 shows that rank 1 is already competitive when both WqW_q and WvW_v are adapted. On WikiSQL, rank 1 gives 73.4 against 73.8 at rank 8. Adapting WqW_q alone needs a larger rank, and the gap is larger on WikiSQL than on MultiNLI. The paper calls this surprising, and it is the main evidence for the low intrinsic rank hypothesis.

Why it mattered

LoRA became the default way to adapt large models cheaply. Because the adapter is a small file that merges into the base weights, people share one base model and many lightweight adapters. Later toolkits adopted the same pattern widely for open-weight language models. The paper also shipped a PyTorch package and checkpoints for RoBERTa, DeBERTa and GPT-2, which made adoption easy.

The analysis in Section 7 matters as much as the method. The authors ask whether the learned update is really low rank and whether it relates to the pretrained weights. Table 7 (layer 48 of GPT-3, r=4r = 4) projects WqW_q onto the directions of ΔWq\Delta W_q. The projection has Frobenius norm 0.32, more than for random directions (0.02) but far less than for WqW_q's own top singular directions (21.67). So the update overlaps with WqW_q somewhat, yet mostly points along directions the pretrained matrix does not emphasise. The update's norm is 6.91, so it amplifies those directions by roughly 6.91 / 0.32, which the paper quotes as 21.5 (the ratio of the rounded table values is 21.6). The paper reads this as the update amplifying task-relevant features that pretraining learned but did not stress. That reading is the authors' interpretation, not a proven mechanism.

Reading guide

Read the paper in this order, with these prerequisites:

  1. Introduction and problem statement (Sections 1 and 2). Needs Language Model and Fine-Tuning for the adaptation setup. Transfer Learning helps with the motivation.
  2. Existing solutions (Section 3). Needs Attention Mechanism for the Transformer layers the adapters sit in, and Adam Optimizer for the memory discussion.
  3. The method (Section 4). Needs Low-Rank Adaptation (LoRA) and Matrix Rank for the factorization, Matrix Multiplication for the forward pass, and Stochastic Gradient Descent for training. The Gaussian initialization needs Gaussian (Normal) Distribution.
  4. Experiments (Section 5). Needs Transformer for the architectures tested.
  5. Understanding the low-rank updates (Section 7). Needs Matrix Rank for the singular value comparison.

Check yourself

QuestionRecall: what does LoRA train, and what does it keep frozen?Show answer

LoRA keeps every pretrained weight matrix W0W_0 frozen and trains two thin matrices AA and BB for each adapted layer. Their product BABA is the learned update. Only those small matrices receive gradients and are stored per task.

QuestionComputation: a layer has d=k=4096d = k = 4096. How many parameters does a full update train, and how many does LoRA train with r=8r = 8?Show answer

A full update trains 4096×4096=16,777,2164096 \times 4096 = 16{,}777{,}216 parameters. LoRA trains r(d+k)=8×(4096+4096)=65,536r(d + k) = 8 \times (4096 + 4096) = 65{,}536. The ratio is 16,777,216/65,536=25616{,}777{,}216 / 65{,}536 = 256, which equals d/(2r)=4096/16=256d / (2r) = 4096 / 16 = 256.

QuestionWhy: why does initializing BB to zero matter for training?Show answer

With B=0B = 0 the product BABA is zero, so the adapted model equals the pretrained model at the first step. Training starts from the pretrained function rather than from a perturbed one, which avoids damaging the features the model already has. Gradients still reach BB through AA, so BB moves away from zero as training proceeds.

QuestionApplied: you need to serve 100 customer-specific versions of a 175 billion parameter model. Using the paper's storage numbers, why is LoRA cheaper than full fine-tuning, and what is the main serving constraint?Show answer

Full fine-tuning needs 100 full copies, about 100×350100 \times 350 GB, or roughly 35 TB. LoRA needs one 350 GB base model plus 100 adapters of about 35 MB each, roughly 354 GB in total. The constraint is batching: if adapters are merged into the base weights, a single batch cannot mix customers. Serving unmerged adapters chosen per sample avoids that, at some latency cost.

Sources

  • Hu, E. J., Shen, Y., Wallis, P., Allen-Zhu, Z., Li, Y., Wang, S., Wang, L., and Chen, W. (2021). LoRA: Low-Rank Adaptation of Large Language Models. arXiv:2106.09685
  • Brown, T. et al. (2020). Language Models are Few-Shot Learners (GPT-3). arXiv:2005.14165. Cited in the paper for the 175 billion parameter model.

Read the full paper

Edward J. Hu, Yelong Shen, Phillip Wallis et al., 2021. arXiv:2106.09685