okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1bb5c7248
main
1// Capacity 262144, live prefill up to the register boundary, and the legacy fallbacks. 2// Compare selected IDs only: unused output padding is not part of the API contract. 3#include "strata/kernels/qsa.hpp" 4#include "strata/kernels/qsa_select.hpp" 5#include <cuda_runtime.h> 6#include <algorithm> 7#include <cstdio> 8#include <cstdlib> 9#include <random> 10#include <vector> 11 12namespace k= strata::kernels; 13namespace { 14void ck (cudaError_t e ) { 15if (e != cudaSuccess ) { 16 std::fprintf (stderr ,"CUDA: %s\n" ,cudaGetErrorString (e )); 17 std::exit (2 ); 18 } 19} 20struct Case {int64_t ctx ,queries ;int mode ; }; 21bool check (Case c ) { 22constexpr int64_t stride = 262144 /4 + 2 ; 23const auto s = k::qsa_real_shapes (); 24const int64_t cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 25 std::vector < int32_t > steps ((size_t )(c .queries * k::kStepCount )); 26 std::vector < float > scores ((size_t )(c .queries * stride ),-999.f ); 27 std::mt19937 rng (742913 ); 28 std::uniform_real_distribution < float > random (0.f ,200.f ); 29for (int64_t i = 0 ;i < c .queries ;++ i ) { 30auto * st = steps .data ()+ i * k::kStepCount ; 31const int64_t pos = c .ctx - c .queries + i ; 32st [k::kStepPos ]= (int32_t )pos ; 33st [k::kStepNKv ]= (int32_t )(pos + 1 ); 34st [k::kStepNBid ]= (int32_t )((pos + 1 ) /s .idx_block ); 35st [k::kStepWidth ]= (int32_t )k::qsa_selection_width (pos + 1 ,s ); 36for (int64_t j = 0 ;j <=st [k::kStepNBid ];++ j ) 37scores [(size_t )(i * stride + j )]= c .mode == 1 ?0.f :c .mode == 2 ?float (j %7 ) :random (rng ); 38if ((pos + 1 ) %s .idx_block ) 39scores [(size_t )(i * stride + st [k::kStepNBid ])]+= 1e9f ; 40 } 41int64_t active = steps [(size_t )((c .queries - 1 )* k::kStepCount + k::kStepNBid )]+ 1 ; 42if (c .mode == 3 )active = 0 ; 43if (c .mode == 4 )active = stride + 1 ; 44float * ds = nullptr ; 45int32_t * dt = nullptr ,* da = nullptr ,* db = nullptr ; 46ck (cudaMalloc (& ds ,scores .size ()* sizeof (float ))); 47ck (cudaMalloc (& dt ,steps .size ()* sizeof (int32_t ))); 48ck (cudaMalloc (& da , (size_t )(c .queries * cap )* sizeof (int32_t ))); 49ck (cudaMalloc (& db , (size_t )(c .queries * cap )* sizeof (int32_t ))); 50ck (cudaMemcpy (ds ,scores .data (),scores .size ()* sizeof (float ),cudaMemcpyHostToDevice )); 51ck (cudaMemcpy (dt ,steps .data (),steps .size ()* sizeof (int32_t ),cudaMemcpyHostToDevice )); 52// No bound: the exact capacity-based dispatch used by captured decode. 53 k::qsa_block_topk (ds ,dt ,c .queries ,stride ,cap ,s ,da ,nullptr ); 54 k::qsa_block_topk (ds ,dt ,c .queries ,stride ,cap ,s ,db ,nullptr ,active ); 55ck (cudaDeviceSynchronize ()); 56 std::vector < int32_t > a ((size_t )(c .queries * cap )),b (a .size ()); 57ck (cudaMemcpy (a .data (),da ,a .size ()* sizeof (int32_t ),cudaMemcpyDeviceToHost )); 58ck (cudaMemcpy (b .data (),db ,b .size ()* sizeof (int32_t ),cudaMemcpyDeviceToHost )); 59bool ok = true; 60for (int64_t i = 0 ;i < c .queries ;++ i ) { 61const int64_t w = steps [(size_t )(i * k::kStepCount + k::kStepWidth )]; 62if (!std::equal (a .begin ()+ i * cap ,a .begin ()+ i * cap + w ,b .begin ()+ i * cap )) { 63 std::fprintf (stderr ,"FAIL ctx=%lld queries=%lld mode=%d row=%lld\n" , 64 (long long )c .ctx , (long long )c .queries ,c .mode , (long long )i ); 65ok = false; 66break ; 67 } 68 } 69ck (cudaFree (ds ));ck (cudaFree (dt ));ck (cudaFree (da ));ck (cudaFree (db )); 70return ok ; 71} 72} 73 74int main () { 75int devices = 0 ; 76if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { 77 std::puts ("SKIP: no CUDA device" ); 78return 77 ; 79 } 80// the active-bound dispatch is Turing's in the engine; forced here so every CUDA card checks it 81#if defined(_WIN32 ) 82_putenv_s ("STRATA_TOPK_ACTIVE_ANY" ,"1" ); 83#else 84setenv ("STRATA_TOPK_ACTIVE_ANY" ,"1" ,1 ); 85#endif 86const Case cases []= { 87 {1111 ,256 ,0 }, {8192 ,256 ,0 }, {32768 ,256 ,0 }, {131072 ,256 ,0 }, 88 {131075 ,255 ,0 }, {135164 ,256 ,0 }, {135168 ,256 ,0 }, {250022 ,256 ,0 }, 89 {262144 ,256 ,0 }, {131072 ,1 ,0 }, {131072 ,8 ,0 }, {131072 ,257 ,0 }, 90 {131072 ,256 ,1 }, {131072 ,256 ,2 }, {131072 ,256 ,3 }, {131072 ,256 ,4 }, 91// either side of the Turing wide-kernel bound (22,528 blocks), and ties above the register limit 92 {90108 ,256 ,0 }, {90112 ,256 ,0 }, {200000 ,256 ,2 }, {262144 ,256 ,1 }, 93 }; 94for (const auto c :cases )if (!check (c ))return 1 ; 95 std::puts ("PASS: 20 top-k active-bound cases, selected IDs bitwise identical" ); 96return 0 ; 97}