okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Strata. ExpertStore (src/dsv41/ds4_store.hpp) on real expert files, no device: the bytes a slot holds equal the file's, the 2// unbuffered reads' rate, the policy's hits on a skewed random access stream. 3// ds4_store_test <gguf> <layer0 gate off> <layer0 up off> <layer0 down off> <expert bytes (gate/up/down)> [budget GB] [policy 0|1|2] [accesses] 4// (offsets from the GGUF's tensor directory: tools/dsv41 header scan; the three tensors of a layer are [384][...] so expert e of 5// a matrix lies at off + e * bytes) 6#include "ds4_store.hpp" 7 8#include <chrono> 9#include <cstdio> 10#include <cstdlib> 11#include <random> 12#include <thread> 13 14using namespace strata::ds4 ; 15 16int main (int argc ,char ** argv ) { 17if (argc < 6 ) { 18 std::printf ("usage: ds4_store_test <gguf> <gate off> <up off> <down off> <matrix bytes> [budget GB] [policy] [accesses]\n" ); 19return 2 ; 20 } 21const std::string path = argv [1 ]; 22const uint64_t offs [3 ]= {std::strtoull (argv [2 ],nullptr ,10 ), std::strtoull (argv [3 ],nullptr ,10 ), std::strtoull (argv [4 ],nullptr ,10 )}; 23const uint64_t mb = std::strtoull (argv [5 ],nullptr ,10 ); 24const double gb = argc > 6 ? std::atof (argv [6 ]) :1.0 ; 25const int policy = argc > 7 ? std::atoi (argv [7 ]) :0 ; 26const int n_acc = argc > 8 ? std::atoi (argv [8 ]) :2000 ; 27const os::fd_t fd = os::open_ro (path ),dfd = os::open_direct (path ),pfd = os::open_par (path ); 28if (fd == os::kBadFd || pfd == os::kBadFd ) { 29 std::printf ("cannot open %s\n" ,path .c_str ()); 30return 2 ; 31 } 32 std::printf ("direct handle: %s\n" ,dfd != os::kBadFd ?"yes" :"NO (buffered)" ); 33ExpertStore ::LayerGeo geo ; 34geo .n_exp = 384 ; 35uint64_t at = 0 ; 36for (int r = 0 ;r < 3 ;++ r ) { 37geo .file [r ]= 0 ; 38geo .off [r ]= offs [r ]; 39geo .stride [r ]= mb ; 40geo .bytes [r ]= mb ; 41geo .tr [r ]= at ;// the engine's blob layout with the store's 8192-byte gaps 42at += ((mb + 255 )& ~(uint64_t )255 )+ ExpertStore ::kSlack ; 43 } 44geo .blob = at ; 45ExpertStore st ; 46ExpertStore ::Cfg cfg ; 47cfg .budget_bytes = (size_t ) (gb * 1e9 ); 48cfg .threads = std::getenv ("STORE_TEST_THREADS" ) ? std::atoi (std::getenv ("STORE_TEST_THREADS" )) :8 ; 49cfg .policy = (ExpertStore ::Policy )policy ; 50cfg .floor_bytes = 4ull <<30 ; 51cfg .ram_avail = [] {return os::ram_avail (); }; 52if (!st .init (cfg , {geo }, {dfd }, {pfd })) { 53 std::printf ("init failed\n" ); 54return 2 ; 55 } 56 std::printf ("slot %.2f MB, up to %zu slots\n" ,st .slot_bytes () /1e6 ,st .slots_max ()); 57// a skewed stream: 70% of the accesses from the 64 hottest experts, the rest uniform; 6 a "token" 58 std::mt19937_64 rng (7 ); 59auto pick = [& ] {return rng () %100 < 70 ? (int ) (rng () %64 ) : (int ) (rng () %384 ); }; 60 std::vector < uint8_t > ref (mb ); 61int bad = 0 ; 62long checked = 0 ; 63const auto t0 = std::chrono::steady_clock::now (); 64for (int a = 0 ;a < n_acc ;++ a ) { 65int es [6 ]; 66for (int i = 0 ;i < 6 ;++ i ) { 67es [i ]= pick (); 68for (int j = 0 ;j < i ;++ j ) 69if (es [j ]== es [i ])es [i ]= (es [i ]+ 1 + j ) %384 ; 70 } 71 std::vector < char > ok ; 72st .ensure (0 ,es ,6 ,ok ); 73if (a %17 == 0 && !std::getenv ("STORE_TEST_NOCMP" ))// a sample compared with the file read plainly 74for (int i = 0 ;i < 6 ;++ i ) 75if (ok [(size_t )i ]) 76for (int r = 0 ;r < 3 ;++ r ) { 77const int s = st .find (0 ,es [i ]); 78 os::pread_full (fd ,ref .data (),mb ,offs [r ]+ (uint64_t )es [i ]* mb ); 79++ checked ; 80if (s < 0 || std::memcmp (st .mat (s ,r ),ref .data (),mb )!= 0 )++ bad ; 81 } 82st .release (0 ,es ,6 ,ok ); 83 } 84// guesses queued behind demand, cancelled, and a shrink: the store must still give the file's bytes 85for (int round = 0 ;round < 3 ;++ round ) { 86for (int e = 100 ;e < 140 ;++ e )st .prefetch (0 ,e ); 87st .cancel_stale (0 ); 88int es [6 ]= {5 + round ,17 ,200 ,201 ,202 ,250 + round }; 89 std::vector < char > ok ; 90st .ensure (0 ,es ,6 ,ok ); 91for (int i = 0 ;i < 6 ;++ i ) 92if (ok [(size_t )i ]) { 93const int sl = st .find (0 ,es [i ]); 94for (int r = 0 ;r < 3 ;++ r ) { 95 os::pread_full (fd ,ref .data (),mb ,offs [r ]+ (uint64_t )es [i ]* mb ); 96++ checked ; 97if (sl < 0 || std::memcmp (st .mat (sl ,r ),ref .data (),mb )!= 0 )++ bad ; 98 } 99 } 100st .release (0 ,es ,6 ,ok ); 101 std::this_thread::sleep_for (std::chrono::milliseconds (800 ));// (the guesses read by now) 102const size_t g = st .shrink_chunks (1 ); 103 std::printf ("round %d: shrank %zu chunk(s), %zu slots left, cancelled %lld\n" ,round ,g ,st .slots_allocated (), (long long )st .stats ().cancelled .load ()); 104 } 105const double dt = std::chrono::duration < double > (std::chrono::steady_clock::now ()- t0 ).count (); 106const auto & s = st .stats (); 107 std::printf ("%d accesses of 6 experts in %.2f s: hits %lld misses %lld evictions %lld fails %lld; read %.2f GB (%.2f GB/s); waited %.2f s; " 108"compared %ld matrices, %d differ\n" , 109n_acc ,dt , (long long )s .hits .load (), (long long )s .misses .load (), (long long )s .evictions .load (), (long long )s .fails .load (), 110s .bytes_read .load () /1e9 ,s .bytes_read .load () /1e9 /dt ,s .wait_ns .load () /1e9 ,checked ,bad ); 111return bad == 0 ?0 :1 ; 112}