okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
main
1// src/kernels/native_multi_parity.cpp - the multi-token router and the k = 10 combine, bitwise against their 2// single-token contracts (#783 PR-c: route_multi, combine_k10_vec4). 3// 4// * native_router_top10_multi (one warp per token, 8 tokens per CTA) against native_router_top10 token by token, 5// for n = 1..19 (a partial last CTA, and more than one CTA), on plain logits, on logits with exact ties and on 6// logits with NaN (the router maps NaN to -FLT_MAX): ids and weights are compared with memcmp; 7// * native_moe_combine / native_moe_combine_multi (k = 10 with a shared row takes the float4 kernel; a misaligned 8// output, k = 9 and a null shared row take the scalar one) against a host replay of the documented contract: 9// the first product rounds to F32, the next ones accumulate with FMA in expert order, the shared row is added 10// once: memcmp. 11#define DPCT_PROFILING_ENABLED 12#include <sycl/sycl.hpp> 13#include <dpct/dpct.hpp> 14#include "strata/kernels/native_moe.hpp" 15#include "strata/kernels/native_router.hpp" 16 17#include <cmath> 18#include <cstdint> 19#include <cstdio> 20#include <cstdlib> 21#include <cstring> 22#include <random> 23#include <string> 24#include <vector> 25 26namespace { 27 28void check (dpct::err0 e ,const char * what ) { 29} 30 31template < typename T > 32T * up (const std::vector < T >& h ) { 33void * p = nullptr ; 34check (DPCT_CHECK_ERROR ( 35p = (void * )sycl::malloc_device (h .size ()* sizeof (T )+ 64 , 36 dpct::get_in_order_queue ())), 37"malloc" ); 38/* 39DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 40in the original code the source host memory is pageable memory. If the 41memory is not pageable, call wait() on event return by memcpy API to ensure 42synchronization behavior. 43*/ 44check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 45p ,h .data (),h .size ()* sizeof (T )).wait ()), 46"h2d" ); 47return (T * )p ; 48} 49template < typename T > 50std::vector < T > down (const T * d ,size_t n ) { 51 std::vector < T > h (n ); 52check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 53 .memcpy (h .data (),d ,n * sizeof (T )) 54 .wait ()), 55"d2h" ); 56return h ; 57} 58 59}// namespace 60 61int main (int argc ,char ** ) { 62 (void )argc ; 63using namespace strata::kernels ; 64 std::mt19937 rng (783 ); 65 std::normal_distribution < float > gauss (0.0f ,1.0f ); 66int bad = 0 ; 67 dpct::queue_ptr st = & dpct::get_in_order_queue (); 68check (DPCT_CHECK_ERROR (st = dpct::get_current_device ().create_queue (true)), 69"stream" ); 70 71// ---- the multi-token router 72for (int mode = 0 ;mode < 3 ;++ mode ) { 73const char * names []= {"random logits" ,"exact ties" ,"NaN logits" }; 74int mode_bad = 0 ,cases = 0 ; 75for (int n = 1 ;n <=19 ;++ n ) { 76 std::vector < float > lg ((size_t )n * 512 ); 77for (auto & v :lg )v = gauss (rng )* 2.0f ; 78if (mode == 1 ) 79for (size_t i = 0 ;i < lg .size ();++ i )lg [i ]= (float ) (int ) (lg [i ]* 2.0f )* 0.5f ;// coarse: many exact ties 80if (mode == 2 ) 81for (int t = 0 ;t < n ;++ t )lg [(size_t )t * 512 + (size_t ) (rng () %512 )]= std::nanf ("" ); 82float * d_l = up (lg ); 83int32_t * d_i ,* d_ir ; 84float * d_w ,* d_wr ; 85check (DPCT_CHECK_ERROR ( 86d_i = (int32_t * )sycl::malloc_device ( 87 (size_t )n * 10 * 4 , dpct::get_in_order_queue ())), 88"i" ); 89check (DPCT_CHECK_ERROR ( 90d_ir = (int32_t * )sycl::malloc_device ( 91 (size_t )n * 10 * 4 , dpct::get_in_order_queue ())), 92"ir" ); 93check (DPCT_CHECK_ERROR ( 94d_w = (float * )sycl::malloc_device ( 95 (size_t )n * 10 * 4 , dpct::get_in_order_queue ())), 96"w" ); 97check (DPCT_CHECK_ERROR ( 98d_wr = (float * )sycl::malloc_device ( 99 (size_t )n * 10 * 4 , dpct::get_in_order_queue ())), 100"wr" ); 101native_router_top10_multi (d_l ,d_i ,d_w ,n ,st ); 102for (int t = 0 ;t < n ;++ t )native_router_top10 (d_l + (size_t )t * 512 ,d_ir + t * 10 ,d_wr + t * 10 ,st ); 103check (DPCT_CHECK_ERROR ( 104 dpct::get_current_device ().queues_wait_and_throw ()), 105"sync" ); 106const auto i1 = down (d_i , (size_t )n * 10 ),i2 = down (d_ir , (size_t )n * 10 ); 107const auto w1 = down (d_w , (size_t )n * 10 ),w2 = down (d_wr , (size_t )n * 10 ); 108++ cases ; 109if (std::memcmp (i1 .data (),i2 .data (),i1 .size ()* 4 )!= 0 || std::memcmp (w1 .data (),w2 .data (),w1 .size ()* 4 )!= 0 ) { 110 std::printf (" *** native_router_top10_multi n=%d (%s) differs from native_router_top10 ***\n" ,n ,names [mode ]); 111++ mode_bad ; 112 } 113 sycl::free (d_l , dpct::get_in_order_queue ()); 114 sycl::free (d_i , dpct::get_in_order_queue ()); 115 sycl::free (d_ir , dpct::get_in_order_queue ()); 116 sycl::free (d_w , dpct::get_in_order_queue ()); 117 sycl::free (d_wr , dpct::get_in_order_queue ()); 118 } 119 std::printf (" %-52s %s (%d cases)\n" , (std::string ("router multi == single, " )+ names [mode ]).c_str (), 120mode_bad ?"*** NO ***" :"bitwise" ,cases ); 121bad += mode_bad ; 122 } 123 124// ---- the combine 125struct Cfg {const char * name ;int k ;bool shared ;int out_off ; }; 126const Cfg cfgs []= {{"k=10 + shared (float4 kernel)" ,10 , true,0 }, {"k=10 + shared, output misaligned" ,10 , true,1 }, 127 {"k=10 without shared" ,10 , false,0 }, {"k=9 + shared" ,9 , true,0 }}; 128const int N = 2048 ; 129for (const Cfg & c :cfgs ) { 130int cfg_bad = 0 ,cases = 0 ; 131for (int n = 1 ;n <=8 ;++ n ) { 132 std::vector < float > parts ((size_t )n * c .k * N ),w ((size_t )n * c .k ),sh ((size_t )n * N ); 133for (auto & v :parts )v = gauss (rng ); 134for (auto & v :w )v = (float ) (rng () %1000 ) /1000.0f ; 135for (auto & v :sh )v = gauss (rng ); 136float * d_p = up (parts ); 137float * d_w = up (w ); 138float * d_s = up (sh ); 139float * d_o = nullptr ; 140check (DPCT_CHECK_ERROR ( 141d_o = (float * )sycl::malloc_device ( 142 (size_t )n * N * 4 + 64 , dpct::get_in_order_queue ())), 143"o" ); 144float * out = d_o + c .out_off ; 145if (c .out_off ) { 146// a misaligned output cannot take a multi launch of rows with the 4-float stride contract either way: 147// the rows are still n*N floats from `out` 148 } 149native_moe_combine_multi (d_p ,d_w ,c .shared ?d_s :nullptr ,out ,N ,c .k ,n ,st ); 150check (DPCT_CHECK_ERROR ( 151 dpct::get_current_device ().queues_wait_and_throw ()), 152"sync" ); 153const auto got = down (out , (size_t )n * N ); 154 std::vector < float > want ((size_t )n * N ); 155for (int t = 0 ;t < n ;++ t ) 156for (int col = 0 ;col < N ;++ col ) { 157float s = parts [((size_t )t * c .k )* N + col ]* w [(size_t )t * c .k ]; 158for (int e = 1 ;e < c .k ;++ e ) 159s = std::fmaf (parts [((size_t )t * c .k + e )* N + col ],w [(size_t )t * c .k + e ],s ); 160if (c .shared )s += sh [(size_t )t * N + col ]; 161want [(size_t )t * N + col ]= s ; 162 } 163++ cases ; 164if (std::memcmp (got .data (),want .data (),got .size ()* 4 )!= 0 ) { 165 std::printf (" *** native_moe_combine_multi n=%d (%s) differs from the contract's host replay ***\n" ,n ,c .name ); 166++ cfg_bad ; 167 } 168// the single-token call is the same arithmetic 169if (n == 1 ) { 170check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 171 .memset (d_o ,0 , (size_t )N * 4 + 64 ) 172 .wait ()), 173"z" ); 174native_moe_combine (d_p ,d_w ,c .shared ?d_s :nullptr ,out ,N ,c .k ,st ); 175check (DPCT_CHECK_ERROR ( 176 dpct::get_current_device ().queues_wait_and_throw ()), 177"sync" ); 178const auto g1 = down (out , (size_t )N ); 179++ cases ; 180if (std::memcmp (g1 .data (),want .data (),g1 .size ()* 4 )!= 0 ) { 181 std::printf (" *** native_moe_combine (%s) differs from the contract's host replay ***\n" ,c .name ); 182++ cfg_bad ; 183 } 184 } 185 sycl::free (d_p , dpct::get_in_order_queue ()); 186 sycl::free (d_w , dpct::get_in_order_queue ()); 187 sycl::free (d_s , dpct::get_in_order_queue ()); 188 sycl::free (d_o , dpct::get_in_order_queue ()); 189 } 190 std::printf (" %-52s %s (%d cases)\n" , (std::string ("combine " )+ c .name ).c_str (),cfg_bad ?"*** NO ***" :"bitwise" ,cases ); 191bad += cfg_bad ; 192 } 193 194 std::printf ("\nnative_multi: %d failures\n" ,bad ); 195if (bad )return 1 ; 196 std::printf ("native_multi_parity OK\n" ); 197return 0 ; 198}