From 8bc536604528b5385267ae4ea19a715c771c24af Mon Sep 17 00:00:00 2001 From: claude Date: Mon, 2 Nov 2026 04:30:16 +0100 Subject: [PATCH] libxe2: w4a8i8 accumulators start at -zero * asum The DPAS now leaves sum - zero * asum directly (int16 operands, |zero * asum| <= 15*127*128), so the epilogue is a convert and the two scalings: no zero-init move, no f32 multiply and subtract an element. The same integer, so the same output (2234-token continuation and greedy 8/8 identical). Probe at M = 2048: gate_up 83 -> 91, inz 81 -> 88, qg 80 -> 87, out 80 -> 86 int8 TOPS. 2234-token prompt 1001 -> 1039 tok/s. (A blocked work-group order and an L1 prefetch of the codes were tried: neither helps; down at K = 17408 stays at ~66.) Co-Authored-By: Claude Opus 5.5 --- libxe2/xe2_gemm_w4_esimd.cpp | 47 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/libxe2/xe2_gemm_w4_esimd.cpp b/libxe2/xe2_gemm_w4_esimd.cpp index 524095e1..3d2af86d 100644 --- a/libxe2/xe2_gemm_w4_esimd.cpp +++ b/libxe2/xe2_gemm_w4_esimd.cpp @@ -169,7 +169,8 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo slm_init<2 * kBuf>(); const int tl = (int)it.get_local_id(0); const int64_t wg = (int64_t)it.get_group(0); - const int64_t rg = RF ? wg % RG : wg / cgw, cg = (RF ? wg / RG : wg % cgw) * kWg + tl; + const int64_t rg = RF ? wg % RG : wg / cgw, cw = RF ? wg / RG : wg % cgw; + const int64_t cg = cw * kWg + tl; const bool live = cg < CG; const int64_t r0 = rg * R, tile0 = (live ? cg : 0) * 2; const unsigned sw = (unsigned)(K - 1), sh = (unsigned)(M - 1); @@ -190,11 +191,26 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo for (int64_t g = 0; g < G; ++g) { const int buf = (int)(g & 1); if (g + 1 < G) stage(g + 1, buf ^ 1); + /* the accumulators start at -zero * asum (int16 operands, exact: |zero * asum| <= + * 15 * 127 * 128), so the DPAS leaves sum - zero * asum, the integer libref's chain + * forms first, and the epilogue only converts and scales */ + simd as = block_load(asumt + g * mpad + r0); + simd sv = block_load(sat + g * mpad + r0); + simd sbj[2]; simd c[RB][2]; #pragma unroll - for (int i = 0; i < RB; ++i) + for (int j = 0; j < 2; ++j) { + simd w = block_load(sz + ((tile0 + j) * G + g) * 16); + simd lo = w & 0xFFFFu, hi = w >> 16; + simd s16 = lo, z16 = hi; + sbj[j] = simd(s16.template bit_cast_view()); + const simd nz = 1024 - simd(simd(z16.template bit_cast_view())); #pragma unroll - for (int j = 0; j < 2; ++j) c[i][j] = 0; + for (int i = 0; i < RB; ++i) +#pragma unroll + for (int m = 0; m < 8; ++m) + c[i][j].template select<16, 1>(m * 16) = nz * (int16_t)as[i * 8 + m]; + } #pragma unroll for (int ks = 0; ks < 4; ++ks) { simd B[2]; @@ -209,35 +225,20 @@ int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const flo xmx::dpas_argument_type::s8>(c[i][j], B[j], Ai); } } - /* the group's terms ((sum - zero * asum) * a_scale) * scale, libref's chain. sum, - * zero * asum and their difference are integers under 2^24 (|a| <= 127, code and - * zero <= 15, 128 a group), so they are exact in f32 and the difference is formed - * there, a row at a time against scalar row factors: the same values as the int32 - * form, without 128-wide broadcast copies and int32 multiplies (the stall - * samples: ~75% of the kernel was this epilogue's ALU, not the DPAS). */ - simd as = block_load(asumt + g * mpad + r0); - simd sv = block_load(sat + g * mpad + r0); - const simd asf = as; + /* the group's terms ((sum - zero * asum) * a_scale) * scale, libref's chain: the + * integer is exact in f32 (under 2^24), scaled a row at a time */ #pragma unroll - for (int j = 0; j < 2; ++j) { - simd w = block_load(sz + ((tile0 + j) * G + g) * 16); - simd lo = w & 0xFFFFu, hi = w >> 16; - simd s16 = lo, z16 = hi; - const simd sb = simd(s16.template bit_cast_view()); - const simd zf = simd(simd(z16.template bit_cast_view())) - 1024.0f; + for (int j = 0; j < 2; ++j) #pragma unroll for (int i = 0; i < RB; ++i) { simd cf = c[i][j]; #pragma unroll for (int m = 0; m < 8; ++m) { - const float a_s = asf[i * 8 + m], a_c = sv[i * 8 + m]; - simd sub = cf.template select<16, 1>(m * 16) - zf * a_s; - simd tt = sub * a_c; - tt = tt * sb; + simd tt = cf.template select<16, 1>(m * 16) * sv[i * 8 + m]; + tt = tt * sbj[j]; acc[i][j].template select<16, 1>(m * 16) += tt; } } - } barrier(); } if (!live) return;