Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions tests/test_dpo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,12 +1289,13 @@ def test_train_vlm_keep_end_raises(self):
# Regression test for #5285: keep_end with a VLM must raise at init time, not silently corrupt training.
# Image tokens live at the start of the sequence (in the prompt); keep_end would drop them.
dataset = load_dataset("trl-internal-testing/zen-image", "conversational_preference", split="train")
training_args = DPOConfig(
output_dir=self.tmp_dir,
max_length=32,
truncation_mode="keep_end",
report_to="none",
)
with pytest.warns(FutureWarning, match="keep_end.*deprecated"):
training_args = DPOConfig(
output_dir=self.tmp_dir,
max_length=32,
truncation_mode="keep_end",
report_to="none",
)
with pytest.raises(ValueError, match="truncation_mode='keep_end' is not supported for vision-language models"):
DPOTrainer(
model="trl-internal-testing/tiny-Qwen2_5_VLForConditionalGeneration",
Expand Down
17 changes: 13 additions & 4 deletions trl/trainer/dpo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import warnings
from dataclasses import dataclass, field
from typing import Any

Expand Down Expand Up @@ -52,8 +53,8 @@ class DPOConfig(_BaseConfig):
Maximum length of the tokenized sequence. Sequences longer than `max_length` are truncated from the left or
right depending on the `truncation_mode`. If `None`, no truncation is applied.
truncation_mode (`str`, *optional*, defaults to `"keep_start"`):
Truncation mode to use when the sequence exceeds `max_length`. Possible values are `"keep_end"` and
`"keep_start"`.
Truncation mode to use when the sequence exceeds `max_length`. The only supported value is
`"keep_start"`. The `"keep_end"` value is deprecated and will be removed in v2.0.0.
padding_free (`bool`, *optional*, defaults to `False`):
Whether to perform forward passes without padding by flattening all sequences in the batch into a single
continuous sequence. This reduces memory usage by eliminating padding overhead. Currently, this is only
Expand Down Expand Up @@ -170,8 +171,8 @@ class DPOConfig(_BaseConfig):
truncation_mode: str = field(
default="keep_start",
metadata={
"help": "Truncation mode to use when the sequence exceeds `max_length`. Possible values are `'keep_end'` "
"and `'keep_start'`.",
"help": "Truncation mode to use when the sequence exceeds `max_length`. The only supported value is "
"`'keep_start'`. The `'keep_end'` value is deprecated and will be removed in v2.0.0.",
"choices": ["keep_end", "keep_start"],
},
)
Expand Down Expand Up @@ -316,4 +317,12 @@ def __post_init__(self):
f"Got {len(self.loss_weights)} weights for {len(self.loss_type)} loss types."
)

if self.truncation_mode == "keep_end":
warnings.warn(
"The `'keep_end'` truncation mode is deprecated and will be removed in v2.0.0. "
"Use `truncation_mode='keep_start'` (the default) instead.",
FutureWarning,
stacklevel=3,
)

super().__post_init__()
Loading