From 5d59701b82c1912aeebabf57333f514b5fb834cf Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 14:51:18 +0200 Subject: [PATCH] libxe2: m2xfp4 verify GEMM reads e_max itself and stores y when K is not split The register form spent ~60 us a call in launches around it: e_max over the weight's exponents, a finalize over the partials, and an activation prepass on 8 work-groups. A lane now reads its column's exponents for e_max, an unsplit launch stores y with its factors directly, and the prepass runs a work-group a (row, 2048 columns). N=34816 463 -> 400 us, N=5120 K=17408 249 -> 207, N=16384 247 -> 216, N=5120 K=25600 373 -> 303. Same arithmetic: 185/185 model cases, worst rel_l2 1.46e-3 as before. Tried and slower: the activation staged in local memory a split at a time (96 KiB a work-group starves occupancy: 1585 us) or a step at a time behind a barrier (506 us). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_gemv_m2.cpp | 57 +++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/libxe2/xe2_gemv_m2.cpp b/libxe2/xe2_gemv_m2.cpp index 593714b5..f6c413a6 100644 --- a/libxe2/xe2_gemv_m2.cpp +++ b/libxe2/xe2_gemv_m2.cpp @@ -222,9 +222,10 @@ int launch_dpas(sycl::queue& q, const half* a16, int64_t lda, const int32_t* ema * 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) { +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) { 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); @@ -246,9 +247,20 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const int32_t* emax, const u const half* abase = at + (part * K + lane) * mpad; int64_t nn[NT]; int emx[NT]; + const bool ev16 = (kg & 15) == 0 && (brs & 15) == 0 && ((uintptr_t)expe0 & 15) == 0; for (int t = 0; t < NT; ++t) { nn[t] = sycl::min(n0 + t * 16 + lane, N - 1); - emx[t] = emax[nn[t]]; + /* the column's largest exponent, read here rather than by a launch of its own */ + const uint8_t* er = expe0 + nn[t] * brs; + int32_t mx = 0; + int64_t g = 0; + if (ev16) + for (; g < kg; g += 16) { + const sycl::vec ev = *(const sycl::vec*)(er + g); + for (int u = 0; u < 16; ++u) mx = sycl::max(mx, (int32_t)ev[u]); + } + for (; g < kg; ++g) mx = sycl::max(mx, (int32_t)er[g]); + emx[t] = mx; } xe2_float8 c[NT][MT]; for (int t = 0; t < NT; ++t) @@ -290,8 +302,16 @@ int launch_dpas_raw(sycl::queue& q, const half* at, const int32_t* emax, const u } 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]; + for (int m = 0; m < 8; ++m) { + const int64_t r = (int64_t)i * 8 + m; + if (splits == 1) { /* the whole K here: y directly */ + if (r < mm && n0 + t * 16 + lane < N) + rad_store_f32(yb, ydt, r * yrs + (n0 + t * 16 + lane) * ycs, + c[t][i][m] * rr[r] * sycl::ldexp(1.0f, emx[t] - 127)); + } else { + pbuf[(sp * mpad + r) * ldp + n0 + t * 16 + lane] = c[t][i][m]; + } + } }); }); } @@ -318,23 +338,28 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 const uint8_t* ab = (const uint8_t*)A->data; const char* sab = (const char*)AS->data; const uint32_t sadt = AS->dtype; + /* the activation prepass: a work-group a (row, 2048 columns), each forming the row's factor */ + const int64_t chunks = raw ? (acols + 2047) / 2048 : 1; + const int64_t cspan = raw ? 2048 : acols; int rc = submit([&] { - q.parallel_for(sycl::nd_range<1>{(size_t)mpad * 256, 256}, [=](sycl::nd_item<1> it) { - const int64_t m = (int64_t)it.get_group(0), t = (int64_t)it.get_local_id(0); + q.parallel_for(sycl::nd_range<1>{(size_t)(mpad * chunks) * 256, 256}, [=](sycl::nd_item<1> it) { + const int64_t m = (int64_t)it.get_group(0) / chunks, ch = (int64_t)it.get_group(0) % chunks; + const int64_t t = (int64_t)it.get_local_id(0); float mx = 0.0f; if (m < M) for (int64_t c = t; c < scols; c += 256) mx = sycl::fmax(mx, rad_load_f32(sab, sadt, m * srs + c)); mx = sycl::reduce_over_group(it.get_group(), mx, sycl::maximum()); const float R = mx > 0.0f ? 224.0f * mx : 1.0f; - if (t == 0) rr[m] = R; + if (t == 0 && ch == 0) rr[m] = R; const float f = 1.0f / R; - for (int64_t c = t; c < acols; c += 256) { + for (int64_t c = ch * cspan + t; c < sycl::min(acols, (ch + 1) * cspan); c += 256) { const int64_t p = c & 7, src = (c & ~(int64_t)7) + (p >> 1) + 4 * (p & 1); 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; } }); + if (raw && splits == 1) return; /* the register form reads e_max itself */ q.parallel_for(sycl::range<1>((size_t)N), [=](sycl::id<1> i) { const uint8_t* er = expe0 + (int64_t)i[0] * brs; int32_t mx = 0; @@ -349,15 +374,17 @@ int m2_dpas(sycl::queue& q, const RadTensor* A, const RadTensor* AS, const uint8 }); }); if (rc < 0) return rc; + char* yb = (char*)Y->data; + const uint32_t ydt = Y->dtype; 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); + 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) + : 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); 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; + if (rc < 0 || (raw && splits == 1)) return rc; return submit([&] { q.parallel_for(sycl::range<1>((size_t)(M * N)), [=](sycl::id<1> i) { const int64_t m = (int64_t)i[0] / N, c = (int64_t)i[0] % N;