16.6 Optimization problem set, part b: constrained optimization, duality, and DPO
Post-training a language model, in the optional post-training chapter of AI Engineering, uses a KL penalty to keep an updated model near its supervised fine-tuned reference. The method that chapter builds, Direct preference optimization and IPO, rests on one closed-form result: the policy that maximizes reward minus a KL penalty is the reference policy tilted by the exponentiated reward. This problem set derives that result and the constrained-optimization machinery behind it, so that when you meet the DPO loss you can see where every term comes from.
Concepts from other sections (4)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- reference policy: the frozen model (usually the SFT checkpoint) against which the policy’s log-ratios are measured. Read more: 18.2 Direct preference optimization and IPO in AI Engineering
- DPO: minimizing over preference pairs, equivalent to fitting the KL-regularized reward objective. Read more: 18.2 Direct preference optimization and IPO in AI Engineering
- Lagrange multiplier: the scalar in at a constrained optimum. Read more: 10.3 Calculus 3 problem set: vectors, gradients, the chain rule, Hessians, multiple integrals, Lagrange
- KL divergence: , the excess cost of predicting with ; nonnegative and asymmetric. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
The set has 18 problems (17 exact answers and one short derivation) in three groups: Lagrange multipliers and the KKT conditions, duality, and the KL-regularized objective with DPO. It is optional pen-and-paper work. It assumes the constrained optimization and Lagrange material of the Calculus 3 problem set and the KL divergence of Entropy, cross-entropy, KL, JS, and the k3 estimator. Attempt each problem on paper before opening its answer. The answers name the common wrong answer and why it fails, since sign conventions are where these derivations usually go wrong.
Learning objectives
- Write the KKT conditions for a problem with constraints and solve small instances by hand, with multipliers of the correct sign.
- Form the dual function as an infimum over the primal variable, maximize it, and check for a zero duality gap.
- Derive the optimum of a KL-regularized reward objective, .
- Explain why the DPO log ratio cancels the intractable normalizer .
The toolkit
Section titled “The toolkit”For a minimization problem with inequality constraints , the Lagrangian is
The KKT conditions at an optimum are four: stationarity (), primal feasibility (), dual feasibility (), and complementary slackness (). The sign of the multiplier pairs with the sign convention of the constraint, which is why every inequality in this set is written as with before is formed. The dual function is , and the dual problem maximizes it over . Weak duality says the dual optimum never exceeds the primal one; strong duality, which holds under suitable convexity and feasibility assumptions such as Slater’s condition, makes them equal.
For a KL-regularized reward objective, maximizing gives
The symbols, briefly:
| Symbol | Meaning |
|---|---|
| inequality constraint, nonpositive when feasible | |
| nonnegative KKT multiplier | |
| fixed reference policy | |
| KL strength, positive | |
| normalizer over candidate completions |
A problem worked in full
Section titled “A problem worked in full”Minimize subject to , written . At , stationarity gives , hence , and the primal value is 1. For the KL side, take two outcomes with equal reference probability, rewards , and : the optimal policy has probabilities and log odds 1. These values reappear in q3 to q6 and q11 to q17.
The problems
Section titled “The problems”For KKT questions, use the convention g(x) <= 0 and multipliers lambda >= 0. Each answer gives the result, the derivation, and the common wrong answer. The first group (q1 to q10) catches sign-convention mistakes in stationarity and feasibility; the second (q11 to q14) builds the dual as an infimum over the primal variable and shows the gap is zero in this convex example; the third (q15 to q18) connects reward differences to policy-to-reference ratios.
Lagrange multipliers and KKT
Section titled “Lagrange multipliers and KKT”q1. Minimize x^2 + y^2 subject to x + y = 2. Give the minimizer. [vector]
Answer
[1, 1]. With , stationarity gives and , so ; the constraint then gives . Geometrically, this is the point of the line closest to the origin, where the gradient is parallel to the constraint normal . A common mistake is [0, 2]: feasible, but .
q2. For the same problem, give the equality multiplier using L=f+lambda*(x+y-2). [number]
Answer
. From at . An equality multiplier has no sign restriction; its sign depends only on how the constraint is written. A common mistake is , which is what the other convention gives.
q3. Minimize x^2 subject to x >= 1. Give the minimizer. [number]
Answer
. The unconstrained minimizer is infeasible, and is increasing on , so the constraint is active at the optimum. A common mistake is , which violates .
q4. Write the nonnegative multiplier for constraint 1-x <= 0 at the minimizer. [number]
Answer
. , so at gives . A common mistake is , from writing the constraint as or flipping the sign in : a negative multiplier on a g <= 0 constraint means the “optimum” is pushing the wrong way.
q5. For problem 3, give the Lagrangian gradient stationarity residual at x=1, lambda=2. [number]
Answer
0. . A common mistake is 4, reporting or the multiplier itself instead of the residual.
q6. Is complementary slackness satisfied at x=1, lambda=2? [bool]
Answer
true. . The constraint is active, so a nonzero multiplier is allowed.
q7. Minimize (x-3)^2 subject to x <= 1. Give the minimizer. [number]
Answer
. The unconstrained minimizer 3 is infeasible, and decreases on , so the optimum sits on the boundary. A common mistake is 3, ignoring the constraint.
q8. Give the nonnegative multiplier for x-1 <= 0 in problem 7. [number]
Answer
. , so at gives . Its size is the rate at which the optimal value would fall if the constraint were loosened: at . A common mistake is , a sign error in .
q9. At a strict feasible point, what value must an inactive inequality multiplier take? [number]
Answer
0. Complementary slackness with forces . An inactive constraint exerts no force on the optimum. A common mistake is 1, as if the multiplier had a default value.
q10. State whether x=0, lambda=0 is feasible for x >= 1, with g=1-x <= 0. [bool]
Answer
false. , so primal feasibility fails, even though stationarity of the unconstrained objective holds there (). This is why KKT is four conditions, not one: stationarity alone finds the unconstrained minimum.
Duality
Section titled “Duality”q11. Minimize x^2 subject to x >= 1. Give the primal optimal value. [number]
Answer
1. The minimizer is (q3), and . A common mistake is 0, the unconstrained minimum, which is infeasible.
q12. For L=x^2+lambda*(1-x), give the dual function inf_x L. [expr in lam]
Answer
. For fixed , is a convex quadratic in minimized at , . Substituting: . A common mistake is (dropping the terms); another is to maximize over , which gives .
q13. Give the dual optimal value for problem 11. [number]
Answer
1. Maximize over : at , which is the KKT multiplier of q4, and . A common mistake is 0, the value of at .
q14. Is the duality gap zero? [bool]
Answer
true. Primal 1 (q11) equals dual 1 (q13). Weak duality always gives dual primal; equality here follows from Slater’s condition: the objective and constraint are convex and there is a strictly feasible point (for example , where ).
KL-regularized objective and DPO
Section titled “KL-regularized objective and DPO”q15. For rewards [1,0], reference probabilities [1/2,1/2], and beta=1, give optimal probabilities proportional to p_ref * exp(reward/beta). [vector]
Answer
, about . The unnormalized weights are and ; dividing by their sum gives the result. A common mistake is , which ignores the reward tilt and returns the reference policy.
q16. Give the log-odds of the optimal policy from problem 15. [number]
Answer
1. , which equals plus the reference log odds . A common mistake is 0, which omits the reward difference.
q17. If chosen and rejected have equal policy/reference log odds, give the per-pair DPO loss. [number]
Answer
. The DPO loss is with the chosen log ratio minus the rejected log ratio. Equal log ratios give , and . This is the loss at initialization, when the policy equals the reference. A common mistake is 0: the loss is , and , not 0.
q18. Derive in one or two sentences why subtracting the reference log ratio recovers the reward difference divided by beta at the optimum. [proof]
Answer
At the optimum, , so . Taking this log ratio for the chosen minus the rejected under the same prompt , the shared cancels and leaves .
The cancellation is the whole point: is a sum over every possible completion and cannot be computed, but DPO only ever needs differences for the same prompt, so it never has to.
Mistakes to watch for
Section titled “Mistakes to watch for”Four mistakes account for most wrong answers in this set:
| Pitfall | How to avoid it |
|---|---|
Using a negative multiplier for g<=0 | write every inequality as g(x) <= 0 before forming L = f + lambda g, so stationarity gives lambda >= 0 (q4, q8) |
| Forgetting feasibility along with stationarity | check all four KKT conditions, not just the gradient (q10) |
Maximizing over x when forming the dual | the dual function is an infimum over x, then a maximum over lambda >= 0 (q12, q13) |
Keeping the unknown Z(x) in log odds | take a difference of log ratios for the same prompt, so log Z(x) cancels (q18) |
Where this leads
Section titled “Where this leads”The closed-form optimum of q15 and the cancellation of q18 are exactly what Direct preference optimization and IPO uses: DPO trains on the optimal policy’s reference-adjusted odds, and Capstone 2: post-trained TinyStories model applies the preference objective to a real model.
A caution about how far the derivation reaches. Strong duality needs assumptions such as convex objectives, convex constraints, and a strict feasible point. Real preference learning also has noisy labels and finite data, so a closed-form derivation is a guide to the objective rather than a guarantee of learned behavior.
Further reading
Section titled “Further reading”- Boyd and Vandenberghe, Convex Optimization, chapter 5.
- Rafailov et al., “Direct Preference Optimization: Your Language Model is Secretly a Reward Model” (2023), for the DPO derivation.
Summary
Section titled “Summary”- KKT multipliers pair with the chosen inequality sign convention: with and , multipliers are nonnegative, and an inactive constraint has .
- Strong duality equates primal and dual optima under suitable convexity and feasibility assumptions, such as Slater’s condition.
- A KL-regularized policy optimum is proportional to the reference policy times exponentiated reward.
- The DPO log ratio subtracts away the unknown normalization constant .