okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
24efc6e33
main
1// xmx_int8_bench (0.1.44): what a DPAS int8 expert GEMM costs next to the FP16 joint_matrix one, on one device. 2// xmx_int8_bench [M=160] [experts=64] [reps=20] build: -DSTRATA_XMX_SG16 for Xe2 (8x16x32), none for DG2 (8x8x32) 3// Shape of a Strata expert: gate+up N=1280, K=2560 (down: N=2560, K=640). Modes: 4// f16 : half A (M x K) and half W (N x K), float accumulate - the GEMM the current dequant + oneMKL path runs 5// i8 : int8 A and W, int32 accumulate over the whole K (no per-block scale): the DPAS floor 6// i8blk : int8 A and W, one DPAS per 32 of K into a zeroed int32 tile, then acc += float(tile) * dw[n] * dx[m][kb] 7// (the per-32 scales of the activations and of the IQ weights): the real fused-kernel epilogue 8// Weights are random int8 (decode cost excluded; a real kernel adds the IQ grid decode). E different experts, so the 9// weights come from DRAM, not L2. Prints us per expert and the effective TOPS; checks i8blk against a host reference. 10#include <sycl/sycl.hpp> 11#include <sycl/ext/oneapi/matrix/matrix.hpp> 12#include <sycl/ext/intel/experimental/grf_size_properties.hpp> 13#include <chrono> 14#include <cmath> 15#include <cstdio> 16#include <cstdlib> 17#include <random> 18#include <vector> 19 20namespace jm= sycl::ext::oneapi::experimental::matrix; 21using half = sycl::half ; 22#ifdef STRATA_XMX_SG16 23constexpr int SGW = 16 ; 24#else 25constexpr int SGW = 8 ; 26#endif 27constexpr int SGM = 2 ,SGN = 4 ,MT = 4 ,NT = 2 ;// 8 sub-groups: tile 64 x (4*2*SGW) 28constexpr int BM = SGM * MT * 8 ,BN = SGN * NT * SGW ,NSG = SGM * SGN ,WG = NSG * SGW ; 29 30// mode 0: f16 31template < class T > 32struct Slm ; 33void run_f16 (sycl::queue & q ,const half * A ,const half * W ,float * C ,int M ,int N ,int K ,int E ) { 34constexpr int KS = 32 ,LD = KS + 8 ; 35const int nmt = (M + BM - 1 ) /BM ,nnt = N /BN ; 36q .submit ([& ](sycl::handler & h ) { 37 sycl::local_accessor < half ,1 > sa (BM * LD ,h ),sb (BN * LD ,h ); 38auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < 256 > }; 39h .parallel_for < class k_f16 > (sycl::nd_range < 3 > ({(size_t )E , (size_t )nmt , (size_t )nnt * WG }, {1 ,1 , (size_t )WG }),props , 40 [= ](sycl::nd_item < 3 > it ) [[sycl ::reqd_sub_group_size (SGW )]] { 41auto sg = it .get_sub_group (); 42const int e = (int )it .get_group (0 ),m0 = (int )it .get_group (1 )* BM ,n0 = (int )it .get_group (2 )* BN ; 43const int lid = (int )it .get_local_id (2 ),sgid = lid /SGW ,sgm = sgid /SGN ,sgn = sgid %SGN ; 44const half * We = W + (size_t )e * N * K ; 45 jm::joint_matrix < sycl::sub_group ,float , jm::use::accumulator ,8 ,SGW > acc [MT ][NT ]; 46for (int i = 0 ;i < MT ;++ i )for (int j = 0 ;j < NT ;++ j ) jm::joint_matrix_fill (sg ,acc [i ][j ],0.0f ); 47half * pa = sa .template get_multi_ptr < sycl::access::decorated::no > ().get (); 48half * pb = sb .template get_multi_ptr < sycl::access::decorated::no > ().get (); 49for (int k0 = 0 ;k0 < K ;k0 += KS ) { 50for (int v = lid ;v < BM * KS /8 ;v += WG ) { 51const int r = v / (KS /8 ),c = (v % (KS /8 ))* 8 ; 52 sycl::vec < half ,8 > x (0 ); 53if (m0 + r < M )x = * reinterpret_cast < const sycl::vec < half ,8 >*> (A + (size_t ) (m0 + r )* K + k0 + c ); 54* reinterpret_cast < sycl::vec < half ,8 >*> (pa + r * LD + c )= x ; 55 } 56for (int v = lid ;v < BN * KS /8 ;v += WG ) { 57const int r = v / (KS /8 ),c = (v % (KS /8 ))* 8 ; 58* reinterpret_cast < sycl::vec < half ,8 >*> (pb + r * LD + c )= 59* reinterpret_cast < const sycl::vec < half ,8 >*> (We + (size_t ) (n0 + r )* K + k0 + c ); 60 } 61it .barrier (sycl::access::fence_space::local_space ); 62for (int kk = 0 ;kk < KS ;kk += 16 ) { 63 jm::joint_matrix < sycl::sub_group ,half , jm::use::a ,8 ,16 , jm::layout::row_major > a [MT ]; 64 jm::joint_matrix < sycl::sub_group ,half , jm::use::b ,16 ,SGW , jm::layout::col_major > b [NT ]; 65for (int i = 0 ;i < MT ;++ i ) 66 jm::joint_matrix_load (sg ,a [i ],sa .template get_multi_ptr < sycl::access::decorated::no > ()+ (sgm * MT * 8 + i * 8 )* LD + kk ,LD ); 67for (int j = 0 ;j < NT ;++ j ) 68 jm::joint_matrix_load (sg ,b [j ],sb .template get_multi_ptr < sycl::access::decorated::no > ()+ (sgn * NT * SGW + j * SGW )* LD + kk ,LD ); 69for (int i = 0 ;i < MT ;++ i )for (int j = 0 ;j < NT ;++ j ) jm::joint_matrix_mad (sg ,acc [i ][j ],a [i ],b [j ],acc [i ][j ]); 70 } 71it .barrier (sycl::access::fence_space::local_space ); 72 } 73for (int i = 0 ;i < MT ;++ i ) { 74const int r = m0 + sgm * MT * 8 + i * 8 ; 75if (r + 8 > M )continue ;// the bench uses M a multiple of 8; rows beyond are zero-padded work only 76for (int j = 0 ;j < NT ;++ j ) 77 jm::joint_matrix_store (sg ,acc [i ][j ], sycl::address_space_cast < sycl::access::address_space::global_space , sycl::access::decorated::no > ( 78C + ((size_t )e * M + r )* N + n0 + sgn * NT * SGW + j * SGW ),N , jm::layout::row_major ); 79 } 80 }); 81 }); 82} 83 84// mode 1/2: int8. BLK: per-32 float epilogue (dw: [E][N][K/32] float, dx: [M][K/32] float) 85constexpr int KI = 32 ;// joint_matrix int8 K 86template < bool BLK ,class Name > 87void run_i8 (sycl::queue & q ,const int8_t * A ,const int8_t * W ,const float * dw ,const float * dx ,float * C ,int M ,int N ,int K ,int E ) { 88constexpr int KS = 64 ,LD = KS + 16 ;// bytes 89const int nmt = (M + BM - 1 ) /BM ,nnt = N /BN ,nkb = K /32 ; 90q .submit ([& ](sycl::handler & h ) { 91 sycl::local_accessor < int8_t ,1 > sa (BM * LD ,h ),sb (BN * LD ,h ); 92 sycl::local_accessor < float ,1 > sdw (BN * 2 ,h ),sdx (BM * 2 ,h ); 93auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < 256 > }; 94h .parallel_for < Name > (sycl::nd_range < 3 > ({(size_t )E , (size_t )nmt , (size_t )nnt * WG }, {1 ,1 , (size_t )WG }),props , 95 [= ](sycl::nd_item < 3 > it ) [[sycl ::reqd_sub_group_size (SGW )]] { 96auto sg = it .get_sub_group (); 97const int e = (int )it .get_group (0 ),m0 = (int )it .get_group (1 )* BM ,n0 = (int )it .get_group (2 )* BN ; 98const int lid = (int )it .get_local_id (2 ),sgid = lid /SGW ,sgm = sgid /SGN ,sgn = sgid %SGN ; 99const int8_t * We = W + (size_t )e * N * K ; 100 jm::joint_matrix < sycl::sub_group ,float , jm::use::accumulator ,8 ,SGW > acc [MT ][NT ]; 101 jm::joint_matrix < sycl::sub_group ,int32_t , jm::use::accumulator ,8 ,SGW > tmp [MT ][NT ]; 102for (int i = 0 ;i < MT ;++ i )for (int j = 0 ;j < NT ;++ j ) { 103 jm::joint_matrix_fill (sg ,acc [i ][j ],0.0f ); 104 jm::joint_matrix_fill (sg ,tmp [i ][j ],0 ); 105 } 106int8_t * pa = sa .template get_multi_ptr < sycl::access::decorated::no > ().get (); 107int8_t * pb = sb .template get_multi_ptr < sycl::access::decorated::no > ().get (); 108float * pdw = sdw .template get_multi_ptr < sycl::access::decorated::no > ().get (); 109float * pdx = sdx .template get_multi_ptr < sycl::access::decorated::no > ().get (); 110for (int k0 = 0 ;k0 < K ;k0 += KS ) { 111for (int v = lid ;v < BM * KS /16 ;v += WG ) { 112const int r = v / (KS /16 ),c = (v % (KS /16 ))* 16 ; 113 sycl::vec < int8_t ,16 > x (0 ); 114if (m0 + r < M )x = * reinterpret_cast < const sycl::vec < int8_t ,16 >*> (A + (size_t ) (m0 + r )* K + k0 + c ); 115* reinterpret_cast < sycl::vec < int8_t ,16 >*> (pa + r * LD + c )= x ; 116 } 117for (int v = lid ;v < BN * KS /16 ;v += WG ) { 118const int r = v / (KS /16 ),c = (v % (KS /16 ))* 16 ; 119* reinterpret_cast < sycl::vec < int8_t ,16 >*> (pb + r * LD + c )= 120* reinterpret_cast < const sycl::vec < int8_t ,16 >*> (We + (size_t ) (n0 + r )* K + k0 + c ); 121 } 122if (BLK ) { 123for (int v = lid ;v < BN * 2 ;v += WG )pdw [v ]= dw [((size_t )e * N + n0 + v /2 )* nkb + k0 /32 + v %2 ]; 124for (int v = lid ;v < BM * 2 ;v += WG )pdx [v ]= (m0 + v /2 < M ) ?dx [(size_t ) (m0 + v /2 )* nkb + k0 /32 + v %2 ] :0.f ; 125 } 126it .barrier (sycl::access::fence_space::local_space ); 127for (int kk = 0 ;kk < KS ;kk += KI ) { 128 jm::joint_matrix < sycl::sub_group ,int8_t , jm::use::a ,8 ,KI , jm::layout::row_major > a [MT ]; 129 jm::joint_matrix < sycl::sub_group ,int8_t , jm::use::b ,KI ,SGW , jm::layout::col_major > b [NT ]; 130for (int i = 0 ;i < MT ;++ i ) 131 jm::joint_matrix_load (sg ,a [i ],sa .template get_multi_ptr < sycl::access::decorated::no > ()+ (sgm * MT * 8 + i * 8 )* LD + kk ,LD ); 132for (int j = 0 ;j < NT ;++ j ) 133 jm::joint_matrix_load (sg ,b [j ],sb .template get_multi_ptr < sycl::access::decorated::no > ()+ (sgn * NT * SGW + j * SGW )* LD + kk ,LD ); 134if (BLK ) { 135for (int i = 0 ;i < MT ;++ i )for (int j = 0 ;j < NT ;++ j ) { 136 jm::joint_matrix_fill (sg ,tmp [i ][j ],0 ); 137 jm::joint_matrix_mad (sg ,tmp [i ][j ],a [i ],b [j ],tmp [i ][j ]); 138const float sw = pdw [(sgn * NT * SGW + j * SGW + (int )sg .get_local_linear_id ())* 2 + kk /32 ]; 139const int rbase = sgm * MT * 8 + i * 8 ; 140int ei = 0 ; 141 jm::joint_matrix_apply (sg ,tmp [i ][j ],acc [i ][j ], [& ](int32_t & t ,float & ac ) { 142ac = sycl::fma ((float )t * sw ,pdx [(rbase + ei )* 2 + kk /32 ],ac ); 143++ ei ; 144 }); 145 } 146 }else { 147for (int i = 0 ;i < MT ;++ i )for (int j = 0 ;j < NT ;++ j ) jm::joint_matrix_mad (sg ,tmp [i ][j ],a [i ],b [j ],tmp [i ][j ]); 148 } 149 } 150it .barrier (sycl::access::fence_space::local_space ); 151 } 152for (int i = 0 ;i < MT ;++ i ) { 153const int r = m0 + sgm * MT * 8 + i * 8 ; 154if (r + 8 > M )continue ; 155for (int j = 0 ;j < NT ;++ j ) { 156if (!BLK ) {// int32 -> float through a per-element copy 157 jm::joint_matrix_apply (sg ,tmp [i ][j ],acc [i ][j ], [& ](int32_t & t ,float & ac ) {ac = (float )t ; }); 158 } 159 jm::joint_matrix_store (sg ,acc [i ][j ], sycl::address_space_cast < sycl::access::address_space::global_space , sycl::access::decorated::no > ( 160C + ((size_t )e * M + r )* N + n0 + sgn * NT * SGW + j * SGW ),N , jm::layout::row_major ); 161 } 162 } 163 }); 164 }); 165} 166class k_i8 ;class k_i8blk ; 167 168int main (int argc ,char ** argv ) { 169const int M = argc > 1 ? std::atoi (argv [1 ]) :160 ; 170const int E = argc > 2 ? std::atoi (argv [2 ]) :64 ; 171const int reps = argc > 3 ? std::atoi (argv [3 ]) :20 ; 172 sycl::queue q {sycl::gpu_selector_v , sycl::property::queue::in_order ()}; 173 std::printf ("device %s, sub-group %d, M %d, %d experts\n" ,q .get_device ().get_info < sycl::info::device::name > ().c_str (),SGW ,M ,E ); 174struct Shape {const char * nm ;int N ,K ; }shapes [2 ]= {{"gate+up" ,1280 ,2560 }, {"down" ,2560 ,640 }}; 175 std::mt19937 rng (5 ); 176for (const Shape & sh :shapes ) { 177const int N = sh .N ,K = sh .K ,nkb = K /32 ; 178const size_t nw = (size_t )E * N * K ,na = (size_t )M * K ; 179 std::vector < int8_t > hA (na ),hW (nw ); 180 std::vector < half > hAh (na ); 181for (size_t i = 0 ;i < na ;++ i ) {hA [i ]= (int8_t ) ((int ) (rng () %255 )- 127 );hAh [i ]= half ((float )hA [i ] /64.f ); } 182for (auto & x :hW )x = (int8_t ) ((int ) (rng () %31 )- 15 ); 183 std::vector < float > hdw ((size_t )E * N * nkb ),hdx ((size_t )M * nkb ); 184for (auto & x :hdw )x = 0.01f * (1 + rng () %8 ); 185for (auto & x :hdx )x = 0.02f * (1 + rng () %8 ); 186int8_t * dA = sycl::malloc_device < int8_t > (na ,q ),* dW = sycl::malloc_device < int8_t > (nw ,q ); 187half * dAh = sycl::malloc_device < half > (na ,q ),* dWh = sycl::malloc_device < half > (nw ,q ); 188float * ddw = sycl::malloc_device < float > (hdw .size (),q ),* ddx = sycl::malloc_device < float > (hdx .size (),q ); 189float * dC = sycl::malloc_device < float > ((size_t )E * M * N ,q ); 190q .memcpy (dA ,hA .data (),na ).wait ();q .memcpy (dW ,hW .data (),nw ).wait (); 191q .memcpy (dAh ,hAh .data (),na * 2 ).wait (); 192 { std::vector < half > t (nw );for (size_t i = 0 ;i < nw ;++ i )t [i ]= half ((float )hW [i ]);q .memcpy (dWh ,t .data (),nw * 2 ).wait (); } 193q .memcpy (ddw ,hdw .data (),hdw .size ()* 4 ).wait ();q .memcpy (ddx ,hdx .data (),hdx .size ()* 4 ).wait (); 194auto time = [& ](auto && fn ) { 195fn ();q .wait (); 196double best = 1e30 ; 197for (int r = 0 ;r < reps ;++ r ) { 198const auto t0 = std::chrono::steady_clock::now (); 199fn ();q .wait (); 200best = std::min (best , std::chrono::duration < double , std::micro > (std::chrono::steady_clock::now ()- t0 ).count ()); 201 } 202return best /E ;// us per expert (launch overhead amortised over E experts) 203 }; 204const double ops = 2.0 * M * N * K ; 205const double t0 = time ([& ] {run_f16 (q ,dAh ,dWh ,dC ,M ,N ,K ,E ); }); 206const double t1 = time ([& ] {run_i8 < false,k_i8 > (q ,dA ,dW ,ddw ,ddx ,dC ,M ,N ,K ,E ); }); 207const double t2 = time ([& ] {run_i8 < true,k_i8blk > (q ,dA ,dW ,ddw ,ddx ,dC ,M ,N ,K ,E ); }); 208// check i8blk expert 3 against a host reference 209 std::vector < float > hC ((size_t )M * N ); 210q .memcpy (hC .data (),dC + (size_t )3 * M * N ,hC .size ()* 4 ).wait (); 211double maxerr = 0 ,maxref = 0 ; 212for (int m = 0 ;m < M ;m += 7 )for (int n = 0 ;n < N ;n += 11 ) { 213double ref = 0 ; 214for (int kb = 0 ;kb < nkb ;++ kb ) { 215long long s = 0 ; 216for (int k = 0 ;k < 32 ;++ k )s += (long long )hA [(size_t )m * K + kb * 32 + k ]* hW [((size_t )3 * N + n )* K + kb * 32 + k ]; 217ref += (double )s * hdw [((size_t )3 * N + n )* nkb + kb ]* hdx [(size_t )m * nkb + kb ]; 218 } 219maxref = std::max (maxref , std::fabs (ref ));maxerr = std::max (maxerr , std::fabs (ref - hC [(size_t )m * N + n ])); 220 } 221 std::printf ("%-8s N %d K %d: f16 %.1f us/expert (%.1f TFLOPs), i8 %.1f us (%.1f TOPS), i8blk %.1f us (%.1f TOPS); i8blk max err %.3g of %.3g\n" , 222sh .nm ,N ,K ,t0 ,ops /t0 /1e6 ,t1 ,ops /t1 /1e6 ,t2 ,ops /t2 /1e6 ,maxerr ,maxref ); 223 sycl::free (dA ,q ); sycl::free (dW ,q ); sycl::free (dAh ,q ); sycl::free (dWh ,q ); sycl::free (ddw ,q ); sycl::free (ddx ,q ); sycl::free (dC ,q ); 224 } 225return 0 ; 226}