AI Hints
Home›Fine-Tuning LLMs Tutorial›Preference Tuning and DPO
Fine-Tuning LLMs · Lesson 92 of 120

DPO

Preferences without a reward model.

⏱ 2 min read▶ Video at 2:19:46🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

In twenty twenty three, researchers found a shortcut. You can train on preferences directly, with no reward model, and no reinforcement learning. It's called DPO.

  • A 2023 shortcut
  • No reward model
  • No reinforcement learning

How DPO works

DPO stands for direct preference optimisation. It uses two copies of the model. The one being trained, and a frozen reference copy. The loss pushes the chosen answer to become more likely than in the reference, and the rejected answer less likely.

keyvalue
DPOdirect preference optimisation
Policythe model being trained
Referencea frozen copy
Goalchosen up, rejected down

Beta

One setting matters most. Beta. It controls how far the model may move away from the reference. A small beta, like zero point one, allows big changes. A large beta keeps the model close to where it started. With LoRA, switching the adapter off gives the reference model for free.

keyvalue
betahow far from the reference
Small (0.1)bigger changes
Large (0.5)stay close
With LoRAadapter off = reference

Our preference data

Here's our preference data. Two hundred pairs. Each has a customer message. The chosen answer is our friendly reply. The rejected answer is curt, and unhelpful. Run it. That's the whole idea. Same message, a better answer, and a worse one.

app.pypython
from prefs import pairs

pair = pairs[0]
print("Pairs:", len(pairs))
print("Customer:", pair["prompt"][1]["content"])
print("Chosen:  ", pair["chosen"][0]["content"][:70])
print("Rejected:", pair["rejected"][0]["content"])
Output
Pairs: 200
Customer: Hello! The driver was rude when delivering Dune.
Chosen:   I'm so sorry about your Dune order. I've passed this to our manager, w
Rejected: Not our problem.
Also uses prefs.py
from datasets import load_dataset

SYSTEM = "Write a short reply to the customer."

def to_pair(row):
    prompt = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": row["message"]}]
    return {"prompt": prompt,
            "chosen": [{"role": "assistant", "content": row["chosen"]}],
            "rejected": [{"role": "assistant", "content": row["rejected"]}]}

pairs = load_dataset("json", data_files="prefs_train.jsonl")["train"]
pairs = pairs.map(to_pair, remove_columns=["message"])

Summary

Let's recap. DPO trains on preferences directly. It compares the model with a frozen reference copy. And beta controls how far it may move. Next, we train with DPO, using TRL.

Key takeaways
  • No reward model
  • Frozen reference
  • beta
Next up: DPO with TRL — train on preferences