okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
6.4 KiB140 linesraw
1// The qwen35 host sampler's penalties (include/strata/qwen35/sampling.hpp), CPU only, no model.
2// #3911: a history token outside a short logit row (--draft-vocab N) must not write past the row.
3#include "strata/qwen35/sampling.hpp"
4
5#include <cstdio>
6#include <cstdlib>
7#include <cstring>
8#include <map>
9
10using strata::qwen35::Sampling;
11using strata::qwen35::apply_penalties;
12
13namespace {
14int checks = 0;
15void check(bool ok, const char* label) {
16    ++checks;
17    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
18}
19
20// The loop as it was before the guard (origin/main of the time), for the in-range comparison.
21void old_penalties(std::vector<float>& lg, const Sampling& s, const std::vector<int32_t>& hist) {
22    if (s.last_n != 0 && (s.repeat != 1.f || s.freq != 0.f || s.present != 0.f)) {
23        std::map<int, int> cnt;
24        const size_t from = s.last_n < 0 || (size_t) s.last_n >= hist.size() ? 0 : hist.size() - (size_t) s.last_n;
25        for (size_t i = from; i < hist.size(); ++i) ++cnt[hist[i]];
26        for (const auto& [id, c] : cnt) {
27            float& v = lg[(size_t) id];
28            if (s.repeat != 1.f) v = v > 0 ? v / s.repeat : v * s.repeat;
29            v -= (float) c * s.freq + s.present;
30        }
31    }
32}
33
34bool same_bits(const std::vector<float>& a, const std::vector<float>& b) {
35    return a.size() == b.size() && std::memcmp(a.data(), b.data(), a.size() * sizeof(float)) == 0;
36}
37}  // namespace
38
39int main() {
40    Sampling s;
41    s.repeat = 1.2f; s.freq = 0.1f; s.present = 0.05f; s.last_n = -1;
42
43    // a short row (a 64-token draft vocabulary) and a history with tokens inside it, past it and negative.  The row's
44    // storage past its size is filled with a marker first: a write past the row would change the marker.
45    {
46        std::vector<float> row(256, 7.f);
47        row.resize(64);
48        for (int i = 0; i < 64; ++i) row[(size_t) i] = (float) i - 20.f;
49        const std::vector<float> before(row.begin(), row.end());
50        const std::vector<int32_t> hist = {3, 100, 3, 63, 64, 65, 255, 100000, -1, 7};
51        apply_penalties(row, s, hist);
52        for (int i = 64; i < 256; ++i) check(row.data()[i] == 7.f, "nothing is written past the row");
53        check(row.size() == 64, "the row keeps its size");
54        // tokens 3 (twice), 63 and 7 are in the row and are penalised; the others are skipped
55        std::vector<float> want = before;
56        for (int id : {3, 7, 63}) {
57            const int c = id == 3 ? 2 : 1;
58            float& v = want[(size_t) id];
59            v = v > 0 ? v / s.repeat : v * s.repeat;
60            v -= (float) c * s.freq + s.present;
61        }
62        check(same_bits(row, want), "the in-row tokens take the penalty, the rest of the row is untouched");
63    }
64
65    // history tokens that are all outside the row: the row does not change
66    {
67        std::vector<float> row(16, 1.5f);
68        const std::vector<float> before = row;
69        apply_penalties(row, s, {16, 17, 1000, -5});
70        check(same_bits(row, before), "only out-of-row tokens: no change");
71    }
72
73    // a row of the whole vocabulary behaves exactly as before the guard, bit for bit, for every window and penalty mix
74    {
75        const int V = 512;
76        std::vector<float> base((size_t) V);
77        for (int i = 0; i < V; ++i) base[(size_t) i] = ((float) ((i * 37) % 101) - 50.f) / 7.f;
78        std::vector<int32_t> hist;
79        for (int i = 0; i < 300; ++i) hist.push_back((i * 53 + 11) % V);
80        const float repeats[] = {1.f, 1.1f, 1.5f};
81        const float freqs[] = {0.f, 0.2f};
82        const float presents[] = {0.f, 0.3f};
83        const int lasts[] = {0, -1, 1, 64, 300, 5000};
84        for (float r : repeats) for (float f : freqs) for (float p : presents) for (int n : lasts) {
85            Sampling t;
86            t.repeat = r; t.freq = f; t.present = p; t.last_n = n;
87            std::vector<float> a = base, b = base;
88            apply_penalties(a, t, hist);
89            old_penalties(b, t, hist);
90            check(same_bits(a, b), "full-vocabulary row: identical to the unguarded loop");
91        }
92    }
93
94    // no penalty configured: untouched even with a history
95    {
96        Sampling t;
97        std::vector<float> row(8, 2.f);
98        const std::vector<float> before = row;
99        apply_penalties(row, t, {1, 2, 99});
100        check(same_bits(row, before), "no penalty: no change");
101    }
102
103    // an empty row with a history does not crash
104    {
105        std::vector<float> row;
106        apply_penalties(row, s, {0, 1, 2});
107        check(row.empty(), "empty row");
108    }
109
110    // #4590: the history a request's penalties start from.  Default: the last prompt token only (the reply's tokens join it);
111    // STRATA_PENALTY_PROMPT=1 (counts_prompt): the whole prompt, as before.
112    {
113        using strata::qwen35::penalty_history_start;
114        const std::vector<int32_t> prompt = {5, 12, 12, 7777, 7778, 50, 51};
115        check(!strata::kernels::penalty_counts_prompt(nullptr) && !strata::kernels::penalty_counts_prompt("0") &&
116                  strata::kernels::penalty_counts_prompt("1"), "the switch: unset and 0 reply only, 1 the old window");
117        const std::vector<int32_t> h = penalty_history_start(prompt, false);
118        check(h.size() == 1 && h[0] == 51, "default: the history starts at the last prompt token");
119        check(penalty_history_start(prompt, true) == prompt, "opt-out: the whole prompt");
120        check(penalty_history_start({}, false).empty(), "empty prompt");
121        // a penalty on the first pick: the task's token 12 is untouched by default, penalised with the old window
122        Sampling t;
123        t.present = 1.5f; t.last_n = 64;
124        std::vector<float> a(8000, 0.f), b(8000, 0.f);
125        apply_penalties(a, t, h);
126        apply_penalties(b, t, penalty_history_start(prompt, true));
127        check(a[12] == 0.f && a[7777] == 0.f && a[51] == -1.5f, "default: only the fed-back token is penalised");
128        check(b[12] == -1.5f && b[7777] == -1.5f && b[51] == -1.5f, "opt-out: the prompt's tokens are penalised");
129        // a reply that repeats itself is still penalised: tokens written join the history
130        std::vector<int32_t> hr = h;
131        hr.push_back(900); hr.push_back(900);
132        std::vector<float> c(8000, 0.f);
133        t.freq = 0.5f;
134        apply_penalties(c, t, hr);
135        check(c[900] == -(1.5f + 2 * 0.5f) && c[12] == 0.f, "default: the reply's own repeats are still penalised");
136    }
137
138    std::printf("qwen35_sampling_test: %d checks passed\n", checks);
139    return 0;
140}