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>
This commit is contained in:
aneinanderandClaude Opus 5.5 committed 2026-11-18 09:09:46 +01:00
1 parent e1d612b29d
commit ca793ea99a
3 files changed
+180 -2

No files matched your search

+68
View File
@@ -808,4 +808,72 @@ int gated_quant_fp8(const RadArgs* a, RadStream st) {
});
}
/* ------------------------------------------------------------------ gate_quant_fp8 */
/* The gated attention's output gate in one launch: h = bf16(bf16(sigmoid(gate)) * x) (or
* bf16(gate * x) at act none), stored to out_bf16 when passed, then E4M3 (or int8) codes and an
* f32 scale per group over h. Byte-identical to libxe2's sigmoid, mul and quant_act_fp8 in turn:
* the sigmoid is rounded to bf16 before the multiply, as the buffer between the unfused pair is,
* with xe2_sigmoid's float exp and its double one near a bf16 tie (always double under
* XE2_EXACT). A row is a (token, head): `gate` a column slice of [q|gate] (pitch 2 * head_dim),
* `x` the attention output. out_bf16 may be `x` itself: a group is at most kCached * 16 values,
* so each lane reads its values into registers before it stores over them. */
int gate_quant_fp8(const RadArgs* a, RadStream st) {
if (!rad_args_have(a, 4)) return RAD_E_INVAL;
const RadTensor *G = &a->t[0], *X = &a->t[1], *Q = &a->t[2], *S = &a->t[3];
const RadTensor* O = rad_arg_in(a, 4);
const int64_t n = rad_args_geti_or(a, "n", 0);
if (n <= 0) return RAD_E_SHAPE;
const char* act_s = rad_args_gets_or(a, "act", "sigmoid");
const bool sig = !std::strcmp(act_s, "sigmoid");
if (!sig && std::strcmp(act_s, "none")) return refuse("gate_quant_fp8: act %s", act_s);
int64_t g = rad_args_geti_or(a, "group", 0);
if (g <= 0 || g > n) g = n;
if (g > (int64_t)kCached * kSub) return refuse("gate_quant_fp8: group %lld > %d", (long long)g, kCached * kSub);
const int64_t ngrp = (n + g - 1) / g;
Rows gv, xv, qv, sv, ob;
if (int rc = rows_view(G, n, &gv, "gate_quant_fp8 gate"); rc < 0) return rc;
const int64_t rows = gv.rows;
if (rows <= 0 || rad_tensor_numel(X) < rows * n || rad_tensor_numel(Q) < rows * n) return RAD_E_SHAPE;
if (int rc = rows_view(X, n, &xv, "gate_quant_fp8 x"); rc < 0) return rc;
if (int rc = rows_view(Q, n, &qv, "gate_quant_fp8 q"); rc < 0) return rc;
if (int rc = scale_view(S, rows, ngrp, &sv, "gate_quant_fp8 scale"); rc < 0) return rc;
const bool has_ob = O != nullptr;
if (has_ob) {
if (rad_tensor_numel(O) < rows * n) return RAD_E_SHAPE;
if (int rc = rows_view(O, n, &ob, "gate_quant_fp8 out_bf16"); rc < 0) return rc;
}
if (gv.dtype != RAD_BF16 || xv.dtype != RAD_BF16) return refuse("gate_quant_fp8: bf16 gate and x only");
const float top = Q->dtype == RAD_I8 ? 127.0f : 448.0f;
const bool fexp = !exact_only();
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
constexpr int kSubs = (int)(kWg / kSub);
const int64_t per_row = (ngrp + kSubs - 1) / kSubs;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * per_row) * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t r = (int64_t)it.get_group(0) / per_row;
const sycl::sub_group sg = it.get_sub_group();
const int64_t gi = ((int64_t)it.get_group(0) % per_row) * kSubs + (int64_t)sg.get_group_linear_id();
if (gi >= ngrp) return; /* uniform across the sub-group */
auto v = [&](int64_t i) {
const float gt = rad_bf16_to_f32(((const uint16_t*)gv.base)[r * gv.pitch + i]);
const float xx = rad_bf16_to_f32(((const uint16_t*)xv.base)[r * xv.pitch + i]);
float m = gt;
if (sig) {
float s = fexp ? sigmoid_fexp(gt) : 0.0f;
if (!fexp || near_bf16_tie(s)) s = sigmoidf(gt);
m = rad_bf16_to_f32(rad_f32_to_bf16(s));
}
return rad_bf16_to_f32(rad_f32_to_bf16(m * xx));
};
const int64_t k0 = gi * g, len = sycl::min(g, n - k0);
const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch,
has_ob ? ob.base : nullptr, ob.dtype, r * ob.pitch);
if (sg.get_local_linear_id() == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
});
});
}
} // namespace xe2
+1
View File
@@ -46,6 +46,7 @@ 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);
+111 -2
View File
@@ -176,6 +176,83 @@ int fuse_qk_norm_rope_gate_kv_store(const RadParam* p, int n_p, uint64_t present
}
}
/* Elementwise rows of [M, n] (libr4d's r4d_shape_unary / r4d_shape_binary): the form callers
* issue, and what the fusion oracle sizes a chain's temporaries from. */
int shape_unary(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = rad_param_getdim(p, n_p, "M", 0), n = rad_param_getdim(p, n_p, "n", 0);
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, n), opd(D, M, n) };
return pick(out, operand, l);
}
int shape_binary(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = rad_param_getdim(p, n_p, "M", 0), n = rad_param_getdim(p, n_p, "n", 0);
const uint32_t D = dt_act(p, n_p, "dtype");
const RadOpdDesc l[] = { opd(D, M, n), opd(D, M, n), opd(D, M, n) };
return pick(out, operand, l);
}
/* gate_quant_fp8: bf16 gate and x, whole 128-column scale groups (the out projection's grid). */
const RadConstraint cGateQuant[] = { RAD_CIN("dtype", "bf16"), RAD_CEQ("group", 128) };
/* gate, x, codes, the [M, n / group] scale plane and out_bf16, flat as libr4d draws them. */
int shape_gate_quant_fp8(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const int64_t M = rad_param_getdim(p, n_p, "M", 0), n = rad_param_getdim(p, n_p, "n", 0);
const int64_t g = rad_param_getdim(p, n_p, "group", 0);
const uint32_t D = dt_act(p, n_p, "dtype");
if (g <= 0) return RAD_E_UNSUPPORTED;
const RadOpdDesc l[] = {
opd(D, M, n),
opd(D, M, n),
opd((uint32_t)RAD_F8E4M3, M, n),
opd((uint32_t)RAD_F32, M, (n + g - 1) / g),
opd(D, M, n),
};
return pick(out, operand, l);
}
/* The chain xe2_gate_quant_fp8 replaces (libr4d's r4d_fuse_gate_quant_fp8): sigmoid into a bf16
* temporary, mul, quant_act_fp8 over the product; at act none, mul and quant_act_fp8. The product
* goes to out_bf16 when passed, else a second temporary.
*
* Operands: [0] gate [1] x [2] q [3] scale [4] out_bf16? */
int fuse_gate_quant_fp8(const RadParam* p, int n_p, uint64_t present, int step, RadFuseStep* out) {
if (!out) return RAD_E_INVAL;
*out = RadFuseStep{};
const RadParam* a = rad_param_find(p, n_p, "act");
const char* act = a && a->kind == RAD_P_STR && a->sval ? a->sval : "sigmoid";
const bool none = !std::strcmp(act, "none");
if (!none && std::strcmp(act, "sigmoid")) return RAD_E_UNSUPPORTED;
bool ok = true;
auto mirror = [&](const char* key) {
const RadParam* f = rad_param_find(p, n_p, key);
if (!f || out->n_p >= RAD_MAX_FUSE_PARAMS) { ok = false; return; }
out->p[out->n_p++] = *f;
};
auto wire = [&](std::initializer_list<int16_t> from) {
if (!ok) return (int)RAD_E_UNSUPPORTED;
for (int16_t v : from) out->from[out->n_from++] = v;
return (int)RAD_OK;
};
const int16_t mid = RAD_FUSE_HAS(present, 4) ? (int16_t)4 : (int16_t)RAD_FUSE_TMP(1);
const int s = none ? step + 1 : step;
switch (s) {
case 0:
out->op = "sigmoid";
for (const char* k : { "M", "n", "dtype" }) mirror(k);
return wire({ 0, RAD_FUSE_TMP(0) }); /* x, y */
case 1:
out->op = "mul";
for (const char* k : { "M", "n", "dtype" }) mirror(k);
return wire({ none ? (int16_t)0 : (int16_t)RAD_FUSE_TMP(0), 1, mid }); /* a, b, y */
case 2:
out->op = "quant_act_fp8";
for (const char* k : { "M", "n", "group", "dtype" }) mirror(k);
return wire({ mid, 2, 3 }); /* x, q, scale */
default:
return RAD_E_NOTFOUND;
}
}
/* 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[] = {
@@ -189,11 +266,11 @@ const RadKernelInfo kKernels[] = {
.computes = "elementwise product, b broadcast along rows when it is one row",
.shape = "any n and row count; rows may be padded, the last axis dense",
.dtypes = "any float dtype in and out, f32 arithmetic, rounded as libref",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::mul },
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::mul, .opd_shape = shape_binary },
{ .name = "xe2_sigmoid", .op = "sigmoid", .family = "elem",
.computes = "1 / (1 + exp(-x)), the exp in double rounded once as libref's",
.shape = "any element count; any strides", .dtypes = "any float dtype in and out",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sigmoid },
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::sigmoid, .opd_shape = shape_unary },
{ .name = "xe2_gelu", .op = "gelu", .family = "elem",
.computes = "0.5x(1 + erf(x / sqrt 2)), erf in double rounded once, as libref's",
.shape = "any element count; any strides", .dtypes = "any float dtype in and out",
@@ -347,6 +424,14 @@ const RadKernelInfo kKernels[] = {
.shape = "any n and group",
.dtypes = "float in, E4M3 codes, f32 scales",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_fp8 },
{ .name = "xe2_gate_quant_fp8", .op = "gate_quant_fp8", .family = "quant",
.computes = "the gated attention's output gate: bf16(sigmoid(gate)) * x (or gate * x at act none), "
"bf16 narrowing, E4M3 (or i8) codes and an f32 scale per group, one launch; "
"byte-identical to xe2_sigmoid, xe2_mul and xe2_quant_act_fp8",
.shape = "rows of n (a (token, head) pair), groups of 128; out_bf16 may be x",
.dtypes = "bf16 gate and x, E4M3 (or i8) codes, f32 scales, optional bf16 out",
.domain = RAD_DOMAIN_DEVICE, .constraints = cGateQuant, .n_constraints = 2,
.launch = xe2::gate_quant_fp8, .opd_shape = shape_gate_quant_fp8, .replaces = fuse_gate_quant_fp8 },
{ .name = "xe2_quant_act_i8g", .op = "quant_act_i8g", .family = "quant",
.computes = "row-major int8 codes and an f32 scale amax/127 per group, libref's q8_group",
.shape = "any n and group",
@@ -817,6 +902,22 @@ const RadOperandSpec oQkNormRopeGateKv[] = {
{ "kv_cache", RAD_OPD_INOUT, 0 },
};
/* gate_quant_fp8 (libr4d's pGateQuantFp8 / oGateQuantFp8): the gate in its own operand. */
const RadParamSpec pGateQuantFp8[] = {
{ "M", RAD_P_INT, RAD_REQUIRED },
{ "n", RAD_P_INT, RAD_REQUIRED },
{ "group", RAD_P_INT, RAD_REQUIRED },
{ "dtype", RAD_P_STR, RAD_REQUIRED },
{ "act", RAD_P_STR, RAD_OPTIONAL },
};
const RadOperandSpec oGateQuantFp8[] = {
{ "gate", RAD_OPD_IN, 0 },
{ "x", RAD_OPD_IN, 0 },
{ "q", RAD_OPD_OUT, 0 },
{ "scale", RAD_OPD_OUT, 0 },
{ "out_bf16", RAD_OPD_OUT, 1 },
};
const RadOpSchema kSchemas[] = {
{ "add_rmsnorm", pAddRmsNorm, (int)(sizeof pAddRmsNorm / sizeof pAddRmsNorm[0]), oAddRmsNorm,
(int)(sizeof oAddRmsNorm / sizeof oAddRmsNorm[0]),
@@ -844,6 +945,11 @@ const RadOpSchema kSchemas[] = {
"qk_norm_rope_gate followed by kv_store, in one launch: each k head's block also writes "
"its rotated k and its v into the paged cache at the token's slot. The `k` kv_store "
"reads is `k_out`, so the tail adds only `v`, `slot_mapping` and `kv_cache`." },
{ "gate_quant_fp8", pGateQuantFp8, (int)(sizeof pGateQuantFp8 / sizeof pGateQuantFp8[0]),
oGateQuantFp8, (int)(sizeof oGateQuantFp8 / sizeof oGateQuantFp8[0]),
"act(gate) * x rounded to bf16 (act sigmoid, its result rounded to bf16 first, or none), "
"then E4M3 codes and an f32 scale per (row, group) over the rounded product; out_bf16 "
"receives the product. A row is a (token, head) pair of a gated attention." },
};
const RadPluginInfo kInfo = {
@@ -856,6 +962,9 @@ const RadPluginInfo kInfo = {
* 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;
/* XE2_NO_FUSED_GATE=1 the same for gate_quant_fp8: the gated block runs sigmoid and mul. */
static const bool gate_off = std::getenv("XE2_NO_FUSED_GATE") != nullptr;
if (gate_off && !std::strcmp(op, "gate_quant_fp8")) return true;
return off && (!std::strcmp(op, "qkv_norm_rope") || !std::strcmp(op, "qkv_norm_rope_kv_store") ||
!std::strcmp(op, "qk_norm_rope_gate") || !std::strcmp(op, "qk_norm_rope_gate_kv_store"));
}