DPO Basics: Preference-Tuning After SFT
Your fine-tune follows instructions now: the format, the tone, the shape of a good answer. So why does it still pick the second-best answer half the time? Because supervised fine-tuning (SFT) only ever showed it one right answer per prompt. It never learned what makes one good answer better than another good answer. That judgment is preference, and the training step built for it is Direct Preference Optimization (DPO). This is the companion tutorial to Train Your Own AI, Episode 5: the verified Unsloth + TRL DPO build for a single 24 GB GPU.
What DPO actually is: pairs and an anchor
For one prompt, you have two answers. A human, or a careful judge, said answer A is the one we want and answer B is not. A is chosen, B is rejected. DPO trains the model to raise the odds of chosen answers and lower the odds of rejected ones at the same time.
Older preference training (RLHF with PPO) needed a separate reward model, a live sampling loop, and a lot more moving parts. DPO skips all of that. A frozen copy of your SFT model, the reference model, sits inside the training run. DPO does not just make chosen answers more likely in absolute terms; it has to beat the reference model on chosen and lose to it on rejected. That anchor is what keeps the model from drifting while it learns your taste. SFT taught behavior. DPO tunes judgment, inside that behavior.
When DPO helps, and when it does not
- It helps when the SFT model is already competent and the remaining errors are taste and ranking: it picks the rambling answer over the tight one, hedges instead of making the call, or writes the polite-but-useless reply instead of the short, correct one.
- It does not fix knowledge. Missing or stale facts are a retrieval problem (see Episode 4).
- It does not fix format. Broken shape is an SFT problem; fix the examples first.
- It cannot invent capability the base model never had.
The order matters: SFT first, until the model is reliably good. Then DPO, to make it reliably pick the better answer. Run DPO on a weak SFT model and you polish mistakes: the pairs rank bad answers against worse ones, and the model learns that ranking faithfully.
Build the pairs: the dataset is the product
Each row needs exactly three fields: prompt, chosen, rejected. In current TRL you can provide them as plain text (the standard format) or as chat messages (the conversational format); TRL applies the model's chat template for you.
{"prompt": "Summarize this ticket in one line.",
"chosen": "Login fails on Safari after the 2.1 update.",
"rejected": "The user is experiencing an issue where, after updating, they are unable to log in using Safari..."}
Build pairs the honest way. Generate two or more answers per real prompt from your SFT model, at a bit of temperature so they actually differ. Rank them: a human where it matters, or a strong judge model with a written rubric where volume demands it. Keep the pair only when the winner is clearly better; throw away coin flips. A thousand clear verdicts beat ten thousand noisy ones, because noise in pairs becomes taste in the model.
Watch the length trap in particular: if chosen answers are systematically longer, DPO will happily learn longer, not better. Balance the lengths.
The training run: Unsloth + TRL DPOTrainer on 24 GB
Load your SFT checkpoint, or the same 7B instruct model you fine-tuned, with Unsloth in 4-bit, and attach the same rank-16 LoRA setup from Episode 1. With a PEFT model, TRL does something elegant: pass no separate reference model (ref_model=None), and the trainer uses the same weights with the adapter switched off as the frozen reference. One model in memory, two roles. In current TRL the tokenizer is passed as processing_class (the older tokenizer= argument was renamed).
pip install unsloth trl peft datasets
from unsloth import FastLanguageModel
from trl import DPOConfig, DPOTrainer
model, tokenizer = FastLanguageModel.from_pretrained(
"unsloth/Qwen2.5-7B-Instruct", # or your SFT checkpoint
max_seq_length=2048, load_in_4bit=True)
model = FastLanguageModel.get_peft_model(
model, r=16, lora_alpha=16, lora_dropout=0,
target_modules=["q_proj","k_proj","v_proj","o_proj",
"gate_proj","up_proj","down_proj"])
args = DPOConfig(
output_dir="dpo-out", beta=0.1, loss_type="sigmoid",
learning_rate=5e-6, num_train_epochs=1,
per_device_train_batch_size=2, gradient_accumulation_steps=4,
max_length=1024, max_prompt_length=512,
optim="adamw_8bit", warmup_ratio=0.1, logging_steps=10)
trainer = DPOTrainer(model=model, ref_model=None, args=args,
train_dataset=pairs, processing_class=tokenizer)
trainer.train()
Two warnings. That learning rate is not a typo: DPO runs far lower than SFT, because preference updates are easy to overdo, and LoRA tolerates 5e-6 where full-parameter DPO typically wants around 5e-7. And one epoch is the starting point, not laziness: DPO overtrains fast, and a second epoch often buys degeneration, not judgment.
Beta: the one dial that matters most
Beta controls how strongly the model must obey the pairs versus how tightly it stays near the reference model. Beta 0.1 is the standard default and the right first run.
- Beta toward 0.05: the model chases preferences harder and drifts further. Stronger taste, higher risk of strange side effects.
- Beta 0.1: the default balance. Start here.
- Beta toward 0.3-0.5: the anchor tightens. Safer, smaller changes, sometimes almost no change at all.
Think of beta as leash length. Change it only after reading the training logs, where rewards/accuracies tells you whether the model is even learning to separate chosen from rejected on your own data.
Prove it worked: win rate, not vibes
- Hold out 50-100 prompts your training pairs never saw.
- Generate blind, side by side: answers from the SFT model and from the DPO model, judged with the same rubric you built the pairs with, human spot-check included.
- Count the win rate. DPO wins if the new model takes a clear majority, roughly 55-65%, without getting longer or stranger.
- Watch the training metrics: rewards accuracy should climb above 0.5 and keep rising gently; rewards margin should grow. Accuracy stuck near a coin flip means your pairs are too noisy or too close to call, and no beta value fixes bad verdicts.
- Re-run your old SFT evals. Preference-tuning that improves taste while quietly breaking format is a trade, not a win.
Recap, and what is next
SFT teaches a model how to answer. DPO teaches it which answer to prefer, from pairs of chosen and rejected replies, anchored to a frozen copy of itself so taste does not become drift. Build clear pairs, balance their lengths, run one epoch at beta 0.1 and a learning rate of 5e-6 on a single 4090-class GPU, and prove it with a blind win rate on prompts the training never saw.
Next in the series: running inference cheap, serving your fine-tune with vLLM on a rented GPU without paying for idle iron.
Find the cheapest GPU for your next run
Live pricing, hardware specs, and availability across 30 GPU clouds, neoclouds, and bare-metal providers.
Open the Directory