okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db137d917
main
1// src/prefill/moe_fused.cu - see include/strata/prefill/moe_fused.hpp (#136: the Q2_0 pack's prompt experts on 2// Strata's own int8 tensor-core kernels, with the grouping on the GPU). 3// 4// The arithmetic. A Q2_0 block is 64 weights w = d_w * (q - 1), q in 0..3, four codes per byte, weight j of a block 5// in byte j / 4 at bits 2 * (j % 4). An activation block of 32 values is x = d_x * a with a = round(x / d_x) in 6// -127..127. Over one 32-value half-block 7// sum w x = d_w * d_x * (sum q a - sum a), 8// so the int8 product takes the codes as they are (0..3 is a valid s8) and the offset is the activations' own sum, 9// stored with the block. The dot product (|sum q a| <= 32 * 3 * 127 < 2^22) becomes a float exactly with one 10// integer add: as_float(0x4B400000 + dot) = 1.5 * 2^23 + dot, and the block stores c = -(1.5 * 2^23 + sum a), so 11// as_float(0x4B400000 + dot) + c = dot - sum a with no rounding. Per output and 64-block that is 12// acc += d_w * (d_x0 * t0 + d_x1 * t1) (t = dot - sum a of each half) 13// - two integer adds, two float adds and three multiply-adds. 14// 15// The K order. mma.sync m16n8k32 takes four consecutive k of a row (A) or column (B) per register. Four consecutive 16// weights are one code byte; four codes of one register instead come from the same 2-bit field of four consecutive 17// bytes - (word >> 2t) & 0x03030303 holds weights t, t+4, t+8, t+12 of a 16-weight word. So the kernels use that order 18// for the k of a half-block: mma k index 16h + 4t + j stands for weight (and activation) 16h + t + 4j. The 19// activations are stored in it (perm32), and a lane's two B registers (h = 0 and 1) are adjacent: one 8-byte load. 20#define DPCT_PROFILING_ENABLED 21#include <sycl/sycl.hpp> 22#include "strata/kprof.hpp" 23#include <dpct/dpct.hpp> 24#include "strata/sycl_queue.hpp" 25#include "strata/prefill/moe_fused.hpp" 26#include "strata/core/geometry.hpp" 27 28#include <algorithm> 29#include <cstdio> 30#include <cstdlib> 31#include <cstring> 32#include <mutex> 33#include <cmath> 34 35namespace strata::prefill::fused { 36namespace { 37 38void ck (dpct::err0 e ,const char * what ) { 39} 40 41constexpr int AB = 80 ;// activation bytes per 64 values: 64 codes, then {d0, c0, d1, c1} 42constexpr int MAGIC = 0x4B400000 ;// the bits of 1.5 * 2^23 43constexpr float MAGICF = 12582912.0f ; 44// The Strata Q2_0 blob (moe_mmq.cu strata_q2_kernel, kernels.cu blob_dequant_kernel): gate/up codes [1280][640 B] 45// (rows interleaved: gate of feature f at row 2f, its up at 2f + 1), down codes [2560][160 B], gate/up scales 46// [1280][40] fp16, down scales [2560][10] fp16. 47constexpr size_t O_GU_CODES = 0 ,O_D_CODES = (size_t )1280 * 640 ,O_GU_SC = O_D_CODES + (size_t )2560 * 160 , 48O_D_SC = O_GU_SC + (size_t )1280 * 40 * 2 ; 49constexpr int THREADS = 512 ;// 16 warps: 4 along the weight rows (64 each) x 4 along the tile rows (16 each) 50constexpr int WROWS = 256 ;// weight rows per work item: 128 features of gate/up, or 256 outputs of down 51constexpr int STAGES = 4 ;// cp.async pipeline depth, one 64-weight block of K per stage 52constexpr int STAGE_BYTES = WROWS * 16 + kTileRows * AB ; 53constexpr size_t smem_bytes (bool gu ) { 54return (size_t )WROWS * (gu ?40 :10 )* 2 + STAGES * STAGE_BYTES + kTileRows * 4 ; 55} 56 57// the stored position of value k of a 32-value half-block (see "The K order") 58__dpct_inline__ int perm32 (int k ) { 59return 8 * (k & 3 )+ 4 * (k >>4 )+ ((k & 15 ) >>2 ); 60} 61// where value k of a 32-value half-block is stored: perm32 for the CUDA kernel; on HIP the gfx11 WMMA kernel's order 62// (value 4b + t of a 16-value word at 4t + b: see expert_w11_kernel) 63__dpct_inline__ int act_pos (int k ) { 64#if defined(__HIPCC__ ) 65return (k & 16 ) | ((k & 3 ) <<2 ) | ((k >>2 )& 3 ); 66#else 67return perm32 (k ); 68#endif 69} 70 71struct Tables { 72int32_t * cnt ,* off ,* fill ,* ts ;// per expert: rows, first row (E + 1), placement cursor, first tile (E + 1) 73 sycl::int2 * tiles ;// {expert, first row}, in expert order 74}; 75size_t tiles_at (int n_expert ) {return ((size_t ) (4 * n_expert + 2 )* 4 + 15 ) /16 * 16 ; } 76Tables tables (void * scratch ,int n_expert ) { 77int32_t * p = (int32_t * )scratch ; 78return {p ,p + n_expert ,p + 2 * n_expert + 1 ,p + 3 * n_expert + 1 , 79 (sycl::int2 * )((uint8_t * )scratch + tiles_at (n_expert ))}; 80} 81 82// ---- activations: one warp per 64 values 83__dpct_inline__ void quant_act_kernel (const float * __restrict__x ,int64_t nblk , 84uint8_t * __restrict__xa ) { 85auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 86const int64_t w = 87 (int64_t )item_ct1 .get_group (2 )* (item_ct1 .get_local_range (2 ) /32 )+ 88item_ct1 .get_local_id (2 ) /32 ; 89const int lane = item_ct1 .get_local_id (2 )& 31 ; 90if (w >=nblk )return ; 91const float v0 = x [w * 64 + lane ],v1 = x [w * 64 + 32 + lane ]; 92float a0 = sycl::fabs (v0 ),a1 = sycl::fabs (v1 ); 93#pragma unroll 94for (int o = 16 ;o > 0 ;o >>=1 ) { 95/* 96DPCT1108: '__shfl_xor_sync' was migrated with the experimental 97feature masked sub_group function which may not be supported by all 98compilers or runtimes. You may need to adjust the code. 99*/ 100a0 = sycl::fmax ( 101a0 , dpct::experimental::permute_sub_group_by_xor ( 1020xffffffffu , 103 sycl::ext::oneapi::this_work_item::get_sub_group (),a0 ,o )); 104/* 105DPCT1108: '__shfl_xor_sync' was migrated with the experimental 106feature masked sub_group function which may not be supported by all 107compilers or runtimes. You may need to adjust the code. 108*/ 109a1 = sycl::fmax ( 110a1 , dpct::experimental::permute_sub_group_by_xor ( 1110xffffffffu , 112 sycl::ext::oneapi::this_work_item::get_sub_group (),a1 ,o )); 113 } 114const int q0 = a0 > 0.0f ? sycl::vec < float ,1 > {(v0 * (127.0f /a0 ))} 115 .convert < int , sycl::rounding_mode::rte > ()[0 ] 116 :0 ; 117const int q1 = a1 > 0.0f ? sycl::vec < float ,1 > {(v1 * (127.0f /a1 ))} 118 .convert < int , sycl::rounding_mode::rte > ()[0 ] 119 :0 ; 120int s0 = q0 ,s1 = q1 ; 121#pragma unroll 122for (int o = 16 ;o > 0 ;o >>=1 ) { 123/* 124DPCT1108: '__shfl_xor_sync' was migrated with the experimental 125feature masked sub_group function which may not be supported by all 126compilers or runtimes. You may need to adjust the code. 127*/ 128s0 += dpct::experimental::permute_sub_group_by_xor ( 1290xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (),s0 , 130o ); 131/* 132DPCT1108: '__shfl_xor_sync' was migrated with the experimental 133feature masked sub_group function which may not be supported by all 134compilers or runtimes. You may need to adjust the code. 135*/ 136s1 += dpct::experimental::permute_sub_group_by_xor ( 1370xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (),s1 , 138o ); 139 } 140uint8_t * out = xa + w * AB ; 141out [act_pos (lane )]= (uint8_t ) (int8_t )q0 ; 142out [32 + act_pos (lane )]= (uint8_t ) (int8_t )q1 ; 143if (lane == 0 ) 144* (sycl::float4 * )(out + 64 )= 145 sycl::float4 (a0 /127.0f ,- (MAGICF + (float )s0 ),a1 /127.0f , 146- (MAGICF + (float )s1 )); 147} 148 149// ---- grouping: counts, offsets and tiles, placement 150__dpct_inline__ void count_kernel (const int32_t * __restrict__ids ,int64_t n , 151int E ,int32_t * __restrict__cnt , 152uint8_t * dpct_local ) { 153auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 154auto hist = (int32_t * )dpct_local ; 155#pragma unroll 156for (int e = item_ct1 .get_local_id (2 );e < E ; 157e += item_ct1 .get_local_range (2 ))hist [e ]= 0 ; 158/* 159DPCT1065: Consider replacing sycl::nd_item::barrier() with 160sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 161performance if there is no access to global memory. 162*/ 163item_ct1 .barrier (); 164for (int64_t i = 165 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 166item_ct1 .get_local_id (2 ); 167i < n ;i += (int64_t )item_ct1 .get_group_range (2 )* 168item_ct1 .get_local_range (2 )) { 169const int e = ids [i ]; 170if ((unsigned )e < (unsigned )E ) 171 dpct::atomic_fetch_add < sycl::access::address_space::generic_space > ( 172& hist [e ],1 ); 173 } 174/* 175DPCT1065: Consider replacing sycl::nd_item::barrier() with 176sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 177performance if there is no access to global memory. 178*/ 179item_ct1 .barrier (); 180#pragma unroll 181for (int e = item_ct1 .get_local_id (2 );e < E ; 182e += item_ct1 .get_local_range (2 )) 183if (hist [e ]) 184 dpct::atomic_fetch_add < sycl::access::address_space::generic_space > ( 185& cnt [e ],hist [e ]); 186} 187 188// exclusive prefix sum over the block (blockDim a multiple of 32); *total = the block's sum 189inline int block_excl_sum (int v ,int * total ,int * sh ) { 190auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 191const int lane = item_ct1 .get_local_id (2 )& 31 , 192w = item_ct1 .get_local_id (2 ) >>5 , 193nw = item_ct1 .get_local_range (2 ) >>5 ; 194int x = v ; 195#pragma unroll 196for (int o = 1 ;o < 32 ;o <<=1 ) { 197/* 198DPCT1108: '__shfl_up_sync' was migrated with the experimental 199feature masked sub_group function which may not be supported by all 200compilers or runtimes. You may need to adjust the code. 201*/ 202const int y = dpct::experimental::shift_sub_group_right ( 2030xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (),x , 204o ); 205if (lane >=o )x += y ; 206 } 207if (lane == 31 )sh [w ]= x ; 208/* 209DPCT1065: Consider replacing sycl::nd_item::barrier() with 210sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 211performance if there is no access to global memory. 212*/ 213item_ct1 .barrier (); 214if (w == 0 ) { 215int s = lane < nw ?sh [lane ] :0 ; 216#pragma unroll 217for (int o = 1 ;o < 32 ;o <<=1 ) { 218/* 219DPCT1108: '__shfl_up_sync' was migrated with the experimental 220feature masked sub_group function which may not be supported by all 221compilers or runtimes. You may need to adjust the code. 222*/ 223const int y = dpct::experimental::shift_sub_group_right ( 2240xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 225s ,o ); 226if (lane >=o )s += y ; 227 } 228sh [lane ]= s ; 229 } 230/* 231DPCT1065: Consider replacing sycl::nd_item::barrier() with 232sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 233performance if there is no access to global memory. 234*/ 235item_ct1 .barrier (); 236const int r = x - v + (w > 0 ?sh [w - 1 ] :0 ); 237* total = sh [nw - 1 ]; 238/* 239DPCT1065: Consider replacing sycl::nd_item::barrier() with 240sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better 241performance if there is no access to global memory. 242*/ 243item_ct1 .barrier ();// sh is reused by the next call 244return r ; 245} 246 247// one block, a thread per expert (E <= 1024) 248__dpct_inline__ void scan_kernel (Tables tb ,int E ) { 249auto & sh = * sycl::ext::oneapi::group_local_memory_for_overwrite < int [32 ]> ( 250 sycl::ext::oneapi::this_work_item::get_work_group < 3 > ()); 251const int e = 252 sycl::ext::oneapi::this_work_item::get_nd_item < 3 > ().get_local_id (2 ); 253const int c = e < E ?tb .cnt [e ] :0 ,nt = (c + kTileRows - 1 ) /kTileRows ; 254int rows = 0 ,tiles = 0 ; 255const int o = block_excl_sum (c ,& rows ,sh ),to = block_excl_sum (nt ,& tiles ,sh ); 256if (e < E ) { 257tb .off [e ]= o ; 258tb .fill [e ]= o ; 259tb .ts [e ]= to ; 260#pragma unroll 261for (int i = 0 ;i < nt ;++ i ) 262tb .tiles [to + i ]= sycl::int2 (e ,o + i * kTileRows ); 263 } 264if (e == 0 ) { 265tb .off [E ]= rows ; 266tb .ts [E ]= tiles ; 267 } 268} 269 270__dpct_inline__ void place_kernel (const int32_t * __restrict__ids ,int64_t n , 271int k ,int E ,Tables tb , 272int32_t * __restrict__slot , 273int32_t * __restrict__src ) { 274auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 275const int64_t i = 276 (int64_t )item_ct1 .get_group (2 )* item_ct1 .get_local_range (2 )+ 277item_ct1 .get_local_id (2 ); 278if (i >=n )return ; 279const int e = ids [i ]; 280if ((unsigned )e >= (unsigned )E ) {slot [i ]= 0 ;return ; }// (the router never writes one) 281const int p = 282 dpct::atomic_fetch_add < sycl::access::address_space::generic_space > ( 283& tb .fill [e ],1 ); 284slot [i ]= p ; 285src [p ]= (int32_t ) (i /k ); 286} 287 288// ---- the expert products 289#if defined(DPCT_COMPATIBILITY_TEMP )&& DPCT_COMPATIBILITY_TEMP >=800 290__dpct_inline__ void cp16 (void * dst ,const void * src ) { 291#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ ) 292 asmvolatile ("cp.async.cg.shared.global [%0], [%1], 16;\n" ::"r" ( 293 (unsigned )__cvta_generic_to_shared (dst )), 294"l" (src )); 295#else 296* (((uint32_t * )(uintptr_t )(unsigned )__cvta_generic_to_shared (dst )))= 297* (((uint32_t * )(uintptr_t )src )); 298if (16 > 4 ) 299* (((uint32_t * )(uintptr_t )(unsigned )__cvta_generic_to_shared (dst ))+ 3001 )= * (((uint32_t * )(uintptr_t )src )+ 1 ); 301if (16 > 8 ) 302* (((uint32_t * )(uintptr_t )(unsigned )__cvta_generic_to_shared (dst ))+ 3032 )= * (((uint32_t * )(uintptr_t )src )+ 2 ); 304if (16 > 12 ) 305* (((uint32_t * )(uintptr_t )(unsigned )__cvta_generic_to_shared (dst ))+ 3063 )= * (((uint32_t * )(uintptr_t )src )+ 3 ); 307#endif 308} 309__dpct_inline__ void cp4 (void * dst ,const void * src ) { 310/* 311DPCT1053: Migration of device assembly code is not supported. 312*/ 313 asmvolatile ("cp.async.ca.shared.global [%0], [%1], 4;\n" ::"r" ( 314 (unsigned )__cvta_generic_to_shared (dst )), 315"l" (src )); 316} 317/* 318DPCT1026: The call to "cp.async.commit_group; 319" was removed because current "cp.async" is migrated to synchronous copy 320operation. You may need to adjust the code to tune the performance. 321*/ 322#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ ) 323__device__ __forceinline__ void cp_commit () { 324 asmvolatile ("cp.async.commit_group;\n" ::); 325#else 326__device__ __forceinline__ void cp_commit () { 327#endif } 328/* 329DPCT1026: The call to "cp.async.wait_group %0; 330" was removed because current "cp.async" is migrated to synchronous copy 331operation. You may need to adjust the code to tune the performance. 332*/ 333#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ ) 334template < int N > __device__ __forceinline__ void cp_wait () { 335 asmvolatile ("cp.async.wait_group %0;\n" ::"n" (N )); 336#else 337template < int N > __device__ __forceinline__ void cp_wait () { 338#endif } 339// d = A (16 x 32 s8, row) * B (32 x 8 s8, col), int32, from zero 340__dpct_inline__ void mma_s8 (int (& d )[4 ],const uint32_t (& a )[4 ],uint32_t b0 , 341uint32_t b1 ) { 342#if defined(__SYCL_DEVICE_ONLY__ )&& defined(__NVPTX__ ) 343 asm("mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 " 344"{%0,%1,%2,%3}, " 345"{%4,%5,%6,%7}, {%8,%9}, {%10,%10,%10,%10};\n" 346 :"=r" (d [0 ]),"=r" (d [1 ]),"=r" (d [2 ]),"=r" (d [3 ]) 347 :"r" (a [0 ]),"r" (a [1 ]),"r" (a [2 ]),"r" (a [3 ]),"r" (b0 ),"r" (b1 ), 348"r" (0 )); 349#else 350 { 351volatile void * d_mat_frag_ct1 [4 ]= {& d [0 ],& d [1 ],& d [2 ],& d [3 ]}; 352 sycl::vec < uint32_t ,4 > a_mat_frag_ct1 (a [0 ],a [1 ],a [2 ],a [3 ]); 353 sycl::vec < uint32_t ,2 > b_mat_frag_ct1 (b0 ,b1 ); 354 sycl::vec < int32_t ,4 > c_mat_frag_ct1 (0 ,0 ,0 ,0 ); 355 dpct::experimental::matrix::mma < 16 ,8 ,32 ,int8_t ,int32_t > ( 356reinterpret_cast < volatile void **> (d_mat_frag_ct1 ), 357& a_mat_frag_ct1 ,& b_mat_frag_ct1 ,& c_mat_frag_ct1 ); 358 } 359#endif 360} 361#endif 362 363// One work item = (tile of up to 64 routed rows of expert e, a block of 256 weight rows); a persistent grid walks 364// the batch's items (their count is on the device). GU: gate/up rows of 128 features against the rows' tokens' 365// activations (`act` per token, 2560 values), SwiGLU, H to int8 per 32 features into `out` (per row, 640 values). 366// !GU: down rows (256 outputs) against H (`act` per row), FP32 into `dm` per row. 367// Warp (wf, wt): weight rows 64 wf.. as four m16 tiles (GU: a tile = 8 features, its gate rows as mma rows 0-7 and 368// its up rows as 8-15, so a lane holds gate and up of one feature: SwiGLU in registers), tile rows 16 wt.. as two n8. 369template < bool GU > 370/* 371DPCT1110: The total declared local variable size in device function 372expert_kernel exceeds 128 bytes and may cause high register pressure. Consult 373with your hardware vendor to find the total register size available and adjust 374the code, or use smaller sub-group size to avoid high register pressure. 375*/ 376__dpct_inline__ void expert_kernel ( 377const Batch b ,const Tables tb ,const uint8_t * __restrict__act , 378const int32_t * __restrict__src ,uint8_t * __restrict__out , 379float * __restrict__dm ,uint8_t * dpct_local ) { 380#if defined(DPCT_COMPATIBILITY_TEMP )&& DPCT_COMPATIBILITY_TEMP >=800 381auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item < 3 > (); 382constexpr int NKB = GU ?40 :10 ;// 64-weight blocks along K 383constexpr int NFB = GU ?5 :10 ;// weight-row blocks per tile 384constexpr int CODE_LD = GU ?640 :160 ;// code bytes per weight row 385constexpr size_t O_CODES = GU ?O_GU_CODES :O_D_CODES ,O_SC = GU ?O_GU_SC :O_D_SC ; 386constexpr int ACT_LD = NKB * AB ;// bytes per activation row: 3200 (a token), 800 (a row's H) 387constexpr unsigned M2 = 0x03030303u ; 388auto smem = (uint8_t * )dpct_local ; 389const sycl::half * wsc = 390 (const sycl::half * )smem ;// [256][NKB] the work item's weight scales 391uint8_t * stages = smem + WROWS * NKB * 2 ;// [STAGES] x {codes [256][16], act [64][AB]} 392int * srow = (int * ) (stages + STAGES * STAGE_BYTES );// [64] the activation row of each tile row 393 394const int tid = item_ct1 .get_local_id (2 ),lane = tid & 31 ,warp = tid >>5 , 395g = lane >>2 ,tig = lane & 3 ; 396const int wf = warp & 3 ,nb0 = 16 * (warp >>2 ); 397int ra [4 ],rb [4 ];// the local weight rows of mma rows g and g + 8 398#pragma unroll 399for (int i = 0 ;i < 4 ;++ i ) { 400if (GU ) {ra [i ]= 2 * (32 * wf + 8 * i + g );rb [i ]= ra [i ]+ 1 ; } 401else {ra [i ]= 64 * wf + 16 * i + g ;rb [i ]= ra [i ]+ 8 ; } 402 } 403const int t0 = tb .ts [b .e0 ],nwork = (tb .ts [b .e1 ]- t0 )* NFB ; 404for (int w = item_ct1 .get_group (2 );w < nwork ; 405w += item_ct1 .get_group_range (2 )) { 406const sycl::int2 tl = tb .tiles [t0 + w /NFB ]; 407const int fb = w %NFB ,e = tl .x (),row0 = tl .y (), 408nrows = sycl::min (kTileRows ,tb .off [e + 1 ]- row0 ); 409const uint8_t * blob = b .blob [e - b .e0 ]; 410const int rbase = fb * WROWS ; 411/* 412DPCT1118: SYCL group functions and algorithms must be encountered in 413converged control flow. You may need to adjust the code. 414*/ 415/* 416DPCT1065: Consider replacing sycl::nd_item::barrier() with 417sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 418better performance if there is no access to global memory. 419*/ 420item_ct1 .barrier ();// the previous item is done with the buffers 421if (tid < kTileRows ) { 422const int r = 423row0 + 424 sycl::min ( 425tid , 426nrows - 1 );// rows past the tile's end repeat its last one 427srow [tid ]= GU ?src [r ] :r ; 428 } 429for (int c = tid ;c < WROWS * 5 ;c += THREADS ) {// the scales: 5 x 16 B (GU) or 5 x 4 B a row 430const int r = c /5 ,q = c %5 ; 431if (GU )cp16 (smem + r * 80 + q * 16 ,blob + O_SC + (size_t ) (rbase + r )* 80 + q * 16 ); 432else cp4 (smem + r * 20 + q * 4 ,blob + O_SC + (size_t ) (rbase + r )* 20 + q * 4 ); 433 } 434/* 435DPCT1118: SYCL group functions and algorithms must be encountered in 436converged control flow. You may need to adjust the code. 437*/ 438/* 439DPCT1065: Consider replacing sycl::nd_item::barrier() with 440sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 441better performance if there is no access to global memory. 442*/ 443item_ct1 .barrier ();// srow 444auto load = [& ](int kb ) { 445uint8_t * st = stages + (kb %STAGES )* STAGE_BYTES ; 446if (tid < WROWS )cp16 (st + tid * 16 ,blob + O_CODES + (size_t ) (rbase + tid )* CODE_LD + kb * 16 ); 447const int a = tid - (THREADS - kTileRows * 5 );// the last 320 threads: 64 rows x 5 x 16 B 448if (a >=0 ) { 449const int r = a /5 ,q = a %5 ; 450cp16 (st + WROWS * 16 + r * AB + q * 16 ,act + (size_t )srow [r ]* ACT_LD + kb * AB + q * 16 ); 451 } 452 }; 453#pragma unroll 454for (int s = 0 ;s < STAGES - 1 ;++ s ) { 455if (s < NKB )load (s ); 456cp_commit (); 457 } 458// a warp whose rows are all past the tile's end only takes part in the loads 459const bool on0 = nb0 < nrows ,on1 = nb0 + 8 < nrows ; 460float acc [4 ][2 ][4 ]; 461#pragma unroll 462for (int i = 0 ;i < 4 ;++ i ) 463#pragma unroll 464for (int n = 0 ;n < 2 ;++ n ) 465#pragma unroll 466for (int q = 0 ;q < 4 ;++ q )acc [i ][n ][q ]= 0.0f ; 467for (int kb = 0 ;kb < NKB ;++ kb ) { 468cp_wait < STAGES - 2 > (); 469/* 470DPCT1118: SYCL group functions and algorithms must be 471encountered in converged control flow. You may need to adjust the 472code. 473*/ 474/* 475DPCT1065: Consider replacing sycl::nd_item::barrier() with 476sycl::nd_item::barrier(sycl::access::fence_space::local_space) for 477better performance if there is no access to global memory. 478*/ 479item_ct1 .barrier (); 480if (kb + STAGES - 1 < NKB )load (kb + STAGES - 1 ); 481cp_commit (); 482if (!on0 )continue ; 483const uint8_t * st = stages + (kb %STAGES )* STAGE_BYTES ; 484const uint8_t * sa = st + WROWS * 16 ; 485 sycl::uint2 bq [2 ][2 ];// [n8 tile][half]: the B registers 486 sycl::float4 sc [2 ] 487 [2 ];// [n8 tile][column 2 tig + c]: {d0, c0, d1, c1} 488#pragma unroll 489for (int n = 0 ;n < 2 ;++ n ) { 490if (n == 1 && !on1 )break ; 491const uint8_t * r = sa + (nb0 + 8 * n + g )* AB + 8 * tig ; 492bq [n ][0 ]= * (const sycl::uint2 * )r ; 493bq [n ][1 ]= * (const sycl::uint2 * )(r + 32 ); 494sc [n ][0 ]= 495* (const sycl::float4 * )(sa + (nb0 + 8 * n + 2 * tig )* AB + 49664 ); 497sc [n ][1 ]= 498* (const sycl::float4 * )(sa + 499 (nb0 + 8 * n + 2 * tig + 1 )* AB + 50064 ); 501 } 502#pragma unroll 503for (int i = 0 ;i < 4 ;++ i ) { 504const sycl::uint4 ca = * (const sycl::uint4 * )(st + ra [i ]* 16 ), 505cb = * (const sycl::uint4 * )(st + rb [i ]* 16 ); 506const float 507da = sycl::vec < sycl::half ,1 > (wsc [ra [i ]* NKB + kb ]) 508 .convert < float , 509 sycl::rounding_mode::automatic > ()[0 ], 510db = sycl::vec < sycl::half ,1 > (wsc [rb [i ]* NKB + kb ]) 511 .convert < float , 512 sycl::rounding_mode::automatic > ()[0 ]; 513const int s = 2 * tig ; 514const uint32_t a0 [4 ]= {(ca .x () >>s )& M2 , (cb .x () >>s )& M2 , 515 (ca .y () >>s )& M2 , (cb .y () >>s )& M2 }; 516const uint32_t a1 [4 ]= {(ca .z () >>s )& M2 , (cb .z () >>s )& M2 , 517 (ca .w () >>s )& M2 , (cb .w () >>s )& M2 }; 518#pragma unroll 519for (int n = 0 ;n < 2 ;++ n ) { 520if (n == 1 && !on1 )break ; 521int d0 [4 ],d1 [4 ]; 522mma_s8 (d0 ,a0 ,bq [n ][0 ].x (),bq [n ][0 ].y ()); 523mma_s8 (d1 ,a1 ,bq [n ][1 ].x (),bq [n ][1 ].y ()); 524#pragma unroll 525for (int q = 0 ;q < 4 ;++ q ) { 526const sycl::float4 z = sc [n ][q & 1 ]; 527const float u0 = sycl::bit_cast < float > (MAGIC + d0 [q ])+ 528z .y (), 529u1 = sycl::bit_cast < float > (MAGIC + d1 [q ])+ 530z .w (); 531acc [i ][n ][q ]= sycl::fma ( 532 (float )(q < 2 ?da :db ), 533 sycl::fma ((float )(z .z ()), (float )u1 ,z .x ()* u0 ), 534acc [i ][n ][q ]); 535 } 536 } 537 } 538 } 539if (!on0 )continue ; 540if (GU ) { 541// SwiGLU of feature 32 wf + 8 i + g (lane g of m tile i), tile rows nb0 + 8 n + 2 tig + c; H to int8 per 542// row over the warp's 32 features (= one 32-value half-block of the down product's K) 543const int blk = 4 * fb + wf ; 544#pragma unroll 545for (int n = 0 ;n < 2 ;++ n ) { 546if (n == 1 && !on1 )break ; 547#pragma unroll 548for (int c = 0 ;c < 2 ;++ c ) { 549float h [4 ],am = 0.0f ; 550#pragma unroll 551for (int i = 0 ;i < 4 ;++ i ) { 552const float gt = acc [i ][n ][c ],up = acc [i ][n ][2 + c ]; 553h [i ]= gt / (1.0f + sycl::native::exp (- gt ))* up ; 554am = sycl::fmax (am , sycl::fabs (h [i ])); 555 } 556#pragma unroll 557/* 558DPCT1108: '__shfl_xor_sync' was migrated with the 559experimental feature masked sub_group function which may not 560be supported by all compilers or runtimes. You may need to 561adjust the code. 562*/ 563for (int o = 4 ;o < 32 ;o <<=1 )am = sycl::fmax ( 564am , 565 dpct::experimental::permute_sub_group_by_xor ( 5660xffffffffu , 567 sycl::ext::oneapi::this_work_item::get_sub_group (), 568am ,o )); 569const float inv = am > 0.0f ?127.0f /am :0.0f ; 570int qv [4 ],sum = 0 ; 571#pragma unroll 572for (int i = 0 ;i < 4 ;++ i ) { 573qv [i ]= 574 sycl::vec < float ,1 > {(h [i ]* inv )} 575 .convert < int , sycl::rounding_mode::rte > ()[0 ]; 576sum += qv [i ]; 577 } 578#pragma unroll 579/* 580DPCT1108: '__shfl_xor_sync' was migrated with the 581experimental feature masked sub_group function which may not 582be supported by all compilers or runtimes. You may need to 583adjust the code. 584*/ 585for (int o = 4 ;o < 32 ;o <<=1 )sum += 586 dpct::experimental::permute_sub_group_by_xor ( 5870xffffffffu , 588 sycl::ext::oneapi::this_work_item::get_sub_group (), 589sum ,o ); 590const int r = nb0 + 8 * n + 2 * tig + c ; 591if (r < nrows ) { 592uint8_t * o = out + (size_t ) (row0 + r )* (10 * AB )+ (blk >>1 )* AB ; 593const int hh = blk & 1 ; 594#pragma unroll 595for (int i = 0 ;i < 4 ;++ i )o [32 * hh + perm32 (8 * i + g )]= (uint8_t ) (int8_t )qv [i ]; 596if (g == 0 )* (sycl::float2 * )(o + 64 + 8 * hh )= 597 sycl::float2 (am /127.0f ,- (MAGICF + (float )sum )); 598 } 599 } 600 } 601 }else { 602#pragma unroll 603for (int i = 0 ;i < 4 ;++ i ) 604#pragma unroll 605for (int n = 0 ;n < 2 ;++ n ) { 606if (n == 1 && !on1 )break ; 607#pragma unroll 608for (int q = 0 ;q < 4 ;++ q ) { 609const int r = nb0 + 8 * n + 2 * tig + (q & 1 ); 610if (r < nrows )dm [(size_t ) (row0 + r )* 2560 + rbase + (q < 2 ?ra [i ] :rb [i ])]= acc [i ][n ][q ]; 611 } 612 } 613 } 614 } 615#endif 616} 617 618#if defined(__HIPCC__ ) 619// ---- Aurora (S23): the same products on gfx11 (RDNA3 / RDNA3.5) matrix cores, v_wmma_i32_16x16x16_iu8 (wave32). 620// Fragments (gfx11): A lane l holds the 16 k of row l % 16 (lanes 16..31 repeat lanes 0..15), B lane l the 16 k of 621// column l % 16, C/D lane l holds D[2i + l / 16][l % 16], i = 0..7. A = 16 weight rows, B = 16 tile rows (tokens): 622// a lane's eight results are eight weight rows of ONE tile row, whose activation scales it already holds. 623// The K order of a 16-weight word (one k-step): A register t = (word >> 2t) & 0x03030303 holds weights t, t+4, t+8, 624// t+12, so k index p = 4t + b stands for weight 4b + t; the activations are stored in that order (act_pos). The 625// integer dot of each 32-value half (two k-steps) is exact and the epilogue is the CUDA kernel's, so the numbers are 626// those of expert_kernel (another K order inside exact integer sums). 627// No LDS staging: a wave reads its weight rows (16 B per row and 64-weight block) and its tile rows' activation 628// blocks (80 B) straight from global memory (L2: an expert's rows are read by every tile, a token's by every weight 629// block), one block ahead in registers; the work item's weight scales go to LDS once. 630#if defined(__gfx1100__ )|| defined(__gfx1101__ )|| defined(__gfx1102__ )|| defined(__gfx1150__ )|| defined(__gfx1151__ ) 631#define STRATA_FUSED_W11 1 632#else 633#define STRATA_FUSED_W11 0 634#endif 635typedef int w11_i4 __attribute__((ext_vector_type (4 ))); 636typedef int w11_i8 __attribute__((ext_vector_type (8 ))); 637constexpr int W_ROWS = 128 ;// weight rows per work item (GU: 64 features = one H block) 638constexpr int W_THREADS = 256 ;// 8 waves: 4 along the weight rows (32 each) x 2 along the tile (32) 639 640__device__ __forceinline__ w11_i8 wmma_iu8 (w11_i4 a ,w11_i4 b ,w11_i8 c ) { 641#if STRATA_FUSED_W11 642return __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32 (false,a , true,b ,c , false); 643#else 644__builtin_trap (); 645return c ; 646#endif 647} 648 649template < bool GU > 650__global__ void __launch_bounds__ (W_THREADS ) 651expert_w11_kernel (const Batch b ,const Tables tb ,const uint8_t * __restrict__act ,const int32_t * __restrict__src , 652uint8_t * __restrict__out ,float * __restrict__dm ) { 653#if STRATA_FUSED_W11 654constexpr int NKB = GU ?40 :10 ;// 64-weight blocks along K 655constexpr int NFB = (GU ?1280 :2560 ) /W_ROWS ;// work items per tile 656constexpr int CODE_LD = GU ?640 :160 ;// code bytes per weight row 657constexpr size_t O_CODES = GU ?O_GU_CODES :O_D_CODES ,O_SC = GU ?O_GU_SC :O_D_SC ; 658constexpr int ACT_LD = NKB * AB ;// bytes per activation row 659constexpr unsigned M2 = 0x03030303u ; 660__shared__ __align__ (16 )uint16_t wsc [W_ROWS * NKB ];// the item's weight scales (fp16), [row][kb] 661__shared__ int srow [kTileRows ]; 662__shared__ float hs [GU ?kTileRows :1 ][GU ?65 :1 ];// GU: H of the item's 64 features, [tile row][feature] 663const int tid = threadIdx .x ,lane = tid & 31 ,wave = tid >>5 ; 664const int wm = wave & 3 ,wn = wave >>2 ,l16 = lane & 15 ,hi = lane >>4 ; 665const int t0 = tb .ts [b .e0 ],nwork = (tb .ts [b .e1 ]- t0 )* NFB ; 666for (int w = blockIdx .x ;w < nwork ;w += gridDim .x ) { 667const int2 tl = tb .tiles [t0 + w /NFB ]; 668const int fb = w %NFB ,e = tl .x ,row0 = tl .y ,nrows = min (kTileRows ,tb .off [e + 1 ]- row0 ); 669const uint8_t * blob = b .blob [e - b .e0 ]; 670const int rbase = fb * W_ROWS ; 671__syncthreads ();// the previous item is done with srow / wsc / hs 672if (tid < kTileRows ) { 673const int r = row0 + min (tid ,nrows - 1 );// rows past the tile's end repeat its last one 674srow [tid ]= GU ?src [r ] :r ; 675 } 676 { 677const uint4 * s4 = reinterpret_cast < const uint4 *> (blob + O_SC + (size_t )rbase * NKB * 2 ); 678for (int i = tid ;i < W_ROWS * NKB /8 ;i += W_THREADS )reinterpret_cast < uint4 *> (wsc )[i ]= s4 [i ]; 679 } 680__syncthreads (); 681const bool on = 32 * wn < nrows ;// a wave past the tile's rows only stages 682float acc [2 ][2 ][8 ]; 683#pragma unroll 684for (int mt = 0 ;mt < 2 ;++ mt ) 685#pragma unroll 686for (int nt = 0 ;nt < 2 ;++ nt ) 687#pragma unroll 688for (int i = 0 ;i < 8 ;++ i )acc [mt ][nt ][i ]= 0.0f ; 689if (on ) { 690const uint8_t * arow [2 ]; 691const uint8_t * brow [2 ]; 692#pragma unroll 693for (int mt = 0 ;mt < 2 ;++ mt )arow [mt ]= blob + O_CODES + (size_t ) (rbase + 32 * wm + 16 * mt + l16 )* CODE_LD ; 694#pragma unroll 695for (int nt = 0 ;nt < 2 ;++ nt )brow [nt ]= act + (size_t )srow [32 * wn + 16 * nt + l16 ]* ACT_LD ; 696uint4 ac [2 ],bq [2 ][4 ]; 697float4 bs [2 ]; 698auto fetch = [& ](int kb ,uint4 (& a )[2 ],uint4 (& q )[2 ][4 ],float4 (& s )[2 ]) { 699#pragma unroll 700for (int mt = 0 ;mt < 2 ;++ mt )a [mt ]= * reinterpret_cast < const uint4 *> (arow [mt ]+ kb * 16 ); 701#pragma unroll 702for (int nt = 0 ;nt < 2 ;++ nt ) { 703const uint4 * p = reinterpret_cast < const uint4 *> (brow [nt ]+ kb * AB ); 704q [nt ][0 ]= p [0 ];q [nt ][1 ]= p [1 ];q [nt ][2 ]= p [2 ];q [nt ][3 ]= p [3 ]; 705s [nt ]= * reinterpret_cast < const float4 *> (brow [nt ]+ kb * AB + 64 ); 706 } 707 }; 708fetch (0 ,ac ,bq ,bs ); 709for (int kb = 0 ;kb < NKB ;++ kb ) { 710uint4 nac [2 ],nbq [2 ][4 ]; 711float4 nbs [2 ]; 712if (kb + 1 < NKB )fetch (kb + 1 ,nac ,nbq ,nbs ); 713w11_i8 c [2 ][2 ][2 ];// [half][mt][nt] 714#pragma unroll 715for (int h = 0 ;h < 2 ;++ h ) 716#pragma unroll 717for (int mt = 0 ;mt < 2 ;++ mt ) 718#pragma unroll 719for (int nt = 0 ;nt < 2 ;++ nt )c [h ][mt ][nt ]= w11_i8 {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; 720#pragma unroll 721for (int ks = 0 ;ks < 4 ;++ ks ) {// k-step = 16-weight word ks of the block 722w11_i4 A [2 ]; 723#pragma unroll 724for (int mt = 0 ;mt < 2 ;++ mt ) { 725const unsigned wd = ks == 0 ?ac [mt ].x :ks == 1 ?ac [mt ].y :ks == 2 ?ac [mt ].z :ac [mt ].w ; 726A [mt ]= w11_i4 {(int ) (wd & M2 ), (int ) ((wd >>2 )& M2 ), (int ) ((wd >>4 )& M2 ), 727 (int ) ((wd >>6 )& M2 )}; 728 } 729#pragma unroll 730for (int nt = 0 ;nt < 2 ;++ nt ) { 731const uint4 q = bq [nt ][ks ]; 732const w11_i4 B = w11_i4 {(int )q .x , (int )q .y , (int )q .z , (int )q .w }; 733#pragma unroll 734for (int mt = 0 ;mt < 2 ;++ mt )c [ks >>1 ][mt ][nt ]= wmma_iu8 (A [mt ],B ,c [ks >>1 ][mt ][nt ]); 735 } 736 } 737// the epilogue: per 64-weight block acc += d_w * (d_x0 * (dot0 - sum0) + d_x1 * (dot1 - sum1)) 738#pragma unroll 739for (int mt = 0 ;mt < 2 ;++ mt ) { 740float dw [8 ]; 741#pragma unroll 742for (int i = 0 ;i < 8 ;++ i ) { 743const int m = 32 * wm + 16 * mt + 2 * i + hi ; 744dw [i ]= __half2float (__ushort_as_half (wsc [m * NKB + kb ])); 745 } 746#pragma unroll 747for (int nt = 0 ;nt < 2 ;++ nt ) { 748const float4 z = bs [nt ]; 749#pragma unroll 750for (int i = 0 ;i < 8 ;++ i ) { 751const float u0 = __int_as_float (MAGIC + c [0 ][mt ][nt ][i ])+ z .y ; 752const float u1 = __int_as_float (MAGIC + c [1 ][mt ][nt ][i ])+ z .w ; 753acc [mt ][nt ][i ]= fmaf (dw [i ],fmaf (z .z ,u1 ,z .x * u0 ),acc [mt ][nt ][i ]); 754 } 755 } 756 } 757if (kb + 1 < NKB ) { 758#pragma unroll 759for (int mt = 0 ;mt < 2 ;++ mt )ac [mt ]= nac [mt ]; 760#pragma unroll 761for (int nt = 0 ;nt < 2 ;++ nt ) { 762bs [nt ]= nbs [nt ]; 763#pragma unroll 764for (int j = 0 ;j < 4 ;++ j )bq [nt ][j ]= nbq [nt ][j ]; 765 } 766 } 767 } 768 } 769if constexpr (GU ) { 770// SwiGLU: row 2f is feature f's gate (lanes 0..15), 2f + 1 its up (lanes 16..31) - the same (mt, i) 771if (on ) { 772#pragma unroll 773for (int mt = 0 ;mt < 2 ;++ mt ) 774#pragma unroll 775for (int nt = 0 ;nt < 2 ;++ nt ) 776#pragma unroll 777for (int i = 0 ;i < 8 ;++ i ) { 778const float up = __shfl_xor_sync (0xffffffffu ,acc [mt ][nt ][i ],16 ); 779const float gt = acc [mt ][nt ][i ]; 780if (hi == 0 )hs [32 * wn + 16 * nt + l16 ][16 * wm + 8 * mt + i ]= gt / (1.0f + __expf (- gt ))* up ; 781 } 782 } 783__syncthreads (); 784// H block fb (features 64 fb .. 64 fb + 63) to int8: a thread per (tile row, half) 785if (tid < 2 * kTileRows ) { 786const int r = tid >>1 ,hh = tid & 1 ; 787if (r < nrows ) { 788float am = 0.0f ; 789#pragma unroll 8 790for (int j = 0 ;j < 32 ;++ j )am = fmaxf (am ,fabsf (hs [r ][32 * hh + j ])); 791const float inv = am > 0.0f ?127.0f /am :0.0f ; 792uint8_t * o = out + (size_t ) (row0 + r )* (10 * AB )+ (size_t )fb * AB ; 793int sum = 0 ; 794uint32_t wd [8 ]= {}; 795#pragma unroll 796for (int j = 0 ;j < 32 ;++ j ) { 797const int qv = __float2int_rn (hs [r ][32 * hh + j ]* inv ); 798sum += qv ; 799const int p = act_pos (j ); 800wd [p >>2 ] |= (uint32_t ) (uint8_t ) (int8_t )qv << (8 * (p & 3 )); 801 } 802uint4 * o4 = reinterpret_cast < uint4 *> (o + 32 * hh ); 803o4 [0 ]= make_uint4 (wd [0 ],wd [1 ],wd [2 ],wd [3 ]); 804o4 [1 ]= make_uint4 (wd [4 ],wd [5 ],wd [6 ],wd [7 ]); 805* reinterpret_cast < float2 *> (o + 64 + 8 * hh )= make_float2 (am /127.0f ,- (MAGICF + (float )sum )); 806 } 807 } 808 }else if (on ) { 809#pragma unroll 810for (int mt = 0 ;mt < 2 ;++ mt ) 811#pragma unroll 812for (int nt = 0 ;nt < 2 ;++ nt ) { 813const int r = 32 * wn + 16 * nt + l16 ; 814if (r < nrows ) { 815float * d = dm + (size_t ) (row0 + r )* 2560 + rbase + 32 * wm + 16 * mt + hi ; 816#pragma unroll 817for (int i = 0 ;i < 8 ;++ i )d [2 * i ]= acc [mt ][nt ][i ]; 818 } 819 } 820 } 821 } 822#else 823__builtin_trap (); 824#endif 825} 826#endif // __HIPCC__ 827 828unsigned blocks (int64_t n ,int per ) {return (unsigned ) ((n + per - 1 ) /per ); } 829 830struct DevInfo { 831bool done = false; 832int cc = 0 ,sms = 0 ,occ_gu = 0 ,occ_d = 0 ; 833}; 834std::mutex g_mu ; 835DevInfo g_dev [32 ]; 836 837#if defined(__HIPCC__ ) 838// Resident blocks per multiprocessor for the gfx11 WMMA prompt-expert kernels' persistent grids. HIP counts a gfx11 839// WGP (two CUs, which these kernels use as one in the default WGP mode) as one multiprocessor, and the occupancy query 840// answers 1 block for these kernels where 2 run side by side - so the grid was half the GPU's room. gfx1151 (20 841// WGPs), 8192 tokens x top 10, IQ3_S / IQ4_NL, one layer: 1 block per WGP 60.7 ms, 2 44.7, 3 49.9, 4 45.2 (the same 842// work items and arithmetic: the results do not change). STRATA_PF_OCC=N sets the blocks per WGP (an experiment knob). 843static int wgp_blocks (int occ ) { 844static const int env = [] { 845const char * v = std::getenv ("STRATA_PF_OCC" ); 846return v != nullptr ? std::atoi (v ) :0 ; 847 }(); 848return env > 0 ?env :2 * std::max (occ ,1 ); 849} 850#endif 851const DevInfo & dev_info ()try { 852int dev = 0 ; 853dev = dpct::get_current_device_id (); 854 std::lock_guard < std::mutex > lk (g_mu ); 855DevInfo & d = g_dev [dev & 31 ]; 856if (d .done )return d ; 857d .done = true; 858#if defined(STRATA_HIP_GFX906 ) 859// gfx906 reports compute capability 9.0 through HIP, but the mma.sync bodies above are CUDA sm_80+ only and 860// empty in a hipcc build: never available here (the prompt path keeps its own expert GEMMs) 861return d ; 862#elif defined(__HIPCC__ ) 863 {// gfx11 (RDNA3 / RDNA3.5) with this build's code for it: the WMMA kernels 864cudaDeviceProp prop ; 865cudaDeviceGetAttribute (& d .sms ,cudaDevAttrMultiProcessorCount ,dev ); 866hipFuncAttributes fa {}; 867if (cudaGetDeviceProperties (& prop ,dev )!= cudaSuccess || std::strncmp (prop .gcnArchName ,"gfx11" ,5 )!= 0 || 868hipFuncGetAttributes (& fa ,reinterpret_cast < const void *> (expert_w11_kernel < true> ))!= hipSuccess ) { 869cudaGetLastError (); 870return d ; 871 } 872if (hipOccupancyMaxActiveBlocksPerMultiprocessor (& d .occ_gu ,expert_w11_kernel < true> ,W_THREADS ,0 )!= hipSuccess || 873hipOccupancyMaxActiveBlocksPerMultiprocessor (& d .occ_d ,expert_w11_kernel < false> ,W_THREADS ,0 )!= hipSuccess ) 874d .occ_gu = d .occ_d = 0 ; 875d .cc = d .occ_gu >=1 && d .occ_d >=1 ?80 :0 ; 876cudaGetLastError (); 877return d ; 878 } 879#else 880int major = 0 ,minor = 0 ; 881major = dpct::get_device (dev ).get_major_version (); 882minor = dpct::get_device (dev ).get_minor_version (); 883d .sms = dpct::get_device (dev ).get_max_compute_units (); 884d .cc = 10 * major + minor ; 885if (d .cc < 80 )return d ; 886// the device code of this card's arch must have the kernels (a build without an sm_80+ target has an empty body) 887 dpct::kernel_function_info fa {}; 888if (DPCT_CHECK_ERROR (dpct::get_kernel_function_info ( 889& fa , (const void * )expert_kernel < true> ))!= 0 || 890fa .ptxVersion < 80 ) { 891/* 892DPCT1026: The call to cudaGetLastError was removed because this 893functionality is redundant in SYCL. 894*/ 895d .cc = 0 ; 896return d ; 897 } 898/* 899DPCT1027: The call to cudaFuncSetAttribute was replaced with 0 because 900SYCL currently does not support corresponding setting. 901*/ 902ck (0 ,"smem gu" ); 903/* 904DPCT1027: The call to cudaFuncSetAttribute was replaced with 0 because 905SYCL currently does not support corresponding setting. 906*/ 907ck (0 ,"smem down" ); 908/* 909DPCT1111: Please verify the input arguments of 910"dpct::experimental::calculate_max_active_wg_per_xecore" base on the target 911function "expert_kernel<true>". 912*/ 913 dpct::experimental::calculate_max_active_wg_per_xecore ( 914& d .occ_gu ,THREADS , 915smem_bytes (true)+ 916dpct_placeholder /* total share local memory size */ ); 917/* 918DPCT1111: Please verify the input arguments of 919"dpct::experimental::calculate_max_active_wg_per_xecore" base on the target 920function "expert_kernel<false>". 921*/ 922 dpct::experimental::calculate_max_active_wg_per_xecore ( 923& d .occ_d ,THREADS , 924smem_bytes (false)+ 925dpct_placeholder /* total share local memory size */ ); 926if (d .occ_gu < 1 || d .occ_d < 1 )d .cc = 0 ;// does not fit this card: the MMQ path 927/* 928DPCT1026: The call to cudaGetLastError was removed because this 929functionality is redundant in SYCL. 930*/ 931#endif 932return d ; 933} 934catch (sycl::exception const & exc ) { 935 std::cerr <<exc .what () <<"Exception caught at file:" <<__FILE__ 936 <<", line:" <<__LINE__ << std::endl ; 937 std::exit (1 ); 938} 939 940}// namespace 941 942bool built () {return true; } 943// the blob layout and the tile loops below are Flash-Next's 2560 x 640 (O_D_CODES = 1280 * 640 ...): another compiled shape never takes this path 944bool available () {return strata::core::kCompiledGeometry .n_embd == 2560 && strata::core::kCompiledGeometry .n_ff == 640 && dev_info ().cc >=80 ; } 945// 0.1.36: on by default for the Q2_0 pack (+13-23% prompts on an RTX 5070; teacher-forced against the FP16 path as 946// close as MMQ at 8K and closer at 32K: phaseA-tests s18-tf); STRATA_PF_FUSED=0 keeps MMQ. The native packs' kernels 947// (moe_fused_iq) stay opt-in: STRATA_PF_FUSED=1 (requested()). 948bool enabled () { 949static const bool env = [] { 950const char * v = std::getenv ("STRATA_PF_FUSED" ); 951#if defined(__HIPCC__ ) 952return v != nullptr && v [0 ]== '1' ;// Aurora: the gfx11 WMMA kernels are opt-in (STRATA_PF_FUSED=1) 953#else 954return v == nullptr || v [0 ]!= '0' ; 955#endif 956 }(); 957return env && available (); 958} 959bool requested () { 960static const bool env = [] { 961const char * v = std::getenv ("STRATA_PF_FUSED" ); 962return v != nullptr && v [0 ]== '1' ; 963 }(); 964return env && available (); 965} 966 967size_t act_bytes (int64_t rows ,int64_t cols ) {return (size_t )rows * (size_t ) (cols /64 )* AB ; } 968size_t group_bytes (int64_t n ,int n_expert ) { 969return tiles_at (n_expert )+ 970 ((size_t )(n + kTileRows - 1 ) /kTileRows + (size_t )n_expert )* 971sizeof (sycl::int2 ); 972} 973 974void quantize_act (const float * x ,int64_t rows ,int64_t cols ,void * xa ,void * stream ) { 975if (rows <=0 )return ; 976const int64_t nblk = rows * (cols /64 ); 977 { 978auto exp_props = sycl::ext::oneapi::experimental::properties { 979 sycl::ext::oneapi::experimental::use_root_sync }; 980 981 strata::kprof::kp (strata::q_of (stream ),__FILE__ ,__func__ ,__LINE__ ) 982-> template parallel_for < dpct_kernel_name < class quant_act_kernel_da6c97 >> ( 983 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (nblk ,8 ))* 984 sycl::range (1 ,1 ,256 ), 985 sycl::range (1 ,1 ,256 )), 986exp_props , 987 [= ](sycl::nd_item < 3 > item_ct1 ) 988 [[sycl ::reqd_sub_group_size (32 )]] { 989quant_act_kernel (x ,nblk , (uint8_t * )xa ); 990 }); 991 } 992/* 993DPCT1010: SYCL uses exceptions to report errors and does not use the 994error codes. The cudaGetLastError function call was replaced with 0. You 995need to rewrite this code. 996*/ 997ck (0 ,"quantize_act" ); 998} 999 1000void group (const int32_t * ids ,int64_t n ,int k ,int n_expert ,void * scratch ,int32_t * slot ,int32_t * src , 1001void * stream ) { 1002if (n_expert < 1 || n_expert > 1024 ) { 1003 std::fprintf (stderr ,"prefill fused experts: %d experts (the grouping takes 1 to 1024)\n" ,n_expert ); 1004 std::exit (1 ); 1005 } 1006const dpct::queue_ptr s = strata::q_of (stream ); 1007const Tables tb = tables (scratch ,n_expert ); 1008ck (DPCT_CHECK_ERROR (s -> memset (tb .cnt ,0 , (size_t )n_expert * 4 )), 1009"group memset" ); 1010if (n > 0 ) { 1011const unsigned nb = std::min < unsigned > (blocks (n ,1024 ),4u * (unsigned ) std::max (dev_info ().sms ,1 )); 1012/* 1013DPCT1049: The work-group size passed to the SYCL kernel may exceed 1014the limit. To get the device limit, query 1015info::device::max_work_group_size. Adjust the work-group size if needed. 1016*/ 1017 { 1018auto exp_props = sycl::ext::oneapi::experimental::properties { 1019 sycl::ext::oneapi::experimental::use_root_sync }; 1020 1021 strata::kprof::kp (s ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & cgh ) { 1022 sycl::local_accessor < uint8_t ,1 > dpct_local_acc_ct1 ( 1023 sycl::range ((size_t )n_expert * 4 ),cgh ); 1024 1025cgh .parallel_for < 1026dpct_kernel_name < class count_kernel_9919e0 >> ( 1027 sycl::nd_range < 3 > (sycl::range (1 ,1 ,nb )* 1028 sycl::range (1 ,1 ,1024 ), 1029 sycl::range (1 ,1 ,1024 )), 1030exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 1031count_kernel (ids ,n ,n_expert ,tb .cnt , 1032dpct_local_acc_ct1 1033 .get_multi_ptr < 1034 sycl::access::decorated::no > () 1035 .get ()); 1036 }); 1037 }); 1038 } 1039 } 1040/* 1041DPCT1049: The work-group size passed to the SYCL kernel may exceed the 1042limit. To get the device limit, query info::device::max_work_group_size. 1043Adjust the work-group size if needed. 1044*/ 1045 { 1046auto exp_props = sycl::ext::oneapi::experimental::properties { 1047 sycl::ext::oneapi::experimental::use_root_sync }; 1048 1049 strata::kprof::kp (s ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class scan_kernel_7ed98e >> ( 1050 sycl::nd_range < 3 > ( 1051 sycl::range (1 ,1 , (unsigned )((n_expert + 31 ) /32 * 32 )), 1052 sycl::range (1 ,1 , (unsigned )((n_expert + 31 ) /32 * 32 ))), 1053exp_props , 1054 [= ](sycl::nd_item < 3 > item_ct1 ) 1055 [[sycl ::reqd_sub_group_size (32 )]] { 1056scan_kernel (tb ,n_expert ); 1057 }); 1058 } 1059if (n > 0 ) { 1060auto exp_props = sycl::ext::oneapi::experimental::properties { 1061 sycl::ext::oneapi::experimental::use_root_sync }; 1062 1063 strata::kprof::kp (s ,__FILE__ ,__func__ ,__LINE__ )-> template parallel_for < dpct_kernel_name < class place_kernel_cf784d >> ( 1064 sycl::nd_range < 3 > (sycl::range (1 ,1 ,blocks (n ,256 ))* 1065 sycl::range (1 ,1 ,256 ), 1066 sycl::range (1 ,1 ,256 )), 1067exp_props , [= ](sycl::nd_item < 3 > item_ct1 ) { 1068place_kernel (ids ,n ,k ,n_expert ,tb ,slot ,src ); 1069 }); 1070 } 1071/* 1072DPCT1010: SYCL uses exceptions to report errors and does not use the 1073error codes. The cudaGetLastError function call was replaced with 0. You 1074need to rewrite this code. 1075*/ 1076ck (0 ,"group" ); 1077} 1078 1079void experts (const Batch & b ,int n_expert ,int64_t n ,const void * scratch ,const void * xa ,const int32_t * src , 1080void * ha ,float * dm ,void * stream ) { 1081if (b .e1 <=b .e0 || n <=0 )return ; 1082const DevInfo & d = dev_info (); 1083const dpct::queue_ptr s = strata::q_of (stream ); 1084const Tables tb = tables (const_cast < void *> (scratch ),n_expert ); 1085// the most tiles the batch can have: every row in it, plus a partial tile per expert 1086const int64_t tiles = (n + kTileRows - 1 ) /kTileRows + (b .e1 - b .e0 ); 1087#if defined(__HIPCC__ ) 1088const unsigned g_gu = (unsigned ) std::min < int64_t > (tiles * (1280 /W_ROWS ), (int64_t )d .sms * wgp_blocks (d .occ_gu )); 1089const unsigned g_d = (unsigned ) std::min < int64_t > (tiles * (2560 /W_ROWS ), (int64_t )d .sms * wgp_blocks (d .occ_d )); 1090expert_w11_kernel < true> <<< g_gu , W_THREADS , 0 , s > >>( b , tb , ( const uint8_t * ) xa , src , ( uint8_t * ) ha , nullptr ); 1091expert_w11_kernel < false > <<< g_d , W_THREADS , 0 , s > >>( b , tb , ( const uint8_t * ) ha , src , nullptr , dm ); 1092#else 1093const unsigned g_gu = ( unsigned ) std::min < int64_t > ( tiles * 5 , ( int64_t ) d . sms * d . occ_gu ); 1094const unsigned g_d = ( unsigned ) std::min < int64_t > ( tiles * 10 , ( int64_t ) d . sms * d . occ_d ); 1095/* 1096DPCT1049: The work-group size passed to the SYCL kernel may exceed the 1097limit. To get the device limit, query info::device::max_work_group_size. 1098Adjust the work-group size if needed. 1099*/ 1100{ 1101auto exp_props = sycl::ext::oneapi::experimental:: properties { 1102sycl::ext::oneapi::experimental:: use_root_sync }; 1103dpct::has_capability_or_fail( s -> get_device(), {sycl::aspect:: fp16 }); 1104 1105strata::kprof:: kp ( s , __FILE__ , __func__ , __LINE__ ) -> submit([ & ](sycl:: handler & cgh ) { 1106sycl:: local_accessor < uint8_t , 1 > dpct_local_acc_ct1 ( 1107sycl::range(smem_bytes(true)), cgh ); 1108 1109cgh .parallel_for < dpct_kernel_name < class expert_kernel_f38977 , 1110dpct_kernel_scalar < true >>> ( 1111sycl::nd_range < 3 > (sycl::range( 1 , 1 , g_gu ) * 1112sycl::range( 1 , 1 , THREADS ), 1113sycl::range( 1 , 1 , THREADS )), 1114exp_props , 1115[ = ](sycl:: nd_item < 3 > item_ct1 ) 1116[[ sycl :: reqd_sub_group_size ( 32 )]] { 1117expert_kernel < true > ( 1118b , tb , ( const uint8_t * ) xa , src , ( uint8_t * ) ha , 1119nullptr , 1120dpct_local_acc_ct1 1121.get_multi_ptr < sycl::access::decorated:: no > () 1122.get()); 1123}); 1124}); 1125} 1126/* 1127DPCT1049: The work-group size passed to the SYCL kernel may exceed the 1128limit. To get the device limit, query info::device::max_work_group_size. 1129Adjust the work-group size if needed. 1130*/ 1131{ 1132auto exp_props = sycl::ext::oneapi::experimental:: properties { 1133sycl::ext::oneapi::experimental:: use_root_sync }; 1134dpct::has_capability_or_fail( s -> get_device(), {sycl::aspect:: fp16 }); 1135 1136strata::kprof:: kp ( s , __FILE__ , __func__ , __LINE__ ) -> submit([ & ](sycl:: handler & cgh ) { 1137sycl:: local_accessor < uint8_t , 1 > dpct_local_acc_ct1 ( 1138sycl::range(smem_bytes(false)), cgh ); 1139 1140cgh .parallel_for < dpct_kernel_name < class expert_kernel_e2d106 , 1141dpct_kernel_scalar < false >>> ( 1142sycl::nd_range < 3 > (sycl::range( 1 , 1 , g_d ) * 1143sycl::range( 1 , 1 , THREADS ), 1144sycl::range( 1 , 1 , THREADS )), 1145exp_props , 1146[ = ](sycl:: nd_item < 3 > item_ct1 ) 1147[[ sycl :: reqd_sub_group_size ( 32 )]] { 1148expert_kernel < false > ( 1149b , tb , ( const uint8_t * ) ha , src , nullptr , dm , 1150dpct_local_acc_ct1 1151.get_multi_ptr < sycl::access::decorated:: no > () 1152.get()); 1153}); 1154}); 1155} 1156#endif 1157/* 1158DPCT1010: SYCL uses exceptions to report errors and does not use the 1159error codes. The cudaGetLastError function call was replaced with 0. You 1160need to rewrite this code. 1161*/ 1162ck( 0 , "experts" ); 1163} 1164 1165} // namespace strata::prefill::fused