diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 4d49fb14..1527305a 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -977,6 +977,9 @@ inline bool seq_active(const RecIO& r, int64_t n) { constexpr int64_t kWgMaxV = 256; constexpr int64_t kWgMaxK = 256; +constexpr int kRegK = 128; +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) { const int64_t K = r.K, V = r.V, Hv = r.Hv; const int64_t nblk = V / kCb; @@ -1002,6 +1005,9 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t /* Under `anchor` the replay is its own launch (phase 0): every block reads the stamp and the * previous step's factors before any block of the head overwrites them in phase 1. */ int rc = RAD_OK; + if (K == kRegK && !std::getenv("XE2_GDN_SLM")) { + rc = launch_recurrent_reg(qu, r, n_seq, orow, gates); + } else for (int phase = r.anchor ? 0 : 1; phase <= 1 && rc >= 0; ++phase) rc = submit([&] { qu.submit([&](sycl::handler& hd) { @@ -1183,6 +1189,138 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t }); } +/* Kernel A with the state in registers, for head_k = 128: a lane's value column of the block is + * st[0..127] (the large register file), k and q spread over the 16 lanes and read by compile-time + * 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) { + 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) + 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::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] { + constexpr int K = kRegK; + 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 n = nh / Hv, h = nh % Hv, hk = h / r.grp; + const int l = (int)it.get_local_id(0); + 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; + const int64_t na = r.nacc.on ? r.nacc.at(n) : 1; + const int64_t rd = na > 0 ? na - 1 : 0; + const Anchor& al = r.al; + int64_t col[6] = { 0, 0, 0, 0, 0, 0 }; + int64_t slot_in = 0; + if (r.anchor) { + if (q_len > al.tmax || na > al.tmax) return; + for (int c = 0; c < 6; ++c) col[c] = r.sidx.li(at2(r.si, n, c)); + bool pad = false; + for (int c = 0; c < 6; ++c) pad = pad || col[c] < 0; + 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; + slot_in = col[3 - ((m >= 0 && m <= 2) ? m : 1)]; + } else { + slot_in = col[2]; + } + } else { + slot_in = r.has_si ? r.sidx.li(at2(r.si, n, rd < r.sw ? rd : r.sw - 1)) : n; + if (slot_in < 0) return; + } + const int64_t fb = r.anchor ? at4(r.stv, col[4], h, 0, 0) : 0; + const int64_t fbs[3] = { r.anchor ? at4(r.stv, col[3], h, 0, 0) : 0, fb, + 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)); + 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) { + float sk2 = 0.0f; + for (int d = 0; d < K; ++d) { + st[d] *= dec; + sk2 += st[d] * kn_at(d); + } + const float uu = bt * (ve - sk2); + for (int d = 0; d < K; ++d) st[d] += uu * kn_at(d); + }; + if (r.anchor && phase == 0) { + 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 d = 0; d < K; ++d) r.state.st(at4(r.stv, col[2], h, e, d), st[d]); + 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 (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 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)); + } + float sq = 0.0f, sk = 0.0f; /* libref's chains, d in order, in every lane */ + for (int d = 0; d < K; ++d) { + const float u = sycl::select_from_group(sg, qv[d / kSub], d % kSub); + const float w = sycl::select_from_group(sg, kv[d / kSub], d % kSub); + sq += u * u; + sk += w * w; + } + const float qs = 1.0f / sycl::sqrt(sq + r.l2eps) * r.scale; + const float ks = 1.0f / sycl::sqrt(sk + r.l2eps); + float qnv[K / kSub]; + for (int j = 0; j < K / kSub; ++j) { + qnv[j] = qv[j] * qs; + knv[j] = kv[j] * ks; + } + update(dec, bt, r.v.ld(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 (r.anchor) + for (int c = 0; c < 3; ++c) { + if (c != 1 && !dup_f) continue; + 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]); + if (l == 0) { + r.state.st(f + al.gb + 2 * gi, gv); + r.state.st(f + al.gb + 2 * gi + 1, bt); + } + } + r.state.st(f + al.vb + e, r.v.ld(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]); + } + }); + }); + }); + return rc; +} + 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,