okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
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 ) { 11if (!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 18Controller c ; 19const Choice ch = c .choose (0 ,0 , true); 20check (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 , 22ch .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 25Controller c ; 26Choice ch = c .choose (0 ,0 , true); 27for (int i = 0 ;i < 400 ;++ i ) {c .observe (ch ,0 ,0 );ch = c .choose (0 ,0 , true);if (ch .source == Source ::None )break ; } 28check (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 32Controller c ; 33const Choice ch = c .choose (8 ,20 , true); 34check (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 , 36ch .expected_tokens ,1000 * ch .tokens_per_ms ); 37CostModel cheap ;// a verify pass whose cost barely grows with width 38cheap .dense_ratio = {1.0 ,1.02 ,1.04 ,1.06 ,1.08 ,1.1 ,1.12 ,1.14 ,1.16 }; 39cheap .hit_rate = 0.95 ; 40Controller c2 (cheap ); 41const Choice ch2 = c2 .choose (8 ,20 , true); 42check (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 ), 44ch2 .k ,ch2 .expected_tokens ,1000 * ch2 .tokens_per_ms ); 45 } 46 {// a bare trigram match, no MTP: not worth a verify window 47Controller c ; 48const Choice ch = c .choose (8 ,3 , false); 49check (ch .source == Source ::None ,"short lookup match alone is not used" ); 50 } 51 {// perfect acceptance is learned and widens the window 52Controller c ; 53Choice ch = c .choose (0 ,0 , true); 54const int k0 = ch .k ; 55for (int i = 0 ;i < 400 ;++ i ) {c .observe (ch ,ch .k ,0 );ch = c .choose (0 ,0 , true); } 56check (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" ); 60for (double p : {0.5 ,0.6 ,0.7 ,0.8 ,0.86 ,0.9 ,0.95 }) { 61Controller c ; 62Choice ch = c .choose (0 ,0 , true); 63for (int i = 0 ;i < 2000 ;++ i ) {// feed a stream with iid acceptance p per position 64int acc = 0 ; 65unsigned r = 2654435761u * (unsigned ) (i + 1 ); 66while (acc < ch .k && ((r = r * 1103515245u + 12345u ) >>8 ) %1000 < p * 1000 )++ acc ; 67c .observe (ch ,acc ,0 ); 68ch = 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" ); 73return g_fail ?1 :0 ; 74}