4.3.8Pretraining & Fine-Tuning LLMs

LoRA and QLoRA

1,957 words9 min readdifficulty · medium1 backlinks

Parameter-efficient fine-tuning: adapt a huge frozen model by training a tiny number of extra parameters.

The core problem

WHY we need this: cost, storage (one adapter per task = few MB vs full model copies), and speed. WHAT LoRA does: freezes original weights, injects a trainable low-rank correction. HOW QLoRA extends it: it also quantizes the frozen base to 4-bit so it fits on a small GPU.


Deriving LoRA from scratch

A linear layer computes h=W0xh = W_0 x where W0Rd×kW_0 \in \mathbb{R}^{d\times k}.

Fine-tuning wants a new weight W=W0+ΔWW = W_0 + \Delta W. The full update ΔW\Delta W has d×kd\times k parameters — as many as W0W_0.

Why this factorization? Any matrix of rank rr can be written as a sum of rr outer products. BABA is exactly that: rr columns of BB times rr rows of AA. So BABA can represent any rank-r\le r matrix, and nothing more — that's the whole point (we deliberately restrict ourselves).

Parameter count: instead of dkd\cdot k, we train only r(d+k)r(d+k). For d=k=4096d=k=4096, r=8r=8: full =16.7= 16.7M vs LoRA =65k= 65\text{k} — a 256× reduction.

Why B=0B=0 at init? If both A,BA,B were random, the model output would be corrupted at step 0. Starting with ΔW=0\Delta W = 0 means the first gradient step is a pure improvement direction.

Why divide by rr? As you raise rr, the sum BAxBAx accumulates more terms and grows. Dividing by rr normalizes so you don't have to re-tune the learning rate every time you change rank.

The magic at inference

After training you can merge: WW0+αrBAW \leftarrow W_0 + \frac{\alpha}{r}BA. Now it's a normal weight matrix — zero extra latency. This is LoRA's killer feature vs adapter layers (which add compute).

Figure — LoRA and QLoRA

QLoRA = Quantization + LoRA

QLoRA's goal: fine-tune a 65B model on a single 48GB GPU. Three ingredients:

The key trick: the base weights are stored in 4-bit (frozen). During the forward pass they are dequantized to bf16 on the fly just for the matmul. Only the LoRA matrices A,BA,B are kept in full precision and receive gradients.

Why quantizing the frozen base is safe: we're not backpropagating into it. Quantization noise on frozen weights is a fixed, small perturbation the adapters learn to compensate for. You'd never quantize weights you're trying to train precisely.


Worked examples


Common mistakes (steel-manned)


Flashcards

What low-rank factorization does LoRA use for the weight update?
ΔWBA\Delta W \approx BA with BRd×rB\in\mathbb{R}^{d\times r}, ARr×kA\in\mathbb{R}^{r\times k}, rmin(d,k)r\ll\min(d,k).
How many parameters does rank-rr LoRA train per d×kd\times k matrix?
r(d+k)r(d+k) instead of dkd\cdot k.
Why is BB initialized to zero?
So ΔW=BA=0\Delta W = BA = 0 at start; training begins exactly at the pretrained model with no output corruption.
Why divide by rr in αrBA\frac{\alpha}{r}BA?
Keeps update magnitude roughly independent of rank, so learning rate need not be retuned per rank.
What is the inference-latency cost of LoRA after merging?
Zero — merge W0+αrBAW_0+\frac{\alpha}{r}BA into one matrix; it's a normal linear layer.
What are QLoRA's three innovations?
4-bit NormalFloat (NF4), Double Quantization, Paged Optimizers.
In QLoRA, which parts are quantized and which receive gradients?
The frozen base weights are 4-bit; only the full-precision LoRA adapters A,BA,B receive gradients.
Why is quantizing the frozen base safe?
No gradients flow into it; adapters learn to compensate for the fixed quantization noise.
What is NF4 optimal for and why?
Normally-distributed data; its bins are quantiles of a normal distribution, matching the weight distribution.
Rule of thumb linking α\alpha and rr?
Set α2r\alpha \approx 2r to keep effective update strength consistent.

Recall Feynman: explain to a 12-year-old

Imagine a huge finished LEGO castle (the pretrained model). You want to change it for a birthday party, but rebuilding the whole castle is crazy expensive. Instead you add a tiny set of removable decorations (the small A,BA,B matrices) — cheap to make, easy to store, and you can swap them for different parties. LoRA = tiny decorations. QLoRA = you also shrink the original castle bricks (4-bit) so the whole thing fits on your small table, while still decorating with high-quality add-ons.

Connections

Concept Map

too costly, 80+GB optimizer

solved by

freezes

learns

factored into

rank r much less than d,k

gives

B init zero

scaled by alpha over r

merge W0 plus BA

extended by

quantizes base to 4-bit NF4

Full fine-tuning

Adaptation cost problem

LoRA low-rank update

Original weights W0

Update dW approx BA

Thin matrices B and A

r times d+k params

256x fewer params

Starts at pretrained model

Rank-independent magnitude

Zero extra latency

QLoRA

Fits on single 48GB GPU

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek bada LLM (jaise 7B parameters) ko poora fine-tune karna bahut mehenga hai — Adam optimizer ke saath ~80GB memory chahiye, sirf ek task ke liye. LoRA ka idea simple hai: original weights ko freeze kar do, aur weight ka jo change (ΔW\Delta W) chahiye usko ek chhoti si low-rank form mein likho: ΔW=BA\Delta W = BA, jahan BB aur AA patli matrices hain (rank rr bahut chhota). Isse parameters d×kd\times k se ghat kar r(d+k)r(d+k) ho jaate hain — 100x se zyada kami. BB ko zero se init karte hain taaki shuru mein model bilkul pretrained jaisa hi behave kare, aur training smoothly improve kare.

QLoRA ek step aage jaata hai: base weights ko 4-bit NF4 mein store karta hai (memory aur kam), lekin train sirf full-precision LoRA adapters hote hain. Forward pass ke time base ko on-the-fly bf16 mein dequantize karke matmul karte hain. Kyunki base pe gradient nahi jaata, 4-bit ki quantization noise se koi bada nuksaan nahi — adapters us noise ko compensate karna seekh lete hain. Isliye 65B model bhi ek single GPU pe fine-tune ho jaata hai.

Ek important baat: α/r\alpha/r scaling. Agar rank badhao par α\alpha same rakho, to effective update strength kam ho jaati hai — isliye rule of thumb: α2r\alpha \approx 2r. Aur inference pe LoRA free hai kyunki tum W0+αrBAW_0 + \frac{\alpha}{r}BA ko merge karke ek normal matrix bana sakte ho — koi extra latency nahi. Yeh sab isliye kaam karta hai kyunki task-specific adaptation genuinely low-dimensional hoti hai.

Go deeper — visual, from zero

Test yourself — Pretraining & Fine-Tuning LLMs

Connections