DL0086 LoRA: Low-Rank Adaptation

What is LoRA, and can you explain the low-rank decomposition idea and why it is parameter-efficient?

Answer

LoRA (Low-Rank Adaptation) freezes every pretrained weight matrix and learns only a rank-constrained update beside it: instead of training W_0 + \Delta W with a full d \times k update, it factors the update as \Delta W = BA with B \in \mathbb{R}^{d \times r}, A \in \mathbb{R}^{r \times k} and r \ll \min(d,k). The motivation is the intrinsic-dimension hypothesis: adapting a large pretrained model to a downstream task moves the weights along a very low-dimensional subspace, so a rank-8 or rank-16 correction captures almost all of the useful change. For one 4096 \times 4096 attention projection, a rank-16 factorization trains 131,072 numbers instead of 16.8M, roughly 0.78% of the matrix. The efficiency that matters in practice is not forward FLOPs, which barely change, but optimizer and gradient state: Adam keeps two moments plus an fp32 master copy per trainable parameter, so shrinking the trainable set by two or three orders of magnitude is what turns a multi-node job into a single-GPU job. At inference the product can be folded back with W' = W_0 + (\alpha/r)BA, so a merged LoRA adds zero latency compared with the original model.

(1) Frozen Base, Additive Branch: the pretrained matrix never receives a gradient; all learning happens in the parallel BA branch whose output is added to the frozen path.
(2) Rank Is The Budget Knob: parameter count is r(d+k) and grows linearly in r, while the full matrix costs dk, so the ratio is tiny for any practical rank.
(3) Zero Initialization On One Factor: A gets a random Gaussian init and B is initialized to zeros, so \Delta W = 0 at step 0 and training starts exactly at the pretrained function.
(4) Memory Savings Come From Optimizer State: gradients and Adam moments exist only for adapter parameters, which is where the 8x reduction in peak training memory comes from, not from cheaper matrix multiplies.
(5) Mergeable And Swappable: a task adapter is a few tens of MB, so many tasks can share one frozen base, either merged per deployment or batched as separate adapters in a single server.

Diagram of a LoRA layer: the input feeds a frozen 4096 by 4096 projection on the upper path and a rank-16 bottleneck A then B then alpha over r scaling on the lower path, with both paths summed into the output

Figure 1: A LoRA layer keeps the frozen 16.8M-parameter projection untouched and routes the input through a rank-16 bottleneck whose 131,072 trainable parameters are added back, scaled by \alpha/r.

Two implementation details do most of the work. The scaling factor \alpha/r decouples the effective update magnitude from the rank, so raising r to add capacity does not silently multiply the learning rate on the adapter branch; the common convention is to fix \alpha and sweep r. The choice of target modules matters more than the rank in most experiments: the original paper adapted only the attention query and value projections, while current practice applies LoRA to all linear layers including the MLP, which raises trainable parameters to about 42M on an 8B model but consistently closes the gap to full fine-tuning. One caveat on merging: if the base weights are quantized, folding a bf16 adapter into a 4-bit matrix reintroduces quantization error, so quantized deployments usually keep the adapter separate.

Mathematical Formulation:
h = W_0 x + \Delta W x
\Delta W = \frac{\alpha}{r} B A
|\Theta| = r(d + k)
\rho = \frac{r(d + k)}{d k}
W' = W_0 + \frac{\alpha}{r} B A

Where:

  • h is the layer output and x \in \mathbb{R}^{k} the input activation for one token.
  • W_0 \in \mathbb{R}^{d \times k} is the frozen pretrained matrix, and \Delta W is the learned correction.
  • B \in \mathbb{R}^{d \times r} and A \in \mathbb{R}^{r \times k} are the trainable factors; the rank r satisfies r \ll \min(d,k), which forces \mathrm{rank}(\Delta W) \leq r.
  • \alpha is a fixed scaling constant, so \alpha/r keeps the update magnitude roughly rank-independent.
  • |\Theta| is the trainable count per adapted matrix and \rho its fraction of the full matrix; with d = k = 4096 and r = 16 this gives 131,072 parameters and \rho \approx 0.0078.
  • Required initial condition: B = 0 with A Gaussian, so \Delta W = 0 before the first step; the last line is the merge applied after training.
Stacked bar chart of training memory in gigabytes for an 8B model: full fine-tuning totals 128 GB from 16 GB base weights, 16 GB gradients and 96 GB optimizer state, LoRA rank 16 totals 16.6 GB, and QLoRA with a 4-bit base totals 4.6 GB

Figure 2: Illustrative training memory for an 8B model before activations: full fine-tuning spends 96 GB on Adam state alone, LoRA with 41.9M trainable parameters needs about 16.6 GB, and a 4-bit frozen base drops that to about 4.6 GB.

PropertyLoRAFull Fine-TuningBottleneck Adapters
Trainable parameters (8B model)41.9M at r=16 on all linear layers, about 0.5%8.0B, all of themComparable order, set by bottleneck width
Added inference latencyNone once merged into the base weightsNoneExtra sequential layers, noticeable at small batch
Peak training memoryAbout 17 GB, or 5 GB with a 4-bit baseAbout 128 GB with Adam in mixed precisionSimilar to LoRA
Multi-task servingHundreds of small adapters over one frozen base, batchableOne 16 GB checkpoint per taskSwappable but cannot be merged away
Quality ceilingMatches full tuning on most instruction and style adaptationUpper bound, clearly better for large domain shiftRoughly on par, less used today

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *