From 5720a958e45a2a6744966570c8b854f0eeead525 Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 05:55:19 +0200 Subject: [PATCH] libxe2: givens_dense_quant on XMX for prefill rows -- 2.07 -> 0.70 s over a 1792-token prefill For 17 or more rows the rotation is a batch of GEMMs, one a (part, 128-column group): the input fp16(x / D) per part, the f16 rotation as B, a batched form of the tiled fp16 XMX kernel (xmx_f16_batched) into f32, then libref's quantiser a (row, part, group). The input rounded once to fp16 where libref sums in double: a fast kernel. KL against the exact reference: mean 0.0037, top-1 97.2% (0.0035 / 95.6% before: within the noise of 250 positions). Up to 16 rows the float-pair kernel stays. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_common.h | 5 +++ libxe2/xe2_gemm_xmx.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ libxe2/xe2_givens.cpp | 44 +++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/libxe2/xe2_common.h b/libxe2/xe2_common.h index ac1052f8..570270e3 100644 --- a/libxe2/xe2_common.h +++ b/libxe2/xe2_common.h @@ -54,6 +54,11 @@ sycl::queue* queue_of(RadStream s); * allocation fails. */ float* workspace(sycl::queue& q, size_t floats, int slot = 0); +/* Batched fp16 XMX GEMM into f32 (xe2_gemm_xmx.cpp): C rows padded up to a multiple of 128 + * are written, so C + b * c_bs must hold ceil(M / 128) * 128 rows of N. */ +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); /* fp16 XMX GEMM with a per-row and per-column output factor (xe2_gemm_xmx.cpp). */ 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, diff --git a/libxe2/xe2_gemm_xmx.cpp b/libxe2/xe2_gemm_xmx.cpp index 4c8d950b..bc717c93 100644 --- a/libxe2/xe2_gemm_xmx.cpp +++ b/libxe2/xe2_gemm_xmx.cpp @@ -251,6 +251,84 @@ int xmx_f16_tiled(sycl::queue& q, const sycl::half* ab, int64_t ars, const sycl: }); } +/* The same tiling, batched and unscaled: batch b multiplies A + b * a_bs by B + b * b_bs into the + * f32 C + b * c_bs, [M, N] row-major (ldc = N). N a multiple of 128, K of 32. */ +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) { + 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>}; + return submit([&] { + q.submit([&](sycl::handler& h) { + sycl::local_accessor As(sycl::range<1>(2 * BM * BK), h); + sycl::local_accessor Bs(sycl::range<1>(2 * (BK / 2) * BN), h); + h.parallel_for(sycl::nd_range<3>{{(size_t)batch, gm, gn * 128}, {1, 1, 128}}, props, + [=](sycl::nd_item<3> 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(2); + const int sm = s / 2, sn = s % 2; + const int64_t bt = (int64_t)it.get_group(0); + const hf* a0 = ab + bt * a_bs; + const hf* b0 = bb + bt * b_bs; + float* c0 = cb + bt * c_bs; + const int64_t mb0 = (int64_t)it.get_group(1) * BM, nb0 = (int64_t)it.get_group(2) * BN; + jm::joint_matrix ma[4]; + jm::joint_matrix mb; + jm::joint_matrix 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 v(0u, 0u, 0u, 0u); + if (gm_ < M) v = *(const sycl::vec*)(a0 + gm_ * ars + k0 + kc); + *(sycl::vec*)&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 v = *(const sycl::vec*)(b0 + (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(&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( + (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(c0 + m0 * N + n0); + jm::joint_matrix_store(sg, mc[i][j], gc, (size_t)N, jm::layout::row_major); + } + }); + }); + }); +} + 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) { diff --git a/libxe2/xe2_givens.cpp b/libxe2/xe2_givens.cpp index 8b8d8466..8376a788 100644 --- a/libxe2/xe2_givens.cpp +++ b/libxe2/xe2_givens.cpp @@ -60,6 +60,50 @@ int givens_dense_quant(const RadArgs* a, RadStream st) { const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype; sycl::queue* q = queue_of(st); if (!q) return RAD_E_DEVICE; + if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows >= 17 && !exact_only() && + xcs == 1 && rad_tensor_is_contiguous(T)) { + /* Prefill: the rotation as a batch of GEMMs on XMX, one a (part, group): + * y[r][j] = sum_i T[p][gi][j][i] * fp16(x[r][gi*128 + i] / D[p][gi*128 + i]). The + * activation rounded once to fp16 (2^-11) where libref sums in double: a fast kernel, held + * to the tolerance and the KL bound. Then libref's quantiser a (row, part, group). */ + const int64_t mp = (rows + 127) / 128 * 128; + const int64_t in_e = P * mp * n; /* fp16 inputs [P][mp][n] */ + const int64_t inbytes = (in_e * 2 + 255) / 256 * 256; + float* ws = workspace(*q, (size_t)(inbytes / 4 + P * ngrp * mp * kG + 64), 7); + if (!ws) return RAD_E_DEVICE; + sycl::half* in16 = (sycl::half*)ws; + float* yv = (float*)((char*)ws + inbytes); /* [P * ngrp][mp][128] */ + int rc = submit([&] { + q->parallel_for(sycl::range<1>((size_t)in_e), [=](sycl::id<1> id) { + const int64_t c = (int64_t)id[0] % n, r = ((int64_t)id[0] / n) % mp, p = (int64_t)id[0] / (n * mp); + in16[id[0]] = r < rows ? (sycl::half)(rad_load_f32(xb, xdt, r * xrs + c) / rad_load_f32(db, ddt, p * n + c)) + : (sycl::half)0.0f; + }); + }); + if (rc < 0) return rc; + /* batch b = p * ngrp + gi: A = in16[p] + gi * 128 (row stride n), B = T[p][gi] [128][128] */ + for (int64_t p = 0; p < P && rc >= 0; ++p) + rc = xmx_f16_batched(*q, in16 + p * mp * n, n, kG, (const sycl::half*)tb + p * ngrp * kG * kG, kG, + kG * kG, yv + p * ngrp * mp * kG, mp * kG, rows, kG, kG, ngrp); + if (rc < 0) return rc; + return submit([&] { + q->parallel_for(sycl::nd_range<1>{(size_t)(rows * P * ngrp) * kG, kG}, [=](sycl::nd_item<1> it) { + const int64_t gid = (int64_t)it.get_group(0); + const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P); + const int j = (int)it.get_local_id(0); + const float y = yv[((p * ngrp + gi) * mp + r) * kG + j]; + const float amax = sycl::reduce_over_group(it.get_group(), sycl::fabs(y), sycl::maximum()); + const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f; + const float inv = 1.0f / scl; + float v = y * inv; + if (v > qmax) v = qmax; + if (v < -qmax) v = -qmax; + if (i8) v = sycl::rint(v); + rad_store_f32(qb, qdt, r * qrs + (p * n + gi * kG + j) * qcs, v); + if (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl); + }); + }); + } if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16)) { /* The fast form. A work-group a (part, group) and up to kRows rows; sub-group s takes the * outputs j = 16 s .. 16 s + 15, and for each its lanes read row j of T in one coalesced