sampler: read suppress_tokens

generation_config.json's suppress_tokens (Gemma 4: the end-of-image and
end-of-audio markers, ids 258883 and 258882) are never sampled. The ids
reach the sampler as SamplingParams::suppress_tokens and leave the row
the way a grammar's mask does. A container carries them already, as the
space-separated generation.suppress_tokens.
This commit is contained in:
keptn42 committed 2026-11-04 07:21:07 +01:00
1 parent c1c05e9029
commit a72686b7e7
6 files changed
+83 -2

No files matched your search

+31 -1
View File
@@ -39,6 +39,7 @@ SamplingParams GenSampling::onto(SamplingParams base) const {
if (ngram_ban_n >= 0) base.ngram_ban_n = ngram_ban_n;
if (ngram_window >= 0) base.ngram_window = ngram_window;
if (!ngram_whitelist.empty()) base.ngram_whitelist = ngram_whitelist;
if (!suppress.empty()) base.suppress_tokens = suppress;
return base;
}
@@ -156,6 +157,23 @@ bool gen_sampling_parse(const std::string& json_text, const char* src, GenSampli
out->ngram_whitelist.push_back((int)id);
}
}
/* transformers' suppress_tokens, a JSON array of ids. Dropped entry by entry like the
* whitelist above. */
it = j.find("suppress_tokens");
if (it != j.end() && !it->is_array()) {
RAD_WARN("%s states suppress_tokens that is not an array; ignoring it", src);
} else if (it != j.end()) {
for (const auto& v : *it) {
const int64_t id = v.is_number_integer() ? v.get<int64_t>() : -1;
if (id < 0 || id > INT32_MAX) {
RAD_WARN("%s states a suppress_tokens entry outside [0, %d]; ignoring it",
src, INT32_MAX);
continue;
}
out->suppress.push_back((int)id);
}
}
return out->any();
}
@@ -175,7 +193,7 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
nlohmann::json j = nlohmann::json::object();
static const char* const kFields[] = { "do_sample", "temperature", "top_p", "min_p", "top_k",
"no_repeat_ngram_size", "no_repeat_ngram_window_size",
"no_repeat_ngram_whitelist" };
"no_repeat_ngram_whitelist", "suppress_tokens" };
for (const char* k : kFields) {
const char* v = get(k);
if (!v || !*v) continue;
@@ -195,6 +213,18 @@ bool gen_sampling_from_kv(const std::function<const char*(const char*)>& get, Ge
}
continue;
}
if (std::strcmp(k, "suppress_tokens") == 0) {
/* An array of scalars is flattened to a space-separated string (flatten_json). */
nlohmann::json ids = nlohmann::json::array();
char* p = const_cast<char*>(v);
for (char* e = nullptr;; p = e) {
const long long id = std::strtoll(p, &e, 10);
if (e == p) break;
ids.push_back(id);
}
j[k] = std::move(ids);
continue;
}
char* end = nullptr;
const double d = std::strtod(v, &end);
if (end == v) {
+4 -1
View File
@@ -33,10 +33,13 @@ struct GenSampling {
* has no field for it; a container states it as `generation.no_repeat_ngram_whitelist`, a
* JSON array of ids. Empty means not stated. */
std::vector<int32_t> ngram_whitelist;
/* transformers' suppress_tokens: ids never sampled. Empty means not stated. */
std::vector<int32_t> suppress;
bool any() const {
return temp >= 0.0f || top_p >= 0.0f || min_p >= 0.0f || top_k >= 0 ||
ngram_ban_n >= 0 || ngram_window >= 0 || !ngram_whitelist.empty();
ngram_ban_n >= 0 || ngram_window >= 0 || !ngram_whitelist.empty() ||
!suppress.empty();
}
/* Overlay onto `base` field by field; unstated fields keep base's, so sources stack. */
+4
View File
@@ -682,6 +682,10 @@ struct SamplingParams {
* duplicates ignored. Empty (the default) is off: the ban is then bit-identical to the
* no-whitelist form. */
std::vector<int32_t> ngram_whitelist;
/* Token ids this request never samples (generation_config.json's suppress_tokens: Gemma 4's
* end-of-image and end-of-audio markers). Global ids, duplicates ignored; empty is off. The
* sampler takes them out of the row as it does a grammar's mask. */
std::vector<int32_t> suppress_tokens;
};
/* Raw logits at chosen vocabulary ids at the prompt's last position, for a caller that answers
+14
View File
@@ -980,6 +980,7 @@ int Sampler::build_step(const std::vector<Request*>& reqs, int64_t step) {
if (budget && think.inside() && think.body >= sp.thinking_budget)
force_close(p, row, sp.reasoning.close);
if (!sp.suppress_tokens.empty()) suppress(p, row, sp.suppress_tokens);
if (want_lp) {
/* Temperature the candidate value carries: early and late, each counted only
@@ -1038,6 +1039,19 @@ void Sampler::force_close(DeviceSampleParams& p, int64_t row, int32_t close) {
p.flags |= RAD_SP_MASK;
}
void Sampler::suppress(DeviceSampleParams& p, int64_t row, const std::vector<int32_t>& ids) {
uint32_t* dst = mask_stage_ + row * mask_words_;
if (!(p.flags & RAD_SP_MASK)) {
std::memset(dst, 0xff, (size_t)mask_words_ * 4);
p.mask_row = (int32_t)row;
p.flags |= RAD_SP_MASK;
}
for (int32_t id : ids) {
const int64_t at = (int64_t)id - cfg_.vocab_off;
if (at >= 0 && at < cfg_.n_vocab_local) dst[at >> 5] &= ~(1u << (uint32_t)(at & 31));
}
}
int Sampler::build_greedy(int64_t n) {
if (!declared_) return RAD_E_STATE;
if (n < 0) return RAD_E_INVAL;
+3
View File
@@ -267,6 +267,9 @@ private:
* inside the block. A grammar mask on the row wins: the close is forced only where the
* grammar admits it, otherwise at a later position. */
void force_close(DeviceSampleParams& p, int64_t row, int32_t close);
/* Take `ids` out of the row: from the grammar's mask where it has one, from a full row where
* not. */
void suppress(DeviceSampleParams& p, int64_t row, const std::vector<int32_t>& ids);
SamplerConfig cfg_{};
const VocabView* vocab_ = nullptr;
+27
View File
@@ -258,4 +258,31 @@ TEST(gen_config_reads_the_ngram_ban) {
CHECK_EQ(greedy.onto(SamplingParams{}).ngram_ban_n, 0);
}
TEST(gen_config_reads_suppress_tokens) {
/* Gemma 4's generation_config.json: the end-of-image and end-of-audio markers. */
GenSampling g;
CHECK(gen_sampling_parse(R"({"temperature": 1.0, "suppress_tokens": [258883, 258882]})",
"test", &g));
CHECK_EQ((int)g.suppress.size(), 2);
CHECK_EQ(g.suppress[0], 258883);
CHECK_EQ(g.suppress[1], 258882);
const SamplingParams sp = g.onto(SamplingParams{});
CHECK_EQ((int)sp.suppress_tokens.size(), 2);
CHECK(SamplingParams{}.suppress_tokens.empty());
/* The container's flattened copy: the array as a space-separated string. */
GenSampling kv;
CHECK(gen_sampling_from_kv(kv_of({ { "suppress_tokens", "258883 258882" } }), &kv));
CHECK_EQ((int)kv.suppress.size(), 2);
CHECK_EQ(kv.suppress[1], 258882);
/* An entry that is no id is dropped on its own, and a non-array is dropped whole. */
GenSampling bad;
CHECK(gen_sampling_parse(R"({"top_k": 64, "suppress_tokens": [7, -1, "x", 9]})", "test", &bad));
CHECK_EQ((int)bad.suppress.size(), 2);
GenSampling flat;
CHECK(gen_sampling_parse(R"({"top_k": 64, "suppress_tokens": 7})", "test", &flat));
CHECK(flat.suppress.empty());
}
RAD_TEST_MAIN()