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_rows.cpp
T
claudeandClaude Opus 5.5 a65ab54213 libxe2: Phase 4 step 1 -- a bandwidth-bound decode GEMV and an ordered sum from local memory
The profile of a MiniCPM5-2B decode step on the B65 (232.8 ms) was 79% gemm_nt, reading its
weights at 62 GB/s, and 13% the norms' serial sum of squares.

- xe2_gemm_nt_gemv / xe2_logits_gemm_gemv (priority 10, M <= 16, bf16): a sub-group an output
  column, 16-byte coalesced weight loads, a tree reduction. Not libref's order: rad-kbench passes
  them at the gemm tolerance (fixture 361/361, MiniCPM5 180/180, worst rel_l2 1.6e-4), and
  XE2_EXACT=1 sends every launch to the exact kernel for bisecting.
- The norms form their squares in parallel into local memory and add them in index order: still
  bit-identical to libref.

Decode 4.2 -> 35.5 tok/s. Against a libref-only host build, over 1914 positions of the repo's docs
with every position through the GEMV: KL mean 5.9e-4, p99 3.5e-3, top-1 98.5% -- libavx's own
distance from libref is 5.7e-4 / 3.0e-3 / 98.6%. The exact path scores KL 0, top-1 100%.

docs/SYCL_PROGRESS.md: the profile, the KL bound (D13), and the user's decision to use the bf16
container for Milestone B instead of the fp8 one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
2026-10-23 19:48:40 +02:00

239 lines
15 KiB
C++

