Unit 7 · ML & applied AI · Lesson 8 of 13
Fine-tuning, LoRA, instruction tuning and preference optimization
Step 1: Learn the ideas
- When not to fine-tune: prompting, retrieval, tools and evaluation first
- Full fine-tuning versus parameter-efficient fine-tuning
- LoRA: low-rank updates, rank, alpha, target modules, merging; QLoRA (awareness)
- Supervised fine-tuning: chat templates, loss masking and data quality
- Reward models trained on pairwise preferences
- RLHF with PPO and a KL penalty (concept level)
- DPO and related direct preference methods
- Failure modes: overfitting, forgetting, reward hacking, format drift
Why it matters
Fine-tuning is expensive to do well and easy to do badly, so interviewers probe judgment as much as mechanics: when it is justified, what it changes in the model, and how you would know it worked. Research roles add the derivation of preference-optimization objectives.
Core ideas
When not to fine-tune. Fine-tuning is effective at changing behavior, style and format; it is a poor way to add fresh or frequently changing facts, and it creates a model you must evaluate, version and serve. Try, in order: a clear prompt with examples, retrieval for knowledge, tools for computation and actions, and an evaluation set that tells you whether any of these work. Fine-tune when you need a consistent format or style at scale, a skill the base model lacks in your domain, lower latency or cost through a smaller specialized model, or behavior that prompting cannot elicit reliably. Without an evaluation set, you cannot tell whether a fine-tune helped.
Full versus parameter-efficient fine-tuning. Full fine-tuning updates every weight and needs memory for weights, gradients and two Adam moment tensors, roughly four times the weight memory at one precision before activations. Parameter-efficient methods freeze the base model and train a small set of new parameters.
LoRA. For a frozen weight W0 of shape (d, k), LoRA learns delta W = B A with B of shape (d, r) and A of shape (r, k), where the rank r is much smaller than d and k, scaled by alpha / r. B starts at zero, so training begins exactly at the base model. Only A and B receive gradients and optimizer state, so memory and checkpoint size drop sharply and many task adapters can share one base model. After training, W0 + (alpha / r) B A can be merged into the base weights for zero inference overhead, or adapters can be kept separate and swapped per request. Attention projections are the usual targets, often with the MLP projections too. QLoRA keeps the frozen base in 4-bit precision while training the adapters in higher precision.
Supervised fine-tuning (SFT). Train on prompt-response pairs with the same next-token loss as pretraining, usually computing the loss only on response tokens and using exactly the chat template the model will see in production. Quality and consistency matter more than volume; a small clean set often beats a large noisy one. Watch for memorized phrasing, forgetting of general skills, and format drift when training and serving templates differ.
RLHF. The classic pipeline, described in the InstructGPT paper: (1) SFT on demonstrations; (2) collect human comparisons between model outputs and train a reward model with a pairwise loss such as -log sigmoid(r(x, y_w) - r(x, y_l)); (3) optimize the policy with PPO to maximize reward minus a KL penalty to the SFT reference model. The KL term keeps the policy where the reward model is trustworthy; without it, the policy finds outputs that exploit reward-model errors (reward hacking). PPO needs on-policy sampling, a value model and careful tuning, which makes it expensive and brittle.
DPO. DPO starts from the same KL-regularized objective, writes the optimal policy in closed form, and expresses the reward implicitly through the log-ratio between policy and reference probabilities. The result is a classification-style loss on preference pairs: -log sigmoid(beta * [(log pi(y_w|x) - log pi_ref(y_w|x)) - (log pi(y_l|x) - log pi_ref(y_l|x))]). It needs log-probabilities of both responses under the trained policy and a frozen reference (the latter can be precomputed), but no reward model and no sampling loop. Tradeoffs: it learns only from the fixed preference data, and it can over-optimize when that data is narrow.
Evaluating a fine-tune. Measure the target behavior on held-out data, plus regression suites for general ability, instruction following and safety behavior. Compare against the strongest prompted baseline, not only the untuned model.
Common misconceptions
- "Fine-tuning is how you teach a model your documents." It is unreliable for facts and stale as soon as documents change; use retrieval for knowledge.
- "LoRA adds inference latency." Not when merged; unmerged adapters add a small extra matrix product.
- "A higher LoRA rank is always better." It adds capacity and overfitting risk; small ranks are often enough, and the right value is an empirical question.
- "DPO makes preference-data quality less important." It is only as good as its pairs; noisy or narrow preferences give noisy or narrow behavior.
- "The KL penalty is a minor regularizer." It is what keeps the policy where the reward model remains valid.
- "RLHF mainly teaches the model new knowledge." It mostly shapes which behaviors the model expresses.
How to approach it
Be ready to argue the decision first: the failure, the baseline, the available data and the evaluation. Then learn the LoRA mechanics from the paper and library docs, and the RLHF and DPO pipelines from InstructGPT and DPO. If you have GPU access, run a small LoRA SFT and a DPO step on public data and evaluate before and after. In interviews, describe only what you actually built and measured.
Suggested order
- 1Read CS229 main notes Ch. 15 (Foundation models overview), including its LoRA section, for a compact framing of fine-tuning versus linear probes.
- 2Read the method section of the LoRA paper and the Hugging Face PEFT LoRA conceptual guide; write down what rank, alpha and target modules control.
- 3Read the InstructGPT methods (SFT, reward model, PPO with a KL penalty) and its discussion of regressions on public benchmarks and of limitations.
- 4Read the DPO derivation with the loss written out, and explain the roles of beta and the reference model.
- 5Write a one-page decision memo for a realistic scenario, such as an assistant that must follow a strict output format: prompting, retrieval, LoRA SFT or preference tuning, plus the evaluation that would decide.
- 6With GPU access, work through CS336 Assignment 5 (SFT, GRPO and the DPO loss), or run a small LoRA SFT and DPO with open-source libraries.
Step 2: Check off what you can do
Tap each one you can genuinely do or explain. Each earns 10 XP.
Step 3: Go deeper
LoRA: Low-Rank Adaptation of Large Language ModelsStart here
arXivThe low-rank update, its initialization, which matrices to adapt and why the update can be merged.
Open(opens in a new tab)Link responded, 5 Oct 2026Direct Preference Optimization: Your Language Model is Secretly a Reward ModelStart here
arXivDerivation of the preference loss from the KL-regularized objective and the role of the reference policy.
Open(opens in a new tab)Link responded, 5 Oct 2026Training language models to follow instructions with human feedback
arXivThe classic RLHF pipeline: demonstrations, a reward model from rankings, PPO with a KL penalty, and how it was evaluated.
Open(opens in a new tab)Link responded, 5 Oct 2026Hugging Face PEFT documentation
Hugging Face documentationSection: LoRA conceptual guide
Practical LoRA configuration: rank, alpha, target modules and merging adapters.
Open(opens in a new tab)Link responded, 5 Oct 2026Stanford CS336 Language Modeling from Scratch
Stanford UniversitySection: Assignment 5: Alignment (handout and starter code)
Implement SFT, policy-gradient (GRPO) training and a DPO loss on a real model; requires GPU access.
Open(opens in a new tab)Link responded, 5 Oct 2026
Step 4: Practice
Without GPUs: write the decision memo from the study order, then implement a LoRA linear layer and the DPO loss in a few lines of PyTorch on toy tensors; check that a zero-initialized B reproduces the base layer exactly and that the DPO loss falls when the chosen response gains probability relative to the reference. With GPU access: LoRA-tune a small open model on a public instruction dataset, then run DPO on public preference pairs; evaluate the prompted base model, the SFT model and the DPO model on the same held-out set plus a small general-capability regression set, and report differences with intervals. CS336 Assignment 5 is the in-depth version.
- Stanford CS336 assignments (tokenizer and transformer, systems, scaling, data, alignment)Open(opens in a new tab)Not attemptedStanford CS336
- DPO loss on a toy preference problem with a known rewardOpen(opens in a new tab)Not attemptedSelf-directed project (authors’ reference code linked)
- LoRA layer from scratch with a merge testOpen(opens in a new tab)Not attemptedSelf-directed project (reference implementation linked)
Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).
Step 5: Test yourself
Answer each one out loud or on paper before you finish the lesson.
- Question 1A team wants to fine-tune so the model knows this week’s product catalog. What do you recommend and why?
- Question 2Why is the LoRA B matrix initialized to zero?
- Question 3Which memory does LoRA save compared with full fine-tuning, and which does it not save?
- Question 4Why is the SFT loss usually masked to response tokens?
- Question 5What is reward hacking, and how does the KL penalty limit it?
- Question 6What does DPO need at training time that SFT does not?
You’re done when you can
- Argue for or against fine-tuning in a given scenario in under five minutes, naming the evaluation that would decide it.
- Explain the LoRA parameterization, initialization and merging with shapes.
- Write the DPO loss and explain each term, including beta and the reference model.
- Describe the three stages of classic RLHF and the purpose of the KL penalty.
Finish the lesson
Unlocks next: Alignment and safety evaluation research (optional)


