From 2c9ffc2888eb26b26e3e0c60bde2287a9b923bb7 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 10 Oct 2026 12:41:05 +0000 Subject: [PATCH] vocab: every id the checkpoint's generation_config names as eos ends a generation A checkpoint states the tokens a generation ends on in generation_config.json (eos_token_id, one id or a list), and rad-convert already carries it into the container as `generation.eos_token_id`. The vocabulary did not read it: it ended on eos, eot and a list of known spellings. Gemma 4 names three, [1, 106, 50] -- , and <|tool_response> -- and the container's eos is only the first. - closes every turn. Without it a reply runs past its own end: a three-sentence answer stopped at max_tokens 400 after ~390 tokens of "---" lines instead of finish_reason "stop" at 65 tokens. - <|tool_response> is what the model writes after its calls, where it waits for the result. Greedy on the 31B QAT checkpoint (TP 2): after a finished call the next token is 50 with and without this change; without it generation goes on and writes the same call again until the token limit, with it the reply ends there (stop_type "eos", one token). The engine reads `generation.eos_token_id` from the metadata before loading the vocabulary (VocabBuild::eos_ids, not baked) and Vocab::load ends a generation on every id in range, logged beside the spellings it adds. text_test: ids in the list end a generation, an id naming no token is no id. --- core/engine_bringup.cpp | 13 +++++++++++++ core/text/tokenizer.cpp | 16 ++++++++++++++++ core/text/tokenizer.h | 5 +++++ tests/text_test.cpp | 20 ++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index d1bd2222..49b836f6 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -3748,6 +3748,19 @@ int Engine::load_vocab() { vb.chat_template = std::move(t); } + /* The checkpoint's own end-of-generation list travels as metadata, flattened from + * generation_config.json -- a space-separated list, or one id. */ + if (const char* ids = rad_meta_gets(&meta_, "generation.eos_token_id", nullptr)) { + const char* q = ids; + while (*q) { + char* end = nullptr; + const long v = std::strtol(q, &end, 10); + if (end == q) break; + if (v >= 0 && v <= INT32_MAX) vb.eos_ids.push_back((int32_t)v); + q = end; + } + } + auto v = std::make_shared(); RAD_TRY(v->load(std::move(vb))); vocab_ = v; diff --git a/core/text/tokenizer.cpp b/core/text/tokenizer.cpp index 562f22cb..300b4ae1 100644 --- a/core/text/tokenizer.cpp +++ b/core/text/tokenizer.cpp @@ -621,6 +621,22 @@ int Vocab::load(VocabBuild b) { RAD_INFO("vocab: end-of-generation also on %s (a control token the container declares " "neither as eos nor eot; without this a turn ending on it runs on)", added.c_str()); + + /* AND WHAT THE CHECKPOINT ITSELF NAMES. generation_config.json's eos_token_id is the + * model's own list of tokens a generation ends on, and the one that is not a guess: + * Gemma 4 names <|tool_response> there beside and , because after its calls + * the model writes it and waits for the response -- a turn that runs past it answers + * its own call. Every id in range is honoured, as transformers honours them. */ + std::string named; + for (int32_t id : b_.eos_ids) { + if (id < 0 || id >= (int32_t)b_.tokens.size()) continue; + if (std::find(eog_.begin(), eog_.end(), id) != eog_.end()) continue; + eog_.push_back(id); + named += (named.empty() ? "" : " ") + b_.tokens[id] + "=" + std::to_string(id); + } + if (!named.empty()) + RAD_INFO("vocab: end-of-generation also on %s (named by the checkpoint's " + "generation_config.json)", named.c_str()); } /* The encoder: the chain compiled and the tables flattened. A pattern the regex compiler diff --git a/core/text/tokenizer.h b/core/text/tokenizer.h index f73eff40..9b33a247 100644 --- a/core/text/tokenizer.h +++ b/core/text/tokenizer.h @@ -85,6 +85,11 @@ struct VocabBuild { /* Diagnostics only: what rad-convert thought it was reading. Not baked. */ std::string source; + + /* The ids the checkpoint's generation_config.json names as eos_token_id, which the container + * carries as metadata (`generation.eos_token_id`) rather than in the vocabulary section. The + * engine fills it from there before load(); not baked. */ + std::vector eos_ids; }; /* ================================================================== the loaded vocab */ diff --git a/tests/text_test.cpp b/tests/text_test.cpp index 91559abd..b0949d93 100644 --- a/tests/text_test.cpp +++ b/tests/text_test.cpp @@ -955,6 +955,26 @@ TEST(vocab_treats_an_out_of_range_special_id_as_absent) { CHECK_EQ(v->pad(), -1); } +/* The checkpoint's own end-of-generation list -- generation_config.json's eos_token_id, which the + * container carries as `generation.eos_token_id` -- ends a generation on every id it names: Gemma 4 + * names <|tool_response> there, where the model stops after its calls. An id naming no token is + * no id. */ +TEST(vocab_ends_a_generation_on_every_id_the_checkpoint_names) { + VocabBuild vb; + vb.kind = RAD_TOK_BPE; + vb.tokens = { "a", "", "<|tool_response>", "<|tool_call>" }; + vb.types = { RAD_TT_NORMAL, RAD_TT_CONTROL, RAD_TT_CONTROL, RAD_TT_CONTROL }; + vb.eos = 1; + vb.eos_ids = { 1, 2, 99, -3 }; + auto v = std::make_shared(); + CHECK_OK(v->load(std::move(vb))); + CHECK(v->is_eog(1)); + CHECK(v->is_eog(2)); + CHECK(!v->is_eog(3)); + CHECK(!v->is_eog(0)); + CHECK_EQ(v->eog().size(), (size_t)2); +} + /* ================================================================== chain steps */ TEST(step_chain_units) {