// The qwen35 host sampler's penalties (include/strata/qwen35/sampling.hpp), CPU only, no model. // #3911: a history token outside a short logit row (--draft-vocab N) must not write past the row. #include "strata/qwen35/sampling.hpp" #include #include #include #include using strata::qwen35::Sampling; using strata::qwen35::apply_penalties; namespace { int checks = 0; void check(bool ok, const char* label) { ++checks; if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); } } // The loop as it was before the guard (origin/main of the time), for the in-range comparison. void old_penalties(std::vector& lg, const Sampling& s, const std::vector& hist) { if (s.last_n != 0 && (s.repeat != 1.f || s.freq != 0.f || s.present != 0.f)) { std::map cnt; const size_t from = s.last_n < 0 || (size_t) s.last_n >= hist.size() ? 0 : hist.size() - (size_t) s.last_n; for (size_t i = from; i < hist.size(); ++i) ++cnt[hist[i]]; for (const auto& [id, c] : cnt) { float& v = lg[(size_t) id]; if (s.repeat != 1.f) v = v > 0 ? v / s.repeat : v * s.repeat; v -= (float) c * s.freq + s.present; } } } bool same_bits(const std::vector& a, const std::vector& b) { return a.size() == b.size() && std::memcmp(a.data(), b.data(), a.size() * sizeof(float)) == 0; } } // namespace int main() { Sampling s; s.repeat = 1.2f; s.freq = 0.1f; s.present = 0.05f; s.last_n = -1; // a short row (a 64-token draft vocabulary) and a history with tokens inside it, past it and negative. The row's // storage past its size is filled with a marker first: a write past the row would change the marker. { std::vector row(256, 7.f); row.resize(64); for (int i = 0; i < 64; ++i) row[(size_t) i] = (float) i - 20.f; const std::vector before(row.begin(), row.end()); const std::vector hist = {3, 100, 3, 63, 64, 65, 255, 100000, -1, 7}; apply_penalties(row, s, hist); for (int i = 64; i < 256; ++i) check(row.data()[i] == 7.f, "nothing is written past the row"); check(row.size() == 64, "the row keeps its size"); // tokens 3 (twice), 63 and 7 are in the row and are penalised; the others are skipped std::vector want = before; for (int id : {3, 7, 63}) { const int c = id == 3 ? 2 : 1; float& v = want[(size_t) id]; v = v > 0 ? v / s.repeat : v * s.repeat; v -= (float) c * s.freq + s.present; } check(same_bits(row, want), "the in-row tokens take the penalty, the rest of the row is untouched"); } // history tokens that are all outside the row: the row does not change { std::vector row(16, 1.5f); const std::vector before = row; apply_penalties(row, s, {16, 17, 1000, -5}); check(same_bits(row, before), "only out-of-row tokens: no change"); } // a row of the whole vocabulary behaves exactly as before the guard, bit for bit, for every window and penalty mix { const int V = 512; std::vector base((size_t) V); for (int i = 0; i < V; ++i) base[(size_t) i] = ((float) ((i * 37) % 101) - 50.f) / 7.f; std::vector hist; for (int i = 0; i < 300; ++i) hist.push_back((i * 53 + 11) % V); const float repeats[] = {1.f, 1.1f, 1.5f}; const float freqs[] = {0.f, 0.2f}; const float presents[] = {0.f, 0.3f}; const int lasts[] = {0, -1, 1, 64, 300, 5000}; for (float r : repeats) for (float f : freqs) for (float p : presents) for (int n : lasts) { Sampling t; t.repeat = r; t.freq = f; t.present = p; t.last_n = n; std::vector a = base, b = base; apply_penalties(a, t, hist); old_penalties(b, t, hist); check(same_bits(a, b), "full-vocabulary row: identical to the unguarded loop"); } } // no penalty configured: untouched even with a history { Sampling t; std::vector row(8, 2.f); const std::vector before = row; apply_penalties(row, t, {1, 2, 99}); check(same_bits(row, before), "no penalty: no change"); } // an empty row with a history does not crash { std::vector row; apply_penalties(row, s, {0, 1, 2}); check(row.empty(), "empty row"); } // #4590: the history a request's penalties start from. Default: the last prompt token only (the reply's tokens join it); // STRATA_PENALTY_PROMPT=1 (counts_prompt): the whole prompt, as before. { using strata::qwen35::penalty_history_start; const std::vector prompt = {5, 12, 12, 7777, 7778, 50, 51}; check(!strata::kernels::penalty_counts_prompt(nullptr) && !strata::kernels::penalty_counts_prompt("0") && strata::kernels::penalty_counts_prompt("1"), "the switch: unset and 0 reply only, 1 the old window"); const std::vector h = penalty_history_start(prompt, false); check(h.size() == 1 && h[0] == 51, "default: the history starts at the last prompt token"); check(penalty_history_start(prompt, true) == prompt, "opt-out: the whole prompt"); check(penalty_history_start({}, false).empty(), "empty prompt"); // a penalty on the first pick: the task's token 12 is untouched by default, penalised with the old window Sampling t; t.present = 1.5f; t.last_n = 64; std::vector a(8000, 0.f), b(8000, 0.f); apply_penalties(a, t, h); apply_penalties(b, t, penalty_history_start(prompt, true)); check(a[12] == 0.f && a[7777] == 0.f && a[51] == -1.5f, "default: only the fed-back token is penalised"); check(b[12] == -1.5f && b[7777] == -1.5f && b[51] == -1.5f, "opt-out: the prompt's tokens are penalised"); // a reply that repeats itself is still penalised: tokens written join the history std::vector hr = h; hr.push_back(900); hr.push_back(900); std::vector c(8000, 0.f); t.freq = 0.5f; apply_penalties(c, t, hr); check(c[900] == -(1.5f + 2 * 0.5f) && c[12] == 0.f, "default: the reply's own repeats are still penalised"); } std::printf("qwen35_sampling_test: %d checks passed\n", checks); return 0; }