Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Huggingface Trl DPO Evaluation and Saving

From Leeroopedia


Knowledge Sources
Domains NLP, RLHF
Last Updated 2026-02-06 17:00 GMT

Overview

Evaluating DPO-trained models using preference-specific metrics and persisting the trained weights are the final stages of the DPO workflow.

Description

After training, evaluating a DPO model requires metrics that go beyond standard language modeling perplexity. The evaluation must assess whether the model has learned to prefer chosen responses over rejected ones according to the implicit reward function.

Evaluation metrics computed by the DPO evaluation loop include:

  • eval_loss: The DPO loss on the evaluation set, computed identically to the training loss.
  • rewards/chosen: The mean implicit reward for chosen responses, computed as beta * (log pi_theta(y_w|x) - log pi_ref(y_w|x)). Higher values indicate the policy assigns more probability to chosen responses relative to the reference.
  • rewards/rejected: The mean implicit reward for rejected responses. Lower values indicate the policy successfully down-weights rejected responses.
  • rewards/margins: The difference between chosen and rejected rewards. This is the primary metric for DPO alignment quality -- larger margins indicate stronger preference separation.
  • rewards/accuracies: The fraction of evaluation samples where the chosen reward exceeds the rejected reward. This directly measures how often the model's implicit ranking agrees with the ground truth preferences.
  • logps/chosen: Mean log probability of chosen responses under the policy model.
  • logps/rejected: Mean log probability of rejected responses under the policy model.
  • logits/chosen: Mean logit values for chosen responses.
  • logits/rejected: Mean logit values for rejected responses.

Optional generation during evaluation: The DPOTrainer can optionally generate sample completions from both the policy and reference models during evaluation (when generate_during_eval=True), logging them to Weights and Biases, Comet, or MLflow for qualitative inspection.

Model saving follows standard Hugging Face conventions:

  • save_model persists the model weights (or adapter weights for PEFT models) and tokenizer to a specified directory
  • push_to_hub uploads the model to the Hugging Face Hub
  • A model card is automatically generated and saved alongside checkpoints

Usage

Evaluate and save a DPO model when:

  • Monitoring training progress via periodic evaluation (every N steps)
  • Comparing different DPO configurations (loss types, beta values)
  • Assessing alignment quality using reward margins and accuracies
  • Saving final trained model for deployment or further fine-tuning
  • Publishing aligned models to the Hugging Face Hub

Theoretical Basis

The evaluation metrics directly reflect the DPO optimization objective. The implicit reward for a response y given prompt x is:

r_theta(x, y) = beta * log(pi_theta(y|x) / pi_ref(y|x))

A well-trained DPO model should satisfy:

r_theta(x, y_w) > r_theta(x, y_l)   for most (x, y_w, y_l) in the eval set

The reward margin is the key diagnostic:

margin = r_theta(x, y_w) - r_theta(x, y_l)
       = beta * [ (log pi_theta(y_w|x) - log pi_ref(y_w|x)) - (log pi_theta(y_l|x) - log pi_ref(y_l|x)) ]

This margin should be positive and increasing during training. If the margin plateaus or decreases, it may indicate:

  • The beta value is too high (over-constraining the policy)
  • The preference data is noisy or inconsistent
  • The model has overfit to the training preferences

The reward accuracy metric provides a simple classification-style evaluation:

accuracy = mean( I[r_theta(x, y_w) > r_theta(x, y_l)] )

where I is the indicator function. Random performance gives 50% accuracy, so any value significantly above 50% indicates the model has learned preference-aligned behavior.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment