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

+39
View File
@@ -135,4 +135,43 @@ inline uint32_t zpt_code(const uint32_t* t, int64_t n, int64_t k, int64_t K) {
return (t[zpt_dword(n, k >> 5, p >> 4, K)] >> (2 * (p & 15))) & 3u;
}
/* The 4-bit zero-point grids' planes tiled (xe2_gemm_w4.cpp; w4a16_asym, w4a8i8_asym) where N % 16
* == 0, K % 128 == 0 and the group is 128. Codes ("xe2.w4.t16"): dword ((n >> 4) * K/8 + d) * 16 +
* (n & 15) holds column n's codes 8d .. 8d + 7, the even ones in the low half and the odd ones in
* the high half, each in order: nibble s is code 8d + 2s and nibble s + 4 code 8d + 2s + 1 (s < 4).
* So (w >> 4s) & 0x000F000F is the fp16 DPAS's VNNI pair (2s, 2s + 1) once 0x6400 is or-ed into
* each half (1024 + code, exact), and a u4 B operand of the int8 DPAS -- which meets A's byte i with
* B's nibble i -- wants the activation's bytes of each 8 evens first. A tile's 128 columns of K (a
* group) are 1 KiB. Scale and zero ("xe2.w4.sz.t16"): dword ((n >> 4) * G + g) * 16 + (n & 15) is
* the f16 scale | f16(1024 + zero) << 16, a tile's group 64 bytes. */
inline bool w4_tiled(int64_t N, int64_t K, int64_t group) {
return N > 0 && N % 16 == 0 && K > 0 && K % 128 == 0 && group == 128;
}
inline int64_t w4t_dword(int64_t n, int64_t d, int64_t K) { return (((n >> 4) * (K / 8) + d) << 4) + (n & 15); }
inline uint32_t w4t_code(const uint32_t* t, int64_t n, int64_t k, int64_t K) {
const int u = (int)(k & 7);
return (t[w4t_dword(n, k >> 3, K)] >> ((u & 1) * 16 + 4 * (u >> 1))) & 15u;
}
inline int64_t w4s_dword(int64_t n, int64_t g, int64_t G) { return (((n >> 4) * G + g) << 4) + (n & 15); }
int gemm_nt_q_w4(const RadArgs* a, RadStream s);
int gemm_nt_q_w4a16(const RadArgs* a, RadStream s);
int gemm_nt_q_w4a8i8(const RadArgs* a, RadStream s);
int gemm_w4_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out);
int gemm_w4_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
const int* sel, const RadTensor* planes, int n, void* dst, int64_t dst_bytes);
int gemm_w4_unrelayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
const int* sel, const void* src, int64_t src_bytes, const RadTensor* planes,
int n);
int quant_act_i8g(const RadArgs* a, RadStream s);
/* The joined head xe2_head_esimd.cpp serves at M <= 8 (an int4 release's int8 head: i8 codes, one
* float scale per (row, 128 of K)); head_dpas leaves exactly these to it. Both read the planes on
* the card, so the test is shared rather than decided on the host. */
inline bool head_i8_fast(uint32_t cdt, int64_t crb, int64_t sbr, int64_t sbc, uint32_t sdt, int64_t K) {
return cdt == RAD_I8 && crb == K && K % 128 == 0 && sbr <= 1 && sbc == 128 &&
(sdt == RAD_BF16 || sdt == RAD_F16 || sdt == RAD_F32);
}
} // namespace xe2