okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f7257bce7
main
1// CPU-only tests for the NVMe tier of the conversation cache (include/strata/core/conversation_disk.hpp): what it 2// keeps, finds, reads back and drops, and that the RAM tier hands it what it evicts. 3// Built with -DSTRATA_BUILD_CONVERSATION_TESTS=ON; no CUDA, no model. 4#include "strata/core/conversation_disk.hpp" 5 6#include <chrono> 7#include <cstdio> 8#include <cstdlib> 9#include <filesystem> 10#include <fstream> 11 12using namespace strata::core ; 13namespace fs= std::filesystem; 14 15namespace { 16 17int checks = 0 ; 18void check (bool ok ,const char * label ) { 19++ checks ; 20if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,label ); std::exit (1 ); } 21} 22 23std::vector < uint8_t > bytes_of (size_t n ,uint8_t seed ) { 24 std::vector < uint8_t > v (n ); 25for (size_t i = 0 ;i < n ;++ i )v [i ]= uint8_t (seed + i * 7 ); 26return v ; 27} 28 29ConversationBuffer pattern (size_t n ,uint8_t seed ) { 30ConversationBuffer b ; 31b .resize (n ); 32b .visit (0 ,n , [& ](uint8_t * p ,size_t c ,size_t at ) { 33for (size_t i = 0 ;i < c ;++ i )p [i ]= uint8_t (seed + (at + i )* 13 ); 34return true; 35 }); 36return b ; 37} 38 39// a checkpoint over tokens base, base+1, ... (count of them) 40ConversationCheckpoint checkpoint (int32_t base ,size_t count ,uint8_t seed ) { 41ConversationCheckpoint c ; 42for (size_t i = 0 ;i < count ;++ i )c .ids .push_back (base + int32_t (i )); 43c .gdn = bytes_of (4099 ,seed ); 44c .ple = bytes_of (77 ,seed + 1 ); 45c .tails = bytes_of (301 ,seed + 2 ); 46c .dead = bytes_of (64 ,seed + 3 ); 47c .block_pos = bytes_of (8 ,seed + 4 ); 48c .used = 42 + seed ; 49return c ; 50} 51 52// one conversation: tokens base.. of `live` length, checkpoints at each of `at` 53SavedConversation conversation (int32_t base ,size_t live , std::vector < size_t > at ,uint8_t seed ) { 54SavedConversation s ; 55for (size_t i = 0 ;i < s .geometry .size ();++ i )s .geometry [i ]= int64_t (100 + i ); 56s .layer_lo = 0 ;s .layer_hi = 48 ; 57s .live = checkpoint (base ,live ,seed ); 58for (size_t n :at )s .checkpoints .push_back (checkpoint (base ,n ,uint8_t (seed + n ))); 59ConversationKv kv ; 60kv .format = 2 ;kv .cells = 1024 ;kv .heads = 2 ;kv .head_dim = 256 ;kv .page_size = 64 ;kv .pooled_rows = 9 ;kv .idx_dim = 128 ; 61kv .k = pattern (65536 + seed ,seed ); 62kv .v = pattern (4096 ,uint8_t (seed + 10 )); 63kv .k_scale = pattern (512 ,uint8_t (seed + 20 )); 64kv .v_scale = pattern (512 ,uint8_t (seed + 30 )); 65kv .pooled = pattern (9 * 128 * 4 ,uint8_t (seed + 40 )); 66s .kv .push_back (std::move (kv )); 67return s ; 68} 69 70std::vector < int32_t > prompt (int32_t base ,size_t count ) { 71 std::vector < int32_t > p ; 72for (size_t i = 0 ;i < count ;++ i )p .push_back (base + int32_t (i )); 73return p ; 74} 75 76size_t files_in (const fs::path & dir ,const char * ext ) { 77size_t n = 0 ; 78for (const auto & e : fs::directory_iterator (dir ))n += e .path ().extension ()== ext ; 79return n ; 80} 81 82}// namespace 83 84int main () { 85const fs::path dir = fs::temp_directory_path () / ("strata-conv-disk-test-" + std::to_string ( 86 std::chrono::steady_clock::now ().time_since_epoch ().count ())); 87const SessionFileIdentity id {0x1111222233334444ull ,0x5555666677778888ull }; 88 std::string error ; 89 90// two conversations are kept, each found by the prompt that continues it, and read back whole 91 { 92ConversationDisk disk (dir .string (),1ull <<30 ); 93check (disk .load ()== 0 ,"an empty folder holds nothing" ); 94const SavedConversation a = conversation (1000 ,900 , {100 ,600 },1 ); 95const SavedConversation b = conversation (5000 ,400 , {100 },2 ); 96check (disk .put (a ,id ,error )&& disk .put (b ,id ,error ),"both are written" ); 97check (disk .size ()== 2 && files_in (dir ,".kvs" )== 2 && files_in (dir ,".idx" )== 2 ,"a file and an index each" ); 98auto m = disk .best (prompt (1000 ,950 ), {}, true); 99check (m .tokens == 900 && m .live ,"the prompt that continues a is matched to a's live state" ); 100m = disk .best (prompt (1000 ,650 ), {}, true); 101check (m .tokens == 600 && !m .live ,"a shorter prompt is matched to a's checkpoint" ); 102check (disk .best (prompt (9000 ,500 ), {}, true).tokens == 0 ,"an unrelated prompt matches nothing" ); 103check (disk .best (prompt (1000 ,950 ), {}, false).tokens == 0 ,"another control vector matches nothing" ); 104SavedConversation back ; 105m = disk .best (prompt (5000 ,450 ), {}, true); 106check (disk .take (m .index ,id ,back ,error ),"b is read back" ); 107check (back .live .ids == b .live .ids && back .live .gdn == b .live .gdn && back .checkpoints .size ()== 1 && 108back .kv .size ()== 1 && back .kv [0 ].k .size ()== b .kv [0 ].k .size (),"b comes back as it was written" ); 109check (disk .size ()== 1 && files_in (dir ,".kvs" )== 1 && files_in (dir ,".idx" )== 1 ,"a taken entry leaves the disk" ); 110 } 111 112// a restart finds what an earlier run wrote, without reading the states; stray files go 113 { 114 std::ofstream (dir /"conv-000000000099.kvs.tmp" ) <<"half" ; 115 std::ofstream (dir /"conv-000000000098.kvs" ) <<"no index" ; 116ConversationDisk disk (dir .string (),1ull <<30 ); 117check (disk .load ()== 1 ,"the remaining conversation is found again" ); 118check (disk .best (prompt (1000 ,950 ), {}, true).tokens == 900 ,"and matched as before" ); 119check (!fs::exists (dir /"conv-000000000099.kvs.tmp" )&& !fs::exists (dir /"conv-000000000098.kvs" ), 120"a half-written file and a file without its index are removed" ); 121SessionFileIdentity other = id ; 122other .model ^=1 ; 123SavedConversation back ; 124check (!disk .take (0 ,other ,back ,error )&& !error .empty (),"another model's engine cannot read it" ); 125check (disk .size ()== 0 && files_in (dir ,".kvs" )== 0 ,"and a file that does not read is not kept" ); 126 } 127 128// the same conversation a turn later replaces its earlier copy; over budget the oldest goes 129 { 130ConversationDisk disk (dir .string (),1ull <<30 ); 131check (disk .put (conversation (1000 ,900 , {100 ,600 },1 ),id ,error ),"turn 1 is written" ); 132check (disk .put (conversation (1000 ,1400 , {100 ,600 ,1200 },1 ),id ,error ),"turn 2 is written" ); 133check (disk .size ()== 1 && disk .best (prompt (1000 ,1500 ), {}, true).tokens == 1400 ,"turn 2 supersedes turn 1" ); 134disk .clear (); 135check (disk .put (conversation (7000 ,900 , {100 },4 ),id ,error ),"one of the size below is written" ); 136const uint64_t one = disk .bytes (); 137disk .clear (); 138ConversationDisk small (dir .string (),one * 2 + one /2 ); 139check (small .put (conversation (1000 ,900 , {100 },1 ),id ,error )&& 140small .put (conversation (5000 ,900 , {100 },2 ),id ,error )&& 141small .put (conversation (9000 ,900 , {100 },3 ),id ,error ),"three are written" ); 142check (small .size ()== 2 && small .bytes () <=one * 2 + one /2 ,"the budget keeps two" ); 143check (small .best (prompt (1000 ,950 ), {}, true).tokens == 0 && small .best (prompt (9000 ,950 ), {}, true).tokens == 900 , 144"the least recently written left" ); 145check (small .clear ()== 2 && files_in (dir ,".kvs" )== 0 && files_in (dir ,".idx" )== 0 ,"clear removes every file" ); 146 } 147 148// the RAM tier hands each conversation it evicts to the spill, and nothing else 149 { 150ConversationCache ram (1ull <<30 ,1 ); 151 std::vector < std::vector < int32_t >> spilled ; 152ram .set_spill ([& ](const SavedConversation & s ) {spilled .push_back (s .live .ids ); }); 153check (ram .put (conversation (1000 ,900 , {100 },1 )),"a is parked" ); 154check (ram .put (conversation (5000 ,900 , {100 },2 )),"b is parked, evicting a" ); 155check (spilled .size ()== 1 && spilled [0 ]== prompt (1000 ,900 ),"a was spilled" ); 156check (ram .put (conversation (5000 ,1300 , {100 ,900 },2 ))&& spilled .size ()== 1 , 157"b's next turn supersedes b without a spill" ); 158ram .clear (); 159check (spilled .size ()== 1 ,"clear spills nothing" ); 160check (ram .put (conversation (1000 ,900 , {100 },1 ))&& ram .spill_all ()== 1 && spilled .size ()== 2 && ram .size ()== 0 , 161"spill_all hands over every parked conversation" ); 162 } 163 164 fs::remove_all (dir ); 165 std::printf ("conversation_disk_test: %d checks passed\n" ,checks ); 166return 0 ; 167}