Remove post-collation truncation from DPO#5350
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This reverts commit 5dade1b.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| Function to use to form a batch from a list of elements of the processed `train_dataset` or `eval_dataset`. | ||
| Will default to [`~trainer.dpo_trainer.DataCollatorForPreference`] if the model is a language model and | ||
| [`~trainer.dpo_trainer.DataCollatorForVisionPreference`] if the model is a vision-language model. | ||
| [`~trainer.dpo_trainer.DataCollatorForVisionPreference`] if the model is a vision-language model. Custom |
There was a problem hiding this comment.
can you please add the same comment in SFTTrainer.data_collator
There was a problem hiding this comment.
I am planning to add the same comment to SFT in my subsequent PR, when I remove post-collation truncation from SFT as well.
|
thanks! I think we might be able to remove |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2983422516
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Remove post-collation truncation from DPO.
This PR removes internal truncation logic from the DPO trainer and require custom data collators to handle truncation themselves. This simplifies the trainer code and clarifies the contract for custom collators. Additionally, the PR updates documentation and error handling to reflect these changes.
Follow-up to:
Motivation
Both built-in DPO collators (
DataCollatorForPreferenceandDataCollatorForVisionPreference) already truncate sequences internally before padding. The only reason_truncate_inputsstill existed in the trainer was as a silent safety net for custom collators, which is arguably worse than no safety net, because it hid the fact that the collator wasn't doing its job.This PR makes the contract explicit and removes the silent fix-up.
Changes
Data Collation and Truncation Handling:
_truncate_inputsmethod and all related calls, shifting responsibility for truncation entirely to the data collator. Now, if a custom data collator is provided, it must handle truncation before padding.data_collatorargument to clearly state that custom collators must truncate sequences before padding, as the trainer will not apply truncation after collation.Code Simplification:
flush_rightimport and related logic, further simplifying the codebase.Model Input Handling:
compute_ref_log_probsand_compute_loss, now directly including all relevant keys from the input dictionary without truncation logic.Note
Medium Risk
Removes a silent safety net that truncated/padded batches inside
DPOTrainer, so customdata_collators that relied on that behavior may now produce overlong or misaligned tensors and fail at runtime.Overview
Removes post-collation truncation from
DPOTrainer. The internal_truncate_inputspath (includingkeep_endflush/realign logic) is deleted, and bothcompute_ref_log_probsand loss computation now consume collator outputs as-is.Updates the trainer/collator contract. The
data_collatordocstring now explicitly requires custom collators to truncate sequences before padding, and model kwargs assembly is simplified to pass through optional fields (e.g.,token_type_ids, multimodal image inputs) directly without length fix-ups.Written by Cursor Bugbot for commit 2983422. This will update automatically on new commits. Configure here.