LoRA: Low-Rank Adaptation

Volume III, Chapter 14 — Part I. Low-rank decomposition of weight updates: theoretical motivation from intrinsic dimension, LoRA formulation, merge at inference, and connection to the SVD.

Intermediate

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. The Full Fine-Tuning Problem
  6. Low-Rank Hypothesis
  7. LoRA Formulation
  8. Parameter Count and Efficiency
  9. Where to Apply LoRA
  10. Training and Merging
  11. Connection to SVD
  12. Worked Examples
  13. Connection to the Broader Curriculum
  14. Common Pitfalls and Misconceptions
  15. Research Perspective
  16. Summary of Takeaways
  17. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. State the LoRA weight update W=W+BA\mathbf{W}' = \mathbf{W} + \mathbf{B}\mathbf{A} with rank-rr decomposition.
  2. Compute LoRA parameter count vs. full fine-tuning.
  3. Explain the low-rank hypothesis for adaptation tasks.
  4. Identify which layers (Q/K/V/O projections) LoRA typically targets.
  5. Describe weight merging at inference: W=W+BA\mathbf{W}' = \mathbf{W} + \mathbf{B}\mathbf{A}.

Prerequisites


Notation

  • W0Rd×k\mathbf{W}_0 \in \mathbb{R}^{d \times k} — Frozen pretrained weight
  • BRd×r,ARr×k\mathbf{B} \in \mathbb{R}^{d \times r}, \mathbf{A} \in \mathbb{R}^{r \times k} — Low-rank adapter factors
  • rr — LoRA rank (rmin(d,k)r \ll \min(d,k))
  • α\alpha — LoRA scaling hyperparameter
  • ΔW=BA\Delta \mathbf{W} = \mathbf{B}\mathbf{A} — Low-rank weight update

Core Intuition

Fine-tuning a 70B-parameter LLM requires updating all weights — prohibitive in memory and storage. LoRA (Hu et al., 2021) freezes pretrained weights W\mathbf{W} and learns a low-rank update ΔW=BA\Delta \mathbf{W} = \mathbf{B}\mathbf{A} where BRd×r\mathbf{B} \in \mathbb{R}^{d \times r}, ARr×k\mathbf{A} \in \mathbb{R}^{r \times k}, rmin(d,k)r \ll \min(d,k).

The hypothesis: task-specific adaptation lives in a low-dimensional subspace of weight space — supported by intrinsic dimension studies and SVD theory.

Series context. Volume III, Chapter 14 (Fine-Tuning).

Low-Rank Adaptation (LoRA)

W (frozen)+B (8×4)×64 trainable vs 64 full · 0% reduction
Rank r
4
Explore: LoRA fine-tunes W' = W + BA where B and A are low-rank matrices (d×r and r×d). Low rank r dramatically reduces trainable parameters while matching full fine-tune quality.

The Full Fine-Tuning Problem

Definition 1 (Full Fine-Tuning). Update all parameters θ\boldsymbol{\theta} of pretrained model:

θ=θαθL.(1)\boldsymbol{\theta}' = \boldsymbol{\theta} - \alpha \nabla_{\boldsymbol{\theta}} \mathcal{L}. \tag{1}

Proposition 1. For θ=N|\boldsymbol{\theta}| = N, fine-tuning requires storing NN optimizer states + NN gradients — typically 3N3N additional memory.

For N=70×109N = 70 \times 10^9, FP16: 420\sim 420 GB — infeasible on consumer hardware.


Low-Rank Hypothesis

Definition 2 (Intrinsic Rank). The effective dimension of task-specific weight updates is much smaller than ambient dimension.

Proposition 2 (Eckart–Young). Best rank-rr approximation to ΔW\Delta \mathbf{W} in Frobenius norm comes from top-rr SVD components.

LoRA parameterizes ΔW\Delta \mathbf{W} directly as low-rank product rather than computing SVD post-hoc.


LoRA Formulation

Definition 3 (LoRA Layer). For linear layer W0Rd×k\mathbf{W}_0 \in \mathbb{R}^{d \times k}:

h=W0x+ΔWx=W0x+BAx,(2)\mathbf{h} = \mathbf{W}_0 \mathbf{x} + \Delta \mathbf{W} \mathbf{x} = \mathbf{W}_0 \mathbf{x} + \mathbf{B}\mathbf{A}\mathbf{x}, \tag{2}

