LLM RL III: Direct Preference Optimization

Direct preference methods use an offline comparison dataset and a reference policy rather than fresh reward-guided rollouts. This stage derives that relationship precisely, implements sequence-level objectives and separates genuine preference learning from data support, formatting and length artifacts.

Section 10 of 12 · 11 units · about 43 hours

0/11

By the end you can Derive and implement DPO from sequence log probabilities, pass sign, masking, reference and gradient oracles, and show with held-out evaluation how , length, label noise or dataset support can reverse an apparent preference gain.

Units

Build it

Mini project · about 12 to 16 hours

Direct preference optimization with controlled comparisons

Derive and test DPO from scratch in policylab, then separate real preference gains from scoring, reference and selection effects. The checked loss, baselines and ledgers feed online-policy training and the capstone evidence pack.

  1. 1Derive the KL-regularized optimum and DPO loss in llm/ from policy ratios, verify the same-prompt reward-shift oracle against hand algebra, list preference and reference assumptions, and pass the free TensorTonic KL-regularized policy objective cases.
  2. 2Build completion scoring in llm/ with shifted masks and explicit EOS, padding, empty and truncation rules, show token evidence for each policy-reference pair, and pass the free TensorTonic DPO loss and canonical-pair cases plus your own gradient and swap tests.
  3. 3Bind reference scores to checkpoint, tokenizer, template and mask identity in schema/, inject a deliberate reference-drift failure, audit support gaps, and differential-test loss and gradients against the pinned RLHF Book DPOLoss with mask, sign and reference regressions and diagnostic records in tests/.
  4. 4Compare small SFT, DPO and no-reference runs on shared data in llm/, separating score normalization from reference removal, then contrast DPO with IPO on synthetic CPU preferences under noise and reuse with smoothed-loss stationary-point checks.
  5. 5Run a bounded sensitivity study over regularization, epochs, reuse or label noise, report held-out prompt and annotator slices with raw generations, and retain one proxy-versus-behavior failure where pair accuracy rises while independent behavior falls.
  6. 6Compare DPO with SFT, rejection and best-of-N controls from a CPU text policy under a stated budget, keep ledgers in ledger/, and defend the frozen task, safety and style verdict with negative evidence across seeds, moving to a decoder only if your GPU fits it.
What you hand in
  • The llm/ DPO package with derivation, scoring oracles and regression tests.
  • Baseline, variant and sensitivity tables with raw generations and failures.
  • Compute and data ledgers with the frozen evaluation and defended claim.
How to check it
How it's graded

Derivation honesty

Pass: The shift oracle matches hand algebra and every preference and reference assumption is listed.

Great: A cross-prompt comparison is shown to break cancellation with a failing test.

Scoring correctness

Pass: Masks, lengths, templates and truncation pass oracles with token evidence and gradient checks.

Great: A means-versus-sums pair flips the margin and the test names the changed objective.

Attribution discipline

Pass: Baselines, IPO comparison and sensitivity slices separate objective, data and evaluator effects.

Great: A retained failure shows pair gains with flat or falling behavior and its cause.

Ledger completeness

Pass: Budgets, candidates, calls and seeds reconcile from raw records with negative evidence kept.

Great: An independent rerun rebuilds every table from raw records alone.

Resources

  • RLHF Book - Direct AlignmentNathan Lambert

    Study Chapter 8 (DPO, its derivation, numerical concerns and online versus offline data) and the code/direct_alignment package at natolambert/rlhf-book at commit 1042143 (DPO, IPO, KTO and APO are validated there; its SimPO and ORPO runs are marked noisy). Re-derive DPO from the KL-regularized objective, implement sequence log-ratio losses from blank tensors and test chosen-rejected swaps, prompt masking, reference drift and length normalization.

  • Direct Preference OptimizationRafailov et al.

    Read the complete derivation, objective and experiments. State the preference-model and reference assumptions, then compare a scratch DPO update with SFT and rejection-style baselines on a held-out slice.

  • A General Theoretical Paradigm to Understand Learning from Human PreferencesGheshlaghi Azar et al.

    Study the preference-model misspecification argument and IPO objective. Use one synthetic preference problem to compare DPO and IPO behavior as noise, margin and data reuse change.

  • Stanford CS224R 2026 - LLM Preference OptimizationStanford University

    Study Lecture 09 (RL for LLMs: preference optimization, slides/09_cs224r_rlhf_2026.pdf) and Section 1.3.2 (IPO) of the default project guidelines. Implement independently, preserve the reference policy and follow course material restrictions.

  • LLM Post-Training Laboratory - Direct Preference ObjectiveRoadmap-Final

    Complete the DPO-style objective with exact sequence log probabilities, frozen reference identity, fixed loss and gradient tests, common raw evaluation records, support analysis and reward, length, style and judge-hacking probes.

  • KTO: Model Alignment as Prospect Theoretic OptimizationEthayarajh et al. (ICML 2024)

    Read the unpaired-feedback setting and the KTO loss with its reference point. State what data KTO needs that DPO does not, and test its loss on fixed log probabilities.

  • SimPO: Simple Preference Optimization with a Reference-Free RewardMeng, Xia and Chen (NeurIPS 2024)

    Read the length-normalized, reference-free reward and the target margin. Use it as the no-reference baseline and compare its length behavior with summed-log-probability DPO.

  • TRL DPO TrainerHugging Face

    After the scratch loss passes, inspect the DPOTrainer data contract and options for TRL 1.15.0. Differential-test one tiny batch against the local objective and record library version, loss type, , padding, truncation and reference behavior.

  • TensorTonic - DPO and Preference ProblemsTensorTonic

    Solve Implement the DPO Loss (cs336-l15-direct-preference-optimization-loss), Canonicalize Pairwise Preference Data (cs336-l15-canonical-preference-pairs) and Score a KL-Regularized Policy Batch (cs336-l15-kl-regularized-policy-objective), free problems in the Language Modeling from Scratch plan, as closed-book checks after your own loss passes.

  • Hands-on Modern RL - Chapter 14 DPOWalking Labs

    Use the DPO derivation page and the hands-on page for a bounded DPO experiment and metrics. Its displayed loss is the per-pair summand (average it over the dataset), and its implicit reward is exact only at the KL-regularized optimum; its training logs are illustrative. Replace any opaque trainer-only step with a checked local loss and retain a or length regime where quality worsens. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; its code/chapterNN folders do not follow the site chapter numbers.