okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
86bed1c92
main
1// 151-b70: fused int8 DPAS expert products (ESIMD, Battlemage) - see include/strata/prefill/esimd_moe.hpp and 2// docs/INTEL_INT8_EXPERTS.md (the behaviour note). 3// 4// * Tile. One hardware thread owns 16 output rows (a DPAS N tile; for gate/up the 16 virtual rows alternate gate/up) and up to 5// MT x 8 activation rows (MT = 2, 4 or 8, picked per group from the experts' mean row count). It walks K in steps of 32: 6// decode the 16 weight rows' 32 values into the B operand (registers only), then for each of its row tiles one s8 DPAS 8x16x32 7// into a zeroed int32 tile, converted to float and scaled by (weight scale of the row and 32-block) x (activation scale of the 8// row and 32-block) into a float accumulator. There is no shared memory and no barrier. 9// * Decode once per tile: the B operand is decoded once per thread and applied to all its MT row tiles. An expert with more rows 10// than 8 x MT is split over row-block threads, each decoding again. A thread's accumulators are MT tiles of 512 bytes in an 11// 8 KB register file (256 GRF of 32 bytes): MT 12, or two output tiles per thread, spilled (3-15 KB) and ran 2-5x slower. 12// * Scales. Activations: q8_1 as llama.cpp (d = amax / 127, q = round half away from zero), one float per row and 32 of K. 13// Weights: IQ3_S d (1 + 2 s), IQ3_XXS d (2 s + 1) / 4, IQ2_S d (2 s + 1) / 8 per 16 (two DPAS per 32 with the other half 14// zeroed), IQ4_NL d per 32, Q2_0 d per 64; the int8 values are the codebook entries with their signs, never rounded. The int32 15// dot product of a 32-block is exact and exactly convertible to float. The float accumulation order is fixed (K ascending). 16// * Edge cases. Rows are padded to tiles of 8 with zeros (stores are masked to the real rows); a group of up to 16 experts per 17// launch; K a multiple of 256 (IQ3_S / IQ3_XXS / IQ2_S) / 64 (Q2_0) / 32 (IQ4_NL); output rows a multiple of 16. Blob reads 18// never go past a matrix end. 19// * Fallback. available() is false without the s8 8x16x32 DPAS (a non-Battlemage device) and the caller keeps the dequantize + 20// oneMKL path; gate_up()/down() return false for a type / shape they do not handle. 21// * ESIMD on this compiler (oneAPI 2026.1, bmg-g31): a dynamic index into the kernel-argument arrays, and `/` or `%` by a run-time 22// value, gave wrong values / garbage pointers (the first weight read faulted the GPU); the kernels use constant-index reads 23// (pick_expert) and 2-D grids. gather<T, N, VS> with VS > 1 is not available on Battlemage. 24#include <sycl/sycl.hpp> 25#include "strata/kprof.hpp" 26#include <sycl/ext/intel/esimd.hpp> 27#include <sycl/ext/intel/experimental/grf_size_properties.hpp> 28#include <sycl/ext/oneapi/matrix/matrix.hpp> 29#include <algorithm> 30#include <cstdio> 31#include <cstdlib> 32#include <cstring> 33#include <mutex> 34#include <type_traits> 35#include <string> 36#include <vector> 37#include <unordered_map> 38#include "strata/sycl_queue.hpp" 39#include "strata/prefill/esimd_moe.hpp" 40 41#ifdef STRATA_I8_NO_ESIMD 42// an AOT build for a device without the s8 8x16x32 DPAS (Alchemist, Xe-HPC): no ESIMD kernels, available() is false 43namespace strata::prefill::i8 { 44void make_layout (Layout & lo ,const int32_t * cnt ,int n ) { 45lo .n = n ; 46int r = 0 ,t = 0 ; 47for (int i = 0 ;i < n ;++ i ) {lo .cnt [i ]= cnt [i ];lo .row0 [i ]= r ;lo .tile0 [i ]= t ;r += cnt [i ];t += (cnt [i ]+ 7 ) /8 ; } 48lo .row0 [n ]= r ; 49lo .tile0 [n ]= t ; 50} 51bool available (void * ) {return false; } 52bool battlemage (void * ) {return false; } 53bool any_battlemage () {return false; } 54size_t xq_bytes (const Layout & lo ,int K ) {return (size_t )lo .tile0 [lo .n ]* 8 * K ; } 55size_t dx_bytes (const Layout & lo ,int K ) {return (size_t )lo .tile0 [lo .n ]* (K /32 )* 8 * sizeof (float ); } 56bool workspace (const Layout & ,int ,int ,Workspace & ,void * ) {return false; } 57void quantize_rows (const uint16_t * ,const Layout & ,int ,int8_t * ,float * ,void * ) {} 58bool gate_up (int ,const uint8_t * const * ,size_t ,int ,const Layout & ,int ,const int8_t * ,const float * ,float * ,void * ) {return false; } 59void swiglu_quantize (const float * ,const Layout & ,int ,int8_t * ,float * ,void * ) {} 60bool down (int ,const uint8_t * const * ,size_t ,int ,const Layout & ,int ,const int8_t * ,const float * ,float * ,void * ) {return false; } 61}// namespace strata::prefill::i8 62#else 63namespace strata::prefill::i8 { 64namespace { 65#define GGML_TABLE_BEGIN (type ,name ,size ) static const type name[size] = { 66#define GGML_TABLE_END () }; 67#include "iq_tables.inc" 68#undef GGML_TABLE_BEGIN 69#undef GGML_TABLE_END 70 71using namespace sycl::ext::intel::esimd ; 72namespace xmx= sycl::ext::intel::esimd::xmx; 73using half = sycl::half ; 74 75// ggml types handled 76constexpr int T_IQ3XXS = 18 ,T_IQ4NL = 20 ,T_IQ3S = 21 ,T_IQ2S = 22 ,T_Q2_0 = 42 ; 77template < int F > constexpr int blk_bytes () {return F == T_IQ3S ?110 :F == T_IQ3XXS ?98 :F == T_IQ2S ?82 :F == T_IQ4NL ?18 :18 ; } 78constexpr int block_bytes_of (int f ) {return f == T_IQ3S ?110 :f == T_IQ3XXS ?98 :f == T_IQ2S ?82 :18 ; } 79constexpr int block_vals_of (int f ) {return (f == T_IQ3S || f == T_IQ3XXS || f == T_IQ2S ) ?256 :f == T_Q2_0 ?64 :32 ; } 80 81// One device table buffer (uint32 units): the codebooks the decoders gather from. 82constexpr int O_IQ3S = 0 ;// iq3s_grid[512] 83constexpr int O_IQ3XXS = 512 ;// iq3xxs_grid[256] 84constexpr int O_IQ2S = 768 ;// iq2s_grid[1024] as 2048 dwords (low dword of an entry first) 85constexpr int O_KSIGNS = 2816 ;// ksigns_iq2xs[128], one byte in the low byte of each dword 86constexpr int O_IQ4 = 2944 ;// [256]: byte b -> kvalues_iq4nl[b & 15] in byte 0, kvalues_iq4nl[b >> 4] in byte 1 87constexpr int O_Q2 = 3200 ;// [256]: byte b -> its four 2-bit codes minus 1, as four int8 (Q2_0 values) 88constexpr int N_TABLE = 3456 ; 89 90uint32_t * tables (sycl::queue & q ) { 91static std::mutex mu ; 92static std::unordered_map < sycl::device ,uint32_t *> per_dev ;// a layer split has one prompt path per card 93 std::lock_guard < std::mutex > lk (mu ); 94uint32_t *& t = per_dev [q .get_device ()]; 95if (!t ) { 96 std::vector < uint32_t > h (N_TABLE ,0 ); 97for (int i = 0 ;i < 512 ;++ i )h [O_IQ3S + i ]= iq3s_grid [i ]; 98for (int i = 0 ;i < 256 ;++ i )h [O_IQ3XXS + i ]= iq3xxs_grid [i ]; 99for (int i = 0 ;i < 1024 ;++ i ) {h [O_IQ2S + 2 * i ]= (uint32_t )iq2s_grid [i ];h [O_IQ2S + 2 * i + 1 ]= (uint32_t ) (iq2s_grid [i ] >>32 ); } 100for (int i = 0 ;i < 128 ;++ i )h [O_KSIGNS + i ]= ksigns_iq2xs [i ]; 101for (int b = 0 ;b < 256 ;++ b ) { 102h [O_IQ4 + b ]= (uint32_t ) (uint8_t )kvalues_iq4nl [b & 15 ] | ((uint32_t ) (uint8_t )kvalues_iq4nl [b >>4 ] <<8 ); 103uint32_t q2 = 0 ; 104for (int k = 0 ;k < 4 ;++ k )q2 |= (uint32_t ) (uint8_t ) (int8_t ) (((b >> (2 * k ))& 3 )- 1 ) << (8 * k ); 105h [O_Q2 + b ]= q2 ; 106 } 107uint32_t * d = sycl::malloc_device < uint32_t > (N_TABLE ,q ); 108q .memcpy (d ,h .data (),N_TABLE * 4 ); 109q .wait (); 110t = d ; 111 } 112return t ; 113} 114 115// unaligned 32-bit read at a 2-byte aligned byte offset (per lane) through two dword gathers 116SYCL_ESIMD_FUNCTION inline simd < uint32_t ,16 > ld32 (const uint8_t * base ,simd < uint32_t ,16 > off ) { 117simd < uint32_t ,16 > a = off & ~3u ; 118simd < uint32_t ,16 > lo = gather < uint32_t ,16 > (reinterpret_cast < const uint32_t *> (base ),a ); 119simd < uint32_t ,16 > hi = gather < uint32_t ,16 > (reinterpret_cast < const uint32_t *> (base ),a + 4u ); 120simd < uint32_t ,16 > sh = (off & 2u ) <<3 ;// 0 or 16 121return (lo >>sh ) | ((hi <<16 ) << (16u - sh )); 122} 123// the same with the second dword clamped to byte offset `lim` (the last dword of the matrix): the down matrix ends the expert blob, and a block 124// whose last field is not dword aligned would otherwise read up to 3 bytes past it 125SYCL_ESIMD_FUNCTION inline simd < uint32_t ,16 > ld32c (const uint8_t * base ,simd < uint32_t ,16 > off ,uint32_t lim ) { 126simd < uint32_t ,16 > a = off & ~3u ; 127simd < uint32_t ,16 > a2 = a + 4u ; 128a2 .merge (simd < uint32_t ,16 > (lim ),a2 > lim ); 129simd < uint32_t ,16 > lo = gather < uint32_t ,16 > (reinterpret_cast < const uint32_t *> (base ),a ); 130simd < uint32_t ,16 > hi = gather < uint32_t ,16 > (reinterpret_cast < const uint32_t *> (base ),a2 ); 131simd < uint32_t ,16 > sh = (off & 2u ) <<3 ; 132return (lo >>sh ) | ((hi <<16 ) << (16u - sh )); 133} 134SYCL_ESIMD_FUNCTION inline simd < uint32_t ,16 > ld16 (const uint8_t * base ,simd < uint32_t ,16 > off ) { 135simd < uint32_t ,16 > a = off & ~3u ; 136simd < uint32_t ,16 > lo = gather < uint32_t ,16 > (reinterpret_cast < const uint32_t *> (base ),a ); 137return (lo >> ((off & 2u ) <<3 ))& 0xffffu ; 138} 139SYCL_ESIMD_FUNCTION inline simd < float ,16 > half_of (simd < uint32_t ,16 > h16 ) { 140simd < uint16_t ,16 > lo = h16 .template bit_cast_view < uint16_t > ().template select < 16 ,2 > (0 ); 141simd < half ,16 > hh = lo .template bit_cast_view < half > (); 142simd < float ,16 > f = hh ; 143return f ; 144} 145SYCL_ESIMD_FUNCTION inline simd < int ,128 > dpas_s8 (simd < uint32_t ,128 > b ,simd < int ,64 > a ) { 146simd < int ,128 > bi = b .template bit_cast_view < int > (); 147return xmx::dpas < 8 ,8 ,int ,int ,int ,int , xmx::dpas_argument_type::s8 , xmx::dpas_argument_type::s8 > (simd < int ,128 > (0 ),bi ,a ); 148} 149// sign nibble -> four negation bytes: (g ^ mask) + m negates the bytes whose sign bit is set (g is never 0xFF per byte) 150SYCL_ESIMD_FUNCTION inline simd < uint32_t ,16 > apply_signs (simd < uint32_t ,16 > g ,simd < uint32_t ,16 > nib ) { 151simd < uint32_t ,16 > m = ((nib * 0x00204081u )& 0x01010101u ); 152simd < uint32_t ,16 > mask = m * 255u ; 153return (g ^mask )+ m ; 154} 155 156struct GArgs { 157const uint8_t * blob [kMaxGroup ]; 158uint32_t sec_off ;// gate/up: byte offset of the up rows; down: byte offset of the down rows 159int n ; 160int row0 [kMaxGroup + 1 ]; 161int tile0 [kMaxGroup + 1 ]; 162int rb0 [kMaxGroup + 1 ];// first row block of expert i 163const int8_t * Xq ; 164const float * dx ; 165float * Y ; 166const uint32_t * tab ; 167int ntiles ;// output tiles of 16 168int nkb ;// K / 32 169int rowb ;// bytes per weight row 170int ldy ; 171uint32_t lim ;// byte offset of the last dword of the matrix the clamped reads (ld32c) stay inside 172int dbg ;// bring-up switches (STRATA_I8_DBG): 1 skip the decode, 2 skip the activation loads, 4 skip the stores, 8 skip the K loop 173}; 174 175// The expert a work item belongs to, with constant-index reads of the kernel argument arrays. 176struct Pick {int e ,row0 ,cnt ,tile0 ,rb0 ;const uint8_t * blob ; }; 177SYCL_ESIMD_FUNCTION inline Pick pick_expert (const GArgs & a ,int rbg ) { 178Pick p {0 ,a .row0 [0 ],a .row0 [1 ]- a .row0 [0 ],a .tile0 [0 ],a .rb0 [0 ],a .blob [0 ]}; 179#pragma unroll 180for (int i = 1 ;i < kMaxGroup ;++ i ) 181if (i < a .n && rbg >=a .rb0 [i ])p = Pick {i ,a .row0 [i ],a .row0 [i + 1 ]- a .row0 [i ],a .tile0 [i ],a .rb0 [i ],a .blob [i ]}; 182return p ; 183} 184 185int fill_args_mt (GArgs & a ,const Layout & lo ,int MT ) { 186int rb = 0 ; 187for (int i = 0 ;i < lo .n ;++ i ) { 188a .row0 [i ]= lo .row0 [i ]; 189a .tile0 [i ]= lo .tile0 [i ]; 190a .rb0 [i ]= rb ; 191const int ntl = (lo .cnt [i ]+ 7 ) /8 ; 192rb += (ntl + MT - 1 ) /MT ; 193 } 194a .row0 [lo .n ]= lo .row0 [lo .n ]; 195a .tile0 [lo .n ]= lo .tile0 [lo .n ]; 196a .rb0 [lo .n ]= rb ; 197a .n = lo .n ; 198return rb ; 199} 200 201template < int F ,int MT ,int G > struct TagGu ; 202template < int F ,int MT ,int G > struct TagDn ; 203 204// ------------------------------------------------------------------------------------------------ gate/up 205template < int F ,int MT ,int G > 206void launch_gu (sycl::queue & q ,const GArgs a ,int nrowblocks ) { 207auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < G > }; 208constexpr int NS = (F == T_IQ2S ) ?2 :1 ;// IQ2_S: two scales per 32 values -> two DPAS with the other half zeroed 209 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 210h .parallel_for < TagGu < F ,MT ,G >> (sycl::range < 2 > ((size_t )nrowblocks , (size_t )a .ntiles ),props , [= ](sycl::id < 2 > id )SYCL_ESIMD_KERNEL { 211const int rbg = (int )id [0 ],nt = (int )id [1 ]; 212const Pick pk = pick_expert (a ,rbg ); 213const int cnt = pk .cnt ; 214const int ntl = (cnt + 7 ) >>3 ; 215const int tl0 = (rbg - pk .rb0 )* MT ; 216const int nvalid = (ntl - tl0 )< MT ? (ntl - tl0 ) :MT ; 217const int tbase = pk .tile0 + tl0 ; 218const uint8_t * We = pk .blob ; 219const uint32_t * T = a .tab ; 220simd < uint32_t ,16 > lane (0 ,1 ); 221simd < uint32_t ,16 > ro = (lane & 1u )* a .sec_off + ((lane >>1 )+ (uint32_t ) (8 * nt ))* (uint32_t )a .rowb ; 222simd < float ,128 * MT > acc = 0.f ; 223const int * Xi = reinterpret_cast < const int *> (a .Xq ); 224for (int sb = 0 ;sb < ((a .dbg & 8 ) ?0 : (a .nkb >>3 ));++ sb ) { 225const uint32_t P = (uint32_t )sb * blk_bytes < F > (); 226const simd < float ,16 > d = half_of (ld16 (We ,ro + P )); 227simd < uint32_t ,16 > w0 = 0 ,w1 = 0 ,w2 = 0 ,w3 = 0 ;// sb-level fields 228if constexpr (F == T_IQ3S ) { 229w0 = ld32 (We ,ro + (P + 106 ));// scales 230w1 = ld32 (We ,ro + (P + 66 ));// qh 0..3 231w2 = ld32 (We ,ro + (P + 70 ));// qh 4..7 232 }else if constexpr (F == T_IQ2S ) { 233w0 = ld32 (We ,ro + (P + 74 ));// scales 0..3 234w1 = ld32 (We ,ro + (P + 78 ));// scales 4..7 235w2 = ld32 (We ,ro + (P + 66 ));// qh 0..3 236w3 = ld32 (We ,ro + (P + 70 ));// qh 4..7 237 } 238#pragma unroll 1 239for (int ib = 0 ;ib < 8 ;++ ib ) { 240const int kb = sb * 8 + ib ; 241simd < uint32_t ,128 > fr0 = 0x01010101u ,fr1 = 0u ; 242simd < float ,16 > sw0 = 1.f ,sw1 = 1.f ; 243if (!(a .dbg & 1 )) { 244if constexpr (F == T_IQ3S ) { 245simd < uint32_t ,16 > q0 = ld32 (We ,ro + (P + 2 + 8 * ib )); 246simd < uint32_t ,16 > q1 = ld32 (We ,ro + (P + 6 + 8 * ib )); 247simd < uint32_t ,16 > sg = ld32 (We ,ro + (P + 74 + 4 * ib )); 248simd < uint32_t ,16 > qhv = (((ib < 4 ) ?w1 :w2 ) >> (8 * (ib & 3 )))& 0xffu ; 249simd < uint32_t ,16 > scn = ((w0 >> (8 * (ib >>1 ))) >> (4 * (ib & 1 )))& 0xfu ; 250sw0 = d * simd < float ,16 > (simd < int ,16 > (scn * 2u + 1u )); 251#pragma unroll 252for (int r = 0 ;r < 8 ;++ r ) { 253simd < uint32_t ,16 > qsw = r < 4 ?q0 :q1 ; 254simd < uint32_t ,16 > idx = ((qsw >> (8 * (r & 3 )))& 0xffu ) | (((qhv >>r )& 1u ) <<8 ); 255simd < uint32_t ,16 > g = gather < uint32_t ,16 > (T + O_IQ3S ,idx <<2 ); 256simd < uint32_t ,16 > nib = ((sg >> (8 * (r >>1 ))) >> (4 * (r & 1 )))& 0xfu ; 257fr0 .template select < 16 ,1 > (16 * r )= apply_signs (g ,nib ); 258 } 259 }else if constexpr (F == T_IQ3XXS ) { 260simd < uint32_t ,16 > q0 = ld32 (We ,ro + (P + 2 + 8 * ib )); 261simd < uint32_t ,16 > q1 = ld32 (We ,ro + (P + 6 + 8 * ib )); 262simd < uint32_t ,16 > aux = ld32 (We ,ro + (P + 66 + 4 * ib )); 263sw0 = d * simd < float ,16 > (simd < int ,16 > ((aux >>28 )* 2u + 1u ))* 0.25f ; 264#pragma unroll 265for (int il = 0 ;il < 4 ;++ il ) { 266simd < uint32_t ,16 > sbyte = gather < uint32_t ,16 > (T + O_KSIGNS , ((aux >> (7 * il ))& 127u ) <<2 ); 267#pragma unroll 268for (int hh = 0 ;hh < 2 ;++ hh ) { 269const int r = 2 * il + hh ; 270simd < uint32_t ,16 > qsw = r < 4 ?q0 :q1 ; 271simd < uint32_t ,16 > idx = (qsw >> (8 * (r & 3 )))& 0xffu ; 272simd < uint32_t ,16 > g = gather < uint32_t ,16 > (T + O_IQ3XXS ,idx <<2 ); 273fr0 .template select < 16 ,1 > (16 * r )= apply_signs (g , (sbyte >> (4 * hh ))& 0xfu ); 274 } 275 } 276 }else if constexpr (F == T_IQ2S ) { 277simd < uint32_t ,16 > idw = ld32 (We ,ro + (P + 2 + 4 * ib )); 278simd < uint32_t ,16 > sgw = ld32 (We ,ro + (P + 34 + 4 * ib )); 279simd < uint32_t ,16 > qhb = (((ib < 4 ) ?w2 :w3 ) >> (8 * (ib & 3 )))& 0xffu ; 280simd < uint32_t ,16 > scb = (((ib < 4 ) ?w0 :w1 ) >> (8 * (ib & 3 )))& 0xffu ; 281sw0 = d * simd < float ,16 > (simd < int ,16 > ((scb & 0xfu )* 2u + 1u ))* 0.125f ; 282sw1 = d * simd < float ,16 > (simd < int ,16 > ((scb >>4 )* 2u + 1u ))* 0.125f ; 283#pragma unroll 284for (int il = 0 ;il < 4 ;++ il ) { 285simd < uint32_t ,16 > idx = ((idw >> (8 * il ))& 0xffu ) | ((qhb << (8 - 2 * il ))& 0x300u ); 286simd < uint32_t ,16 > gl = gather < uint32_t ,16 > (T + O_IQ2S , (idx <<3 )); 287simd < uint32_t ,16 > gh = gather < uint32_t ,16 > (T + O_IQ2S , (idx <<3 )+ 4u ); 288simd < uint32_t ,16 > sbyte = (sgw >> (8 * il ))& 0xffu ; 289simd < uint32_t ,16 > vl = apply_signs (gl ,sbyte & 0xfu ); 290simd < uint32_t ,16 > vh = apply_signs (gh ,sbyte >>4 ); 291if (il < 2 ) { 292fr0 .template select < 16 ,1 > (16 * (2 * il ))= vl ; 293fr0 .template select < 16 ,1 > (16 * (2 * il + 1 ))= vh ; 294 }else { 295fr1 .template select < 16 ,1 > (16 * (2 * il ))= vl ; 296fr1 .template select < 16 ,1 > (16 * (2 * il + 1 ))= vh ; 297 } 298 } 299// fr0 holds values 0..15 (dwords 0..3, the rest zero), fr1 values 16..31 (dwords 4..7) 300fr0 .template select < 64 ,1 > (64 )= 0u ; 301fr1 .template select < 64 ,1 > (0 )= 0u ; 302 } 303 } 304#pragma unroll 305for (int i = 0 ;i < MT ;++ i ) { 306if (i < nvalid ) { 307simd < int ,64 > av = 0x01010101 ; 308simd < float ,8 > dxv = 1.f ; 309if (!(a .dbg & 2 )) { 310av = block_load < int ,64 > (Xi + ((size_t ) (tbase + i )* a .nkb + kb )* 64 ); 311dxv = block_load < float ,8 > (a .dx + ((size_t ) (tbase + i )* a .nkb + kb )* 8 ); 312 } 313simd < float ,128 > tf = dpas_s8 (fr0 ,av ); 314#pragma unroll 315for (int r = 0 ;r < 8 ;++ r ) { 316const float dxr = dxv [r ]; 317acc .template select < 16 ,1 > (128 * i + 16 * r )+= tf .template select < 16 ,1 > (16 * r )* (sw0 * dxr ); 318 } 319if constexpr (NS == 2 ) { 320simd < float ,128 > tg = dpas_s8 (fr1 ,av ); 321#pragma unroll 322for (int r = 0 ;r < 8 ;++ r ) { 323const float dxr = dxv [r ]; 324acc .template select < 16 ,1 > (128 * i + 16 * r )+= tg .template select < 16 ,1 > (16 * r )* (sw1 * dxr ); 325 } 326 } 327 } 328 } 329 } 330 } 331#pragma unroll 332for (int i = 0 ;i < MT ;++ i ) { 333if (i < nvalid ) { 334#pragma unroll 335for (int r = 0 ;r < 8 ;++ r ) { 336const int rl = (tl0 + i )* 8 + r ; 337if (rl < cnt && !(a .dbg & 4 )) 338block_store < float ,16 > (a .Y + ((size_t ) (pk .row0 + rl ))* a .ldy + 16 * nt ,acc .template select < 16 ,1 > (128 * i + 16 * r )); 339 } 340 } 341 } 342 }); 343 }); 344} 345 346// ------------------------------------------------------------------------------------------------ down 347template < int F ,int MT ,int G > 348void launch_dn (sycl::queue & q ,const GArgs a ,int nrowblocks ) { 349auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < G > }; 350 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 351h .parallel_for < TagDn < F ,MT ,G >> (sycl::range < 2 > ((size_t )nrowblocks , (size_t )a .ntiles ),props , [= ](sycl::id < 2 > id )SYCL_ESIMD_KERNEL { 352const int rbg = (int )id [0 ],nt = (int )id [1 ]; 353const Pick pk = pick_expert (a ,rbg ); 354const int cnt = pk .cnt ; 355const int ntl = (cnt + 7 ) >>3 ; 356const int tl0 = (rbg - pk .rb0 )* MT ; 357const int nvalid = (ntl - tl0 )< MT ? (ntl - tl0 ) :MT ; 358const int tbase = pk .tile0 + tl0 ; 359const uint8_t * We = pk .blob ; 360const uint32_t * T = a .tab ; 361simd < uint32_t ,16 > lane (0 ,1 ); 362simd < uint32_t ,16 > ro = (lane + (uint32_t ) (16 * nt ))* (uint32_t )a .rowb + a .sec_off ; 363simd < float ,128 * MT > acc = 0.f ; 364const int * Xi = reinterpret_cast < const int *> (a .Xq ); 365for (int kb = 0 ;kb < ((a .dbg & 8 ) ?0 :a .nkb );++ kb ) { 366simd < float ,16 > sw0 = 1.f ; 367simd < uint32_t ,128 > fr0 = 0x01010101u ; 368if (!(a .dbg & 1 )) { 369if constexpr (F == T_IQ4NL ) { 370const uint32_t P = (uint32_t )kb * 18 ; 371sw0 = half_of (ld16 (We ,ro + P )); 372#pragma unroll 373for (int j = 0 ;j < 4 ;++ j ) { 374simd < uint32_t ,16 > qd = ld32c (We ,ro + (P + 2 + 4 * j ),a .lim ); 375// four byte-pair lookups (lo value in byte 0, hi value in byte 1), repacked into the two dwords 376simd < uint32_t ,16 > t0 = gather < uint32_t ,16 > (T + O_IQ4 , (qd & 0xffu ) <<2 ); 377simd < uint32_t ,16 > t1 = gather < uint32_t ,16 > (T + O_IQ4 , ((qd >>8 )& 0xffu ) <<2 ); 378simd < uint32_t ,16 > t2 = gather < uint32_t ,16 > (T + O_IQ4 , ((qd >>16 )& 0xffu ) <<2 ); 379simd < uint32_t ,16 > t3 = gather < uint32_t ,16 > (T + O_IQ4 , (qd >>24 ) <<2 ); 380fr0 .template select < 16 ,1 > (16 * j )= (t0 & 0xffu ) | ((t1 & 0xffu ) <<8 ) | ((t2 & 0xffu ) <<16 ) | (t3 <<24 ); 381fr0 .template select < 16 ,1 > (16 * (j + 4 ))= 382 ((t0 >>8 )& 0xffu ) | (t1 & 0xff00u ) | ((t2 & 0xff00u ) <<8 ) | ((t3 & 0xff00u ) <<16 ); 383 } 384 }else {// T_Q2_0: 64 values per block (2 + 16 bytes), two 32-blocks per block, one table lookup per byte 385const uint32_t P = (uint32_t ) (kb >>1 )* 18 ; 386const int hb = kb & 1 ; 387sw0 = half_of (ld16 (We ,ro + P )); 388simd < uint32_t ,16 > q0 = ld32c (We ,ro + (P + 2 + 8 * hb ),a .lim ); 389simd < uint32_t ,16 > q1 = ld32c (We ,ro + (P + 6 + 8 * hb ),a .lim ); 390#pragma unroll 391for (int r = 0 ;r < 8 ;++ r ) { 392simd < uint32_t ,16 > qb = ((r < 4 ?q0 :q1 ) >> (8 * (r & 3 )))& 0xffu ; 393fr0 .template select < 16 ,1 > (16 * r )= gather < uint32_t ,16 > (T + O_Q2 ,qb <<2 ); 394 } 395 } 396 } 397#pragma unroll 398for (int i = 0 ;i < MT ;++ i ) { 399if (i < nvalid ) { 400simd < int ,64 > av = 0x01010101 ; 401simd < float ,8 > dxv = 1.f ; 402if (!(a .dbg & 2 )) { 403av = block_load < int ,64 > (Xi + ((size_t ) (tbase + i )* a .nkb + kb )* 64 ); 404dxv = block_load < float ,8 > (a .dx + ((size_t ) (tbase + i )* a .nkb + kb )* 8 ); 405 } 406simd < float ,128 > tf = dpas_s8 (fr0 ,av ); 407#pragma unroll 408for (int r = 0 ;r < 8 ;++ r ) { 409const float dxr = dxv [r ]; 410acc .template select < 16 ,1 > (128 * i + 16 * r )+= tf .template select < 16 ,1 > (16 * r )* (sw0 * dxr ); 411 } 412 } 413 } 414 } 415#pragma unroll 416for (int i = 0 ;i < MT ;++ i ) { 417if (i < nvalid ) { 418#pragma unroll 419for (int r = 0 ;r < 8 ;++ r ) { 420const int rl = (tl0 + i )* 8 + r ; 421if (rl < cnt && !(a .dbg & 4 )) 422block_store < float ,16 > (a .Y + ((size_t ) (pk .row0 + rl ))* a .ldy + 16 * nt ,acc .template select < 16 ,1 > (128 * i + 16 * r )); 423 } 424 } 425 } 426 }); 427 }); 428} 429 430// ------------------------------------------------------------------------------------------------ work-group versions 431// A work group is WGT = 8 threads side by side on 8 consecutive output tiles (128 outputs) of the same row block. The 432// activation tiles of the row block are read from global memory ONCE per group, in chunks of 4 steps of 32 (128 of K), into local 433// memory (double buffered, one barrier per chunk) and every thread reads them from there, instead of every thread reading all of 434// them from L2 (80 threads per expert re-read the same ~200 KB: the activation loads were 8 of 19 us of gate/up). The codebook 435// the decoders gather from is copied into local memory by the group once (its gathers then cost ~nothing). 436constexpr int WGT = 8 ; 437constexpr auto kAl16 = sycl::ext::oneapi::experimental::properties {sycl::ext::oneapi::experimental::alignment < 16 > }; 438 439// bytes of the tables a format gathers from (local memory), and the segments copied from the global table buffer 440template < int F > constexpr int tab_bytes () { 441return F == T_IQ3S ?2048 :F == T_IQ3XXS ?1536 :F == T_IQ2S ?8192 :1024 ;// IQ3XXS: grid 1024 + ksigns 512 442} 443 444template < int F ,int MT > 445struct WgLayout { 446static constexpr int TB = tab_bytes < F > (); 447static constexpr int ASZ = MT * 1024 ;// activation tiles of one stage: MT tiles x 4 steps x 256 bytes 448static constexpr int DSZ = MT * 128 ;// their scales: MT tiles x 4 steps x 32 bytes 449static constexpr int STAGE = ASZ + DSZ ; 450static constexpr int TOTAL = TB + 2 * STAGE ; 451static constexpr int SPLIT = 8 /MT ;// threads per tile in the staging load 452static constexpr int PART = 1024 /SPLIT ;// bytes of one tile's activations a thread stages 453}; 454 455// Staging of one chunk's activation tiles: global -> registers (LOAD_CHUNK) -> local memory stage SS (STORE_CHUNK). Macros, not 456// lambdas: a lambda capturing the staging registers by reference put them in private memory. 457#define LOAD_CHUNK (CC ) \ 458 if (s_tile < nvalid) { \ 459 const int* src_ = Xi + ((size_t) (tbase + s_tile) * a.nkb + (CC) * 4) * 64 + s_part * (L::PART / 4); \ 460 _Pragma("unroll") \ 461 for (int k = 0; k < L::PART / 256; ++k) stg.template select<64, 1>(64 * k) = block_load<int, 64>(src_ + 64 * k, kAl16); \ 462 if (s_part == 0) stg_dx = block_load<float, 32>(a.dx + ((size_t) (tbase + s_tile) * a.nkb + (CC) * 4) * 8, kAl16); \ 463 } 464#define STORE_CHUNK (SS ) \ 465 if (s_tile < nvalid) { \ 466 const uint32_t base_ = L::TB + (SS) * L::STAGE; \ 467 _Pragma("unroll") \ 468 for (int k = 0; k < L::PART / 256; ++k) \ 469 slm_block_store<int, 64>(base_ + s_tile * 1024 + s_part * L::PART + 256 * k, stg.template select<64, 1>(64 * k), kAl16); \ 470 if (s_part == 0) slm_block_store<float, 32>(base_ + L::ASZ + s_tile * 128, stg_dx, kAl16); \ 471 } 472 473template < int F ,int MT ,int G > struct TagGuW ; 474template < int F ,int MT ,int G > struct TagDnW ; 475 476template < int F ,int MT ,int G > 477void launch_gu_wg (sycl::queue & q ,const GArgs a ,int nrowblocks ) { 478using L = WgLayout < F ,MT > ; 479auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < G > }; 480constexpr int NS = (F == T_IQ2S ) ?2 :1 ; 481 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 482h .parallel_for < TagGuW < F ,MT ,G >> (sycl::nd_range < 2 > (sycl::range < 2 > ((size_t )nrowblocks , (size_t )a .ntiles ), sycl::range < 2 > (1 ,WGT )),props , 483 [= ](sycl::nd_item < 2 > it )SYCL_ESIMD_KERNEL { 484slm_init < L ::TOTAL > (); 485const int rbg = (int )it .get_group (0 ),lt = (int )it .get_local_id (1 ),nt = (int )it .get_global_id (1 ); 486const Pick pk = pick_expert (a ,rbg ); 487const int cnt = pk .cnt ; 488const int ntl = (cnt + 7 ) >>3 ; 489const int tl0 = (rbg - pk .rb0 )* MT ; 490const int nvalid = (ntl - tl0 )< MT ? (ntl - tl0 ) :MT ; 491const int tbase = pk .tile0 + tl0 ; 492const uint8_t * We = pk .blob ; 493const uint32_t * T = a .tab ; 494const int * Xi = reinterpret_cast < const int *> (a .Xq ); 495// the codebook into local memory: thread lt copies its eighth of each segment 496if constexpr (F == T_IQ3S ) { 497#pragma unroll 498for (int k = 0 ;k < tab_bytes < F > () /WGT /64 ;++ k ) 499slm_block_store < uint32_t ,16 > (lt * (tab_bytes < F > () /WGT )+ 64 * k ,block_load < uint32_t ,16 > (T + O_IQ3S + (lt * (tab_bytes < F > () /WGT )+ 64 * k ) /4 ,kAl16 ),kAl16 ); 500 }else if constexpr (F == T_IQ3XXS ) { 501#pragma unroll 502for (int k = 0 ;k < 1024 /WGT /64 ;++ k ) 503slm_block_store < uint32_t ,16 > (lt * (1024 /WGT )+ 64 * k ,block_load < uint32_t ,16 > (T + O_IQ3XXS + (lt * (1024 /WGT )+ 64 * k ) /4 ,kAl16 ),kAl16 ); 504slm_block_store < uint32_t ,16 > (1024 + lt * (512 /WGT ),block_load < uint32_t ,16 > (T + O_KSIGNS + lt * (512 /WGT ) /4 ,kAl16 ),kAl16 ); 505 }else {// IQ2_S 506#pragma unroll 507for (int k = 0 ;k < tab_bytes < F > () /WGT /64 ;++ k ) 508slm_block_store < uint32_t ,16 > (lt * (tab_bytes < F > () /WGT )+ 64 * k ,block_load < uint32_t ,16 > (T + O_IQ2S + (lt * (tab_bytes < F > () /WGT )+ 64 * k ) /4 ,kAl16 ),kAl16 ); 509 } 510// staging: this thread's piece of the chunk c (tile, part) into registers, then into stage s 511const int s_tile = lt /L ::SPLIT ,s_part = lt %L ::SPLIT ; 512simd < int ,L ::PART /4 > stg ; 513simd < float ,32 > stg_dx ; 514simd < uint32_t ,16 > lane (0 ,1 ); 515simd < uint32_t ,16 > ro = (lane & 1u )* a .sec_off + ((lane >>1 )+ (uint32_t ) (8 * nt ))* (uint32_t )a .rowb ; 516simd < float ,128 * MT > acc = 0.f ; 517const int nchunks = (a .dbg & 8 ) ?0 : (a .nkb >>2 ); 518if (nchunks > 0 ) {LOAD_CHUNK (0 );STORE_CHUNK (0 ); } 519barrier ();// the codebook and chunk 0 are in local memory 520simd < float ,16 > d = 0.f ; 521simd < uint32_t ,16 > w0 = 0 ,w1 = 0 ,w2 = 0 ,w3 = 0 ; 522for (int c = 0 ;c < nchunks ;++ c ) { 523const int sb = c >>1 ; 524const int stage_off = L ::TB + (c & 1 )* L ::STAGE ; 525if (c + 1 < nchunks ) {LOAD_CHUNK (c + 1 ); } 526if ((c & 1 )== 0 ) {// new superblock: its scale and the fields shared by its 8 steps 527const uint32_t P = (uint32_t )sb * blk_bytes < F > (); 528d = half_of (ld16 (We ,ro + P )); 529if constexpr (F == T_IQ3S ) { 530w0 = ld32 (We ,ro + (P + 106 ));w1 = ld32 (We ,ro + (P + 66 ));w2 = ld32 (We ,ro + (P + 70 )); 531 }else if constexpr (F == T_IQ2S ) { 532w0 = ld32 (We ,ro + (P + 74 ));w1 = ld32 (We ,ro + (P + 78 ));w2 = ld32 (We ,ro + (P + 66 ));w3 = ld32 (We ,ro + (P + 70 )); 533 } 534 } 535const uint32_t P = (uint32_t )sb * blk_bytes < F > (); 536#pragma unroll 1 537for (int ib4 = 0 ;ib4 < 4 ;++ ib4 ) { 538const int ib = ((c & 1 ) <<2 )+ ib4 ; 539simd < uint32_t ,128 > fr0 = 0x01010101u ,fr1 = 0u ; 540simd < float ,16 > sw0 = 1.f ,sw1 = 1.f ; 541if (!(a .dbg & 1 )) { 542if constexpr (F == T_IQ3S ) { 543simd < uint32_t ,16 > q0 = ld32 (We ,ro + (P + 2 + 8 * ib )); 544simd < uint32_t ,16 > q1 = ld32 (We ,ro + (P + 6 + 8 * ib )); 545simd < uint32_t ,16 > sg = ld32 (We ,ro + (P + 74 + 4 * ib )); 546simd < uint32_t ,16 > qhv = (((ib < 4 ) ?w1 :w2 ) >> (8 * (ib & 3 )))& 0xffu ; 547simd < uint32_t ,16 > scn = ((w0 >> (8 * (ib >>1 ))) >> (4 * (ib & 1 )))& 0xfu ; 548sw0 = d * simd < float ,16 > (simd < int ,16 > (scn * 2u + 1u )); 549#pragma unroll 550for (int r = 0 ;r < 8 ;++ r ) { 551simd < uint32_t ,16 > qsw = r < 4 ?q0 :q1 ; 552simd < uint32_t ,16 > idx = ((qsw >> (8 * (r & 3 )))& 0xffu ) | (((qhv >>r )& 1u ) <<8 ); 553simd < uint32_t ,16 > g = slm_gather < uint32_t ,16 > (idx <<2 ); 554simd < uint32_t ,16 > nib = ((sg >> (8 * (r >>1 ))) >> (4 * (r & 1 )))& 0xfu ; 555fr0 .template select < 16 ,1 > (16 * r )= apply_signs (g ,nib ); 556 } 557 }else if constexpr (F == T_IQ3XXS ) { 558simd < uint32_t ,16 > q0 = ld32 (We ,ro + (P + 2 + 8 * ib )); 559simd < uint32_t ,16 > q1 = ld32 (We ,ro + (P + 6 + 8 * ib )); 560simd < uint32_t ,16 > aux = ld32 (We ,ro + (P + 66 + 4 * ib )); 561sw0 = d * simd < float ,16 > (simd < int ,16 > ((aux >>28 )* 2u + 1u ))* 0.25f ; 562#pragma unroll 563for (int il = 0 ;il < 4 ;++ il ) { 564simd < uint32_t ,16 > sbyte = slm_gather < uint32_t ,16 > (1024u + (((aux >> (7 * il ))& 127u ) <<2 )); 565#pragma unroll 566for (int hh = 0 ;hh < 2 ;++ hh ) { 567const int r = 2 * il + hh ; 568simd < uint32_t ,16 > qsw = r < 4 ?q0 :q1 ; 569simd < uint32_t ,16 > idx = (qsw >> (8 * (r & 3 )))& 0xffu ; 570simd < uint32_t ,16 > g = slm_gather < uint32_t ,16 > (idx <<2 ); 571fr0 .template select < 16 ,1 > (16 * r )= apply_signs (g , (sbyte >> (4 * hh ))& 0xfu ); 572 } 573 } 574 }else if constexpr (F == T_IQ2S ) { 575simd < uint32_t ,16 > idw = ld32 (We ,ro + (P + 2 + 4 * ib )); 576simd < uint32_t ,16 > sgw = ld32 (We ,ro + (P + 34 + 4 * ib )); 577simd < uint32_t ,16 > qhb = (((ib < 4 ) ?w2 :w3 ) >> (8 * (ib & 3 )))& 0xffu ; 578simd < uint32_t ,16 > scb = (((ib < 4 ) ?w0 :w1 ) >> (8 * (ib & 3 )))& 0xffu ; 579sw0 = d * simd < float ,16 > (simd < int ,16 > ((scb & 0xfu )* 2u + 1u ))* 0.125f ; 580sw1 = d * simd < float ,16 > (simd < int ,16 > ((scb >>4 )* 2u + 1u ))* 0.125f ; 581#pragma unroll 582for (int il = 0 ;il < 4 ;++ il ) { 583simd < uint32_t ,16 > idx = ((idw >> (8 * il ))& 0xffu ) | ((qhb << (8 - 2 * il ))& 0x300u ); 584simd < uint32_t ,16 > gl = slm_gather < uint32_t ,16 > (idx <<3 ); 585simd < uint32_t ,16 > gh = slm_gather < uint32_t ,16 > ((idx <<3 )+ 4u ); 586simd < uint32_t ,16 > sbyte = (sgw >> (8 * il ))& 0xffu ; 587simd < uint32_t ,16 > vl = apply_signs (gl ,sbyte & 0xfu ); 588simd < uint32_t ,16 > vh = apply_signs (gh ,sbyte >>4 ); 589if (il < 2 ) { 590fr0 .template select < 16 ,1 > (16 * (2 * il ))= vl ; 591fr0 .template select < 16 ,1 > (16 * (2 * il + 1 ))= vh ; 592 }else { 593fr1 .template select < 16 ,1 > (16 * (2 * il ))= vl ; 594fr1 .template select < 16 ,1 > (16 * (2 * il + 1 ))= vh ; 595 } 596 } 597fr0 .template select < 64 ,1 > (64 )= 0u ; 598fr1 .template select < 64 ,1 > (0 )= 0u ; 599 } 600 } 601#pragma unroll 602for (int i = 0 ;i < MT ;++ i ) { 603if (i < nvalid ) { 604simd < int ,64 > av = 0x01010101 ; 605simd < float ,8 > dxv = 1.f ; 606if (!(a .dbg & 2 )) { 607av = slm_block_load < int ,64 > (stage_off + i * 1024 + ib4 * 256 ,kAl16 ); 608dxv = slm_block_load < float ,8 > (stage_off + L ::ASZ + i * 128 + ib4 * 32 ,kAl16 ); 609 } 610simd < float ,128 > tf = dpas_s8 (fr0 ,av ); 611#pragma unroll 612for (int r = 0 ;r < 8 ;++ r ) { 613const float dxr = dxv [r ]; 614acc .template select < 16 ,1 > (128 * i + 16 * r )+= tf .template select < 16 ,1 > (16 * r )* (sw0 * dxr ); 615 } 616if constexpr (NS == 2 ) { 617simd < float ,128 > tg = dpas_s8 (fr1 ,av ); 618#pragma unroll 619for (int r = 0 ;r < 8 ;++ r ) { 620const float dxr = dxv [r ]; 621acc .template select < 16 ,1 > (128 * i + 16 * r )+= tg .template select < 16 ,1 > (16 * r )* (sw1 * dxr ); 622 } 623 } 624 } 625 } 626 } 627if (c + 1 < nchunks ) {STORE_CHUNK ((c + 1 )& 1 ); } 628barrier (); 629 } 630#pragma unroll 631for (int i = 0 ;i < MT ;++ i ) { 632if (i < nvalid ) { 633#pragma unroll 634for (int r = 0 ;r < 8 ;++ r ) { 635const int rl = (tl0 + i )* 8 + r ; 636if (rl < cnt && !(a .dbg & 4 )) 637block_store < float ,16 > (a .Y + ((size_t ) (pk .row0 + rl ))* a .ldy + 16 * nt ,acc .template select < 16 ,1 > (128 * i + 16 * r )); 638 } 639 } 640 } 641 }); 642 }); 643} 644 645template < int F ,int MT ,int G > 646void launch_dn_wg (sycl::queue & q ,const GArgs a ,int nrowblocks ) { 647using L = WgLayout < F ,MT > ; 648auto props = sycl::ext::oneapi::experimental::properties {sycl::ext::intel::experimental::grf_size < G > }; 649 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 650h .parallel_for < TagDnW < F ,MT ,G >> (sycl::nd_range < 2 > (sycl::range < 2 > ((size_t )nrowblocks , (size_t )a .ntiles ), sycl::range < 2 > (1 ,WGT )),props , 651 [= ](sycl::nd_item < 2 > it )SYCL_ESIMD_KERNEL { 652slm_init < L ::TOTAL > (); 653const int rbg = (int )it .get_group (0 ),lt = (int )it .get_local_id (1 ),nt = (int )it .get_global_id (1 ); 654const Pick pk = pick_expert (a ,rbg ); 655const int cnt = pk .cnt ; 656const int ntl = (cnt + 7 ) >>3 ; 657const int tl0 = (rbg - pk .rb0 )* MT ; 658const int nvalid = (ntl - tl0 )< MT ? (ntl - tl0 ) :MT ; 659const int tbase = pk .tile0 + tl0 ; 660const uint8_t * We = pk .blob ; 661const uint32_t * T = a .tab ; 662const int * Xi = reinterpret_cast < const int *> (a .Xq ); 663constexpr uint32_t TOFF = (F == T_IQ4NL ) ?O_IQ4 :O_Q2 ; 664#pragma unroll 665for (int k = 0 ;k < tab_bytes < F > () /WGT /64 ;++ k ) 666slm_block_store < uint32_t ,16 > (lt * (tab_bytes < F > () /WGT )+ 64 * k ,block_load < uint32_t ,16 > (T + TOFF + (lt * (tab_bytes < F > () /WGT )+ 64 * k ) /4 ,kAl16 ),kAl16 ); 667const int s_tile = lt /L ::SPLIT ,s_part = lt %L ::SPLIT ; 668simd < int ,L ::PART /4 > stg ; 669simd < float ,32 > stg_dx ; 670simd < uint32_t ,16 > lane (0 ,1 ); 671simd < uint32_t ,16 > ro = (lane + (uint32_t ) (16 * nt ))* (uint32_t )a .rowb + a .sec_off ; 672simd < float ,128 * MT > acc = 0.f ; 673const int nchunks = (a .dbg & 8 ) ?0 : (a .nkb >>2 ); 674if (nchunks > 0 ) {LOAD_CHUNK (0 );STORE_CHUNK (0 ); } 675barrier (); 676for (int c = 0 ;c < nchunks ;++ c ) { 677const int stage_off = L ::TB + (c & 1 )* L ::STAGE ; 678if (c + 1 < nchunks ) {LOAD_CHUNK (c + 1 ); } 679#pragma unroll 1 680for (int ib4 = 0 ;ib4 < 4 ;++ ib4 ) { 681const int kb = c * 4 + ib4 ; 682simd < float ,16 > sw0 = 1.f ; 683simd < uint32_t ,128 > fr0 = 0x01010101u ; 684if (!(a .dbg & 1 )) { 685if constexpr (F == T_IQ4NL ) { 686const uint32_t P = (uint32_t )kb * 18 ; 687sw0 = half_of (ld16 (We ,ro + P )); 688#pragma unroll 689for (int j = 0 ;j < 4 ;++ j ) { 690simd < uint32_t ,16 > qd = ld32c (We ,ro + (P + 2 + 4 * j ),a .lim ); 691simd < uint32_t ,16 > t0 = slm_gather < uint32_t ,16 > ((qd & 0xffu ) <<2 ); 692simd < uint32_t ,16 > t1 = slm_gather < uint32_t ,16 > (((qd >>8 )& 0xffu ) <<2 ); 693simd < uint32_t ,16 > t2 = slm_gather < uint32_t ,16 > (((qd >>16 )& 0xffu ) <<2 ); 694simd < uint32_t ,16 > t3 = slm_gather < uint32_t ,16 > ((qd >>24 ) <<2 ); 695fr0 .template select < 16 ,1 > (16 * j )= (t0 & 0xffu ) | ((t1 & 0xffu ) <<8 ) | ((t2 & 0xffu ) <<16 ) | (t3 <<24 ); 696fr0 .template select < 16 ,1 > (16 * (j + 4 ))= 697 ((t0 >>8 )& 0xffu ) | (t1 & 0xff00u ) | ((t2 & 0xff00u ) <<8 ) | ((t3 & 0xff00u ) <<16 ); 698 } 699 }else {// T_Q2_0 700const uint32_t P = (uint32_t ) (kb >>1 )* 18 ; 701const int hb = kb & 1 ; 702sw0 = half_of (ld16 (We ,ro + P )); 703simd < uint32_t ,16 > q0 = ld32c (We ,ro + (P + 2 + 8 * hb ),a .lim ); 704simd < uint32_t ,16 > q1 = ld32c (We ,ro + (P + 6 + 8 * hb ),a .lim ); 705#pragma unroll 706for (int r = 0 ;r < 8 ;++ r ) { 707simd < uint32_t ,16 > qb = ((r < 4 ?q0 :q1 ) >> (8 * (r & 3 )))& 0xffu ; 708fr0 .template select < 16 ,1 > (16 * r )= slm_gather < uint32_t ,16 > (qb <<2 ); 709 } 710 } 711 } 712#pragma unroll 713for (int i = 0 ;i < MT ;++ i ) { 714if (i < nvalid ) { 715simd < int ,64 > av = 0x01010101 ; 716simd < float ,8 > dxv = 1.f ; 717if (!(a .dbg & 2 )) { 718av = slm_block_load < int ,64 > (stage_off + i * 1024 + ib4 * 256 ,kAl16 ); 719dxv = slm_block_load < float ,8 > (stage_off + L ::ASZ + i * 128 + ib4 * 32 ,kAl16 ); 720 } 721simd < float ,128 > tf = dpas_s8 (fr0 ,av ); 722#pragma unroll 723for (int r = 0 ;r < 8 ;++ r ) { 724const float dxr = dxv [r ]; 725acc .template select < 16 ,1 > (128 * i + 16 * r )+= tf .template select < 16 ,1 > (16 * r )* (sw0 * dxr ); 726 } 727 } 728 } 729 } 730if (c + 1 < nchunks ) {STORE_CHUNK ((c + 1 )& 1 ); } 731barrier (); 732 } 733#pragma unroll 734for (int i = 0 ;i < MT ;++ i ) { 735if (i < nvalid ) { 736#pragma unroll 737for (int r = 0 ;r < 8 ;++ r ) { 738const int rl = (tl0 + i )* 8 + r ; 739if (rl < cnt && !(a .dbg & 4 )) 740block_store < float ,16 > (a .Y + ((size_t ) (pk .row0 + rl ))* a .ldy + 16 * nt ,acc .template select < 16 ,1 > (128 * i + 16 * r )); 741 } 742 } 743 } 744 }); 745 }); 746} 747 748// MT from the group's mean number of 8-row tiles per expert. 749int pick_mt (const Layout & lo ) { 750const double mean = lo .n ? (double )lo .tile0 [lo .n ] /lo .n :0 ; 751if (mean <=2.0 )return 2 ; 752if (mean <=4.0 )return 4 ; 753return 8 ; 754} 755int dbg_flags () { 756static const int v = [] {const char * s = std::getenv ("STRATA_I8_DBG" );return s ? std::atoi (s ) :0 ; }(); 757return v ; 758} 759int mt_for (const Layout & lo ) { 760static const int mo = [] {const char * s = std::getenv ("STRATA_I8_MT" );return s ? std::atoi (s ) :0 ; }(); 761return mo ?mo :pick_mt (lo ); 762} 763 764// register mode per tile height: STRATA_I8_GRF=128 (more threads per EU, spills at MT 8) or 256; default 256 765int wg_for () { 766static const int v = [] {const char * s = std::getenv ("STRATA_I8_WG" );return s ? std::atoi (s ) :1 ; }(); 767return v ; 768} 769int grf_for (int mt ) { 770static const int g = [] {const char * s = std::getenv ("STRATA_I8_GRF" );return s ? std::atoi (s ) :256 ; }(); 771 (void )mt ; 772return g == 128 ?128 :256 ; 773} 774 775template < class F > 776bool dispatch_mt (int mt ,F && f ) { 777switch (mt ) { 778case 2 :f (std::integral_constant < int ,2 > {});return true; 779case 4 :f (std::integral_constant < int ,4 > {});return true; 780case 8 :f (std::integral_constant < int ,8 > {});return true; 781default :return false; 782 } 783} 784 785struct QArgs { 786int n ; 787int row0 [kMaxGroup + 1 ]; 788int tile0 [kMaxGroup + 1 ]; 789int nkb ; 790int ld ;// row length in elements of the source 791}; 792class QuantTag ; 793class SwigluTag ; 794 795}// namespace 796 797void make_layout (Layout & lo ,const int32_t * cnt ,int n ) { 798lo .n = n ; 799int r = 0 ,t = 0 ; 800for (int i = 0 ;i < n ;++ i ) { 801lo .cnt [i ]= cnt [i ]; 802lo .row0 [i ]= r ; 803lo .tile0 [i ]= t ; 804r += cnt [i ]; 805t += (cnt [i ]+ 7 ) /8 ; 806 } 807lo .row0 [n ]= r ; 808lo .tile0 [n ]= t ; 809} 810 811namespace { 812bool bmg_arch (const sycl::device & d ) { 813namespace ex= sycl::ext::oneapi::experimental; 814if (!d .is_gpu ())return false; 815const auto a = d .get_info < ex::info::device::architecture > (); 816return a == ex::architecture::intel_gpu_bmg_g21 || a == ex::architecture::intel_gpu_bmg_g31 ; 817} 818}// namespace 819 820bool battlemage (void * stream ) { 821 sycl::queue & q = * strata::q_of (stream ); 822return bmg_arch (q .get_device ()); 823} 824 825bool any_battlemage () { 826static const bool v = [] { 827try { 828for (const auto & d : sycl::device::get_devices (sycl::info::device_type::gpu )) 829if (bmg_arch (d ))return true; 830 }catch (...) {} 831return false; 832 }(); 833return v ; 834} 835 836bool available (void * stream ) { 837 sycl::queue & q = * strata::q_of (stream ); 838static std::mutex mu ; 839static std::unordered_map < sycl::device ,bool > per_dev ; 840 std::lock_guard < std::mutex > lk (mu ); 841const sycl::device d = q .get_device (); 842if (auto it = per_dev .find (d );it != per_dev .end ())return it -> second ; 843const bool ok = [& ] { 844const char * v = std::getenv ("STRATA_I8_FORCE" ); 845if (v && v [0 ]== '0' )return false; 846if (!d .has (sycl::aspect::ext_intel_esimd ))return false; 847namespace mx= sycl::ext::oneapi::experimental::matrix; 848bool s8 = false; 849for (const auto & c :d .get_info < sycl::ext::oneapi::experimental::info::device::matrix_combinations > ()) 850if (c .atype == mx::matrix_type::sint8 && c .btype == mx::matrix_type::sint8 && c .nsize == 16 && c .ksize == 32 )s8 = true; 851return s8 ; 852 }(); 853per_dev [d ]= ok ; 854return ok ; 855} 856 857size_t xq_bytes (const Layout & lo ,int K ) {return (size_t )lo .tile0 [lo .n ]* 8 * K ; } 858size_t dx_bytes (const Layout & lo ,int K ) {return (size_t )lo .tile0 [lo .n ]* (K /32 )* 8 * sizeof (float ); } 859 860bool workspace (const Layout & lo ,int K ,int n_ff ,Workspace & ws ,void * stream ) { 861 sycl::queue & q = * strata::q_of (stream ); 862static std::mutex mu ; 863struct St {Workspace cur ;size_t cap_x = 0 ,cap_h = 0 ; }; 864static std::unordered_map < sycl::device ,St > per_dev ; 865 std::lock_guard < std::mutex > lk (mu ); 866St & st = per_dev [q .get_device ()]; 867Workspace & cur = st .cur ; 868size_t & cap_x = st .cap_x ; 869size_t & cap_h = st .cap_h ; 870const size_t nx = xq_bytes (lo ,K ),nh = xq_bytes (lo ,n_ff ); 871if (nx > cap_x || nh > cap_h ) { 872q .wait (); 873if (cur .xq ) { sycl::free (cur .xq ,q ); sycl::free (cur .dx ,q ); sycl::free (cur .hq ,q ); sycl::free (cur .dh ,q ); } 874cap_x = std::max (nx ,cap_x + cap_x /4 ); 875cap_h = std::max (nh ,cap_h + cap_h /4 ); 876cur .xq = sycl::malloc_device < int8_t > (cap_x ,q ); 877cur .dx = sycl::malloc_device < float > (cap_x /32 /8 * 8 + 64 ,q );// rows * K/32 floats = bytes / 32 878cur .hq = sycl::malloc_device < int8_t > (cap_h ,q ); 879cur .dh = sycl::malloc_device < float > (cap_h /32 + 64 ,q ); 880if (!cur .xq || !cur .dx || !cur .hq || !cur .dh ) {cap_x = cap_h = 0 ;cur = Workspace {};ws = Workspace {};return false; } 881 } 882ws = cur ; 883return true; 884} 885 886void quantize_rows (const uint16_t * X ,const Layout & lo ,int K ,int8_t * Xq ,float * dx ,void * stream ) { 887if (lo .tile0 [lo .n ]== 0 )return ; 888 sycl::queue & q = * strata::q_of (stream ); 889QArgs a {}; 890a .n = lo .n ; 891for (int i = 0 ;i <=lo .n ;++ i ) {a .row0 [i ]= lo .row0 [i ];a .tile0 [i ]= lo .tile0 [i ]; } 892a .nkb = K /32 ; 893a .ld = K ; 894const size_t nthreads = (size_t )lo .tile0 [lo .n ]* a .nkb ; 895const half * Xh = reinterpret_cast < const half *> (X ); 896uint32_t * Xd = reinterpret_cast < uint32_t *> (Xq ); 897 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 898h .parallel_for < QuantTag > (sycl::range < 2 > ((size_t )lo .tile0 [lo .n ], (size_t )a .nkb ), [= ](sycl::id < 2 > id )SYCL_ESIMD_KERNEL { 899const int T = (int )id [0 ],kb = (int )id [1 ]; 900// the expert of tile T, by constant-index reads of the argument arrays (see pick_expert) 901int t0e = a .tile0 [0 ],r0e = a .row0 [0 ],r1e = a .row0 [1 ]; 902#pragma unroll 903for (int i = 1 ;i < kMaxGroup ;++ i ) 904if (i < a .n && T >=a .tile0 [i ]) {t0e = a .tile0 [i ];r0e = a .row0 [i ];r1e = a .row0 [i + 1 ]; } 905const int cnt = r1e - r0e ; 906simd < float ,8 > dxv = 0.f ; 907#pragma unroll 908for (int r = 0 ;r < 8 ;++ r ) { 909const int rl = (T - t0e )* 8 + r ; 910simd < uint32_t ,8 > qd = 0u ; 911if (rl < cnt ) { 912simd < half ,32 > xh = block_load < half ,32 > (Xh + ((size_t ) (r0e + rl ))* a .ld + kb * 32 ); 913simd < float ,32 > x = xh ; 914float amax = hmax < float > (abs (x )); 915const float dd = amax /127.f ; 916const float id127 = amax > 0.f ?127.f /amax :0.f ; 917simd < float ,32 > y = x * id127 ; 918simd < float ,32 > adj = 0.5f ; 919adj .merge (simd < float ,32 > (-0.5f ),y < 0.f ); 920simd < int ,32 > qi = convert < int > (y + adj ); 921simd < int8_t ,32 > q8 = convert < int8_t > (qi ); 922qd = q8 .template bit_cast_view < uint32_t > (); 923dxv [r ]= dd ; 924 } 925block_store < uint32_t ,8 > (Xd + ((size_t )T * a .nkb + kb )* 64 + r * 8 ,qd ); 926 } 927block_store < float ,8 > (dx + ((size_t )T * a .nkb + kb )* 8 ,dxv ); 928 }); 929 }); 930} 931 932void swiglu_quantize (const float * GU ,const Layout & lo ,int n_ff ,int8_t * Hq ,float * dh ,void * stream ) { 933if (lo .tile0 [lo .n ]== 0 )return ; 934 sycl::queue & q = * strata::q_of (stream ); 935QArgs a {}; 936a .n = lo .n ; 937for (int i = 0 ;i <=lo .n ;++ i ) {a .row0 [i ]= lo .row0 [i ];a .tile0 [i ]= lo .tile0 [i ]; } 938a .nkb = n_ff /32 ; 939a .ld = 2 * n_ff ; 940const size_t nthreads = (size_t )lo .tile0 [lo .n ]* a .nkb ; 941uint32_t * Hd = reinterpret_cast < uint32_t *> (Hq ); 942 strata::kprof::kp (& q ,__FILE__ ,__func__ ,__LINE__ )-> submit ([& ](sycl::handler & h ) { 943h .parallel_for < SwigluTag > (sycl::range < 2 > ((size_t )lo .tile0 [lo .n ], (size_t )a .nkb ), [= ](sycl::id < 2 > id )SYCL_ESIMD_KERNEL { 944const int T = (int )id [0 ],kb = (int )id [1 ]; 945// the expert of tile T, by constant-index reads of the argument arrays (see pick_expert) 946int t0e = a .tile0 [0 ],r0e = a .row0 [0 ],r1e = a .row0 [1 ]; 947#pragma unroll 948for (int i = 1 ;i < kMaxGroup ;++ i ) 949if (i < a .n && T >=a .tile0 [i ]) {t0e = a .tile0 [i ];r0e = a .row0 [i ];r1e = a .row0 [i + 1 ]; } 950const int cnt = r1e - r0e ; 951simd < float ,8 > dxv = 0.f ; 952#pragma unroll 953for (int r = 0 ;r < 8 ;++ r ) { 954const int rl = (T - t0e )* 8 + r ; 955simd < uint32_t ,8 > qd = 0u ; 956if (rl < cnt ) { 957simd < float ,64 > v = block_load < float ,64 > (GU + ((size_t ) (r0e + rl ))* a .ld + kb * 64 ); 958simd < float ,32 > g = v .template select < 32 ,2 > (0 ); 959simd < float ,32 > u = v .template select < 32 ,2 > (1 ); 960simd < float ,32 > x = g * u / (1.f + exp (- g )); 961float amax = hmax < float > (abs (x )); 962const float dd = amax /127.f ; 963const float id127 = amax > 0.f ?127.f /amax :0.f ; 964simd < float ,32 > y = x * id127 ; 965simd < float ,32 > adj = 0.5f ; 966adj .merge (simd < float ,32 > (-0.5f ),y < 0.f ); 967simd < int ,32 > qi = convert < int > (y + adj ); 968simd < int8_t ,32 > q8 = convert < int8_t > (qi ); 969qd = q8 .template bit_cast_view < uint32_t > (); 970dxv [r ]= dd ; 971 } 972block_store < uint32_t ,8 > (Hd + ((size_t )T * a .nkb + kb )* 64 + r * 8 ,qd ); 973 } 974block_store < float ,8 > (dh + ((size_t )T * a .nkb + kb )* 8 ,dxv ); 975 }); 976 }); 977} 978 979bool gate_up (int gu_type ,const uint8_t * const * blob ,size_t up_off ,int n_ff ,const Layout & lo ,int K ,const int8_t * Xq , 980const float * dx ,float * GU ,void * stream ) { 981if (lo .n == 0 || lo .tile0 [lo .n ]== 0 )return true; 982if ((gu_type != T_IQ3S && gu_type != T_IQ3XXS && gu_type != T_IQ2S )|| K %256 != 0 || (2 * n_ff ) %16 != 0 || 983 (up_off & 3 )!= 0 || lo .n > kMaxGroup ) 984return false; 985 sycl::queue & q = * strata::q_of (stream ); 986GArgs a {}; 987for (int i = 0 ;i < lo .n ;++ i )a .blob [i ]= blob [i ]; 988a .sec_off = (uint32_t )up_off ; 989a .Xq = Xq ;a .dx = dx ;a .Y = GU ; 990a .tab = tables (q ); 991a .ntiles = 2 * n_ff /16 ; 992a .nkb = K /32 ; 993a .rowb = (K /256 )* block_bytes_of (gu_type ); 994a .ldy = 2 * n_ff ; 995a .dbg = dbg_flags (); 996const int mt = mt_for (lo ); 997const int rb = fill_args_mt (a ,lo ,mt ); 998return dispatch_mt (mt , [& ](auto mtc ) { 999constexpr int MT = decltype(mtc )::value ; 1000if (wg_for ()&& gu_type != T_IQ2S && a .nkb %4 == 0 && a .ntiles %WGT == 0 ) {// IQ2_S: its 8 KB codebook copied into local memory by every group costs more than it saves (35 vs 16 us) 1001if (gu_type == T_IQ3S )launch_gu_wg < T_IQ3S ,MT ,256 > (q ,a ,rb ); 1002else if (gu_type == T_IQ3XXS )launch_gu_wg < T_IQ3XXS ,MT ,256 > (q ,a ,rb ); 1003else launch_gu_wg < T_IQ2S ,MT ,256 > (q ,a ,rb ); 1004 }else if (grf_for (MT )== 128 ) { 1005if (gu_type == T_IQ3S )launch_gu < T_IQ3S ,MT ,128 > (q ,a ,rb ); 1006else if (gu_type == T_IQ3XXS )launch_gu < T_IQ3XXS ,MT ,128 > (q ,a ,rb ); 1007else launch_gu < T_IQ2S ,MT ,128 > (q ,a ,rb ); 1008 }else { 1009if (gu_type == T_IQ3S )launch_gu < T_IQ3S ,MT ,256 > (q ,a ,rb ); 1010else if (gu_type == T_IQ3XXS )launch_gu < T_IQ3XXS ,MT ,256 > (q ,a ,rb ); 1011else launch_gu < T_IQ2S ,MT ,256 > (q ,a ,rb ); 1012 } 1013 }); 1014} 1015 1016bool down (int d_type ,const uint8_t * const * blob ,size_t down_off ,int n_embd ,const Layout & lo ,int n_ff ,const int8_t * Hq , 1017const float * dh ,float * Dm ,void * stream ) { 1018if (lo .n == 0 || lo .tile0 [lo .n ]== 0 )return true; 1019if ((d_type != T_IQ4NL && d_type != T_Q2_0 )|| n_ff %block_vals_of (d_type )!= 0 || n_embd %16 != 0 || (down_off & 3 )!= 0 || 1020lo .n > kMaxGroup ) 1021return false; 1022 sycl::queue & q = * strata::q_of (stream ); 1023GArgs a {}; 1024for (int i = 0 ;i < lo .n ;++ i )a .blob [i ]= blob [i ]; 1025a .sec_off = (uint32_t )down_off ; 1026a .Xq = Hq ;a .dx = dh ;a .Y = Dm ; 1027a .tab = tables (q ); 1028a .ntiles = n_embd /16 ; 1029a .nkb = n_ff /32 ; 1030a .rowb = (n_ff /block_vals_of (d_type ))* block_bytes_of (d_type ); 1031a .lim = (uint32_t ) (down_off + (size_t )n_embd * (size_t )a .rowb )- 4u ; 1032a .ldy = n_embd ; 1033a .dbg = dbg_flags (); 1034const int mt = mt_for (lo ); 1035const int rb = fill_args_mt (a ,lo ,mt ); 1036return dispatch_mt (mt , [& ](auto mtc ) { 1037constexpr int MT = decltype(mtc )::value ; 1038if (wg_for ()&& a .nkb %4 == 0 && a .ntiles %WGT == 0 ) { 1039if (d_type == T_IQ4NL )launch_dn_wg < T_IQ4NL ,MT ,256 > (q ,a ,rb ); 1040else launch_dn_wg < T_Q2_0 ,MT ,256 > (q ,a ,rb ); 1041 }else if (grf_for (MT )== 128 ) { 1042if (d_type == T_IQ4NL )launch_dn < T_IQ4NL ,MT ,128 > (q ,a ,rb ); 1043else launch_dn < T_Q2_0 ,MT ,128 > (q ,a ,rb ); 1044 }else { 1045if (d_type == T_IQ4NL )launch_dn < T_IQ4NL ,MT ,256 > (q ,a ,rb ); 1046else launch_dn < T_Q2_0 ,MT ,256 > (q ,a ,rb ); 1047 } 1048 }); 1049} 1050 1051}// namespace strata::prefill::i8 1052#endif // STRATA_I8_NO_ESIMD