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
aneinanderandClaude Opus 5.5 ca793ea99a libxe2: gate_quant_fp8 (output gate, multiply and quantiser in one launch)
The gated attention block (qwen35 family, Qwen4exp, lfm2, krea2) takes
gate_quant_fp8 when a library declares it, otherwise sigmoid + mul (+
quant_act_fp8). One sub-group per (row, 128-column group), the sigmoid
rounded to bf16 before the multiply, xe2_sigmoid's float exp with the
double one near a bf16 tie (always double under XE2_EXACT): byte-identical
to libxe2's sigmoid -> mul -> quant_act_fp8, which the row declares as its
chain (.replaces) for rad-kbench's fusion oracle. out_bf16 may alias x.

xe2_sigmoid and xe2_mul get [M, n] shape hooks (libr4d's unary/binary),
which the oracle sizes the chain's temporaries from.
XE2_NO_FUSED_GATE=1 hides the row for an A/B.

kbench: fixture 2144/2144; fusion oracle 40/40 byte-identical on the 9B,
the 27B, and the 9B under XE2_EXACT. 27B int4, spec off: 646 -> 630
launches a decode step, same output hash, decode 30.6 tok/s either way
(the saving is ~16 dispatch gaps, ~0.3%), prefill 2.4k 1387 -> 1400 tok/s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 09:09:46 +01:00

211 lines
12 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"
/* spechub's operand shapes for the coupling ops (plugin/spechub/spechub.cpp, built into xe2_couple.cpp) */
int rad_mtp_couple_propose_shape(const RadParam*, int, int, RadOpdDesc*);
int rad_mtp_couple_verify_shape(const RadParam*, int, int, RadOpdDesc*);
namespace xe2 {
int add(const RadArgs* a, RadStream s);
int mul(const RadArgs* a, RadStream s);
int sigmoid(const RadArgs* a, RadStream s);
/* xe2_elem_act.cpp */
int gelu(const RadArgs* a, RadStream s);
int silu(const RadArgs* a, RadStream s);
int gelu_tanh(const RadArgs* a, RadStream s);
int scale(const RadArgs* a, RadStream s);
int mul_weight(const RadArgs* a, RadStream s);
int clamp(const RadArgs* a, RadStream s);
int fill(const RadArgs* a, RadStream s);
int iota(const RadArgs* a, RadStream s);
int act_mul(const RadArgs* a, RadStream s);
int gelu_mul(const RadArgs* a, RadStream s);
int rmsnorm_p(const RadArgs* a, RadStream s);
int relu(const RadArgs* a, RadStream s);
int logit_softcap(const RadArgs* a, RadStream s);
int rel_sinusoid(const RadArgs* a, RadStream s);
int pool_rows(const RadArgs* a, RadStream s);
int qk_norm_rope(const RadArgs* a, RadStream s);
int qkv_norm_rope(const RadArgs* a, RadStream s);
int qkv_norm_rope_kv_store(const RadArgs* a, RadStream s);
int qk_norm_rope_gate(const RadArgs* a, RadStream s);
int qk_norm_rope_gate_kv_store(const RadArgs* a, RadStream s);
int conv1d_dw(const RadArgs* a, RadStream s);
int conv2d(const RadArgs* a, RadStream s);
int attn_local_rel(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 add_rmsnorm(const RadArgs* a, RadStream s);
int mtp_couple_propose(const RadArgs* a, RadStream s);
int mtp_couple_verify(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 gate_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 gemm_nt_bias_fast(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