diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index ba572dad..7295eb50 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -1342,6 +1342,7 @@ constexpr int64_t kWgMaxV = 256; constexpr int64_t kWgMaxK = 256; constexpr int kRegK = 128; +constexpr int kBRegs = (int)(kWgMaxV / kSub); /* kernel B's columns a lane */ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* orow, const float* gates); int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) { @@ -1507,7 +1508,69 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t }); }); if (rc < 0) return rc; - /* kernel B: the gated RMS norm and the fp8 group, a work-group a (token, head) */ + /* kernel B: the gated RMS norm and the fp8 group, a sub-group a (token, head), its V columns + * kSub apart in each lane's registers. The sum over e is libref's chain, lane by lane in e + * order; the amax is a maximum, exact in any order. One wave of Tk * Hv threads: a work-group + * of V work-items a (token, head), each a few dependent steps of latency, took ~2.4 waves of + * the card's threads: 31.8 -> 24.9 us a call at Tk = 8, Hv = 48. */ + if (V % kSub == 0 && V / kSub <= kBRegs && !std::getenv("XE2_GDN_NORM_WG")) { + constexpr size_t kSgs = 8; /* sub-groups a work-group */ + const int64_t rows = Tk * Hv; + return submit([&] { + qu.parallel_for(sycl::nd_range<1>{(size_t)((rows + kSgs - 1) / kSgs) * kSgs * kSub, kSgs * kSub}, + [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + const sycl::sub_group sg = it.get_sub_group(); + const int64_t th = (int64_t)(it.get_global_id(0) / kSub); + if (th >= rows) return; /* uniform across the sub-group */ + const int64_t tok = th / Hv, h = th % Hv; + const int l = (int)sg.get_local_linear_id(); + const int nj = (int)(V / kSub); + int64_t n = 0; + while (n + 1 < n_seq && r.cu.at(n + 1) <= tok) ++n; + if (!seq_active(r, n)) return; + float ov[kBRegs]; + for (int j = 0; j < kBRegs; ++j) ov[j] = j < nj ? orow[th * V + j * kSub + l] : 0.0f; + float sq2 = 0.0f; + for (int j = 0; j < kBRegs; ++j) { + if (j >= nj) break; + for (int i = 0; i < kSub; ++i) { + const float v = sycl::select_from_group(sg, ov[j], i); + sq2 += v * v; + } + } + const float sc = 1.0f / sycl::sqrt(sq2 / (float)V + r.neps); + float amax = 0.0f; + for (int j = 0; j < kBRegs; ++j) { + if (j >= nj) break; + const int64_t e = j * kSub + l; + float x = ov[j] * sc; + if (r.has_nw) x *= r.nw.ld(e * r.nw_s); + if (r.has_z) { + const float zz = r.z.ld(at3(r.zv, tok, h, e)); + x *= (r.nact == 1 ? sigm(zz) : silu(zz)); + } + r.o.st(at3(r.ov, tok, h, e), x); + if (r.has_q8) { + ov[j] = r.o.ld(at3(r.ov, tok, h, e)); + amax = sycl::maximum()(amax, sycl::fabs(ov[j])); + } + } + if (!r.has_q8) return; + amax = sycl::reduce_over_group(sg, amax, sycl::maximum()); + const float qsc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f; + const float inv = 1.0f / qsc; + for (int j = 0; j < kBRegs; ++j) { + if (j >= nj) break; + const int64_t e = j * kSub + l; + float c = ov[j] * inv; + if (c > 448.0f) c = 448.0f; + if (c < -448.0f) c = -448.0f; + r.oq.st(tok * r.oq_rs + h * V * r.oq_cs + e * r.oq_cs, c); + } + if (l == 0) r.osc.st(tok * r.osc_rs + h * r.osc_cs, qsc); + }); + }); + } return submit([&] { qu.submit([&](sycl::handler& hd) { sycl::local_accessor ob(sycl::range<1>((size_t)V), hd);