Archived
libxe2: rmsnorm_quant_fp8 keeps a row's values in registers -- 0.30 -> 0.16 s a 1882-token prefill
A sub-group now loads the 128-groups it later quantises, so the normed inputs and the gain stay in its registers between the ordered sum and the codes; only the squares go to local memory. Three n-wide arrays (60 KB at n = 5120) allowed two rows on a Xe core at once, and a row's sum is a serial chain, so the prefill's 941-row calls ran as ~24 waves of it. Bit-identical: fixture 20/20 and 400/400 live oracle issues at rel 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
1 parent
a7868f37f3
commit
bbc7607222
1 file changed
+26
-20
+26
-20
@@ -262,27 +262,31 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
* 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. */
|
||||
/* Sub-group s loads the groups s, s + 16, ... it later quantises (lane l the 8 values at
|
||||
* 8 l of each), so the normed inputs and the gain stay in its registers between the sum and
|
||||
* the codes; only the squares go to local memory (20 KB at n = 5120, so several rows run on
|
||||
* a Xe core at once -- three n-wide arrays allowed two). */
|
||||
constexpr int kGrpMax = (int)(kRowMax / (8 * kSub) / kSubs); /* groups a sub-group */
|
||||
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 sycl::sub_group sg = it.get_sub_group();
|
||||
const int sgi = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
|
||||
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];
|
||||
V8 xv[kGrpMax], rv[kGrpMax];
|
||||
sycl::vec<float, 8> wv[kGrpMax];
|
||||
#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;
|
||||
for (int j = 0; j < kGrpMax; ++j) {
|
||||
const int64_t gi = sgi + (int64_t)j * kSubs;
|
||||
const int64_t c = gi * g + lane * 8;
|
||||
const int64_t cc = gi < ngrp ? c : 0;
|
||||
xv[j] = *(const V8*)(xr + cc);
|
||||
rv[j] = has_res ? *(const V8*)(rr_ + cc) : V8(0);
|
||||
if (wf) {
|
||||
@@ -294,10 +298,12 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
for (int u = 0; u < 8; ++u) wv[j][u] = rad_bf16_to_f32(w8[u]);
|
||||
}
|
||||
}
|
||||
float vv[kGrpMax][8];
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kPer; ++j) {
|
||||
const int64_t c = (t + (int64_t)j * kWg) * 8;
|
||||
if (c >= n) continue;
|
||||
for (int j = 0; j < kGrpMax; ++j) {
|
||||
const int64_t gi = sgi + (int64_t)j * kSubs;
|
||||
if (gi >= ngrp) continue;
|
||||
const int64_t c = gi * g + lane * 8;
|
||||
V8 so;
|
||||
for (int u = 0; u < 8; ++u) {
|
||||
float v = rad_bf16_to_f32(xv[j][u]);
|
||||
@@ -306,17 +312,15 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
so[u] = rad_f32_to_bf16(s0);
|
||||
v = rad_bf16_to_f32(so[u]); /* what reading it back gives */
|
||||
}
|
||||
sv_[c + u] = v;
|
||||
vv[j][u] = v;
|
||||
sq[c + u] = v * v;
|
||||
wl[c + u] = wv[j][u] + wadd;
|
||||
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) {
|
||||
if (sgi == 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);
|
||||
@@ -324,13 +328,15 @@ int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
|
||||
}
|
||||
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) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < kGrpMax; ++j) {
|
||||
const int64_t gi = sgi + (int64_t)j * kSubs;
|
||||
if (gi >= ngrp) continue; /* uniform */
|
||||
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]));
|
||||
c[u] = rad_bf16_to_f32(rad_f32_to_bf16(vv[j][u] * rs * wv[j][u]));
|
||||
amax = sycl::fmax(amax, sycl::fabs(c[u]));
|
||||
}
|
||||
amax = sycl::reduce_over_group(sg, amax, sycl::maximum<float>());
|
||||
|
||||
Reference in new issue
Block a user