libxe2: a local-memory-tiled fp16 XMX GEMM for the m2xfp4 prefill -- 27B prefill 91 -> 105 tok/s

xmx_f16_tiled: a 128 x 128 work-group tile, 4 x 2 sub-groups of 32 x 64 (16 accumulators, the
large register file), K in steps of 32 with A and a VNNI B staged in local memory, double
buffered, each sub-group's A tiles loaded once a K16 step; the accumulators stored straight to an
f32 scratch and scaled into y by a second kernel (a generic store in the epilogue spilled them and
ran 3x slower than the old tiling). ~30 TFLOPS on the 27B's prefill shapes in a standalone probe
against ~18 / ~11 for the previous per-sub-group tiling (the card's measured fp16 DPAS peak: ~98).
gemm_nt_q over a 1792-token prefill 6.4 -> 3.7 s. KL against the exact reference unchanged (mean
0.0035, top-1 95.6%): the same products.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-24 05:49:41 +02:00
1 parent e9b2714081
commit 3b30d7803b
1 file changed
+93
+93
View File
@@ -17,6 +17,8 @@
#include "xe2_common.h"
#include "xe2_ops.h"
#include <cstdlib>
namespace xe2 {
namespace {
@@ -160,9 +162,100 @@ int logits_gemm_xmx(const RadArgs* a, RadStream s) {
* rows past M are not written. N a multiple of 64, K of 16. */
namespace xe2 {
/* The local-memory tiling: a 128 x 128 work-group tile (4 x 2 sub-groups of 32 x 64, 16
* accumulators each, the large register file), K in steps of 32 with A [128][32] and B VNNI
* [16 pairs][128] staged in local memory, double buffered; each sub-group's A tiles loaded once
* a K16 step. ~30 TFLOPS on the 27B's prefill shapes (gemm.cpp in scratch; the per-sub-group form
* below ~11-18, the card's DPAS peak ~98). N a multiple of 128, K of 32. */
int xmx_f16_tiled(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 N, int64_t K,
const float* rs, const float* cs) {
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);
namespace intelex = sycl::ext::intel::experimental;
syclex::properties props{intelex::grf_size<256>};
/* The accumulators go straight to an f32 [M, N] scratch (a generic store in this kernel's
* epilogue spills its 16 accumulators); a second kernel applies the factors and y's dtype. */
const int64_t mp = (int64_t)gm * BM;
float* cbuf = workspace(q, (size_t)(mp * N), 6);
if (!cbuf) return RAD_E_DEVICE;
int rc = submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<hf, 1> As(sycl::range<1>(2 * BM * BK), h);
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>(2 * (BK / 2) * BN), h);
h.parallel_for(sycl::nd_range<2>{{gm, gn * 128}, {1, 128}}, props,
[=](sycl::nd_item<2> 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(), t = (int)it.get_local_id(1);
const int sm = s / 2, sn = s % 2;
const int64_t mb0 = (int64_t)it.get_group(0) * BM, nb0 = (int64_t)it.get_group(1) * BN;
jm::joint_matrix<sycl::sub_group, hf, jm::use::a, 8, 16, jm::layout::row_major> ma[4];
jm::joint_matrix<sycl::sub_group, hf, jm::use::b, 16, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc[4][4];
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) jm::joint_matrix_fill(sg, mc[i][j], 0.0f);
auto stage_in = [&](int buf, int64_t k0) {
for (int r = 0; r < 4; ++r) {
const int idx = t + r * 128;
const int row = idx / 4, kc = (idx % 4) * 8;
const int64_t gm_ = mb0 + row;
sycl::vec<uint32_t, 4> v(0u, 0u, 0u, 0u);
if (gm_ < M) v = *(const sycl::vec<uint32_t, 4>*)(ab + gm_ * ars + k0 + kc);
*(sycl::vec<uint32_t, 4>*)&As[buf * BM * BK + row * BK + kc] = v;
}
for (int r = 0; r < 2; ++r) {
const int idx = t + r * 128;
const int n = idx / 2, kc = (idx % 2) * 16;
const sycl::vec<uint32_t, 8> v = *(const sycl::vec<uint32_t, 8>*)(bb + (nb0 + n) * brs + k0 + kc);
for (int u = 0; u < 8; ++u) Bs[buf * (BK / 2) * BN + (kc / 2 + u) * BN + n] = v[u];
}
};
stage_in(0, 0);
int buf = 0;
for (int64_t k0 = 0; k0 < K; k0 += BK) {
sycl::group_barrier(it.get_group());
if (k0 + BK < K) stage_in(buf ^ 1, k0 + BK);
for (int kk = 0; kk < BK; kk += 16) {
for (int i = 0; i < 4; ++i) {
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(&As[buf * BM * BK + (sm * SGM + i * 8) * BK + kk]);
jm::joint_matrix_load(sg, ma[i], la, BK);
}
for (int j = 0; j < 4; ++j) {
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(
(hf*)&Bs[buf * (BK / 2) * BN + (kk / 2) * BN] + (sn * SGN + j * 16) * 2);
jm::joint_matrix_load(sg, mb, lb, BN * 2);
for (int i = 0; i < 4; ++i) jm::joint_matrix_mad(sg, mc[i][j], ma[i], mb, mc[i][j]);
}
}
buf ^= 1;
}
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
const int64_t m0 = mb0 + sm * SGM + i * 8, n0 = nb0 + sn * SGN + j * 16;
auto gc = sycl::address_space_cast<sycl::access::address_space::global_space,
sycl::access::decorated::no>(cbuf + m0 * N + n0);
jm::joint_matrix_store(sg, mc[i][j], gc, (size_t)N, jm::layout::row_major);
}
});
});
});
if (rc < 0) return rc;
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]);
});
});
}
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 (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;
const size_t gm = (size_t)((mpad + kTm * kSgs - 1) / (kTm * kSgs));
const size_t gn = (size_t)(N / (kTn * kTiles));