#7449·trl

SFT prompt-completion trains the wrong tokens with Gemma 4 12B/26B/31B

Author: uzunenesCreated Sep 29, 2026Updated Sep 30, 2026

With Gemma 4 12B-it (26B-A4B-it and 31B-it ship the same template), the generation prompt ends with an empty thought block:

<|turn>model\n<|channel>thought\n<channel|>

but the template drops it when the assistant turn is rendered as part of the full conversation. So in the conversational prompt-completion path the tokenized prompt isn't a prefix of prompt+completion. SFTTrainer logs the "Mismatch between tokenized prompt..." warning and keeps going with a mask built from len(prompt_ids). The result:

  • training never sees the thought block that every inference prompt has
  • the first 4 tokens of each completion get no loss
  • short completions (4 tokens or fewer) end up fully masked and get dropped. On trl-internal-testing/zen conversational_prompt_completion that's 8 of 17 rows.

I ran into this fine-tuning 12B-it with LoRA; the model fell into repetition after a few steps. E2B/E4B are fine. #5213 says the warning is safe to ignore, but here it isn't.

Repro on CPU (main @ 0c59cc2, transformers 5.17.0):

python
from datasets import Dataset
from transformers import AutoTokenizer
from trl import SFTConfig, SFTTrainer

tok = AutoTokenizer.from_pretrained("google/gemma-4-12B-it")
ds = Dataset.from_list([{
    "prompt": [{"role": "system", "content": "You translate."}, {"role": "user", "content": "Say hello."}],
    "completion": [{"role": "assistant", "content": "Hello there, my friend."}],
}])
tr = SFTTrainer(model="trl-internal-testing/tiny-Gemma4ForConditionalGeneration", processing_class=tok, train_dataset=ds,
                args=SFTConfig(output_dir="out", max_steps=1, bf16=False, use_cpu=True, report_to="none"))
row = tr.train_dataset[0]
print(repr(tok.decode(row["input_ids"])))
print(repr(tok.decode([t for t, l in zip(row["input_ids"], row["labels"]) if l != -100])))
'<bos><|turn>system\nYou translate.<turn|>\n<|turn>user\nSay hello.<turn|>\n<|turn>model\nHello there, my friend.<turn|>\n'
' friend.<turn|>\n'

I'd expect ...<|turn>model\n<|channel>thought\n<channel|>Hello there, my friend.<turn|>\n with loss on the whole completion.

I have a fix for SFT: when the prefix check fails, it uses the generation-prompt ids followed by whatever the full render adds after the common text prefix. There are tests for it, and the SFT and data_utils suites still pass. It will also change rows for other templates that already trigger this warning (Qwen3.5, Nemotron 3, Olmo3, R1-style). DPO and KTO seem to have the same logic. I can open a PR if this approach looks right to you, either SFT only or for all three.