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 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-08 01:53:59 +01:00
1 parent 2f1fdd4d11
commit 6abca01f17
2 files changed
+98 -4

No files matched your search

+8 -3
View File
@@ -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<int64_t>(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<int64_t>(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;
+90 -1
View File
@@ -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 <bool ABF>
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<kMaxSplits * R * 16 * 4>();
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<float, 128> tot[2];
tot[0] = 0.0f;
tot[1] = 0.0f;
for (int64_t g = g0; g < g1; ++g) {
simd<uint32_t, 256> cw;
#pragma unroll
for (int qd = 0; qd < 4; ++qd) cw.template select<64, 1>(qd * 64) = block_load<uint32_t, 64>(cb + g * 256 + qd * 64);
simd<uint32_t, 16> szw = block_load<uint32_t, 16>(sb + g * 16);
simd<uint32_t, 16> zzd = (szw >> 16) | (szw & 0xFFFF0000u);
const simd<half, 32> zz = zzd.template bit_cast_view<half>();
simd<float, 128> cg[2];
cg[0] = 0.0f;
cg[1] = 0.0f;
#pragma unroll
for (int j2 = 0; j2 < 4; ++j2) {
simd<half, 256> 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<uint16_t, 16, 8, 2, false>(ab, sw, sh, spt, (int)(g * 128 + j2 * 32), b * 8));
else
Ab[b] = load_2d<half, 16, 8, 2, false>(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<half, 256> B;
#pragma unroll
for (int t = 0; t < 2; ++t) {
const simd<uint32_t, 16> w = cw.template select<16, 1>((2 * j + t) * 16);
#pragma unroll
for (int s4 = 0; s4 < 4; ++s4) {
simd<uint32_t, 16> d = ((w >> (4 * s4)) & 0x000F000Fu) | 0x64006400u;
B.template select<32, 1>((4 * t + s4) * 32) = d.template bit_cast_view<half>() - zz;
}
}
#pragma unroll
for (int b = 0; b < 2; ++b) {
const simd<half, 128> A = Ab[b].template select<128, 1>(jj * 128);
cg[b] = xmx::dpas<8, 8, float>(cg[b], B, A);
}
}
}
simd<uint32_t, 16> lo = szw & 0xFFFFu;
simd<uint16_t, 16> s16 = lo;
const simd<float, 16> sc = simd<half, 16>(s16.template bit_cast_view<half>());
#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<float, 128>((uint32_t)(sp * R * 16 * 4), tot[0]);
slm_block_store<float, 128>((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<float, 16> v = 0.0f;
for (int u = 0; u < S; ++u) v += slm_block_load<float, 16>((uint32_t)((u * R * 16 + r * 16) * 4));
if (ydt == RAD_F32) block_store<float, 16>((float*)yb + r * yrs + n0, v);
else block_store<uint16_t, 16>((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<ABF>(q, a, ars, mm, ct, sz, N, K, splits, yb, ydt, yrs); \
}
if (a_bf16) XE2_W4_RC(true)
XE2_W4_RC(false)