Skip to content

[Bug] Gemma 4 base models are missing <bos> token in tokenizer config #7903

Closed

Description

@mx781
  1. Did you update? pip install --upgrade unsloth unsloth_zoo - yes
  2. Colab or Kaggle or local / cloud - cloud
  3. Number GPUs used, use nvidia-smi - 1x RTX 4060 Ti
  4. Which notebook? Please link! - none, minimal repro below
  5. 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
  6. Which trainer? SFTTrainer, GRPOTrainer etc - 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:

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

  1. BardiaKoopah commented on Aug 6, 2026

    @BardiaKoopah
    Contributor

    Confirming the config half of this, and being clear about what I did not check.

    add_bos_token: true is present in google/gemma-4-E2B and absent from all four unsloth base mirrors:

    repo add_bos_token
    google/gemma-4-E2B true
    unsloth/gemma-4-E2B missing
    unsloth/gemma-4-E4B missing
    unsloth/gemma-4-31B missing
    unsloth/gemma-4-26B-A4B missing

    So it's all four base models @mx781 listed, not just E2B.

    The -it variants 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.jinja emits {{- 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 -it chat template, fetched directly from the Hub. What I did not verify: the degenerate-repetition behavior. unsloth/gemma-4-E2B is 10.25 GB of weights and even unsloth/gemma-4-E2B-unsloth-bnb-4bit is 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": true to the four base mirrors' tokenizer_config.json, it doesn't need the behavioral repro to justify it — the delta against google/ is the argument on its own.

  2. added a commit that references this issue on Aug 6, 2026
    de4da6d
  3. added a commit that references this issue on Aug 7, 2026
    e663ada
  4. BardiaKoopah commented on Aug 7, 2026

    @BardiaKoopah
    Contributor

    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):

    model unsloth/ google/
    gemma-4-E2B absent true
    gemma-4-E4B absent absent
    gemma-4-31B absent absent
    gemma-4-26B-A4B absent absent

    Only E2B is a mirror-vs-upstream delta. For E4B, 31B and 26B-A4B, google omits add_bos_token too, 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 -it variants 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-E2B is 10.25 GB and I am on a 16 GB machine.

    Apologies for the noise.

  5. added 2 commits that reference this issue on Aug 13, 2026
    631bc3b
    597e0c6
  6. added a commit that references this issue on Sep 8, 2026
    30529a6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions