okami.mom/strata

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

Niko1221Strata: Qwen3.8-Flash-Next on a 12-24 GB NVIDIA GPU + 64 GB RAM, one-click install01529aaa8

main
3.9 KiB74 linesraw
1// src/spec/controller_test.cpp - plan v0.3 P6: the speculation controller's decisions (no model, no GPU).
2#include "strata/spec/controller.hpp"
3
4#include <cstdio>
5
6using namespace strata::spec;
7
8namespace {
9int g_fail = 0;
10void check(bool ok, const char* what) {
11    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", what); ++g_fail; }
12}
13const char* name(Source s) { return s == Source::Mtp ? "mtp" : s == Source::Lookup ? "lookup" : "none"; }
14}  // namespace
15
16int main() {
17    {   // priors (MTP 0.86 per draft): MTP with a short-to-medium window beats plain decoding
18        Controller c;
19        const Choice ch = c.choose(0, 0, true);
20        check(ch.source == Source::Mtp && ch.k >= 2 && ch.k <= 5, "MTP chosen with k in 2..5 at the prior");
21        std::printf("prior: %s k=%d expected %.2f tokens, %.1f tok/s (plain %.1f)\n", name(ch.source), ch.k,
22                    ch.expected_tokens, 1000 * ch.tokens_per_ms, 1000.0 / c.cost().step_ms(0, false));
23    }
24    {   // a drafter that keeps missing its first token is switched off
25        Controller c;
26        Choice ch = c.choose(0, 0, true);
27        for (int i = 0; i < 400; ++i) { c.observe(ch, 0, 0); ch = c.choose(0, 0, true); if (ch.source == Source::None) break; }
28        check(ch.source == Source::None, "persistent rejection turns speculation off");
29        std::printf("after rejections: %s, p0 %.2f\n", name(ch.source), c.mtp_accept(0));
30    }
31    {   // a long lookup match beats MTP; how LONG a window pays depends on what a wide verify costs
32        Controller c;
33        const Choice ch = c.choose(8, 20, true);
34        check(ch.source == Source::Lookup, "long lookup match preferred over MTP");
35        std::printf("long lookup match, measured costs: %s k=%d expected %.2f, %.1f tok/s\n", name(ch.source), ch.k,
36                    ch.expected_tokens, 1000 * ch.tokens_per_ms);
37        CostModel cheap;                                    // a verify pass whose cost barely grows with width
38        cheap.dense_ratio = {1.0, 1.02, 1.04, 1.06, 1.08, 1.1, 1.12, 1.14, 1.16};
39        cheap.hit_rate = 0.95;
40        Controller c2(cheap);
41        const Choice ch2 = c2.choose(8, 20, true);
42        check(ch2.source == Source::Lookup && ch2.k == 8, "cheap wide verify takes the whole lookup window");
43        std::printf("long lookup match, cheap wide verify: %s k=%d expected %.2f, %.1f tok/s\n", name(ch2.source),
44                    ch2.k, ch2.expected_tokens, 1000 * ch2.tokens_per_ms);
45    }
46    {   // a bare trigram match, no MTP: not worth a verify window
47        Controller c;
48        const Choice ch = c.choose(8, 3, false);
49        check(ch.source == Source::None, "short lookup match alone is not used");
50    }
51    {   // perfect acceptance is learned and widens the window
52        Controller c;
53        Choice ch = c.choose(0, 0, true);
54        const int k0 = ch.k;
55        for (int i = 0; i < 400; ++i) { c.observe(ch, ch.k, 0); ch = c.choose(0, 0, true); }
56        check(ch.k > k0 && c.mtp_accept(0) > 0.99, "full acceptance learned and the window widens");
57        std::printf("after full acceptance: %s k=%d (was %d), p0 %.3f\n", name(ch.source), ch.k, k0, c.mtp_accept(0));
58    }
59    std::printf("\nMTP acceptance -> choice (default cost model):\n");
60    for (double p : {0.5, 0.6, 0.7, 0.8, 0.86, 0.9, 0.95}) {
61        Controller c;
62        Choice ch = c.choose(0, 0, true);
63        for (int i = 0; i < 2000; ++i) {                    // feed a stream with iid acceptance p per position
64            int acc = 0;
65            unsigned r = 2654435761u * (unsigned) (i + 1);
66            while (acc < ch.k && ((r = r * 1103515245u + 12345u) >> 8) % 1000 < p * 1000) ++acc;
67            c.observe(ch, acc, 0);
68            ch = c.choose(0, 0, true);
69        }
70        std::printf("  p=%.2f: %s k=%d, %.1f tok/s\n", p, name(ch.source), ch.k, 1000 * ch.tokens_per_ms);
71    }
72    std::printf("controller tests: %s\n", g_fail ? "FAILED" : "OK");
73    return g_fail ? 1 : 0;
74}