From 84732432c01bc763562329d1a7928eafe28fb823 Mon Sep 17 00:00:00 2001 From: pascallinxweiler Date: Sat, 24 Oct 2026 12:42:02 +0200 Subject: [PATCH] libxe2: gdn recurrent update with typed operands and coalesced state rows -- 700 -> 285 us a verify call The register kernel read every element through the dtype-switching accessors, so a row's 128 state loads did not issue together, and a lane stored its own row 512 bytes from its neighbours. It is now templated on the staged q/k/v type and, for an f32 state with contiguous rows, moves a work-group's 16 rows through local memory in coalesced lines. The anchor replay's decays are taken one token a lane. At the speculative verify (8 tokens): the 8-token pass 408 -> 101 us, the replay 222 -> 110 us; 48 calls a round, 32.7 -> ~13 ms. Bit-identical to libref (fixture 90/90 at rel_l2 0, 400/400 live oracle issues). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1 --- libxe2/xe2_gdn.cpp | 104 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 20 deletions(-) diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 110c9347..77e02b68 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -17,6 +17,7 @@ #include #include +#include namespace xe2 { @@ -1342,7 +1343,12 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t * broadcasts. Every chain is the one above (over d in order), so the result is the same, without * the local-memory round trips a column's three passes over d cost there. */ -int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* orow, const float* gates) { +/* QT: the staged q, k and v as stored (uint16_t bf16, float, or void for any dtype); COAL: an f32 + * state with contiguous rows, loaded and stored a work-group's 16 rows at a time through local + * memory (coalesced) rather than a row a lane. The generic accessors switch on the dtype at every + * element, which kept the 128 loads of a row from issuing together: 8x the probe's time. */ +template +int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* orow, const float* gates) { const int64_t V = r.V, Hv = r.Hv; const int64_t nblk = V / kCb; namespace intelex = sycl::ext::intel::experimental; @@ -1351,9 +1357,25 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* for (int phase = r.anchor ? 0 : 1; phase <= 1 && rc >= 0; ++phase) rc = submit([&] { qu.submit([&](sycl::handler& hd) { + sycl::local_accessor tr(sycl::range<1>(COAL ? (size_t)kCb * (kRegK + 1) : 1), hd); hd.parallel_for(sycl::nd_range<1>{(size_t)(n_seq * Hv * nblk) * kCb, (size_t)kCb}, props, [=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { constexpr int K = kRegK; + char* const sp = r.state.p; + const uint32_t sdt = r.state.dt; + auto LS = [&](int64_t o) -> float { + if constexpr (COAL) return ((const float*)sp)[o]; + else return rad_load_f32(sp, sdt, o); + }; + auto SS = [&](int64_t o, float v) { + if constexpr (COAL) ((float*)sp)[o] = v; + else rad_store_f32(sp, sdt, o, v); + }; + auto LQ = [&](const DT& d, int64_t o) -> float { + if constexpr (std::is_same_v) return rad_bf16_to_f32(((const uint16_t*)d.p)[o]); + else if constexpr (std::is_same_v) return ((const float*)d.p)[o]; + else return d.ld(o); + }; 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; @@ -1375,7 +1397,7 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* if (pad) return; if (phase == 0) { const int64_t fb0 = at4(r.stv, col[4], h, 0, 0); - const int m = (int)(r.state.ld(fb0 + al.mark) + 0.5f) - 1; + const int m = (int)(LS(fb0 + al.mark) + 0.5f) - 1; slot_in = col[3 - ((m >= 0 && m <= 2) ? m : 1)]; } else { slot_in = col[2]; @@ -1389,7 +1411,35 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* r.anchor ? at4(r.stv, col[5], h, 0, 0) : 0 }; const bool dup_f = r.anchor && (col[3] != col[4] || col[5] != col[4]); float st[K]; - for (int d = 0; d < K; ++d) st[d] = r.state.ld(at4(r.stv, slot_in, h, e, d)); + auto load_rows = [&](int64_t slot) { + if constexpr (COAL) { + const float* base = (const float*)sp + at4(r.stv, slot, h, e0, 0); + for (int i = 0; i < kCb * K / kSub; ++i) { + const int idx = i * kSub + l; + tr[(idx / K) * (K + 1) + idx % K] = base[idx]; + } + sycl::group_barrier(sg); + for (int d = 0; d < K; ++d) st[d] = tr[l * (K + 1) + d]; + sycl::group_barrier(sg); + } else { + for (int d = 0; d < K; ++d) st[d] = LS(at4(r.stv, slot, h, e, d)); + } + }; + auto store_rows = [&](int64_t slot) { + if constexpr (COAL) { + for (int d = 0; d < K; ++d) tr[l * (K + 1) + d] = st[d]; + sycl::group_barrier(sg); + float* base = (float*)sp + at4(r.stv, slot, h, e0, 0); + for (int i = 0; i < kCb * K / kSub; ++i) { + const int idx = i * kSub + l; + base[idx] = tr[(idx / K) * (K + 1) + idx % K]; + } + sycl::group_barrier(sg); + } else { + for (int d = 0; d < K; ++d) SS(at4(r.stv, slot, h, e, d), st[d]); + } + }; + load_rows(slot_in); float knv[K / kSub]; /* kn[16 j + lane] */ auto kn_at = [&](int d) { return sycl::select_from_group(sg, knv[d / kSub], d % kSub); }; auto update = [&](float dec, float bt, float ve) { @@ -1402,20 +1452,21 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* for (int d = 0; d < K; ++d) st[d] += uu * kn_at(d); }; if (r.anchor && 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; + const float decl = l < na ? rexp(LS(fb + l * al.fstr + al.gb + 2 * gi)) : 0.0f; for (int64_t t = 0; t < na; ++t) { const int64_t f = fb + t * al.fstr; - for (int64_t gi = 0; gi < al.groups; ++gi) { - if (!(e0 >= 32 * gi && e0 < sycl::min(V, 32 * gi + 32))) continue; /* uniform */ - for (int j = 0; j < K / kSub; ++j) knv[j] = r.state.ld(f + gi * K + j * kSub + l); - update(rexp(r.state.ld(f + al.gb + 2 * gi)), r.state.ld(f + al.gb + 2 * gi + 1), - r.state.ld(f + al.vb + e)); - } + for (int j = 0; j < K / kSub; ++j) knv[j] = LS(f + gi * K + j * kSub + l); + update(sycl::select_from_group(sg, decl, (int)t), LS(f + al.gb + 2 * gi + 1), + LS(f + al.vb + e)); } - for (int d = 0; d < K; ++d) r.state.st(at4(r.stv, col[2], h, e, d), st[d]); + store_rows(col[2]); return; } if (r.anchor && blk == 0 && l == 0) - for (int c = 0; c < 3; ++c) r.state.st(fbs[c] + al.mark, (float)(c + 1)); + for (int c = 0; c < 3; ++c) SS(fbs[c] + al.mark, (float)(c + 1)); for (int64_t t = 0; t < q_len; ++t) { const int64_t tok = bos + t; const float gv = gates[(tok * Hv + h) * 3 + 0]; @@ -1423,8 +1474,8 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* const float dec = gates[(tok * Hv + h) * 3 + 2]; float qv[K / kSub], kv[K / kSub]; for (int j = 0; j < K / kSub; ++j) { - qv[j] = r.q.ld(at3(r.qv, tok, hk, j * kSub + l)); - kv[j] = r.k.ld(at3(r.kv, tok, hk, j * kSub + l)); + qv[j] = LQ(r.q, at3(r.qv, tok, hk, j * kSub + l)); + kv[j] = LQ(r.k, at3(r.kv, tok, hk, j * kSub + l)); } float sq = 0.0f, sk = 0.0f; /* libref's chains, d in order, in every lane */ for (int d = 0; d < K; ++d) { @@ -1440,7 +1491,7 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* qnv[j] = qv[j] * qs; knv[j] = kv[j] * ks; } - update(dec, bt, r.v.ld(at3(r.vv, tok, h, e))); + 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; @@ -1450,18 +1501,17 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* const int64_t f = fbs[c] + t * al.fstr; if (blk == 0) for (int64_t gi = 0; gi < al.groups; ++gi) { - for (int j = 0; j < K / kSub; ++j) r.state.st(f + gi * K + j * kSub + l, knv[j]); + for (int j = 0; j < K / kSub; ++j) SS(f + gi * K + j * kSub + l, knv[j]); if (l == 0) { - r.state.st(f + al.gb + 2 * gi, gv); - r.state.st(f + al.gb + 2 * gi + 1, bt); + SS(f + al.gb + 2 * gi, gv); + SS(f + al.gb + 2 * gi + 1, bt); } } - r.state.st(f + al.vb + e, r.v.ld(at3(r.vv, tok, h, e))); + SS(f + al.vb + e, LQ(r.v, at3(r.vv, tok, h, e))); } if (r.anchor) continue; 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) - for (int d = 0; d < K; ++d) r.state.st(at4(r.stv, so, h, e, d), st[d]); + if (so >= 0) store_rows(so); /* uniform */ } }); }); @@ -1469,6 +1519,20 @@ int launch_recurrent_reg(sycl::queue& qu, const RecIO& r, int64_t n_seq, float* return rc; } +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 && + !std::getenv("XE2_GDN_GENERIC"); + 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); + 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); +} + int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t Tk) { /* Kernel B covers every token row: a row no sequence owns this step (cu stops short of T) is * normed from a stale scratch row, so the split form is taken only when cu covers all of T,