From 006452c2792ab8bc2f39faae6f9407f6747732be Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 15:46:35 +0200 Subject: [PATCH] libxe2: the w2a8 draft head on register int8 DPAS -- 4.2 -> 3.05 ms a round dpas_s8u8 (xe2_common.h: SPV_INTEL_subgroup_matrix_multiply_accumulate's int8 form, K = 32, A signed, B unsigned; layout checked in a probe) takes a lane's column of codes as B straight from registers, so the group sums no longer go through a local-memory tile and two sub-group barriers a step; the activation is staged once a work-group as A columns, and a step's codes are one 8- or 16-byte load. The group terms are libref's, in order, as before: bit-identical to the libref-checked sub-group form on 50/50 live calls (a temporary cross-check), fixture 228/228. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_common.h | 7 ++++ libxe2/xe2_gemm_zp.cpp | 83 +++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/libxe2/xe2_common.h b/libxe2/xe2_common.h index 2e64cd50..945ed877 100644 --- a/libxe2/xe2_common.h +++ b/libxe2/xe2_common.h @@ -24,6 +24,8 @@ typedef int xe2_int8 __attribute__((ext_vector_type(8))); typedef float xe2_float8 __attribute__((ext_vector_type(8))); extern SYCL_EXTERNAL xe2_float8 __spirv_SubgroupMatrixMultiplyAccumulateINTEL(int, xe2_short8, xe2_int8, xe2_float8, int); +extern SYCL_EXTERNAL xe2_int8 __spirv_SubgroupMatrixMultiplyAccumulateINTEL(int, xe2_short8, xe2_int8, + xe2_int8, int); namespace xe2 { @@ -78,6 +80,11 @@ int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl inline xe2_float8 dpas_f16(xe2_short8 a, xe2_int8 b, xe2_float8 c) { return __spirv_SubgroupMatrixMultiplyAccumulateINTEL(16, a, b, c, 0xC00); /* A, B packed fp16 */ } +/* The int8 form, K = 32, exact in int32: lane l holds A[0..7][2l, 2l + 1] (a pair of int8 a short), + * lane n B's column n as eight dwords of four consecutive k; A signed, B unsigned (0x31). */ +inline xe2_int8 dpas_s8u8(xe2_short8 a, xe2_int8 b, xe2_int8 c) { + return __spirv_SubgroupMatrixMultiplyAccumulateINTEL(32, a, b, c, 0x31); +} /* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */ template diff --git a/libxe2/xe2_gemm_zp.cpp b/libxe2/xe2_gemm_zp.cpp index 8946a92f..616babf0 100644 --- a/libxe2/xe2_gemm_zp.cpp +++ b/libxe2/xe2_gemm_zp.cpp @@ -288,14 +288,28 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint }); } -/* The same group terms with the integer sums on XMX: an int8 DPAS (8 x 32 by 32 x 16) forms each - * group's sum of a * code exactly in int32 for up to 8 rows and 16 columns at once. The codes - * unpack straight into the packed B operand: four codes along k are one dword, and the step - * order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as stored. - * A sub-group owns 16 columns and walks the groups in order, each lane adding its column's terms - * as the per-output form does -- libref's chain, at one read of the weight. */ +/* The same group terms with the integer sums on XMX: an int8 DPAS on registers (dpas_s8u8, 8 x 32 + * by 32 x 16) forms each group's sum of a * code exactly in int32 for up to 8 rows and 16 columns + * at once. The codes unpack straight into a lane's B column: four codes along k are one dword, and + * the step order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as + * stored (staged once a work-group as A columns). A sub-group owns 16 columns and walks the groups + * in order, each lane adding its column's terms as the per-output form does -- libref's chain, at + * one read of the weight and without the B tile's trip through local memory. */ constexpr int kZpDpasSgs = 16; +/* The 4 codes of logical dword j of a 32-column step, from the step's codes in registers: 8 bytes + * (u2, a byte a dword) or 16 (u4, two). */ +template +inline uint32_t zp_dword_reg(const uint32_t* w, int j) { + if constexpr (CBITS == 2) { + const uint32_t b = (w[j / 4] >> (8 * (j % 4))) & 0xFFu; + return (b & 3u) | ((b >> 2) & 3u) << 8 | ((b >> 4) & 3u) << 16 | ((b >> 6) & 3u) << 24; + } else { + const uint32_t h = (w[j / 2] >> (16 * (j % 2))) & 0xFFFFu; + return (h & 15u) | ((h >> 4) & 15u) << 8 | ((h >> 8) & 15u) << 16 | ((h >> 12) & 15u) << 24; + } +} + template inline uint32_t zp_dword(const uint8_t* crow, int64_t d) { /* the 4 codes of logical dword d */ if constexpr (CBITS == 2) { @@ -312,24 +326,22 @@ int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint const char* sumb, uint32_t sumdt, int64_t asum_s, int64_t mpad, const uint8_t* bb, int64_t brow, const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, int64_t N, int64_t K, int64_t group, int mm) { - namespace jm = sycl::ext::oneapi::experimental::matrix; const int64_t G = K / group; const size_t wgs = (size_t)((N + 16 * kZpDpasSgs - 1) / (16 * kZpDpasSgs)); return submit([&] { q.submit([&](sycl::handler& h) { - sycl::local_accessor As(sycl::range<1>((size_t)(8 * K)), h); - sycl::local_accessor Bs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h); - sycl::local_accessor Cs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h); + /* the activation as A columns: at[(k / 2) * 8 + m] is the pair (a[m][k], a[m][k + 1]) */ + sycl::local_accessor At(sycl::range<1>((size_t)(4 * K)), h); h.parallel_for(sycl::nd_range<1>{wgs * kZpDpasSgs * kSub, (size_t)kZpDpasSgs * kSub}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { const sycl::sub_group sg = it.get_sub_group(); const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id(); const int t = (int)it.get_local_id(0); - for (int64_t i = t; i < 2 * K; i += kZpDpasSgs * kSub) { /* 8 rows, 4 bytes at a time */ - const int64_t m = i / (K / 4), c = (i % (K / 4)) * 4; - uint32_t v = 0u; - if (m < mm) v = *(const uint32_t*)(ab + m * ars + c); - *(uint32_t*)&As[m * K + c] = v; + for (int64_t i = t; i < 4 * K; i += kZpDpasSgs * kSub) { + const int64_t m = i % 8, kp = i / 8; + uint16_t v = 0; + if (m < mm) v = *(const uint16_t*)(ab + m * ars + 2 * kp); + At[i] = v; } sycl::group_barrier(it.get_group()); const int64_t n0 = ((int64_t)it.get_group(0) * kZpDpasSgs + s) * 16; @@ -344,44 +356,38 @@ int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint sa[m] = m < mm ? rad_load_f32(sab, sadt, m * sa_rs) : 0.0f; acc[m] = 0.0f; } - uint32_t* bt = &Bs[s * 8 * 16]; - int32_t* ct = &Cs[s * 8 * 16]; - auto lb = sycl::address_space_cast((int8_t*)bt); - auto lc = sycl::address_space_cast(ct); - jm::joint_matrix ma; - jm::joint_matrix mb; - jm::joint_matrix mc; for (int64_t g = 0; g < G; ++g) { - jm::joint_matrix_fill(sg, mc, 0); + xe2_int8 c(0); for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 32) { /* stored dword r of this step holds logical dword (r & ~3) | swap(r & 3) */ + uint32_t w[CBITS]; /* the step's codes: 8 bytes (u2) or 16 (u4) */ + if constexpr (CBITS == 2) { + const sycl::vec v = *(const sycl::vec*)(crow + k0 / 4); + w[0] = v[0]; w[1] = v[1]; + } else { + const sycl::vec v = *(const sycl::vec*)(crow + k0 / 2); + for (int u = 0; u < 4; ++u) w[u] = v[u]; + } + xe2_int8 b; for (int r = 0; r < 8; ++r) { const int ld = (r & ~3) | ((r & 1) << 1) | ((r & 2) >> 1); - bt[r * 16 + lane] = zp_dword(crow, k0 / 4 + ld); + b[r] = (int)zp_dword_reg(w, ld); } - sycl::group_barrier(sg); - auto la = sycl::address_space_cast(&As[k0]); - jm::joint_matrix_load(sg, ma, la, (size_t)K); - jm::joint_matrix_load(sg, mb, lb, 64); - jm::joint_matrix_mad(sg, mc, ma, mb, mc); - sycl::group_barrier(sg); + const sycl::vec av = *(const sycl::vec*)&At[(k0 / 2 + lane) * 8]; + xe2_short8 a; + for (int m = 0; m < 8; ++m) a[m] = (short)av[m]; + c = dpas_s8u8(a, b, c); } - jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major); - sycl::group_barrier(sg); if (live) { const float sb = sc.at(n, g); const float z = (float)(int32_t)zp.at(n, g); for (int m = 0; m < 8; ++m) { if (m >= mm) break; - const float sub = (float)ct[m * 16 + lane] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s); + const float sub = (float)c[m] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s); const float term = sub * sa[m] * sb; acc[m] += term; } } - sycl::group_barrier(sg); } if (live) for (int m = 0; m < 8; ++m) { @@ -427,7 +433,8 @@ int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) { const int64_t sa_rs = row_stride(AS, 1); const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1; const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1; - if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 4 == 0 && + if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 2 == 0 && + brow % 16 == 0 && rad_ptr_aligned(B->data, 16) && !std::getenv("XE2_ZP_GEMV")) { auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>; int rc = dp(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,