From 238159182380abf0455e68e68ac309650bfca723 Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Sun, 11 Oct 2026 09:17:46 -0500 Subject: [PATCH] tokenizer: end a Gemma 4 generation on and <|tool_response> Gemma 4 declares as its eos but ends a turn on , and a tool-calling turn on <|tool_response> (the opener of the result the caller supplies, written after the model's last ). Neither was in kEogNames, so every Gemma 4 reply ran to max_tokens. llama.cpp's loader lists both for this vocabulary. A vocabulary that has <|tool_response> as a control token also has as ordinary text (llama.cpp concludes the same); ending on it would cut a reply that writes the HTML tag, so is not an end-of-generation token there. This branch already ends a generation on every id generation_config.json names (c65fabe), which covers both tokens for a container that carries `generation.eos_token_id`. The spellings here cover one that does not, and the rule is new either way: the eos list never names for Gemma 4, but kEogNames did. Co-Authored-By: Claude Opus 5.5 --- core/text/tokenizer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/text/tokenizer.cpp b/core/text/tokenizer.cpp index 300b4ae1..91bf6137 100644 --- a/core/text/tokenizer.cpp +++ b/core/text/tokenizer.cpp @@ -603,16 +603,27 @@ int Vocab::load(VocabBuild b) { if (b_.eos >= 0) eog_.push_back(b_.eos); if (b_.eot >= 0 && b_.eot != b_.eos) eog_.push_back(b_.eot); { + /* Gemma 4 ends a turn on `` and a tool-calling turn on `<|tool_response>` -- the + * opener of the result the CALLER supplies, which the model writes after its last + * `` -- and declares neither: its eos is ``. llama.cpp's loader lists + * both for it, and both are control tokens nothing else spells. */ static const char* const kEogNames[] = { "<|endoftext|>", "<|im_end|>", "<|eot_id|>", "<|end_of_text|>", "", "<|end|>", "<|EOT|>", "<|eom_id|>", "<|return|>", "", "", + "", "<|tool_response>", }; + /* A vocabulary with `<|tool_response>` (Gemma 4) has `` as ordinary text, as + * llama.cpp also concludes; ending on it would end a reply that writes the HTML tag. */ + bool gemma4 = false; + for (int32_t id : special_) + if (b_.types[id] == RAD_TT_CONTROL && b_.tokens[id] == "<|tool_response>") gemma4 = true; std::string added; for (int32_t id : special_) { if (b_.types[id] != RAD_TT_CONTROL) continue; if (std::find(eog_.begin(), eog_.end(), id) != eog_.end()) continue; bool known = false; for (const char* n : kEogNames) if (b_.tokens[id] == n) { known = true; break; } + if (gemma4 && b_.tokens[id] == "") known = false; if (!known) continue; eog_.push_back(id); added += (added.empty() ? "" : " ") + b_.tokens[id] + "=" + std::to_string(id);