okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
main
1// route_window_parity - the verify window's batched routing against the per-token calls, bit for bit. 2// 3// `moe_route_window` replaces n per-token (projection + top-k) pairs by one multi-column BF16 projection and one 4// n-token top-k. Speculative decoding is exact only if a token's route does not depend on how many tokens share 5// its window, so both batched kernels must equal their one-token forms exactly - this checks that on random data 6// shaped like the router (2560 -> 256, top-10) and like the shared-expert gate (2560 -> 1). 7#define DPCT_PROFILING_ENABLED 8#include <sycl/sycl.hpp> 9#include <dpct/dpct.hpp> 10#include "strata/kernels/bf16_gemv.hpp" 11#include "strata/kernels/router_top10.hpp" 12 13#include <cstdint> 14#include <cstdio> 15#include <cstring> 16#include <random> 17#include <vector> 18 19namespace { 20void ck (dpct::err0 e ,const char * what ) { 21/* 22DPCT1009: SYCL reports errors using exceptions and does not use error 23codes. Please replace the "get_error_string_dummy(...)" with a real 24error-handling function. 25*/ 26} 27uint16_t bf16 (float f ) {uint32_t i ; std::memcpy (& i ,& f ,4 );return (uint16_t ) ((i + ((i >>16 )& 1u )+ 0x7FFFu ) >>16 ); } 28}// namespace 29 30int main () { 31const int N = 2560 ,E = 256 ,K = 10 ; 32int bad = 0 ; 33 std::mt19937 rng (7 ); 34 std::normal_distribution < float > nd (0.0f ,1.0f ); 35for (int n_out : {E ,1 }) { 36for (int T : {2 ,3 ,4 ,6 }) { 37 std::vector < uint16_t > w ((size_t )n_out * N ); 38 std::vector < float > x ((size_t )T * N ); 39for (auto & v :w )v = bf16 (nd (rng )* 0.05f ); 40for (auto & v :x )v = nd (rng ); 41uint16_t * dw ;float * dx ,* dy1 ,* dyn ; 42ck (DPCT_CHECK_ERROR (dw = (uint16_t * )sycl::malloc_device ( 43w .size ()* 2 , dpct::get_in_order_queue ())), 44"w" ); 45ck (DPCT_CHECK_ERROR ( 46dx = (float * )sycl::malloc_device ( 47x .size ()* 4 , dpct::get_in_order_queue ())), 48"x" ); 49ck (DPCT_CHECK_ERROR ( 50dy1 = (float * )sycl::malloc_device ( 51 (size_t )T * n_out * 4 , dpct::get_in_order_queue ())), 52"y1" ); 53ck (DPCT_CHECK_ERROR ( 54dyn = (float * )sycl::malloc_device ( 55 (size_t )T * n_out * 4 , dpct::get_in_order_queue ())), 56"yn" ); 57/* 58DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 59assuming in the original code the source host memory is pageable 60memory. If the memory is not pageable, call wait() on event return 61by memcpy API to ensure synchronization behavior. 62*/ 63ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 64dw ,w .data (),w .size ()* 2 ).wait ()), 65"up w" ); 66/* 67DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 68assuming in the original code the source host memory is pageable 69memory. If the memory is not pageable, call wait() on event return 70by memcpy API to ensure synchronization behavior. 71*/ 72ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 73dx ,x .data (),x .size ()* 4 ).wait ()), 74"up x" ); 75for (int t = 0 ;t < T ;++ t ) 76 strata::kernels::bf16_gemv_fp32_mmvf (dx + (size_t )t * N ,dw ,dy1 + (size_t )t * n_out ,N ,n_out ,nullptr ); 77 strata::kernels::bf16_gemv_fp32_mmvf_cols (dx ,dw ,dyn ,N ,n_out ,T ,nullptr ); 78ck (DPCT_CHECK_ERROR ( 79 dpct::get_current_device ().queues_wait_and_throw ()), 80"sync" ); 81 std::vector < float > y1 ((size_t )T * n_out ),yn ((size_t )T * n_out ); 82ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 83 .memcpy (y1 .data (),dy1 ,y1 .size ()* 4 ) 84 .wait ()), 85"down 1" ); 86ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 87 .memcpy (yn .data (),dyn ,yn .size ()* 4 ) 88 .wait ()), 89"down n" ); 90const bool same = std::memcmp (y1 .data (),yn .data (),y1 .size ()* 4 )== 0 ; 91 std::printf (" mmvf cols n_out %3d T %d %s\n" ,n_out ,T ,same ?"bit-identical" :"*** DIFFERS ***" ); 92if (!same )++ bad ; 93if (n_out == E ) { 94int * i1 ,* in_ ;float * w1 ,* wn ; 95ck (DPCT_CHECK_ERROR ( 96i1 = (int * )sycl::malloc_device ( 97 (size_t )T * K * 4 , dpct::get_in_order_queue ())), 98"i1" ); 99ck (DPCT_CHECK_ERROR ( 100in_ = (int * )sycl::malloc_device ( 101 (size_t )T * K * 4 , dpct::get_in_order_queue ())), 102"in" ); 103ck (DPCT_CHECK_ERROR ( 104w1 = (float * )sycl::malloc_device ( 105 (size_t )T * K * 4 , dpct::get_in_order_queue ())), 106"w1" ); 107ck (DPCT_CHECK_ERROR ( 108wn = (float * )sycl::malloc_device ( 109 (size_t )T * K * 4 , dpct::get_in_order_queue ())), 110"wn" ); 111for (int t = 0 ;t < T ;++ t ) 112 strata::kernels::router_top10 (dy1 + (size_t )t * E ,1 ,E ,K ,i1 + t * K ,w1 + t * K ,nullptr ); 113 strata::kernels::router_top10 (dy1 ,T ,E ,K ,in_ ,wn ,nullptr ); 114ck (DPCT_CHECK_ERROR ( 115 dpct::get_current_device ().queues_wait_and_throw ()), 116"sync top" ); 117 std::vector < int > a ((size_t )T * K ),b ((size_t )T * K ); 118 std::vector < float > fa ((size_t )T * K ),fb ((size_t )T * K ); 119ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 120 .memcpy (a .data (),i1 ,a .size ()* 4 ) 121 .wait ()), 122"ids1" ); 123ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 124 .memcpy (b .data (),in_ ,b .size ()* 4 ) 125 .wait ()), 126"idsn" ); 127ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 128 .memcpy (fa .data (),w1 ,fa .size ()* 4 ) 129 .wait ()), 130"w1" ); 131ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 132 .memcpy (fb .data (),wn ,fb .size ()* 4 ) 133 .wait ()), 134"wn" ); 135const bool s2 = a == b && std::memcmp (fa .data (),fb .data (),fa .size ()* 4 )== 0 ; 136 std::printf (" top10 n T %d %s\n" ,T ,s2 ?"bit-identical" :"*** DIFFERS ***" ); 137if (!s2 )++ bad ; 138 sycl::free (i1 , dpct::get_in_order_queue ()); 139 sycl::free (in_ , dpct::get_in_order_queue ()); 140 sycl::free (w1 , dpct::get_in_order_queue ()); 141 sycl::free (wn , dpct::get_in_order_queue ()); 142 } 143 sycl::free (dw , dpct::get_in_order_queue ()); 144 sycl::free (dx , dpct::get_in_order_queue ()); 145 sycl::free (dy1 , dpct::get_in_order_queue ()); 146 sycl::free (dyn , dpct::get_in_order_queue ()); 147 } 148 } 149 std::printf (bad ?"route_window_parity: %d FAILURES\n" :"route_window_parity OK\n" ,bad ); 150return bad ?1 :0 ; 151}