okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9c059bade
main
1// src/prefill/kva_test.cpp - the KVA switch, the automatic tail and the reasons a read is turned away (no GPU needed). 2#include "strata/kernels/window_start.hpp" 3#include "strata/prefill/kva.hpp" 4#include "strata/prefill/kva_floor.hpp" 5 6#include <cstdio> 7#include <cstdlib> 8#include <cstring> 9#include <string> 10#include <vector> 11 12using namespace strata::prefill ; 13 14static int fails = 0 ; 15#define CHECK (c ) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0) 16 17static void unset (const char * k ) { 18#if defined(_WIN32 ) 19_putenv_s (k ,"" ); 20#else 21unsetenv (k ); 22#endif 23} 24static void put (const char * k ,const char * v ) { 25#if defined(_WIN32 ) 26_putenv_s (k ,v ); 27#else 28setenv (k ,v ,1 ); 29#endif 30} 31static void clear_all () { 32for (const char * k : {"STRATA_KVA" ,"STRATA_KVA_SPLIT" ,"STRATA_KVA_SUFFIX" ,"STRATA_KVA_MIN_PROMPT" ,"STRATA_KVA_PARITY" , 33"STRATA_KVA_CAPTURE" ,"STRATA_KVA_CAPTURE_FP32" ,"STRATA_KVA_ORACLE" ,"STRATA_KVA_ORACLE_FP32" , 34"STRATA_KVA_PROJECTOR" ,"STRATA_KVA_MODE" ,"STRATA_KVA_ONLY" ,"STRATA_KVA_DRAFT_FLOOR" ,"STRATA_KVA_SKIP" ,"STRATA_KVA_ANY_MODEL" }) 35unset (k ); 36} 37static bool is (const char * got ,const char * want ) {return got != nullptr && std::strcmp (got ,want )== 0 ; } 38 39int main () { 40// ---- the switch: off unless STRATA_KVA=1 (0.2.7 ships KVA opt-in; unset is the exact 0.2.6 engine) 41clear_all (); 42 { 43const KvaConfig c = kva_config_from_env (); 44CHECK (!c .on && !c .explicit_on );// unset: off 45CHECK (c .split == 36 && c .min_prompt == 4096 && c .suffix_env == 0 && kKvaSplitDefault == 36 ); 46CHECK (c .mode == KvaConfig ::Mode ::Identity && c .only == KvaConfig ::Only ::All && !c .capture ); 47KvaEngine eng ; 48CHECK (!c .wants_cell_floor (eng , true)); 49CHECK (is (c .decline (eng ,0 ,30000 ,0 ,-1 ,48 ),"off (STRATA_KVA is not 1)" ));// a 30K cold prompt is read exactly 50 } 51put ("STRATA_KVA" ,"0" ); 52 {const KvaConfig c = kva_config_from_env ();CHECK (!c .on && !c .explicit_on ); } 53put ("STRATA_KVA" ,"1" ); 54 {const KvaConfig c = kva_config_from_env ();CHECK (c .on && c .explicit_on ); } 55put ("STRATA_KVA" ,"" );// an empty value is "not set" 56 {const KvaConfig c = kva_config_from_env ();CHECK (!c .on && !c .explicit_on ); } 57clear_all (); 58put ("STRATA_KVA" ,"1" );// everything below runs with KVA switched on 59 60// ---- switched on: split 36 and a 2048-token tail (the configuration measured), or STRATA_KVA_SPLIT / STRATA_KVA_SUFFIX 61 { 62const KvaConfig c = kva_config_from_env (); 63CHECK (c .on && c .split == 36 && c .effective_suffix ()== kKvaSuffixDefault && kKvaSuffixDefault == 2048 ); 64put ("STRATA_KVA_SPLIT" ,"30" ); 65CHECK (kva_config_from_env ().split == 30 ); 66unset ("STRATA_KVA_SPLIT" ); 67put ("STRATA_KVA_SUFFIX" ,"3000" ); 68const KvaConfig e = kva_config_from_env (); 69CHECK (e .effective_suffix ()== 3000 ); 70unset ("STRATA_KVA_SUFFIX" ); 71 } 72 73// ---- who is turned away, and why (the first reason that holds is the one named) 74 { 75const KvaConfig c = kva_config_from_env (); 76KvaEngine eng ; 77bool routine = false; 78// a cold 14.6K prompt on one device: applies (14600 > 2048 + 64) 79CHECK (c .decline (eng ,0 ,14600 ,0 ,-1 ,48 )== nullptr ); 80CHECK (c .decline (eng ,0 ,14600 ,0 ,48 ,48 )== nullptr );// le == n_layers is the whole trunk too 81// a continuation (turn 2 of a chat, a resumed prompt): routine 82CHECK (is (c .decline (eng ,9000 ,14600 ,0 ,-1 ,48 , false, false,& routine ),"a continuation read (pos0 != 0)" )&& routine ); 83// short prompts and a prompt the tail would swallow: routine 84CHECK (is (c .decline (eng ,0 ,3000 ,0 ,-1 ,48 , false, false,& routine ),"the prompt is shorter than STRATA_KVA_MIN_PROMPT" )&& routine ); 85CHECK (c .decline (eng ,0 ,4096 ,0 ,-1 ,48 )== nullptr );// 4096 is the first prompt that is long enough 86put ("STRATA_KVA_SUFFIX" ,"5000" );// a longer tail swallows a prompt of 5000 and not one of 5065 87 {const KvaConfig s = kva_config_from_env (); 88CHECK (is (s .decline (eng ,0 ,5000 ,0 ,-1 ,48 , false, false,& routine ),"the suffix leaves too few positions to approximate" )&& routine ); 89CHECK (s .decline (eng ,0 ,5065 ,0 ,-1 ,48 )== nullptr ); } 90unset ("STRATA_KVA_SUFFIX" ); 91// not routine: the user may want to know 92CHECK (is (c .decline (eng ,0 ,14600 ,24 ,-1 ,48 , false, false,& routine ),"a layer split (not the whole trunk)" )&& !routine ); 93CHECK (is (c .decline (eng ,0 ,14600 ,0 ,24 ,48 ),"a layer split (not the whole trunk)" )); 94CHECK (is (c .decline (eng ,0 ,14600 ,0 ,-1 ,48 , true),"a prompt with pictures" )); 95CHECK (is (c .decline (eng ,0 ,14600 ,0 ,-1 ,48 , false, true),"the DFlash2 drafter reads the late layers' hidden states" )); 96// the engine rules the whole process out (batch slots, a layer split, a peer device) 97KvaEngine blocked = eng ; 98blocked .blocked = "batch slots (--batch)" ; 99CHECK (is (c .decline (blocked ,0 ,14600 ,0 ,-1 ,48 , false, false,& routine ),"batch slots (--batch)" )&& !routine ); 100// a model whose shape KVA was not measured on (not 48 layers / 512 experts) is turned away, not warned about; 101// the routine reasons (a short prompt, a continuation) still come first 102CHECK (is (c .decline (eng ,0 ,14600 ,0 ,-1 ,40 , false, false,& routine , true),kKvaShapeReason )&& !routine ); 103CHECK (is (c .decline (eng ,0 ,3000 ,0 ,-1 ,40 , false, false,& routine , true),"the prompt is shorter than STRATA_KVA_MIN_PROMPT" )&& routine ); 104CHECK (c .decline (eng ,0 ,14600 ,0 ,-1 ,48 , false, false,& routine , false)== nullptr );// the measured shape applies 105put ("STRATA_KVA_ANY_MODEL" ,"1" );// the research override allows it (prefill.cpp then warns once) 106 {const KvaConfig a = kva_config_from_env ();CHECK (a .any_model && a .decline (eng ,0 ,14600 ,0 ,-1 ,40 , false, false,nullptr , true)== nullptr ); } 107unset ("STRATA_KVA_ANY_MODEL" ); 108CHECK (!kva_config_from_env ().any_model ); 109// capture wins over the rest 110put ("STRATA_KVA_CAPTURE" ,"/tmp/kva-cap" ); 111 {const KvaConfig o = kva_config_from_env ();CHECK (o .capture && is (o .decline (eng ,0 ,14600 ,0 ,-1 ,48 ),"capture mode" )); } 112unset ("STRATA_KVA_CAPTURE" ); 113 } 114clear_all (); 115 116// ---- the research modes still parse 117put ("STRATA_KVA_ORACLE" ,"/tmp/o" ); 118put ("STRATA_KVA_ORACLE_FP32" ,"1" ); 119put ("STRATA_KVA_ONLY" ,"qsa" ); 120put ("STRATA_KVA_SPLIT" ,"30" ); 121put ("STRATA_KVA_MIN_PROMPT" ,"1024" ); 122 { 123const KvaConfig c = kva_config_from_env (); 124CHECK (c .mode == KvaConfig ::Mode ::Oracle && c .oracle_fp32 && c .oracle_dir == "/tmp/o" ); 125CHECK (c .only == KvaConfig ::Only ::Qsa && c .split == 30 && c .min_prompt == 1024 ); 126 } 127clear_all (); 128 129// ---- STRATA_KVA_SKIP: the default is the state-only form; "moe" runs the attention halves and skips the MoE half 130 { 131CHECK (kva_config_from_env ().skip == KvaConfig ::Skip ::StateOnly ); 132put ("STRATA_KVA_SKIP" ,"moe" ); 133CHECK (kva_config_from_env ().skip == KvaConfig ::Skip ::Moe ); 134put ("STRATA_KVA_SKIP" ,"state" ); 135CHECK (kva_config_from_env ().skip == KvaConfig ::Skip ::StateOnly ); 136put ("STRATA_KVA_SKIP" ,"bogus" ); 137CHECK (kva_config_from_env ().skip == KvaConfig ::Skip ::StateOnly ); 138unset ("STRATA_KVA_SKIP" ); 139 } 140// ---- "not applied => identical to STRATA_KVA=0", the parts that need no GPU 141// (1) by default the draft layer is 0.2.6's own code; the cell floor is a research option, wired only when asked 142 { 143KvaEngine eng ; 144put ("STRATA_KVA" ,"1" ); 145const KvaConfig on = kva_config_from_env (); 146CHECK (on .on && !on .draft_floor && !on .wants_cell_floor (eng , true)); 147put ("STRATA_KVA_DRAFT_FLOOR" ,"1" ); 148const KvaConfig fl = kva_config_from_env (); 149CHECK (fl .draft_floor && fl .wants_cell_floor (eng , true)); 150CHECK (!fl .wants_cell_floor (eng , false));// no draft layer: nothing to wire 151KvaEngine blocked ; 152blocked .blocked = "batch slots (--batch)" ; 153CHECK (!fl .wants_cell_floor (blocked , true));// ruled out for the process: 0.2.6's own attention code 154put ("STRATA_KVA" ,"0" ); 155const KvaConfig off = kva_config_from_env (); 156CHECK (!off .wants_cell_floor (eng , true));// STRATA_KVA=0: untouched 157clear_all (); 158 } 159// (2) with a floor of 0 the window is exactly the plain window, for every length and every window 160 { 161using strata::kernels::window_start ; 162bool same = true; 163for (int window : {1 ,2 ,64 ,2048 ,32768 ,0x7fffffff }) 164for (int n_kv : {0 ,1 ,2 ,63 ,64 ,65 ,2047 ,2048 ,2049 ,4096 ,14000 ,32768 ,32769 ,100000 }) { 165const int plain = n_kv > window ?n_kv - window :0 ;// what window_ids always selected 166if (window_start (n_kv ,window ,0 )!= plain )same = false; 167 } 168CHECK (same ); 169CHECK (window_start (14000 ,32768 ,11264 )== 11264 );// a floor inside the window raises the start 170CHECK (window_start (14000 ,2048 ,11264 )== 11952 );// a floor below the window changes nothing 171CHECK (window_start (14000 ,32768 ,20000 )== 13999 );// a floor past the end keeps one cell 172CHECK (window_start (14000 ,32768 ,14000 )== 13999 ); 173CHECK (window_start (1 ,32768 ,5 )== 0 ); 174CHECK (window_start (0 ,32768 ,5 )== 0 ); 175 } 176// (3) the floor follows the session's tokens: a state no KVA read produced has floor 0 177 { 178KvaFloorRegistry reg ; 179 std::vector < int64_t > doc ; 180for (int i = 0 ;i < 20000 ;++ i )doc .push_back (1000 + (i * 7919 ) %5000 ); 181CHECK (reg .floor_for (doc .data (), (int64_t )doc .size ())== 0 );// nothing remembered 182reg .note (doc .data (),11264 );// a KVA read approximated [0, 11264) 183CHECK (reg .floor_for (doc .data (),20000 )== 11264 );// the live session / a later turn of it 184CHECK (reg .floor_for (doc .data (),5000 )== 5000 );// a checkpoint inside the approximated part 185 std::vector < int64_t > other (doc ); 186for (int i = 0 ;i < 20000 ;++ i )other [(size_t )i ]+= 1 ;// a different prompt entirely 187CHECK (reg .floor_for (other .data (),20000 )== 0 );// short chats, other documents: floor 0 188 std::vector < int64_t > shared (doc .begin (),doc .begin ()+ 3000 );// a new chat sharing the first 3000 tokens 189for (int i = 3000 ;i < 9000 ;++ i )shared .push_back (900000 + i ); 190CHECK (reg .floor_for (shared .data (),9000 )== 3000 ); 191reg .note (doc .data (),11264 );// the same read again: no second entry 192CHECK (reg .size ()== 1 ); 193KvaFloorRegistry small (2 ); 194for (int k = 0 ;k < 5 ;++ k ) { std::vector < int64_t > d (100 ,k );small .note (d .data (),100 ); } 195CHECK (small .size ()== 2 );// bounded 196reg .clear (); 197CHECK (reg .floor_for (doc .data (),20000 )== 0 ); 198 } 199 200if (fails == 0 ) std::printf ("kva_test: ok\n" ); 201return fails == 0 ?0 :1 ; 202}