okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
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" ); 22if (!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 ) { 28int T = in .start ; 29if (in .use_mtp && in .spec_cost && !in .first_window ) { 30T = std::clamp (in .chain_rows ,1 ,in .cap ); 31 }else if (in .draft_on && in .spec_min_p > 0.0 ) { 32T = 1 ; 33while (T < in .cap && in .dprob [T - 1 ] >= (float )in .spec_min_p )++ T ; 34 } 35if (in .df2_active && in .draft_conf > 0.0 ) { 36T = 1 ; 37double chain = 1.0 ; 38while (T < in .cap && (chain *= (double )in .dprob [T - 1 ]) >=in .draft_conf )++ T ; 39 } 40return T ; 41} 42}// namespace 43 44int main () { 45 std::printf ("window_rule_test\n" ); 46const float p [8 ]= {0.9f ,0.8f ,0.3f ,0.9f ,0.9f ,0.9f ,0.9f ,0.9f }; 47 {// 1. the table 48WindowRuleIn in ; 49in .use_mtp = true;in .draft_on = true;in .cap = 6 ;in .start = 6 ;in .dprob = p ;in .chain_rows = 1 ; 50in .df2_active = true;in .spec_cost = true; 51check (choose_window (in )== 6 ,"DFlash2 + cost rule, no min-p, no conf: the full cap (was 1)" ); 52in .spec_min_p = 0.5 ; 53check (choose_window (in )== 3 ,"DFlash2 + cost rule + --spec-min-p 0.5: stops at the weak draft (was 1)" ); 54in .spec_min_p = 0.0 ;in .draft_conf = 0.35 ; 55check (choose_window (in )== 3 ,"DFlash2 + cost rule + --draft-conf 0.35: the chain product rule (0.9*0.8*0.3=0.216)" ); 56in .draft_conf = 0.0 ;in .first_window = true; 57check (choose_window (in )== 6 ,"first window: left to the caller (it sets 1 / merge_T after)" ); 58in = WindowRuleIn {}; 59in .use_mtp = true;in .draft_on = true;in .cap = 4 ;in .start = 4 ;in .dprob = p ;in .spec_cost = true;in .chain_rows = 2 ; 60check (choose_window (in )== 2 ,"MTP + cost rule: the chain's rows" ); 61in .chain_rows = 9 ; 62check (choose_window (in )== 4 ,"MTP + cost rule: clamped to the cap" ); 63in .spec_cost = false;in .spec_min_p = 0.5 ; 64check (choose_window (in )== 3 ,"MTP + min-p 0.5: the first weak draft ends the window" ); 65in .spec_min_p = 0.0 ; 66check (choose_window (in )== 4 ,"MTP, no rule: the full cap" ); 67in .use_mtp = false;in .draft_on = false;in .start = 1 ; 68check (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 ); 72long n = 0 ,same = 0 ,differ_df2_cost = 0 ,differ_other = 0 ; 73for (int use_mtp = 0 ;use_mtp < 2 ;++ use_mtp ) 74for (int df2 = 0 ;df2 < 2 ;++ df2 ) 75for (int cost = 0 ;cost < 2 ;++ cost ) 76for (int first = 0 ;first < 2 ;++ first ) 77for (int cap = 1 ;cap <=8 ;++ cap ) 78for (int rows = 1 ;rows <=8 ;++ rows ) 79for (double mp : {0.0 ,0.25 ,0.5 }) 80for (double dc : {0.0 ,0.15 ,0.35 }) { 81float pr [8 ]; 82for (float & x :pr )x = (float ) (std::rand () %101 ) /100.0f ; 83WindowRuleIn in ; 84in .use_mtp = use_mtp ;in .draft_on = use_mtp || df2 ;in .df2_active = df2 ;in .spec_cost = cost ;in .first_window = first ; 85in .chain_rows = rows ;in .cap = cap ;in .start = use_mtp ?cap :1 ;in .spec_min_p = mp ; 86in .draft_conf = dc ;in .dprob = pr ; 87++ n ; 88const int a = old_rule (in ),b = choose_window (in ); 89if (a == b ) {++ same ;continue ; } 90// a difference is allowed only where the old rule used the MTP chain's rows for DFlash2's candidates 91if (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 ); 94check (differ_other == 0 ,"default path unchanged: no input outside the buggy case changes its window" ); 95check (differ_df2_cost > 0 ,"the buggy case does change (the test sees it)" ); 96 } 97 {// 3. the q rule 98check (mtp_q_applies (true, false, true, false, false,4 ),"MTP drew the window, sampled, probabilistic: q attached" ); 99check (!mtp_q_applies (true, true, true, false, false,4 ),"DFlash2 drew the window: no MTP q (exact-match path)" ); 100check (!mtp_q_applies (true, false, true, true, false,4 ),"a lookup window: point masses, no q" ); 101check (!mtp_q_applies (true, false, true, false, true,4 ),"first window: no q" ); 102check (!mtp_q_applies (true, false, true, false, false,1 ),"one row: no q" ); 103check (!mtp_q_applies (true, false, false, false, false,4 ),"greedy / mode off: no q" ); 104check (!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" ); 107return g_fail == 0 ?0 :1 ; 108}