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_ops.h
T
claudeandClaude Opus 5.5 efdfc4a570 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>
2026-10-28 00:06:22 +01:00

178 lines
9.9 KiB
C++

/* xe2_ops.h -- every libxe2 launch, by op. The rows that select them are in xe2_rows.cpp. */
#pragma once
#include "rad_abi.h"
namespace xe2 {
int add(const RadArgs* a, RadStream s);
int mul(const RadArgs* a, RadStream s);
int sigmoid(const RadArgs* a, RadStream s);
int scatter_rows(const RadArgs* a, RadStream s);
int layernorm(const RadArgs* a, RadStream s);
int grid_embed(const RadArgs* a, RadStream s);
int rmsnorm(const RadArgs* a, RadStream s);
int rmsnorm_quant_fp8(const RadArgs* a, RadStream s);
int quant_act_fp8(const RadArgs* a, RadStream s);
int gated_quant_fp8(const RadArgs* a, RadStream s);
int rope(const RadArgs* a, RadStream s);
int rope_table(const RadArgs* a, RadStream s);
int embed_lookup(const RadArgs* a, RadStream s);
int gather_rows(const RadArgs* a, RadStream s);
int cast(const RadArgs* a, RadStream s);
int gemm_nt(const RadArgs* a, RadStream s);
int logits_gemm(const RadArgs* a, RadStream s);
int kv_store(const RadArgs* a, RadStream s);
int attn_paged(const RadArgs* a, RadStream s);
int sample_penalties(const RadArgs* a, RadStream s);
int sample_dry(const RadArgs* a, RadStream s);
int sample_ngram_ban(const RadArgs* a, RadStream s);
int sample_temp(const RadArgs* a, RadStream s);
int sample_mask(const RadArgs* a, RadStream s);
int sample_topk(const RadArgs* a, RadStream s);
int sample_topp(const RadArgs* a, RadStream s);
int sample_minp(const RadArgs* a, RadStream s);
int sample_typical(const RadArgs* a, RadStream s);
int sample_xtc(const RadArgs* a, RadStream s);
int sample_pick(const RadArgs* a, RadStream s);
int sample_argmax(const RadArgs* a, RadStream s);
int sample_logprobs(const RadArgs* a, RadStream s);
int gemm_nt_q(const RadArgs* a, RadStream s);
int gemm_nt_q_gated(const RadArgs* a, RadStream s);
int silu_mul(const RadArgs* a, RadStream s);
int quant_act_i8(const RadArgs* a, RadStream s);
int row_topk(const RadArgs* a, RadStream s);
int dflash_select(const RadArgs* a, RadStream s);
int gemm_nt_q_zp(const RadArgs* a, RadStream s);
/* Phase 4's fast kernels (xe2_gemv.cpp). XE2_EXACT=1 sends them to the correctness-first ones. */
bool exact_only();
int gemm_nt_gemv(const RadArgs* a, RadStream s);
int logits_gemm_gemv(const RadArgs* a, RadStream s);
int gemm_nt_xmx(const RadArgs* a, RadStream s);
int logits_gemm_xmx(const RadArgs* a, RadStream s);
int attn_paged_fast(const RadArgs* a, RadStream s);
int64_t attn_paged_scratch(const RadArgs* a);
int gemm_zp_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out);
int gemm_zp_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_zp_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 gemm_nt_q_m2(const RadArgs* a, RadStream s);
int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream s);
int gemm_nt_q_m2_gemv(const RadArgs* a, RadStream s);
int gemm_nt_q_m2_xmx(const RadArgs* a, RadStream s);
int givens_dense_quant(const RadArgs* a, RadStream s);
int gemm_nt_bias(const RadArgs* a, RadStream s);
int dflash_conv(const RadArgs* a, RadStream s);
int attn_dense(const RadArgs* a, RadStream s);
int logit_rerank(const RadArgs* a, RadStream s);
int logit_rerank_head(const RadArgs* a, RadStream s);
int logits_gemm_head(const RadArgs* a, RadStream s);
int logits_gemm_head_gemv(const RadArgs* a, RadStream s);
int head_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out);
int head_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 head_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 gdn_conv_prep(const RadArgs* a, RadStream s);
int gdn_conv_update(const RadArgs* a, RadStream s);
int gdn_kkt_solve(const RadArgs* a, RadStream s);
int gdn_chunk_scan(const RadArgs* a, RadStream s);
int gdn_recurrent_update(const RadArgs* a, RadStream s);
int gdn_gated_rmsnorm(const RadArgs* a, RadStream s);
int gdn_conv_recurrent_update(const RadArgs* a, RadStream s);
int gemm_m2_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out);
int gemm_m2_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_m2_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);
/* The m2xfp4 planes tiled (xe2_gemm_m2.cpp's layout hooks), where N % 16 == 0 and K % 512 == 0.
* Codes ("xe2.m2.tiled16"): a 16-column tile's 128-column step is 1 KiB, dword w (codes 8w .. 8w + 7)
* of its columns side by side -- the [word][lane] registers of the verify GEMM, one block load;
* m2t_off is byte b (codes 2b, 2b + 1) of column n's row. The meta bytes after the codes and the
* E8M0 exponents ("xe2.m2.e8m0.t16") in chunks of a tile's 16 groups, 256 bytes [dword][lane], a
* dword four consecutive groups of a column; m2e_off is group g's byte of column n. The exponent
* plane's stored form then holds each column's largest exponent, int32, at m2_emax_off. */
inline bool m2_tiled(int64_t N, int64_t K) { return N % 16 == 0 && K % 512 == 0; }
inline int64_t m2t_off(int64_t n, int64_t b, int64_t K) {
return ((((n >> 4) * (K / 128) + (b >> 6)) * 16 + ((b >> 2) & 15)) * 64) + (n & 15) * 4 + (b & 3);
}
inline int64_t m2e_off(int64_t n, int64_t g, int64_t kg) {
return ((((n >> 4) * (kg / 16) + (g >> 4)) * 4 + ((g & 15) >> 2)) * 16 + (n & 15)) * 4 + (g & 3);
}
inline int64_t m2_emax_off(int64_t N, int64_t kg) { return (N * kg + 63) / 64 * 64; }
/* The zero-point GEMM's u2 codes tiled (xe2_gemm_zp.cpp, tag "xe2.zp.u2.t16") where N % 16 == 0,
* K % 32 == 0 and the group a multiple of 32: a 16-column tile's 32-column step is 128 bytes,
* dword [r][lane] holding the column's codes for the activation's stored positions 16 r .. 16 r +
* 15, two bits each from the low end -- the u2 B operand of an int8 DPAS as it lies. The
* activation is quant_act_i8's step order (position p holds column p with bits 2 and 3 swapped),
* so dword r's code u is logical column 32 s + swap23(16 r + u). zpt_code reads logical column k. */
inline bool zp_tiled(int cbits, int64_t N, int64_t K, int64_t group) {
return cbits == 2 && N > 0 && N % 16 == 0 && K > 0 && K % 32 == 0 && group > 0 && group % 32 == 0 && K % group == 0;
}
inline int64_t zpt_dword(int64_t n, int64_t s, int r, int64_t K) {
return (((n >> 4) * (K / 32) + s) * 2 + r) * 16 + (n & 15);
}
inline uint32_t zpt_code(const uint32_t* t, int64_t n, int64_t k, int64_t K) {
const int x = (int)(k & 31);
const int p = (x & ~12) | ((x & 4) << 1) | ((x & 8) >> 1);
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