27

Fine-tuning and LoRA

When should you adapt a model, and how is it done cheaply?

11 minExecutive path

Fine-tuning and LoRA

With voice. You can pause and jump to any step.

Lesson complete

Subtitles

Part two ended with a base model: it can continue text, but it does not know your case. Not your tone, not your formats, not your categories.

Voice

No voice: the lesson advances on its own with subtitles.

The voice for this lesson is being recorded: for now it advances on its own with subtitles.

0:00 / 0:00

What you will achieve

  • Explain what fine-tuning is and why moving every parameter with examples from a single area can make the model forget what it already knew.
  • Calculate how many numbers LoRA trains with the rule rank × (rows + columns) and compare them with those of the full table.
  • Decide among prompt, RAG, and fine-tuning for a case, and know what data and what evaluation to demand before tuning.

Key ideas

  1. Fine-tuning keeps training an already trained model on pairs of input and desired answer: it is the same loop as in lesson 8 (answer, measure the error, backpropagate, adjust), with far less data.
  2. Moving every parameter with examples from a single area disturbs everything else: that is catastrophic forgetting. The basic defense is replay (mixing in examples of what it already knew) and also measuring what it already knew.
  3. LoRA freezes the original table and trains a low-rank correction ΔW = B·A: rank × (rows + columns) numbers instead of rows × columns. On GPT-3, about ten thousand times fewer trainable parameters.
  4. B starts at zero, so the model starts out identical; since the base never changes, the adapter (B and A) can be removed or swapped: one base model and one adapter per task. QLoRA stores the base in 4 bits and so tunes a 65-billion-parameter model on one 48 GB GPU.
  5. A good prompt first; if information that changes is missing, RAG; fine-tuning for a stable, repeated behavior, with good data and your own evaluation. It is not a good way to add new facts.

What many people believe

Common belief For the model to know my company’s documents, I have to fine-tune it on them.

What is true Fine-tuning changes how the model behaves (tone, format, task), but it is a poor way to add facts: it learns them slowly, cannot cite them, and, once it learns them, may make more things up. For your own knowledge that changes, start with RAG; LoRA makes tuning cheaper, but it does not change this.

Check yourself

Four questions. No grade, no account: they just show whether the idea stuck.

ConceptWhat does LoRA train?
ConceptYou fine-tune a model only on examples from your area: it improves there but gets worse at tasks it used to do well. What happened, and what helps?
CalculationA 4096 by 4096 table is adapted with rank-8 LoRA. How many numbers do B and A have in total?
ApplicationA team proposes fine-tuning the model on the company policy manual so it “learns it.” The manual changes every quarter and answers must cite the paragraph. What do you answer?

Under the hoodFor those who want the math and the code. You do not need it to follow the course.

What supervised fine-tuning minimizes

Given a model with weights θ\theta and examples (xi,yi)(x_i, y_i) (an input and the desired answer), supervised fine-tuning keeps minimizing the cross-entropy,

L(θ)=−1N∑i=1Nlog⁡pθ(yi∣xi)L(\theta) = -\frac{1}{N}\sum_{i=1}^{N} \log p_\theta(y_i \mid x_i)

with the same gradient descent from lesson 4 and the backpropagation gradients from lesson 8. In a language model, yiy_i is a sequence of tokens and the sum runs only over the answer tokens. It starts from the pretrained weights θ0\theta_0 (lesson 23), with much smaller learning rates than in pretraining and only a few passes over the data. LIMA (Zhou et al., 2023) used 1,000 examples, about 750,000 tokens, on the 65-billion-parameter LLaMA, which was pretrained on 1.4 trillion tokens (Touvron et al., 2023).

LoRA: the math

For a pretrained weight matrix W0∈Rd×kW_0\in\mathbb{R}^{d\times k}, LoRA freezes it and writes the update as

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

The forward pass is h=W0x+αrB(Ax)h = W_0x + \tfrac{\alpha}{r}B(Ax). AA starts as a Gaussian and BB as zeros, so ΔW=0\Delta W = 0 at the start (Hu et al., 2021); only AA and BB receive gradients. Since ∂L/∂A\partial L/\partial A is proportional to BB, only BB moves on the first step.

Each column bjb_j of BB and each row aja_j of AA form a rank-one piece, bjajb_j a_j (column times row, like the lesson’s multiplication table): BA=∑j=1rbjajBA = \sum_{j=1}^{r} b_j a_j. Hence the name: the correction has rank at most rr.

Trainable parameters: r (d+k)r\,(d+k) instead of dkdk. It saves only if r<dkd+kr < \dfrac{dk}{d+k}; for a square matrix, r<d/2r<d/2. With d=k=32d=k=32 (the tiny model’s middle layer), r=16r=16 is a tie: 16⋅64=1,02416\cdot 64 = 1{,}024.

Once trained it can be merged, W=W0+αrBAW = W_0 + \tfrac{\alpha}{r}BA, and inference costs the same as the original model; or the adapter can stay separate to swap it for another.

For GPT-3 175B (dmodel=12,288d_{\text{model}}=12{,}288, 96 layers), adapting only WqW_q and WvW_v with r=4r=4 gives 96⋅2⋅4⋅(12,288+12,288)=18,874,36896\cdot 2\cdot 4\cdot(12{,}288+12{,}288)=18{,}874{,}368 trainable parameters: about 9,300 times fewer than 175 billion (the paper says “roughly 10,000×”). In FP16 the adapter takes about 35 MB versus the model’s 350 GB, and 100 adaptations take about 354 GB instead of 35 TB (Hu et al., 2021).

