okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2eba71548
main
1// xmx_group_parity: iq_xmx_grouped (the A770 port's fused decode + grouped XMX) against the engine's dequant + 2// oneMKL per expert, on random expert blobs of each format the native packs use. Also times both. 3// xmx_group_parity [mean_rows=160] [n=16] 4#include "strata/kernels/iq_kernels.hpp" 5#include "strata/sycl_queue.hpp" 6#include <sycl/sycl.hpp> 7#include <dpct/dpct.hpp> 8#include <dpct/blas_utils.hpp> 9#include <chrono> 10#include <cmath> 11#include <cstdio> 12#include <cstdlib> 13#include <random> 14#include <vector> 15static uint16_t f2h (float f ) {return sycl::bit_cast < uint16_t > (sycl::half (f )); } 16int main (int argc ,char ** argv ) { 17const int mean = argc > 1 ? std::atoi (argv [1 ]) :160 ; 18const int n = argc > 2 ? std::atoi (argv [2 ]) :16 ; 19 sycl::queue * q = & dpct::get_in_order_queue (); 20#ifdef STRATA_NO_DG2_XMX 21 std::printf ("xmx_group_parity: SKIP (the sub-group 8 / 8x8x16 kernels are not built for this AOT device)" ); 22return 0 ; 23#else 24#ifndef STRATA_XMX_SG16 25if (strata::intel_gpu_gen (q -> get_device ())== strata::IntelGpuGen ::Battlemage ) { 26 std::printf ("xmx_group_parity: SKIP (this build has the sub-group 8 / 8x8x16 kernels; Battlemage needs the AOT bmg build)" ); 27return 0 ; 28 } 29#endif 30#endif 31 dpct::blas::descriptor_ptr hd = new dpct::blas::descriptor (); 32hd -> set_queue (q ); 33 std::mt19937 rng (3 ); 34 std::vector < int32_t > cnt (n ); 35 std::gamma_distribution < double > gd (1.5 ,mean /1.5 ); 36int64_t tot = 0 ; 37for (auto & c :cnt ) {c = std::max (1 , (int )gd (rng ));tot += c ; } 38int fails = 0 ; 39struct Case {int ty ;bool gu ; }; 40// the formats of the native packs' experts that the prompt path's fused XMX takes (prefill.cpp xmx2_fmt) 41for (Case cs : {Case {18 , true},Case {21 , true},Case {22 , true},Case {23 , true},Case {20 , false},Case {42 , false}}) { 42const int64_t K = cs .gu ?2560 :640 ,N = cs .gu ?1280 :2560 ,n_ff = 640 ; 43const int blk_elems = cs .ty == 20 ?32 :cs .ty == 42 ?64 :256 ; 44const size_t blk_bytes = strata::kernels::iq_row_bytes (cs .ty ,blk_elems ); 45const size_t mat_bytes = strata::kernels::iq_row_bytes (cs .ty ,K )* (cs .gu ?n_ff :N ); 46if (blk_bytes == 0 || mat_bytes == 0 ) { std::printf ("type %d: not supported\n" ,cs .ty );continue ; } 47auto blob = [& ]() { 48 std::vector < uint8_t > b (mat_bytes ); 49for (auto & x :b )x = (uint8_t )rng (); 50if (cs .ty != 29 )// d = 0.0625 at each block's start (IQ1_M keeps its scale in the scales field) 51for (size_t o = 0 ;o + 1 < b .size ();o += blk_bytes ) {b [o ]= 0x00 ;b [o + 1 ]= 0x2C ; } 52return b ; 53 }; 54 std::vector < uint8_t *> dg (n ),du (n ); 55 std::vector < const uint8_t *> pg (n ),pu (n ); 56for (int i = 0 ;i < n ;++ i ) { 57auto g = blob (); 58dg [i ]= sycl::malloc_device < uint8_t > (mat_bytes ,* q ); 59q -> memcpy (dg [i ],g .data (),mat_bytes ); 60pg [i ]= dg [i ]; 61if (cs .gu ) { 62auto u = blob (); 63du [i ]= sycl::malloc_device < uint8_t > (mat_bytes ,* q ); 64q -> memcpy (du [i ],u .data (),mat_bytes ); 65pu [i ]= du [i ]; 66 } 67 } 68 std::vector < uint16_t > hx ((size_t )tot * K ); 69 std::normal_distribution < float > nd (0.f ,1.f ); 70for (auto & x :hx )x = f2h (nd (rng )* 0.25f ); 71uint16_t * dx = sycl::malloc_device < uint16_t > (hx .size (),* q ); 72q -> memcpy (dx ,hx .data (),hx .size ()* 2 ); 73uint16_t * dw = sycl::malloc_device < uint16_t > ((size_t )N * K ,* q ); 74float * yr = sycl::malloc_device < float > ((size_t )tot * N ,* q ); 75float * yx = sycl::malloc_device < float > ((size_t )tot * N ,* q ); 76q -> wait (); 77auto ref = [& ]() { 78int64_t o = 0 ; 79for (int i = 0 ;i < n ;++ i ) { 80if (cs .gu ) strata::kernels::iq_dequant_gu_f16 (cs .ty ,dg [i ],du [i ],n_ff ,K ,dw ,q ); 81else strata::kernels::iq_dequant_f16 (cs .ty ,dg [i ],N * K ,dw ,q ); 82const float a = 1.f ,b = 0.f ; 83 dpct::blas::gemm (hd , oneapi::mkl::transpose::trans , oneapi::mkl::transpose::nontrans , (int )N ,cnt [i ], 84 (int )K ,& a ,dw , dpct::library_data_t::real_half , (int )K ,dx + o * K , 85 dpct::library_data_t::real_half , (int )K ,& b ,yr + o * N , 86 dpct::library_data_t::real_float , (int )N , dpct::compute_type::f32 ); 87o += cnt [i ]; 88 } 89 }; 90auto mine = [& ]() { 91 strata::kernels::iq_xmx_grouped (cs .ty ,pg .data (),cs .gu ?pu .data () :nullptr ,cnt .data (),n ,dx ,yx , 92 (int )N , (int )K ,q ); 93 }; 94auto tm = [& ](auto f ) { 95f ();q -> wait (); 96const auto a = std::chrono::steady_clock::now (); 97for (int r = 0 ;r < 10 ;++ r )f (); 98q -> wait (); 99return std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- a ).count () /10 ; 100 }; 101const double t_ref = tm (ref ),t_x = tm (mine ); 102 std::vector < float > a ((size_t )tot * N ),b ((size_t )tot * N ); 103q -> memcpy (a .data (),yr ,a .size ()* 4 ); 104q -> memcpy (b .data (),yx ,b .size ()* 4 ).wait (); 105double e = 0 ,mx = 0 ; 106size_t bad = 0 ,bad_ref = 0 ,mism = 0 ; 107for (size_t i = 0 ;i < a .size ();++ i ) { 108if (!std::isfinite (a [i ])) {++ bad_ref ;if (std::isfinite (b [i ]))++ mism ;continue ; } 109if (!std::isfinite (b [i ])) {++ bad ;continue ; } 110e = std::max (e , (double ) std::fabs (a [i ]- b [i ])); 111mx = std::max (mx , (double ) std::fabs (a [i ])); 112 } 113if (bad_ref ) std::printf (" (reference non-finite %zu of %zu, fused finite there %zu)\n" ,bad_ref ,a .size (),mism ); 114const bool ok = bad == 0 && mism == 0 && mx > 0 && e <=2e-3 * mx ; 115fails += !ok ; 116 std::printf ("type %2d %-7s rows %5lld: dequant+oneMKL %7.3f ms | fused xmx %7.3f ms (%.2fx) relerr %.1e %s\n" , 117cs .ty ,cs .gu ?"gate/up" :"down" , (long long )tot ,t_ref ,t_x ,t_ref /t_x ,e / (mx > 0 ?mx :1 ), 118ok ?"OK" :"FAIL" ); 119for (int i = 0 ;i < n ;++ i ) { sycl::free (dg [i ],* q );if (cs .gu ) sycl::free (du [i ],* q ); } 120 sycl::free (dx ,* q ); sycl::free (dw ,* q ); sycl::free (yr ,* q ); sycl::free (yx ,* q ); 121 } 122 std::printf ("%s\n" ,fails ?"FAILED" :"all OK" ); 123return fails ?1 :0 ; 124}