okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8aec2625d
main
1// The elastic K/V when the expert cache cannot give it slots (kv_grow_rules.hpp): the pools of a QSA state made elastic 2// at init map only their first cells; the K/V then grows from new VRAM (the take that returns 0, as generate.cpp's 3// kvg_ensure does when the cache has nothing to give) to the cells a long prompt needs and on to the whole context, and 4// every cell it maps is writable and reads back. GPU with virtual memory needed; skips with 77 otherwise. 5#include "strata/core/geometry.hpp" 6#include "strata/core/kv_grow_rules.hpp" 7#include "strata/core/layer.hpp" 8#include "strata/core/layout.hpp" 9#include "strata/core/vmm.hpp" 10 11#include <cuda_runtime.h> 12 13#include <cstdint> 14#include <cstdio> 15#include <string> 16#include <vector> 17 18using namespace strata::core ; 19 20namespace { 21int failed = 0 ; 22#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 23 24// cells [0, cells) of one [page][kv_head][page_size][w] pool are its first pages, contiguous 25size_t pool_bytes (int64_t cells ,int64_t page_size ,int64_t heads ,int64_t w ) { 26return (size_t ) ((cells + page_size - 1 ) /page_size )* (size_t ) (heads * page_size * w ); 27} 28 29bool fill_and_check (const QsaState & st ,const strata::kernels::QsaShapes & s ,int64_t cells ,uint8_t v ) { 30const int64_t scb = (s .head_dim /64 )* 2 ; 31const std::pair < void * ,int64_t > arrays []= {{st .k_q ,s .head_dim }, {st .v_q ,s .head_dim }, {st .k_scale ,scb }, {st .v_scale ,scb }}; 32for (const auto & [p ,w ] :arrays ) { 33const size_t n = pool_bytes (cells ,s .page_size ,s .n_head_kv ,w ); 34if (cudaMemset (p ,v ,n )!= cudaSuccess )return false; 35if (cudaDeviceSynchronize ()!= cudaSuccess )return false; 36 std::vector < uint8_t > h (4096 );// the last bytes: the far end of the mapped cells 37if (cudaMemcpy (h .data (), (const uint8_t * )p + (n - h .size ()),h .size (),cudaMemcpyDeviceToHost )!= cudaSuccess )return false; 38for (const uint8_t b :h ) 39if (b != v )return false; 40 } 41return true; 42} 43}// namespace 44 45int main (int argc ,char ** argv ) { 46if (cudaFree (nullptr )!= cudaSuccess || !vmm_available ()) { 47 std::printf ("kv_grow_refused_test: no CUDA device with virtual memory - skipped\n" ); 48return 77 ; 49 } 50const ModelGeometry g ;// the model's QSA shapes 51const strata::kernels::QsaShapes s = qsa_shapes (g ); 52const int64_t max_cells = 65536 ,init = 4096 ; 53qsa_set_kv_int8 (true); 54qsa_set_kv_elastic (true,init ); 55CHECK (qsa_kv_elastic ()); 56void * base = nullptr ; 57CHECK (cudaMalloc (& base ,qsa_state_bytes (g ,max_cells ))== cudaSuccess ); 58QsaState st ; 59CHECK (qsa_state_init (g ,max_cells ,base ,st )> 0 ); 60CHECK (st .kv_elastic >=0 && st .k_q != nullptr && st .v_scale != nullptr ); 61CHECK (cudaDeviceSynchronize ()== cudaSuccess ); 62const int64_t cells0 = qsa_kv_elastic_cells (); 63 std::printf ("kv_grow_refused_test: first mapping %lld of %lld cells (%.1f of %.1f MiB)\n" , (long long )cells0 , 64 (long long )max_cells , (double )qsa_kv_elastic_mapped_bytes () /1048576.0 , 65 (double )qsa_kv_elastic_full_bytes () /1048576.0 ); 66CHECK (cells0 >=init && cells0 < max_cells ); 67CHECK (fill_and_check (st ,s ,cells0 ,0x11 )); 68 69// the refused state of BLOCK 6: --expert-cache 100 (128 slots, the floor), pools elastic 70const KvGrowStart r = kv_grow_start (qsa_kv_elastic (), true, true, true,128 ,128 ); 71CHECK (r .on && !r .from_cache ); 72// a 20,000-token prompt (kvg_ensure(n + 256)): its cells are not mapped yet, then grown from new VRAM 73const int64_t want = 20000 + 256 ,step = 8192 ; 74const int64_t target = kv_grow_target (want ,step ,max_cells ); 75CHECK (target >=want ); 76CHECK (qsa_kv_elastic_need (target )> 0 ); 77int64_t fresh = 0 ; 78CHECK (qsa_kv_elastic_grow (target , [& ] {++ fresh ;return (VmmChunk )0 ; })); 79CHECK (fresh > 0 && qsa_kv_elastic_need (target )== 0 ); 80const int64_t cells1 = qsa_kv_elastic_cells (); 81CHECK (cells1 >=target ); 82CHECK (fill_and_check (st ,s ,cells1 ,0x22 )); 83// --full-card (not in ctest: it takes every free byte of the card for a moment): with no new VRAM either, the grow 84// reports false (generate.cpp's callers then end the request with their "no VRAM is left" line), the cells mapped 85// so far stay mapped and usable, the device has no sticky error, and the grow succeeds once memory is free again 86bool full_card = false; 87for (int i = 1 ;i < argc ;++ i )full_card = full_card || std::string (argv [i ])== "--full-card" ; 88if (full_card ) { 89 std::vector < void *> hog ; 90for (const size_t block : {(size_t )256 <<20 , (size_t )2 <<20 }) { 91void * p = nullptr ; 92while (cudaMalloc (& p ,block )== cudaSuccess )hog .push_back (p ); 93 (void )cudaGetLastError (); 94 } 95size_t free_b = 0 ,total_b = 0 ; 96 (void )cudaMemGetInfo (& free_b ,& total_b ); 97 std::printf ("kv_grow_refused_test: full card: %zu blocks taken, %.1f MiB free\n" ,hog .size (), (double )free_b /1048576.0 ); 98const bool grew = qsa_kv_elastic_grow (max_cells , [] {return (VmmChunk )0 ; }); 99 (void )cudaGetLastError (); 100 std::printf ("kv_grow_refused_test: full card: the grow to %lld cells returned %s; %lld cells mapped\n" , 101 (long long )max_cells ,grew ?"true" :"false" , (long long )qsa_kv_elastic_cells ()); 102CHECK (!grew ); 103CHECK (qsa_kv_elastic_cells () >=cells1 ); 104CHECK (cudaDeviceSynchronize ()== cudaSuccess ); 105CHECK (fill_and_check (st ,s ,cells1 ,0x44 )); 106for (void * p :hog ) (void )cudaFree (p ); 107 } 108// the whole context: every cell of --max-context can be mapped the same way 109CHECK (qsa_kv_elastic_grow (max_cells , [] {return (VmmChunk )0 ; })); 110CHECK (qsa_kv_elastic_cells () >=max_cells && qsa_kv_elastic_need (max_cells )== 0 ); 111CHECK (fill_and_check (st ,s ,max_cells ,0x33 )); 112 std::printf ("kv_grow_refused_test: grown from new VRAM to %lld then %lld cells (%lld new chunks for the first grow)\n" , 113 (long long )cells1 , (long long )qsa_kv_elastic_cells (), (long long )fresh ); 114CHECK (cudaGetLastError ()== cudaSuccess ); 115if (failed ) { 116 std::fprintf (stderr ,"kv_grow_refused_test: %d failed\n" ,failed ); 117return 1 ; 118 } 119 std::printf ("kv_grow_refused_test: ok\n" ); 120return 0 ; 121}