From 6abca01f17ca4364b9fa7c4d85ce4ee1ff2dbf20 Mon Sep 17 00:00:00 2001 From: claude Date: Sun, 8 Nov 2026 01:53:59 +0100 Subject: [PATCH] libxe2: w4a16 decode GEMM in 16-row blocks, each group's codes decoded once gemm_nt_q at w4a16_asym (M <= 64) ran 8-row blocks, each reading and decoding the whole 4-bit weight: 16 rows paid twice, 64 rows eight times. launch16x2 takes two 8-row bands against each group's codes, loaded and decoded once, the activation loaded 32 columns at a time so both bands fit the default register file (no spill). The per-row arithmetic is launch16<8>'s. XE2_W4_RB8=1 keeps 8-row blocks. Qwen3.8-27B int4 (RedHatAI, as shipped), MTP depth 3, Arc Pro B65: 4 concurrent 256-token streams 97.6-99.2 -> 109.4-110.0 tok/s aggregate (+12%); one stream unchanged (43.1 -> 43.2). XE2_W4_CHECK=50 against libref's loop on the host: rel_l2 <= 6.6e-5 on every shape, tens of ties in ~10^5 outputs. Co-Authored-By: Claude Opus 5.5 --- libxe2/xe2_gemm_w4.cpp | 11 +++-- libxe2/xe2_gemm_w4_esimd.cpp | 91 +++++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/libxe2/xe2_gemm_w4.cpp b/libxe2/xe2_gemm_w4.cpp index 164f3b60..7fb73341 100644 --- a/libxe2/xe2_gemm_w4.cpp +++ b/libxe2/xe2_gemm_w4.cpp @@ -406,12 +406,17 @@ int gemm_nt_q_w4a16(const RadArgs* a, RadStream st) { const uint16_t* a16 = (const uint16_t*)c.ab; const int64_t ars = c.ars; const int splits = w4_splits(c.G); - for (int64_t m0 = 0; m0 < c.M; m0 += 8) { - const int mm = (int)std::min(8, c.M - m0); - const int rcount = mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8)); + /* blocks of 16 rows (two 8-row bands on each decoded group), a tail of 8 or fewer as before; + * XE2_W4_RB8=1 keeps 8-row blocks */ + static const bool rb8 = std::getenv("XE2_W4_RB8") != nullptr; + for (int64_t m0 = 0; m0 < c.M;) { + const int64_t left = c.M - m0; + const int mm = (int)std::min(rb8 || left <= 8 ? 8 : 16, left); + const int rcount = mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : (mm <= 8 ? 8 : 16))); const int rc = w4a16_esimd(*q, rcount, a16 + m0 * ars, a_bf16, ars, mm, (const uint32_t*)c.bb, (const uint32_t*)c.bsb, c.N, c.K, splits, c.yb + m0 * c.ys_r * (c.ydt == RAD_F32 ? 4 : 2), c.ydt, c.ys_r); if (rc != RAD_OK) return rc; + m0 += mm; } check_call(*q, c, "w4a16 fp16 DPAS", false, g_check_a16); return RAD_OK; diff --git a/libxe2/xe2_gemm_w4_esimd.cpp b/libxe2/xe2_gemm_w4_esimd.cpp index bac2ae33..b15688b7 100644 --- a/libxe2/xe2_gemm_w4_esimd.cpp +++ b/libxe2/xe2_gemm_w4_esimd.cpp @@ -144,6 +144,94 @@ int launch16(sycl::queue& q, const void* av, int64_t ars, int mm, const uint32_t }); } +/* launch16's form for 9-16 rows: two 8-row bands of the activation against each group's codes, loaded + * and decoded once (the 8-row form run twice read and decoded the weight twice). The activation is + * loaded 32 columns at a time inside the step loop rather than a group at a time, which keeps both + * bands' operands in the default register file. The same per-row arithmetic as launch16<8>: each + * band's DPAS chain over the group, the group sum scaled once, the splits added in order. */ +template +int launch16x2(sycl::queue& q, const void* av, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, int64_t N, + int64_t K, int S, char* yb, uint32_t ydt, int64_t yrs) { + constexpr int R = 16; + const int64_t ntile = N / 16, G = K / 128; + const int64_t per = G / S; + return submit([&] { + q.parallel_for(sycl::nd_range<1>{(size_t)(ntile * S), (size_t)S}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { + using namespace esimd; + slm_init(); + const int sp = (int)it.get_local_id(0); + const int64_t tile = (int64_t)it.get_group(0); + const int64_t g0 = sp * per, g1 = g0 + per; + const uint32_t* cb = ct + tile * (K / 8) * 16; + const uint32_t* sb = sz + tile * G * 16; + const unsigned sw = (unsigned)(K * 2 - 1), sh = (unsigned)(mm - 1), spt = (unsigned)(ars * 2 - 1); + const half* a = (const half*)av; + const uint16_t* ab = (const uint16_t*)av; + simd tot[2]; + tot[0] = 0.0f; + tot[1] = 0.0f; + for (int64_t g = g0; g < g1; ++g) { + simd cw; +#pragma unroll + for (int qd = 0; qd < 4; ++qd) cw.template select<64, 1>(qd * 64) = block_load(cb + g * 256 + qd * 64); + simd szw = block_load(sb + g * 16); + simd zzd = (szw >> 16) | (szw & 0xFFFF0000u); + const simd zz = zzd.template bit_cast_view(); + simd cg[2]; + cg[0] = 0.0f; + cg[1] = 0.0f; +#pragma unroll + for (int j2 = 0; j2 < 4; ++j2) { + simd Ab[2]; /* a band's 8 rows by 32 columns: [step][row][16] */ +#pragma unroll + for (int b = 0; b < 2; ++b) { + if constexpr (ABF) + Ab[b] = bf16_to_half<256>(load_2d(ab, sw, sh, spt, (int)(g * 128 + j2 * 32), b * 8)); + else + Ab[b] = load_2d(a, sw, sh, spt, (int)(g * 128 + j2 * 32), b * 8); + } +#pragma unroll + for (int jj = 0; jj < 2; ++jj) { + const int j = 2 * j2 + jj; + simd B; +#pragma unroll + for (int t = 0; t < 2; ++t) { + const simd w = cw.template select<16, 1>((2 * j + t) * 16); +#pragma unroll + for (int s4 = 0; s4 < 4; ++s4) { + simd d = ((w >> (4 * s4)) & 0x000F000Fu) | 0x64006400u; + B.template select<32, 1>((4 * t + s4) * 32) = d.template bit_cast_view() - zz; + } + } +#pragma unroll + for (int b = 0; b < 2; ++b) { + const simd A = Ab[b].template select<128, 1>(jj * 128); + cg[b] = xmx::dpas<8, 8, float>(cg[b], B, A); + } + } + } + simd lo = szw & 0xFFFFu; + simd s16 = lo; + const simd sc = simd(s16.template bit_cast_view()); +#pragma unroll + for (int b = 0; b < 2; ++b) +#pragma unroll + for (int m = 0; m < 8; ++m) tot[b].template select<16, 1>(m * 16) += cg[b].template select<16, 1>(m * 16) * sc; + } + slm_block_store((uint32_t)(sp * R * 16 * 4), tot[0]); + slm_block_store((uint32_t)((sp * R + 8) * 16 * 4), tot[1]); + barrier(); + const int64_t n0 = tile * 16; + for (int r = sp; r < R && r < mm; r += S) { /* row r: the splits in order */ + simd v = 0.0f; + for (int u = 0; u < S; ++u) v += slm_block_load((uint32_t)((u * R * 16 + r * 16) * 4)); + if (ydt == RAD_F32) block_store((float*)yb + r * yrs + n0, v); + else block_store((uint16_t*)yb + r * yrs + n0, to_bf16(v)); + } + }); + }); +} + /* The prefill GEMM. A work-group of 8 threads shares 16 rows, and each thread reads its A operands * ([8 rows][32 bytes] a K-32 step) with its own 2D loads, which hit L1. (They were staged in local * memory a work-group, double-buffered behind a barrier a group: 15-30% slower.) A thread owns 16 @@ -284,7 +372,8 @@ int w4a16_esimd(sycl::queue& q, int rc, const void* a, bool a_bf16, int64_t ars, case 1: return launch16<1, false, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ case 2: return launch16<2, false, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ case 4: return launch16<4, false, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ - default: return launch16<8, true, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ + case 8: return launch16<8, true, ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ + default: return launch16x2(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \ } if (a_bf16) XE2_W4_RC(true) XE2_W4_RC(false)