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

+15
View File
@@ -180,6 +180,21 @@ inline void st_index(uint32_t dt, void* p, int64_t i, int64_t v) {
}
}
/* libref takes exp in double and rounds it once (sigmoid, silu); on this card that is ~3x the cost of
* 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 a kernel may compute the value with a float exp (a few ulps
* from libref's chain) and run libref's chain again only where near_bf16_tie says the result lies
* within 16 ulps of a tie -- or is not finite. The bf16 is then libref's (probe silu.cpp: 0 of 21M
* silu outputs differ, at a third of the double form's time; the float chain alone, 85). */
inline bool near_bf16_tie(float s) {
const uint32_t u = sycl::bit_cast<uint32_t>(s);
const uint32_t lo = u & 0xFFFFu;
const uint32_t d = lo > 0x8000u ? lo - 0x8000u : 0x8000u - lo;
return d <= 16u || (u & 0x7F800000u) == 0x7F800000u;
}
/* 1 / (1 + e^-x) with a float exp: the fast half of a near_bf16_tie pair. */
inline float sigmoid_fexp(float x) { return 1.0f / (1.0f + sycl::exp(-x)); }
/* Rounds `v` through a store and load of `dt`, as libref's round_dt does. The float dtypes the
* kernels meet are converted directly: a round trip through a stack buffer can land in private
* (scratch) memory on the card, a global-memory round trip an element. */