Skip to content

LoRA: Low-Rank Adaptation of Large Language Models

Skip →Model soups & weight averaging
main track #10corefundamentalpaperrubric 11/122021 · ~45 min

Edward J. Hu, Yelong Shen, Phillip Wallis, …, Weizhu Chen

Status

TL;DR. Freeze the pretrained weight W0W_0 and learn its update as ΔW=BA\Delta W = BA, where BB is d×rd \times r, AA is r×kr \times k and rr is tiny. On GPT-3 175B this cuts trainable parameters by 10,000× and training memory by 3×, matches or beats full fine-tuning, and after merging BABA into W0W_0 costs nothing at inference.

Why it matters now. LoRA is the default way an academic lab fine-tunes a 7B–70B model: rerankers, embedders, RAG readers, LLM judges and RL policies. It also lets one base model serve many tasks by swapping small adapters. Knowing where the rank goes and how the scaling works saves many wasted runs.

For a frozen weight W0∈Rd×kW_0 \in \mathbb{R}^{d \times k}, LoRA adds a trainable low-rank path:

h=W0x+αr BAx,B∈Rd×r,  A∈Rr×k,  r≪min⁡(d,k).h = W_0 x + \frac{\alpha}{r}\, B A x, \qquad B \in \mathbb{R}^{d \times r},\; A \in \mathbb{R}^{r \times k},\; r \ll \min(d, k).

AA starts as random Gaussian and BB as zero, so ΔW=0\Delta W = 0 at the start and training begins from the pretrained model. The trainable parameters per matrix are r(d+k)r(d+k) instead of dkdk. The factor α/r\alpha/r keeps the effective step size stable when rr changes; the paper sets α\alpha to the first rr tried and never tunes it. At deployment, W=W0+αrBAW = W_0 + \frac{\alpha}{r} BA is a single matrix.

The bet is that the update has a low intrinsic rank. As rr grows towards the rank of W0W_0, training all matrices with LoRA approaches full fine-tuning, so LoRA is a generalisation rather than a different model.

