libxe2: add_rmsnorm (schema and row); arch/common: the plain norm and the attention gate without unread work

- libxe2 serves `add_rmsnorm` (device-only; libxe2 now publishes its schema, libr4d's spelling):
  x += a then rmsnorm of the x written in one pass, at rmsnorm's own bf16 partition, so both
  outputs are byte-identical to the pair here. NormQuantFP8's plain form takes it where it
  resolves (one launch instead of `add` + `rmsnorm`, ~150 a round).
- AttnGatedFP8: under the plain norm an out projection that reads no codes skips the
  quantiser after the gate's sigmoid + mul.

Greedy output 8/8 identical to the previous commit; decode proxy 81.2 -> 82.5 tok/s; declared
ops 2046 -> 1902.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-11-02 03:17:05 +01:00
1 parent f26eec2633
commit 4511822138
5 files changed
+148 -4

No files matched your search

+11 -2
View File
@@ -465,11 +465,20 @@ inline int AttnGatedFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int
RAD_STR("dtype", g.dtype), RAD_STR("act", "sigmoid")),
RAD_NOWEIGHTS),
{w.qg, w.attn.x}, {w.attn.cq(), w.attn.cs(), w.attn.x});
/* Under Geom::plain_norm an out projection that reads no codes (a 4-bit one) takes the
* pair alone: the quantiser after it wrote codes nobody read. */
bool o_plain = false;
if (!op_gq && g.plain_norm) {
RAD_ARCH_TRY(o.probe(b, nm, g, nm.f("blk.%d.attn_output", l), { src.o }));
o_plain = o.codes_free && o.reads_x;
}
if (!op_gq && g.head_dim % RAD_FP8_BLOCK == 0) {
RAD_ARCH_TRY(gate_pair(decl_b(b, nm.f("blk.%d.attn_gate", l), g.act_dtype,
{g.max_tok, q_dim})));
RAD_ARCH_TRY(q_gate.declare(b, g, w.attn, q_dim));
if (!q_gate.op) return RAD_E_INVAL;
if (!o_plain) {
RAD_ARCH_TRY(q_gate.declare(b, g, w.attn, q_dim));
if (!q_gate.op) return RAD_E_INVAL;
}
}
}
+19
View File
@@ -389,6 +389,9 @@ struct NormQuantFP8 {
* without the codes. */
bool x_only = false;
rad_op op_add = 0;
/* x_only with a residual, where a library serves `add_rmsnorm` (byte-identical to the pair):
* the add and the norm as one launch, `op` unused. */
rad_op op_addn = 0;
Geom gm{};
/* The row width, for turning a row slice into element offsets. */
int64_t n = 0;
@@ -428,6 +431,17 @@ inline int NormQuantFP8::declare(RadBuilder* b, const Geom& g, rad_weight gain,
if (x_only) {
if (fuse_ar != kArNone || !src || !a.x || !w || n <= 0) return RAD_E_INVAL;
const int64_t rows = max_rows > 0 ? max_rows : g.max_tok;
if (res && rad_op_declared(b, "add_rmsnorm")) {
const rad_op h = RAD_OP(b, "add_rmsnorm",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n),
RAD_F64("eps", g.eps), RAD_F64("wadd", g.wadd),
RAD_STR("dtype", g.dtype)),
RAD_WEIGHTS(w));
if (h && rad_op_resolved(b, h)) {
op_addn = rw(b, h, { res, src }, { res, a.x });
if (op_addn) return RAD_OK;
}
}
if (res) {
op_add = rw(b, RAD_OP(b, "add",
RAD_PARAMS(RAD_RANGE("M", 1, rows), RAD_INT("n", n),
@@ -519,6 +533,11 @@ inline void NormQuantFP8::step_plain(RadCtx* c, int64_t T, int64_t r0, int64_t r
inline void NormQuantFP8::step(RadCtx* c, int64_t T, int64_t r0, int64_t rows) const {
if (rows <= 0) rows = T - r0;
if (x_only) {
if (op_addn) {
RAD_ISSUE_N(c, op_addn, rows, brow_slice(res, r0, rows, n), brow_slice(src, r0, rows, n),
RAD_W(w), brow_slice(a.x, r0, rows, n));
return;
}
if (op_add)
RAD_ISSUE_N(c, op_add, rows, brow_slice(src, r0, rows, n), brow_slice(res, r0, rows, n),
brow_slice(res, r0, rows, n));
+89
View File
@@ -403,6 +403,95 @@ int quant_act_i8g(const RadArgs* a, RadStream st) {
return quant_act_groups(a, st, 127.0f, "quant_act_i8g");
}
/* ------------------------------------------------------------------ add_rmsnorm */
/* x += a (bf16, rounded as `add` rounds it), then y = rmsnorm(x) over the x written, at rmsnorm's
* own partition (the bf16 rows above: a work-item eight elements, the same per-item order and the
* same group reduction), so both outputs are byte-identical to `add` then `rmsnorm` here. Other
* shapes or dtypes: the two kernels in order. */
int add_rmsnorm(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 4 || !a->t[0].data || !a->t[1].data || !a->t[3].data) return RAD_E_INVAL;
const RadTensor* w = rad_arg_in(a, 2);
const float wadd = (float)rad_args_getf_or(a, "wadd", 0.0);
if (!w && wadd != 0.0f) return RAD_E_INVAL;
const int64_t n = rad_args_geti_or(a, "n", w && w->rank ? w->shape[w->rank - 1] : 0);
const float eps = (float)rad_args_getf_or(a, "eps", 1e-6);
const float mult = (float)rad_args_getf_or(a, "mult", 1.0);
if (n <= 0) return RAD_E_SHAPE;
Rows x, ad, y;
Gain g;
if (int rc = rows_view(&a->t[0], n, &x, "add_rmsnorm x"); rc < 0) return rc;
if (int rc = rows_view(&a->t[1], n, &ad, "add_rmsnorm a"); rc < 0) return rc;
if (int rc = rows_view(&a->t[3], n, &y, "add_rmsnorm y"); rc < 0) return rc;
if (int rc = gain_view(w, n, &g, "add_rmsnorm w"); rc < 0) return rc;
const int64_t rows = x.rows;
if (rows <= 0 || y.rows < rows || ad.rows < rows) return RAD_E_SHAPE;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t nv = n / 8, wgv = (nv + kSub - 1) / kSub * kSub;
const bool fast = !exact_only() && x.dtype == RAD_BF16 && ad.dtype == RAD_BF16 && y.dtype == RAD_BF16 &&
n % 8 == 0 && n > 512 && wgv <= 1024 && x.pitch % 8 == 0 && ad.pitch % 8 == 0 &&
y.pitch % 8 == 0 && rad_ptr_aligned(x.base, 16) && rad_ptr_aligned(ad.base, 16) &&
rad_ptr_aligned(y.base, 16) &&
(!g.base || ((g.dtype == RAD_F32 || g.dtype == RAD_BF16) && g.stride == 1));
if (!fast) {
/* the pair: x += a elementwise, then rmsnorm's own launch over x */
int rc = submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)rows * kWg, kWg}, [=](sycl::nd_item<1> it) {
const int64_t r = (int64_t)it.get_group(0);
for (int64_t i = (int64_t)it.get_local_id(0); i < n; i += (int64_t)kWg) {
const float v = rad_load_f32(x.base, x.dtype, r * x.pitch + i) +
rad_load_f32(ad.base, ad.dtype, r * ad.pitch + i);
rad_store_f32(x.base, x.dtype, r * x.pitch + i, v);
}
});
});
if (rc != RAD_OK) return rc;
RadArgs b2 = *a;
const RadTensor tt[3] = { a->t[0], a->t[2], a->t[3] };
b2.t = tt;
b2.n_t = 3;
return rmsnorm(&b2, st);
}
using V8 = sycl::vec<uint16_t, 8>;
return submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)(rows * wgv), (size_t)wgv},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t r = (int64_t)it.get_group(0);
const int64_t t = (int64_t)it.get_local_id(0);
const bool live = t < nv;
float v[8];
float ss = 0.0f;
if (live) {
uint16_t* xr = (uint16_t*)x.base + r * x.pitch + t * 8;
const V8 u = *(const V8*)xr;
const V8 d = *(const V8*)((const uint16_t*)ad.base + r * ad.pitch + t * 8);
V8 o;
#pragma unroll
for (int j = 0; j < 8; ++j) {
const float sum = sycl::bit_cast<float>((uint32_t)u[j] << 16) + sycl::bit_cast<float>((uint32_t)d[j] << 16);
o[j] = rad_f32_to_bf16(sum);
v[j] = sycl::bit_cast<float>((uint32_t)o[j] << 16);
ss += v[j] * v[j];
}
*(V8*)xr = o;
}
ss = sycl::reduce_over_group(it.get_group(), ss, sycl::plus<float>());
if (!live) return;
const float s = 1.0f / sycl::sqrt(ss / (float)n + eps);
V8 o;
#pragma unroll
for (int j = 0; j < 8; ++j) {
const int64_t i = t * 8 + j;
const float gw = !g.base ? 1.0f
: (g.dtype == RAD_F32 ? ((const float*)g.base)[i]
: sycl::bit_cast<float>((uint32_t)((const uint16_t*)g.base)[i] << 16)) + wadd;
o[j] = rad_f32_to_bf16(v[j] * s * gw * mult);
}
*(V8*)((uint16_t*)y.base + r * y.pitch + t * 8) = o;
});
});
}
/* ------------------------------------------------------------------ rmsnorm_quant_fp8 */
int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 5) return RAD_E_INVAL;
+1
View File
@@ -12,6 +12,7 @@ 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 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);
+28 -2
View File
@@ -80,6 +80,12 @@ const RadKernelInfo kKernels[] = {
.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_add_rmsnorm", .op = "add_rmsnorm", .family = "norm",
.computes = "x += a (bf16, as add rounds it), then rmsnorm of the x written at rmsnorm's own "
"partition: byte-identical to add then rmsnorm here",
.shape = "any n; bf16 rows wider than 512, 8-aligned, in one pass, else the pair",
.dtypes = "float x, a, gain and y (bf16 in the recipes), f32 arithmetic",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::add_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",
@@ -426,6 +432,22 @@ const RadKernelInfo kKernels[] = {
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::gdn_conv_recurrent_update },
};
/* Schemas of the device-only ops libxe2 serves that libref does not declare (docs/OPS.md, libr4d's
* spellings field for field). */
const RadParamSpec pAddRmsNorm[] = {
{ "M", RAD_P_INT, 1, 0 }, { "n", RAD_P_INT, 1, 0 }, { "eps", RAD_P_F64, 1, 0 },
{ "dtype", RAD_P_STR, 1, 0 }, { "wadd", RAD_P_F64, 0, 0 }, { "mult", RAD_P_F64, 0, 0 },
};
const RadOperandSpec oAddRmsNorm[] = {
{ "x", RAD_OPD_INOUT, 0 }, { "a", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 0 }, { "y", RAD_OPD_OUT, 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." },
};
const RadPluginInfo kInfo = {
RAD_PLUGIN_KERNEL, "libxe2", "0.1",
"Intel Xe2 (Battlemage) kernels in SYCL, checked against libref",
@@ -441,8 +463,12 @@ __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 0; }
__attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int) { return nullptr; }
__attribute__((visibility("default"))) int rad_kernel_schema_count(void) {
return (int)(sizeof kSchemas / sizeof kSchemas[0]);
}
__attribute__((visibility("default"))) const RadOpSchema* rad_kernel_schema_at(int i) {
return i >= 0 && i < rad_kernel_schema_count() ? &kSchemas[i] : nullptr;
}
__attribute__((visibility("default"))) int rad_kernel_count(void) {
return (int)(sizeof kKernels / sizeof kKernels[0]);
}