Repository navigation
Closed
Labels
Description
- Did you update?
pip install --upgrade unsloth unsloth_zoo- yes ColaborKaggleor local / cloud - cloud- Number GPUs used, use
nvidia-smi- 1x RTX 4060 Ti - Which notebook? Please link! - none, minimal repro below
- Which Unsloth version, TRL version, transformers version, PyTorch version? - unsloth 2026.8.3, unsloth_zoo 2026.8.3, transformers 5.5.0, trl 0.24.0, torch 2.11.0+cu130
- Which trainer?
SFTTrainer,GRPOTraineretc - none, inference only
It looks like base (non-it) gemma4 tokenizer configs are missing add_bos_token: true, which is present in the google/ equivalent:
- unsloth: https://huggingface.co/unsloth/gemma-4-E2B/raw/main/tokenizer_config.json
- google: https://huggingface.co/google/gemma-4-E2B/raw/main/tokenizer_config.json
As a result, doing inference (with or without fine-tuning) like in the example notebooks results in degenerate repeated text when generating. This applies to the other base models (E4B, 31B, 26B-A4B). -it variants are fine, since their chat_template.jinja adds the <bos>.
Is there a different way the token is supposed to land there? Didn't see anything in the gemma 4 docs.
Repro below, only diff is whether <bos> gets prepended before generate:
from unsloth import FastModel
import torch, os
model, tokenizer = FastModel.from_pretrained(
model_name="unsloth/gemma-4-E2B",
dtype=None,
max_seq_length=1024,
load_in_4bit=True,
full_finetuning=False,
token=os.environ.get("HF_TOKEN"),
)
prompt = "This book is largely concerned with Hobbits, and from its pages,"
ids = tokenizer(text=prompt, return_tensors="pt")["input_ids"]
torch.manual_seed(3407)
out = model.generate(input_ids=ids.to("cuda"), attention_mask=torch.ones_like(ids).to("cuda"),
max_new_tokens=64, temperature=1.0, top_p=0.95, top_k=64, use_cache=True)
print(tokenizer.decode(out[0][ids.shape[1]:], skip_special_tokens=True))
# ' its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its pages, its'
bos = torch.tensor([[tokenizer.bos_token_id]])
ids = torch.cat([bos, ids], dim=1) # <-- only change: hardcode <bos> upfront
torch.manual_seed(3407)
out = model.generate(input_ids=ids.to("cuda"), attention_mask=torch.ones_like(ids).to("cuda"),
max_new_tokens=64, temperature=1.0, top_p=0.95, top_k=64, use_cache=True)
print(tokenizer.decode(out[0][ids.shape[1]:], skip_special_tokens=True))
# ' you will learn how to make your own Hobbits. The author will teach you how to make a Hobbit-sized hat, a Hobbit-sized shirt, a Hobbit-sized belt, and a Hobbit-sized pair of pants. You will also learn how to make a Hobbit-sized pair of shoes, a Hobbit-'
Activity
BardiaKoopah commentedon Aug 6, 2026
Confirming the config half of this, and being clear about what I did not check.
add_bos_token: trueis present ingoogle/gemma-4-E2Band absent from all four unsloth base mirrors:add_bos_tokengoogle/gemma-4-E2Btrueunsloth/gemma-4-E2Bunsloth/gemma-4-E4Bunsloth/gemma-4-31Bunsloth/gemma-4-26B-A4BSo it's all four base models @mx781 listed, not just E2B.
The
-itvariants also lack the key, but that's harmless there for the reason given in the report, which I checked:unsloth/gemma-4-E2B-it/chat_template.jinjaemits{{- bos_token -}}at line 182, so the token lands regardless. That's also why this only shows up on the base models — they're exactly the set with no chat template to compensate.What I verified: the tokenizer_config delta above and the
-itchat template, fetched directly from the Hub. What I did not verify: the degenerate-repetition behavior.unsloth/gemma-4-E2Bis 10.25 GB of weights and evenunsloth/gemma-4-E2B-unsloth-bnb-4bitis 9.03 GB; I'm on a 16 GB Apple Silicon machine and can't load either. So I'm confirming the missing key, not the generation symptom — though the mechanism described (no<bos>, degenerate output on a Gemma base model) is well established and the side-by-side repro in the issue looks sound.If the fix is just adding
"add_bos_token": trueto the four base mirrors'tokenizer_config.json, it doesn't need the behavioral repro to justify it — the delta againstgoogle/is the argument on its own.fix(tokenizer): enable add_bos_token for Gemma 4 base models
fix(tokenizer): enable add_bos_token for Gemma 4 base models
BardiaKoopah commentedon Aug 7, 2026
Correction to my comment above — I overstated the scope and want to fix that before anyone acts on it.
I checked google's side for E2B only, then generalised to the other three base models. Verified properly now (all HTTP 200, fetched today):
unsloth/google/trueOnly E2B is a mirror-vs-upstream delta. For E4B, 31B and 26B-A4B, google omits
add_bos_tokentoo, so those mirrors match their source and adding the key would make them diverge from upstream rather than restore parity.That is a meaningful difference: my earlier comment implied a four-repo mirroring regression, and it is a one-repo mirroring regression.
If E4B/31B/26B-A4B also generate degenerately without
<bos>— which the original report suggests and I have not tested — then that is a question for upstream, or a deliberate divergence worth deciding on its own merits, not a parity fix.What still stands: the E2B delta, and that the
-itvariants are unaffected because their chat template emits{{- bos_token -}}(chat_template.jinja:182). What I still have not verified is the generation behaviour on any of them —unsloth/gemma-4-E2Bis 10.25 GB and I am on a 16 GB machine.Apologies for the noise.
fix(tokenizer): enable add_bos_token for Gemma 4 base models
fix(tokenizer): enable add_bos_token for Gemma 4 base models
fix(tokenizer): dedupe BOS when Gemma 4 chat template also emits it
Slim the Gemma 4 BOS detection and key it on emitted ids
fix(tokenizer): enable add_bos_token for Gemma 4 base models (#10312)