Skip to content
HasanAI
Log in Sign up

🧩 LoRA: Low-Rank Adaptation

Freeze the model, learn a rank-r update: 10,000x fewer trainable parameters
Ten Landmark Papers Intermediate
Contents

    LoRA: Low-Rank Adaptation (Hu et al., 2021)

    Fine-tuning GPT-3 the ordinary way means updating all 175 billion weights and saving a new 350 GB copy for every task. LoRA's observation is that the change a fine-tune makes to a weight matrix has low intrinsic rank. You do not need to learn a full $d \times k$ update. Two thin matrices whose product has rank $r$ are enough:

    $$h = W_0 x + \Delta W x = 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)$$

    $W_0$ is frozen. Only $A$ and $B$ train. $A$ starts random and $B$ starts at zero, so at step 0 the adapted model is exactly the pre-trained one.

    1. How many parameters this saves, from a 64-dim toy to GPT-3
    2. A controlled test: a target update of rank 4, learned with r = 1 to 16
    3. Does full fine-tuning produce low-rank updates on its own? An SVD check
    4. Fine-tuning a small network on new classes: LoRA vs head-only vs full
    5. Merging: zero extra inference latency, verified
    6. QLoRA, and the knobs that matter in practice
    helpers ready

    1. The parameter arithmetic

    A full update of a $d \times k$ matrix has $dk$ parameters. LoRA has $r(d + k)$. For square matrices that is a fraction $2r/d$ of the original, so the larger the model, the larger the saving.

    The paper's headline setting is GPT-3 175B ($d_{model} = 12{,}288$, 96 layers) with LoRA on only the query and value projections, at $r = 4$.

    ==============================================================================
      TRAINABLE PARAMETERS
    ==============================================================================
          64 x 64     full        4,096   LoRA r=4:      512   (12.500% of full)
          64 x 64     full        4,096   LoRA r=8:    1,024   (25.000% of full)
         768 x 768    full      589,824   LoRA r=4:    6,144   ( 1.042% of full)
         768 x 768    full      589,824   LoRA r=8:   12,288   ( 2.083% of full)
        4096 x 4096   full   16,777,216   LoRA r=4:   32,768   ( 0.195% of full)
        4096 x 4096   full   16,777,216   LoRA r=8:   65,536   ( 0.391% of full)
       12288 x 12288  full  150,994,944   LoRA r=4:   98,304   ( 0.065% of full)
       12288 x 12288  full  150,994,944   LoRA r=8:  196,608   ( 0.130% of full)
    
    GPT-3 175B, LoRA on W_q and W_v in all 96 layers, r = 4
    ------------------------------------------------------------------------------
      trainable: 96 layers x 2 matrices x 4 x (12288 + 12288) = 18,874,368
      saved per task in fp16: 37.7 MB   vs  full model 350 GB
      reduction in trainable parameters: 9,272x
      (paper: '10,000 times' fewer trainable parameters, checkpoint 350 GB -> 35 MB)

    2. A controlled test: the update really is rank 4

    To see what rank buys, build the situation LoRA assumes, where the truth is known. A frozen "pre-trained" layer $W_0$ (64Γ—64). A new task whose ideal weight is $W_0 + \Delta W^*$, where $\Delta W^*$ has rank exactly 4, plus a little full-rank noise. Then fine-tune it with full updates and with LoRA at ranks 1, 2, 4, 8 and 16.

    The prediction is that ranks 1 to 3 cannot represent the update and plateau. Rank 4 and above should remove essentially all of the error. The only part they should miss is the small full-rank noise, which by construction only a full update can fit.

    ==============================================================================
      LEARNING A RANK-4 UPDATE
    ==============================================================================
      singular values of the true update: [6.568 5.051 3.925 2.824 0.    0.   ]
      validation MSE of the frozen pre-trained layer: 1.42505
    
    LoRA starts as an exact copy of the pre-trained layer (B = 0)
    ------------------------------------------------------------------------------
      [PASS] LoRA output at step 0 == W0 x
    
    Validation MSE after 1,500 steps
    ------------------------------------------------------------------------------
      full        trainable 4,096   MSE 0.000000   (  0.00% of the frozen layer's error)
      LoRA r=1    trainable   128   MSE 0.765971   ( 53.75% of the frozen layer's error)
      LoRA r=2    trainable   256   MSE 0.369708   ( 25.94% of the frozen layer's error)
      LoRA r=4    trainable   512   MSE 0.000091   (  0.01% of the frozen layer's error)
      LoRA r=8    trainable 1,024   MSE 0.000086   (  0.01% of the frozen layer's error)
      LoRA r=16   trainable 2,048   MSE 0.000075   (  0.01% of the frozen layer's error)
      [PASS] rank 1-3 cannot fit a rank-4 update: r=2 error is > 10x the r=4 error
    
      error contributed by the full-rank noise term alone: 0.000100
      [PASS] r >= 4 removes > 99.9% of the frozen layer's error
      [PASS] what r = 4 leaves is the full-rank noise, which no rank-4 update can express (within 20%)

    LoRA as a picture: a frozen matrix, and next to it two thin trainable matrices whose product is the update. The cells below are the top-left 12Γ—12 corner of the real $W_0$, with the first 12 rows of $B$ and the first 12 columns of $A$ from the $r = 2$ run above, frame by frame as they train.

    3. Does full fine-tuning find a low-rank update by itself?

    The paper's justification goes further than "low rank is enough". It argues that the updates found by ordinary fine-tuning are themselves close to low rank. On the toy task, take the update full fine-tuning learned, $W_{\text{final}} - W_0$, and look at its singular values as training progresses.

    ==============================================================================
      SINGULAR VALUES OF THE FULL FINE-TUNING UPDATE
    ==============================================================================
      top 8 singular values: [6.5659 5.0522 3.9252 2.8259 0.0193 0.0179 0.0171 0.017 ]
      share of the update's energy in the top 1/2/4/8 directions: 0.468 / 0.746 / 1.000 / 1.000
      [PASS] over 99% of the full update lies in 4 of 64 directions

    That was a constructed case, where the truth was rank 4 by design. A more realistic test is a small network pre-trained on one task and fine-tuned on another. Here a 2-hidden-layer MLP is pre-trained on handwritten digits 0 to 4, then adapted to tell apart digits 5 to 9, which it has never seen. Each method gets a fresh output head:

    • Head only: freeze the whole body
    • LoRA r = 1, 2, 4, 8 on both hidden layers, plus the head
    • Full fine-tuning of everything
    ==============================================================================
      PRE-TRAIN ON 0-4, ADAPT TO 5-9
    ==============================================================================
      pre-training set: 901 images of 0-4   fine-tuning set: 150   test: 448
      pre-trained accuracy on 0-4 (train set): 1.000
      head only  trainable    645   test accuracy on 5-9: 0.760 Β± 0.008  (3 seeds)
      LoRA r=1   trainable  1,093   test accuracy on 5-9: 0.940 Β± 0.011  (3 seeds)
      LoRA r=2   trainable  1,541   test accuracy on 5-9: 0.952 Β± 0.003  (3 seeds)
      LoRA r=4   trainable  2,437   test accuracy on 5-9: 0.967 Β± 0.003  (3 seeds)
      LoRA r=8   trainable  4,229   test accuracy on 5-9: 0.966 Β± 0.005  (3 seeds)
      full       trainable 25,477   test accuracy on 5-9: 0.975 Β± 0.002  (3 seeds)
      [PASS] LoRA r=4 beats head-only
    
      full fine-tune, layer 2 update: energy in top 4 / 8 / 16 of 128 directions = 0.902 / 0.971 / 0.989

    The realistic case is less clean than the constructed one, and the numbers say so. Full fine-tuning's update is concentrated but not rank 4. LoRA at small ranks recovers most of the gap between "head only" and "full fine-tuning", and the paper's GPT-3 experiments show the same: $r$ as small as 1 to 4 on the attention projections came close to full fine-tuning on most benchmarks, and sometimes beat it.

    5. Merging: no added latency

    Adapters add layers, and therefore time, to every forward pass. LoRA does not have to. After training, compute $W = W_0 + \frac{\alpha}{r}BA$ once, and the model is an ordinary model again. To switch tasks, subtract one update and add another.

    ==============================================================================
      MERGE, THEN UNMERGE
    ==============================================================================
      [PASS] merged layer output == W0 x + scale * B A x
      [PASS] subtracting the update recovers W0 exactly (to float precision)
      matmuls per forward pass: unmerged 3 (W0, A, B), merged 1

    6. QLoRA and the knobs that matter

    QLoRA (Dettmers et al., 2023) stores the frozen $W_0$ in 4-bit NormalFloat and trains LoRA adapters in 16-bit on top. Because the base weights never change, quantising them costs little, and the paper fine-tuned a 65B model on a single 48 GB GPU.

    In practice:

    • Which matrices. The paper used $W_q$ and $W_v$. Common practice now applies LoRA to all linear layers, including the MLP, which usually helps more than raising $r$.
    • Rank $r$: 8 to 64 is typical. The toy test shows why too low a rank plateaus. Beyond the update's real rank, extra rank adds parameters and little else.
    • $\alpha$: the update is scaled by $\alpha / r$, so changing $r$ does not silently change the effective learning rate. A common default is $\alpha = 2r$.
    • Initialisation: $B = 0$ is not optional. It is what makes step 0 identical to the pre-trained model.

    Summary

    • LoRA freezes $W_0$ and learns $\Delta W = \frac{\alpha}{r} BA$, which has $r(d+k)$ parameters instead of $dk$. For GPT-3 at $r = 4$ on $W_q, W_v$ that is 18.9M trainable parameters, about 9,000Γ— fewer, and a checkpoint of about 38 MB.
    • When the needed update has rank 4, LoRA at rank 1 to 2 plateaus. Rank 4 removes 99.99% of the error, and what remains is exactly the full-rank part no rank-4 update can express. This is measured, not assumed.
    • Full fine-tuning concentrates its update in a few directions: 100% of the energy in 4 directions in the constructed case, and 90% in 4 of 128 in the digits network. On new digit classes, LoRA r = 4 reached 96.7% against 97.5% for full fine-tuning and 76% for the head alone, with a tenth of the trainable parameters.
    • Merged LoRA adds zero inference cost, and unmerging switches tasks.

    References

    1. LoRA: Low-Rank Adaptation of Large Language Models β€” Hu et al., 2021 (ICLR 2022)
    2. QLoRA: Efficient Finetuning of Quantized LLMs β€” Dettmers et al., 2023