trl icon indicating copy to clipboard operation
trl copied to clipboard

Align CPO/ORPO with DPO: Add evaluate() override

Open DaoyuanLi2816 opened this issue 5 months ago • 0 comments

Follow-up to #6116 (core trainers), #6148 (KTO) and #6195 (TPO): CPOTrainer and ORPOTrainer preprocess eval_dataset in __init__ but don't override evaluate(), so passing a raw (unprocessed) dataset directly to evaluate(eval_dataset=...) — e.g. a held-out test set — fails because the columns don't match the tokenized signature:

ValueError: No columns in the dataset match the model's forward method signature...

This adds an evaluate() override to both trainers that preprocesses a directly-passed dataset the same way __init__ does (extract prompt → apply chat template → tokenize), mirroring DPO/KTO/TPO. A str (which selects an eval dataset already prepared at init time) is left untouched, and dicts are handled per key.

BCOTrainer has the same gap, but its tokenization path is different (module-level _tokenize with reference/embedding precomputation), so I've left it for a separate PR.

Verification

  • Added test_evaluate_with_raw_dataset to both trainers — each fails on main with the ValueError above and passes with this change.
  • Full tests/experimental/test_cpo_trainer.py and tests/experimental/test_orpo_trainer.py pass locally; ruff check / ruff format --check clean.

[!NOTE] Low Risk Behavioral fix aligned with DPO/KTO/TPO; only affects eval when passing a non-string dataset to evaluate(), with no training or auth changes.

Overview CPOTrainer and ORPOTrainer now override evaluate() so a raw preference dataset passed at call time (e.g. a held-out test split) is preprocessed like __init__: extract prompt → chat template → tokenize_row, then super().evaluate() runs. str eval keys (datasets already tokenized at init) are unchanged; dict eval sets are prepared per key under PartialState().main_process_first().

Regression tests test_evaluate_with_raw_dataset were added for both trainers to assert eval_loss is returned when evaluating an unprocessed zen preference dataset.

Reviewed by Cursor Bugbot for commit c0d2a6f8a305741c899e89cda284e17b4eb472c7. Bugbot is set up for automated code reviews on this repo. Configure here.

DaoyuanLi2816 avatar Jun 30 '26 04:06 DaoyuanLi2816