/* xe2_gemv_m2_esimd.cpp -- the m2xfp4 verify GEMM (2 <= M <= 16) in ESIMD: the register-DPAS form * of xe2_gemv_m2.cpp (launch_dpas_raw) with the same arithmetic, written per hardware thread. * * Why ESIMD: in SYCL a lane's half2 scale multiply compiles to two SIMD16 fp16 multiplies plus four * moves splitting and re-packing the pair, ~10 instructions a weight pair, and that ALU work -- not * the weight stream -- set the kernel's time (probe m2v.cpp: 333 us at gate_up's shape, 193 us with * the decode removed). Here a thread holds its 16 columns' code words as [word][lane] registers, a * decoded word pair is one 32-wide fp16 multiply, and the loop body is ~4 integer ops a weight pair: * 240 us at the same shape, bit-identical (probe m2e.cpp). * * Layout: a thread 16 output columns, lane = column, as the SIMT kernel's sub-group. Its codes are * read 16 bytes a lane at a time (LSC d32x4 gathers, [dword][lane] in registers), so a word of 8 * nibbles is one register across the 16 columns -- or, stored tiled (xe2_ops.h, m2_tiled), a step's * 1 KiB already in that order, four block loads. The activation is ae[(c / 16) * mpad + m][c % 16] * (c the permuted column of xe2_gemv_m2.cpp's prepass, m the row): a DPAS A operand of 8 rows by 16 * k is one 256-byte block load. Results: y directly (f32 / bf16) when the whole K is one thread's, * else partial sums into pbuf for the caller's reduction, exactly as launch_dpas_raw does. */ #include "xe2_common.h" #include namespace xe2 { namespace { namespace esimd = sycl::ext::intel::esimd; namespace xmx = sycl::ext::intel::esimd::xmx; using half = sycl::half; constexpr int kKs = 4; /* 32-groups a step: 64 bytes of a column's codes */ template int launch(sycl::queue& q, const half* ae, const uint8_t* codes0, const uint8_t* meta0, const uint8_t* expe0, const int32_t* emax, int64_t brs, float* pbuf, int64_t N, int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits, const float* rr, char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int mm, int64_t ldp) { constexpr int kWg = 8; /* threads a work-group */ const int64_t ntile = (N + 15) / 16; const int64_t nthr = ntile * splits, ng = (nthr + kWg - 1) / kWg; const int64_t kg = K / 32; const int64_t per = ((kg + splits - 1) / splits + kKs - 1) / kKs * kKs; return submit([&] { q.parallel_for(sycl::nd_range<1>{(size_t)(ng * kWg), (size_t)kWg}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { using namespace esimd; const int64_t tid = (int64_t)it.get_global_id(0); if (tid >= nthr) return; const int64_t tile = tid % ntile, sp = tid / ntile; const int64_t n0 = tile * 16; const int64_t g0s = sp * per, g1s = kg < g0s + per ? kg : g0s + per; const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2); const half* abase = ae + part * K * mpad; const simd lane(0, 1); simd col = lane + (uint32_t)n0; col.merge(simd((uint32_t)(N - 1)), col > (uint32_t)(N - 1)); const uint8_t* cbase = codes0 + n0 * (K / 2); const simd coff = (col - (uint32_t)n0) * (uint32_t)(K / 2); const uint32_t* tbase = (const uint32_t*)codes0 + tile * (kg / 4) * 256; /* TL: the tile's steps */ const simd eoff = col * (uint32_t)brs; const simd moff = col * (uint32_t)kg; /* the column's largest exponent, from the caller's prepass */ const simd mx = gather(emax, col * 4u); simd c[MT]; _Pragma("unroll") for (int i = 0; i < MT; ++i) c[i] = 0.0f; /* a step: kKs groups -- the code lines, the activation blocks, the decode and DPAS */ auto step = [&](int64_t g0, simd e4, simd m4) SYCL_ESIMD_FUNCTION { simd line; /* [word 16][lane 16] */ if constexpr (TL) { /* tiled: the step is these 1 KiB as they lie */ _Pragma("unroll") for (int qd = 0; qd < 4; ++qd) line.template select<64, 1>(qd * 64) = block_load(tbase + (g0 / kKs) * 256 + qd * 64); } else { _Pragma("unroll") for (int qd = 0; qd < 4; ++qd) line.template select<64, 1>(qd * 64) = gather((const uint32_t*)(cbase + g0 * 16 + qd * 16), coff); } /* the step's activation, 8 k-blocks of [mpad rows][16 k], loaded with the codes */ simd As; _Pragma("unroll") for (int b = 0; b < kKs * 2 * MT; ++b) As.template select<128, 1>(b * 128) = block_load(abase + (g0 * 2 * mpad + b * 8) * 16); _Pragma("unroll") for (int gg = 0; gg < kKs; ++gg) { const simd d = simd((e4 >> (8 * gg)) & 0xFF) - mx; _Pragma("unroll") for (int hh = 0; hh < 2; ++hh) { /* 16 k: words 2 hh, 2 hh + 1 */ simd B; _Pragma("unroll") for (int w = 0; w < 2; ++w) { const int j = 2 * hh + w; /* (1 + k/4) 2^(14 + d) by bits, exact in f32, then to fp16: the SIMT * kernel's (half)((1 + 0.25 k) * ldexp(16384, d)), 0 below d = -30 */ const simd kf = (m4 >> (8 * gg + 2 * j)) & 3; simd fb = (simd(d + 141) << 23) | (kf << 21); simd khf = fb.template bit_cast_view(); khf.merge(simd(0.0f), d < -30); const simd kh = khf; simd kh2; kh2.template select<16, 2>(0) = kh; kh2.template select<16, 2>(1) = kh; const simd wv = line.template select<16, 1>((gg * 4 + j) * 16); _Pragma("unroll") for (int u = 0; u < 4; ++u) { simd dw = u < 3 ? ((wv << (9 - 4 * u)) & 0x0E000E00u) : ((wv >> 3) & 0x0E000E00u); dw |= (wv << (12 - 4 * u)) & 0x80008000u; const simd hv = dw.template bit_cast_view(); B.template select<32, 1>((w * 4 + u) * 32) = hv * kh2; } } _Pragma("unroll") for (int i = 0; i < MT; ++i) { const simd A = As.template select<128, 1>(((gg * 2 + hh) * MT + i) * 128); c[i] = xmx::dpas<8, 8, float>(c[i], B, A); } } } }; /* the exponents and metadata 16 bytes a lane, four steps' worth a gather (a 4-byte gather * a step was a third of the kernel's memory messages for an eighth of its bytes); the * tail of a split that is not 16 groups long a step at a time */ int64_t g0 = g0s; if constexpr (TL) { /* tiled: a chunk of 16 groups is 256 bytes [step][lane], a step's 64 one block load; * a split's steps before its first whole chunk and after its last one at a time */ const uint32_t* et = (const uint32_t*)expe0 + tile * (kg / 16) * 64; const uint32_t* mt = (const uint32_t*)meta0 + tile * (kg / 16) * 64; for (; g0 < g1s && (g0 & 15); g0 += kKs) step(g0, block_load(et + g0 * 4), block_load(mt + g0 * 4)); for (; g0 + 4 * kKs <= g1s; g0 += 4 * kKs) { simd E = block_load(et + g0 * 4); simd Mt = block_load(mt + g0 * 4); _Pragma("unroll") for (int s4 = 0; s4 < 4; ++s4) step(g0 + kKs * s4, E.template select<16, 1>(s4 * 16), Mt.template select<16, 1>(s4 * 16)); } for (; g0 < g1s; g0 += kKs) step(g0, block_load(et + g0 * 4), block_load(mt + g0 * 4)); } else { for (; g0 + 4 * kKs <= g1s; g0 += 4 * kKs) { simd E = gather((const uint32_t*)(expe0 + g0), eoff); simd Mt = gather((const uint32_t*)(meta0 + g0), moff); _Pragma("unroll") for (int s4 = 0; s4 < 4; ++s4) step(g0 + kKs * s4, E.template select<16, 1>(s4 * 16), Mt.template select<16, 1>(s4 * 16)); } for (; g0 < g1s; g0 += kKs) step(g0, gather((const uint32_t*)(expe0 + g0), eoff), gather((const uint32_t*)(meta0 + g0), moff)); } const simd_mask<16> ok = (lane + (uint32_t)n0) < (uint32_t)N; if (splits == 1) { /* 2^(mx - 127) by bits (mx = 0: the subnormal 2^-127) */ simd sb = simd(mx) << 23; sb.merge(simd(0x00400000u), mx == 0); const simd sc = sb.template bit_cast_view(); const uint32_t esz = ydt == RAD_F32 ? 4u : 2u; const simd yoff = lane * (uint32_t)(ycs * esz); _Pragma("unroll") for (int i = 0; i < MT; ++i) _Pragma("unroll") for (int m = 0; m < 8; ++m) { const int r = i * 8 + m; if (r >= mm) continue; simd v = c[i].template select<16, 1>(m * 16) * rr[r] * sc; char* yrow = yb + (r * yrs + n0 * ycs) * (int64_t)esz; if (ydt == RAD_F32) { scatter((float*)yrow, yoff, v, ok); } else { /* bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */ const simd u = v.template bit_cast_view(); simd h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16; h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u); const simd h16 = h; scatter((uint16_t*)yrow, yoff, h16, ok); } } } else { _Pragma("unroll") for (int i = 0; i < MT; ++i) _Pragma("unroll") for (int m = 0; m < 8; ++m) { const int64_t r = (int64_t)i * 8 + m; scatter(pbuf + (sp * mpad + r) * ldp + n0, lane * 4u, c[i].template select<16, 1>(m * 16), ok); } } }); }); } } // namespace int m2_esimd(sycl::queue& q, int mt, const half* ae, const uint8_t* codes0, const uint8_t* meta0, const uint8_t* expe0, const int32_t* emax, int64_t brs, float* pbuf, int64_t N, int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits, const float* rr, char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int mm, int64_t ldp, bool tl) { auto go = [&](auto mt_, auto tl_) { return launch(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, ydt, yrs, ycs, mm, ldp); }; using T = std::true_type; using F = std::false_type; using M1 = std::integral_constant; using M2 = std::integral_constant; return mt == 1 ? (tl ? go(M1{}, T{}) : go(M1{}, F{})) : (tl ? go(M2{}, T{}) : go(M2{}, F{})); } } // namespace xe2