libxe2: fused attention prologue (qkv_norm_rope, qkv_norm_rope_kv_store)

The one-launch prologue that Gemma 3, Gemma 4, Hunyuan v3 and the DFlash2 drafter use where a
kernel library declares it (libr4d did; libxe2 did not, so those models ran the unfused five or six
launches a layer). docs/OPS.md requires it byte-identical to the unfused ops, so each step is
libxe2's own: xe2_rmsnorm's bf16 head-row norm (its partition and reduce_over_group; XE2_EXACT=1
libref's ascending sum, as xe2_rmsnorm then takes), the bf16 rounding the norm stores, xe2_rope's
arithmetic on its frequency table, xe2_kv_store's conversion. A sub-group a (token, head), the head
staged in local memory for the rotary pairs. The schemas are libr4d's.

XE2_NO_FUSED_PROLOGUE=1 leaves the two rows undeclared, so an architecture takes its unfused chain:
the A/B, and an escape hatch.

Gemma 4 E2B: greedy outputs identical with and without (4 requests), decode 58.0 -> 58.6 tok/s.
A random Gemma 3 (v_norm 0): identical, 1570 -> 1600 tok/s. Default fixture 2144/2144.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-08 20:29:25 +01:00
1 parent 25c8fbd136
commit 0c7606423b
3 files changed
+366 -5

No files matched your search

+116 -5
View File
@@ -6,6 +6,9 @@
* row here, and on the SYCL backend the engine stops at startup naming it (docs/SYCL_PORTING.md
* §4.4).
*/
#include <cstdlib>
#include <cstring>
#include <vector>
#include "rad_abi.h"
#include "rad_abi_layout.h"
#include "xe2_ops.h"
@@ -17,6 +20,8 @@ const RadConstraint cRopeModes[] = { RAD_CIN("mode", "neox gptj mrope imrope axi
const RadConstraint cFloatDtype[] = { RAD_CIN("dtype", "bf16 f16 fp16 f32 fp32") };
/* bf16 operands, any M (xe2_gemm_nt_bias picks its GEMM by M). */
const RadConstraint cBf16[] = { RAD_CIN("dtype", "bf16") };
/* the fused attention prologue: bf16 heads of 128, 256 or 512 */
const RadConstraint cQnr[] = { RAD_CIN("dtype", "bf16"), RAD_CDIV("head_dim", 128), RAD_CLE("head_dim", 512) };
/* The decode band of the skinny bf16 GEMV (xe2_gemv.cpp). */
const RadConstraint cGemvBf16[] = { RAD_CIN("dtype", "bf16"), RAD_CLE("M", 16) };
/* The prefill band of the bf16 XMX GEMM (xe2_gemm_xmx.cpp). */
@@ -124,6 +129,17 @@ const RadKernelInfo kKernels[] = {
.computes = "libref's per-head RMS norm (gain + wadd, or unit), rounded, then rotation: neox, gptj, blocks, gptj_blocks, imrope, axial; v_norm",
.shape = "head_dim, rotary_dim <= 512; a work-item a (row, head)", .dtypes = "any float qkv, gains",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::qk_norm_rope },
{ .name = "xe2_qkv_norm_rope", .op = "qkv_norm_rope", .family = "rope",
.computes = "rmsnorm_p (v, or v from the raw k), the q and k rmsnorms and the two NeoX rotaries in one "
"launch, byte-identical to libxe2's five (its bf16 head-row norm; XE2_EXACT=1 libref's order)",
.shape = "head_dim 128, 256 or 512; q, k, v rows 16-byte aligned; positions [M]",
.dtypes = "bf16 q, k, v; bf16 or f32 gains",
.domain = RAD_DOMAIN_DEVICE, .constraints = cQnr, .n_constraints = 3, .launch = xe2::qkv_norm_rope },
{ .name = "xe2_qkv_norm_rope_kv_store", .op = "qkv_norm_rope_kv_store", .family = "rope",
.computes = "xe2_qkv_norm_rope, then xe2_kv_store of the finished k and v heads from the same launch",
.shape = "as xe2_qkv_norm_rope; any paged cache xe2_kv_store takes",
.dtypes = "bf16 q, k, v; a bf16 or fp8 cache",
.domain = RAD_DOMAIN_DEVICE, .constraints = cQnr, .n_constraints = 3, .launch = xe2::qkv_norm_rope_kv_store },
{ .name = "xe2_conv1d_dw", .op = "conv1d_dw", .family = "conv",
.computes = "causal depthwise conv over time within cu segments, f32 in tap order, + bias, one rounding",
.shape = "rows of n, k taps", .dtypes = "any float dtype",
@@ -276,7 +292,7 @@ const RadKernelInfo kKernels[] = {
.computes = "split-KV paged attention with an online softmax: a work-group a (row, kv head, "
"key partition), its sub-groups the kv head's query heads, partitions merged from "
"scratch (not libref's order). XE2_EXACT=1 runs xe2_attn_paged_ref instead",
.shape = "head_dim a multiple of 16 up to 256 (else the exact kernel); any gqa, causal or "
.shape = "head_dim a multiple of 16 up to 512 (else the exact kernel); 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, .priority = 10, .launch = xe2::attn_paged_fast,
@@ -540,11 +556,81 @@ const RadParamSpec pAddRmsNorm[] = {
const RadOperandSpec oAddRmsNorm[] = {
{ "x", RAD_OPD_INOUT, 0 }, { "a", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 0 }, { "y", RAD_OPD_OUT, 0 },
};
/* libr4d's schemas for the fused attention prologue (r4d_rows.cpp), which libxe2 now also declares */
const RadParamSpec pQkvNormRope[] = {
{ "M", RAD_P_INT, 1 },
{ "head_dim", RAD_P_INT, 1 },
{ "n_head", RAD_P_INT, 1 },
{ "n_head_kv", RAD_P_INT, 1 },
{ "theta", RAD_P_F64, 1 },
{ "eps", RAD_P_F64, 1 },
{ "scale", RAD_P_F64, 0 },
{ "mode", RAD_P_STR, 0 },
{ "rotary_dim", RAD_P_INT, 0 },
{ "ladder", RAD_P_STR, 0 },
{ "wadd", RAD_P_F64, 0 },
{ "v_from_k", RAD_P_INT, 0 },
/* 0 leaves v unnormed (Gemma 3; a Qwen3-shaped layer); absent, 1 */
{ "v_norm", RAD_P_INT, 0 },
{ "dtype", RAD_P_STR, 1 },
};
const RadOperandSpec oQkvNormRope[] = {
{ "q", RAD_OPD_INOUT, 0 },
{ "k", RAD_OPD_INOUT, 0 },
{ "v", RAD_OPD_INOUT, 0 }, /* written from the raw k under v_from_k */
{ "positions", RAD_OPD_IN, 0 },
{ "q_w", RAD_OPD_WEIGHT, 0 },
{ "k_w", RAD_OPD_WEIGHT, 0 },
};
/* `qkv_norm_rope_kv_store` is qkv_norm_rope followed by kv_store in one launch: the prologue's
* operands, then kv_store's slot mapping and paged cache; every parameter is one of the two ops'.
* Byte-identical to the pair (docs/OPS.md). */
const RadParamSpec pQkvNormRopeKv[] = {
{ "M", RAD_P_INT, 1 },
{ "head_dim", RAD_P_INT, 1 },
{ "n_head", RAD_P_INT, 1 },
{ "n_head_kv", RAD_P_INT, 1 },
{ "theta", RAD_P_F64, 1 },
{ "eps", RAD_P_F64, 1 },
{ "scale", RAD_P_F64, 0 },
{ "mode", RAD_P_STR, 0 },
{ "rotary_dim", RAD_P_INT, 0 },
{ "ladder", RAD_P_STR, 0 },
{ "wadd", RAD_P_F64, 0 },
{ "v_from_k", RAD_P_INT, 0 },
{ "dtype", RAD_P_STR, 1 },
{ "v_norm", RAD_P_INT, 0 },
{ "block_size", RAD_P_INT, 1 },
{ "kv_dtype", RAD_P_STR, 1 },
{ "k_scale", RAD_P_F64, 0 },
{ "v_scale", RAD_P_F64, 0 },
};
const RadOperandSpec oQkvNormRopeKv[] = {
{ "q", RAD_OPD_INOUT, 0 },
{ "k", RAD_OPD_INOUT, 0 },
{ "v", RAD_OPD_INOUT, 0 },
{ "positions", RAD_OPD_IN, 0 },
{ "q_w", RAD_OPD_WEIGHT, 0 },
{ "k_w", RAD_OPD_WEIGHT, 0 },
{ "slot_mapping", RAD_OPD_IN, 0 },
{ "kv_cache", RAD_OPD_INOUT, 0 },
};
const RadOpSchema kSchemas[] = {
{ "add_rmsnorm", pAddRmsNorm, (int)(sizeof pAddRmsNorm / sizeof pAddRmsNorm[0]), oAddRmsNorm,
(int)(sizeof oAddRmsNorm / sizeof oAddRmsNorm[0]),
"x = x + a, then y = rmsnorm(x) * (w + wadd) * mult over the x written: a residual add and the "
"next norm in one pass, byte-identical to add then rmsnorm." },
{ "qkv_norm_rope", pQkvNormRope, (int)(sizeof pQkvNormRope / sizeof pQkvNormRope[0]), oQkvNormRope,
(int)(sizeof oQkvNormRope / sizeof oQkvNormRope[0]),
"per-head q and k RMS norms, the weightless v norm and the rotary of q and k over separate "
"q, k and v projections, in one dispatch; v from the raw k under v_from_k. n_head 0 "
"leaves q unread (the k and v heads alone)." },
{ "qkv_norm_rope_kv_store", pQkvNormRopeKv, (int)(sizeof pQkvNormRopeKv / sizeof pQkvNormRopeKv[0]),
oQkvNormRopeKv, (int)(sizeof oQkvNormRopeKv / sizeof oQkvNormRopeKv[0]),
"qkv_norm_rope, then kv_store of the finished k and v heads into the paged cache, in one "
"launch." },
};
const RadPluginInfo kInfo = {
@@ -553,6 +639,31 @@ const RadPluginInfo kInfo = {
"intel_gpu_bmg_g31",
};
/* XE2_NO_FUSED_PROLOGUE=1 leaves the fused attention prologue undeclared, so an architecture takes
* its unfused chain: the A/B for its byte-identity, and an escape hatch. */
bool hidden(const char* op) {
static const bool off = std::getenv("XE2_NO_FUSED_PROLOGUE") != nullptr;
return off && (!std::strcmp(op, "qkv_norm_rope") || !std::strcmp(op, "qkv_norm_rope_kv_store"));
}
const std::vector<const RadKernelInfo*>& kernels() {
static const std::vector<const RadKernelInfo*> v = [] {
std::vector<const RadKernelInfo*> r;
for (const RadKernelInfo& k : kKernels)
if (!hidden(k.op)) r.push_back(&k);
return r;
}();
return v;
}
const std::vector<const RadOpSchema*>& schemas() {
static const std::vector<const RadOpSchema*> v = [] {
std::vector<const RadOpSchema*> r;
for (const RadOpSchema& o : kSchemas)
if (!hidden(o.op)) r.push_back(&o);
return r;
}();
return v;
}
} // namespace
extern "C" {
@@ -563,16 +674,16 @@ __attribute__((visibility("default"))) uint32_t rad_plugin_abi_version(void) {
__attribute__((visibility("default"))) uint64_t rad_plugin_abi_layout(void) { return rad_abi_layout(); }
__attribute__((visibility("default"))) const RadPluginInfo* rad_plugin_info(void) { return &kInfo; }
__attribute__((visibility("default"))) int rad_kernel_schema_count(void) {
return (int)(sizeof kSchemas / sizeof kSchemas[0]);
return (int)schemas().size();
}
__attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int i) {
return i >= 0 && i < rad_kernel_schema_count() ? &kSchemas[i] : nullptr;
return i >= 0 && i < rad_kernel_schema_count() ? schemas()[(size_t)i] : nullptr;
}
__attribute__((visibility("default"))) int rad_kernel_count(void) {
return (int)(sizeof kKernels / sizeof kKernels[0]);
return (int)kernels().size();
}
__attribute__((visibility("default"))) const RadKernelInfo* rad_kernel_at(int i) {
return i >= 0 && i < rad_kernel_count() ? &kKernels[i] : nullptr;
return i >= 0 && i < rad_kernel_count() ? kernels()[(size_t)i] : nullptr;
}
} // extern "C"