04

Fine-Tuning and LoRA

Pretraining teaches a model language; fine-tuning teaches it a job — and LoRA does it without touching most of the weights.

Key terms

TermPlain definition
Pretrained modelThe base model from Day 1 — good at predicting the next token, but not shaped to follow instructions.
Fine-tuningContinuing training on a smaller, task-specific dataset so the model adopts a new behavior.
Supervised fine-tuning (SFT)Fine-tuning on labeled (prompt, ideal answer) pairs so the model imitates good responses.
Full fine-tuningUpdating every weight in the model — accurate but expensive to store and run.
LoRALow-Rank Adaptation: freeze the original weights and learn a tiny low-rank update instead.
RankThe number r of independent directions in a matrix; a low-rank update uses a small r.
AdapterA small set of extra weights added to a frozen model to specialize it.

Pretrained models are not yet helpful

A model fresh from pretraining (Day 1) is a next-token machine. Ask it a question and it may continue with more questions, because internet text often lists questions together. It has the knowledge but not the habit of answering.

Fine-tuning fixes the habit. We show the model many examples of the behavior we want — answering, summarizing, following an instruction — and nudge its weights toward producing that behavior. The knowledge from pretraining stays; only the response style shifts.

Supervised fine-tuning

Supervised fine-tuning (SFT) is the simplest recipe. We collect a dataset of (prompt, ideal answer) pairs written or curated by humans, then train the model with the same next-token objective from Day 1 — but now the "next tokens" are the ideal answer.

The loss is unchanged: average surprise over the answer tokens (Day 1). What changes is the data. After a few thousand well-chosen pairs, the model learns to treat a prompt as something to respond to rather than merely continue.

The catch: full fine-tuning updates every weight. A 7-billion-parameter model then needs a second 7-billion-parameter copy stored for each task. That is where LoRA comes in.

LoRA: freeze W, learn B times A

LoRA keeps the original weight matrix W frozen and learns a small update ΔW that is added at run time: W_effective = W + B·A. Here B and A are two skinny matrices whose product has the same shape as W, but far fewer parameters.

Take a tiny weight matrix W of shape 4 by 4 — that is 4·4 = 16 numbers. Instead of retraining all 16, LoRA learns a rank-1 update: B of shape 4 by 1 and A of shape 1 by 4.

W (frozen, 16 numbers):

c1c2c3c4
r10.20.10.00.3
r20.10.40.20.1
r30.00.20.50.0
r40.30.10.00.2

The learned factors — B (4 by 1) and A (1 by 4):

| B | | --- | | 1 | | 2 | | 0 | | 1 |

A →a1a2a3a4
1012

The update ΔW = B·A fills each cell with B[row] · A[col]:

ΔWc1c2c3c4
r1 (B=1)1012
r2 (B=2)2024
r3 (B=0)0000
r4 (B=1)1012

Row 2 is just row 1 doubled, and row 4 equals row 1 — every row is a multiple of the single pattern A. That is exactly what "rank 1" means: one direction, scaled per row. At run time the model uses W + ΔW.

Why low rank is enough — count the parameters

The payoff is the parameter count. Compare storing a full update against storing the two LoRA factors:

UpdateNumbers to learnFormula
Full 4 by 416d · d
LoRA rank 18d·r + r·d = 2·d·r

For this tiny d = 4, LoRA already halves the count: 2·4·1 = 8 versus 16. The gap explodes at real sizes. For a d = 4096 weight matrix:

UpdateNumbers to learn
Full (4096·4096)16,777,216
LoRA rank 8 (2·4096·8)65,536

That is roughly 0.4% of the full count — for one matrix. Why can such a thin update work at all? Fine-tuning does not need to rebuild the model; it only needs to steer it. Empirically the adjustment a task requires lives in a small number of directions, so a low-rank B·A captures most of the benefit while the frozen W keeps all the pretrained knowledge. You can even keep many small LoRA adapters — one per task — and swap them on top of a single frozen base.

Key takeaways

  • Pretraining gives knowledge; fine-tuning gives the behavior (answering, following instructions).
  • SFT reuses the Day 1 next-token loss, but on curated (prompt, ideal answer) pairs.
  • Full fine-tuning updates every weight and needs a full model copy per task.
  • LoRA freezes W and learns a low-rank update B·A, storing a tiny fraction of the parameters.
  • Low rank works because a task's needed adjustment lives in a few directions; adapters can be swapped per task.

Checklist

  • [ ] Can you explain why a pretrained model answers a question with more questions?
  • [ ] Can you state what SFT changes versus what it keeps from pretraining?
  • [ ] Can you compute B·A for a 4 by 1 and 1 by 4 pair and get a 4 by 4 update?
  • [ ] Can you compare the parameter count of a full update versus LoRA using d·d versus 2·d·r?
  • [ ] Can you say in one sentence why a low-rank update is usually enough?