/* xe2_rows.cpp -- libxe2's plugin exports: identity and the kernel rows the engine selects from.
*
* libxe2 is radiance's kernel library for Intel Xe2 GPUs (the Arc Pro B65, Battlemage G31) through
* SYCL. It defines no op schemas of its own: every op it serves is in the conventional vocabulary,
* whose schemas libref fixes (docs/OPS.md). All rows are device-domain; an op libxe2 lacks has no
* row here, and on the SYCL backend the engine stops at startup naming it (docs/SYCL_PORTING.md
* §4.4).
*/
#include "rad_abi.h"
#include "xe2_ops.h"
namespace {
const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj") };
/* The GEMMs here read float weights only; a quantised dtype key is gemm_nt_q's. */
const RadConstraint cFloatDtype[] = { RAD_CIN("dtype", "bf16 f16 fp16 f32 fp32") };
/* The decode band of the skinny bf16 GEMV (xe2_gemv.cpp). */
const RadConstraint cGemvBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CLE("M", 16) };
/* The block-scaled pairs xe2_gemm_q.cpp serves; the zero-point grids (w2a8, w4*_asym) and mxfp4 are
* other rows. */
const RadConstraint cFp8Dtype[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16") };
const RadConstraint cFp8Gated[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16"), RAD_CLE("N", 57344) };
/* The zero-point grids over quant_act_i8's activation (xe2_gemm_zp.cpp). */
const RadConstraint cZpI8Dtype[] = { RAD_CIN("dtype", "w2a8 w4a8_asym") };
/* Rows by field name (C++20 designated initialisers must follow declaration order, so the compiler
* checks it), unlike a positional table where a shifted field can still compile. */
const RadKernelInfo kKernels[] = {
{ .name = "xe2_add", .op = "add", .family = "elem",
.computes = "elementwise sum, b broadcast along rows when it is one row; optional div",
.shape = "any n and row count; rows may be padded, the last axis dense",
.dtypes = "any float dtype in and out (bf16, f16, f32, fp8), f32 arithmetic, rounded as libref",
.domain = RAD_DOMAIN_DEVICE, .priority = 0,
.launch = xe2::add },
{ .name = "xe2_rmsnorm", .op = "rmsnorm", .family = "norm",
.computes = "RMS norm, one work-group a row, f32 work-group reduction",
.shape = "any n; rows may be padded",
.dtypes = "float x, gain and y (bf16 in the recipes), f32 arithmetic",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::rmsnorm },
{ .name = "xe2_rmsnorm_quant_fp8", .op = "rmsnorm_quant_fp8", .family = "norm",
.computes = "residual add (optional), RMS norm, bf16 narrowing, E4M3 codes and an f32 scale "
"per group; int8 codes at amax/127 for an int8 q",
.shape = "any n and group; one sub-group a quantisation group",
.dtypes = "bf16 in, E4M3 (or i8) codes, f32 scales, optional bf16 out",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::rmsnorm_quant_fp8 },
{ .name = "xe2_quant_act_fp8", .op = "quant_act_fp8", .family = "quant",
.computes = "E4M3 codes and an f32 scale amax/448 per group",
.shape = "any n and group",
.dtypes = "float in, E4M3 codes, f32 scales",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_fp8 },
{ .name = "xe2_gated_quant_fp8", .op = "gated_quant_fp8", .family = "quant",
.computes = "silu(gate)*up or sigmoid(gate)*up over packed gate_up, bf16 narrowing, E4M3 "
"codes and an f32 scale per group",
.shape = "any n and group",
.dtypes = "bf16 gate_up, E4M3 (or i8) codes, f32 scales, optional bf16 out",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::gated_quant_fp8 },
{ .name = "xe2_rope", .op = "rope", .family = "rope",
.computes = "rotary embedding in place over q and k heads; frequency ladder (YaRN, llama3) "
"on the host as libref builds it, cos/sin in double",
.shape = "neox or gptj, one position component a token, rotary_dim <= 256",
.dtypes = "bf16 qkv (any float), integer or float positions",
.domain = RAD_DOMAIN_DEVICE, .constraints = cRopeModes, .n_constraints = 1,
.launch = xe2::rope },
{ .name = "xe2_embed_lookup", .op = "embed_lookup", .family = "vocab",
.computes = "embedding gather times mult; an id outside the table writes a zero row",
.shape = "any n_embd and n_vocab, vocab_offset",
.dtypes = "integer ids, float table and output",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::embed_lookup },
{ .name = "xe2_gather_rows", .op = "gather_rows", .family = "elem",
.computes = "row gather; a negative or out-of-range index writes a zero row",
.shape = "any n",
.dtypes = "integer indices, float rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::gather_rows },
{ .name = "xe2_cast", .op = "cast", .family = "elem",
.computes = "dtype conversion; integer to integer exact",
.shape = "any strides (libref's offlin order)",
.dtypes = "any float or integer pair",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::cast },
{ .name = "xe2_gemm_nt_gemv", .op = "gemm_nt", .family = "gemm",
.computes = "skinny Y = A B^T for decode: a sub-group an output column, 16-byte coalesced "
"weight loads, a tree reduction (not libref's order; checked at the gemm "
"tolerance). XE2_EXACT=1 runs xe2_gemm_nt_ref instead",
.shape = "M <= 16; bf16 rows 16-byte aligned, K a multiple of 8 (else the exact kernel)",
.dtypes = "bf16 A and W, bf16 or f32 Y, f32 accumulate",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cGemvBf16, .n_constraints = 2,
.launch = xe2::gemm_nt_gemv },
{ .name = "xe2_logits_gemm_gemv", .op = "logits_gemm", .family = "gemm",
.computes = "logits at decode, as xe2_gemm_nt_gemv", .shape = "M <= 16",
.dtypes = "bf16 x and head, f32 logits",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cGemvBf16, .n_constraints = 2,
.launch = xe2::logits_gemm_gemv },
{ .name = "xe2_gemm_nt_ref", .op = "gemm_nt", .family = "gemm",
.computes = "Y = A B^T, one work-item an output, f32 over ascending k (libref's order); the "
"correctness-first form, not the fast one",
.shape = "any M, N, K; strided A",
.dtypes = "float A and W (bf16 in the recipes), float Y",
.domain = RAD_DOMAIN_DEVICE, .constraints = cFloatDtype, .n_constraints = 1,
.launch = xe2::gemm_nt },
{ .name = "xe2_logits_gemm_ref", .op = "logits_gemm", .family = "gemm",
.computes = "logits = x lm_head^T, as xe2_gemm_nt_ref",
.shape = "any M, n_vocab, n_embd",
.dtypes = "float x and head, f32 logits",
.domain = RAD_DOMAIN_DEVICE, .constraints = cFloatDtype, .n_constraints = 1,
.launch = xe2::logits_gemm },
{ .name = "xe2_kv_store", .op = "kv_store", .family = "attn",
.computes = "this step's K and V into the paged cache at slot_mapping; fp8 stores k/k_scale",
.shape = "any head_dim, kv heads and block size; a negative or out-of-range slot is skipped",
.dtypes = "bf16 k and v, bf16 or E4M3 cache",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::kv_store },
{ .name = "xe2_attn_paged_ref", .op = "attn_paged", .family = "attn",
.computes = "paged attention, one work-group a (row, head), libref's order: serial dots, "
"keys summed in order; the correctness-first form",
.shape = "head_dim <= 512, any gqa, causal or not, sliding window, ragged batches",
.dtypes = "bf16 query, bf16 or E4M3 cache with per-(seq, head) descales, bf16 out",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::attn_paged },
{ .name = "xe2_sample_penalties", .op = "sample_penalties", .family = "sample",
.computes = "repetition, frequency and presence penalties over the row's history", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_penalties },
{ .name = "xe2_sample_dry", .op = "sample_dry", .family = "sample",
.computes = "DRY penalty over the row's history", .shape = "one work-item a row; quadratic in the window, as libref",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_dry },
{ .name = "xe2_sample_ngram_ban", .op = "sample_ngram_ban", .family = "sample",
.computes = "windowed no-repeat-ngram ban, optional whitelist", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_ngram_ban },
{ .name = "xe2_sample_temp", .op = "sample_temp", .family = "sample",
.computes = "logits divided by the row's temperature", .shape = "one work-item an element",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_temp },
{ .name = "xe2_sample_mask", .op = "sample_mask", .family = "sample",
.computes = "grammar bitmask: a cleared bit sets -inf", .shape = "one work-item an element",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_mask },
{ .name = "xe2_sample_topk", .op = "sample_topk", .family = "sample",
.computes = "the row's top_k candidates, descending, ties to the lower id", .shape = "one work-group a row, k reduction rounds",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_topk },
{ .name = "xe2_sample_topp", .op = "sample_topp", .family = "sample",
.computes = "top-p over the candidate set", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_topp },
{ .name = "xe2_sample_minp", .op = "sample_minp", .family = "sample",
.computes = "min-p over the candidate set", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_minp },
{ .name = "xe2_sample_typical", .op = "sample_typical", .family = "sample",
.computes = "locally typical sampling over the candidate set", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_typical },
{ .name = "xe2_sample_xtc", .op = "sample_xtc", .family = "sample",
.computes = "exclude top choices, seeded coin", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_xtc },
{ .name = "xe2_sample_pick", .op = "sample_pick", .family = "sample",
.computes = "inverse-CDF draw over the candidates, seeded per row", .shape = "one work-item a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_pick },
{ .name = "xe2_sample_argmax", .op = "sample_argmax", .family = "sample",
.computes = "greedy token, ties to the lower id", .shape = "one work-group a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_argmax },
{ .name = "xe2_sample_logprobs", .op = "sample_logprobs", .family = "sample",
.computes = "max, double normaliser and the top k (id, logit) pairs", .shape = "one work-group a row",
.dtypes = "f32 logits and candidates, RadSampleParams rows",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sample_logprobs },
{ .name = "xe2_gemm_nt_q_fp8_ref", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over E4M3 codes with group and block scales, libref's quant_dot per "
"output; the correctness-first form",
.shape = "any M, N, K; 128x128 block or per-row group scale grids",
.dtypes = "fp8a8, fp8a8f32, fp8a16: E4M3 weights, E4M3 or bf16 activations",
.domain = RAD_DOMAIN_DEVICE, .constraints = cFp8Dtype, .n_constraints = 1,
.launch = xe2::gemm_nt_q },
{ .name = "xe2_gemm_nt_q_gated_fp8_ref", .op = "gemm_nt_q_gated", .family = "gemm",
.computes = "gemm_nt_q over a gate_up plane with gated_quant_fp8 in the epilogue, one "
"work-group a row",
.shape = "N <= 57344 (n gated values in local memory)",
.dtypes = "fp8a8, fp8a8f32, fp8a16; E4M3 (or i8) codes and f32 scales out",
.domain = RAD_DOMAIN_DEVICE, .constraints = cFp8Gated, .n_constraints = 2,
.launch = xe2::gemm_nt_q_gated },
{ .name = "xe2_silu_mul", .op = "silu_mul", .family = "elem",
.computes = "silu(gate) * up over a packed gate_up", .shape = "any n",
.dtypes = "bf16 (any float)", .domain = RAD_DOMAIN_DEVICE, .launch = xe2::silu_mul },
{ .name = "xe2_quant_act_i8", .op = "quant_act_i8", .family = "quant",
.computes = "int8 codes with one scale a row, in the int8 WMMA step order; optional group sums",
.shape = "n a multiple of 16", .dtypes = "float in, i8 codes, f32 scale",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_i8 },
{ .name = "xe2_row_topk", .op = "row_topk", .family = "sample",
.computes = "exact per-row top-R, descending, ties to the lower column; one work-group a row",
.shape = "any N and R", .dtypes = "float x, integer ids and float values out",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::row_topk },
{ .name = "xe2_dflash_select", .op = "dflash_select", .family = "draft",
.computes = "the block drafter's greedy walk over its candidate lattice, one work-item a "
"sequence",
.shape = "any steps, top_k and rank", .dtypes = "integer candidates, float scores and factors",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::dflash_select },
{ .name = "xe2_gemm_nt_q_zp_ref", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over an unsigned zero-point grid against quant_act_i8's activation, "
"libref's gemm_zp per output; the correctness-first form",
.shape = "any M, N; K a multiple of 16 and of group",
.dtypes = "w2a8, w4a8_asym: u2/u4 codes, f16 scale and unsigned zero per (row, group), i8 "
"activation in step order with row scale and group sums",
.domain = RAD_DOMAIN_DEVICE, .constraints = cZpI8Dtype, .n_constraints = 1,
.launch = xe2::gemm_nt_q_zp, .layout = xe2::gemm_zp_layout,
.relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout },
};
const RadPluginInfo kInfo = {
RAD_PLUGIN_KERNEL, "libxe2", "0.1",
"Intel Xe2 (Battlemage) kernels in SYCL, checked against libref",
"intel_gpu_bmg_g31",
};
} // namespace
extern "C" {
__attribute__((visibility("default"))) uint32_t rad_plugin_abi_version(void) {
return RAD_ABI_VERSION;
}
__attribute__((visibility("default"))) const RadPluginInfo* rad_plugin_info(void) { return &kInfo; }
__attribute__((visibility("default"))) int rad_kernel_schema_count(void) { return 0; }
__attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int) { return nullptr; }
__attribute__((visibility("default"))) int rad_kernel_count(void) {
return (int)(sizeof kKernels / sizeof kKernels[0]);
}
__attribute__((visibility("default"))) const RadKernelInfo* rad_kernel_at(int i) {
return i >= 0 && i < rad_kernel_count() ? &kKernels[i] : nullptr;
}
} // extern "C"