TensorTonic
Study Plans
Study Plan
Implement the DPO Loss
Loss FunctionsLanguage ModelsReinforcement Learning
Medium

Compute the Direct Preference Optimization loss for preferred and rejected response pairs. For pair i, let ℓi+\ell_i^+ℓi+​ and ℓi−\ell_i^-ℓi−​ be the policy's supplied sequence log probabilities, and ℓref,i+\ell_{\mathrm{ref},i}^+ℓref,i+​ and ℓref,i−\ell_{\mathrm{ref},i}^-ℓref,i−​ the reference model's. With scale β\betaβ, define

zi=β[(ℓi+−ℓi−)−(ℓref,i+−ℓref,i−)].z_i=\beta\left[(\ell_i^+-\ell_i^-)-(\ell_{\mathrm{ref},i}^+-\ell_{\mathrm{ref},i}^-)\right].zi​=β[(ℓi+​−ℓi−​)−(ℓref,i+​−ℓref,i−​)].

For label smoothing ϵ\epsilonϵ, the per-pair loss is

Li=−(1−ϵ)log⁡σ(zi)−ϵlog⁡σ(−zi).L_i=-(1-\epsilon)\log\sigma(z_i)-\epsilon\log\sigma(-z_i).Li​=−(1−ϵ)logσ(zi​)−ϵlogσ(−zi​).

Here, σ\sigmaσ is the logistic sigmoid and log is the natural logarithm. The implicit rewards are

ri+=β(ℓi+−ℓref,i+)r_i^+=\beta(\ell_i^+-\ell_{\mathrm{ref},i}^+)ri+​=β(ℓi+​−ℓref,i+​) ri−=β(ℓi−−ℓref,i−).r_i^-=\beta(\ell_i^--\ell_{\mathrm{ref},i}^-).ri−​=β(ℓi−​−ℓref,i−​).

The loss is the mean of LiL_iLi​. Preference accuracy is the fraction of pairs with strictly greater chosen reward; ties count as incorrect.

Return a Python dictionary of PyTorch tensors: loss and preference_accuracy are scalars; per_example_loss, chosen_rewards, and rejected_rewards each have shape (batch,). Every output preserves the input floating dtype and device.

Loading visualization...

Examples

Input: policy_chosen = [-1], policy_rejected = [-2], reference_chosen = [-1.5], reference_rejected = [-1.5], beta = 0.5, label_smoothing = 0

Output: {"loss":0.474077,"per_example_loss":[0.474077],"chosen_rewards":[0.25],"rejected_rewards":[-0.25],"preference_accuracy":1}

Explanation: The policy gives the preferred response a larger relative margin than the reference, producing a positive DPO logit.

Input: policy_chosen = [-2,-1], policy_rejected = [-1,-2], reference_chosen = [-1,-1], reference_rejected = [-1,-1], beta = 1, label_smoothing = 0

Output: {"loss":0.813262,"per_example_loss":[1.313262,0.313262],"chosen_rewards":[-1,0],"rejected_rewards":[0,-1],"preference_accuracy":0.5}

Input: policy_chosen = [0], policy_rejected = [0], reference_chosen = [0], reference_rejected = [0], beta = 2, label_smoothing = 0.1

Output: {"loss":0.693147,"per_example_loss":[0.693147],"chosen_rewards":[0],"rejected_rewards":[0],"preference_accuracy":0}

Hint 1

torch.nn.functional.logsigmoid evaluates log-sigmoid stably.

Hint 2

Tensor.mean reduces a batch vector to a scalar tensor.

Hint 3

A comparison produces a Boolean tensor that can be converted to the input dtype.

Requirements

  • Use PyTorch and preserve the supplied floating dtype and device.
  • Evaluate the smoothed loss stably for large positive or negative preference margins.
  • Do not modify input tensors.

Constraints

  • All four log-probability inputs are aligned one-dimensional PyTorch tensors from a nonempty batch.
  • Floating tensors share dtype and device; editor inputs use float32.
  • beta is positive and finite; label_smoothing lies in [0, 0.5].
  • Inputs are finite and may be noncontiguous.
Try Similar Problems
Score a KL-Regularized Policy BatchMediumCompare Sequence and Token Length NormalizationMediumCanonicalize Pairwise Preference DataMediumCompute a Clipped GRPO SurrogateHardBuild an Assistant-Only SFT Loss MaskEasyImplement Symmetric CLIP Contrastive LossMediumNormalize GRPO Group AdvantagesMediumImplement the SigLIP Pairwise LossMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMediumPPO Clipped Surrogate ObjectiveMediumDQN Loss with Target NetworkMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.

