okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// A770 port: grouped FP16 XMX GEMM for the prompt path's experts (STRATA_PF_XMX=1). 2// Y[r][n] = sum_k X[r][k] * W_e[n][k] for each expert e of a group, its rows r in [row0 + off[e], row0 + off[e+1]). 3// One launch per group instead of one oneMKL call per expert. joint_matrix 8x8x16 (DG2), SLM-tiled, 256 GRF. 4// Measured standalone on the A770 (64 experts, gamma-distributed rows, vs per-expert oneMKL): down 6.1x at mean 40 5// rows, 2.8x at 160, 1.75x at 320; gate/up 3.0x / 1.46x / 0.79x (oneMKL wins above ~250 rows: the caller picks). 6#include <sycl/sycl.hpp> 7#include "strata/kprof.hpp" 8#include <sycl/ext/oneapi/matrix/matrix.hpp> 9#include <sycl/ext/intel/experimental/grf_size_properties.hpp> 10#include <cstdlib> 11#include <cstdio> 12#include "strata/sycl_queue.hpp" 13#include "strata/prefill/xmx_moe.hpp" 14 15namespace strata::prefill::xmx { 16namespace { 17using half = sycl::half ; 18namespace jm= sycl::ext::oneapi::experimental::matrix; 19// Sub-group width = the XMX tile's N: 8 on Alchemist (DG2, joint_matrix 8x8x16), 16 on Xe2 (Battlemage, 8x16x16; the 20// CMake switch STRATA_XMX_SG16 for an AOT bmg / lnl build). A build has one shape: the other is not valid there. 21#ifdef STRATA_XMX_SG16 22constexpr int kSgW = 16 ; 23#else 24constexpr int kSgW = 8 ; 25#endif 26 27struct Grp { 28const half * w [kMaxGroup ]; 29int off [kMaxGroup + 1 ];// rows, relative to row0 30int tstart [kMaxGroup + 1 ];// first M-tile of each expert 31int n ; 32}; 33 34template < int SGM_ ,int SGN_ ,int MT_ ,int NT_ ,int KS_ > 35struct Cfg { 36static constexpr int SGM = SGM_ ,SGN = SGN_ ,MT = MT_ ,NT = NT_ ,KS = KS_ ; 37static constexpr int SG = kSgW ; 38static constexpr int BM = SGM * MT * 8 ,BN = SGN * NT * kSgW ,NSG = SGM * SGN ,WG = NSG * SG ; 39}; 40 41template < class C ,class Name > 42void launch (sycl::queue & q ,const half * X ,float * Y ,const Grp & gp ,int ntiles ,int N ,int K ) { 43constexpr int BM = C ::BM ,BN = C ::BN ,KS = C ::KS ,WG = C ::WG ,SG = C ::SG ; 44constexpr int LDA = KS + 8 ,LDB = KS + 8 ; 45const int nb = N /BN ; 46#ifndef STRATA_NO_DG2_XMX 47constexpr int TN = kSgW ; 48 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 49 sycl::local_accessor < half ,1 > sa (BM * LDA ,h ),sb (BN * LDB ,h ); 50auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < 256 > }; 51h .parallel_for < Name > (sycl::nd_range < 2 > ({(size_t )ntiles , (size_t )nb * WG }, {1 , (size_t )WG }),props , 52 [= ](sycl::nd_item < 2 > it ) [[sycl ::reqd_sub_group_size (kSgW )]] { 53auto sg = it .get_sub_group (); 54const int t = (int )it .get_group (0 ); 55int e = 0 ; 56while (e + 1 < gp .n && t >=gp .tstart [e + 1 ])++ e ; 57const int m0 = (t - gp .tstart [e ])* BM ; 58const int rows = sycl::min (BM ,gp .off [e + 1 ]- gp .off [e ]- m0 ); 59const half * Xe = X + ((size_t )gp .off [e ]+ m0 )* K ; 60float * Ye = Y + ((size_t )gp .off [e ]+ m0 )* N ; 61const half * We = gp .w [e ]; 62const int n0 = (int )it .get_group (1 )* BN ; 63const int lid = (int )it .get_local_id (1 ),sgid = lid /SG ; 64const int sgm = sgid /C ::SGN ,sgn = sgid %C ::SGN ; 65 jm::joint_matrix < sycl::sub_group ,float , jm::use::accumulator ,8 ,TN > acc [C ::MT ][C ::NT ]; 66for (int i = 0 ;i < C ::MT ;++ i ) 67for (int j = 0 ;j < C ::NT ;++ j ) jm::joint_matrix_fill (sg ,acc [i ][j ],0.0f ); 68half * pa = sa .template get_multi_ptr < sycl::access::decorated::no > ().get (); 69half * pb = sb .template get_multi_ptr < sycl::access::decorated::no > ().get (); 70for (int k0 = 0 ;k0 < K ;k0 += KS ) { 71constexpr int VA = BM * KS /8 ,VB = BN * KS /8 ; 72for (int v = lid ;v < VA ;v += WG ) { 73const int r = v / (KS /8 ),c = (v % (KS /8 ))* 8 ; 74 sycl::vec < half ,8 > x (0 ); 75if (r < rows )x = * reinterpret_cast < const sycl::vec < half ,8 >*> (Xe + (size_t )r * K + k0 + c ); 76* reinterpret_cast < sycl::vec < half ,8 >*> (pa + r * LDA + c )= x ; 77 } 78for (int v = lid ;v < VB ;v += WG ) { 79const int r = v / (KS /8 ),c = (v % (KS /8 ))* 8 ; 80* reinterpret_cast < sycl::vec < half ,8 >*> (pb + r * LDB + c )= 81* reinterpret_cast < const sycl::vec < half ,8 >*> (We + (size_t ) (n0 + r )* K + k0 + c ); 82 } 83it .barrier (sycl::access::fence_space::local_space ); 84for (int kk = 0 ;kk < KS ;kk += 16 ) { 85 jm::joint_matrix < sycl::sub_group ,half , jm::use::a ,8 ,16 , jm::layout::row_major > a [C ::MT ]; 86 jm::joint_matrix < sycl::sub_group ,half , jm::use::b ,16 ,TN , jm::layout::col_major > b [C ::NT ]; 87for (int i = 0 ;i < C ::MT ;++ i ) 88 jm::joint_matrix_load (sg ,a [i ],sa .template get_multi_ptr < sycl::access::decorated::no > ()+ 89 (sgm * C ::MT * 8 + i * 8 )* LDA + kk ,LDA ); 90for (int j = 0 ;j < C ::NT ;++ j ) 91 jm::joint_matrix_load (sg ,b [j ],sb .template get_multi_ptr < sycl::access::decorated::no > ()+ 92 (sgn * C ::NT * TN + j * TN )* LDB + kk ,LDB ); 93for (int i = 0 ;i < C ::MT ;++ i ) 94for (int j = 0 ;j < C ::NT ;++ j ) jm::joint_matrix_mad (sg ,acc [i ][j ],a [i ],b [j ],acc [i ][j ]); 95 } 96it .barrier (sycl::access::fence_space::local_space ); 97 } 98for (int i = 0 ;i < C ::MT ;++ i ) { 99const int r = sgm * C ::MT * 8 + i * 8 ; 100if (r >=rows )continue ; 101for (int j = 0 ;j < C ::NT ;++ j ) { 102const int c = n0 + sgn * C ::NT * TN + j * TN ; 103if (r + 8 <=rows ) { 104 jm::joint_matrix_store (sg ,acc [i ][j ], 105 sycl::address_space_cast < sycl::access::address_space::global_space , sycl::access::decorated::no > ( 106Ye + (size_t )r * N + c ),N , jm::layout::row_major ); 107 }else {// partial rows: through this sub-group's own SLM slice 108float * st = reinterpret_cast < float *> (pa )+ sgid * 8 * TN ; 109 jm::joint_matrix_store (sg ,acc [i ][j ], 110 sycl::address_space_cast < sycl::access::address_space::local_space , sycl::access::decorated::no > (st ), 111TN , jm::layout::row_major ); 112 sycl::group_barrier (sg ); 113const int lim = rows - r ,ln = (int )sg .get_local_linear_id (); 114for (int rr = 0 ;rr < lim ;++ rr )Ye [(size_t ) (r + rr )* N + c + ln ]= st [rr * TN + ln ]; 115 sycl::group_barrier (sg ); 116 } 117 } 118 } 119 }); 120 }); 121#else 122 (void )q ; (void )X ; (void )Y ; (void )gp ; (void )ntiles ; (void )N ; (void )K ; 123 std::fprintf (stderr ,"grouped XMX: the 8x8x16 / sub-group 8 kernel is not built for this device (STRATA_NO_DG2_XMX)\n" ); 124 std::exit (1 ); 125#endif 126} 127 128class k_small ;class k_big ; 129#ifdef STRATA_XMX_SG16 130using Small = Cfg < 2 ,4 ,4 ,2 ,32 > ;// 64 x 128 tile: few rows per expert (8 sub-groups of 16) 131using Big = Cfg < 4 ,2 ,4 ,4 ,32 > ;// 128 x 128 tile 132#else 133using Small = Cfg < 2 ,8 ,4 ,2 ,32 > ;// 64 x 128 tile: few rows per expert 134using Big = Cfg < 4 ,4 ,4 ,4 ,32 > ;// 128 x 128 tile 135#endif 136 137template < class C > 138int fill (Grp & gp ,const int32_t * cnt ,int n ) { 139int t = 0 ,o = 0 ; 140for (int i = 0 ;i < n ;++ i ) { 141gp .off [i ]= o ;gp .tstart [i ]= t ; 142o += cnt [i ];t += (cnt [i ]+ C ::BM - 1 ) /C ::BM ; 143 } 144gp .off [n ]= o ;gp .tstart [n ]= t ;gp .n = n ; 145return t ; 146} 147}// namespace 148 149int mode () { 150// The kernels are joint_matrix 8x8x16 on sub-group 8 (DG2, Arc A-series) or, in a STRATA_XMX_SG16 build, 8x16x16 on 151// sub-group 16 (Xe2, Battlemage). 152static const int v = [] { 153const char * e = std::getenv ("STRATA_PF_XMX" ); 154// 0.1.48 (owner approved 2026-10-18): mode 2 (the fused grouped XMX launch) is the default on Arc Alchemist (A-series): A750 prompts +30..39% (4K), +32..35% (1-2K tokens), 155// first-token KL 0.002 against a 0.0005 floor, HumanEval / reasoning / agent / CJK / needles inside the run-to-run spread; Battlemage is 156// 7-11% slower with it, so it stays off there. STRATA_PF_XMX=0 turns it off, 1 is the dequantize + grouped XMX variant. 157const bool alchemist = strata::intel_gpu_gen (strata::q_of (nullptr )-> get_device ())== strata::IntelGpuGen ::Alchemist ; 158const int want = e ? std::atoi (e ) :alchemist ?2 :0 ; 159if (want == 0 )return 0 ; 160#ifdef STRATA_NO_DG2_XMX 161 std::fprintf (stderr ,"STRATA_PF_XMX ignored: this build is AOT for a device without the sub-group 8 XMX kernels\n" ); 162return 0 ; 163#else 164#ifndef STRATA_XMX_SG16 165if (strata::intel_gpu_gen (strata::q_of (nullptr )-> get_device ())== strata::IntelGpuGen ::Battlemage ) { 166 std::fprintf (stderr ,"STRATA_PF_XMX ignored: this build has the sub-group 8 / 8x8x16 kernels (Arc A-series); Battlemage needs the AOT bmg build\n" ); 167return 0 ; 168 } 169#endif 170return want ; 171#endif 172 }(); 173return v ; 174} 175bool enabled () {return mode ()!= 0 ; } 176 177int gu_max_mean () { 178static const int v = [] {const char * e = std::getenv ("STRATA_PF_XMX_GU_MAX" );return e ? std::atoi (e ) :250 ; }(); 179return v ; 180} 181 182void grouped_f16 (const uint16_t * X ,const uint16_t * const * W ,const int32_t * cnt ,int n ,float * Y ,int N ,int K , 183void * stream ) { 184if (n <=0 )return ; 185 sycl::queue & q = * strata::q_of (stream ); 186Grp gp {}; 187int64_t tot = 0 ; 188for (int i = 0 ;i < n ;++ i ) {gp .w [i ]= reinterpret_cast < const half *> (W [i ]);tot += cnt [i ]; } 189if (tot <=0 )return ; 190const half * Xh = reinterpret_cast < const half *> (X ); 191if (tot /n < 96 ) { 192const int nt = fill < Small > (gp ,cnt ,n ); 193launch < Small ,k_small > (q ,Xh ,Y ,gp ,nt ,N ,K ); 194 }else { 195const int nt = fill < Big > (gp ,cnt ,n ); 196launch < Big ,k_big > (q ,Xh ,Y ,gp ,nt ,N ,K ); 197 } 198} 199}// namespace strata::prefill::xmx