From c81aa869a0d8299defd2d4e6f85cf231d77a7fa0 Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 17:49:44 +0200 Subject: [PATCH] libxe2: prefill GEMMs on oneMKL -- 27B prefill 208 -> 305 tok/s oneMKL's SYCL GEMM (part of oneAPI; libxe2 links it when CMake finds it, RAD_XE2_MKL=OFF or XE2_NO_MKL=1 keep the hand-written kernels) runs the fp16 prefill GEMMs at 85-91 TFLOPS on random data at the 27B's shapes, deterministic run to run, against ~23-30 for the hand-tiled XMX kernel: xmx_f16_scaled (the m2xfp4 prefill) and xmx_f16_batched (the rotation's) go to its gemm and gemm_batch, and bf16 gemm_nt at M >= 17 to its bf16 GEMM (N = 96 had fallen to the exact kernel, 5 ms a call). The m2xfp4 weight decode to fp16 now takes a (column, 32-group) a work-item. fp16 or bf16 products with f32 accumulation, as before: the 27B's cases 133/133 (rel_l2 <= 1.42e-3), the fixture 1410/1410. Prefill GEMMs over a 1882-token prompt 4.06 -> 1.66 s. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- docs/SYCL_PROGRESS.md | 17 +++++++++++ libxe2/CMakeLists.txt | 21 +++++++++++++ libxe2/xe2_gemm_xmx.cpp | 67 +++++++++++++++++++++++++++++++++++++++++ libxe2/xe2_gemv_m2.cpp | 29 +++++++++++------- 4 files changed, 123 insertions(+), 11 deletions(-) diff --git a/docs/SYCL_PROGRESS.md b/docs/SYCL_PROGRESS.md index 0734c442..a1ab48c3 100644 --- a/docs/SYCL_PROGRESS.md +++ b/docs/SYCL_PROGRESS.md @@ -610,6 +610,23 @@ tok/s at 2584 tokens (unchanged). - VTune's GPU analyses do not run here (it does not recognise the host CPU); the per-kernel split above came from temporary waits around each sub-kernel. +## Qwen3.8-27B: prefill (2026-10-24, continued) + +1882-token prompt, device time by op (`--profile-ops`), before -> after: + +| op | before | after | how | +|---|---|---|---| +| gdn_chunk_scan | 4.80 s | 0.92 | typed loads for the engine's bf16/f32 operands (4.8 -> 2.7), then 1024 work-items a work-group: its ~124 KB of local memory leaves one a Xe core, and 256 left three quarters of the core idle (2.7 -> 0.92). Exact | +| gdn_kkt_solve | 1.51 | 0.33 | a work-group a (head, chunk) -- chunks are independent -- the row terms over 512 work-items, then the solve from local memory. Exact | +| gdn_conv_prep | 0.54 | 0.45 | the conv a work-item a (token, channel), the gate cumsum a work-item a (head, chunk). Exact | +| gdn_gated_rmsnorm | 0.38 | 0.20 | the row staged in local memory before the ordered sum. Exact | +| m2xfp4 GEMMs (M >= 17) | 4.06 | see below | oneMKL's fp16 GEMM (f32 accumulate) in place of the hand-tiled XMX kernel | + +oneMKL (part of oneAPI, linked by libxe2 when found; `RAD_XE2_MKL=OFF` or `XE2_NO_MKL=1` keeps the +hand-tiled kernel): 85-91 TFLOPS on random fp16 data at the 27B's shapes (probe `mklg.cpp`: +M=941 N=34816 K=5120 3.8 ms, M=1882 N=5120 K=17408 3.7 ms), deterministic run to run, against +~23-30 for the hand-tiled kernel. The prefill rotation's batched GEMMs use its `gemm_batch`. + ## Next steps 1. The verify round, ~137 ms now against vLLM's ~68: the m2xfp4 verify GEMMs (~75 ms, ~2.5x their diff --git a/libxe2/CMakeLists.txt b/libxe2/CMakeLists.txt index e1fc44b9..0ad0552e 100644 --- a/libxe2/CMakeLists.txt +++ b/libxe2/CMakeLists.txt @@ -39,3 +39,24 @@ get_filename_component(_xe2_icpx_bin "${CMAKE_CXX_COMPILER}" DIRECTORY) get_filename_component(_xe2_oneapi_lib "${_xe2_icpx_bin}/../lib" ABSOLUTE) target_link_options(libxe2 PRIVATE ${_xe2_sycl} "LINKER:-rpath,${_xe2_oneapi_lib}" "LINKER:--no-undefined") + +# oneMKL's SYCL BLAS for the prefill-sized fp16 GEMMs (xe2_gemm_xmx.cpp, xmx_f16_scaled): ~85-90 +# TFLOPS at the 27B's shapes against ~25 for the hand-tiled kernel, deterministic run to run. Part +# of oneAPI, found beside icpx; without it the hand-tiled kernel serves (RAD_XE2_MKL=OFF forces that). +option(RAD_XE2_MKL "libxe2: oneMKL's SYCL GEMM for the prefill fp16 GEMMs" ON) +get_filename_component(_xe2_oneapi_root "${_xe2_icpx_bin}/../../.." ABSOLUTE) +find_library(_xe2_mkl_sycl_blas mkl_sycl_blas + HINTS "$ENV{MKLROOT}/lib" "${_xe2_oneapi_root}/mkl/latest/lib" NO_DEFAULT_PATH) +find_path(_xe2_mkl_inc oneapi/mkl/blas.hpp + HINTS "$ENV{MKLROOT}/include" "${_xe2_oneapi_root}/mkl/latest/include" NO_DEFAULT_PATH) +if(RAD_XE2_MKL AND _xe2_mkl_sycl_blas AND _xe2_mkl_inc) + get_filename_component(_xe2_mkl_lib "${_xe2_mkl_sycl_blas}" DIRECTORY) + message(STATUS "libxe2: oneMKL SYCL BLAS from ${_xe2_mkl_lib}") + target_compile_definitions(libxe2 PRIVATE XE2_HAVE_MKL=1 MKL_ILP64) + target_include_directories(libxe2 SYSTEM PRIVATE "${_xe2_mkl_inc}") + target_link_libraries(libxe2 PRIVATE "${_xe2_mkl_sycl_blas}" "${_xe2_mkl_lib}/libmkl_intel_ilp64.so" + "${_xe2_mkl_lib}/libmkl_sequential.so" "${_xe2_mkl_lib}/libmkl_core.so") + target_link_options(libxe2 PRIVATE "LINKER:-rpath,${_xe2_mkl_lib}") +else() + message(STATUS "libxe2: no oneMKL SYCL BLAS; the prefill GEMMs use the hand-tiled XMX kernel") +endif() diff --git a/libxe2/xe2_gemm_xmx.cpp b/libxe2/xe2_gemm_xmx.cpp index bc717c93..e1303ab5 100644 --- a/libxe2/xe2_gemm_xmx.cpp +++ b/libxe2/xe2_gemm_xmx.cpp @@ -15,6 +15,10 @@ * (docs/SYCL_PROGRESS.md D13). XE2_EXACT=1 sends every launch to the exact kernel. */ #include "xe2_common.h" + +#if XE2_HAVE_MKL +#include +#endif #include "xe2_ops.h" #include @@ -84,6 +88,35 @@ int xmx(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, (A->rank < 2 || A->stride[A->rank - 1] == 1) && (B->rank < 2 || B->stride[B->rank - 1] == 1) && rad_ptr_aligned(A->data, 16) && rad_ptr_aligned(B->data, 16); +#if XE2_HAVE_MKL + /* oneMKL's bf16 GEMM (f32 accumulate and C) for any N: the joint_matrix kernel below needs N a + * multiple of 64, and N = 96 (the DeltaNet's a/b projections) fell to the exact kernel, 5 ms a + * prefill call. The products are exact in f32 either way; the sum order is the library's. */ + static const bool no_mkl = std::getenv("XE2_NO_MKL") != nullptr; + if (!no_mkl && M >= 17 && A->dtype == RAD_BF16 && B->dtype == RAD_BF16 && + (Y->dtype == RAD_BF16 || Y->dtype == RAD_F32) && (A->rank < 2 || A->stride[A->rank - 1] == 1) && + (B->rank < 2 || B->stride[B->rank - 1] == 1)) { + sycl::queue* q = queue_of(st); + if (!q) return RAD_E_DEVICE; + float* cbuf = workspace(*q, (size_t)(M * N), 6); + if (!cbuf) return RAD_E_DEVICE; + try { + oneapi::mkl::blas::row_major::gemm(*q, oneapi::mkl::transpose::nontrans, oneapi::mkl::transpose::trans, + M, N, K, 1.0f, (const bf16*)A->data, ars, (const bf16*)B->data, brs, + 0.0f, cbuf, N); + } catch (const std::exception& e) { + return refuse("oneMKL gemm (bf16): %s", e.what()); + } + char* yb = (char*)Y->data; + const uint32_t ydt = Y->dtype; + return submit([&] { + q->parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> id) { + const int64_t m = (int64_t)id[0] / N, n = (int64_t)id[0] % N; + rad_store_f32(yb, ydt, m * yrs + n * ycs, cbuf[id[0]]); + }); + }); + } +#endif const int64_t M8 = M / kTm * kTm; if (!ok || M8 == 0) return exact(a, st); @@ -256,6 +289,19 @@ int xmx_f16_tiled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl: int xmx_f16_batched(sycl::queue& q, const sycl::half* ab, int64_t ars, int64_t a_bs, const sycl::half* bb, int64_t brs, int64_t b_bs, float* cb, int64_t c_bs, int64_t M, int64_t N, int64_t K, int64_t batch) { +#if XE2_HAVE_MKL + static const bool no_mkl = std::getenv("XE2_NO_MKL") != nullptr; + if (!no_mkl) { /* oneMKL's strided batch: the same fp16 products, f32 accumulate, M rows of C */ + try { + oneapi::mkl::blas::row_major::gemm_batch(q, oneapi::mkl::transpose::nontrans, oneapi::mkl::transpose::trans, + M, N, K, 1.0f, ab, ars, a_bs, bb, brs, b_bs, 0.0f, cb, N, + c_bs, batch); + } catch (const std::exception& e) { + return refuse("oneMKL gemm_batch: %s", e.what()); + } + return RAD_OK; + } +#endif using hf = sycl::half; constexpr int BM = 128, BN = 128, BK = 32, SGM = 32, SGN = 64; const size_t gm = (size_t)((M + BM - 1) / BM), gn = (size_t)(N / BN); @@ -332,6 +378,27 @@ int xmx_f16_batched(sycl::queue& q, const sycl::half* ab, int64_t ars, int64_t a int xmx_f16_scaled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl::half* bb, int64_t brs, 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) { +#if XE2_HAVE_MKL + static const bool no_mkl = std::getenv("XE2_NO_MKL") != nullptr; + if (!no_mkl && M >= 17) { + /* oneMKL: fp16 operands, f32 accumulate and C, into the f32 scratch; the factors and y's + * dtype by the epilogue below, as the tiled kernel's */ + float* cbuf = workspace(q, (size_t)(M * N), 6); + if (!cbuf) return RAD_E_DEVICE; + try { + oneapi::mkl::blas::row_major::gemm(q, oneapi::mkl::transpose::nontrans, oneapi::mkl::transpose::trans, + M, N, K, 1.0f, ab, ars, bb, brs, 0.0f, cbuf, N); + } catch (const std::exception& e) { + return refuse("oneMKL gemm: %s", e.what()); + } + return submit([&] { + q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> id) { + const int64_t m = (int64_t)id[0] / N, n = (int64_t)id[0] % N; + rad_store_f32(yb, ydt, m * yrs + n * ycs, cbuf[id[0]] * rs[m] * cs[n]); + }); + }); + } +#endif if (N % 128 == 0 && K % 32 == 0 && ars % 8 == 0 && brs % 16 == 0 && !std::getenv("XE2_XMX_PLAIN")) return xmx_f16_tiled(q, ab, ars, bb, brs, yb, ydt, yrs, ycs, M, N, K, rs, cs); using hf = sycl::half; diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index f6c413a6..a578a0bb 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -514,7 +514,7 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) { rad_tensor_numel(A) >= M * acols && rad_tensor_numel(AS) >= M * scols && rad_tensor_numel(BS) >= N * kb && (!(pb1 > 0 || pb2 > 0) || ((e1 % 16) == 0 && (e2 % 16) == 0 && !(e1 == e2 && e2 < N))); - if (!ok || K % 16 || e1 % 64 || e2 % 64 || N % 64) return gemm_nt_q_m2(a, st); + if (!ok || K % 16 || e1 % 64 || e2 % 64 || N % 64 || !rad_ptr_aligned(B->data, 16)) return gemm_nt_q_m2(a, st); sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; /* fp16 operands: the activation fp16(a * a_scale / R_m), R_m = 224 * the row's largest scale @@ -558,18 +558,25 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) { emax[i[0]] = mx; cs[i[0]] = sycl::ldexp(1.0f, mx - 127); }); - const int64_t nw = N * (K / 2); /* a work-item a code byte: two weights */ - q->parallel_for(sycl::range<1>((size_t)nw), [=](sycl::id<1> i) { - const int64_t n = (int64_t)i[0] / (K / 2), j = (int64_t)i[0] % (K / 2); - const uint8_t byte = codes0[n * (K / 2) + j]; + /* a work-item a (column, 32-group): its 16 code bytes, exponent and meta byte read once, + * 32 weights stored 64 bytes at a time (a work-item a code byte re-read the group's + * exponent and meta for every weight: ~3 of a 6.7 ms gate_up call) */ + const int64_t ng = N * kb; + q->parallel_for(sycl::range<1>((size_t)ng), [=](sycl::id<1> i) { + const int64_t n = (int64_t)i[0] / kb, g = (int64_t)i[0] % kb; constexpr float lut[16] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f, -0.0f, -0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f }; - const int mx = emax[n]; - for (int hh = 0; hh < 2; ++hh) { - const int64_t k = 2 * j + hh; - const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + (k >> 5)] - mx); - const float km = 1.0f + 0.25f * (float)((meta0[n * kb + (k >> 5)] >> (2 * ((k & 31) >> 3))) & 3); - w16[n * K + k] = (half)(lut[(byte >> (4 * hh)) & 0xF] * km * ev); + const sycl::vec cb = *(const sycl::vec*)(codes0 + n * (K / 2) + g * 16); + const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + g] - emax[n]); + const uint32_t mt = meta0[n * kb + g]; + for (int q8 = 0; q8 < 4; ++q8) { /* 8 weights: the subgroup q8 of the group */ + const float km = 1.0f + 0.25f * (float)((mt >> (2 * q8)) & 3); + sycl::vec o; + for (int u = 0; u < 8; ++u) { + const int kk = q8 * 8 + u; + o[u] = (half)(lut[(cb[kk >> 1] >> (4 * (kk & 1))) & 0xF] * km * ev); + } + *(sycl::vec*)(w16 + n * K + g * 32 + q8 * 8) = o; } }); });