Skip to content

Gemma-4 26B/31B: train with the empty thought channel on non-thinking turns - #12867

Merged
danielhanchen merged 9 commits into
mainfrom
fix/gemma4-empty-thought-train
Oct 7, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
fix/gemma4-empty-thought-train

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

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:

<|turn>model
<|channel>thought
<channel|>Answer...<turn|>

Their own chat_template.jinja emits <|channel>thought\n<channel|> only in the generation prompt (when enable_thinking is false). It never adds it to assistant turns in the history, and neither did Unsloth's gemma-4 / gemma-4-thinking templates. 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 from gemma4_thinking_template. Every model turn gets one thought channel right after <|turn>model\n:

    • a turn after the last user message that carries reasoning / reasoning_content renders it as <|channel>thought\n{reasoning}\n<channel|>, the same string and the same last-user gate as the model's own template;
    • any other turn, when thinking is off, gets the empty <|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\n when 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 Go text/template, it is byte-identical to the 26B HF template in the training format for single, multi-turn and system-prompt conversations.

  • get_chat_template switches gemma-4, gemma4, gemma-4-thinking and gemma4-thinking to 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 own gemma-4-thinking template is excluded from the check, since it primes every size. A tokenizer that already carries the new template keeps it if get_chat_template is 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:

  • With 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.
  • With auto-detection (the default in the 12B notebook), the detected response part is the full generation prompt <|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-turn reasoning_content / reasoning rendered 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):

main this PR
26B training text <|turn>model\nHello! <|turn>model\n<|channel>thought\n<channel|>Hello!
26B gemma-4 generation prompt == model's own no yes
26B gemma-4-thinking generation prompt == model's own yes yes
26B, final turn with reasoning_content: "2+2=4" <|turn>model\n4 <|turn>model\n<|channel>thought\n2+2=4\n<channel|>4
26B Ollama generation prompt has the empty channel no yes
E2B, every rendered string, both names byte-identical to main
26B labels, response_part="<|turn>model\n" Hello!<turn|> <|channel>thought\n<channel|>Hello!<turn|>
26B labels, auto-detected 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.

model arm step-0 all step-0 resp train loss step 1 / 2 / 3 / 30 held-out resp after 30 steps
26B-A4B-it main 6.560 5.803 7.30 / 5.39 / 4.63 / 0.90 0.822
26B-A4B-it this PR 2.547 1.246 1.78 / 1.05 / 0.94 / 0.87 0.753
E2B-it main 2.707 2.318 3.44 / 1.74 / 1.72 / 0.99 0.922
E2B-it this PR 2.707 2.318 3.44 / 1.74 / 1.71 / 0.99 0.922

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:

  • 26B, this PR: the model's own generation prompt and Unsloth's give identical output. All six answers are clean and end with <turn|>.
  • 26B, main, with Unsloth's gemma-4 generation prompt (no empty channel): one answer starts with a leaked text\n<|channel>thought\n<channel|> before the reply.

Not covered

  • 31B and 12B were not run (no local weights); detection relies on their templates carrying the same empty-channel primer.
  • transformers 4.57.6 was not run. The new Jinja only uses constructs Google's own Gemma 4 template already uses.
  • No GGUF was exported and run in Ollama; the Ollama template was rendered with Go text/template only.
  • The base gemma4_ollama (E2B / E4B) writes <|turn>assistant for history turns and adds a blank line between turns. That predates this PR and is not changed here.
  • A 26B tokenizer saved with the old gemma-4-thinking template 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).

… 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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-07T10:05:54.308842Z 3ea25d7 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 57a9996999

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 96bcbe7b84

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 9243c96390

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 7, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 3ea25d7bc1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant