Skip to content

feat: add OLMo 3 training chat template with generation markers - #6690

Closed
RudrenduPaul wants to merge 1 commit into
huggingface:mainfrom
RudrenduPaul:feat/olmo3-training-chat-template
Closed

RudrenduPaul wants to merge 1 commit into
huggingface:mainfrom
RudrenduPaul:feat/olmo3-training-chat-template

Conversation

@RudrenduPaul

@RudrenduPaul RudrenduPaul commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a training-compatible variant of the OLMo 3 chat template with {% generation %} / {% endgeneration %} markers, enabling return_assistant_tokens_mask=True for assistant-only loss masking in SFT training.

OLMo 3 isn't in the family list originally tracked by #5471 (now closed), so this fills a gap that was left out of that sweep — the model's native chat template has no {% generation %} markers and get_training_chat_template() currently raises for it.

Files changed:

  • trl/chat_templates/olmo3.jinja — original OLMo 3 chat template (source of truth for the exact-match comparison in get_training_chat_template())
  • trl/chat_templates/olmo3_training.jinja — training variant: same text output as the original, with {% generation %} / {% endgeneration %} wrapping the assistant output (content, function_calls, and the <|im_end|> / eos_token terminator)
  • trl/chat_template_utils.py — loads both templates; adds an OLMo 3 branch to get_training_chat_template(); updates the docstring
  • tests/test_chat_template_utils.py — adds trl-internal-testing/tiny-Olmo3ForCausalLM (the fixture from Add testing for Olmo3 #5962) to TestGetTrainingChatTemplate's parametrize list, gated on transformers>=4.57.0 to match its existing skip condition elsewhere in the file
  • trl/chat_templates/README.md, docs/source/chat_templates.md — document the new template pair

Template design: OLMo 3 uses a bespoke function-calling schema (functions / function_calls fields plus an environment role) instead of the standard tools / tool_calls / tool interface, so supports_tool_calling() is False for it and get_training_chat_template() does not require prefix-preservation. Only the {% generation %} markers were needed — no structural prefix-preservation fix, matching the same reasoning already applied to Llama 3 and the Nemotron 3 family.

Testing: pytest tests/test_chat_template_utils.py -k olmo3 → 12 passed, 2 skipped (skips are the tool-calling-specific assertions, correctly skipped since OLMo 3 doesn't support standard tool calling). Full file: pytest tests/test_chat_template_utils.py → 359 passed, 226 skipped, 0 failed.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link to it if that's the case. — Related to Tracking: Add {% generation %} chat templates for common model families #5471 (closed; OLMo 3 was not in its tracked family list)
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

AI writing disclosure

AI-assisted: Claude Code helped draft the training-template diff and test wiring, following the same pattern as my earlier merged PRs (#5493, #5522, #5526, #5527) for this repo. I reviewed and verified the change myself before submitting.

Who can review?

@qgallouedec


Note

Low Risk
Additive template wiring and tests only; no changes to training loops, auth, or shared inference paths beyond optional template swap at init.

Overview
Adds OLMo 3 to TRL’s supported chat-template families so SFT with assistant_only_loss=True no longer fails on allenai/Olmo-3-* tokenizers.

New olmo3.jinja (reference copy for exact-match detection) and olmo3_training.jinja, which wraps assistant content, function_calls, and the turn terminator in {% generation %} / {% endgeneration %} without changing rendered text. get_training_chat_template() loads both and returns the training variant when the tokenizer template matches the reference.

Docs and TestGetTrainingChatTemplate gain OLMo 3 (tiny-Olmo3ForCausalLM, transformers>=4.57.0). No prefix-preservation patch: Olmo 3’s custom functions / function_calls schema is outside standard tool calling, so GRPO-style prefix checks do not apply.

Reviewed by Cursor Bugbot for commit 3e39de2. Bugbot is set up for automated code reviews on this repo. Configure here.

Adds trl/chat_templates/olmo3.jinja and olmo3_training.jinja so
get_training_chat_template() supports OLMo 3, wrapping assistant output
in {% generation %} / {% endgeneration %} markers for assistant-only
loss masking in SFT. Reuses the existing tiny-Olmo3ForCausalLM test
fixture from huggingface#5962.
@qgallouedec

Copy link
Copy Markdown
Member

Thanks. OLMo 3 checkpoints ship 6 different chat templates, and this covers one of them (7B-Think, not the most used 7B-Instruct). It also puts in the loss the <think> that the generation prompt already writes (#8109). Albert is adding the OLMo 3 templates through #7962, so closing in favour of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants