okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
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#define DPCT_PROFILING_ENABLED 4#include <sycl/sycl.hpp> 5#include <dpct/dpct.hpp> 6#include "strata/kernels/qsa.hpp" 7#include "strata/kernels/qsa_select.hpp" 8#include <algorithm> 9#include <cstdio> 10#include <cstdlib> 11#include <random> 12#include <vector> 13 14namespace k= strata::kernels; 15namespace { 16void ck (dpct::err0 e ) { 17} 18struct Case {int64_t ctx ,queries ;int mode ; }; 19bool check (Case c ) { 20constexpr int64_t stride = 262144 /4 + 2 ; 21const auto s = k::qsa_real_shapes (); 22const int64_t cap = k::qsa_selection_width (k::kTopkMaxCells ,s ); 23 std::vector < int32_t > steps ((size_t )(c .queries * k::kStepCount )); 24 std::vector < float > scores ((size_t )(c .queries * stride ),-999.f ); 25 std::mt19937 rng (742913 ); 26 std::uniform_real_distribution < float > random (0.f ,200.f ); 27for (int64_t i = 0 ;i < c .queries ;++ i ) { 28auto * st = steps .data ()+ i * k::kStepCount ; 29const int64_t pos = c .ctx - c .queries + i ; 30st [k::kStepPos ]= (int32_t )pos ; 31st [k::kStepNKv ]= (int32_t )(pos + 1 ); 32st [k::kStepNBid ]= (int32_t )((pos + 1 ) /s .idx_block ); 33st [k::kStepWidth ]= (int32_t )k::qsa_selection_width (pos + 1 ,s ); 34for (int64_t j = 0 ;j <=st [k::kStepNBid ];++ j ) 35scores [(size_t )(i * stride + j )]= c .mode == 1 ?0.f :c .mode == 2 ?float (j %7 ) :random (rng ); 36if ((pos + 1 ) %s .idx_block ) 37scores [(size_t )(i * stride + st [k::kStepNBid ])]+= 1e9f ; 38 } 39int64_t active = steps [(size_t )((c .queries - 1 )* k::kStepCount + k::kStepNBid )]+ 1 ; 40if (c .mode == 3 )active = 0 ; 41if (c .mode == 4 )active = stride + 1 ; 42float * ds = nullptr ; 43int32_t * dt = nullptr ,* da = nullptr ,* db = nullptr ; 44ck (DPCT_CHECK_ERROR (ds = sycl::malloc_device < float > ( 45scores .size (), dpct::get_in_order_queue ()))); 46ck (DPCT_CHECK_ERROR (dt = sycl::malloc_device < int32_t > ( 47steps .size (), dpct::get_in_order_queue ()))); 48ck (DPCT_CHECK_ERROR ( 49da = sycl::malloc_device < int32_t > ((size_t )(c .queries * cap ), 50 dpct::get_in_order_queue ()))); 51ck (DPCT_CHECK_ERROR ( 52db = sycl::malloc_device < int32_t > ((size_t )(c .queries * cap ), 53 dpct::get_in_order_queue ()))); 54/* 55DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 56in the original code the source host memory is pageable memory. If the 57memory is not pageable, call wait() on event return by memcpy API to ensure 58synchronization behavior. 59*/ 60ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 61ds ,scores .data (),scores .size ()* sizeof (float )).wait ())); 62/* 63DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 64in the original code the source host memory is pageable memory. If the 65memory is not pageable, call wait() on event return by memcpy API to ensure 66synchronization behavior. 67*/ 68ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 69dt ,steps .data (),steps .size ()* sizeof (int32_t )).wait ())); 70// No bound: the exact capacity-based dispatch used by captured decode. 71 k::qsa_block_topk (ds ,dt ,c .queries ,stride ,cap ,s ,da ,nullptr ); 72 k::qsa_block_topk (ds ,dt ,c .queries ,stride ,cap ,s ,db ,nullptr ,active ); 73ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ())); 74 std::vector < int32_t > a ((size_t )(c .queries * cap )),b (a .size ()); 75ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 76 .memcpy (a .data (),da ,a .size ()* sizeof (int32_t )) 77 .wait ())); 78ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 79 .memcpy (b .data (),db ,b .size ()* sizeof (int32_t )) 80 .wait ())); 81bool ok = true; 82for (int64_t i = 0 ;i < c .queries ;++ i ) { 83const int64_t w = steps [(size_t )(i * k::kStepCount + k::kStepWidth )]; 84if (!std::equal (a .begin ()+ i * cap ,a .begin ()+ i * cap + w ,b .begin ()+ i * cap )) { 85 std::fprintf (stderr ,"FAIL ctx=%lld queries=%lld mode=%d row=%lld\n" , 86 (long long )c .ctx , (long long )c .queries ,c .mode , (long long )i ); 87ok = false; 88break ; 89 } 90 } 91ck (DPCT_CHECK_ERROR (sycl::free (ds , dpct::get_in_order_queue ()))); 92ck (DPCT_CHECK_ERROR (sycl::free (dt , dpct::get_in_order_queue ()))); 93ck (DPCT_CHECK_ERROR (sycl::free (da , dpct::get_in_order_queue ()))); 94ck (DPCT_CHECK_ERROR (sycl::free (db , dpct::get_in_order_queue ()))); 95return ok ; 96} 97} 98 99int main ()try { 100int devices = 0 ; 101if (DPCT_CHECK_ERROR (devices = dpct::device_count ())!= 0 || devices == 0 ) { 102 std::puts ("SKIP: no CUDA device" ); 103return 77 ; 104 } 105// the active-bound dispatch is Turing's in the engine; forced here so every CUDA card checks it 106#if defined(_WIN32 ) 107_putenv_s ("STRATA_TOPK_ACTIVE_ANY" ,"1" ); 108#else 109setenv ("STRATA_TOPK_ACTIVE_ANY" ,"1" ,1 ); 110#endif 111const Case cases []= { 112 {1111 ,256 ,0 }, {8192 ,256 ,0 }, {32768 ,256 ,0 }, {131072 ,256 ,0 }, 113 {131075 ,255 ,0 }, {135164 ,256 ,0 }, {135168 ,256 ,0 }, {250022 ,256 ,0 }, 114 {262144 ,256 ,0 }, {131072 ,1 ,0 }, {131072 ,8 ,0 }, {131072 ,257 ,0 }, 115 {131072 ,256 ,1 }, {131072 ,256 ,2 }, {131072 ,256 ,3 }, {131072 ,256 ,4 }, 116// either side of the Turing wide-kernel bound (22,528 blocks), and ties above the register limit 117 {90108 ,256 ,0 }, {90112 ,256 ,0 }, {200000 ,256 ,2 }, {262144 ,256 ,1 }, 118 }; 119for (const auto c :cases )if (!check (c ))return 1 ; 120 std::puts ("PASS: 20 top-k active-bound cases, selected IDs bitwise identical" ); 121return 0 ; 122} 123catch (sycl::exception const & exc ) { 124 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 125 <<", line:" <<__LINE__ << std::endl ; 126 std::exit (1 ); 127}