libxe2: libref's sigmoid and silu at a third of the cost, bit-identical -- gated norm 230 -> 37 ms, gated_quant 320 -> 254 ms at 2234 tokens

libref takes exp in double and rounds once; on the B65 that is ~3x the rest of an activation.
Rounded to bf16, two f32 values a few ulps apart give the same bf16 unless they straddle a
rounding tie, so the activation runs with a float exp and repeats libref's double chain only
where the f32 result lies within 16 ulps of a tie (near_bf16_tie, xe2_common.h). Probe silu.cpp:
0 of 21M bf16 outputs differ from libref's (the float chain alone: 85), at a third of the time.

- gated_quant_fp8: the gate's sigmoid this way.
- gdn_conv_prep: the convolution's silu this way.
- gdn_gated_rmsnorm: 128-channel rows with a bf16 output take a sub-group a row (8 rows a
  work-group): the squares added in index order by lane broadcasts (libref's chain), the gate as
  above. The 128-item work-group a row left 127 items waiting on one's ordered sum.

rad-kbench gdn + gated_quant: fixture 165/165, container 440/440, every libxe2 row of these ops
at rel_l2 0; op oracle on live operands (tolerance 1e-9): 60/60 agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-28 09:56:34 +01:00
1 parent 5ed4ff847e
commit 6969fcf8cb
3 files changed
+73 -3

No files matched your search

+50 -1
View File
@@ -136,6 +136,7 @@ size_t groups_for(int64_t n, size_t wg) { return (size_t)((n + (int64_t)wg - 1)
} // namespace
/* ------------------------------------------------------------------ gdn_conv_prep */
int gdn_conv_prep(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 16) return RAD_E_INVAL;
@@ -238,7 +239,10 @@ int gdn_conv_prep(const RadArgs* a, RadStream st) {
: (warm && has_cs && slot >= 0 ? rad_bf16_to_f32(sp[at3(cvs, slot, c, src + width - 1)]) : 0.0f);
acc += val * rad_bf16_to_f32(wp[j]);
}
((uint16_t*)out.p)[at3(ov, bos + t, oh, od)] = rad_f32_to_bf16(silu(acc));
/* silu with a float exp, libref's double one only near a bf16 tie (xe2_common.h) */
float s = acc * sigmoid_fexp(acc);
if (near_bf16_tie(s)) s = silu(acc);
((uint16_t*)out.p)[at3(ov, bos + t, oh, od)] = rad_f32_to_bf16(s);
return;
}
for (int64_t j = 0; j < width; ++j) {
@@ -1968,6 +1972,51 @@ int gdn_gated_rmsnorm(const RadArgs* a, RadStream st) {
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
constexpr size_t wg = 128;
if (ch == 128 && orr.dtype == RAD_BF16 && !std::getenv("XE2_GDN_GENERIC")) {
/* A sub-group a row of 128 (a (token, head) of the prefill: ~98k rows at a 2048-token chunk,
* where a 128-item work-group a row left 127 items waiting on one's ordered sum): a lane its
* 8 consecutive values, the squares added in index order by lane broadcasts (libref's chain),
* and the gate with a float exp, libref's double one only near a bf16 tie (xe2_common.h). */
const DT xd = dt_of(x);
const bool xv = xr.dtype == RAD_BF16 && xr.pitch % 8 == 0 && rad_ptr_aligned(xr.base, 16);
return submit([&] {
syclex::nd_launch(*qu, sycl::nd_range<1>{(size_t)((rows + 7) / 8) * 128, 128},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int64_t r = (int64_t)it.get_group(0) * 8 + (int64_t)sg.get_group_linear_id();
if (r >= rows) return; /* uniform over the sub-group */
const int lane = (int)sg.get_local_linear_id();
float u[8];
if (xv) {
const sycl::vec<uint16_t, 8> v8 = *(const sycl::vec<uint16_t, 8>*)((const uint16_t*)xr.base + r * xr.pitch + lane * 8);
for (int e = 0; e < 8; ++e) u[e] = rad_bf16_to_f32(v8[e]);
} else {
for (int e = 0; e < 8; ++e) u[e] = xd.ld(r * xr.pitch + lane * 8 + e);
}
float ss = 0.0f;
for (int l = 0; l < kSub; ++l)
for (int e = 0; e < 8; ++e) {
const float w = sycl::select_from_group(sg, u[e], l);
ss += w * w;
}
const float sc = 1.0f / sycl::sqrt(ss / (float)ch + eps);
const int64_t zb = zslice ? (r / zheads) * zs0 + (r % zheads) * ch * zs1 : r * zpitch;
sycl::vec<uint16_t, 8> o8;
for (int e = 0; e < 8; ++e) {
const int64_t i = lane * 8 + e;
const float zz = zd.ld(zb + i * zs);
const float base = u[e] * sc * wd.ld(i * ws);
const float sf = sigmoid_fexp(zz);
float y = base * (act == 1 ? sf : zz * sf);
if (near_bf16_tie(y)) y = base * (act == 1 ? sigm(zz) : silu(zz));
o8[e] = rad_f32_to_bf16(y);
}
uint16_t* op = (uint16_t*)orr.base + r * orr.pitch + lane * 8;
if (orr.pitch % 8 == 0 && rad_ptr_aligned(orr.base, 16)) *(sycl::vec<uint16_t, 8>*)op = o8;
else for (int e = 0; e < 8; ++e) op[e] = o8[e];
});
});
}
if (ch <= 1024) {
/* the row in local memory, read once by all work-items, then work-item 0's ordered sum
* from there (its serial walk over global memory was the kernel's time) */