From 7dd17c6488e55e01574d6451f55a605d8ac54d2a Mon Sep 17 00:00:00 2001 From: claude Date: Mon, 26 Oct 2026 20:00:15 +0100 Subject: [PATCH] libxe2: gdn recurrent update as one launch a step -- replay, token pass, gates and gated norm fused A work-group a (sequence, v head), its V / 16 sub-groups the column blocks: the replay and the token pass are ordered by a work-group barrier instead of a launch boundary, the replayed state stays in registers instead of going to its slot and back, the gates are formed in the kernel and the gated norm and fp8 group run at the end over outputs kept in local memory. Four launches a layer (4.31 ms a round) -> one (3.76 ms). XE2_GDN_SPLIT=1 keeps the split form. rad-kbench on the 27B: gdn_recurrent_update anchor q_len 4/8/16 rel_l2 0 against libref, 90/90 gdn cases passed; greedy output identical (8/8). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_gdn.cpp | 138 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 117 insertions(+), 21 deletions(-) diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 7295eb50..c30e6b0c 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -1345,9 +1345,12 @@ 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_fused(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk); + int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) { const int64_t K = r.K, V = r.V, Hv = r.Hv; const int64_t nblk = V / kCb; + if (int rc = launch_recurrent_fused(qu, r, n_seq, Tk); rc != RAD_E_UNSUPPORTED) return rc; float* orow = workspace(qu, (size_t)(Tk * Hv * V + 64), 3); if (!orow) return RAD_E_DEVICE; /* Each token's gate, beta and decay -- libref's double exp / log1p, which this card emulates -- @@ -1627,17 +1630,21 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t * from issuing together: 8x the probe's time. (Staging the block's 16 rows through local memory * instead, coalesced, took 8.3 KB a 16-lane work-group: ~300 of the 384 fit the card at once, and * the phase ran in two waves -- 39.6 against 24.7 us in probe rec2.cpp.) */ -template -int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* orow, const float* gates) { +template +int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* orow, const float* gates, + int64_t Tk) { const int64_t V = r.V, Hv = r.Hv; const int64_t nblk = V / kCb; namespace intelex = sycl::ext::intel::experimental; syclex::properties props{intelex::grf_size<256>}; int rc = RAD_OK; - for (int phase = r.anchor ? 0 : 1; phase <= 1 && rc >= 0; ++phase) + /* FUSED: one launch, both phases, the gates and kernel B's epilogue in it (see launch_recurrent_wg) */ + for (int phase = (r.anchor && !FUSED) ? 0 : 1; phase <= 1 && rc >= 0; ++phase) rc = submit([&] { qu.submit([&](sycl::handler& hd) { - hd.parallel_for(sycl::nd_range<1>{(size_t)(n_seq * Hv * nblk) * kCb, (size_t)kCb}, props, + sycl::local_accessor os(sycl::range<1>(FUSED ? (size_t)(Tk * V) : 1), hd); + const size_t wg = FUSED ? (size_t)V : (size_t)kCb; + hd.parallel_for(sycl::nd_range<1>{(size_t)(n_seq * Hv * nblk) * kCb, wg}, props, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { constexpr int K = kRegK; char* const sp = r.state.p; @@ -1657,9 +1664,10 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float }; const sycl::sub_group sg = it.get_sub_group(); const int64_t gid = (int64_t)it.get_group(0); - const int64_t blk = gid % nblk, nh = gid / nblk; + const int64_t blk = FUSED ? (int64_t)sg.get_group_linear_id() : gid % nblk; + const int64_t nh = FUSED ? gid : gid / nblk; const int64_t n = nh / Hv, h = nh % Hv, hk = h / r.grp; - const int l = (int)it.get_local_id(0); + const int l = (int)sg.get_local_linear_id(); const int64_t e0 = blk * kCb, e = e0 + l; const int64_t bos = r.cu.at(n), q_len = r.cu.at(n + 1) - bos; if (q_len <= 0 && !r.anchor) return; @@ -1674,7 +1682,7 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float bool pad = false; for (int c = 0; c < 6; ++c) pad = pad || col[c] < 0; if (pad) return; - if (phase == 0) { + if (FUSED || phase == 0) { const int64_t fb0 = at4(r.stv, col[4], h, 0, 0); const int m = (int)(LS(fb0 + al.mark) + 0.5f) - 1; slot_in = col[3 - ((m >= 0 && m <= 2) ? m : 1)]; @@ -1723,7 +1731,7 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float const float uu = bt * (ve - sk2); for (int d = 0; d < K; ++d) st[d] += uu * kn_at(d); }; - if (r.anchor && phase == 0) { + if (r.anchor && (FUSED || phase == 0)) { /* the block's 32-column group; its replay decays, one token a lane (the * double exp is emulated: a chain of na of them was most of this phase) */ const int64_t gi = e0 / 32; @@ -1735,15 +1743,34 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float LS(f + al.vb + e)); } store_rows(col[2]); - return; + /* the head's replay reads the factors and the stamp that its token pass + * overwrites below: every block done reading first */ + if constexpr (FUSED) sycl::group_barrier(it.get_group()); + else return; } if (r.anchor && blk == 0 && l == 0) for (int c = 0; c < 3; ++c) SS(fbs[c] + al.mark, (float)(c + 1)); + float g_l[3] = { 0.0f, 0.0f, 0.0f }; /* FUSED: token (t & ~15) + lane's gates */ for (int64_t t = 0; t < q_len; ++t) { const int64_t tok = bos + t; - const float gv = gates[(tok * Hv + h) * 3 + 0]; - const float bt = gates[(tok * Hv + h) * 3 + 1]; - const float dec = gates[(tok * Hv + h) * 3 + 2]; + float gv, bt, dec; + if constexpr (FUSED) { + /* the gates kernel's arithmetic, sixteen tokens at a time, a token a lane */ + if ((t & 15) == 0 && t + l < q_len) { + const float alog = r.alog.p ? r.alog.ld(h * r.alog_s) : 0.0f; + const float dtb = r.dtb.p ? r.dtb.ld(h * r.dtb_s) : 0.0f; + g_l[0] = -rexp(alog) * softplus(r.av.ld(at2(r.a2, tok + l, h)) + dtb, r.spthr); + g_l[1] = sigm(r.bv.ld(at2(r.b2, tok + l, h))); + g_l[2] = rexp(g_l[0]); + } + gv = sycl::select_from_group(sg, g_l[0], (int)(t & 15)); + bt = sycl::select_from_group(sg, g_l[1], (int)(t & 15)); + dec = sycl::select_from_group(sg, g_l[2], (int)(t & 15)); + } else { + gv = gates[(tok * Hv + h) * 3 + 0]; + bt = gates[(tok * Hv + h) * 3 + 1]; + dec = gates[(tok * Hv + h) * 3 + 2]; + } float qv[K / kSub], kv[K / kSub]; for (int j = 0; j < K / kSub; ++j) { qv[j] = LQ(r.q, at3(r.qv, tok, hk, j * kSub + l)); @@ -1766,7 +1793,8 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float update(dec, bt, LQ(r.v, at3(r.vv, tok, h, e))); float oe = 0.0f; for (int d = 0; d < K; ++d) oe += st[d] * sycl::select_from_group(sg, qnv[d / kSub], d % kSub); - orow[(tok * Hv + h) * V + e] = oe; + if constexpr (FUSED) os[t * V + e] = oe; + else orow[(tok * Hv + h) * V + e] = oe; if (r.anchor) for (int c = 0; c < 3; ++c) { if (c != 1 && !dup_f) continue; @@ -1785,25 +1813,93 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float const int64_t so = r.has_si ? r.sidx.li(at2(r.si, n, t < r.sw ? t : r.sw - 1)) : n; if (so >= 0) store_rows(so); /* uniform */ } + if constexpr (FUSED) { + /* kernel B's epilogue, a sub-group a token, from the outputs in local memory */ + sycl::group_barrier(it.get_group()); + for (int64_t t = blk; t < q_len; t += nblk) { + const int64_t tok = bos + t; + float ov[K / kSub]; + for (int j = 0; j < K / kSub; ++j) ov[j] = j < nblk ? os[t * V + j * kSub + l] : 0.0f; + float sq2 = 0.0f; + for (int j = 0; j < K / kSub; ++j) { + if (j >= nblk) 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 < K / kSub; ++j) { + if (j >= nblk) break; + const int64_t ee = j * kSub + l; + float x = ov[j] * sc; + if (r.has_nw) x *= r.nw.ld(ee * r.nw_s); + if (r.has_z) { + const float zz = r.z.ld(at3(r.zv, tok, h, ee)); + x *= (r.nact == 1 ? sigm(zz) : silu(zz)); + } + r.o.st(at3(r.ov, tok, h, ee), x); + if (r.has_q8) { + ov[j] = r.o.ld(at3(r.ov, tok, h, ee)); + amax = sycl::maximum()(amax, sycl::fabs(ov[j])); + } + } + if (!r.has_q8) continue; + 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 < K / kSub; ++j) { + if (j >= nblk) break; + const int64_t ee = 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 + ee * r.oq_cs, c); + } + if (l == 0) r.osc.st(tok * r.osc_rs + h * r.osc_cs, qsc); + } + } }); }); }); return rc; } +bool coal_state(const RecIO& r) { + return r.state.dt == RAD_F32 && r.stv.s3 == 1 && r.stv.s2 == kRegK && r.V % kCb == 0 && + rad_ptr_aligned(r.state.p, 16) && r.stv.s1 % 4 == 0 && r.stv.s0 % 4 == 0 && + !std::getenv("XE2_GDN_GENERIC"); +} + +/* The register kernel as one launch a step: a work-group a (sequence, v head), its V / 16 + * sub-groups the column blocks, so the replay and the token pass are ordered by a work-group + * barrier instead of a launch boundary, the replayed state stays in registers rather than going + * to its slot and back, the gates are formed in the kernel, and kernel B's norm and fp8 group run + * at the end over the outputs kept in local memory. The same arithmetic as the split form. */ +int launch_recurrent_fused(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) { + if (r.K != kRegK || r.V > kRegK || r.V % kCb || !coal_state(r) || Tk * r.V > 8192 || + std::getenv("XE2_GDN_SPLIT") || std::getenv("XE2_GDN_SLM")) + return RAD_E_UNSUPPORTED; + const uint32_t qd = r.q.dt; + if (r.k.dt != qd || r.v.dt != qd) return RAD_E_UNSUPPORTED; + if (qd == RAD_BF16) return launch_recurrent_reg_t(qu, r, n_seq, nullptr, nullptr, Tk); + if (qd == RAD_F32) return launch_recurrent_reg_t(qu, r, n_seq, nullptr, nullptr, Tk); + return RAD_E_UNSUPPORTED; +} + int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* orow, const float* gates) { - const bool coal = r.state.dt == RAD_F32 && r.stv.s3 == 1 && r.stv.s2 == kRegK && r.V % kCb == 0 && - rad_ptr_aligned(r.state.p, 16) && r.stv.s1 % 4 == 0 && r.stv.s0 % 4 == 0 && - !std::getenv("XE2_GDN_GENERIC"); + constexpr int64_t Tk = 0; + const bool coal = coal_state(r); const uint32_t qd = r.q.dt; const bool same = r.k.dt == qd && r.v.dt == qd; if (same && qd == RAD_BF16) - return coal ? launch_recurrent_reg_t(qu, r, n_seq, orow, gates) - : launch_recurrent_reg_t(qu, r, n_seq, orow, gates); + return coal ? launch_recurrent_reg_t(qu, r, n_seq, orow, gates, Tk) + : launch_recurrent_reg_t(qu, r, n_seq, orow, gates, Tk); if (same && qd == RAD_F32) - return coal ? launch_recurrent_reg_t(qu, r, n_seq, orow, gates) - : launch_recurrent_reg_t(qu, r, n_seq, orow, gates); - return launch_recurrent_reg_t(qu, r, n_seq, orow, gates); + return coal ? launch_recurrent_reg_t(qu, r, n_seq, orow, gates, Tk) + : launch_recurrent_reg_t(qu, r, n_seq, orow, gates, Tk); + return launch_recurrent_reg_t(qu, r, n_seq, orow, gates, Tk); } int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) {