Slides · Evaluation and model adaptation
Fine-tuning methods: SFT, LoRA, QLoRA and DPO
1 / 26
Fine-tuning methods: SFT, LoRA, QLoRA and DPO
How a model is trained in stages, and how to adapt one cheaply when you cannot retrain all of it.
Concept
Mechanism first, then the bill
- Last lesson: whether to fine-tune. This one: how.
- Interviews and design reviews ask two things: what is the method doing, and what does it cost in memory and hardware?
- The stages tell you which data to collect. The arithmetic tells you what you can afford.
Diagram
A model is trained in stages
Compare
Which data, which lesson learned
| Stage | Data it uses | What it teaches |
|---|---|---|
| Pretraining | A huge text corpus | Language ability and most world knowledge, via next-token prediction |
| SFT | (prompt, ideal response) pairs | Follow instructions in a format, style or task routine |
| RLHF | Human preference pairs, then a reward model | Prefer better over worse answers, via reinforcement learning |
| DPO | Preference pairs directly | The same preferences, with a classification-style loss |
| Distillation | A large model’s outputs | A small model keeping a narrow task’s quality at lower cost |
Compare
What one record looks like (invented examples)
| Stage | One training record |
|---|---|
| SFT | prompt: “Classify: ‘My card was charged twice.’” response: “billing” |
| Preference | prompt: “My phone battery is swollen. What should I do?”; preferred: “Stop using it, do not charge or puncture it, and take it to a repair or recycling point.”; rejected: “It is probably fine; keep using it unless it gets hot.” |
- An SFT record holds one answer to imitate.
- A preference record holds a prompt plus a ranked pair, so the model learns which of two answers is better.
Diagram
RLHF versus DPO
Sources[1] Training language models to follow instructions with human feedback[2] Direct Preference Optimization: Your Language Model is Secretly a Reward Model
Concept
RLHF in InstructGPT
- Step 1: supervised fine-tuning on labeller demonstrations (SFT).
- Step 2: labellers rank model outputs, and a reward model learns to predict which one people prefer.
- Step 3: the SFT policy is optimised against that reward with PPO.
- At each token the objective also charges a KL penalty relative to the SFT model, which the authors add to limit over-optimisation of the reward model.
The cost: two extra training pieces, a reward model and a reinforcement-learning loop.
Source[1] Training language models to follow instructions with human feedback
Math
DPO’s loss in one line
L=−E[logσ(β(logpref(yw∣x)pθ(yw∣x)−logpref(yl∣x)pθ(yl∣x)))]| Symbol | Meaning |
|---|---|
| x | The prompt |
| yw, yl | The preferred (winning) and rejected (losing) responses |
| pθ | The model being trained |
| pref | The frozen reference model, the starting point |
| β | Controls how far the model may move from the reference |
| σ | Squashes (sigmoid) the margin into a probability, giving a classification-style loss |
Reading it: the gap between the two log-ratios widens. The TRL docs note that in practice this is typically achieved by pushing the rejected response down rather than lifting the preferred one. No reward model and no RL loop.
Sources[2] Direct Preference Optimization: Your Language Model is Secretly a Reward Model[3] DPO Trainer (TRL documentation)
Concept
LoRA: a removable overlay
- Full fine-tuning rewrites the whole textbook.
- LoRA leaves the textbook frozen and learns a small correction for each page.
- Only the overlay is trained, so you can store many overlays and swap them.
- The overlay is not literal extra text: the correction is a matrix added to a weight matrix.
Math
The LoRA update
y=Wx+rαBAxW:d×k (frozen)B:d×r (trainable)A:r×k (trainable)r≪min(d,k)trainable weights=dr+rk| Symbol | Meaning |
|---|---|
| W | The pretrained weight matrix, frozen |
| A, B | The small trainable matrices; ΔW=BA |
| r | The rank, much smaller than d and k |
| α | A constant that scales the update |
At the start, A is random Gaussian and B is zero, so ΔW is zero and training begins from the original model.
Source[4] LoRA: Low-Rank Adaptation of Large Language Models
Diagram
Two parallel paths, summed
Source[4] LoRA: Low-Rank Adaptation of Large Language Models
Concept
Why keeping W frozen is the saving
- No gradients or optimiser state are kept for the big matrix; only A and B are updated.
- The authors estimate VRAM falls by up to two thirds when training with Adam and r is far below the model dimension, because optimiser state is not kept for the frozen weights.
- For GPT-3 175B the authors report a checkpoint shrinking from 350 GB to 35 MB (r=4, query and value matrices only).
- After training, B·A can be merged into W, so serving adds no latency. Or keep it separate and swap adapters.
Sources[4] LoRA: Low-Rank Adaptation of Large Language Models[5] LoRA (PEFT conceptual guide)
Worked example
Worked example: a rectangular layer
Layers are not always square. Take a 4096×11008 layer with rank 16 (illustrative shape).
layer weightsB (d×r)A (r×k)adapter totalfraction=4096×11008=45,088,768=4096×16=65,536=16×11008=176,128=65,536+176,128=241,664=45,088,768241,664=0.536%The count is dr+rk, not 2dr. The "2dr" shortcut only holds when d=k.
Worked example
Worked example: rank sweep, then a whole model
Same 4096×11008 layer (45,088,768 weights), different rank:
| Rank r | d·r + r·k | Fraction of the layer |
|---|---|---|
| 4 | 16,384+44,032=60,416 | 0.134 % |
| 8 | 32,768+88,064=120,832 | 0.268 % |
| 16 | 65,536+176,128=241,664 | 0.536 % |
| 32 | 131,072+352,256=483,328 | 1.072 % |
Doubling the rank doubles the count and the fraction. Now a hypothetical model that adapts two 4096×4096 matrices in each of 32 layers at r=8 (illustrative):
per matrixadapterstotalfile size=2×4096×8=65,536=32 layers×2=64=64×65,536=4,194,304(about 4.2 million)=4,194,304×2 B≈8.4 MB at 16-bitWhich matrices get adapters is a design choice, so the whole-model fraction depends on it.
Concept
What LoRA gives, and what it limits
- Small files and swappable: one base model can serve many adapters, for example one per task or tenant.
- Mergeable: fold B·A into W for no added latency, or keep it separate to stay swappable.
- Limit: a low rank restricts how much the model can change. It suits style, format and narrow domain behaviour. A very large shift may need a higher rank or full fine-tuning.
Source[4] LoRA: Low-Rank Adaptation of Large Language Models
Concept
QLoRA: freeze the base in 4-bit
- The frozen base model is stored in 4-bit; gradients flow back through it into LoRA adapters trained in higher precision.
- Each time a 4-bit tensor is needed for compute, it is first converted up to BFloat16 and the multiplication runs in 16-bit.
- Three ingredients: the NF4 data type, double quantisation (quantising the quantisation constants), and paged optimisers for memory spikes.
- The authors report fine-tuning a 65B-parameter model on a single 48 GB GPU, preserving 16-bit fine-tuning task performance in their experiments.
Worked example
Worked example: weights-only memory
bytes per weight: 16-bit = 2, 4-bit = 0.5
weights (GB)=parameters (billions)×bytes per weight| Model size | 16-bit weights | 4-bit weights | Fits a 24 GB GPU for weights alone? |
|---|---|---|---|
| 7B | 7×2=14 GB | 7×0.5=3.5 GB | Both |
| 20B | 20×2=40 GB | 20×0.5=10 GB | Only 4-bit |
| 33B | 33×2=66 GB | 33×0.5=16.5 GB | Only 4-bit, with 7.5 GB left |
Weights only. Activations, optimiser state, adapters and quantisation constants come on top, so real use is higher. The authors say paged optimisers are what make 33B and 65B QLoRA tuning possible on a single 24 or 48 GB GPU.
Concept
The price of QLoRA
- Every use of a weight adds a dequantise step, so training steps are typically slower than 16-bit LoRA. That is an expectation to measure, not a number from the paper.
- The “matches 16-bit quality” result is reported for specific settings. Check it on your own task.
- You save memory; you spend time and some certainty.
Compare
Full fine-tune, LoRA or QLoRA
| Method | Trains | Hardware need | Typical use |
|---|---|---|---|
| Full fine-tune | All weights | Highest | Large behaviour shift with budget and plenty of data; higher forgetting risk |
| LoRA | Small adapters, base in 16-bit | Moderate | Style, format, domain behaviour; many swappable variants |
| QLoRA | Adapters, base in 4-bit | Lowest | Large model on limited GPUs, accepting slower steps |
Diagram
Choosing a method
Check yourself
Check: name the stage
A team collects 200,000 answers produced by a large model on its ticket-triage prompts and trains a small model to reproduce them. Which stage is this, what data does it use, and what is the goal?
Answer
Distillation. The data are the large model’s outputs on the task’s prompts. The goal is to keep a narrow task’s quality at lower cost and latency. It is not pretraining (no raw corpus) and not preference tuning (no ranked pairs).
Check yourself
Check: count an adapter
A layer maps 1024 inputs to 4096 outputs (a 4096×1024 weight matrix, d=4096, k=1024). You attach a rank-4 adapter. How many trainable weights, and what fraction of the layer?
Answer
B is d×r=4096×4=16,384 and A is r×k=4×1024=4,096, so 20,480 trainable weights. The layer has 4096×1024=4,194,304 weights, so the fraction is 4,194,30420,480=0.488%.
Check yourself
Check: when not to reach for QLoRA
A team has one 80 GB GPU and a 7B base model. They want a modest change of reply style. Weights alone are 14 GB at 16-bit. Which method, and why not the one with the lowest memory?
Answer
LoRA with the base in 16-bit. The weights fit comfortably, and the change is style, which suits a small adapter. QLoRA would save memory the team does not need while adding dequantisation work per step and an unverified quality match for their task. Full fine-tuning is not needed for a modest shift.
Summary
Recap
- Pretraining gives knowledge, SFT teaches behaviour, RLHF and DPO teach preference, distillation shrinks a model.
- DPO drops the reward model and the RL loop, but still needs a frozen reference.
- LoRA freezes W and trains B·A: d·r + r·k weights, swappable and mergeable.
- QLoRA keeps the frozen base in 4-bit: least memory, slower steps.
- Choose by hardware and size of change: full fine-tune, LoRA or QLoRA.
References
References & Sources (1 of 2)
Training language models to follow instructions with human feedback (opens in a new tab)
Long Ouyang, Jeff Wu, Xu Jiang, Diogo Almeida, et al.2022NeurIPS 2022Paper
arXiv 2203.02155 (opens in a new tab)https://proceedings.neurips.cc/paper_files/paper/2022/hash/b1efde53be364a73914f58805a001731-Abstract-Conference.html
Used for The RLHF pipeline of demonstrations, a reward model trained on comparisons, and PPO with a per-token KL penalty from the SFT model.
Direct Preference Optimization: Your Language Model is Secretly a Reward Model (opens in a new tab)
Rafael Rafailov, Archit Sharma, Eric Mitchell, Christopher D. Manning, et al.2023NeurIPS 2023Paper
arXiv 2305.18290 (opens in a new tab)https://proceedings.neurips.cc/paper_files/paper/2023/hash/a85b405ed65c6477a4fe8302b5e06ce7-Abstract-Conference.html
Used for DPO trains on preference pairs with a classification-style loss and a reference policy, without a separate reward model or RL loop.
DPO Trainer (TRL documentation) (opens in a new tab)
Hugging Face2026Documentation
https://huggingface.co/docs/trl/dpo_trainer
Used for The DPO loss form and the roles of the reference model and beta in a widely used implementation.
LoRA: Low-Rank Adaptation of Large Language Models (opens in a new tab)
Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, et al.2022ICLR 2022Paper
arXiv 2106.09685 (opens in a new tab)https://iclr.cc/virtual/2022/poster/6319
Used for The low-rank update W x + B A x with a random Gaussian A and zero B, the VRAM and checkpoint savings, and merging for no extra inference latency.
LoRA (PEFT conceptual guide) (opens in a new tab)
Hugging Face2026Documentation
https://huggingface.co/docs/peft/main/en/conceptual_guides/lora
Used for How rank, alpha, target modules and merging appear in a LoRA configuration.
References
References & Sources (2 of 2)
QLoRA: Efficient Finetuning of Quantized LLMs (opens in a new tab)
Tim Dettmers, Artidoro Pagnoni, Ari Holtzman, Luke Zettlemoyer2023NeurIPS 2023Paper
arXiv 2305.14314 (opens in a new tab)https://proceedings.neurips.cc/paper_files/paper/2023/hash/1feb87871436031bdc0f2beaa62a049b-Abstract-Conference.html
Used for A frozen 4-bit base with LoRA adapters, NF4, double quantisation, paged optimisers, dequantisation to BFloat16 on use, and the 65B-on-48-GB result.
Keys: ← → move, Home End first and last, F full screen.