okami.mom/strata

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

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
6.4 KiB108 linesraw
1// src/spec/window_rule_test.cpp - which rule picks the verify window, and whose q lists judge the candidates (spec/window_rule.hpp).
2//
3// DFlash2 runs with the MTP layer loaded beside it as the fallback (use_mtp stays true).  Before this rule existed, the cost-aware
4// window (STRATA_SPEC_COST=1, a GB10 default) read `chain_rows`, which only the MTP chain writes, so DFlash2 candidates were
5// verified one row at a time; and the MTP's q lists (STRATA_SPEC_PROB=1) were attached to DFlash2's windows.  The tests:
6//   1. the table of the fixed cases (DFlash2 + cost rule with and without --draft-conf / --spec-min-p);
7//   2. the DEFAULT PATH IS UNCHANGED: for every input the old inline code handled correctly (MTP as the source, any flags;
8//      DFlash2 with --draft-conf > 0 or with the cost rule off) a copy of the old code returns the same window;
9//   3. the q rule.
10#include "strata/spec/window_rule.hpp"
11
12#include <algorithm>
13#include <cstdio>
14#include <cstdlib>
15
16using namespace strata::spec;
17
18namespace {
19int g_fail = 0;
20void check(bool ok, const char* what) {
21    std::printf("  %-78s %s\n", what, ok ? "ok" : "FAIL");
22    if (!ok) ++g_fail;
23}
24
25// the inline code of f-0291-df2-mem 96f0307aa (the min-p rule on `draft_on`, the cost rule on use_mtp; CLI loop; the serve loop differs only in the start value and in first_window, which both callers
26// overwrite afterwards)
27int old_rule(const WindowRuleIn& in) {
28    int T = in.start;
29    if (in.use_mtp && in.spec_cost && !in.first_window) {
30        T = std::clamp(in.chain_rows, 1, in.cap);
31    } else if (in.draft_on && in.spec_min_p > 0.0) {
32        T = 1;
33        while (T < in.cap && in.dprob[T - 1] >= (float) in.spec_min_p) ++T;
34    }
35    if (in.df2_active && in.draft_conf > 0.0) {
36        T = 1;
37        double chain = 1.0;
38        while (T < in.cap && (chain *= (double) in.dprob[T - 1]) >= in.draft_conf) ++T;
39    }
40    return T;
41}
42}  // namespace
43
44int main() {
45    std::printf("window_rule_test\n");
46    const float p[8] = {0.9f, 0.8f, 0.3f, 0.9f, 0.9f, 0.9f, 0.9f, 0.9f};
47    {   // 1. the table
48        WindowRuleIn in;
49        in.use_mtp = true; in.draft_on = true; in.cap = 6; in.start = 6; in.dprob = p; in.chain_rows = 1;
50        in.df2_active = true; in.spec_cost = true;
51        check(choose_window(in) == 6, "DFlash2 + cost rule, no min-p, no conf: the full cap (was 1)");
52        in.spec_min_p = 0.5;
53        check(choose_window(in) == 3, "DFlash2 + cost rule + --spec-min-p 0.5: stops at the weak draft (was 1)");
54        in.spec_min_p = 0.0; in.draft_conf = 0.35;
55        check(choose_window(in) == 3, "DFlash2 + cost rule + --draft-conf 0.35: the chain product rule (0.9*0.8*0.3=0.216)");
56        in.draft_conf = 0.0; in.first_window = true;
57        check(choose_window(in) == 6, "first window: left to the caller (it sets 1 / merge_T after)");
58        in = WindowRuleIn{};
59        in.use_mtp = true; in.draft_on = true; in.cap = 4; in.start = 4; in.dprob = p; in.spec_cost = true; in.chain_rows = 2;
60        check(choose_window(in) == 2, "MTP + cost rule: the chain's rows");
61        in.chain_rows = 9;
62        check(choose_window(in) == 4, "MTP + cost rule: clamped to the cap");
63        in.spec_cost = false; in.spec_min_p = 0.5;
64        check(choose_window(in) == 3, "MTP + min-p 0.5: the first weak draft ends the window");
65        in.spec_min_p = 0.0;
66        check(choose_window(in) == 4, "MTP, no rule: the full cap");
67        in.use_mtp = false; in.draft_on = false; in.start = 1;
68        check(choose_window(in) == 1, "no MTP (serve): one row unless a lookup fires");
69    }
70    {   // 2. exhaustive over the small input space; random probabilities
71        std::srand(7);
72        long n = 0, same = 0, differ_df2_cost = 0, differ_other = 0;
73        for (int use_mtp = 0; use_mtp < 2; ++use_mtp)
74            for (int df2 = 0; df2 < 2; ++df2)
75                for (int cost = 0; cost < 2; ++cost)
76                    for (int first = 0; first < 2; ++first)
77                        for (int cap = 1; cap <= 8; ++cap)
78                            for (int rows = 1; rows <= 8; ++rows)
79                                for (double mp : {0.0, 0.25, 0.5})
80                                    for (double dc : {0.0, 0.15, 0.35}) {
81                                        float pr[8];
82                                        for (float& x : pr) x = (float) (std::rand() % 101) / 100.0f;
83                                        WindowRuleIn in;
84                                        in.use_mtp = use_mtp; in.draft_on = use_mtp || df2; in.df2_active = df2; in.spec_cost = cost; in.first_window = first;
85                                        in.chain_rows = rows; in.cap = cap; in.start = use_mtp ? cap : 1; in.spec_min_p = mp;
86                                        in.draft_conf = dc; in.dprob = pr;
87                                        ++n;
88                                        const int a = old_rule(in), b = choose_window(in);
89                                        if (a == b) { ++same; continue; }
90                                        // a difference is allowed only where the old rule used the MTP chain's rows for DFlash2's candidates
91                                        if (use_mtp && df2 && cost && !first && dc == 0.0) ++differ_df2_cost; else ++differ_other;
92                                    }
93        std::printf("  %ld inputs: %ld identical, %ld differ (DFlash2 + cost rule, no --draft-conf), %ld differ elsewhere\n", n, same, differ_df2_cost, differ_other);
94        check(differ_other == 0, "default path unchanged: no input outside the buggy case changes its window");
95        check(differ_df2_cost > 0, "the buggy case does change (the test sees it)");
96    }
97    {   // 3. the q rule
98        check(mtp_q_applies(true, false, true, false, false, 4), "MTP drew the window, sampled, probabilistic: q attached");
99        check(!mtp_q_applies(true, true, true, false, false, 4), "DFlash2 drew the window: no MTP q (exact-match path)");
100        check(!mtp_q_applies(true, false, true, true, false, 4), "a lookup window: point masses, no q");
101        check(!mtp_q_applies(true, false, true, false, true, 4), "first window: no q");
102        check(!mtp_q_applies(true, false, true, false, false, 1), "one row: no q");
103        check(!mtp_q_applies(true, false, false, false, false, 4), "greedy / mode off: no q");
104        check(!mtp_q_applies(false, false, true, false, false, 4), "no MTP: no q");
105    }
106    std::printf("%s\n", g_fail == 0 ? "window_rule_test: ok" : "window_rule_test: FAILED");
107    return g_fail == 0 ? 0 : 1;
108}