Skip to content

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.

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 gi(x)≤0g_i(x) \le 0 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, π∗∝πrefexp⁡(r/β)\pi^* \propto \pi_{\text{ref}} \exp(r/\beta).
  • Explain why the DPO log ratio cancels the intractable normalizer Z(x)Z(x).

For a minimization problem with inequality constraints gi(x)≤0g_i(x) \le 0, the Lagrangian is

L(x,λ)=f(x)+∑iλi gi(x).L(x, \lambda) = f(x) + \sum_i \lambda_i\, g_i(x).

The KKT conditions at an optimum are four: stationarity (∇xL=0\nabla_x L = 0), primal feasibility (gi(x)≤0g_i(x) \le 0), dual feasibility (λi≥0\lambda_i \ge 0), and complementary slackness (λi gi(x)=0\lambda_i\, g_i(x) = 0). The sign of the multiplier pairs with the sign convention of the constraint, which is why every inequality in this set is written as g(x)≤0g(x) \le 0 with λ≥0\lambda \ge 0 before LL is formed. The dual function is d(λ)=inf⁡xL(x,λ)d(\lambda) = \inf_x L(x, \lambda), and the dual problem maximizes it over λ≥0\lambda \ge 0. 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 Eπ[r]−β DKL(π ∥ πref)\mathbb{E}_{\pi}[r] - \beta\, D_{\mathrm{KL}}(\pi \,\|\, \pi_{\text{ref}}) gives

π∗(y∣x)=πref(y∣x) exp⁡(r(x,y)/β)Z(x).\pi^*(y \mid x) = \frac{\pi_{\text{ref}}(y \mid x)\, \exp(r(x, y)/\beta)}{Z(x)}.

The symbols, briefly:

SymbolMeaning
gi(x)g_i(x)inequality constraint, nonpositive when feasible
λi\lambda_inonnegative KKT multiplier
πref\pi_{\text{ref}}fixed reference policy
β\betaKL strength, positive
Z(x)Z(x)normalizer over candidate completions

Minimize x2x^2 subject to x≥1x \ge 1, written 1−x≤01 - x \le 0. At x=1x = 1, stationarity gives 2x−λ=02x - \lambda = 0, hence λ=2\lambda = 2, and the primal value is 1. For the KL side, take two outcomes with equal reference probability, rewards [1,0][1, 0], and β=1\beta = 1: the optimal policy has probabilities [e/(1+e), 1/(1+e)][e/(1+e),\ 1/(1+e)] and log odds 1. These values reappear in q3 to q6 and q11 to q17.

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.

q1. Minimize x^2 + y^2 subject to x + y = 2. Give the minimizer. [vector]

Answer

[1, 1]. With L=x2+y2+λ(x+y−2)L = x^2 + y^2 + \lambda(x + y - 2), stationarity gives 2x+λ=02x + \lambda = 0 and 2y+λ=02y + \lambda = 0, so x=yx = y; the constraint then gives x=y=1x = y = 1. Geometrically, this is the point of the line x+y=2x + y = 2 closest to the origin, where the gradient (2x,2y)(2x, 2y) is parallel to the constraint normal (1,1)(1, 1). A common mistake is [0, 2]: feasible, but f=4>2f = 4 > 2.

q2. For the same problem, give the equality multiplier using L=f+lambda*(x+y-2). [number]

Answer

λ=−2\lambda = -2. From 2x+λ=02x + \lambda = 0 at x=1x = 1. An equality multiplier has no sign restriction; its sign depends only on how the constraint is written. A common mistake is +2+2, which is what the other convention L=f−λ(x+y−2)L = f - \lambda(x + y - 2) gives.

q3. Minimize x^2 subject to x >= 1. Give the minimizer. [number]

Answer

x=1x = 1. The unconstrained minimizer x=0x = 0 is infeasible, and x2x^2 is increasing on [1,∞)[1, \infty), so the constraint is active at the optimum. A common mistake is −1-1, which violates x≥1x \ge 1.

q4. Write the nonnegative multiplier for constraint 1-x <= 0 at the minimizer. [number]

Answer

λ=2\lambda = 2. L=x2+λ(1−x)L = x^2 + \lambda(1 - x), so ∂L/∂x=2x−λ=0\partial L/\partial x = 2x - \lambda = 0 at x=1x = 1 gives λ=2≥0\lambda = 2 \ge 0. A common mistake is −2-2, from writing the constraint as x−1≤0x - 1 \le 0 or flipping the sign in LL: 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. 2x−λ=2−2=02x - \lambda = 2 - 2 = 0. A common mistake is 4, reporting 2x+λ2x + \lambda or the multiplier itself instead of the residual.

q6. Is complementary slackness satisfied at x=1, lambda=2? [bool]

Answer

true. λ g(x)=2(1−1)=0\lambda\,g(x) = 2(1 - 1) = 0. The constraint is active, so a nonzero multiplier is allowed.

q7. Minimize (x-3)^2 subject to x <= 1. Give the minimizer. [number]

Answer

x=1x = 1. The unconstrained minimizer 3 is infeasible, and (x−3)2(x - 3)^2 decreases on (−∞,1](-\infty, 1], 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

