okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a7cc8faa5
main
1#include "../src/expert_residency.hpp" 2 3#include <cstdio> 4#include <cstdlib> 5#include <random> 6 7using namespace fnx ; 8 9static void check (bool ok ,const char * what ) { 10if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,what ); std::exit (1 ); } 11} 12template < typename F > static void refuses (F f ) { 13try {f (); }catch (const std::runtime_error & ) {return ; } 14check (false,"impossible placement must fail" ); 15} 16 17int main () { 18// A cold one-layer pool must keep its hottest slots, even while a larger pool can still shrink. 19const std::vector < std::pair < size_t ,size_t >> sizes {{3 ,2 }, {4 ,3 }, {3 ,2 }}; 20const std::vector < double > score {10 ,11 ,12 ,13 ,0 ,1 ,2 ,3 ,14 ,15 ,16 ,17 }; 21auto p = expert_residency (sizes ,score ,4 ,2 ,0 ,6 ); 22check (p .resident == std::vector < std::vector < char >> {{0 ,0 ,0 ,0 }, {0 ,0 ,1 ,1 }, {1 ,1 ,1 ,1 }},"rare pool floor and next cold pool" ); 23check (p .count == 6 && p .moved == 34 ,"extra counts only experts actually evicted" ); 24p = expert_residency (sizes ,score ,4 ,2 ,8 ,2 ); 25check (p .count == 4 && p .moved == 24 ,"byte target followed by extra evictions" ); 26p = expert_residency (sizes ,score ,4 ,2 ,0 ,0 ); 27check (p .count == 0 ,"zero request retains all experts" ); 28p = expert_residency (sizes ,score ,4 ,2 ,44 ,0 ); 29check (p .count == 8 && p .moved == 44 ,"exact capacity succeeds" ); 30refuses ([& ] {expert_residency (sizes ,score ,4 ,2 ,45 ,0 ); }); 31refuses ([& ] {expert_residency (sizes ,score ,4 ,2 ,44 ,1 ); }); 32refuses ([& ] {expert_residency (sizes ,score ,4 ,5 ,0 ,0 ); }); 33 34// Equal total bytes do not make equal pools: gate/up and down strides both matter. 35p = expert_residency ({{3 ,2 }, {2 ,3 }}, {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 },4 ,2 ,0 ,4 ); 36check (p .resident == std::vector < std::vector < char >> {{0 ,0 ,1 ,1 }, {0 ,0 ,1 ,1 }},"pool strides and stable ties" ); 37 38// Prod-shaped 43/1/4 layer pools; the old global ordering empties the rare pool at this pressure. 39 std::vector < std::pair < size_t ,size_t >> prod (48 , {1843200 ,1228800 }); 40prod [2 ]= {2252800 ,1740800 }; 41for (int l : {4 ,30 ,46 ,47 })prod [(size_t )l ]= {1843200 ,1740800 }; 42 std::vector < double > ranks (48 * 512 ); 43 std::mt19937 rng (42 ); 44for (double & s :ranks )s = (double )rng () /rng .max (); 45for (int e = 0 ;e < 512 ;++ e )ranks [2 * 512 + e ]= -1 + e /512.0 ; 46p = expert_residency (prod ,ranks ,512 ,320 ,3.0 * (1ull <<30 ),18253 ); 47check (std::count (p .resident [2 ].begin (),p .resident [2 ].end (),1 )== 320 ,"320 slots survive automatic VRAM eviction" ); 48for (int e = 0 ;e < 512 ;++ e )check (p .resident [2 ][(size_t )e ]== (e >=192 ),"rare pool keeps hottest experts" ); 49 50// A large explicit host budget must obey the same pool floor as VRAM-pressure evictions. 51p = expert_residency (prod ,ranks ,512 ,320 ,59.0 * (1ull <<30 ),0 ); 52check (p .moved >=59.0 * (1ull <<30 ),"explicit RAM target reached" ); 53check (std::count (p .resident [2 ].begin (),p .resident [2 ].end (),1 )== 320 ,"explicit RAM target keeps rare pool floor" ); 54 55// Where no floor is hit, placement stays byte-for-byte the former global ranking, including ties. 56for (int run = 0 ;run < 1000 ;++ run ) { 57 std::vector < double > ranks (12 ); 58for (double & s :ranks )s = rng () %5 ; 59 std::vector < int > order (12 ); 60 std::iota (order .begin (),order .end (),0 ); 61 std::stable_sort (order .begin (),order .end (), [& ](int a ,int b ) {return ranks [a ]< ranks [b ]; }); 62p = expert_residency (sizes ,ranks ,4 ,2 ,0 ,2 ); 63for (int i = 0 ;i < 12 ;++ i ) 64check (p .resident [i /4 ][i %4 ]== (i != order [0 ]&& i != order [1 ]),"unconstrained placement unchanged" ); 65 } 66// ---- the GSQ-RCO IQ3_S set, read off its GGUF: 7 pools by (gate+up, down) bytes of one expert. Layer 21 and layer 47 are pools of 512 experts 67// each, layers 12/20/30 one of 1,536. The old global ranking (a copy of the code the planner replaced) takes experts coldest per byte 68// first, so a profile that is cold on a small pool empties it below the window floor; the planner must not. 69 { 70struct PoolDef {size_t gu ,dn ; std::vector < int > layers ; }; 71const std::vector < PoolDef > defs { 72 {1049600 ,921600 , {2 ,3 ,4 ,5 ,6 ,9 ,11 ,15 ,19 ,23 ,24 ,26 ,32 ,34 ,39 }}, 73 {1254400 ,921600 , {0 ,7 ,14 ,16 ,18 ,22 ,31 ,33 ,37 ,38 ,41 ,42 ,43 ,46 }}, 74 {1408000 ,921600 , {17 ,25 ,27 ,28 ,29 ,35 ,40 ,44 ,45 }}, 75 {1049600 ,460800 , {1 ,8 ,10 ,13 ,36 }}, 76 {1254400 ,460800 , {12 ,20 ,30 }}, 77 {1408000 ,460800 , {21 }}, 78 {1740800 ,921600 , {47 }}}; 79 std::vector < std::pair < size_t ,size_t >> iq3 (48 ); 80int n_l = 0 ; 81for (const auto & d :defs ) 82for (int l :d .layers ) {iq3 [(size_t )l ]= {d .gu ,d .dn };++ n_l ; } 83check (n_l == 48 ,"IQ3_S table has 48 layers" ); 84const int floor = 2 * 16 * 10 ;// EXPERT_WINDOW_SLOTS: MAXT rows x TOPK picks, twice 85const double gib1 = (double ) (1ull <<30 ); 86// the engine scores per byte; this profile is cold exactly on the two single-layer pools and the 3-layer pool 87 std::vector < double > sc (48 * 512 ); 88 std::mt19937 r2 (7 ); 89for (size_t i = 0 ;i < sc .size ();++ i ) { 90const size_t l = i /512 ; 91const bool rare = l == 21 || l == 47 || l == 12 || l == 20 || l == 30 ; 92sc [i ]= (rare ?0.0 :0.5 )+ ((double ) (r2 () %1000 ) /1000.0 )* 0.4 ; 93sc [i ] /= (double )iq3 [l ].first + iq3 [l ].second ; 94 } 95auto old_left = [& ](double want ,size_t layer ) {// experts of `layer` still resident after the old ranking moved `want` bytes 96 std::vector < int > order (48 * 512 ); 97 std::iota (order .begin (),order .end (),0 ); 98 std::stable_sort (order .begin (),order .end (), [& ](int a ,int b ) {return sc [(size_t )a ]< sc [(size_t )b ]; }); 99double moved = 0 ; 100int left = 512 ; 101for (int i :order ) { 102if (moved >=want )break ; 103moved += (double )iq3 [(size_t ) (i /512 )].first + iq3 [(size_t ) (i /512 )].second ; 104if ((size_t ) (i /512 )== layer )-- left ; 105 } 106return left ; 107 }; 108int old_failures = 0 ,targets = 0 ; 109for (double g = 2 ;g <=40 ;g += 2 ,++ targets ) { 110auto q = expert_residency (iq3 ,sc ,512 ,floor ,g * gib1 ,0 );// must place every target up to 40 GiB (the set has 46.8 GiB) 111check (q .moved >=g * gib1 ,"IQ3_S: RAM target reached" ); 112for (const auto & d :defs ) { 113int kept = 0 ; 114for (int l :d .layers )kept += (int ) std::count (q .resident [(size_t )l ].begin (),q .resident [(size_t )l ].end (),1 ); 115check (kept >=floor ,"IQ3_S: every size pool keeps a window of resident experts" ); 116 } 117if (old_left (g * gib1 ,21 )< floor )++ old_failures ; 118 } 119check (old_failures > 0 ,"IQ3_S negative control: the old ranking empties the 1-layer pool below the window at some target" ); 120// beyond what the floors allow, the request is refused whole (nothing is uploaded) 121refuses ([& ] {expert_residency (iq3 ,sc ,512 ,floor ,46.0 * gib1 ,0 ); }); 122// an explicit window larger than a pool: refused at once 123refuses ([& ] {expert_residency (iq3 ,sc ,512 ,513 ,0 ,0 ); }); 124 std::printf ("IQ3_S sweep (2..40 GiB in RAM, %d targets): the old ranking leaves layer 21 under %d slots at %d of them, the planner at none\n" , 125targets ,floor ,old_failures ); 126 } 127// injected failure: the card's pool keys are the sliced, 16-byte-padded strides; they partition the layers exactly like the whole-expert bytes 128 { 129const std::vector < std::pair < size_t ,size_t >> whole {{1049600 ,921600 }, {1049600 ,460800 }, {1254400 ,921600 }, {1254400 ,460800 }, {1408000 ,921600 }}; 130for (int cards : {2 ,3 ,4 }) { 131 std::vector < std::pair < size_t ,size_t >> sliced ; 132for (auto & w :whole )sliced .push_back ({(w .first / (size_t )cards + 15 ) /16 * 16 , (w .second / (size_t )cards + 15 ) /16 * 16 }); 133for (size_t a = 0 ;a < whole .size ();++ a ) 134for (size_t b = 0 ;b < whole .size ();++ b ) 135check ((whole [a ]== whole [b ])== (sliced [a ]== sliced [b ]),"pools by whole-expert bytes and by a card's sliced strides are the same partition" ); 136 } 137 } 138 std::puts ("expert_residency_test: PASS" ); 139}