This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gemm_w4_esimd.cpp
T
claudeandClaude Opus 5.5 84af4ef2f9 libxe2: w4a8i8 prefill GEMM's long-K form in row bands instead of A staged in SLM
down's shape (N <= 8192, K >= 16384) kept the SLM-staged form. It now takes the per-thread L1 form
too, with the work-groups in bands of 32 row groups: rows fastest made every 256-column slice read
all of A from memory (35 MiB at M = 2048, twenty slices), where a band's A stays in L2 across its
slices. The SLM form had no other user and is gone.

Probe w4p.cpp, down 5120 x 17408, int8 TOPS: M = 1024 61 -> 70, M = 2048 76 -> 79, M = 4096
80 -> 86; every output identical (M = 517 and 2048, all six shapes). rad-kbench gemm_nt_q
--all-rows 371/371. qwen3.8-27b-w4a16 (mtp4fc), fresh ~2.1k-token prompts: prefill ~1314 ->
~1345 tok/s; greedy 8/8 identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 11:47:13 +01:00

306 lines
17 KiB
C++

/* xe2_gemm_w4_esimd.cpp -- the 4-bit zero-point GEMMs in ESIMD, over the planes stored tiled
* (xe2_ops.h, w4_tiled; xe2_gemm_w4.cpp has the forms and their checks).
*
* w4a16_esimd, M <= 64 in blocks of 8 rows: a thread owns a 16-column tile and a split of K, walked a
* group (128 columns) at a time -- the group's codes are 1 KiB, four block loads. Each dword w of eight
* codes decodes into four VNNI pairs, ((w >> 4s) & 0x000F000F) | 0x64006400 being the pair (1024 +
* code) in fp16, and one 32-wide fp16 subtract of the column's 1024 + zero leaves code - zero, exact.
* Eight fp16 DPAS of RC rows of the activation each (RC = 1, 2, 4 or 8: the DPAS repeat count, so a
* decode row costs one row of the systolic array, not eight) sum the group in f32, which is scaled
* by the column's scale once and added to the split's total; a tile's splits are one work-group and
* meet in local memory (launch16). The activation comes from row loads for RC <= 4 and from 2D block
* loads at RC = 8, which return the DPAS A operand as it lies (probe w4d.cpp: M = 8 at gate_up's
* shape 466 -> 210 us; at M = 4 row loads were faster).
*
* w4a8i8_esimd, any M: int8 DPAS with the tiled codes as its u4 B operand, 8 codes a dword, against
* the prepass's activation (bytes of each 8 evens first, the codes' nibble order), read by each
* thread with 2D loads that hit L1 (launch_i8). A group is four K-32 steps of DPAS into int32 -- exact -- and then
* each output's term ((sum - zero * asum) * a_scale) * scale is added to its f32 total in group
* order: libref's result bit for bit. */
#include "xe2_common.h"
#include <sycl/ext/intel/esimd.hpp>
namespace xe2 {
namespace {
namespace esimd = sycl::ext::intel::esimd;
namespace xmx = sycl::ext::intel::esimd::xmx;
using half = sycl::half;
/* f32 to bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */
inline esimd::simd<uint16_t, 16> to_bf16(esimd::simd<float, 16> v) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint32_t, 16> u = v.template bit_cast_view<uint32_t>();
simd<uint32_t, 16> h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16;
h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u);
return simd<uint16_t, 16>(h);
}
/* A work-group is one tile's S splits of K: each thread sums its groups, the S partial sums meet in
* local memory and are added in split order, and the group's first threads store y. S is the caller's
* (w4_splits: about 8 groups a thread). Against the first form -- a thread a (tile, split) and a
* separate reduction launch over partials in global memory -- probe w4d.cpp at M = 4 (us):
* gate_up 190-208 -> 162, N 16384 125-147 -> 79, N 12288 108-117 -> 61, down 148 -> 83, N 5120
* K 6144 35 (+ the reduction) -> 32, N 1024 10 (+) -> 7.4: the weight's bytes at the card's ~560 GB/s. */
constexpr int kMaxSplits = 32;
/* bf16 codes to fp16, through f32 and rounded to nearest even: sycl::half(float), bit for bit */
template <int W>
inline esimd::simd<half, W> bf16_to_half(esimd::simd<uint16_t, W> v) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint32_t, W> u = simd<uint32_t, W>(v) << 16;
return simd<half, W>(u.template bit_cast_view<float>().read());
}
/* ABF: the activation is bf16, narrowed to fp16 at its load (the cast upstream dropped, fused). */
template <int RC, bool A2D, bool ABF>
int launch16(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) {
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 * RC * 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, RC * 16> tot = 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<half, RC * 128> Ar; /* A2D: [pair of steps][step][row][16]; else [row][128] */
if constexpr (A2D) {
#pragma unroll
for (int j2 = 0; j2 < 4; ++j2) {
if constexpr (ABF)
Ar.template select<RC * 32, 1>(j2 * RC * 32) = bf16_to_half<RC * 32>(
load_2d<uint16_t, 16, RC, 2, false>(ab, sw, sh, spt, (int)(g * 128 + j2 * 32), 0));
else
Ar.template select<RC * 32, 1>(j2 * RC * 32) =
load_2d<half, 16, RC, 2, false>(a, sw, sh, spt, (int)(g * 128 + j2 * 32), 0);
}
} else {
#pragma unroll
for (int m = 0; m < RC; ++m) {
if (m < mm) {
if constexpr (ABF)
Ar.template select<128, 1>(m * 128) = bf16_to_half<128>(block_load<uint16_t, 128>(ab + m * ars + g * 128));
else
Ar.template select<128, 1>(m * 128) = block_load<half, 128>(a + m * ars + g * 128);
}
else Ar.template select<128, 1>(m * 128) = 0;
}
}
simd<uint32_t, 16> zzd = (szw >> 16) | (szw & 0xFFFF0000u);
const simd<half, 32> zz = zzd.template bit_cast_view<half>();
simd<float, RC * 16> cg = 0.0f;
#pragma unroll
for (int j = 0; j < 8; ++j) {
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;
}
}
simd<half, RC * 16> A;
if constexpr (A2D) {
A = Ar.template select<RC * 16, 1>((j / 2) * RC * 32 + (j & 1) * RC * 16);
} else {
#pragma unroll
for (int m = 0; m < RC; ++m) A.template select<16, 1>(m * 16) = Ar.template select<16, 1>(m * 128 + 16 * j);
}
cg = xmx::dpas<8, RC, float>(cg, 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 m = 0; m < RC; ++m) tot.template select<16, 1>(m * 16) += cg.template select<16, 1>(m * 16) * sc;
}
slm_block_store<float, RC * 16>((uint32_t)(sp * RC * 16 * 4), tot);
barrier();
const int64_t n0 = tile * 16;
for (int r = sp; r < RC && 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 * RC * 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
* rows by 32 columns (two 8-row blocks by two tiles): 128 registers, so 8 threads an EU, and the
* group's epilogue on whole [8 rows][16] blocks against the factors broadcast once. Probe w4p.cpp
* at M = 2048, int8 TOPS, from the first form: gate_up 27 -> 115, inz 27 -> 96, down 24 -> 79,
* N = 5120 K = 6144 29 -> 108; every output identical to the first form's. */
template <int BAND>
int launch_i8(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad,
const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt,
int64_t yrs) {
constexpr int RB = 2, R = RB * 8; /* rows a work-group (32 rows on the 256-register file: 0.6x) */
constexpr int kWg = 8; /* threads a work-group, two tiles each */
const int64_t G = K / 128, RG = (M + R - 1) / R, CG = N / 32;
const int64_t cgw = (CG + kWg - 1) / kWg;
namespace intelex = sycl::ext::intel::experimental;
syclex::properties props{intelex::grf_size<128>};
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(RG * cgw * kWg), (size_t)kWg}, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
const int tl = (int)it.get_local_id(0);
const int64_t wg = (int64_t)it.get_group(0);
/* rows fastest: the work-groups over one 256-column slice of the weight run together and it
* stays in L2. With BAND, in bands of that many row groups, a band's A kept in L2 across its
* column slices: a long K over a narrow output (down's) re-read all of A from memory a slice */
int64_t rg = wg % RG, cw = wg / RG;
if constexpr (BAND > 0) {
const int64_t band = wg / (BAND * cgw), left = RG - band * BAND;
const int64_t rb = left < BAND ? left : (int64_t)BAND, inner = wg - band * BAND * cgw;
rg = band * BAND + inner % rb;
cw = inner / rb;
}
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);
simd<float, 128> acc[RB][2];
#pragma unroll
for (int i = 0; i < RB; ++i)
#pragma unroll
for (int j = 0; j < 2; ++j) acc[i][j] = 0.0f;
/* the loads' addresses in a 2D payload and pointers that only move: rebuilt a load at a
* time, the 64-bit address math was a third of the loop's instructions, and the loop is
* ALU-bound (stall samples: 72% active). Probe w4p.cpp at M = 2048, int8 TOPS: gate_up
* 91 -> 98, inz 74 -> 81, down 64 -> 76, out 86 -> 90. (One 16 x 16 2D load of a tile's
* group instead of four block loads: down 71, out 74.) */
namespace xesimd = sycl::ext::intel::experimental::esimd;
const uint32_t* cb0 = ct + tile0 * (K / 8) * 16;
const int kr = (int)(K / 8);
const uint32_t* szp = sz + tile0 * G * 16;
const int32_t* asp = asumt + r0;
const float* sap = sat + r0;
/* each thread reads its own A operands with 2D loads, which hit L1 (the work-group's
* threads read the same rows): no staging in SLM, no barrier a group */
xesimd::config_2d_mem_access<uint32_t, 8, 8, 1> ap8((const uint32_t*)ap, sw, sh, sw, 0, (int)r0);
for (int64_t g = 0; g < G; ++g) {
/* 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<int32_t, R> as = block_load<int32_t, R>(asp);
simd<float, R> sv = block_load<float, R>(sap);
asp += mpad;
sap += mpad;
simd<float, 16> sbj[2];
simd<int32_t, 128> c[RB][2];
#pragma unroll
for (int j = 0; j < 2; ++j) {
simd<uint32_t, 16> w = block_load<uint32_t, 16>(szp + j * G * 16);
simd<uint32_t, 16> lo = w & 0xFFFFu, hi = w >> 16;
simd<uint16_t, 16> s16 = lo, z16 = hi;
sbj[j] = simd<half, 16>(s16.template bit_cast_view<half>());
const simd<int16_t, 16> nz = 1024 - simd<int16_t, 16>(simd<half, 16>(z16.template bit_cast_view<half>()));
#pragma unroll
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];
}
szp += 16;
const uint32_t* cbp = cb0 + g * 256;
#pragma unroll
for (int ks = 0; ks < 4; ++ks) {
simd<uint32_t, 64> B[2];
#pragma unroll
for (int j = 0; j < 2; ++j) B[j] = block_load<uint32_t, 64>(cbp + j * kr * 16 + ks * 64);
#pragma unroll
for (int i = 0; i < RB; ++i) {
ap8.set_x((int)(g * 32 + ks * 8));
ap8.set_y((int)(r0 + i * 8));
const simd<uint32_t, 64> Ai = xesimd::lsc_load_2d<uint32_t, 8, 8, 1>(ap8);
#pragma unroll
for (int j = 0; j < 2; ++j)
c[i][j] = xmx::dpas<8, 8, int32_t, int32_t, uint32_t, uint32_t, xmx::dpas_argument_type::u4,
xmx::dpas_argument_type::s8>(c[i][j], B[j], Ai);
}
}
/* 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)
#pragma unroll
for (int i = 0; i < RB; ++i) {
simd<float, 128> cf = c[i][j];
#pragma unroll
for (int m = 0; m < 8; ++m) {
simd<float, 16> 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;
}
}
}
if (!live) return;
#pragma unroll
for (int i = 0; i < RB; ++i)
#pragma unroll
for (int m = 0; m < 8; ++m) {
const int64_t row = r0 + i * 8 + m;
if (row >= M) continue;
#pragma unroll
for (int j = 0; j < 2; ++j) {
const int64_t n0 = (tile0 + j) * 16;
const simd<float, 16> v = acc[i][j].template select<16, 1>(m * 16);
if (ydt == RAD_F32) block_store<float, 16>((float*)yb + row * yrs + n0, v);
else block_store<uint16_t, 16>((uint16_t*)yb + row * yrs + n0, to_bf16(v));
}
}
});
});
}
} // namespace
int w4a16_esimd(sycl::queue& q, int rc, const void* a, bool a_bf16, int64_t ars, int mm, const uint32_t* ct,
const uint32_t* sz, int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs) {
if (splits < 1 || splits > kMaxSplits || (K / 128) % splits) return RAD_E_UNSUPPORTED;
#define XE2_W4_RC(ABF) \
switch (rc) { \
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); \
}
if (a_bf16) XE2_W4_RC(true)
XE2_W4_RC(false)
#undef XE2_W4_RC
}
int w4a8i8_esimd(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad,
const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt,
int64_t yrs) {
/* A read per thread from L1 instead of staged in SLM a work-group, rows fastest; a long K over a
* narrow output (down's 5120 x 17408) in bands of 32 row groups. Probe w4p.cpp at M = 2048, int8
* TOPS, SLM -> now: gate_up 97 -> 115, inz 81 -> 96, qg 73 -> 80..98, out 90 -> 108, kv 72 -> 92,
* down 76 -> 79 (M = 1024: 61 -> 70, M = 4096: 80 -> 86); every output identical. */
if (N <= 8192 && K >= 16384) return launch_i8<32>(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs);
return launch_i8<0>(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs);
}
} // namespace xe2