diff --git a/libxe2/xe2_norm.cpp b/libxe2/xe2_norm.cpp index 5057082a..6e239b61 100644 --- a/libxe2/xe2_norm.cpp +++ b/libxe2/xe2_norm.cpp @@ -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 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 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 wv[kPer]; + V8 xv[kGrpMax], rv[kGrpMax]; + sycl::vec 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());