okami.mom/strata

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

Niko1221Merge f-027-kva (KVA ships off in 0.2.7: STRATA_KVA unset is the exact 0.2.6 engine; STRATA_KVA=1 approximates layers 36-47 with a 2048-token exact tail, the configuration that kept agent loops and long extraction at the exact level; geometry gate; draft fixes; opt-in STRATA_KVA_SKIP=moe)9c059bade

main
11.0 KiB202 linesraw
1// src/prefill/kva_test.cpp - the KVA switch, the automatic tail and the reasons a read is turned away (no GPU needed).
2#include "strata/kernels/window_start.hpp"
3#include "strata/prefill/kva.hpp"
4#include "strata/prefill/kva_floor.hpp"
5
6#include <cstdio>
7#include <cstdlib>
8#include <cstring>
9#include <string>
10#include <vector>
11
12using namespace strata::prefill;
13
14static int fails = 0;
15#define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0)
16
17static void unset(const char* k) {
18#if defined(_WIN32)
19    _putenv_s(k, "");
20#else
21    unsetenv(k);
22#endif
23}
24static void put(const char* k, const char* v) {
25#if defined(_WIN32)
26    _putenv_s(k, v);
27#else
28    setenv(k, v, 1);
29#endif
30}
31static void clear_all() {
32    for (const char* k : {"STRATA_KVA", "STRATA_KVA_SPLIT", "STRATA_KVA_SUFFIX", "STRATA_KVA_MIN_PROMPT", "STRATA_KVA_PARITY",
33                          "STRATA_KVA_CAPTURE", "STRATA_KVA_CAPTURE_FP32", "STRATA_KVA_ORACLE", "STRATA_KVA_ORACLE_FP32",
34                          "STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR", "STRATA_KVA_SKIP", "STRATA_KVA_ANY_MODEL"})
35        unset(k);
36}
37static bool is(const char* got, const char* want) { return got != nullptr && std::strcmp(got, want) == 0; }
38
39int main() {
40    // ---- the switch: off unless STRATA_KVA=1 (0.2.7 ships KVA opt-in; unset is the exact 0.2.6 engine)
41    clear_all();
42    {
43        const KvaConfig c = kva_config_from_env();
44        CHECK(!c.on && !c.explicit_on);                                      // unset: off
45        CHECK(c.split == 36 && c.min_prompt == 4096 && c.suffix_env == 0 && kKvaSplitDefault == 36);
46        CHECK(c.mode == KvaConfig::Mode::Identity && c.only == KvaConfig::Only::All && !c.capture);
47        KvaEngine eng;
48        CHECK(!c.wants_cell_floor(eng, true));
49        CHECK(is(c.decline(eng, 0, 30000, 0, -1, 48), "off (STRATA_KVA is not 1)"));   // a 30K cold prompt is read exactly
50    }
51    put("STRATA_KVA", "0");
52    { const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); }
53    put("STRATA_KVA", "1");
54    { const KvaConfig c = kva_config_from_env(); CHECK(c.on && c.explicit_on); }
55    put("STRATA_KVA", "");   // an empty value is "not set"
56    { const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); }
57    clear_all();
58    put("STRATA_KVA", "1");   // everything below runs with KVA switched on
59
60    // ---- switched on: split 36 and a 2048-token tail (the configuration measured), or STRATA_KVA_SPLIT / STRATA_KVA_SUFFIX
61    {
62        const KvaConfig c = kva_config_from_env();
63        CHECK(c.on && c.split == 36 && c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 2048);
64        put("STRATA_KVA_SPLIT", "30");
65        CHECK(kva_config_from_env().split == 30);
66        unset("STRATA_KVA_SPLIT");
67        put("STRATA_KVA_SUFFIX", "3000");
68        const KvaConfig e = kva_config_from_env();
69        CHECK(e.effective_suffix() == 3000);
70        unset("STRATA_KVA_SUFFIX");
71    }
72
73    // ---- who is turned away, and why (the first reason that holds is the one named)
74    {
75        const KvaConfig c = kva_config_from_env();
76        KvaEngine eng;
77        bool routine = false;
78        // a cold 14.6K prompt on one device: applies (14600 > 2048 + 64)
79        CHECK(c.decline(eng, 0, 14600, 0, -1, 48) == nullptr);
80        CHECK(c.decline(eng, 0, 14600, 0, 48, 48) == nullptr);   // le == n_layers is the whole trunk too
81        // a continuation (turn 2 of a chat, a resumed prompt): routine
82        CHECK(is(c.decline(eng, 9000, 14600, 0, -1, 48, false, false, &routine), "a continuation read (pos0 != 0)") && routine);
83        // short prompts and a prompt the tail would swallow: routine
84        CHECK(is(c.decline(eng, 0, 3000, 0, -1, 48, false, false, &routine), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine);
85        CHECK(c.decline(eng, 0, 4096, 0, -1, 48) == nullptr);   // 4096 is the first prompt that is long enough
86        put("STRATA_KVA_SUFFIX", "5000");   // a longer tail swallows a prompt of 5000 and not one of 5065
87        { const KvaConfig s = kva_config_from_env();
88          CHECK(is(s.decline(eng, 0, 5000, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine);
89          CHECK(s.decline(eng, 0, 5065, 0, -1, 48) == nullptr); }
90        unset("STRATA_KVA_SUFFIX");
91        // not routine: the user may want to know
92        CHECK(is(c.decline(eng, 0, 14600, 24, -1, 48, false, false, &routine), "a layer split (not the whole trunk)") && !routine);
93        CHECK(is(c.decline(eng, 0, 14600, 0, 24, 48), "a layer split (not the whole trunk)"));
94        CHECK(is(c.decline(eng, 0, 14600, 0, -1, 48, true), "a prompt with pictures"));
95        CHECK(is(c.decline(eng, 0, 14600, 0, -1, 48, false, true), "the DFlash2 drafter reads the late layers' hidden states"));
96        // the engine rules the whole process out (batch slots, a layer split, a peer device)
97        KvaEngine blocked = eng;
98        blocked.blocked = "batch slots (--batch)";
99        CHECK(is(c.decline(blocked, 0, 14600, 0, -1, 48, false, false, &routine), "batch slots (--batch)") && !routine);
100        // a model whose shape KVA was not measured on (not 48 layers / 512 experts) is turned away, not warned about;
101        // the routine reasons (a short prompt, a continuation) still come first
102        CHECK(is(c.decline(eng, 0, 14600, 0, -1, 40, false, false, &routine, true), kKvaShapeReason) && !routine);
103        CHECK(is(c.decline(eng, 0, 3000, 0, -1, 40, false, false, &routine, true), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine);
104        CHECK(c.decline(eng, 0, 14600, 0, -1, 48, false, false, &routine, false) == nullptr);   // the measured shape applies
105        put("STRATA_KVA_ANY_MODEL", "1");   // the research override allows it (prefill.cpp then warns once)
106        { const KvaConfig a = kva_config_from_env(); CHECK(a.any_model && a.decline(eng, 0, 14600, 0, -1, 40, false, false, nullptr, true) == nullptr); }
107        unset("STRATA_KVA_ANY_MODEL");
108        CHECK(!kva_config_from_env().any_model);
109        // capture wins over the rest
110        put("STRATA_KVA_CAPTURE", "/tmp/kva-cap");
111        { const KvaConfig o = kva_config_from_env(); CHECK(o.capture && is(o.decline(eng, 0, 14600, 0, -1, 48), "capture mode")); }
112        unset("STRATA_KVA_CAPTURE");
113    }
114    clear_all();
115
116    // ---- the research modes still parse
117    put("STRATA_KVA_ORACLE", "/tmp/o");
118    put("STRATA_KVA_ORACLE_FP32", "1");
119    put("STRATA_KVA_ONLY", "qsa");
120    put("STRATA_KVA_SPLIT", "30");
121    put("STRATA_KVA_MIN_PROMPT", "1024");
122    {
123        const KvaConfig c = kva_config_from_env();
124        CHECK(c.mode == KvaConfig::Mode::Oracle && c.oracle_fp32 && c.oracle_dir == "/tmp/o");
125        CHECK(c.only == KvaConfig::Only::Qsa && c.split == 30 && c.min_prompt == 1024);
126    }
127    clear_all();
128
129    // ---- STRATA_KVA_SKIP: the default is the state-only form; "moe" runs the attention halves and skips the MoE half
130    {
131        CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly);
132        put("STRATA_KVA_SKIP", "moe");
133        CHECK(kva_config_from_env().skip == KvaConfig::Skip::Moe);
134        put("STRATA_KVA_SKIP", "state");
135        CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly);
136        put("STRATA_KVA_SKIP", "bogus");
137        CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly);
138        unset("STRATA_KVA_SKIP");
139    }
140    // ---- "not applied => identical to STRATA_KVA=0", the parts that need no GPU
141    // (1) by default the draft layer is 0.2.6's own code; the cell floor is a research option, wired only when asked
142    {
143        KvaEngine eng;
144        put("STRATA_KVA", "1");
145        const KvaConfig on = kva_config_from_env();
146        CHECK(on.on && !on.draft_floor && !on.wants_cell_floor(eng, true));
147        put("STRATA_KVA_DRAFT_FLOOR", "1");
148        const KvaConfig fl = kva_config_from_env();
149        CHECK(fl.draft_floor && fl.wants_cell_floor(eng, true));
150        CHECK(!fl.wants_cell_floor(eng, false));          // no draft layer: nothing to wire
151        KvaEngine blocked;
152        blocked.blocked = "batch slots (--batch)";
153        CHECK(!fl.wants_cell_floor(blocked, true));       // ruled out for the process: 0.2.6's own attention code
154        put("STRATA_KVA", "0");
155        const KvaConfig off = kva_config_from_env();
156        CHECK(!off.wants_cell_floor(eng, true));          // STRATA_KVA=0: untouched
157        clear_all();
158    }
159    // (2) with a floor of 0 the window is exactly the plain window, for every length and every window
160    {
161        using strata::kernels::window_start;
162        bool same = true;
163        for (int window : {1, 2, 64, 2048, 32768, 0x7fffffff})
164            for (int n_kv : {0, 1, 2, 63, 64, 65, 2047, 2048, 2049, 4096, 14000, 32768, 32769, 100000}) {
165                const int plain = n_kv > window ? n_kv - window : 0;   // what window_ids always selected
166                if (window_start(n_kv, window, 0) != plain) same = false;
167            }
168        CHECK(same);
169        CHECK(window_start(14000, 32768, 11264) == 11264);   // a floor inside the window raises the start
170        CHECK(window_start(14000, 2048, 11264) == 11952);    // a floor below the window changes nothing
171        CHECK(window_start(14000, 32768, 20000) == 13999);   // a floor past the end keeps one cell
172        CHECK(window_start(14000, 32768, 14000) == 13999);
173        CHECK(window_start(1, 32768, 5) == 0);
174        CHECK(window_start(0, 32768, 5) == 0);
175    }
176    // (3) the floor follows the session's tokens: a state no KVA read produced has floor 0
177    {
178        KvaFloorRegistry reg;
179        std::vector<int64_t> doc;
180        for (int i = 0; i < 20000; ++i) doc.push_back(1000 + (i * 7919) % 5000);
181        CHECK(reg.floor_for(doc.data(), (int64_t) doc.size()) == 0);        // nothing remembered
182        reg.note(doc.data(), 11264);                                         // a KVA read approximated [0, 11264)
183        CHECK(reg.floor_for(doc.data(), 20000) == 11264);                    // the live session / a later turn of it
184        CHECK(reg.floor_for(doc.data(), 5000) == 5000);                      // a checkpoint inside the approximated part
185        std::vector<int64_t> other(doc);
186        for (int i = 0; i < 20000; ++i) other[(size_t) i] += 1;             // a different prompt entirely
187        CHECK(reg.floor_for(other.data(), 20000) == 0);                      // short chats, other documents: floor 0
188        std::vector<int64_t> shared(doc.begin(), doc.begin() + 3000);        // a new chat sharing the first 3000 tokens
189        for (int i = 3000; i < 9000; ++i) shared.push_back(900000 + i);
190        CHECK(reg.floor_for(shared.data(), 9000) == 3000);
191        reg.note(doc.data(), 11264);                                         // the same read again: no second entry
192        CHECK(reg.size() == 1);
193        KvaFloorRegistry small(2);
194        for (int k = 0; k < 5; ++k) { std::vector<int64_t> d(100, k); small.note(d.data(), 100); }
195        CHECK(small.size() == 2);                                            // bounded
196        reg.clear();
197        CHECK(reg.floor_for(doc.data(), 20000) == 0);
198    }
199
200    if (fails == 0) std::printf("kva_test: ok\n");
201    return fails == 0 ? 0 : 1;
202}