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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-24 17:49:44 +02:00
1 parent 3f782b935c
commit c81aa869a0
4 files changed
+123 -11

No files matched your search

+67
View File
@@ -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 <oneapi/mkl/blas.hpp>
#endif
#include "xe2_ops.h"
#include <cstdlib>
@@ -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;