λ=4\lambda = 4. L=(x−3)2+λ(x−1)L = (x - 3)^2 + \lambda(x - 1), so 2(x−3)+λ=02(x - 3) + \lambda = 0 at x=1x = 1 gives λ=4\lambda = 4. Its size is the rate at which the optimal value would fall if the constraint were loosened: ddb(b−3)2=−4\frac{d}{db}(b - 3)^2 = -4 at b=1b = 1. A common mistake is −4-4, a sign error in gg.

q9. At a strict feasible point, what value must an inactive inequality multiplier take? [number]

Answer

0. Complementary slackness λigi(x)=0\lambda_i g_i(x) = 0 with gi(x)<0g_i(x) < 0 forces λi=0\lambda_i = 0. 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. g(0)=1>0g(0) = 1 > 0, so primal feasibility fails, even though stationarity of the unconstrained objective holds there (2⋅0−0=02 \cdot 0 - 0 = 0). This is why KKT is four conditions, not one: stationarity alone finds the unconstrained minimum.

q11. Minimize x^2 subject to x >= 1. Give the primal optimal value. [number]

Answer

1. The minimizer is x=1x = 1 (q3), and f(1)=1f(1) = 1. 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

λ−λ2/4\lambda - \lambda^2/4. For fixed λ\lambda, LL is a convex quadratic in xx minimized at 2x−λ=02x - \lambda = 0, x=λ/2x = \lambda/2. Substituting: λ2/4+λ−λ2/2=λ−λ2/4\lambda^2/4 + \lambda - \lambda^2/2 = \lambda - \lambda^2/4. A common mistake is λ\lambda (dropping the xx terms); another is to maximize over xx, which gives +∞+\infty.

q13. Give the dual optimal value for problem 11. [number]

Answer

1. Maximize d(λ)=λ−λ2/4d(\lambda) = \lambda - \lambda^2/4 over λ≥0\lambda \ge 0: d′(λ)=1−λ/2=0d'(\lambda) = 1 - \lambda/2 = 0 at λ=2\lambda = 2, which is the KKT multiplier of q4, and d(2)=2−1=1d(2) = 2 - 1 = 1. A common mistake is 0, the value of dd at λ=0\lambda = 0.

q14. Is the duality gap zero? [bool]

Answer

true. Primal 1 (q11) equals dual 1 (q13). Weak duality always gives dual ≤\le primal; equality here follows from Slater’s condition: the objective and constraint are convex and there is a strictly feasible point (for example x=2x = 2, where g=−1<0g = -1 < 0).

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

[e/(1+e), 1/(1+e)][e/(1 + e),\ 1/(1 + e)], about [0.731,0.269][0.731, 0.269]. The unnormalized weights are 12e1\frac12 e^1 and 12e0\frac12 e^0; dividing by their sum Z=12(e+1)Z = \frac12(e + 1) gives the result. A common mistake is [1/2,1/2][1/2, 1/2], 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. log⁡e/(1+e)1/(1+e)=log⁡e=1\log\frac{e/(1+e)}{1/(1+e)} = \log e = 1, which equals (r1−r2)/β(r_1 - r_2)/\beta plus the reference log odds log⁡(12/12)=0\log(\frac12/\frac12) = 0. 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

log⁡2\log 2. The DPO loss is −log⁡σ(β Δ)-\log\sigma(\beta\,\Delta) with Δ\Delta the chosen log ratio minus the rejected log ratio. Equal log ratios give Δ=0\Delta = 0, and −log⁡σ(0)=−log⁡12=log⁡2≈0.693-\log\sigma(0) = -\log\frac12 = \log 2 \approx 0.693. This is the loss at initialization, when the policy equals the reference. A common mistake is 0: the loss is softplus(−βΔ)\mathrm{softplus}(-\beta\Delta), and softplus(0)=log⁡2\mathrm{softplus}(0) = \log 2, 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, π∗(y∣x)=πref(y∣x)exp⁡(r(x,y)/β)/Z(x)\pi^*(y \mid x) = \pi_{\text{ref}}(y \mid x)\exp(r(x, y)/\beta)/Z(x), so log⁡π∗(y∣x)πref(y∣x)=r(x,y)β−log⁡Z(x)\log\frac{\pi^*(y \mid x)}{\pi_{\text{ref}}(y \mid x)} = \frac{r(x, y)}{\beta} - \log Z(x). Taking this log ratio for the chosen ywy_w minus the rejected yly_l under the same prompt xx, the shared log⁡Z(x)\log Z(x) cancels and leaves r(x,yw)−r(x,yl)β\frac{r(x, y_w) - r(x, y_l)}{\beta}.

The cancellation is the whole point: Z(x)Z(x) 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.

Four mistakes account for most wrong answers in this set:

PitfallHow to avoid it
Using a negative multiplier for g<=0write every inequality as g(x) <= 0 before forming L = f + lambda g, so stationarity gives lambda >= 0 (q4, q8)
Forgetting feasibility along with stationaritycheck all four KKT conditions, not just the gradient (q10)
Maximizing over x when forming the dualthe dual function is an infimum over x, then a maximum over lambda >= 0 (q12, q13)
Keeping the unknown Z(x) in log oddstake a difference of log ratios for the same prompt, so log Z(x) cancels (q18)

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.

  • 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.
  • KKT multipliers pair with the chosen inequality sign convention: with g(x)≤0g(x) \le 0 and L=f+λgL = f + \lambda g, multipliers are nonnegative, and an inactive constraint has λ=0\lambda = 0.
  • 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 Z(x)Z(x).