diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index a578a0bb..b56ee874 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -316,6 +316,16 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const }); } +} // namespace + +/* xe2_gemv_m2_esimd.cpp: launch_dpas_raw's arithmetic in ESIMD, the activation as [c / 16][m][c % 16] */ +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); + +namespace { + int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8_t* codes0, const uint8_t* meta0, const uint8_t* expe0, int64_t brs, const RadTensor* Y, int64_t M, int64_t N, int64_t K, int64_t acols, int64_t scols, int64_t ars, int64_t srs, int64_t yrs, @@ -328,6 +338,9 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 const int64_t ntile = (N + 127) / 128, ldp = ntile * 128; constexpr int64_t kFill = 160; const int splits = (int)std::min(std::max(1, (kFill + ntile - 1) / ntile), std::max(1, kg / kKs)); + /* the ESIMD form of the register kernel unless XE2_M2_SIMT=1; it stores f32 and bf16 itself */ + static const bool simt = std::getenv("XE2_M2_SIMT") != nullptr; + const bool es = raw && !simt && (splits > 1 || Y->dtype == RAD_F32 || Y->dtype == RAD_BF16); const int64_t abytes = (mpad * acols * 2 + 255) / 256 * 256, rbytes = (mpad * 4 + 255) / 256 * 256; float* ws = workspace(q, (size_t)((abytes + rbytes + N * 4) / 4 + 64)); float* pbuf = workspace(q, (size_t)(splits * mpad * ldp), 2); @@ -354,7 +367,7 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 const float f = 1.0f / R; for (int64_t c = ch * cspan + t; c < sycl::min(acols, (ch + 1) * cspan); c += 256) { const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1); - a16[raw ? c * mpad + m : m * acols + c] = + a16[es ? ((c >> 4) * mpad + m) * 16 + (c & 15) : raw ? c * mpad + m : m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 128) * f) : (half)0.0f; } @@ -376,7 +389,10 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 if (rc < 0) return rc; char* yb = (char*)Y->data; const uint32_t ydt = Y->dtype; - if (raw) + if (es) + rc = m2_esimd(q, mt, a16, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, ydt, + yrs, ycs, (int)M, ldp); + else if (raw) rc = mt == 1 ? launch_dpas_raw<1, 1>(q, a16, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, ydt, yrs, ycs, (int)M) : launch_dpas_raw<2, 1>(q, a16, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits, diff --git a/libxe2/xe2_gemv_m2_esimd.cpp b/libxe2/xe2_gemv_m2_esimd.cpp new file mode 100644 index 00000000..b08a0cc1 --- /dev/null +++ b/libxe2/xe2_gemv_m2_esimd.cpp @@ -0,0 +1,161 @@ +/* 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. 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 simd eoff = col * (uint32_t)brs; + const simd moff = col * (uint32_t)kg; + /* the column's largest exponent: read here when this thread has the whole K, else the + * caller's emax pass */ + simd mx = 0; + if (splits == 1) { + for (int64_t g = 0; g < kg; g += 4) { + const simd e = gather((const uint32_t*)(expe0 + g), eoff); + _Pragma("unroll") for (int b = 0; b < 4; ++b) mx = max(mx, simd((e >> (8 * b)) & 0xFF)); + } + } else { + mx = gather(emax, col * 4u); + } + simd c[MT]; + _Pragma("unroll") for (int i = 0; i < MT; ++i) c[i] = 0.0f; + for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) { + simd line; /* [word 16][lane 16] */ + _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); + const simd e4 = gather((const uint32_t*)(expe0 + g0), eoff); + const simd m4 = gather((const uint32_t*)(meta0 + g0), moff); + /* 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); + } + } + } + } + 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) { + return mt == 1 ? launch<1>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, + ydt, yrs, ycs, mm, ldp) + : launch<2>(q, ae, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, + ydt, yrs, ycs, mm, ldp); +} + +} // namespace xe2