Implement the DPO Loss

Loss FunctionsLanguage ModelsReinforcement Learning
Medium

Compute the Direct Preference Optimization loss for preferred and rejected response pairs. For pair i, let ℓi+\ell_i^+ℓi+​ and ℓi−\ell_i^-ℓi−​ be the policy's supplied sequence log probabilities, and ℓref,i+\ell_{\mathrm{ref},i}^+ℓref,i+​ and ℓref,i−\ell_{\mathrm{ref},i}^-ℓref,i−​ the reference model's. With scale β\betaβ, define

zi=β[(ℓi+−ℓi−)−(ℓref,i+−ℓref,i−)].z_i=\beta\left[(\ell_i^+-\ell_i^-)-(\ell_{\mathrm{ref},i}^+-\ell_{\mathrm{ref},i}^-)\right].zi​=β[(ℓi+​−ℓi−​)−(ℓref,i+​−ℓref,i−​)].

For label smoothing ϵ\epsilonϵ, the per-pair loss is

Li=−(1−ϵ)log⁡σ(zi)−ϵlog⁡σ(−zi).L_i=-(1-\epsilon)\log\sigma(z_i)-\epsilon\log\sigma(-z_i).Li​=−(1−ϵ)logσ(zi​)−ϵlogσ(−zi​).

Here, σ\sigmaσ is the logistic sigmoid and log is the natural logarithm. The implicit rewards are

ri+=β(ℓi+−ℓref,i+)r_i^+=\beta(\ell_i^+-\ell_{\mathrm{ref},i}^+)ri+​=β(ℓi+​−ℓref,i+​) ri−=β(ℓi−−ℓref,i−).r_i^-=\beta(\ell_i^--\ell_{\mathrm{ref},i}^-).ri−​=β(ℓi−​−ℓref,i−​).

The loss is the mean of LiL_iLi​. Preference accuracy is the fraction of pairs with strictly greater chosen reward; ties count as incorrect.

Return a Python dictionary of PyTorch tensors: loss and preference_accuracy are scalars; per_example_loss, chosen_rewards, and rejected_rewards each have shape (batch,). Every output preserves the input floating dtype and device.

Loading visualization...

Examples

Input: policy_chosen = [-1], policy_rejected = [-2], reference_chosen = [-1.5], reference_rejected = [-1.5], beta = 0.5, label_smoothing = 0

Output: {"loss":0.474077,"per_example_loss":[0.474077],"chosen_rewards":[0.25],"rejected_rewards":[-0.25],"preference_accuracy":1}

Explanation: The policy gives the preferred response a larger relative margin than the reference, producing a positive DPO logit.

Input: policy_chosen = [-2,-1], policy_rejected = [-1,-2], reference_chosen = [-1,-1], reference_rejected = [-1,-1], beta = 1, label_smoothing = 0

Output: {"loss":0.813262,"per_example_loss":[1.313262,0.313262],"chosen_rewards":[-1,0],"rejected_rewards":[0,-1],"preference_accuracy":0.5}

Input: policy_chosen = [0], policy_rejected = [0], reference_chosen = [0], reference_rejected = [0], beta = 2, label_smoothing = 0.1

Output: {"loss":0.693147,"per_example_loss":[0.693147],"chosen_rewards":[0],"rejected_rewards":[0],"preference_accuracy":0}

Hint 1

torch.nn.functional.logsigmoid evaluates log-sigmoid stably.

Hint 2

Tensor.mean reduces a batch vector to a scalar tensor.

Hint 3

A comparison produces a Boolean tensor that can be converted to the input dtype.

Requirements

  • Use PyTorch and preserve the supplied floating dtype and device.
  • Evaluate the smoothed loss stably for large positive or negative preference margins.
  • Do not modify input tensors.

Constraints

  • All four log-probability inputs are aligned one-dimensional PyTorch tensors from a nonempty batch.
  • Floating tensors share dtype and device; editor inputs use float32.
  • beta is positive and finite; label_smoothing lies in [0, 0.5].
  • Inputs are finite and may be noncontiguous.
Try Similar Problems
Score a KL-Regularized Policy BatchMediumCompare Sequence and Token Length NormalizationMediumCanonicalize Pairwise Preference DataMediumCompute a Clipped GRPO SurrogateHardBuild an Assistant-Only SFT Loss MaskEasyImplement Symmetric CLIP Contrastive LossMediumNormalize GRPO Group AdvantagesMediumImplement the SigLIP Pairwise LossMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMediumPPO Clipped Surrogate ObjectiveMediumDQN Loss with Target NetworkMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.