tokenizer: end a Gemma 4 generation on <turn|> and <|tool_response>

Gemma 4 declares <eos> as its eos but ends a turn on <turn|>, and a
tool-calling turn on <|tool_response> (the opener of the result the caller
supplies, written after the model's last <tool_call|>). 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 </s> as
ordinary text (llama.cpp concludes the same); ending on it would cut a reply
that writes the HTML tag, so </s> 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 </s> rule is new either way: the eos list never names </s> for Gemma 4,
but kEogNames did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 committed 2026-10-12 14:40:04 -05:00
1 parent c37f47cc78
commit 2381591823
1 file changed
+11
+11
View File
@@ -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 `<turn|>` and a tool-calling turn on `<|tool_response>` -- the
* opener of the result the CALLER supplies, which the model writes after its last
* `<tool_call|>` -- and declares neither: its eos is `<eos>`. 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_of_turn>",
"<|end|>", "<|EOT|>", "<|eom_id|>", "<|return|>", "<eos>", "</s>",
"<turn|>", "<|tool_response>",
};
/* A vocabulary with `<|tool_response>` (Gemma 4) has `</s>` 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] == "</s>") known = false;
if (!known) continue;
eog_.push_back(id);
added += (added.empty() ? "" : " ") + b_.tokens[id] + "=" + std::to_string(id);