Align BCO with DPO: Add evaluate() override and precompute_ref_batch_size
Follow-up to #6116 (core), #6148 (KTO), #6195 (TPO) and #6211 (CPO/ORPO). My #6211 explicitly deferred BCO ("BCOTrainer has the same gap, but its tokenization path is different … left for a separate PR") — this is that follow-up. Two coupled, eval-side alignment gaps:
1. evaluate() override. BCOTrainer preprocesses eval_dataset in __init__ but doesn't override evaluate(), so passing a raw (unprocessed) dataset directly to evaluate(eval_dataset=...) fails with KeyError: 'completion_input_ids' (the raw rows are never tokenized). This adds an evaluate() override that preprocesses a directly-passed dataset the same way __init__ does (extract prompt → unpair → apply chat template → _tokenize → _process_tokens); a str selector and the no-arg path are left untouched. Reference log-probs (under precompute_ref_log_probs) are computed lazily by get_eval_dataloader, so no extra precompute is needed in the override.
2. precompute_ref_batch_size. DPO and KTO expose this (precomputing reference log-probs can use a larger batch than training to speed up preprocessing); BCOTrainer hardcoded per_device_{train,eval}_batch_size. Added the config field and wired it into both precompute dataloaders as precompute_ref_batch_size or per_device_*_batch_size.
Verification
- Added
test_evaluate_with_raw_dataset(parametrized overprecompute_ref_log_probs): trains, then callsevaluate(eval_dataset=<raw>). Fails onmainwithKeyError: 'completion_input_ids'and passes with this change. - Full
tests/experimental/test_bco_trainer.pypasses locally;ruff check/ruff format --checkclean.
[!NOTE] Low Risk Eval-path and preprocessing alignment with existing preference trainers; no auth or training-loss logic changes beyond batch size for ref precompute.
Overview Aligns BCOTrainer with DPO/KTO/TPO on evaluation and reference log-prob precomputation.
evaluate()now preprocesses a directly passed rawDataset(extract prompt → unpair → chat template →_tokenize→_process_tokens), fixingKeyError: 'completion_input_ids'when callingevaluate(eval_dataset=...). String selectors and the default init eval path are unchanged.get_eval_dataloaderonly caches precomputed reference log-probs on the trainer’s initeval_dataset; ad-hoc eval sets get fresh precompute without mutatingself.eval_dataset.
BCOConfig.precompute_ref_batch_sizeis added and used in train/eval reference precompute dataloaders asprecompute_ref_batch_size or per_device_*_batch_size.Tests cover raw-dataset
evaluate(with/withoutprecompute_ref_log_probs) and that foreign eval datasets do not overwrite the init eval set.Reviewed by Cursor Bugbot for commit 7a2594ac7d5d82f65380b15ec97e7e50c99e7743. Bugbot is set up for automated code reviews on this repo. Configure here.
Good catch. Fixed: get_eval_dataloader now distinguishes the trainer's own eval dataset from one passed directly to evaluate(). A directly-passed (foreign) dataset has its reference log-probs precomputed fresh and is not cached back onto self.eval_dataset (nor does it flip _precomputed_eval_ref_log_probs), so a later no-arg evaluate() still runs on the original init eval set.
This also fixes the converse case the old logic had: once _precomputed_eval_ref_log_probs was set (e.g. after evaluating the init dataset), a subsequent evaluate(eval_dataset=<foreign>) would have skipped precompute entirely and failed because the foreign dataset lacked reference_logps.
Added test_evaluate_with_raw_dataset_keeps_init_eval_dataset to cover it (with precompute_ref_log_probs=True, asserts self.eval_dataset is unchanged after evaluate(eval_dataset=<other>)).