okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
43b105a75
main
1// The resident RAM mode with a layer split: the compact copy holds the experts no stage's cache holds AND each 2// stage's LEND REGION - the experts in the cache slots the prompt path borrows - so a prompt streams the 3// borrowed experts from RAM instead of reading the model files through page faults. Runs on a CPU-only 4// PC: the device calls are wrapped at the link (`--wrap` in CMakeLists.txt, conversation_transfer_test's 5// pattern), the memory is ordinary memory, and `pin = false` keeps the copy pageable. 6#include "strata/core/expert_source.hpp" 7#include "strata/kernels/cpu/expert_layout.hpp" 8 9#include <cuda_runtime.h> 10 11#include <chrono> 12#include <cstdint> 13#include <cstdlib> 14#include <cstring> 15#include <filesystem> 16#include <fstream> 17#include <iostream> 18#include <stdexcept> 19#include <string> 20#include <system_error> 21#include <utility> 22#include <vector> 23 24// ---- the wrapped device calls: memory is ordinary memory and every operation completes at once. 25extern "C" cudaError_t __wrap_cudaMalloc (void ** p ,size_t n ) { 26* p = std::malloc (n ); 27return * p ?cudaSuccess :cudaErrorMemoryAllocation ; 28} 29extern "C" cudaError_t __wrap_cudaFree (void * p ) { std::free (p );return cudaSuccess ; } 30extern "C" cudaError_t __wrap_cudaMemset (void * d ,int v ,size_t n ) { std::memset (d ,v ,n );return cudaSuccess ; } 31extern "C" cudaError_t __wrap_cudaMemcpy (void * d ,const void * s ,size_t n ,cudaMemcpyKind k ) { 32 std::memcpy (d ,s ,n ); 33return cudaSuccess ; 34} 35extern "C" cudaError_t __wrap_cudaMemcpyAsync (void * d ,const void * s ,size_t n ,cudaMemcpyKind k ,cudaStream_t st ) { 36 std::memcpy (d ,s ,n ); 37return cudaSuccess ; 38} 39extern "C" cudaError_t __wrap_cudaStreamSynchronize (cudaStream_t st ) {return cudaSuccess ; } 40extern "C" cudaError_t __wrap_cudaDeviceSynchronize (void ) {return cudaSuccess ; } 41extern "C" cudaError_t __wrap_cudaGetLastError (void ) {return cudaSuccess ; } 42extern "C" cudaError_t __wrap_cudaGetDevice (int * d ) {* d = 0 ;return cudaSuccess ; } 43extern "C" cudaError_t __wrap_cudaSetDevice (int d ) {return cudaSuccess ; } 44extern "C" cudaError_t __wrap_cudaMemGetInfo (size_t * free_b ,size_t * total_b ) { 45* free_b = 64ull <<30 ; 46* total_b = 128ull <<30 ; 47return cudaSuccess ; 48} 49extern "C" cudaError_t __wrap_cudaHostAlloc (void ** p ,size_t n ,unsigned f ) { 50* p = std::malloc (n ); 51return * p ?cudaSuccess :cudaErrorMemoryAllocation ; 52} 53extern "C" cudaError_t __wrap_cudaFreeHost (void * p ) { std::free (p );return cudaSuccess ; } 54 55namespace fs= std::filesystem; 56 57namespace { 58 59constexpr int64_t kLayers = 2 ;// stage 0 owns layer 0, stage 1 owns layer 1 (the split's `claimed` layers) 60constexpr int64_t kExperts = 4 ; 61 62void require (bool ok ,const std::string & message ) { 63if (!ok )throw std::runtime_error (message ); 64} 65 66struct TempDirectory { 67 fs::path path ; 68 69explicit TempDirectory (const fs::path & base = fs::temp_directory_path ()) { 70const auto stamp = std::chrono::steady_clock::now ().time_since_epoch ().count (); 71path = base / ("strata-split-lend-test-" + std::to_string (stamp )); 72 fs::create_directories (path ); 73 } 74 75 ~TempDirectory () { 76 std::error_code ignored ; 77 fs::remove_all (path ,ignored ); 78 } 79}; 80 81const uint8_t truth_byte (int64_t l ,int64_t e ,size_t i ) { 82return (uint8_t ) ((i * 131u + (size_t ) (l * kExperts + e )* 37u ) ^ (i >>8 )); 83} 84 85// one fully-built fixture: experts.bin written, the source open, both stage caches open with their 86// own layer's experts admitted and filled (highest slot = highest expert, as the 87// cache fills from slot 0 up) 88struct Rig { 89 std::vector < std::vector < uint8_t >> truth ;// [layer * kExperts + expert] 90TempDirectory dir ; 91 strata::core::FileExpertSource src ; 92 strata::core::ExpertCache stage0 ,stage1 ;// the two split stages' caches 93size_t blob = 0 ; 94 95explicit Rig (const char * tag ) { 96 std::string err ; 97require (strata::kernels::cpu::expert_layout_load (dir .path .string (),kLayers ,kExperts ,err ),err ); 98blob = (size_t ) strata::kernels::cpu::BLOB ; 99truth .resize ((size_t )kLayers * kExperts ); 100 { 101 std::ofstream out (dir .path /"experts.bin" , std::ios::binary ); 102for (int64_t l = 0 ;l < kLayers ;++ l ) 103for (int64_t e = 0 ;e < kExperts ;++ e ) { 104 std::vector < uint8_t >& t = truth [(size_t )l * kExperts + e ]; 105t .resize (blob ); 106for (size_t i = 0 ;i < blob ;++ i )t [i ]= truth_byte (l ,e ,i ); 107out .write ((const char * )t .data (), (std::streamsize )blob ); 108 } 109require ((bool )out , std::string (tag )+ ": fixture write failed" ); 110 } 111require (src .open (dir .path .string (),kLayers ,kExperts ,err ),err ); 112// kExperts slots each: every layer of the stage, and the tail the prompt path borrows 113require (stage0 .open (kExperts ,kLayers ,kExperts ,blob ,err ),err ); 114require (stage1 .open (kExperts ,kLayers ,kExperts ,blob ,err ),err ); 115for (int64_t e = 0 ;e < kExperts ;++ e ) { 116const int32_t s0 = stage0 .admit (0 ,e ); 117require (s0 >=0 && stage0 .fill_slot_blocking (s0 ,truth [(size_t )e ].data (),err ),err ); 118const int32_t s1 = stage1 .admit (1 ,e ); 119require (s1 >=0 && stage1 .fill_slot_blocking (s1 ,truth [(size_t )kExperts + e ].data (),err ),err ); 120 } 121 } 122 123// every expert of stage 1's cache, the way generate hands the later stages to pin_cache_complement 124 std::vector < std::pair < int32_t ,int32_t >> stage1_pairs ()const { 125 std::vector < std::pair < int32_t ,int32_t >> pairs ; 126for (int64_t e = 0 ;e < kExperts ;++ e )pairs .emplace_back (1 , (int32_t )e ); 127return pairs ; 128 } 129}; 130 131// both stage caches hold everything of their layer: the complement - the experts NO cache 132// holds - is empty, so before this feature the copy kept no bytes at all and every expert a prompt borrowed 133// came back through page faults from the mapped files. 134void test_split_lend_region_served_from_ram () { 135Rig rig ("split lend region" ); 136 std::string err ; 137 138// each stage lends its two highest slots to the prompt path: pairs listed highest slot first, 139// as the borrowing takes them 140rig .src .stage_lend_regions ({{{0 ,3 }, {0 ,2 }}, {{1 ,3 }, {1 ,2 }}}); 141require (rig .src .pin_cache_complement (rig .stage0 ,err , false,rig .stage1_pairs (),-1 ,0 ,0 ),err ); 142 143require (rig .src .resident_bytes ()== 4 * (uint64_t )rig .blob , 144"the copy is the empty complement plus the two stages' lend regions" ); 145require (rig .src .resident_lent_slots ()== 4 ,"the copy reports both stages' lend regions" ); 146for (int64_t l = 0 ;l < kLayers ;++ l ) 147for (int64_t e = 0 ;e < kExperts ;++ e ) { 148const bool in_a_cache = true;// every pair is held by some stage's cache: none is in the complement 149require (rig .src .has_resident (l ,e )== (e >=2 ),"the copy holds exactly the two stages' lent experts" ); 150 } 151 152// the borrowed experts are served from the RAM copy: the lookup resolves into the copy, the bytes match 153// the file's, and not one read touched the file source 154 std::vector < uint8_t > dst (rig .blob ); 155for (int64_t l = 0 ;l < kLayers ;++ l ) 156for (int64_t e = 2 ;e < kExperts ;++ e ) { 157require (rig .src .blob (l ,e )== rig .src .resident_blob (l ,e ),"a lent expert is not served from the copy" ); 158require (rig .src .copy_blob (l ,e ,dst .data ())&& 159dst == rig .truth [(size_t )l * kExperts + e ], 160"the copy serves wrong bytes for a lent expert" ); 161 } 162require (rig .src .file_reads ()== 0 && rig .src .file_read_bytes ()== 0 , 163"the prompt path's borrowed experts came from the file source, not the RAM copy" ); 164} 165 166// a --resident-budget-gib smaller than the regions: the copy keeps the bytes it can - the same budget 167// caps the region walk - and the experts past it keep the file fallback that reads the same bytes 168void test_split_lend_region_budget () { 169Rig rig ("split lend region budget" ); 170 std::string err ; 171 172rig .src .stage_lend_regions ({{{0 ,3 }, {0 ,2 }}, {{1 ,3 }, {1 ,2 }}}); 173const uint64_t budget = 2 * (uint64_t )rig .blob ;// room for one stage's region, not both 174require (rig .src .pin_cache_complement (rig .stage0 ,err , false,rig .stage1_pairs (),-1 ,0 ,budget ),err ); 175 176require (rig .src .resident_bytes ()== budget ,"the budget did not cap the stage lend regions" ); 177require (rig .src .resident_lent_slots ()== 2 ,"the clamped regions kept the wrong count" ); 178require (rig .src .has_resident (0 ,3 )&& rig .src .has_resident (0 ,2 ),"the first stage's region was not kept" ); 179require (!rig .src .has_resident (1 ,3 )&& !rig .src .has_resident (1 ,2 ),"bytes past the budget entered the copy" ); 180 181 std::vector < uint8_t > dst (rig .blob ); 182require (rig .src .copy_blob (0 ,3 ,dst .data ())&& dst == rig .truth [3 ],"wrong bytes from the region inside the cap" ); 183require (rig .src .file_reads ()== 0 ,"an expert the copy holds was read from the file" ); 184require (rig .src .copy_blob (1 ,3 ,dst .data ())&& dst == rig .truth [kExperts + 3 ], 185"the expert past the budget lost its file fallback" ); 186require (rig .src .file_reads ()== 0 && rig .src .file_read_bytes ()== (uint64_t )rig .blob , 187"the expert past the budget was served by the file source, exactly one blob read" ); 188} 189 190// the #848 shape, with no stage regions set: with the later stage's pairs handed over as 191// `additional_gpu_pairs` the copy keeps NOTHING (the complement is empty and the single lend region is off), so 192// this is the state that measured 3.7 GB of NVMe reads over one 73K-token prompt on 2026-10-06 193void test_split_without_regions_keeps_nothing () { 194Rig rig ("split without regions" ); 195 std::string err ; 196 197require (rig .src .pin_cache_complement (rig .stage0 ,err , false,rig .stage1_pairs (),-1 ,0 ,0 ),err ); 198require (rig .src .complement_ready (),"the empty complement did not complete" ); 199require (rig .src .resident_bytes ()== 0 && rig .src .resident_lent_slots ()== 0 , 200"without stage regions the copy should hold no expert at all" ); 201for (int64_t l = 0 ;l < kLayers ;++ l ) 202for (int64_t e = 0 ;e < kExperts ;++ e ) 203require (!rig .src .has_resident (l ,e ),"an expert neither in a cache's complement region entered the copy" ); 204} 205 206}// namespace 207 208int main () { 209try { 210test_split_lend_region_served_from_ram (); 211test_split_lend_region_budget (); 212test_split_without_regions_keeps_nothing (); 213 std::cout <<"split_lend_complement_test: PASS\n" ; 214return 0 ; 215 }catch (const std::exception & error ) { 216 std::cerr <<"split_lend_complement_test: " <<error .what () <<'\n' ; 217return 1 ; 218 } 219}