okami.mom/strata

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

Niko1221Merge f-agent-cap 33648fef1 into f-p4-int (STRATA_SERVE_STOP_SKIP, default on: a decode window that finishes after the server's STOP is dropped whole)07f611c75

main
13.8 KiB211 linesraw
1// (asserts stay on in a Release build: the test is its own check)
2#undef NDEBUG
3#include "strata/core/serve_stop_bound.hpp"
4#include <algorithm>
5#include <cassert>
6#include <iostream>
7#include <string>
8#include <vector>
9
10int main() {
11    int cases = 0;
12    // Independent reference: the existing emission loop, including its budget and EOS rules.
13    for (int accepted = 1; accepted <= 16; ++accepted)
14        for (int remaining = 1; remaining <= 18; ++remaining)
15            for (int eos = -1; eos < accepted; ++eos) {
16                std::vector<int> outputs(accepted);
17                for (int i = 0; i < accepted; ++i) outputs[i] = i == eos ? -1 : 100 + i;
18                std::vector<int> reference;
19                for (int i = 0; i < accepted && static_cast<int>(reference.size()) < remaining; ++i) {
20                    reference.push_back(outputs[i]);
21                    if (outputs[i] == -1) break;
22                }
23                const int kept = strata::core::serve_stop_bound(accepted, remaining, outputs.data(),
24                                                               [](int t) { return t == -1; });
25                assert(kept > 0 && kept <= accepted && kept <= remaining);
26                assert(std::vector<int>(outputs.begin(), outputs.begin() + kept) == reference);
27                // Unlike the old full-window commit, no unexposed input suffix enters the cached history.
28                assert(kept == static_cast<int>(reference.size()));
29                ++cases;
30            }
31    assert(strata::core::serve_stop_bound(4, 0, static_cast<const int*>(nullptr), [](int) { return false; }) == 0);
32    // GEN hold=N:ID: against the emission loop with the hold (stop after N emitted tokens unless ID came first; EOS and
33    // max_new as before), for every window shape, start, hold, max_new and place of ID / EOS in the window
34    int hold_cases = 0;
35    const int ID = 7777;   // a real token id (a negative one means "no hold")
36    for (int accepted = 1; accepted <= 8; ++accepted)
37        for (int produced = 0; produced <= 6; ++produced)
38            for (int hold = 1; hold <= 10; ++hold)
39                for (int max_new = produced + 1; max_new <= 12; ++max_new)
40                    for (int id_at = -1; id_at < accepted; ++id_at)
41                        for (int eos = -1; eos < accepted; ++eos) {
42                            if (eos >= 0 && eos == id_at) continue;
43                            if (produced >= hold) continue;   // a hold the request already reached has ended it
44                            std::vector<int> outputs(accepted);
45                            for (int i = 0; i < accepted; ++i) outputs[i] = i == eos ? -1 : i == id_at ? ID : 100 + i;
46                            std::vector<int> reference;
47                            bool on = true;
48                            int64_t done = produced;
49                            for (int i = 0; i < accepted; ++i) {
50                                const int64_t cap = on ? std::min(max_new, hold) : max_new;
51                                if (done >= cap) break;
52                                reference.push_back(outputs[i]);
53                                ++done;
54                                if (outputs[i] == ID) on = false;
55                                if (outputs[i] == -1) break;
56                            }
57                            const int64_t rem = strata::core::serve_hold_remaining(accepted, produced, max_new, hold, ID,
58                                                                                   outputs.data());
59                            const int kept = strata::core::serve_stop_bound(accepted, rem, outputs.data(),
60                                                                           [](int t) { return t == -1; });
61                            assert(kept == static_cast<int>(reference.size()));
62                            assert(std::vector<int>(outputs.begin(), outputs.begin() + kept) == reference);
63                            // no hold: exactly the max_new bound
64                            assert(strata::core::serve_hold_remaining(accepted, produced, max_new, -1, ID, outputs.data()) ==
65                                   max_new - produced);
66                            ++hold_cases;
67                        }
68    std::cout << "SUMMARY hold cases=" << hold_cases << " passed\n";
69    // --batch slots (P2b): the slot's windows (SlotHold helpers) against an independent per-token reference, over a whole reply
70    // of several windows of changing width: same tokens, same finish, the commit never past what was emitted, the hold ending at
71    // exactly N unless ID came first.  (A window may stop at the hold while ID is still in its rows: the next window goes on.)
72    int slot_cases = 0;
73    const int widths[4] = {1, 3, 2, 4};
74    for (int first = 0; first < 4; ++first)
75        for (int hold = 1; hold <= 10; ++hold)
76            for (int max_new = 1; max_new <= 12; ++max_new)
77                for (int id_at = -1; id_at < 12; ++id_at)
78                    for (int eos = -1; eos < 12; ++eos) {
79                        if (eos >= 0 && eos == id_at) continue;
80                        if (hold > 12) continue;
81                        std::vector<int> stream(14);
82                        for (int i = 0; i < 14; ++i) stream[i] = i == eos ? -1 : i == id_at ? ID : 100 + i;
83                        // reference: one token at a time
84                        std::vector<int> ref;
85                        std::string ref_fin;
86                        {
87                            bool on = true;
88                            for (int i = 0; i < 14; ++i) {
89                                ref.push_back(stream[i]);
90                                if (stream[i] == ID) on = false;
91                                const int produced = static_cast<int>(ref.size());
92                                if (stream[i] == -1) { ref_fin = "stop"; break; }
93                                if (produced >= max_new) { ref_fin = "length"; break; }
94                                if (on && produced >= hold) { ref_fin = "hold"; break; }
95                            }
96                        }
97                        // the slot: windows of widths[...] rows, the cap per window as generate.cpp has it
98                        strata::core::SlotHold h = strata::core::slot_hold_from(hold, ID, true);
99                        std::vector<int> got;
100                        std::string fin;
101                        int64_t produced = 0, committed_rows = 0;
102                        for (int w = first; fin.empty() && produced < 14; ++w) {
103                            const int nrow = widths[w % 4];
104                            int kp = 1;   // the proposals the target picked, in order (here: all of them), and nothing ended
105                            while (kp < nrow && produced + kp < 14 && stream[produced + kp - 1] != -1 &&
106                                   produced + kp + 1 <= strata::core::slot_cap(max_new, h))
107                                ++kp;
108                            committed_rows += kp;
109                            const int64_t base = produced;   // (the window's rows start here; `produced` counts on while they are emitted)
110                            for (int j = 0; j < kp; ++j) {
111                                const int y = stream[base + j];
112                                got.push_back(y);
113                                ++produced;
114                                strata::core::slot_hold_emitted(h, y);
115                                if (y == -1) fin = "stop";
116                                else if (produced >= max_new) fin = "length";
117                                else if (strata::core::slot_hold_reached(h, produced, max_new)) fin = "hold";
118                                if (!fin.empty()) break;
119                            }
120                        }
121                        if (got != ref || fin != ref_fin) {
122                            std::cerr << "MISMATCH first=" << first << " hold=" << hold << " max_new=" << max_new << " id_at=" << id_at << " eos=" << eos
123                                      << " got:";
124                            for (int y : got) std::cerr << ' ' << y;
125                            std::cerr << " (" << fin << ") ref:";
126                            for (int y : ref) std::cerr << ' ' << y;
127                            std::cerr << " (" << ref_fin << ")\n";
128                        }
129                        assert(got == ref);
130                        assert(fin == ref_fin);
131                        assert(committed_rows == static_cast<int64_t>(got.size()));   // not one row committed past what was emitted
132                        if (fin == "hold") {
133                            assert(static_cast<int>(got.size()) == hold);
134                            for (int y : got) assert(y != ID);
135                        }
136                        // no hold: the hold helpers change nothing
137                        assert(strata::core::slot_cap(max_new, strata::core::SlotHold{}) == max_new);
138                        ++slot_cases;
139                    }
140    assert(!strata::core::slot_hold_from(5, -1, true).on && !strata::core::slot_hold_from(0, ID, true).on &&
141           !strata::core::slot_hold_from(5, ID, false).on && strata::core::slot_hold_from(5, ID, true).on);
142    std::cout << "SUMMARY slot hold cases=" << slot_cases << " passed\n";
143    // on unless the variable is exactly "0" (every backend, since the bound left HIP-only)
144    assert(strata::core::serve_stop_bound_on(nullptr));
145    assert(strata::core::serve_stop_bound_on("1"));
146    assert(strata::core::serve_stop_bound_on(""));
147    assert(!strata::core::serve_stop_bound_on("0"));
148    std::cout << "SUMMARY stop_bound cases=" << cases << " passed\n";
149
150    // ---- STRATA_SERVE_STOP_SKIP: a window that finishes after the server's STOP is dropped whole -----------------------
151    // the decision: only when enabled, STOP seen, not merged, not the request's first window, and the commit still undone
152    int skip_cases = 0;
153    for (int m = 0; m < 32; ++m) {
154        const bool enabled = m & 1, stop = m & 2, merged = m & 4, first = m & 8, deferred = m & 16;
155        assert(strata::core::serve_skip_stopped_window(enabled, stop, merged, first, deferred) ==
156               (enabled && stop && !merged && !first && deferred));
157        ++skip_cases;
158    }
159    assert(strata::core::serve_stop_skip_on(nullptr));
160    assert(strata::core::serve_stop_skip_on("1"));
161    assert(strata::core::serve_stop_skip_on(""));
162    assert(!strata::core::serve_stop_skip_on("0"));
163    // A ledger of the serial loop: windows of 1..6 rows; each window commits and prints its rows (bounded by the cap, as above) unless it is dropped;
164    // STOP is seen from the end of window `stop_at` on (-1: never) and the loop breaks after the first window that saw it.
165    // Properties: (1) no STOP, or the switch off: the ledger is the plain loop's; (2) the switch on and the stopped window eligible (not the
166    // first, commit undone): that window adds nothing - committed rows = the rows of the windows before it; (3) not eligible (the first window,
167    // or a one-row window the state advanced itself): as without the switch; (4) the cap ends a request before STOP is looked at: same rows.
168    int ledger_cases = 0;
169    for (int code = 0; code < 6 * 6 * 6 * 6; ++code) {
170        int rows[4];
171        for (int w = 0; w < 4; ++w) rows[w] = 1 + (code / (w == 0 ? 1 : w == 1 ? 6 : w == 2 ? 36 : 216)) % 6;
172        for (int stop_at = -1; stop_at < 4; ++stop_at)
173            for (int self_mask = 0; self_mask < 16; ++self_mask)   // window w is a self-committed one-row window (only when rows[w] == 1)
174                for (int cap = 1; cap <= 12; cap += 3)
175                    for (int on = 0; on < 2; ++on) {
176                        auto run = [&](bool enabled, int& committed, int& emitted) {
177                            committed = emitted = 0;
178                            for (int w = 0; w < 4 && emitted < cap; ++w) {
179                                const bool deferred = !(rows[w] == 1 && ((self_mask >> w) & 1));
180                                const bool stop_seen = stop_at >= 0 && w >= stop_at;
181                                if (strata::core::serve_skip_stopped_window(enabled, stop_seen, false, w == 0, deferred)) break;
182                                const int b = std::min(rows[w], cap - emitted);
183                                committed += b;
184                                emitted += b;
185                                if (stop_seen) break;
186                            }
187                        };
188                        int c_on = 0, e_on = 0, c_off = 0, e_off = 0;
189                        run(on != 0, c_on, e_on);
190                        run(false, c_off, e_off);
191                        assert(c_on == e_on && c_off == e_off);   // rows committed = tokens printed in this model: never a row the client was not sent
192                        if (!on || stop_at < 0) {
193                            assert(c_on == c_off);   // (1)
194                        } else {
195                            // the rows of the windows before `stop_at`, up to the cap
196                            int before = 0;
197                            for (int w = 0; w < stop_at && before < cap; ++w) before += std::min(rows[w], cap - before);
198                            const bool deferred = !(rows[stop_at] == 1 && ((self_mask >> stop_at) & 1));
199                            if (before >= cap) assert(c_on == before && c_off == before);   // (4)
200                            else if (stop_at > 0 && deferred) {
201                                assert(c_on == before);                                       // (2)
202                                assert(c_off == before + std::min(rows[stop_at], cap - before));
203                            } else {
204                                assert(c_on == c_off);                                        // (3)
205                            }
206                        }
207                        ++ledger_cases;
208                    }
209    }
210    std::cout << "SUMMARY stop_skip decision cases=" << skip_cases << " ledger cases=" << ledger_cases << " passed\n";
211}