where BRd×r\mathbf{B} \in \mathbb{R}^{d \times r}, ARr×k\mathbf{A} \in \mathbb{R}^{r \times k}, rank rmin(d,k)r \ll \min(d, k).

Initialization: A\mathbf{A} random Gaussian, B=0\mathbf{B} = \mathbf{0} — so ΔW=0\Delta \mathbf{W} = \mathbf{0} at start (pretrained behavior preserved).

Scaling: Often ΔWx(α/r)BAx\Delta \mathbf{W} \mathbf{x} \leftarrow (\alpha/r) \mathbf{B}\mathbf{A}\mathbf{x} with hyperparameter α\alpha.


Parameter Count and Efficiency

Proposition 3. LoRA parameters for one layer: r(d+k)r(d + k) vs. full: dkdk.

Example. d=k=4096d = k = 4096, r=16r = 16: LoRA =16×8192=131= 16 \times 8192 = 131K vs. full =16.8= 16.8M — 128× reduction per layer.

For entire Transformer: apply to Q/K/V/O projections across LL layers.


Where to Apply LoRA

Standard targets: WQ,WK,WV,WO\mathbf{W}_Q, \mathbf{W}_K, \mathbf{W}_V, \mathbf{W}_O in Self-Attention.

Optional: MLP layers (Wup,Wdown\mathbf{W}_{\text{up}}, \mathbf{W}_{\text{down}}), embedding layer.

Proposition 4. Applying LoRA to all attention projections captures most adaptation benefit at modest parameter cost.


Training and Merging

Training: Only A,B\mathbf{A}, \mathbf{B} receive gradients; W0\mathbf{W}_0 frozen.

Inference merge:

W=W0+BA.(3)\mathbf{W}' = \mathbf{W}_0 + \mathbf{B}\mathbf{A}. \tag{3}

Merged model has zero inference overhead — same architecture as base model.

Multi-adapter: Keep separate (Bi,Ai)(\mathbf{B}_i, \mathbf{A}_i) per task; swap at runtime without merging.


Connection to SVD

Proposition 5. If optimal ΔW\Delta \mathbf{W}^* has rank rr^*, LoRA with rrr \geq r^* can represent ΔW\Delta \mathbf{W}^* exactly.

Proposition 6. LoRA is a reparameterization of rank-rr matrices via product manifold — not unique factorization (unlike SVD).

See SVD for optimal low-rank approximation theory.


Worked Examples

Example 1: Parameter Savings

Llama 7B: 32 layers, 4 projections, d=4096d = 4096, r=8r = 8. LoRA params 32×4×8×81928.4\approx 32 \times 4 \times 8 \times 8192 \approx 8.4M vs. 7B full.

Example 2: Merge

After training, W=W0+BA\mathbf{W}' = \mathbf{W}_0 + \mathbf{B}\mathbf{A} — single matrix multiply at inference.


Connection to the Broader Curriculum


Common Pitfalls and Misconceptions

Pitfall 1: rr too small → underfitting; too large → no savings.

Pitfall 2: Forgetting scaling factor α/r\alpha/r.

Pitfall 3: LoRA on all layers may not beat selective application.

Pitfall 4: Merged weights lose adapter modularity.


Research Perspective

LoRA (Hu et al., 2021). QLoRA (Dettmers et al., 2023). DoRA, AdaLoRA, VeRA. Standard for DPO and instruction tuning.


Summary of Takeaways

  • LoRAW=W0+BA\mathbf{W}' = \mathbf{W}_0 + \mathbf{B}\mathbf{A}
  • Paramsr(d+k)r(d + k) per layer
  • Merge — Zero inference overhead
  • Basis — Low-rank / SVD

Next: KV Cache


Exercises

Exercise 1. Compute LoRA params for given d,k,r,Ld, k, r, L.

Exercise 2. Prove rank(BA)r\text{rank}(\mathbf{B}\mathbf{A}) \leq r.

Exercise 3. Why initialize B=0\mathbf{B} = \mathbf{0}?

Exercise 4. Compare LoRA to top-rr SVD of full ΔW\Delta \mathbf{W}.

Exercise 5. Gradient flow: which params update in LoRA?

Exercise 6. LoRA + Quantization (QLoRA concept).

Exercise 7. Apply LoRA to Self-Attention Q projection only.

Exercise 8. Storage for 10 task-specific LoRA adapters vs. 10 full models.