okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
28e8d0e05
main
1// #533: the segmented expert cache (--vram-elastic). A shrink gives the last segments' VRAM back to the driver and 2// keeps every slot left exactly as it was (same address, same bytes); a grow maps them again at the same addresses. 3// Uniform and sized slots. Needs a CUDA device with virtual memory management (exits 77 without one). 4#include "strata/core/expert_cache.hpp" 5#include "strata/core/expert_cache_refuse.hpp" 6#include "strata/core/residency_trim.hpp" 7 8#include <cuda_runtime.h> 9 10#include <cstdio> 11#include <string> 12#include <utility> 13#include <vector> 14 15namespace { 16int fail (const char * what ,const std::string & err = "" ) { 17 std::fprintf (stderr ,"FAIL %s %s\n" ,what ,err .c_str ()); 18return 1 ; 19} 20 21size_t free_vram () { 22size_t f = 0 ,t = 0 ; 23cudaMemGetInfo (& f ,& t ); 24return f ; 25} 26 27// slot i holds the byte (i * 7 + 1) everywhere 28bool fill (strata::core::ExpertCache & c ,int64_t from ,int64_t to ,int64_t blob ) { 29 std::vector < uint8_t > b ((size_t )blob ); 30for (int64_t i = from ;i < to ;++ i ) { 31 std::fill (b .begin (),b .end (), (uint8_t ) (i * 7 + 1 )); 32if (cudaMemcpy (c .device_slot ((int32_t )i ),b .data (), (size_t )blob ,cudaMemcpyHostToDevice )!= cudaSuccess ) 33return false; 34 } 35return true; 36} 37 38bool check (const strata::core::ExpertCache & c ,int64_t to ,int64_t blob ) { 39 std::vector < uint8_t > b ((size_t )blob ); 40for (int64_t i = 0 ;i < to ;++ i ) { 41if (cudaMemcpy (b .data (),c .device_slot ((int32_t )i ), (size_t )blob ,cudaMemcpyDeviceToHost )!= cudaSuccess ) 42return false; 43for (const uint8_t x :b ) 44if (x != (uint8_t ) (i * 7 + 1 ))return false; 45 } 46return true; 47} 48}// namespace 49 50int main () { 51int n = 0 ; 52if (cudaGetDeviceCount (& n )!= cudaSuccess || n == 0 ) { 53 std::puts ("no CUDA device: skipped" ); 54return 77 ; 55 } 56constexpr int64_t kBlob = 1 <<20 ,kSlots = 96 ,kSeg = 8ll <<20 ; 57for (int sized = 0 ;sized < 2 ;++ sized ) { 58 strata::core::ExpertCache c ; 59c .set_segment_bytes (kSeg ); 60 std::string err ; 61const bool ok = sized ?c .open_sized (std::vector < int64_t > (kSlots ,kBlob ),4 ,64 ,err ) 62 :c .open (kSlots ,4 ,64 ,kBlob ,err ); 63if (!ok ) { 64// #1475: a HIP build has no segmented cache ("--vram-elastic ... is CUDA-only for now"): a skip, not a failure 65if (err .find ("virtual memory management" )!= std::string::npos || 66err .find ("CUDA-only" )!= std::string::npos ) { 67 std::printf ("%s: skipped\n" ,err .c_str ()); 68return 77 ; 69 } 70return fail ("open" ,err ); 71 } 72if (!c .segmented ()|| c .slots ()!= kSlots || c .mapped_bytes ()< kSlots * kBlob )return fail ("open sizes" ); 73uint8_t * const first = c .device_slot (0 ); 74if (!fill (c ,0 ,kSlots ,kBlob ))return fail ("fill" ); 75const size_t before = free_vram (); 76// keep 30 MiB: rounded up to whole 8 MiB segments = 32 MiB = 32 slots 77if (!c .shrink (30ll <<20 ,err ))return fail ("shrink" ,err ); 78if (c .slots ()!= 32 || c .mapped_bytes ()!= 32ll <<20 || c .bytes ()!= 32ll <<20 ) 79return fail ("shrunk sizes" ); 80if (c .full_slots ()!= kSlots || c .full_bytes ()!= kSlots * kBlob )return fail ("full sizes" ); 81const size_t freed = free_vram ()- before ; 82if (freed + (8u <<20 )< (size_t ) (64ll <<20 )) {// ~64 MiB back to the driver (WDDM rounds a little) 83 std::fprintf (stderr ,"freed %zu bytes\n" ,freed ); 84return fail ("VRAM not given back" ); 85 } 86if (c .device_slot (0 )!= first || !check (c ,32 ,kBlob ))return fail ("slots kept after the shrink" ); 87// grow to 70 MiB: whole segments only, 64 MiB = 64 slots 88if (!c .grow (70ll <<20 ,err )|| c .slots ()!= 64 )return fail ("grow part" ,err ); 89if (!c .grow (1ll <<40 ,err )|| c .slots ()!= kSlots || c .mapped_bytes ()< kSlots * kBlob ) 90return fail ("grow all" ,err ); 91if (c .device_slot (0 )!= first || !check (c ,32 ,kBlob ))return fail ("slots kept after the grow" ); 92if (!fill (c ,32 ,kSlots ,kBlob )|| !check (c ,kSlots ,kBlob ))return fail ("the grown slots" ); 93if (c .slots_within (0 )!= 0 || c .slots_within (kBlob * 3 + 5 )!= 3 || c .bytes_of (5 )!= 5 * kBlob ) 94return fail ("slots_within / bytes_of" ); 95c .close (); 96if (c .valid ()|| c .slots ()!= 0 )return fail ("close" ); 97 } 98// a shrink the driver refuses part way (strata/core/expert_cache_refuse.hpp): what was given back before the refusal 99// is gone, the rest keeps its bytes, the residency entries past it are taken (strata/core/residency_trim.hpp), and a 100// grow maps everything again. 12 segments of 8 MiB; a shrink to 30 MiB walks segments 11 down to 4. 101 { 102 strata::core::ExpertCache c ; 103c .set_segment_bytes (kSeg ); 104 std::string err ; 105if (!c .open (kSlots ,4 ,64 ,kBlob ,err )|| !fill (c ,0 ,kSlots ,kBlob ))return fail ("refusal: open" ,err ); 106 std::vector < int32_t > res ((size_t )kSlots ); 107for (int64_t i = 0 ;i < kSlots ;++ i )res [(size_t )i ]= (int32_t )i ;// expert i in slot i 108// (a) the release of segment 9 refused after its unmap: segments 11 and 10 are gone, 9 is unmapped, 72 slots stay 109 strata::core::ec_test::refuse_segment_call = [](const char * call ,int64_t seg ) { 110return seg == 9 && std::string (call )== "release" ; 111 }; 112const bool ok_a = c .shrink (30ll <<20 ,err ); 113 strata::core::ec_test::refuse_segment_call = nullptr ; 114if (ok_a || err .empty ())return fail ("refusal (a): the shrink reported no error" ); 115if (c .slots ()!= 72 || c .mapped_bytes ()!= 72ll <<20 ) { 116 std::fprintf (stderr ,"slots %lld mapped %lld\n" , (long long )c .slots (), (long long )c .mapped_bytes ()); 117return fail ("refusal (a): an unmapped segment is still counted" ); 118 } 119if (!check (c ,72 ,kBlob ))return fail ("refusal (a): the slots kept" ); 120 std::vector < std::pair < int32_t ,int32_t >> ev ; 121if (strata::core::evict_slots_past (res ,c .slots (),ev )!= 24 || ev .size ()!= 24 || ev .front ()!= std::make_pair (72 ,72 )|| 122res [71 ]!= 71 || res [72 ]!= strata::core::kNotResident || res [95 ]!= strata::core::kNotResident ) 123return fail ("refusal (a): the residency entries past the mapped slots" ); 124// (b) the unmap of segment 6 refused: 8 and 7 go, 6 stays mapped, 56 slots stay 125 strata::core::ec_test::refuse_segment_call = [](const char * call ,int64_t seg ) { 126return seg == 6 && std::string (call )== "unmap" ; 127 }; 128const bool ok_b = c .shrink (30ll <<20 ,err ); 129 strata::core::ec_test::refuse_segment_call = nullptr ; 130if (ok_b || c .slots ()!= 56 || c .mapped_bytes ()!= 56ll <<20 || !check (c ,56 ,kBlob )) 131return fail ("refusal (b): a segment the driver kept mapped" ); 132if (strata::core::evict_slots_past (res ,c .slots (),ev )!= 16 || ev .size ()!= 40 || res [55 ]!= 55 || 133res [56 ]!= strata::core::kNotResident ) 134return fail ("refusal (b): the residency entries past the mapped slots" ); 135// a grow maps all of it again, segment 9's kept handle included, and every slot holds what is written to it 136if (!c .grow (1ll <<40 ,err )|| c .slots ()!= kSlots || c .mapped_bytes ()< kSlots * kBlob )return fail ("refusal: grow" ,err ); 137if (!check (c ,56 ,kBlob )|| !fill (c ,56 ,kSlots ,kBlob )|| !check (c ,kSlots ,kBlob ))return fail ("refusal: the grown slots" ); 138// a clean shrink after it, then close 139if (!c .shrink (30ll <<20 ,err )|| c .slots ()!= 32 )return fail ("refusal: a later shrink" ,err ); 140c .close (); 141if (c .valid ())return fail ("refusal: close" ); 142 } 143// the default (no segment size): one allocation, shrink refused 144 strata::core::ExpertCache d ; 145 std::string err ; 146if (!d .open (8 ,2 ,4 ,kBlob ,err )|| d .segmented ()|| d .shrink (0 ,err ))return fail ("unsegmented" ,err ); 147 std::puts ("expert cache segments: shrink gives VRAM back and keeps the slots, grow maps them again, a refused shrink " 148"counts only what stays mapped" ); 149return 0 ; 150}