LoRA: a frozen weight plus a trainable low-rank bypass The input x of width k goes through two parallel paths. The left path is the frozen pretrained weight W0 of size d by k. The right path first multiplies by A, an r by k matrix initialised with Gaussian noise, then by B, a d by r matrix initialised to zero, and scales the result by alpha over r. The two outputs are added to give h. Because B starts at zero, the update BA is zero at the start, so training begins exactly at the pretrained model. A side panel compares parameter counts for one 4096 by 4096 matrix: about 16.8 million for full fine-tuning against 65,536 for rank 8. At deployment BA can be added into W0, so there is no extra latency. h = W₀x + (α/r)·BAx h (width d) + W₀ (d × k) frozen, no gradients no optimizer state B d × r init = 0 r (tiny) A r × k init ~ Gaussian × α/r x (width k) One 4096 × 4096 matrix full update: d·k = 16.8M LoRA r = 8: r(d + k) = 65,536 → 0.39% of the matrix Why B = 0 at init BA = 0, so step 0 is the pretrained model exactly; ∂L/∂A = 0 at first, B moves first Why α/r α is fixed to the first r tried; less re-tuning of the LR when r changes (Adam) Deploy: W = W₀ + (α/r)BA
Original sketch. The input passes through the frozen W₀ and a trainable rank-r bypass B·A, scaled by α/r. B starts at zero, so the first step is the pretrained model.
  • Table 4 (GPT-3 175B): full fine-tuning gets 73.8 on WikiSQL and 89.5 on MNLI-m. LoRA with 4.7M parameters gets 73.4 and 91.7; with 37.7M, 74.0 and 91.6. Prefix-based methods drop when they use more than 256 (prefix-embedding) or 32 (prefix-layer) special tokens.
  • Table 3 (GPT-2 medium, E2E NLG): LoRA with 0.35M parameters reaches BLEU 70.4, against 68.2 for full fine-tuning.
  • Storage: with r=4r = 4 on Wq,WvW_q, W_v, a GPT-3 checkpoint shrinks from 350GB to 35MB. Training throughput rises by 25% (43.1 vs 32.5 tokens/s per V100).
  • §7: Tables 5–6 on where to put the rank and how much is needed. Table 7 shows ΔWq\Delta W_q amplifies directions that WqW_q does not emphasise, by a factor of about 21.5 at r=4r = 4.
  • Batching inputs from different tasks in one forward pass is awkward if each task’s BABA is merged into the weights.
  • The paper adapts only attention matrices and picks which ones by heuristics. It leaves open a principled way to choose the matrices.
  • Low intrinsic rank is shown for tasks close to pretraining. Whether it holds for distant objectives, such as contrastive embedding training, is open.
  • LoRA still needs the full-precision frozen model in memory. QLoRA removes most of that cost.
  • Next: QLoRA trains LoRA adapters on a 4-bit base model; knowledge distillation is the other main way to specialise a model cheaply.
  • Pretrained bases: BERT, T5 and GPT-3 are the models LoRA was tested on.
  • IR use: low-rank updates are a cheap way to build domain rerankers on a ColBERT-style or cross-encoder base, and to train LLM judges.
  • RL: the same frozen-base, small-update idea is one way to fit PPO-style policy training into an academic GPU budget.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    For one 4096 × 4096 projection, compare the trainable parameters of a full update with LoRA at r = 8. Then take the paper's GPT-3 setting: an 18M-parameter budget equals r = 8 on one attention matrix type across all 96 layers. Back out the implied model width, check the paper's 35MB fp16 checkpoint figure, and say what r gives the same budget on all four attention matrices.

    What a strong answer covers
    • Full update: d·k = 4096² ≈ 16.8M. LoRA: B is d × r and A is r × k, so r(d + k) = 8 × 8192 = 65,536, about 0.39% of the matrix.
    • Budget: 96 layers × r(d + k) with r = 8 and d = k gives 96 × 8 × 2d = 18M, so d ≈ 11.7K, a model width of roughly 12K.
    • Storage: 18M parameters × 2 bytes ≈ 36MB, consistent with the paper's "roughly 35MB if stored in FP16".
    • Same budget on four matrix types: 4 × r × 2d × 96 = 18M gives r = 2, which is the last column of Table 5; two types give r = 4.
    • Optimizer state scales with trainable parameters, which is where most of the 1.2TB → 350GB VRAM saving comes from.
  2. Mechanism

    LoRA computes h = W₀x + (α/r)·BAx with A initialised from a Gaussian and B set to zero. Derive the gradients of the loss with respect to A and B at the first step, explain why this initialisation is chosen, what the α/r factor buys, and why LoRA adds no inference latency while adapter layers do.

    What a strong answer covers
    • With upstream gradient g = ∂L/∂h: ∂L/∂B = (α/r)·g·(Ax)ᵀ and ∂L/∂A = (α/r)·Bᵀ·g·xᵀ. At step 0, B = 0 so ∂L/∂A = 0, while ∂L/∂B is non-zero because Ax is a random projection. B moves first, then A starts receiving gradient.
    • B = 0 makes ΔW = BA = 0, so training starts exactly at the pretrained model. Making both zero would give zero gradients for both.
    • α/r: with Adam, tuning α is roughly the same as tuning the learning rate. The paper fixes α to the first r tried and does not tune it, which reduces re-tuning when r changes.
    • Latency: at deployment W = W₀ + (α/r)BA is computed once, so the forward pass is a single matmul as before. Adapters add sequential layers; Table 1 shows +20.7% and +30.3% latency for GPT-2 medium at batch size 1 and sequence length 128.
    • Task switching: subtract BA and add another adapter's B′A′, so many tasks can share one copy of W₀ in memory.
  3. What if…

    Using Tables 5 and 6 (GPT-3, WikiSQL and MultiNLI), explain what the paper learns about where to put the rank and how much rank is needed. Why does r = 1 on Wq and Wv nearly match r = 64, while Wq alone needs more?

    What a strong answer covers
    • Table 5, fixed 18M budget: Wq at r = 8 gives 70.4 / 91.0, Wv at r = 8 gives 73.0 / 91.0, Wq and Wv at r = 4 give 73.7 / 91.3, all four at r = 2 give 73.7 / 91.7. Spreading rank over more matrices beats concentrating it.
    • Table 6: Wq and Wv at r = 1 give 73.4 / 91.3 and at r = 64 give 73.5 / 91.4, so extra rank adds nothing. Wq alone goes from 68.8 at r = 1 to 70.5 at r = 4 on WikiSQL.
    • Subspace analysis: the top singular direction learned with r = 8 and r = 64 overlaps with normalised similarity above 0.5, while lower directions differ, suggesting the extra directions are mostly noise.
    • ΔWq appears to have a higher intrinsic rank than ΔWv across random seeds, matching the Wq-alone result.
    • Caveat: these are two tasks on one huge model. On GPT-2 medium (Table 18) performance peaks at r = 16 for validation loss and r = 4 for BLEU.
  4. Critique

    Table 7 projects Wq onto the top singular directions of ΔWq and finds an amplification factor of about 21.5. Explain how that number is computed, what it suggests about what LoRA learns, and what the evidence does not show.

    What a strong answer covers
    • At layer 48 with r = 4, ‖ΔWq‖_F = 6.91 while ‖UᵀWqVᵀ‖_F = 0.32 when U, V come from ΔWq. The ratio 6.91 / 0.32 ≈ 21.5 says ΔW strongly amplifies directions that W barely uses.
    • Projecting onto W's own top directions gives 21.67 and onto random directions 0.02, so ΔW is related to W but does not repeat its top directions.
    • Interpretation in the paper: adaptation amplifies task-specific features already present but not emphasised in the pretrained model.
    • Limits: one layer, one matrix type, one model, and adaptation tasks that are close to pretraining. Low intrinsic rank may fail for tasks far from pretraining, such as new languages or new retrieval objectives; the paper notes that adapting to hard tasks will need more trainable parameters.
  5. Design

    You maintain a cross-encoder reranker (a 7B decoder) and want separate domain versions for biomedical, legal, code and 20 enterprise customers. Design a LoRA training and serving setup, choosing which matrices to adapt, the rank and the serving strategy, and say how you would check that a domain adapter has not hurt general ranking quality.

    What a strong answer covers
    • Train one adapter per domain on frozen shared weights. Start with all attention matrices at small r, following Tables 5–6; widen r or add MLP matrices only if validation nDCG saturates below the full fine-tuning baseline.
    • Storage arithmetic, following the paper's GPT-3 example: 100 adapters at 35MB plus one 350GB base is about 354GB, versus 35TB for 100 full copies. The same logic applies at 7B scale.
    • Serving: either merge per domain (no latency, one domain per replica) or keep the base in memory and swap adapters per request batch, at the cost of the extra BAx computation unless merged.
    • Checks: in-domain nDCG@10 against full fine-tuning, plus a held-out general benchmark (e.g. a BEIR-style mix) with and without the adapter to detect regressions.
    • Hyperparameters: keep α fixed when sweeping r, as the paper does, so learning-rate tuning carries over.
  6. Research idea

    LoRA's evidence for low intrinsic rank comes from generation and classification tasks. Propose an experiment to test whether the weight update needed to turn a decoder LLM into a dense retriever (contrastive training with in-batch negatives) is also low rank, and what result would change practice.

    What a strong answer covers
    • Train the same embedding model with full fine-tuning and with LoRA at r ∈ {1, 4, 16, 64, 256} on a fixed contrastive recipe; evaluate nDCG@10 and recall@100 on a held-out retrieval benchmark.
    • Compute the singular-value spectrum of the full fine-tuning update ΔW per layer, and the subspace similarity φ between LoRA runs of different ranks and seeds, as in §7.2.
    • If quality keeps rising with r and the full update spectrum is flat, retrieval adaptation is not low rank, and full fine-tuning or larger ranks are justified for embedders.
    • If small r matches full fine-tuning, cheap per-domain retrievers become practical, and the amplified directions (as in Table 7) could be inspected to see what the update emphasises.

Done with this page?

Status