okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
43b105a75
main
1#include "strata/program/split_ring.hpp" 2 3#include <cstdint> 4#include <cstdio> 5#include <vector> 6 7using namespace strata::program ; 8 9#define CHECK (cond ) \ 10 do { \ 11 if (!(cond)) { \ 12 std::printf("split_ring_test FAIL line %d: %s\n", __LINE__, #cond); \ 13 return 1; \ 14 } \ 15 ++checks; \ 16 } while (0) 17 18// a cache of `slots` slots whose blobs vary per layer like a native pack's (2.4 .. 3.6 MB), as the prefix sum the 19// expert cache keeps 20static std::vector < uint64_t > make_offsets (int64_t slots ) { 21 std::vector < uint64_t > off ((size_t )slots + 1 ,0 ); 22for (int64_t i = 0 ;i < slots ;++ i )off [(size_t )i + 1 ]= off [(size_t )i ]+ 2400000ull + (uint64_t ) (i %13 )* 100000ull ; 23return off ; 24} 25 26int main () { 27int checks = 0 ; 28 29// 1. the ring the split chooses: the env wins, else 96 when most experts are resident, 0 = the pinned-share rule 30CHECK (split_ring_slots (nullptr ,0.9 )== 96 ); 31CHECK (split_ring_slots (nullptr ,0.75 )== 96 ); 32CHECK (split_ring_slots (nullptr ,0.74 )== 0 ); 33CHECK (split_ring_slots ("384" ,0.9 )== 384 ); 34CHECK (split_ring_slots ("384" ,0.1 )== 384 ); 35CHECK (split_ring_slots ("512" ,0.9 )== 512 ); 36CHECK (split_ring_slots ("0" ,0.9 )== 0 ); 37CHECK (split_ring_slots ("-3" ,0.9 )== 0 ); 38CHECK (split_ring_slots ("junk" ,0.9 )== 0 ); 39 40// 2. the lend count: uniform slots round up, sized slots walk from the end 41CHECK (split_lend_slots (1000 ,1000 * 100 ,nullptr ,100 ,0 )== 0 ); 42CHECK (split_lend_slots (1000 ,1000 * 100 ,nullptr ,100 ,1 )== 1 ); 43CHECK (split_lend_slots (1000 ,1000 * 100 ,nullptr ,100 ,100 )== 1 ); 44CHECK (split_lend_slots (1000 ,1000 * 100 ,nullptr ,100 ,101 )== 2 ); 45CHECK (split_lend_slots (1000 ,0 ,nullptr ,0 ,500 )== 0 );// no blob size: nothing to count 46 { 47const std::vector < uint64_t > off = make_offsets (50 ); 48const int64_t bytes = (int64_t )off .back (); 49CHECK (split_lend_slots (50 ,bytes ,off .data (),3600000 ,0 )== 0 ); 50const uint64_t last = off [50 ]- off [49 ]; 51CHECK (split_lend_slots (50 ,bytes ,off .data (),3600000 ,last )== 1 ); 52CHECK (split_lend_slots (50 ,bytes ,off .data (),3600000 ,last + 1 )== 2 ); 53CHECK (split_lend_slots (50 ,bytes ,off .data (),3600000 ,off [50 ])== 50 ); 54CHECK (split_lend_slots (50 ,bytes ,off .data (),3600000 ,off [50 ]+ 1 )== 50 );// never past the cache 55 } 56 57// 3. the order: the RAM copy's lend region and the borrow must be sized with the SAME ring. The need of a chunk is 58// its own buffers plus ring_slots * max_blob (Prefill::bytes_needed); the engine used to size the region with the 59// default ring (96) and apply the STRATA_SPLIT_RING override afterwards, so a ring of 384 borrowed more slots 60// than the region kept. 61 { 62const int64_t slots = 6000 ; 63const std::vector < uint64_t > off = make_offsets (slots ); 64const int64_t bytes = (int64_t )off .back (); 65const uint64_t max_blob = 3600000 ,buffers = 3000ull <<20 ; 66auto need = [& ](int ring ) {return buffers + (uint64_t )ring * max_blob ; }; 67auto lend = [& ](int ring ) {return split_lend_slots (slots ,bytes ,off .data (),max_blob ,need (ring )); }; 68 69const int default_ring = 96 ; 70const int ring = split_ring_slots ("384" ,0.9 );// STRATA_SPLIT_RING=384 71const int64_t borrowed = lend (ring );// what the prompt path borrows with the override in force 72const int64_t kept_old_order = lend (default_ring );// the region sized before the override applied 73const int64_t kept_new_order = lend (split_ring_slots ("384" ,0.9 ));// decided first, then sized 74CHECK (kept_old_order < borrowed );// the bug: borrowed experts without a RAM copy 75CHECK (kept_new_order == borrowed );// the fix: the copy keeps every borrowed slot 76// no override in play (production: STRATA_SPLIT_RING unset, ring 96 either way): nothing changes 77CHECK (lend (split_ring_slots (nullptr ,0.5 )> 0 ?split_ring_slots (nullptr ,0.5 ) :default_ring )== 78kept_old_order ); 79// a bigger ring borrows monotonically more 80CHECK (lend (96 ) <=lend (256 )&& lend (256 ) <=lend (384 )&& lend (384 ) <=lend (512 )); 81 } 82 83 std::printf ("split_ring_test OK (%d checks)\n" ,checks ); 84}