Skip to content
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
aa61a92
Accept processor in `get_training_chat_template`
qgallouedec Apr 15, 2026
10e6396
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 16, 2026
6e847a8
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 17, 2026
00e92fe
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 20, 2026
550f75e
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 20, 2026
cf3ede4
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 22, 2026
91c9c36
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 22, 2026
48e4bd7
backward compat
qgallouedec Apr 22, 2026
30c3986
fix typo in doc
qgallouedec Apr 22, 2026
6700975
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 22, 2026
74e71c1
Apply suggestions from code review
qgallouedec Apr 22, 2026
baff509
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 22, 2026
de3eb84
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 23, 2026
48ff506
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 27, 2026
815e714
style
qgallouedec Apr 27, 2026
54f486a
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 28, 2026
20078b4
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 28, 2026
b951d55
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 29, 2026
7a398c0
fix qwen36
qgallouedec Apr 29, 2026
e0fb2e1
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 29, 2026
c001dbc
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec Apr 30, 2026
67a0f10
Merge branch 'main' into processor-in-get_training_chat_template
qgallouedec May 3, 2026
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
27 changes: 13 additions & 14 deletions trl/chat_template_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,17 @@ def is_chat_template_prefix_preserving(tokenizer: PreTrainedTokenizer) -> bool:
gptoss_training_chat_template = (_CHAT_TEMPLATES_DIR / "gptoss_training.jinja").read_text()


def get_training_chat_template(tokenizer: PreTrainedTokenizer) -> str | None:
def get_training_chat_template(processing_class: PreTrainedTokenizer | ProcessorMixin) -> str | None:
Comment thread
qgallouedec marked this conversation as resolved.
Outdated
r"""
Get a training-compatible chat template, if needed.

Returns a patched chat template that is prefix-preserving and includes `{%% generation %%}` / `{%% endgeneration
%%}` markers for assistant-only loss masking. Returns `None` if the tokenizer's template already satisfies both
requirements. Currently DeepSeek-V3, GPT-OSS, LLaMA 3, Qwen2.5, and Qwen3 are supported.
%%}` markers for assistant-only loss masking. Returns `None` if the template already satisfies both requirements.
Currently DeepSeek-V3, GPT-OSS, LLaMA 3, Qwen2.5, and Qwen3 are supported.

Args:
tokenizer (`PreTrainedTokenizer`):
Tokenizer instance to check.
processing_class (`PreTrainedTokenizer` or `ProcessorMixin`):
Tokenizer or processor instance to check.

Returns:
`str` or `None`:
Expand Down Expand Up @@ -497,28 +497,27 @@ def get_training_chat_template(tokenizer: PreTrainedTokenizer) -> str | None:
```
"""
# First check if patching is needed
if is_chat_template_prefix_preserving(tokenizer) and "{% generation %}" in tokenizer.chat_template:
if is_chat_template_prefix_preserving(processing_class) and "{% generation %}" in processing_class.chat_template:
return None # No patching needed

if tokenizer.chat_template == deepseekv3_chat_template:
if processing_class.chat_template == deepseekv3_chat_template:
return deepseekv3_training_chat_template

if tokenizer.chat_template == gptoss_chat_template:
if processing_class.chat_template == gptoss_chat_template:
return gptoss_training_chat_template

if tokenizer.chat_template == llama3_chat_template:
if processing_class.chat_template == llama3_chat_template:
return llama3_training_chat_template

if tokenizer.chat_template == qwen2_5_chat_template:
Comment thread
cursor[bot] marked this conversation as resolved.
if processing_class.chat_template == qwen2_5_chat_template:
return qwen2_5_training_chat_template

if tokenizer.chat_template == qwen3_chat_template:
if processing_class.chat_template == qwen3_chat_template:
return qwen3_training_chat_template

raise ValueError(
"The tokenizer's chat template is not training-compatible (missing prefix-preservation or "
"`{% generation %}` markers) and patching is not supported for this template. "
"Please manually modify the tokenizer's chat template for training."
"The chat template is not training-compatible (missing prefix-preservation or `{% generation %}` markers) "
"and patching is not supported for this template. Please manually modify the chat template for training."
)


Expand Down
Loading