From c943bc65e3751c10c2ff8a29b42bc33b98a7c24a Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 12:01:34 +0200 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_norm.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/libxe2/xe2_norm.cpp b/libxe2/xe2_norm.cpp index 4c9b3eb4..5057082a 100644 --- a/libxe2/xe2_norm.cpp +++ b/libxe2/xe2_norm.cpp @@ -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 sv_(sycl::range<1>((size_t)n), h); + sycl::local_accessor sq(sycl::range<1>((size_t)n), h); + sycl::local_accessor 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; + 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 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 w0 = *(const sycl::vec*)((const float*)gbase + cc); + const sycl::vec w1 = *(const sycl::vec*)((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()); + const float sc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f; + const float inv = 1.0f / sc; + sycl::vec 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*)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