libxe2: gdn recurrent state rows loaded and stored as a lane's own 16-byte vectors -- 50 -> 28 us a call

The 16-row block staged through local memory took 8.3 KB a 16-lane work-group, so only ~300 of the
384 work-groups fit the card at once and each phase ran in two waves (probe rec2.cpp: 39.6 vs 24.7
us for the 8-token pass). Same values into the same registers: greedy output identical (8/8); a
round's device time 75.8 -> 72.8 ms (unitrace, story prompt).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-26 19:35:14 +01:00
1 parent 2675dba42c
commit d87dd860ce
1 file changed
+14 -24
+14 -24
View File
@@ -1559,9 +1559,11 @@ int launch_recurrent_wg(sycl::queue& qu, const RecIO& r, int64_t n_seq, int64_t
* the local-memory round trips a column's three passes over d cost there. */
/* 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. */
* state with contiguous 16-byte aligned rows, a lane's row loaded and stored as 16-byte vectors.
* 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. (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 <class QT, bool COAL>
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;
@@ -1572,7 +1574,6 @@ int launch_recurrent_reg_t(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<float, 1> 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;
@@ -1628,33 +1629,21 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float
float st[K];
auto load_rows = [&](int64_t slot) {
if constexpr (COAL) {
/* every load issued before the first local store: the state pointer is
* generic, so a load after a local store waits for it, and interleaved
* the 128 loads ran one memory latency each */
const float* base = (const float*)sp + at4(r.stv, slot, h, e0, 0);
float ld[kCb * K / kSub];
for (int i = 0; i < kCb * K / kSub; ++i) ld[i] = base[i * kSub + l];
for (int i = 0; i < kCb * K / kSub; ++i) {
const int idx = i * kSub + l;
tr[(idx / K) * (K + 1) + idx % K] = ld[i];
/* the lane's own row, 512 contiguous bytes, as 16-byte loads */
const sycl::float4* row = (const sycl::float4*)((const float*)sp + at4(r.stv, slot, h, e, 0));
for (int i = 0; i < K / 4; ++i) {
const sycl::float4 x = row[i];
st[4 * i] = x[0]; st[4 * i + 1] = x[1]; st[4 * i + 2] = x[2]; st[4 * i + 3] = x[3];
}
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);
sycl::float4* row = (sycl::float4*)((float*)sp + at4(r.stv, slot, h, e, 0));
for (int i = 0; i < K / 4; ++i)
row[i] = sycl::float4(st[4 * i], st[4 * i + 1], st[4 * i + 2], st[4 * i + 3]);
} else {
for (int d = 0; d < K; ++d) SS(at4(r.stv, slot, h, e, d), st[d]);
}
@@ -1741,6 +1730,7 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float
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");
const uint32_t qd = r.q.dt;
const bool same = r.k.dt == qd && r.v.dt == qd;