From Pieces to DPO

Introduction

Project introduction

From Pieces to DPO

You have written every piece DPO needs, including the Bradley-Terry probability, the masked log-probability of a reply, the DPO loss with its implicit rewards, the reward accuracy and margin, the IPO loss, and per-token KL divergence.

In the notebook they align a real model. A small transformer that answers requests at a made-up help desk, rudely about half the time, trains on 600 pairs in which the polite reply won.

What you will see

After 100 short steps the model is polite on every one of 120 requests it never trained on, and it still carries out every request. Looking inside shows how it happened. The rude replies became thousands of times less likely, while the polite ones gained only a little. The loss asks for a gap, and pushing the rejected reply down is the easiest way to make one.

Then you change beta. A smaller beta still gives perfectly polite replies, so the preference numbers look just as good, but KL on neutral text grows about 16 times, and a very small beta breaks the model while the loss reports success. Beta sets how big a gap the loss asks for; the step size, scaled here so every beta starts at the same speed, sets how fast a run gets there, and the notebook shows that a fixed step size gives a different picture. In this tiny model the tone comes down to a single word, so these effects are larger and faster than in big models, which train with much smaller steps and usually start from beta 0.1.

One preference pair, before and after DPO

training 0%

The prompt is please fix the gate .

chosenlog-prob -1.78

i will fix the gate now . happy to help .

rejectedlog-prob -1.82

i will fix the gate now . do not ask again .

The log-probability is the log of how likely the model is to write the reply; 0 is certain, and a longer bar is more likelypolite replies 54%
Preference pairschosen beats rejected
Frozen referencethe starting model
DPO losswiden the gap
Aligned policypolite, still on task

How the notebook is organized

01

Build DPO

Score replies, freeze a reference, write the loss and metrics, and train.

02

Inspect the result

Check tone and task on new requests, and find which tokens moved.

03

Sweep beta

Measure drift with KL on neutral text, compare four beta values, and try IPO.

Align, inspect, and stress-test a language model

Run the whole investigation in the browser, about 45 minutes.

Open the notebook project