libxe2: m2xfp4 codes stored tiled at load -- the verify GEMM's step one block load, gate_up 240 -> 222 us

Where N % 16 == 0 and K % 128 == 0 the layout hook asks for "xe2.m2.tiled16": a 16-column tile's
128-column step is 1 KiB, dword w of its columns side by side, the ESIMD kernel's [word][lane]
registers as they lie (four block loads in place of four d32x4 gathers). M = 1 goes to the same
kernel when tiled. Every m2 reader in libxe2 (exact, SIMT GEMV, jm and SIMT DPAS forms, prefill
decode) reads the tiled form; unrelayout inverts it for the oracle.

rad-kbench against libref: 70 checked, 70 passed (worst rel_l2 1.2e-3). M = 8: gate_up 240 -> 222,
down 140 -> 119, N=1024 50 -> 25 us. Greedy output identical (8 prompts x 64 tokens); 512-token
decode 31.8 -> 33.3 tok/s.

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-26 01:47:12 +01:00
1 parent 2a764d2d38
commit 68d510b1f4
4 files changed
+107 -37

No files matched your search

+44 -17
View File
@@ -64,7 +64,7 @@ inline float e4m3(uint8_t b) {
template <int MM, int NC>
int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* codes0,
const uint8_t* meta0, const uint8_t* expe0, char* yb, uint32_t ydt, int64_t N, int64_t K,
int64_t acols, int64_t brs, int64_t yrs, int64_t ycs, int mm, int64_t e1, int64_t e2) {
int64_t acols, int64_t brs, int64_t yrs, int64_t ycs, int mm, int64_t e1, int64_t e2, bool tl) {
const int64_t per_wg = 16 * NC;
const size_t groups = (size_t)((N + per_wg - 1) / per_wg);
const int64_t kg = K / 32;
@@ -87,7 +87,11 @@ int launch(sycl::queue& q, const half* a16, const float* rr, const uint8_t* code
for (int64_t g = lane; g < kg; g += kSub) {
for (int c = 0; c < NC; ++c) {
const int64_t n = n0 + (c < nc ? c : 0);
const sycl::vec<uint32_t, 4> cw = *(const sycl::vec<uint32_t, 4>*)(codes0 + n * (K / 2) + g * 16);
sycl::vec<uint32_t, 4> cw;
if (tl)
for (int j = 0; j < 4; ++j) cw[j] = *(const uint32_t*)(codes0 + m2t_off(n, g * 16 + j * 4, K));
else
cw = *(const sycl::vec<uint32_t, 4>*)(codes0 + n * (K / 2) + g * 16);
const float ev = sycl::ldexp(1.0f, (int)expe0[n * brs + g] - 127);
const uint32_t mt = meta0[n * kg + g];
H2 w2[16];
@@ -144,7 +148,7 @@ constexpr int kKs = 4; /* 32-groups a step: one 64-byte line of a column's cod
template <int MT>
int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, 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) {
float* pbuf, int64_t N, int64_t K, int64_t e1, int64_t e2, int64_t mpad, int splits, bool tl) {
const int64_t ntile = (N + 127) / 128;
const size_t groups = (size_t)(ntile * splits);
const int64_t kg = K / 32;
@@ -172,7 +176,11 @@ int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* ema
jm::joint_matrix<sycl::sub_group, float, jm::use::accumulator, 8, 16> mc[MT];
for (int i = 0; i < MT; ++i) jm::joint_matrix_fill(sg, mc[i], 0.0f);
for (int64_t g0 = g0s; g0 < g1s; g0 += kKs) {
const sycl::vec<uint32_t, 16> line = *(const sycl::vec<uint32_t, 16>*)(crow + g0 * 16);
sycl::vec<uint32_t, 16> line;
if (tl)
for (int w = 0; w < 16; ++w) line[w] = *(const uint32_t*)(codes0 + m2t_off(n, g0 * 16 + w * 4, K));
else
line = *(const sycl::vec<uint32_t, 16>*)(crow + g0 * 16);
const uint32_t e4 = *(const uint32_t*)(expe0 + n * brs + g0);
const uint32_t m4 = *(const uint32_t*)(meta0 + n * kg + g0);
for (int gg = 0; gg < kKs; ++gg) {
@@ -225,7 +233,7 @@ template <int MT, int NT>
int launch_dpas_raw(sycl::queue& q, const half* at, 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, const float* rr, char* yb, uint32_t ydt,
int64_t yrs, int64_t ycs, int mm) {
int64_t yrs, int64_t ycs, int mm, bool tl) {
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);
@@ -269,7 +277,11 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const
sycl::vec<uint32_t, 16> line[NT];
uint32_t e4[NT], m4[NT];
for (int t = 0; t < NT; ++t) {
line[t] = *(const sycl::vec<uint32_t, 16>*)(codes0 + nn[t] * (K / 2) + g0 * 16);
if (tl)
for (int w = 0; w < 16; ++w)
line[t][w] = *(const uint32_t*)(codes0 + m2t_off(nn[t], g0 * 16 + w * 4, K));
else
line[t] = *(const sycl::vec<uint32_t, 16>*)(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);
}
@@ -322,7 +334,7 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const uint8_t* codes0, const
int m2_esimd(sycl::queue& q, int mt, const half* ae, const uint8_t* codes0, const uint8_t* meta0,
const uint8_t* expe0, const int32_t* emax, int64_t brs, float* pbuf, int64_t N, int64_t K,
int64_t e1, int64_t e2, int64_t mpad, int splits, const float* rr, char* yb, uint32_t ydt,
int64_t yrs, int64_t ycs, int mm, int64_t ldp);
int64_t yrs, int64_t ycs, int mm, int64_t ldp, bool tl);
namespace {
@@ -332,6 +344,7 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
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;
const bool tl = m2_tiled(N, K); /* the stored form gemm_m2_layout asked for */
/* 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;
@@ -398,15 +411,15 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8
const uint32_t ydt = Y->dtype;
if (es)
rc = m2_esimd(q, mt, a16, codes0, meta0, expe0, emax, brs, pbuf, N, K, e1, e2, mpad, splits, rr, yb, ydt,
yrs, ycs, (int)M, ldp);
yrs, ycs, (int)M, ldp, tl);
else if (raw)
rc = mt == 1 ? launch_dpas_raw<1, 1>(q, a16, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits,
rr, yb, ydt, yrs, ycs, (int)M)
rr, yb, ydt, yrs, ycs, (int)M, tl)
: launch_dpas_raw<2, 1>(q, a16, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits,
rr, yb, ydt, yrs, ycs, (int)M);
rr, yb, ydt, yrs, ycs, (int)M, tl);
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);
rc = mt == 1 ? launch_dpas<1>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits, tl)
: launch_dpas<2>(q, a16, acols, emax, codes0, meta0, expe0, brs, pbuf, N, K, e1, e2, mpad, splits, tl);
if (rc < 0 || (raw && splits == 1)) return rc;
return submit([&] {
q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> i) {
@@ -453,8 +466,10 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
const uint8_t* codes0 = (const uint8_t*)B->data;
const uint8_t* meta0 = codes0 + N * (K / 2);
const int mm = (int)M;
/* the verify band on XMX, where its line loads fit: K a multiple of 128, aligned rows */
if (mm >= 2 && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, 4) &&
const bool tl = m2_tiled(N, K);
/* the verify band on XMX, where its line loads fit: K a multiple of 128, aligned rows; M = 1 too
* when the codes are tiled (the ESIMD kernel's form, which the lane-a-group GEMV reads poorly) */
if ((mm >= 2 || tl) && K % 128 == 0 && brs % 4 == 0 && rad_ptr_aligned(BS->data, 4) &&
((uintptr_t)meta0 & 3) == 0 && (K / 2) % 64 == 0 && rad_ptr_aligned(B->data, 64))
return m2_dpas(*q, A, AS, codes0, meta0, (const uint8_t*)BS->data, brs, Y, M, N, K, acols, scols,
ars, srs, yrs, ycs, e1, e2);
@@ -489,7 +504,7 @@ int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream st) {
auto go = [&](auto tag, auto cols) {
constexpr int MM = decltype(tag)::value, NC = decltype(cols)::value;
return launch<MM, NC>(*q, a16, rr, codes0, meta0, (const uint8_t*)BS->data, (char*)Y->data, Y->dtype,
N, K, acols, brs, yrs, ycs, mm, e1, e2);
N, K, acols, brs, yrs, ycs, mm, e1, e2, tl);
};
/* Columns a sub-group: four at decode (the activation read once for four weight rows), fewer
* as the rows' accumulators grow. */
@@ -585,11 +600,23 @@ int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream st) {
* 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;
const bool tl = m2_tiled(N, K);
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;
/* tiled codes: consecutive work-items the 16 columns of a (tile, group), so a word's loads
* are one 64-byte line */
const int64_t ii = (int64_t)i[0];
const int64_t n = tl ? (ii / (16 * kb)) * 16 + (ii & 15) : ii / kb;
const int64_t g = tl ? (ii >> 4) % kb : ii % 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 sycl::vec<uint8_t, 16> cb = *(const sycl::vec<uint8_t, 16>*)(codes0 + n * (K / 2) + g * 16);
sycl::vec<uint8_t, 16> cb;
if (tl)
for (int j = 0; j < 4; ++j) {
const sycl::vec<uint8_t, 4> w4 = *(const sycl::vec<uint8_t, 4>*)(codes0 + m2t_off(n, g * 16 + j * 4, K));
for (int u = 0; u < 4; ++u) cb[j * 4 + u] = w4[u];
}
else
cb = *(const sycl::vec<uint8_t, 16>*)(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 */