okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// GPU contract test for the optional gfx1100 port of PR1825. Synthetic, no model or coding score. 2#include "strata/kernels/qsa.hpp" 3#include "strata/kernels/qsa_select.hpp" 4#include "strata/kernels/gfx_arch.hpp" 5#include <cuda_runtime.h> 6#include <algorithm> 7#include <cmath> 8#include <cstdint> 9#include <cstdio> 10#include <cstdlib> 11#include <cstring> 12#include <limits> 13#include <random> 14#include <vector> 15 16namespace strata::kernels { 17// Test-only host wrapper copied from the unchanged baseline; both arms receive the SAME active bound. 18void qsa_block_scores_reference (const float * ,const float * ,const float * ,const int32_t * ,int64_t , 19int64_t ,const QsaShapes & ,float * ,void * ,int64_t ); 20} 21namespace k= strata::kernels; 22namespace { 23void ck (cudaError_t e ) { 24if (e != cudaSuccess ) { std::fprintf (stderr ,"HIP: %s\n" ,cudaGetErrorString (e )); std::exit (2 ); } 25} 26void require (bool ok ,const char * why ) { 27if (!ok ) { std::fprintf (stderr ,"FAIL: %s\n" ,why ); std::exit (1 ); } 28} 29struct Buffer { 30void * p = nullptr ;size_t bytes ; 31explicit Buffer (size_t n ):bytes (n ) {ck (cudaMalloc (& p ,n + 64 ));ck (cudaMemset (p ,0xd3 ,n + 64 )); } 32 ~Buffer () {ck (cudaFree (p )); } 33template < typename T > T * as () {return static_cast < T *> (p ); } 34void guard () { 35unsigned char v [64 ]; 36ck (cudaMemcpy (v ,static_cast < char *> (p )+ bytes ,64 ,cudaMemcpyDeviceToHost )); 37for (auto c :v )require (c == 0xd3 ,"allocation guard modified" ); 38 } 39}; 40struct Case {int64_t context ,queries ,capacity ;int mode ;int bound ;bool timing ; }; 41int cases_done = 0 ,engaged = 0 ; 42uint64_t scores_checked = 0 ,ids_checked = 0 ; 43 44void check (Case c ) { 45const auto s = k::qsa_real_shapes (); 46const int64_t nb = c .capacity /s .idx_block + 2 ,nq = c .queries ; 47const int64_t active = c .context /s .idx_block + 1 ; 48const int64_t bound = c .bound == 0 ?active :c .bound == 1 ?0 :c .bound == 2 ?-1 :nb + 1 ; 49const int64_t cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 50 std::mt19937 rng (91731 + (unsigned )c .context + 7 * (unsigned )nq + 101 * c .mode ); 51 std::normal_distribution < float > nd (0 ,1 ); 52 std::uniform_int_distribution < int > expo (-20 ,20 ),small (-3 ,3 ); 53uint64_t sequence = 0 ; 54auto value = [& ]()-> float { 55++ sequence ; 56if (c .mode == 1 )return sequence %11 == 0 ?0.f : std::ldexp (nd (rng ),expo (rng )); 57if (c .mode == 2 )return (float )small (rng ); 58if (c .mode == 3 )return sequence %2 ?0.f :-0.f ; 59if (c .mode == 4 ) { 60if (sequence %23 == 0 )return std::numeric_limits < float > ::quiet_NaN (); 61if (sequence %29 == 0 )return std::numeric_limits < float > ::infinity (); 62if (sequence %31 == 0 )return - std::numeric_limits < float > ::infinity (); 63 } 64if (c .mode == 5 )return sequence %4 < 2 ? std::ldexp (1.f ,20 ) :- std::ldexp (1.f ,20 ); 65return nd (rng ); 66 }; 67 std::vector < float > pooled (nb * s .idx_dim ),dead (s .idx_dim ),q (nq * s .idx_n_head * s .idx_dim ); 68for (auto & v :pooled )v = value (); 69for (auto & v :dead )v = value (); 70for (auto & v :q )v = value (); 71// Padding after the largest live block must never affect scores or selected IDs. 72for (int64_t i = active * s .idx_dim ;i < (int64_t )pooled .size ();++ i ) 73pooled [i ]= std::numeric_limits < float > ::quiet_NaN (); 74 std::vector < int32_t > steps (nq * k::kStepCount ); 75for (int64_t i = 0 ;i < nq ;++ i ) { 76auto * st = steps .data ()+ i * k::kStepCount ; 77const int64_t n = c .context - nq + i + 1 ; 78st [k::kStepPos ]= n - 1 ;st [k::kStepNKv ]= n ; 79st [k::kStepNBid ]= n /s .idx_block ; 80st [k::kStepWidth ]= k::qsa_selection_width (n ,s ); 81 } 82Buffer p (pooled .size ()* 4 ),d (dead .size ()* 4 ),query (q .size ()* 4 ),step (steps .size ()* 4 ); 83Buffer a (nq * nb * 4 ),b (nq * nb * 4 ),ia (nq * cap * 4 ),ib (nq * cap * 4 ); 84ck (cudaMemcpy (p .p ,pooled .data (),p .bytes ,cudaMemcpyHostToDevice )); 85ck (cudaMemcpy (d .p ,dead .data (),d .bytes ,cudaMemcpyHostToDevice )); 86ck (cudaMemcpy (query .p ,q .data (),query .bytes ,cudaMemcpyHostToDevice )); 87ck (cudaMemcpy (step .p ,steps .data (),step .bytes ,cudaMemcpyHostToDevice )); 88auto reference = [& ] { 89 k::qsa_block_scores_reference (p .as < float > (),d .as < float > (),query .as < float > (),step .as < int32_t > (), 90nq ,nb ,s ,a .as < float > (),nullptr ,bound ); 91 }; 92auto candidate = [& ] { 93 k::qsa_block_scores (p .as < float > (),d .as < float > (),query .as < float > (),step .as < int32_t > (), 94nq ,nb ,s ,b .as < float > (),nullptr ,bound ); 95 }; 96ck (cudaMemset (a .p ,0xa5 ,a .bytes ));ck (cudaMemset (b .p ,0x5a ,b .bytes )); 97reference ();candidate ();ck (cudaDeviceSynchronize ()); 98 std::vector < uint32_t > av (nq * nb ),bv (nq * nb ); 99ck (cudaMemcpy (av .data (),a .p ,a .bytes ,cudaMemcpyDeviceToHost )); 100ck (cudaMemcpy (bv .data (),b .p ,b .bytes ,cudaMemcpyDeviceToHost )); 101uint64_t checked = 0 ; 102for (int64_t i = 0 ;i < nq ;++ i ) { 103const int64_t last = steps [i * k::kStepCount + k::kStepNBid ]; 104for (int64_t j = 0 ;j < nb ;++ j ) { 105const auto at = i * nb + j ; 106if (j <=last ) { 107if (av [at ]!= bv [at ]) { 108 std::fprintf (stderr ,"MISMATCH ctx=%lld nq=%lld mode=%d query=%lld block=%lld bits=%08x/%08x\n" , 109 (long long )c .context , (long long )nq ,c .mode , (long long )i , (long long )j ,av [at ],bv [at ]); 110require (false,"scores differ bitwise" ); 111 } 112++ checked ; 113 }else { 114require (av [at ]== 0xa5a5a5a5 && bv [at ]== 0x5a5a5a5a ,"inactive score padding overwritten" ); 115 } 116 } 117 } 118ck (cudaMemset (ia .p ,0xff ,ia .bytes ));ck (cudaMemset (ib .p ,0xff ,ib .bytes )); 119 k::qsa_block_topk (a .as < float > (),step .as < int32_t > (),nq ,nb ,cap ,s ,ia .as < int32_t > (),nullptr ,bound ); 120 k::qsa_block_topk (b .as < float > (),step .as < int32_t > (),nq ,nb ,cap ,s ,ib .as < int32_t > (),nullptr ,bound ); 121ck (cudaDeviceSynchronize ()); 122 std::vector < int32_t > iav (nq * cap ),ibv (nq * cap ); 123ck (cudaMemcpy (iav .data (),ia .p ,ia .bytes ,cudaMemcpyDeviceToHost )); 124ck (cudaMemcpy (ibv .data (),ib .p ,ib .bytes ,cudaMemcpyDeviceToHost )); 125require (iav == ibv ,"selected IDs differ" ); 126for (int64_t i = 0 ;i < nq ;++ i ) 127for (int64_t j = steps [i * k::kStepCount + k::kStepWidth ];j < cap ;++ j ) 128require (iav [i * cap + j ]== -1 ,"ID padding overwritten" ); 129for (auto * v : {& p ,& d ,& query ,& step ,& a ,& b ,& ia ,& ib })v -> guard (); 130const bool branch = std::getenv ("STRATA_SCORES_THREAD" )&& 131 std::atoi (std::getenv ("STRATA_SCORES_THREAD" ))== 1 && bound >=16385 && bound <=nb && nq >=64 ; 132 std::printf ("PASS ctx=%lld nq=%lld capacity=%lld mode=%d bound=%lld expected_thread=%d scores=%llu ids=%lld\n" , 133 (long long )c .context , (long long )nq , (long long )c .capacity ,c .mode , (long long )bound , 134 (int )branch , (unsigned long long )checked , (long long )(nq * cap )); 135++ cases_done ;engaged += branch ;scores_checked += checked ;ids_checked += nq * cap ; 136if (c .timing ) { 137// Same actual bound/capacity, warmed, interleaved ABBA. Events measure GPU execution, not host fixture setup. 138for (int i = 0 ;i < 3 ;++ i ) {reference ();candidate (); } 139ck (cudaDeviceSynchronize ()); 140cudaEvent_t e0 ,e1 ;ck (cudaEventCreate (& e0 ));ck (cudaEventCreate (& e1 )); 141float times [2 ][4 ]{}; 142for (int batch = 0 ;batch < 4 ;++ batch ) { 143const int order [4 ]= {0 ,1 ,1 ,0 }; 144for (int v :order ) { 145ck (cudaEventRecord (e0 )); 146for (int rep = 0 ;rep < 5 ;++ rep ) {if (v == 0 )reference ();else candidate (); } 147ck (cudaEventRecord (e1 ));ck (cudaEventSynchronize (e1 )); 148float ms = 0 ;ck (cudaEventElapsedTime (& ms ,e0 ,e1 ));times [v ][batch ]+= ms /10 ; 149 } 150 } 151float ta = 0 ,tb = 0 ; 152for (int i = 0 ;i < 4 ;++ i ) {ta += times [0 ][i ]/4 ;tb += times [1 ][i ]/4 ; } 153 std::printf ("TIMING ctx=%lld nq=%lld baseline_ms=%.6f candidate_ms=%.6f speedup=%.6f trials=4 repeats_per_trial=10\n" , 154 (long long )c .context , (long long )nq ,ta ,tb ,ta /tb ); 155ck (cudaEventDestroy (e0 ));ck (cudaEventDestroy (e1 )); 156 } 157} 158}// namespace 159 160int main (int argc ,char ** argv ) { 161int devices = 0 ; 162if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { std::puts ("SKIP no GPU" );return 77 ; } 163cudaDeviceProp properties {};ck (cudaGetDeviceProperties (& properties ,0 )); 164if (!k::gfx_arch_is (properties .gcnArchName ,"gfx1100" )&& !k::gfx_arch_is_gfx12 (properties .gcnArchName )) { std::puts ("SKIP requires gfx1100 or gfx12" );return 77 ; } 165require (std::getenv ("STRATA_SCORES_THREAD" )&& std::strcmp (std::getenv ("STRATA_SCORES_THREAD" ),"1" )== 0 , 166"run with STRATA_SCORES_THREAD=1 so the candidate branch is tested" ); 167const bool full = argc > 1 && std::strcmp (argv [1 ],"--full" )== 0 ; 168for (int64_t n : {1 ,6 ,8 ,9 ,63 ,64 ,65 ,127 ,128 ,129 ,256 }) 169check ({131075 ,n ,262144 ,0 ,0 ,false}); 170for (int64_t context : {65535 ,65536 ,65537 ,65538 ,65539 ,131072 ,262144 }) 171check ({context ,65 ,262144 ,0 ,0 ,false}); 172for (int bound : {1 ,2 ,3 })check ({131075 ,65 ,262144 ,0 ,bound ,false}); 173for (int mode : {1 ,2 ,3 ,4 ,5 })check ({131075 ,129 ,262144 ,mode ,0 ,false}); 174check ({131075 ,65 ,131075 ,0 ,0 ,false}); 175if (full ) { 176for (int64_t context : {131072 ,131073 ,131074 ,131075 })check ({context ,256 ,262144 ,2 ,0 ,false}); 177check ({131072 ,256 ,262144 ,0 ,0 ,true}); 178check ({240000 ,256 ,262144 ,0 ,0 ,true}); 179check ({140000 ,1024 ,262144 ,0 ,0 ,true}); 180check ({240000 ,1024 ,262144 ,0 ,0 ,true}); 181 } 182 std::printf ("QUALIFIED cases=%d expected_thread_cases=%d scores=%llu ids=%llu bitwise_scores=true bitwise_ids=true guards=true\n" , 183cases_done ,engaged , (unsigned long long )scores_checked , (unsigned long long )ids_checked ); 184return 0 ; 185}