Why the change has low rank

If M=∑iσiuivi⊤M=\sum_i \sigma_i u_i v_i^\top is the singular value decomposition, the Eckart–Young theorem says the best rank-rr matrix (in Frobenius norm) is the sum of the first rr terms, and its error is ∑i>rσi2\sum_{i>r}\sigma_i^2. In the tiny model, the change that full fine-tuning leaves in the 32×32 layer has singular values that fall quickly: the first four gather about 97% of ∑σi2\sum\sigma_i^2. That is what LoRA assumes and exploits; Aghajanyan et al. (2020) had already measured that fine-tuning a language model needs very few effective dimensions. It is not a law: Hu et al. do not expect a small rank to work for every task (for example, a language different from the pretraining one), and Biderman et al. (2024) measured that, in programming and math, full fine-tuning learns changes whose rank is 10 to 100 times higher than typical LoRA configurations.

Forgetting, replay, and what the evidence says

With narrow data, moving shared weights shifts whatever that data does not cover. In the tiny model (70 new examples from a single area, 300 full-batch Adam steps, one run with fixed seeds):

tuning trainable accuracy on the new part accuracy on what it knew
none (base model) — 0% 100%
full 1,251 98% 93%
full + 30 for replay 1,251 94% 97%
LoRA r = 2 128 91% 87%
LoRA r = 2 + 30 for replay 128 93% 98%

Here LoRA without replay forgets more than full fine-tuning; in large models, Biderman et al. (2024) found the opposite on average (LoRA learns less and forgets less), but not that it stops forgetting. Moral: forgetting is a risk with both methods, and you measure it with data the tuning did not see. The name “catastrophic” comes from McCloskey and Cohen (1989); in the toy the loss is moderate.

Memory: from sixteen bytes per parameter to less

Training with Adam in mixed precision stores, per parameter: FP16 weights (2 bytes), FP16 gradients (2) and, in FP32, a copy of the weights, momentum, and variance (4+4+44+4+4): 1616 bytes (Rajbhandari et al., 2019; lesson 25). With LoRA, frozen weights only need their copy (2 bytes in FP16) and the 16 bytes are paid only for AA and BB. QLoRA also stores the base in 4 bits (NF4 format, with “double quantization” of the constants) and trains the adapter in 16 bits. Our own count for a 7-billion-parameter model, without activations: full fine-tuning 7⋅16=1127\cdot 16=112 GB; FP16 base, 14 GB; 4-bit base, about 3.5 GB, plus the adapter and activations.

The papers’ figures do not come exactly out of this count, because they depend on what is stored and how: the LoRA paper gives, for GPT-3 175B, from 1.2 TB to 350 GB, and the QLoRA paper counts more than 780 GB to fine-tune 65 billion parameters in 16 bits (about 12 bytes per parameter). Always compare under the same count.

The “Q” part can be tried in the tiny model: with its three matrices in 4 bits (NF4, blocks of 64), the map changes in 22 of 1,600 cells, and rank-2 LoRA with replay reaches 91% on the new part and 98% on what it knew, versus 93% and 98% with the 16-bit base. It is a toy: it reproduces the idea, it proves nothing about large models.

In code

import numpy as np

# 1) the count: one GPT-3 matrix (12288 x 12288) and rank 8
d = k = 12288
r = 8
full = d * k
lora = r * (d + k)
print(full, lora, full // lora)            # 150994944 196608 768

# 2) the correction starts at zero (B = 0): the model does not change
rng = np.random.default_rng(0)
W0 = rng.normal(size=(6, 6))               # frozen matrix (tiny)
A = rng.normal(size=(2, 6))                # r = 2, Gaussian
B = np.zeros((6, 2))                       # zeros
x = rng.normal(size=6)
h = W0 @ x + B @ (A @ x)                   # alpha/r = 1
print(np.allclose(h, W0 @ x))              # True

# 3) after training, B is no longer zero: the correction has rank <= 2
B = rng.normal(size=(6, 2)) * 0.1
print(np.linalg.matrix_rank(B @ A))        # 2

What comes next

Tuning on input-output pairs changes format, style, and task. Turning a model that only completes text into an assistant uses that same mechanism, but the question is which pairs and which signal: instruction tuning and reinforcement learning from human feedback (lesson 28). And it is practical: QLoRA was used to instruction-tune the Guanaco family (Dettmers et al., 2023), and those authors found that data quality matters far more than quantity (9 thousand OASST1 examples beat 450 thousand from FLAN v2 in their chatbot test).

Decision lens

What this idea means if your job is to decide, not to code.

  • Demand the baseline before tuning: what does a good prompt achieve? And RAG? If the team cannot show those numbers on your own cases, tuning is a blind bet.
  • Fine-tuning changes behavior and style; it is not a good way to “stuff in fresh knowledge.” For prices, policies, or catalogs that change, RAG updates without retraining and lets you cite the source.
  • Ask about data and evaluation first: how many examples, who reviewed them, which unseen cases measure the improvement, and how you check what the model may have forgotten.
  • LoRA makes training and storage cheap (megabyte adapters, not hundred-gigabyte copies), but each adapter is tied to the base model it was trained on: ask who maintains it when the base changes, and treat tuning data as sensitive.

Terms in this lesson

Full transcript
CommentsThis lesson’s public conversation: questions, facts to check, ideas.

Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub

Sources