From 2f1fdd4d116cdba1411b827f5c531b5d86218053 Mon Sep 17 00:00:00 2001 From: claude Date: Sun, 8 Nov 2026 01:38:14 +0100 Subject: [PATCH] libxe2: bf16 GEMV at 9-16 rows on DPAS, the weight read once At 9 <= M <= 16 the bf16 skinny GEMM (gemm_nt, logits_gemm) fell to the SIMT kernel's one column a sub-group, 6-8x the 8-row DPAS kernel's time on the same weight. The ESIMD kernel gains RC = 16: two 8-row DPAS on each transposed weight tile, rows past M clamped by the 2D load and not stored. MiniCPM5-2B bf16 on the Arc Pro B65 (rad-kbench --bench, cold weights, M = 16): gate_up 712 -> 104 us, down 465 -> 61, qkv 148 -> 29, o 145 -> 25. Twelve concurrent 256-token streams: 198 -> 654 tok/s aggregate. rad-kbench gemm_nt + logits_gemm: MiniCPM5 360/360, fixture 686/686. XE2_GEMV_SIMT=1 keeps the SIMT form. Co-Authored-By: Claude Opus 5.5 --- libxe2/xe2_gemv.cpp | 12 +++++++++++- libxe2/xe2_gemv_esimd.cpp | 31 +++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libxe2/xe2_gemv.cpp b/libxe2/xe2_gemv.cpp index 0c4ec10c..bad7c176 100644 --- a/libxe2/xe2_gemv.cpp +++ b/libxe2/xe2_gemv.cpp @@ -6,7 +6,8 @@ * against the same 8 columns of the activation, and keeps a partial sum a column; the sub-group then * reduces and lane 0 stores. Measured on the B65 at a 1.8k context: one column 9.1 ms a step of * gemm_nt, two 8.5 (~465 GB/s of the card's ~600), four 10.9 (fewer sub-groups, more registers). - * For 2 <= M <= 16, one column a sub-group with a partial sum a row. A weight byte is read once per step whatever M is, so the cost is the weight's + * For 2 <= M <= 16 the ESIMD kernel (xe2_gemv_esimd.cpp) where its 2D loads can read the operands, else + * one column a sub-group with a partial sum a row. A weight byte is read once per step whatever M is, so the cost is the weight's * bytes over the card's bandwidth. * * Not libref's order: each lane sums its own strided slice and the slices are reduced as a tree, so @@ -283,6 +284,15 @@ int gemv(const RadArgs* a, RadStream st, const char* nkey, const char* kkey, int } if (mm <= 8 && N % 16 == 0 && K % 16 == 0 && K <= 6144 && !simt) return launch_dpas8(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm); + /* 9 <= M <= 16 (9-16 sequences decoding, or a deep verify): the ESIMD kernel's two-band form, two + * 8-row DPAS on each weight tile, so the weight is still read once. The SIMT form below read it + * at 6-8x the 8-row cost (MiniCPM5-2B gate_up at M = 16: 712 -> 104 us). */ + if (mm > 8 && !simt && N % 16 == 0 && K % 16 == 0 && ycs == 1 && rad_ptr_aligned(ab, 64) && + rad_ptr_aligned(bb, 64) && rad_ptr_aligned(yb, 64) && (brs * 2) % 64 == 0 && (ars * 2) % 64 == 0 && + (yrs * (ydt == RAD_F32 ? 4 : 2)) % 64 == 0) { + const int S = gemv_esimd_splits(N, K); + if (S > 0) return gemv_esimd(*q, 16, ab, ars, mm, bb, brs, yb, ydt, yrs, N, K, S); + } return launch<1, kMaxM>(*q, ab, bb, yb, ydt, N, K, ars, brs, yrs, ycs, mm); } diff --git a/libxe2/xe2_gemv_esimd.cpp b/libxe2/xe2_gemv_esimd.cpp index 353c7e15..88151bb0 100644 --- a/libxe2/xe2_gemv_esimd.cpp +++ b/libxe2/xe2_gemv_esimd.cpp @@ -1,9 +1,9 @@ -/* xe2_gemv_esimd.cpp -- the skinny bf16 GEMM (gemm_nt and logits_gemm at M <= 8) in ESIMD. +/* xe2_gemv_esimd.cpp -- the skinny bf16 GEMM (gemm_nt and logits_gemm at M <= 16) in ESIMD. * * A thread owns a (16-column tile, K split): per 16 columns of K it takes W^T straight from the * weight rows by one transposed 2D load (16 rows by 8 dwords, which is the bf16 DPAS's VNNI B * operand as it arrives) and the activation rows by a 2D load (rows past M read as zeros), and runs - * one DPAS of RC = 1, 2, 4 or 8 rows. A work-group is one tile's S splits; their partial sums meet in + * one DPAS of RC = 1, 2, 4 or 8 rows (RC = 16: two 8-row DPAS on each weight tile). A work-group is one tile's S splits; their partial sums meet in * local memory and are added in split order, so the result is the same run to run. S is chosen for * ~8192 threads: narrow outputs need many splits to fill the card (probe bfg.cpp, M = 1 unless said): * @@ -26,9 +26,13 @@ namespace xmx = sycl::ext::intel::esimd::xmx; constexpr int kMaxS = 40; +/* RC rows a DPAS; RC = 16 is two 8-row DPAS on each weight tile, so 9-16 rows still read the weight + * once (the second band's rows past mm are clamped by the 2D load and never stored). */ template int launch(sycl::queue& q, const uint16_t* a, int64_t ars, int mm, const uint16_t* w, int64_t wrs, char* yb, uint32_t ydt, int64_t yrs, int64_t N, int64_t K, int S) { + constexpr int RD = RC > 8 ? 8 : RC; /* rows a DPAS */ + constexpr int NB = RC > 8 ? 2 : 1; /* DPAS bands */ const int64_t tiles = N / 16; const int64_t kper = K / S; return submit([&] { @@ -39,16 +43,22 @@ int launch(sycl::queue& q, const uint16_t* a, int64_t ars, int mm, const uint16_ const int64_t n0 = (int64_t)it.get_group(0) * 16, k0 = sp * kper; const unsigned wsw = (unsigned)(K * 2 - 1), wsh = (unsigned)(N - 1), wsp = (unsigned)(wrs * 2 - 1); const unsigned asw = (unsigned)(K * 2 - 1), ash = (unsigned)(mm - 1), asp = (unsigned)(ars * 2 - 1); - simd c = 0.0f; + simd c[NB]; +#pragma unroll + for (int b = 0; b < NB; ++b) c[b] = 0.0f; for (int64_t k = k0; k < k0 + kper; k += 16) { simd bt = load_2d((const uint32_t*)w, wsw, wsh, wsp, (int)(k / 2), (int)n0); - simd av; - if constexpr (RC == 1) av = block_load(a + k); - else av = load_2d(a, asw, ash, asp, (int)k, 0); - c = xmx::dpas<8, RC, float, float, uint32_t, uint16_t, xmx::dpas_argument_type::bf16, - xmx::dpas_argument_type::bf16>(c, bt, av); +#pragma unroll + for (int b = 0; b < NB; ++b) { + simd av; + if constexpr (RC == 1) av = block_load(a + k); + else av = load_2d(a, asw, ash, asp, (int)k, b * 8); + c[b] = xmx::dpas<8, RD, float, float, uint32_t, uint16_t, xmx::dpas_argument_type::bf16, + xmx::dpas_argument_type::bf16>(c[b], bt, av); + } } - slm_block_store((uint32_t)(sp * RC * 16 * 4), c); +#pragma unroll + for (int b = 0; b < NB; ++b) slm_block_store((uint32_t)((sp * RC + b * 8) * 16 * 4), c[b]); barrier(); for (int r = sp; r < RC && r < mm; r += S) { /* row r's 16 sums, the splits in order */ simd s = 0.0f; @@ -88,7 +98,8 @@ int gemv_esimd(sycl::queue& q, int rc, const uint16_t* a, int64_t ars, int mm, c case 1: return launch<1>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); case 2: return launch<2>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); case 4: return launch<4>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); - default: return launch<8>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); + case 8: return launch<8>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); + default: return launch<16>(q, a, ars, mm, w, wrs, yb, ydt, yrs, N, K, S); } }