Unit 1 of 11 · about 4 hours · derive

The KL-regularized objective and its optimal policy

DPO starts with a reward-maximization problem, not a classification trick. Its closed-form solution tells you which reference and support assumptions the later loss inherits.

You'll be able to

  • Derive the optimal completion distribution for a fixed reward and reference policy.
  • Explain how KL direction, reference support and the regularization coefficient constrain that solution.

Covers

KL-regularized reward-maximization objectiveOptimal policy form under a reference policy
1

Read

In this order, only the parts listed

0 of 2 done

  1. The RLHF Book: the optimal RLHF solutionRLHF Book - Direct Alignment

    Chapter 8, DPO Derivation: Deriving the Optimal RLHF Solution (page read 2026-10-14)

    Stop before Deriving DPO Objectives for BT Models. Track maximizers, not equality of the objective values after sign changes and division by the regularization coefficient.

  2. DPO: optimal-policy derivationDirect Preference Optimization

    arXiv:2305.18290v3, Section 3 equation 3, Section 4 equation 4 and Appendix A.1

2

Focus on

  • For each prompt, maximize expected reward minus , with . The expectation defining KL is under the policy being optimized.
  • The solution is . Require a finite partition function and optimize on reference support.
  • Reuse Entropy, cross-entropy, KL divergence and mutual information and Constraints, KKT conditions and proximal methods. Here the new step is rewriting the reward objective as .
Common mistakes · 3
  • Reversing KL. A reference-to-policy penalty yields a different optimization problem.
  • Reading the penalty coefficient as a hard KL budget. A finite-data DPO run does not attain a prescribed divergence simply by setting it.
  • Giving positive mass to a completion with zero reference probability. Its policy-to-reference KL is infinite.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive the solution by normalization and certify it with the KL rewrite. For one prompt and two completions, take reference probabilities , rewards and . Obtain , and optimal objective . Check in CPU PyTorch with torch.float64, natural logs and absolute tolerance ; no sampling or model is needed.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why does the partition function not change while optimizing the policy?Answer

    The reward, reference and regularization coefficient are fixed. The partition function depends on these and the prompt, not on the candidate policy.

  2. 2.What happens as the regularization coefficient grows without bound for bounded rewards?Answer

    The exponential reward tilt approaches one, and the optimum approaches the reference distribution.

  3. 3.Can a single sampled log-ratio be negative although KL is nonnegative?Answer

    Yes. Nonnegativity holds for the expectation under the policy, not for each completion's log-ratio.

5

Practice

  • TensorTonic: Score a KL-Regularized Policy Batch

    Language Modeling from Scratch: kl_regularized_policy_objective; all three public cases

    The free problem checks positive sampled divergence, a mixed masked batch and a fully masked response. It scores supplied log probabilities; it does not prove the optimal-policy formula or estimate KL from arbitrary offline preferences.

You're done when

You derive and certify the two-completion optimum unaided and pass the named problem without clamping negative sampled divergences.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 8 more in the section
  • RLHF Book - Direct AlignmentNathan Lambert · primary

    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. · required

    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.

Also in this section

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

    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.

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

    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) · support

    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.

  • Stanford CS224R 2026 - LLM Preference OptimizationStanford University · required

    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.

  • TRL DPO TrainerHugging Face · support

    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 · support

    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 · support

    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.

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

    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.