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