Skip to content

fix Gemma 4 multimodal chat-template markers in processor_gemma4#4158

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_931310443
Jun 12, 2026
Merged

fix Gemma 4 multimodal chat-template markers in processor_gemma4#4158
copybara-service[bot] merged 1 commit into
mainfrom
test_931310443

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

fix Gemma 4 multimodal chat-template markers in processor_gemma4

The Gemma 4 multimodal SFT path was emitting Gemma 3 chat-template markers
("<start_of_turn>", "<end_of_turn>") which are NOT special tokens in the
Gemma 4 tokenizer. They BPE-tokenize into 7-token noise sequences each, so a
training label like "A<end_of_turn>" became an 8-token sequence
([236776 'A', 236820 '<', 643 'end', 236779 '', 1340 'of', 236779 '',
887 'turn', 236813 '>']).

With sft_train_on_completion_only=true the model learned to reproduce this
noise sequence after every answer, producing severe response-format collapse
post-SFT (e.g. "A<B<C<D<...").

The Gemma 4 chat template uses different special tokens:
(id 2)
<|turn> (id 105)
<turn|> (id 106)
This CL switches the prompt and response formatters to use them.

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/maxtext/multimodal/processor.py 0.00% 1 Missing ⚠️
src/maxtext/multimodal/processor_gemma4.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

The Gemma 4 multimodal SFT path was emitting Gemma 3 chat-template markers
("<start_of_turn>", "<end_of_turn>") which are NOT special tokens in the
Gemma 4 tokenizer. They BPE-tokenize into 7-token noise sequences each, so a
training label like "A<end_of_turn>" became an 8-token sequence
([236776 'A', 236820 '<', 643 'end', 236779 '_', 1340 'of', 236779 '_',
887 'turn', 236813 '>']).

With sft_train_on_completion_only=true the model learned to reproduce this
noise sequence after every answer, producing severe response-format collapse
post-SFT (e.g. "A<B<C<D<...").

The Gemma 4 chat template uses different special tokens:
  <bos>    (id 2)
  <|turn>  (id 105)
  <turn|>  (id 106)
This CL switches the prompt and response formatters to use them.

PiperOrigin-RevId: 931396545
@copybara-service copybara-service Bot merged commit 5f3dc2b into main Jun 12, 2026
@copybara-service copybara-service Bot deleted the test_931310443 branch June 12, 2026 23:45
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.

1 participant