libxe2: rmsnorm_quant_fp8 with 16-byte loads for bf16 rows -- 112 -> 44 us a call

Half the norm's time was its per-element loads of x, the residual and the gain. bf16 rows (gain
bf16 or f32) now load and store 8 values at a time and a 128-group's codes go out 8 bytes a lane;
the arithmetic and the ordered sum are the general form's. Bit-identical to libref (kbench rel_l2
0; 408/408 live oracle issues). 128 calls a speculative round: 17 -> 6.5 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-24 12:01:34 +02:00
1 parent 8abcee5d35
commit c943bc65e3
1 file changed
+105
+105
View File
@@ -248,6 +248,111 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
constexpr int kSubs = (int)(kWg / kSub);
auto a16 = [](const void* p) { return rad_ptr_aligned(p, 16); };
const bool vec = n <= kRowMax && n % g == 0 && g == 8 * kSub && x.dtype == RAD_BF16 && x.pitch % 8 == 0 &&
a16(x.base) && (gw.dtype == RAD_BF16 || gw.dtype == RAD_F32) && gw.stride == 1 && a16(gw.base) &&
(!has_res || (res.dtype == RAD_BF16 && res.pitch % 8 == 0 && a16(res.base))) &&
(!has_ob || (ob.dtype == RAD_BF16 && ob.pitch % 8 == 0 && a16(ob.base))) &&
Q->dtype == RAD_F8E4M3 && qv.pitch % 8 == 0 && rad_ptr_aligned(qv.base, 8);
if (vec) {
const char* gbase = gw.base;
const uint32_t gdt = gw.dtype;
/* The bf16 rows of the form below, 8 values a 16-byte load or store: x, the residual and
* the gain (bf16 or f32) in one read each, the sum and its squares formed as there; a group of 128 is
* a sub-group's, 8 contiguous values a lane, and its codes are stored 8 bytes a lane. The
* same arithmetic and order as the general form -- each value's, each group's amax and
* scale, the sum of squares -- without its per-element loads, which were half its time. */
return submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<float, 1> sv_(sycl::range<1>((size_t)n), h);
sycl::local_accessor<float, 1> sq(sycl::range<1>((size_t)n), h);
sycl::local_accessor<float, 1> wl(sycl::range<1>((size_t)n), h);
h.parallel_for(sycl::nd_range<1>{(size_t)rows * kWg, kWg},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
using V8 = sycl::vec<uint16_t, 8>;
const int64_t r = (int64_t)it.get_group(0);
const int t = (int)it.get_local_id(0);
auto grp = it.get_group();
constexpr int kPer = (int)(kRowMax / 8 / kWg);
const uint16_t* xr = (const uint16_t*)x.base + r * x.pitch;
uint16_t* rr_ = has_res ? (uint16_t*)res.base + r * res.pitch : nullptr;
const bool wf = gdt == RAD_F32;
V8 xv[kPer], rv[kPer];
sycl::vec<float, 8> wv[kPer];
#pragma unroll
for (int j = 0; j < kPer; ++j) {
const int64_t c = (t + (int64_t)j * kWg) * 8;
const int64_t cc = c < n ? c : 0;
xv[j] = *(const V8*)(xr + cc);
rv[j] = has_res ? *(const V8*)(rr_ + cc) : V8(0);
if (wf) {
const sycl::vec<float, 4> w0 = *(const sycl::vec<float, 4>*)((const float*)gbase + cc);
const sycl::vec<float, 4> w1 = *(const sycl::vec<float, 4>*)((const float*)gbase + cc + 4);
for (int u = 0; u < 4; ++u) { wv[j][u] = w0[u]; wv[j][u + 4] = w1[u]; }
} else {
const V8 w8 = *(const V8*)((const uint16_t*)gbase + cc);
for (int u = 0; u < 8; ++u) wv[j][u] = rad_bf16_to_f32(w8[u]);
}
}
#pragma unroll
for (int j = 0; j < kPer; ++j) {
const int64_t c = (t + (int64_t)j * kWg) * 8;
if (c >= n) continue;
V8 so;
for (int u = 0; u < 8; ++u) {
float v = rad_bf16_to_f32(xv[j][u]);
if (has_res) {
const float s0 = v + rad_bf16_to_f32(rv[j][u]);
so[u] = rad_f32_to_bf16(s0);
v = rad_bf16_to_f32(so[u]); /* what reading it back gives */
}
sv_[c + u] = v;
sq[c + u] = v * v;
wl[c + u] = wv[j][u] + wadd;
}
if (has_res) *(V8*)(rr_ + c) = so;
}
sycl::group_barrier(grp);
const sycl::sub_group sg = it.get_sub_group();
const int lane = (int)sg.get_local_linear_id();
float ss = 0.0f;
if (sg.get_group_linear_id() == 0) {
for (int64_t i = 0; i < n; i += kSub) {
const float v = sq[i + lane];
for (int j = 0; j < kSub; ++j) ss += sycl::select_from_group(sg, v, j);
}
}
ss = sycl::group_broadcast(grp, ss, 0);
const float rs = 1.0f / sycl::sqrt(ss / (float)n + eps);
const int sgi = (int)sg.get_group_linear_id();
for (int64_t gi = sgi; gi < ngrp; gi += kSubs) {
const int64_t i0 = gi * g + lane * 8;
float c[8];
float amax = 0.0f;
for (int u = 0; u < 8; ++u) {
c[u] = rad_bf16_to_f32(rad_f32_to_bf16(sv_[i0 + u] * rs * wl[i0 + u]));
amax = sycl::fmax(amax, sycl::fabs(c[u]));
}
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
const float sc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f;
const float inv = 1.0f / sc;
sycl::vec<uint8_t, 8> qb;
V8 ov;
for (int u = 0; u < 8; ++u) {
float cc = c[u] * inv;
if (cc > 448.0f) cc = 448.0f;
if (cc < -448.0f) cc = -448.0f;
qb[u] = rad_f32_to_fp8e4m3(cc);
ov[u] = rad_f32_to_bf16(c[u]);
}
*(sycl::vec<uint8_t, 8>*)((uint8_t*)qv.base + r * qv.pitch + i0) = qb;
if (has_ob) *(V8*)((uint16_t*)ob.base + r * ob.pitch + i0) = ov;
if (lane == 0) rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
}
});
});
});
}
if (n <= kRowMax) {
/* A row up to kRowMax in local memory: the (narrowed) residual sum and its squares are
* formed once from one read of x and the residual, the sum stored once; sub-group 0 adds