Repository navigation
Gemma-4 26B/31B: train with the empty thought channel on non-thinking turns - #12867
Conversation
… turns gemma-4-26B-A4B-it and gemma-4-31B-it open every non-thinking model turn with <|channel>thought\n<channel|>, but their chat template only emits it in the generation prompt, and so did Unsloth's gemma-4 / gemma-4-thinking templates. SFT text then lacked it and 26B started at an assistant loss of 5.5 instead of 1.3. get_chat_template now detects models whose own template primes the empty channel and renders it on model turns without thinking content, with the generation prompt matching the model's own. E2B / E4B render as before.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…nnel, and prime the Ollama template
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
gemma-4-26B-A4B-it and gemma-4-31B-it were trained so that a non-thinking model turn opens with an empty thought channel:
Their own
chat_template.jinjaemits<|channel>thought\n<channel|>only in the generation prompt (whenenable_thinkingis false). It never adds it to assistant turns in the history, and neither did Unsloth'sgemma-4/gemma-4-thinkingtemplates. SFT text built with them is off-distribution for the whole answer on 26B/31B. Google's Gemma 4 prompt formatting guide recommends adding the empty channel to training prompts when fine-tuning these two models on data without thinking (https://ai.google.dev/gemma/docs/core/prompt-formatting-gemma4). The same asymmetry is reported on the google/gemma-4-31B-it discussion #77.E2B / E4B do not have the primer in their template, and adding the channel barely changes their loss, so they are left exactly as they are.
Change
New
gemma4_empty_thought_template, derived fromgemma4_thinking_template. Every model turn gets one thought channel right after<|turn>model\n:reasoning/reasoning_contentrenders it as<|channel>thought\n{reasoning}\n<channel|>, the same string and the same last-user gate as the model's own template;<|channel>thought\n<channel|>. Inline<|channel>...<channel|>in the content is still stripped, so those turns get the empty channel too.Its generation prompt is the model's own (empty channel when thinking is off, plain
<|turn>model\nwhen on).The Ollama modelfile switches with it to
gemma4_empty_thought_ollama, a standalone template that renders every assistant history turn as<|turn>model\n<|channel>thought\n<channel|>...and ends the generation prompt with the empty channel, so GGUF exports of 26B/31B prompt the way they were trained. Rendered with Gotext/template, it is byte-identical to the 26B HF template in the training format for single, multi-turn and system-prompt conversations.get_chat_templateswitchesgemma-4,gemma4,gemma-4-thinkingandgemma4-thinkingto it only when the tokenizer's or processor's own chat template contains the empty-channel string. The check reads template content, not the model name, so future sizes with the same template behave the same. Unsloth's owngemma-4-thinkingtemplate is excluded from the check, since it primes every size. A tokenizer that already carries the new template keeps it ifget_chat_templateis called again.No new argument. Models whose template does not prime the channel (E2B, E4B) get byte-identical output to before.
train_on_responses_only:response_part = "<|turn>model\n"(the 26B/31B notebooks), the empty channel is part of the trained response, the same tokens the model produces before its answer.<|turn>model\n<|channel>thought\n<channel|>, so the channel is masked and only the answer is trained.Tests
tests/test_gemma4_empty_thought.py(CPU, no unsloth import): detection on the 26B and E2B template tails, processor dict templates, the re-call cases, exact strings for multi-turn with a system prompt, list content, final-turnreasoning_content/reasoningrendered as the thought channel, reasoning before the last user turn dropped (as in the model's template), inline thoughts stripped,enable_thinking=True, generation prompt parity, the Ollama generation prompt, and train text == generation prompt + answer. 41 passed together with the existing Gemma 4 and processor chat template tests.Real tokenizers (google/gemma-4-26B-A4B-it and gemma-4-E2B-it, tokenizer and processor, both template names, transformers 5.17):
<|turn>model\nHello!<|turn>model\n<|channel>thought\n<channel|>Hello!gemma-4generation prompt == model's owngemma-4-thinkinggeneration prompt == model's ownreasoning_content: "2+2=4"<|turn>model\n4<|turn>model\n<|channel>thought\n2+2=4\n<channel|>4response_part="<|turn>model\n"Hello!<turn|><|channel>thought\n<channel|>Hello!<turn|>Hello!<turn|>Hello!<turn|>GPU results
Setup: FineTome-100k. Step-0 loss is measured on 32 held-out conversations. Then 30 steps of SFT with the 12B notebook settings (bf16 LoRA r=8, lr 2e-4, batch 1 x GA 4, adamw_8bit,
train_on_responses_only).chat_template = "gemma-4",<bos>kept, unsloth-zoo main, transformers 5.17. "resp" means the response tokens only.For E2B, the training text is byte-identical between the arms and step 0 matches exactly. The later 3rd-decimal differences are run-to-run GPU nondeterminism.
Greedy generations after training, 6 held-out prompts:
<turn|>.gemma-4generation prompt (no empty channel): one answer starts with a leakedtext\n<|channel>thought\n<channel|>before the reply.Not covered
text/templateonly.gemma4_ollama(E2B / E4B) writes<|turn>assistantfor history turns and adds a blank line between turns. That predates this PR and is not changed here.gemma-4-thinkingtemplate from main and re-templated here is not detected and keeps the old behaviour.Related: unslothai/notebooks#251 (the Gemma 4 notebooks also stripped
<bos>; fixed separately in the notebooks repo).