libxe2: the int4 W4A16 release's GEMMs -- w4a16_asym on fp16 DPAS, w4a8i8_asym on int8 DPAS, quant_act_i8g

For qwen3.8-27b-w4a16-rtn (u4 codes, f16 scale and u4 zero per 128 of K): the engine casts a
linear's input to f16 for gemm_nt_q at w4a16_asym up to 64 rows a step, and quantises it with
quant_act_i8g for w4a8i8_asym above. None had a device kernel.

- The planes are stored tiled at load (xe2_ops.h, w4_tiled): a 16-column tile's codes side by
  side, the even codes of each dword in its low half so (w >> 4s) & 0x000F000F is a VNNI pair, and
  the scale with f16(1024 + zero) one dword a (column, group). unrelayout inverts both exactly.
- gemm_nt_q_w4: libref's gemm_zp loop per output, the exact form (XE2_EXACT=1, XE2_W4_EXACT=1).
- w4a16_asym, M <= 64: ESIMD, a thread a tile; (code | 0x6400) less f16(1024 + zero) is
  code - zero exactly; eight fp16 DPAS of RC = 1, 2, 4 or 8 rows a group, scaled in f32 a group.
  Narrow outputs split K. Probe w4d.cpp, incompressible codes: gate_up (N 34816, K 5120) 173 us at
  M = 1 (546 GB/s), 190 us at M = 4.
- w4a8i8_asym: int8 DPAS takes the tiled codes as its u4 B operand (nibble i meets A's byte i), so
  a prepass reorders the activation's bytes of each 8 evens first and forms each group's sum of
  codes; the group's integer sum is exact and its term ((sum - zero * asum) * a_scale) * scale is
  added in group order.
- quant_act_i8g: quant_act_fp8's form at libref's int8 rule (top 127).

rad-kbench and the op oracle cannot draw the zero-point grids, so XE2_W4_CHECK=n checks the first
n calls of each form against libref's loop on the host, on the engine's operands. On the 27B: the
exact form 0 of 81920 and 0 of 25600 outputs differ; w4a8i8 (M = 2048) 0 of 349568 and 0 of 327680;
w4a16 (M = 4) 0 of 65536 and 2 of 20480 (bf16 ties, rel_l2 5e-9). quant_act_i8g: rad-kbench
fixture 153/153 (quant_act*), the 27B 96/96, rel_l2 0. ctest -L sycl 3/3; the host tier's five
failures are the baseline's (FFmpeg, CPU budgets).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-28 00:06:22 +01:00
1 parent ab0175564b
commit efdfc4a570
7 files changed
+892 -48

No files matched your search

+20 -6
View File
@@ -179,8 +179,12 @@ int rmsnorm(const RadArgs* a, RadStream st) {
});
}
/* ------------------------------------------------------------------ quant_act_fp8 */
int quant_act_fp8(const RadArgs* a, RadStream st) {
/* ------------------------------------------------------------------ quant_act_fp8, quant_act_i8g */
namespace {
/* Codes and a scale amax / top per group: quant_act_fp8 (top 448, E4M3 codes) and quant_act_i8g
* (top 127, int8 codes rounded to nearest even and saturating) -- libref's q8_group either way. */
int quant_act_groups(const RadArgs* a, RadStream st, float top, const char* what) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const int64_t n = rad_args_geti_or(a, "n", 0);
if (n <= 0) return RAD_E_SHAPE;
@@ -188,11 +192,11 @@ int quant_act_fp8(const RadArgs* a, RadStream st) {
if (g <= 0 || g > n) g = n;
const int64_t ngrp = (n + g - 1) / g;
Rows x, qv, sv;
if (int rc = rows_view(&a->t[0], n, &x, "quant_act_fp8 x"); rc < 0) return rc;
if (int rc = rows_view(&a->t[0], n, &x, what); rc < 0) return rc;
const int64_t rows = x.rows;
if (rows <= 0 || rad_tensor_numel(&a->t[1]) < rows * n) return RAD_E_SHAPE;
if (int rc = rows_view(&a->t[1], n, &qv, "quant_act_fp8 q"); rc < 0) return rc;
if (int rc = scale_view(&a->t[2], rows, ngrp, &sv, "quant_act_fp8 scale"); rc < 0) return rc;
if (int rc = rows_view(&a->t[1], n, &qv, what); rc < 0) return rc;
if (int rc = scale_view(&a->t[2], rows, ngrp, &sv, what); rc < 0) return rc;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
constexpr int kSubs = (int)(kWg / kSub);
@@ -208,13 +212,23 @@ int quant_act_fp8(const RadArgs* a, RadStream st) {
if (gi >= ngrp) return; /* uniform over the sub-group */
auto v = [&](int64_t i) { return rad_load_f32(x.base, x.dtype, r * x.pitch + i); };
const int64_t k0 = gi * g, len = sycl::min(g, n - k0);
const float sc = quant_group(sg, v, k0, len, 448.0f, qv.base, qv.dtype, r * qv.pitch);
const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch);
if (sg.get_local_linear_id() == 0)
rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
});
});
}
} // namespace
int quant_act_fp8(const RadArgs* a, RadStream st) { return quant_act_groups(a, st, 448.0f, "quant_act_fp8"); }
int quant_act_i8g(const RadArgs* a, RadStream st) {
if (rad_args_have(a, 3) && a->t[1].dtype != RAD_I8)
return refuse("quant_act_i8g: codes are int8, not dtype %u", a->t[1].dtype);
return quant_act_groups(a, st, 127.0f, "quant_act_i8g");
}
/* ------------------------------------------------------------------ rmsnorm_quant_fp8 */
int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 5) return RAD_E_INVAL;