MCPcopy Create free account
hub / github.com/Niko1221/Strata / main

Function main

engine/src/spec/controller_test.cpp:16–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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;

Callers

nothing calls this directly

Calls 6

step_msMethod · 0.80
mtp_acceptMethod · 0.80
checkFunction · 0.70
nameFunction · 0.70
chooseMethod · 0.45
observeMethod · 0.45

Tested by

no test coverage detected