Skip to content
Merged
Changes from 2 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
17 changes: 12 additions & 5 deletions trl/chat_template_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,26 @@ def is_chat_template_prefix_preserving(tokenizer: PreTrainedTokenizer) -> bool:
{"role": "tool", "name": "dummy", "content": "dummy"},
]

if isinstance(tokenizer, ProcessorMixin):
from PIL import Image

dummy_image = Image.new("RGB", (8, 8))
messages1 = prepare_multimodal_messages(messages1, images=[dummy_image])
messages2 = prepare_multimodal_messages(messages2, images=[dummy_image])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taken from #5558 ta avoid that tokenize=False -> True breaks this function for VLMs

Comment thread
qgallouedec marked this conversation as resolved.

try:
text1 = tokenizer.apply_chat_template(messages1, tokenize=False)
text2 = tokenizer.apply_chat_template(messages2, tokenize=False, add_generation_prompt=True)
ids1 = tokenizer.apply_chat_template(messages1, tokenize=True, return_dict=False)
ids2 = tokenizer.apply_chat_template(messages2, tokenize=True, return_dict=False, add_generation_prompt=True)
Comment thread
qgallouedec marked this conversation as resolved.
Outdated
except TypeError:
# Best-effort fallback for templates that reject dict args (e.g. DeepSeek-V3). This is a chat template
# bug (see transformers#45419), and the training chat template fixes it to avoid blocking users.
dummy_tool_calls = [{"type": "function", "function": {"name": "dummy", "arguments": "{}"}}]
messages1[1]["tool_calls"] = dummy_tool_calls
messages2[1]["tool_calls"] = dummy_tool_calls
text1 = tokenizer.apply_chat_template(messages1, tokenize=False)
text2 = tokenizer.apply_chat_template(messages2, tokenize=False, add_generation_prompt=True)
ids1 = tokenizer.apply_chat_template(messages1, tokenize=True, return_dict=False)
ids2 = tokenizer.apply_chat_template(messages2, tokenize=True, return_dict=False, add_generation_prompt=True)
Comment thread
qgallouedec marked this conversation as resolved.
Outdated

return text2.startswith(text1)
return ids2[: len(ids1)] == ids1
Comment thread
cursor[bot] marked this conversation as resolved.


deepseekv3_training_chat_template = (_CHAT_TEMPLATES_DIR / "deepseekv3_training.jinja").read_text()
Expand Down
Loading