okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b749503db
main
1// ConversationBuffer on the segment pool (STRATA_PARK_SEGPOOL_MIB): a real-sized 2K and 64K K/V image alternating, a pool smaller than 2// the request, and the buffers' bytes with the pool on and off. Host only; the OS side is a fake that counts calls. 3#include "strata/core/conversation_buffer.hpp" 4 5#include <cstdio> 6#include <cstdlib> 7#include <cstring> 8#include <memory> 9#include <vector> 10#ifdef _WIN32 11#include <malloc.h> 12#endif 13 14using namespace strata::core ; 15 16namespace { 17// 2 MiB aligned memory from the C library: MSVC's C runtime has no std::aligned_alloc (its free cannot release it), so _aligned_malloc there 18void * aligned_2m (size_t bytes ) { 19#ifdef _WIN32 20return _aligned_malloc (bytes ,size_t (2 ) <<20 ); 21#else 22return std::aligned_alloc (size_t (2 ) <<20 ,bytes ); 23#endif 24} 25void aligned_free (void * p ) { 26#ifdef _WIN32 27_aligned_free (p ); 28#else 29 std::free (p ); 30#endif 31} 32 33int checks = 0 ; 34void check (bool value ,const char * description ) { 35++ checks ; 36if (!value ) { std::fprintf (stderr ,"FAIL: %s\n" ,description ); std::exit (1 ); } 37} 38constexpr size_t MiB = size_t (1 ) <<20 ; 39 40struct FakeOs final :SegmentPoolOs { 41void * reserve (size_t bytes )override {return aligned_2m (bytes ); } 42void unreserve (void * base ,size_t )override {aligned_free (base ); } 43bool commit (void * ,size_t )override {return true; } 44bool populate (void * ,size_t )override {return true; } 45void release (void * p ,size_t n )override { std::memset (p ,0 ,n ); } 46}; 47 48// One conversation's K/V host image at Qwen3.8-Flash-Next's int8 sizes (13 layers: 12 QSA + the draft layer; two K/V heads of 256): 49// k and v cells x 512 B, the two scale buffers cells x 32 B, the pooled indexer rows (one 128-float row per 128 tokens) 50struct Image { 51 std::vector < ConversationBuffer > buffers ;// 5 per layer 52size_t bytes = 0 ; 53explicit Image (size_t tokens ,uint8_t tag ,bool fill = true) { 54const size_t cells = (tokens + 63 ) /64 * 64 ; 55const size_t sizes [5 ]= {cells * 512 ,cells * 512 ,cells * 32 ,cells * 32 , (tokens /128 + 1 )* 128 * 4 }; 56buffers .resize (13 * 5 ); 57for (size_t layer = 0 ;layer < 13 ;++ layer ) 58for (size_t part = 0 ;part < 5 ;++ part ) { 59ConversationBuffer & b = buffers [layer * 5 + part ]; 60b .resize_for_overwrite (sizes [part ]); 61bytes += sizes [part ]; 62if (fill ) 63b .visit (0 ,b .size (), [& ](uint8_t * p ,size_t n ,size_t at ) { 64 std::memset (p ,int (tag + layer * 5 + part ),n ); 65 (void )at ; 66return true; 67 }); 68 } 69 } 70bool intact (uint8_t tag )const { 71for (size_t i = 0 ;i < buffers .size ();++ i ) { 72bool ok = true; 73buffers [i ].visit (0 ,buffers [i ].size (), [& ](const uint8_t * p ,size_t n ,size_t ) { 74for (size_t k = 0 ;k < n ;k += 997 )ok = ok && p [k ]== uint8_t (tag + i ); 75return ok ; 76 }); 77if (!ok )return false; 78 } 79return true; 80 } 81}; 82} 83 84int main () { 85FakeOs os ; 86// pool off: every segment is a plain allocation (nothing pooled), the sizes are what they were 87 { 88check (segment_pool ()== nullptr ,"no pool is installed" ); 89Image img (61225 ,1 ); 90check (img .intact (1 )&& img .bytes > 700 * MiB && img .bytes < 900 * MiB ,"a 64K image is about 0.83 GB (13 layers)" ); 91 } 92SegmentPool pool (os ,1024 * MiB ); 93check (pool .active ()&& segment_pool_install (& pool ),"a 1 GiB pool is installed" ); 94// a 2K and a 64K conversation alternating: the second 64K image is served entirely from the pool 95 { 96auto a = std::make_unique < Image > (1931 ,10 );// 2K 97const uint64_t after_2k = pool .hits (); 98check (after_2k > 0 && pool .live_requested ()> 0 ,"the 2K image's large buffers come from the pool" ); 99check (pool .live_requested ()< a -> bytes ,"its small buffers (scales, pooled rows) stay on the heap" ); 100auto z = std::make_unique < Image > (61225 ,20 );// new 64K 101check (z -> intact (20 )&& a -> intact (10 ),"both images keep their bytes" ); 102const size_t live_both = pool .live_requested (); 103const uint64_t misses_first = pool .misses (); 104z .reset ();// the 64K image goes (evicted): its ranges are free and warm 105check (pool .live_requested ()< live_both && pool .idle_bytes ()> 0 ,"an evicted image's bytes go back to the pool" ); 106auto w = std::make_unique < Image > (1931 ,30 );// another 2K 107check (w -> intact (30 ),"a 2K image takes some of the freed ranges" ); 108auto z2 = std::make_unique < Image > (61225 ,40 );// the 64K conversation again 109check (z2 -> intact (40 )&& a -> intact (10 )&& w -> intact (30 ),"the second 64K image and the 2K images are intact" ); 110check (pool .misses ()== misses_first ,"the second 64K image took every large buffer from the pool: no new miss" ); 111z2 .reset ();w .reset ();a .reset (); 112check (pool .live_requested ()== 0 ,"everything is given back" ); 113 } 114// growth by appending (a continued conversation) and rewind 115 { 116ConversationBuffer b ; 117b .resize (3 * MiB ,7 ); 118const size_t live = pool .live_requested (); 119b .resize (3 * MiB + 5 ,9 );// within the segment's spare capacity or a new small segment 120 std::vector < uint8_t > tail (5 ); 121check (b .read (tail .data (),3 * MiB ,5 )&& tail [0 ]== 9 && tail [4 ]== 9 ,"append keeps working on a pooled buffer" ); 122b .resize (1 * MiB ); 123check (b .size ()== 1 * MiB && pool .live_requested () >=live ,"a shortened buffer keeps its segment" ); 124ConversationBuffer c = b ;// a copy is its own storage 125check (c == b ,"a copy equals its source" ); 126ConversationBuffer d = std::move (b ); 127check (d .size ()== 1 * MiB && b .size ()== 0 ,"a move leaves the source empty" ); 128 } 129check (pool .live_requested ()== 0 ,"no pooled byte is left after the buffers are gone" ); 130segment_pool_uninstall_for_test (); 131// a pool smaller than the request: the pool takes what it can, the rest is the heap, and every byte is right 132 { 133SegmentPool small (os ,256 * MiB ); 134segment_pool_install (& small ); 135 { 136Image big (61225 ,50 ); 137check (big .intact (50 ),"a 64K image over a 256 MiB pool keeps its bytes" ); 138check (small .hits ()> 0 && small .misses ()> 0 ,"the pool served part of it and missed the rest" ); 139check (small .live_requested () <=256 * MiB ,"the pool never holds more than its size" ); 140Image other (8000 ,60 ); 141check (other .intact (60 )&& big .intact (50 ),"an 8K image beside it, both intact" ); 142 } 143check (small .live_requested ()== 0 ,"all pooled bytes given back" ); 144segment_pool_uninstall_for_test (); 145 } 146 std::printf ("conversation_buffer_pool_test: %d checks passed\n" ,checks ); 147return 0 ; 148}