DPO Loss and Gradient in NumPy
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement `dpo_loss` and `dpo_loss_grad` from per-example policy and reference log-probabilities of chosen and rejected responses. Both take the same arguments:
```python
dpo_loss(policy_chosen_logps, policy_rejected_logps, ref_chosen_logps,
ref_rejected_logps, beta=0.1, label_smoothing=0.0, reduction="mean")
```
Write `pc`, `pr`, `rc`, `rr` for the four inputs in that order. With `z = beta * ((pc - rc) - (pr - rr))` and smoothing $\varepsilon$ = `label_smoothing`, the per-example loss is
$$
\ell = -(1 - \varepsilon)\log\sigma(z) - \varepsilon \log\sigma(-z)
$$
The log-sigmoid must be numerically stable (finite for $|z|$ in the thousands).
- `reduction`: `"mean"` and `"sum"` return a Python `float`, and `"none"` returns the per-example array.
- Any other `reduction` raises `ValueError` in both functions.
- `dpo_loss_grad` returns a tuple `(grad_policy_chosen, grad_policy_rejected)` of arrays shaped like the inputs, where `grad_policy_rejected == -grad_policy_chosen`.
- Gradients are of the reduced loss: `"mean"` divides by N, and `"sum"` and `"none"` give the unscaled per-example gradient.
- Gradients must stay finite and exact at extreme margins.
For example:
```python
pc = [1000., -1000.]; pr = [-1000., 1000.]; rc = rr = [0., 0.]
dpo_loss(pc, pr, rc, rr, beta=1.0, reduction="none") # [0.0, 2000.0]
dpo_loss_grad(pc, pr, rc, rr, beta=1.0) # ([0.0, -0.5], [0.0, 0.5])
# zero margin (policy == reference) -> loss = log(2) for any beta
```
```hint Stable log-sigmoid
Write $\log\sigma(x)$ in terms of $\log(1 + e^{-x})$, then find a NumPy routine that evaluates it without forming $e^{-x}$ when $x$ is very negative. Check its value by hand at $x = 2000$ and $x = -2000$.
```
```hint One scalar per example
The inputs reach the loss only through $z$. Find $d\ell/dz$ first, including the smoothing term, then look at how `pc` and `pr` each enter $z$.
```
```hint Check the reductions
Decide where the factor ${1/N}$ belongs for `"mean"`, and confirm that the zero-margin case gives log 2 before you trust the extreme values.
```
### Constraints & Assumptions
- All four inputs are 1-D array-likes of equal length N ≥ 1.
- `beta > 0` and `0 ≤ label_smoothing ≤ 0.5`.
- Use NumPy only (no autograd).
### What a Strong Answer Covers
- A log-sigmoid that stays finite and exact for $|z|$ in the thousands, used by both the loss and the gradient.
- The derivative of the smoothed loss with respect to $z$, including the $\varepsilon$ term and the chain-rule factor `beta`.
- Opposite-signed gradients for the chosen and rejected policy log-probabilities.
- Reduction handling that agrees between loss and gradient, with `ValueError` for unknown values.
- Sanity checks against the zero-margin value and the extreme example.
### Follow-up Questions
- How would you switch this to the IPO objective?
- How are the sequence log-probabilities computed from token logits?
- What metrics would you log during DPO training?
- With `label_smoothing > 0`, at what margin is the per-example loss minimized?
Overview: Implement the Direct Preference Optimization loss and its gradient in NumPy from policy and reference log-probabilities, with label smoothing and three reductions. It tests a numerically stable log-sigmoid at extreme margins, the chain rule through the reward margin, and consistent gradient scaling.