okami.mom/strata

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

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

main
6.5 KiB125 linesraw
1// #4590: which tokens the repeat / frequency / presence penalties count.  Header-only, CPU, no model.
2//
3// Default: the current reply only.  The prompt (the new task's digits, a quoted earlier answer, earlier assistant turns)
4// is not in the window; the last prompt token, which the first decode step feeds in, is.  STRATA_PENALTY_PROMPT=1 is the
5// old window: the last penalty_last_n tokens of prompt + reply.
6#include "strata/kernels/sampler.hpp"
7
8#include <cstdio>
9#include <cstdlib>
10#include <vector>
11
12using strata::kernels::penalty_counts_prompt;
13using strata::kernels::penalty_rows;
14using strata::kernels::penalty_window_from;
15
16namespace {
17int checks = 0;
18void check(bool ok, const char* label) {
19    ++checks;
20    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
21}
22
23constexpr int kH = 64;           // penalty_last_n 64 (the server's default window)
24constexpr int32_t kTaskTok = 12; // a token of the new task ("12"), in the last 64 prompt tokens
25constexpr int32_t kOldAnswer = 7777;   // a token of an earlier assistant turn, quoted near the end of the prompt
26
27// A request's prompt: filler, an earlier assistant answer (kOldAnswer tokens), then the new task (kTaskTok), then the
28// assistant header whose last token is the fed-back head x.
29std::vector<int32_t> prompt() {
30    std::vector<int32_t> ids;
31    for (int i = 0; i < 300; ++i) ids.push_back(1000 + i);
32    for (int i = 0; i < 20; ++i) ids.push_back(kOldAnswer + (i % 3));
33    for (int i = 0; i < 10; ++i) ids.push_back(kTaskTok);
34    for (int i = 0; i < 4; ++i) ids.push_back(50 + i);
35    return ids;
36}
37
38// The row the engine stages for the pick after `reply` has written its tokens: the tokens `consumed` holds when decode
39// began (the prompt without its last token) followed by the reply, read from penalty_window_from().
40std::vector<int32_t> row_after(const std::vector<int32_t>& ids, const std::vector<int32_t>& reply, bool counts_prompt) {
41    std::vector<int32_t> consumed(ids.begin(), ids.end() - 1);
42    const size_t from = penalty_window_from(consumed.size(), counts_prompt);
43    const int32_t x = ids.back();
44    // window = [x, reply...]: the single row of the last one is "the pick after the last reply token"
45    std::vector<int32_t> window = {x};
46    window.insert(window.end(), reply.begin(), reply.end());
47    const int T = (int) window.size();
48    std::vector<int32_t> rows((size_t) T * kH);
49    penalty_rows(consumed.data() + from, (int64_t) (consumed.size() - from), window.data(), T, kH, rows.data());
50    return std::vector<int32_t>(rows.end() - kH, rows.end());   // the last row
51}
52
53int count_of(const std::vector<int32_t>& row, int32_t tok) {
54    int c = 0;
55    for (int32_t t : row) c += t == tok;
56    return c;
57}
58int filled(const std::vector<int32_t>& row) {
59    int c = 0;
60    for (int32_t t : row) c += t >= 0;
61    return c;
62}
63}  // namespace
64
65int main() {
66    // ---- the switch: unset, empty, 0 and anything but "1" is the default (reply only)
67    check(!penalty_counts_prompt(nullptr), "env unset: reply only");
68    check(!penalty_counts_prompt(""), "env empty: reply only");
69    check(!penalty_counts_prompt("0"), "env 0 (the old opt-in value): reply only");
70    check(!penalty_counts_prompt("2"), "env 2: reply only");
71    check(penalty_counts_prompt("1"), "env 1: the old window");
72
73    const std::vector<int32_t> ids = prompt();
74    const int32_t x = ids.back();
75
76    // ---- first pick (nothing written yet)
77    {
78        const std::vector<int32_t> r = row_after(ids, {}, false);
79        check(filled(r) == 1 && r[kH - 1] == x, "default: the first pick counts only the fed-back head x");
80        check(count_of(r, kTaskTok) == 0 && count_of(r, kOldAnswer) == 0, "default: no prompt task / earlier-answer token");
81        const std::vector<int32_t> o = row_after(ids, {}, true);
82        check(filled(o) == kH && o[kH - 1] == x, "opt-out: a full window of prompt tokens ends in x");
83        check(count_of(o, kTaskTok) == 10, "opt-out: the new task's token is in the window (the old behaviour)");
84    }
85
86    // ---- mid reply: the reply's own tokens are counted in both modes, a loop still gets penalised
87    {
88        const std::vector<int32_t> reply = {900, 901, 900, 902, 900};
89        const std::vector<int32_t> r = row_after(ids, reply, false);
90        check(filled(r) == 1 + (int) reply.size(), "default: the window is x + the reply");
91        check(count_of(r, 900) == 3 && count_of(r, 901) == 1 && count_of(r, 902) == 1, "default: reply tokens counted");
92        check(count_of(r, kTaskTok) == 0 && count_of(r, kOldAnswer) == 0, "default: still no prompt token mid reply");
93        const std::vector<int32_t> o = row_after(ids, reply, true);
94        check(count_of(o, 900) == 3 && count_of(o, kTaskTok) > 0, "opt-out: reply and prompt tokens both counted");
95    }
96
97    // ---- a long reply: the window slides over the reply alone, the prompt never comes back
98    {
99        std::vector<int32_t> reply;
100        for (int i = 0; i < 200; ++i) reply.push_back(2000 + (i % 7));
101        const std::vector<int32_t> r = row_after(ids, reply, false);
102        check(filled(r) == kH, "default: a reply longer than the window fills it");
103        bool prompt_token = false;
104        for (int32_t t : r) prompt_token |= (t >= 0 && t < 2000);
105        check(!prompt_token, "default: after 63 reply tokens no prompt token (not even x) is in the window");
106        const std::vector<int32_t> o = row_after(ids, reply, true);
107        check(filled(o) == kH && count_of(o, kTaskTok) == 0, "opt-out: the same long reply has pushed the prompt out too");
108    }
109
110    // ---- a restored conversation: the session comes back from the conversation cache or a saved slot with its state, but
111    // the history is built from the request's ids and the tokens written since, so it is the same as a cold read of the
112    // same ids.  A second turn that quotes the first answer in the prompt does not carry the first reply's window over.
113    {
114        const std::vector<int32_t> cold = row_after(ids, {900, 901}, false);
115        const std::vector<int32_t> restored = row_after(ids, {900, 901}, false);
116        check(cold == restored, "restored session: same window as a cold read of the same ids");
117        std::vector<int32_t> next = ids;   // turn 2: the prompt is turn 1's prompt + its reply + a new task
118        next.insert(next.end(), {900, 901, 902, 99});
119        const std::vector<int32_t> r2 = row_after(next, {}, false);
120        check(filled(r2) == 1 && r2[kH - 1] == 99, "turn 2 default: turn 1's reply tokens in the prompt are not counted");
121    }
122
123    std::printf("penalty_window_test: %d checks passed\n", checks);
124    return 0;
125}