Archived
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:
1 parent
5ed4ff847e
commit
6969fcf8cb
3 files changed
+73
-3
No files matched your search
@@ -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. */
|
||||
|
||||
+50
-1
@@ -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) */
|
||||
|
||||
+8
-2
@@ -565,8 +565,14 @@ int gated_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
auto v = [&](int64_t i) {
|
||||
const float gt = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + i);
|
||||
const float up = rad_load_f32(gu.base, gu.dtype, r * gu.pitch + n + i);
|
||||
const float sgm = sigmoidf(gt);
|
||||
return rad_bf16_to_f32(rad_f32_to_bf16((act == 0 ? gt * sgm : sgm) * up));
|
||||
/* a float exp, libref's double one only near a bf16 tie: the same bf16 (xe2_common.h) */
|
||||
const float sf = sigmoid_fexp(gt);
|
||||
float y = (act == 0 ? gt * sf : sf) * up;
|
||||
if (near_bf16_tie(y)) {
|
||||
const float sgm = sigmoidf(gt);
|
||||
y = (act == 0 ? gt * sgm : sgm) * up;
|
||||
}
|
||||
return rad_bf16_to_f32(rad_f32_to_bf16(y));
|
||||
};
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user