From b0e34c63248d6cf84dc846734bb1a5f6506fcd0e Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 13:56:02 +0200 Subject: [PATCH] libxe2: m2xfp4 verify GEMM on register DPAS -- no local-memory round trip SPV_INTEL_subgroup_matrix_multiply_accumulate gives DPAS on operands in registers (dpas_f16 in xe2_common.h; libxe2's link enables the extension). Its B operand is a lane's column as dwords of fp16 pairs, which is what a lane decodes from its codes, so the 2 <= M <= 16 m2xfp4 GEMM no longer writes the decoded weights to local memory and reads them back as a joint_matrix tile; the activation is transposed once so a lane's A column is one 16-byte load. 15-20% a call at M = 8 (N=34816 K=5120 518 -> 463 us, N=5120 K=17408 312 -> 249 us). Same products: rel_l2 at most 1.46e-3 as before (185/185 model cases). XE2_M2_JM=1 keeps the local-memory form. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/CMakeLists.txt | 5 ++- libxe2/xe2_common.h | 15 +++++++ libxe2/xe2_gemv_m2.cpp | 99 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/libxe2/CMakeLists.txt b/libxe2/CMakeLists.txt index 42d3a643..e1fc44b9 100644 --- a/libxe2/CMakeLists.txt +++ b/libxe2/CMakeLists.txt @@ -29,8 +29,11 @@ rad_add_plugin(libxe2 kernels SOURCES ${XE2_SRC}) # codes that sit exactly on an E4M3 rounding tie, which bf16 inputs hit often. # Device code is generated at link time (AOT), so every flag that shapes it goes to the link too; # on the compile line alone it changes nothing on the card. +# SPV_INTEL_subgroup_matrix_multiply_accumulate: DPAS on operands in registers (xe2_common.h's +# dpas_f16), for kernels that decode a weight straight into the B operand. set(_xe2_sycl -fsycl "-fsycl-targets=${RAD_SYCL_TARGETS}" -ffp-model=precise -ffp-contract=off - -Wno-overriding-option -foffload-fp32-prec-div -foffload-fp32-prec-sqrt) + -Wno-overriding-option -foffload-fp32-prec-div -foffload-fp32-prec-sqrt + "SHELL:-Xspirv-translator -spirv-ext=+SPV_INTEL_subgroup_matrix_multiply_accumulate") target_compile_options(libxe2 PRIVATE ${_xe2_sycl} -fvisibility=hidden) get_filename_component(_xe2_icpx_bin "${CMAKE_CXX_COMPILER}" DIRECTORY) get_filename_component(_xe2_oneapi_lib "${_xe2_icpx_bin}/../lib" ABSOLUTE) diff --git a/libxe2/xe2_common.h b/libxe2/xe2_common.h index 570270e3..2e64cd50 100644 --- a/libxe2/xe2_common.h +++ b/libxe2/xe2_common.h @@ -18,6 +18,13 @@ #include #include +/* The SPIR-V builtin behind dpas_f16 below, declared at global scope so it keeps its name. */ +typedef short xe2_short8 __attribute__((ext_vector_type(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); + namespace xe2 { namespace syclex = sycl::ext::oneapi::experimental; @@ -64,6 +71,14 @@ int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl char* yb, uint32_t ydt, int64_t yrs, int64_t ycs, int64_t M, int64_t mpad, int64_t N, int64_t K, const float* rs, const float* cs); +/* One DPAS on operands held in registers (SPV_INTEL_subgroup_matrix_multiply_accumulate; libxe2's + * link enables it), sub-group 16, fp16 8x16 by 16x16 into f32: lane k holds A[0..7][k] in `a`, lane + * n holds B's column n as eight dwords of pairs (B[2i][n], B[2i+1][n]) in `b`, and C[0..7][n] in + * `c`. joint_matrix reaches the same instruction only through memory. */ +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 */ +} + /* Runs `submit`, turning a SYCL exception into RAD_E_DEVICE with its message. */ template int submit(F&& f) { diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index 5918aa6e..593714b5 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -18,6 +18,7 @@ #include "xe2_ops.h" #include +#include namespace xe2 { @@ -213,12 +214,97 @@ int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* ema }); } +/* The same products with DPAS on registers (dpas_f16): a lane owns one output column of each of + * the sub-group's NT 16-column tiles and decodes its codes straight into the B operand -- a word's + * four (u, u + 4) pairs are B's dwords in the activation's order -- so nothing goes through local + * memory and no barrier is taken (NT = 2, a lane two columns, measured slower: 463 -> 639 us at + * N = 34816). The activation is transposed for it, at[(part K + k) * mpad + m], + * so a lane's A column (k = k0 + lane, the tile's rows) is one 16-byte load, shared by the NT + * tiles. K split as above. */ +template +int launch_dpas_raw(sycl::queue& q, const half* at, const int32_t* emax, const uint8_t* codes0, + const uint8_t* meta0, const uint8_t* expe0, int64_t brs, float* pbuf, int64_t N, + int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits) { + constexpr int64_t kCols = 128 * NT; /* columns a work-group: 8 sub-groups */ + const int64_t ntile = (N + kCols - 1) / kCols; + const size_t groups = (size_t)(ntile * splits); + const int64_t kg = K / 32; + const int64_t per = ((kg + splits - 1) / splits + kKs - 1) / kKs * kKs; + const int64_t ldp = ntile * kCols; + return submit([&] { + q.parallel_for(sycl::nd_range<1>{groups * 128, 128}, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + using H2 = sycl::vec; + using A8 = sycl::vec; + 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 int64_t tile = (int64_t)it.get_group(0) % ntile, sp = (int64_t)it.get_group(0) / ntile; + const int64_t g0s = sp * per, g1s = sycl::min(kg, g0s + per); + const int64_t n0 = tile * kCols + s * 16 * NT; + if (n0 >= N) return; /* uniform */ + /* the tiles of one sub-group share a part: part bounds are multiples of 16 * NT */ + const int64_t part = n0 < e1 ? 0 : (n0 < e2 ? 1 : 2); + const half* abase = at + (part * K + lane) * mpad; + int64_t nn[NT]; + int emx[NT]; + for (int t = 0; t < NT; ++t) { + nn[t] = sycl::min(n0 + t * 16 + lane, N - 1); + emx[t] = emax[nn[t]]; + } + xe2_float8 c[NT][MT]; + for (int t = 0; t < NT; ++t) + for (int i = 0; i < MT; ++i) c[t][i] = xe2_float8(0.0f); + for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) { + sycl::vec line[NT]; + uint32_t e4[NT], m4[NT]; + for (int t = 0; t < NT; ++t) { + line[t] = *(const sycl::vec*)(codes0 + nn[t] * (K / 2) + g0 * 16); + e4[t] = *(const uint32_t*)(expe0 + nn[t] * brs + g0); + m4[t] = *(const uint32_t*)(meta0 + nn[t] * kg + g0); + } + for (int gg = 0; gg < kKs; ++gg) + for (int hh = 0; hh < 2; ++hh) { /* 16 k: words 2 hh, 2 hh + 1 of the group */ + xe2_short8 a[MT]; + for (int i = 0; i < MT; ++i) { + const A8 av = *(const A8*)(abase + (g0 * 32 + gg * 32 + hh * 16) * mpad + i * 8); + for (int m = 0; m < 8; ++m) a[i][m] = (short)av[m]; + } + for (int t = 0; t < NT; ++t) { + const int d = (int)((e4[t] >> (8 * gg)) & 0xFF) - emx[t]; + const float sc = d < -30 ? 0.0f : sycl::ldexp(16384.0f, d); + const uint32_t mt = (m4[t] >> (8 * gg)) & 0xFF; + xe2_int8 b; + for (int w = 0; w < 2; ++w) { + const int j = 2 * hh + w; + const uint32_t word = line[t][gg * 4 + j]; + const half kh = (half)((1.0f + 0.25f * (float)((mt >> (2 * j)) & 3)) * sc); + const H2 k2(kh, kh); + for (int u = 0; u < 4; ++u) { + const uint32_t mb2 = u < 3 ? (word << (9 - 4 * u)) & 0x0E000E00u : (word >> 3) & 0x0E000E00u; + const uint32_t sb2 = (word << (12 - 4 * u)) & 0x80008000u; + b[w * 4 + u] = sycl::bit_cast(sycl::bit_cast

(mb2 | sb2) * k2); + } + } + for (int i = 0; i < MT; ++i) c[t][i] = dpas_f16(a[i], b, c[t][i]); + } + } + } + for (int t = 0; t < NT; ++t) + for (int i = 0; i < MT; ++i) + for (int m = 0; m < 8; ++m) + pbuf[(sp * mpad + (int64_t)i * 8 + m) * ldp + n0 + t * 16 + lane] = c[t][i][m]; + }); + }); +} + 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, int64_t ycs, int64_t e1, int64_t e2) { const int mt = (int)((M + 7) / 8); const int64_t mpad = (int64_t)mt * 8, kg = K / 32, kb = kg; + /* the register form unless XE2_M2_JM=1 asks for the local-memory one */ + static const bool jm_form = std::getenv("XE2_M2_JM") != nullptr; + const bool raw = !jm_form; 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)); @@ -244,8 +330,9 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 const float f = 1.0f / R; for (int64_t c = t; c < acols; c += 256) { const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1); - a16[m * acols + c] = m < M ? (half)(e4m3(ab[m * ars + src]) * rad_load_f32(sab, sadt, m * srs + src / 128) * f) - : (half)0.0f; + a16[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; } }); q.parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) { @@ -262,8 +349,12 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 }); }); if (rc < 0) return rc; - rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits) - : launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits); + if (raw) + rc = mt == 1 ? launch_dpas_raw<1, 1>(q, a16, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits) + : launch_dpas_raw<2, 1>(q, a16, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits); + else + rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits) + : launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits); if (rc < 0) return rc; char* yb = (char*)Y->data; const uint32_t ydt = Y->dtype;