libxe2: fast decode kernels for Qwen3.8-27B-PARO-M2XFP4 -- 0.1 -> 12.1 tok/s on the B65

- m2xfp4 GEMV (M <= 16): e2m1 decoded to fp16 by bits alone ((mag << 9) | sign << 15 is exactly
  e2m1 * 2^-14; the card keeps fp16 subnormals), K permuted (u, u + 4) inside each 8 so a code
  pair is one shift and mask, the activation as fp16(a * a_scale / R * 2^14) in that order, half2
  subgroup and group sums, f32 across groups; four columns a sub-group at decode. The scalar
  form was ALU-bound (~10 ops a weight, 80 ms a token); this one runs at 415-525 GB/s on the
  27B's shapes in a standalone probe. rad-kbench against libref on the container: rel_l2 ~5e-5 to
  ~3e-3, tolerance 4e-2.
- m2xfp4 prefill (M >= 17): the weight decoded to bf16 (exact) and the activation to
  bf16(e4m3 * scale), then the bf16 XMX GEMM per part.
- givens_dense_quant: a sub-group an output, the rotation row read coalesced, libref's double sum
  as a float pair (two-product and two-sum, ~48 bits); the double form stays for XE2_EXACT=1.
  146 -> 5.4 ms a token.
- the joined fp8 head (logits_gemm, M <= 16): E4M3 to fp16 by bits ((b & 0x7F) << 7 is E4M3 *
  2^-8), bytes (0, 2), (1, 3) as half pairs; an i8 head or a narrow scale block element by element.
  43 -> 3.9 ms.
- gated delta net, still bit-identical to libref: the decode step split over 16-column blocks
  (384 work-groups, not 48) with the gated norm in a second kernel and the anchor replay its own
  launch (block 0 rewrites the factors other blocks read); the chunked scan and the kkt solve
  work-group parallel; persistent staging (workspace slots) instead of a malloc and a wait a call.
- rmsnorm_quant_fp8: the row in local memory, loads issued before stores, the ordered sum fed by
  sub-group broadcasts -- still libref's chain (a probe puts that chain's floor at ~32 us for 5120).
- round_dt converts the float dtypes directly.

rad-kbench: 1410/1410 on the fixture; the 27B container's M <= 32 bands 77/77. Decode (greedy, no
speculation) 12.1 tok/s, coherent text; with the DFlash2 drafter 5.3 tok/s and the same text.

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-24 03:22:48 +02:00
1 parent 3158324bdb
commit 15944be7e3
9 files changed
+1030 -35

No files matched your search

+388 -18
View File
@@ -349,6 +349,59 @@ int gdn_kkt_solve(const RadArgs* a, RadStream st) {
const IV cuv = iv_of(cu);
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
if (chunk <= 256 && chunk % kSub == 0) {
/* A work-group a (sequence, v head), a work-item a column j of the chunk: for each row i,
* work-item j forms row[j] (its sum over d in order) and then column j of the inverse,
* s[j] = delta - sum over l of row[l] A[l][j] in l order -- libref's chains, spread over j. */
const int64_t need = chunk * chunk + chunk * K;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
const size_t wg = (size_t)chunk;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> row(sycl::range<1>((size_t)chunk), hd);
sycl::local_accessor<float, 1> gcl(sycl::range<1>((size_t)chunk), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t nh = (int64_t)it.get_group(0);
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t j = (int64_t)it.get_local_id(0);
auto grpw = it.get_group();
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
float* Am = ws + nh * need;
float* kc = Am + chunk * chunk;
for (int64_t c0 = 0; c0 < slen; c0 += chunk) {
const int64_t L = (c0 + chunk < slen) ? chunk : slen - c0;
sycl::group_barrier(grpw);
if (j < L) {
const int64_t tj = bos + c0 + j;
gcl[j] = gs.ld(at2(gv, tj, h));
for (int64_t d = 0; d < K; ++d) kc[j * K + d] = ks.ld(at3(kv, tj, hk, d));
}
sycl::group_barrier(grpw);
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
if (j < i) {
const float bi = bs.ld(at2(bv, ti, h));
float rj = 0.0f;
for (int64_t d = 0; d < K; ++d) rj += kc[i * K + d] * kc[j * K + d];
row[j] = bi * rj * rexp(gcl[i] - gcl[j]);
}
sycl::group_barrier(grpw);
if (j <= i) {
float sj = (i == j) ? 1.0f : 0.0f;
for (int64_t l = j; l < i; ++l) sj -= row[l] * Am[l * chunk + j];
Am[i * chunk + j] = sj;
} else {
Am[i * chunk + j] = 0.0f;
}
As.st(at3(Av, ti, h, j), Am[i * chunk + j]);
sycl::group_barrier(grpw);
}
}
});
});
});
}
const int64_t need = chunk * chunk + 3 * chunk + 2 * chunk * K;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
@@ -442,6 +495,106 @@ int gdn_chunk_scan(const RadArgs* a, RadStream st) {
const IV cuv = iv_of(cu);
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
if (V % kSub == 0 && V <= 256 && chunk <= V && K * V <= 128 * 128) {
/* A work-group a (sequence, v head), a work-item a value column e (and, for the Q K^T
* rows, a chunk position j): the state in local memory, the chunk's widened operands in
* the workspace. Every element is libref's chain in libref's order: the sums over d for
* K S^T, Q S^T and the rows, over j for V' and O, and the state's decay then sum over j. */
const int64_t need = 3 * chunk * V + 2 * chunk * K + chunk * chunk;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
const size_t wg = (size_t)V;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> St(sycl::range<1>((size_t)(K * V)), hd);
sycl::local_accessor<float, 1> gcb(sycl::range<1>((size_t)(2 * chunk)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H) * wg, wg}, [=](sycl::nd_item<1> it) {
const int64_t nh = (int64_t)it.get_group(0);
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t e = (int64_t)it.get_local_id(0);
auto gw = it.get_group();
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
const int64_t sn = has_si ? sis.li(n * sw) : n;
if (sn < 0) return; /* uniform */
float* Wm = ws + nh * need;
float* Vp = Wm + chunk * V;
float* vc = Vp + chunk * V;
float* kc = vc + chunk * V;
float* qc = kc + chunk * K;
float* R = qc + chunk * K; /* [chunk, chunk]: row i of sA */
float* gc = &gcb[0];
float* bc = gc + chunk;
for (int64_t d = 0; d < K; ++d) St[d * V + e] = has_h0 ? h0s.ld(at4(h0v, sn, h, e, d)) : 0.0f;
for (int64_t c0 = 0; c0 < slen; c0 += chunk) {
const int64_t L = (c0 + chunk < slen) ? chunk : slen - c0;
sycl::group_barrier(gw);
for (int64_t j = 0; j < L; ++j) {
const int64_t tj = bos + c0 + j;
vc[j * V + e] = vs.ld(at3(vv, tj, h, e));
for (int64_t d = e; d < K; d += V) {
kc[j * K + d] = ks.ld(at3(kv, tj, hk, d));
qc[j * K + d] = qs.ld(at3(qv, tj, hk, d));
}
}
if (e < L) {
const int64_t tj = bos + c0 + e;
gc[e] = gs.ld(at2(gv, tj, h));
bc[e] = bs.ld(at2(bv, tj, h));
}
sycl::group_barrier(gw);
const float glast = gc[L - 1];
/* W = beta . (V - e^{g_j} . (K S^T)) */
for (int64_t j = 0; j < L; ++j) {
const float ge = rexp(gc[j]);
float acc = 0.0f;
for (int64_t d = 0; d < K; ++d) acc += kc[j * K + d] * St[d * V + e];
Wm[j * V + e] = bc[j] * (vc[j * V + e] - ge * acc);
}
/* V' = A W (lane e, sum over j <= i in order) */
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
float vp = 0.0f;
for (int64_t j = 0; j <= i; ++j) vp += As.ld(at3(Av, ti, h, j)) * Wm[j * V + e];
Vp[i * V + e] = vp;
}
/* the sA rows, lane j: R[i][j] = scale e^{g_i - g_j} sum_d q_i k_j */
if (e < L)
for (int64_t i = e; i < L; ++i) {
float rj = 0.0f;
for (int64_t d = 0; d < K; ++d) rj += qc[i * K + d] * kc[e * K + d];
R[i * chunk + e] = scale * rexp(gc[i] - gc[e]) * rj;
}
sycl::group_barrier(gw);
/* O = scale . e^{g_i} . (Q S^T) + sA V' */
for (int64_t i = 0; i < L; ++i) {
const int64_t ti = bos + c0 + i;
const float sg = scale * rexp(gc[i]);
float inr = 0.0f;
for (int64_t d = 0; d < K; ++d) inr += qc[i * K + d] * St[d * V + e];
float acc = sg * inr;
for (int64_t j = 0; j <= i; ++j) acc += R[i * chunk + j] * Vp[j * V + e];
os.st(at3(ov, ti, h, e), acc);
}
sycl::group_barrier(gw);
/* S = e^{g_last} S + V'^T (e^{g_last - g_j} . K) */
for (int64_t j = 0; j < L; ++j) {
const float gsc = rexp(glast - gc[j]);
for (int64_t d = e; d < K; d += V) kc[j * K + d] = gsc * kc[j * K + d];
}
sycl::group_barrier(gw);
const float el = rexp(glast);
for (int64_t d = 0; d < K; ++d) {
float sr = el * St[d * V + e];
for (int64_t j = 0; j < L; ++j) sr += Vp[j * V + e] * kc[j * K + d];
St[d * V + e] = sr;
}
}
if (has_ht)
for (int64_t d = 0; d < K; ++d) hts.st(at4(htv, sn, h, e, d), St[d * V + e]);
});
});
});
}
const int64_t need = K * V + 3 * chunk * V + 3 * chunk * K + chunk * chunk + 3 * chunk + 2 * V;
float* ws = workspace(*qu, (size_t)(N * H * need));
if (!ws) return RAD_E_DEVICE;
@@ -725,7 +878,7 @@ inline void recurrent(const RecIO& r, int64_t n, int64_t h, float* wsp) {
/* Builds RecIO from libref's operand list (q, k, v, a, b, A_log, dt_bias, state, state_idx,
* num_accepted, cu, z, norm_w, o[, o_q, o_scale]). */
int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_seq) {
int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_seq, int64_t* tk_out) {
if (nt < 14) return RAD_E_INVAL;
const RadTensor *q = &t[0], *k = &t[1], *v = &t[2], *av = &t[3], *bvt = &t[4];
const RadTensor *state = &t[7], *o = &t[13];
@@ -754,6 +907,7 @@ int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_se
if (Hv <= 0) return RAD_E_SHAPE;
const int64_t Tk = rad_tensor_numel(v) / (Hv * V);
if (Tk <= 0) return RAD_E_SHAPE;
*tk_out = Tk;
const int64_t Hg = rad_tensor_numel(q) / (Tk * K);
if (Hg <= 0 || Hv % Hg != 0) return RAD_E_SHAPE;
r->Hv = Hv;
@@ -795,7 +949,229 @@ int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_se
return RAD_OK;
}
int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq) {
/* The same step spread over value columns. Kernel A: a 16-lane work-group a (sequence, v head,
* block of 16 columns e), its 128 x 16 slice of the state in local memory (read and written a
* state row at a time with the lanes along d, coalesced): every chain libref forms runs over d, so
* each column is libref's arithmetic, and the un-normalised output column goes to a scratch row.
* Kernel B: a work-group a (token, v head) for the gated RMS norm -- its sum over e serial in e
* order on one work-item, as libref's -- and the folded fp8 group. 384 work-groups a 27B layer
* instead of 48. */
constexpr int64_t kCb = 16; /* columns a work-group of kernel A */
/* Whether sequence n is stepped at all -- the early returns of libref's loop, which leave its o
* rows untouched. */
inline bool seq_active(const RecIO& r, int64_t n) {
const int64_t q_len = r.cu.at(n + 1) - r.cu.at(n);
if (q_len <= 0 && !r.anchor) return false;
const int64_t na = r.nacc.on ? r.nacc.at(n) : 1;
if (r.anchor) {
if (q_len > r.al.tmax || na > r.al.tmax) return false;
for (int c = 0; c < 6; ++c)
if (r.sidx.li(at2(r.si, n, c)) < 0) return false;
return true;
}
const int64_t rd = na > 0 ? na - 1 : 0;
const int64_t slot_in = r.has_si ? r.sidx.li(at2(r.si, n, rd < r.sw ? rd : r.sw - 1)) : n;
return slot_in >= 0;
}
constexpr int64_t kWgMaxV = 256;
constexpr int64_t kWgMaxK = 256;
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;
float* orow = workspace(qu, (size_t)(Tk * Hv * V + 64), 3);
if (!orow) return RAD_E_DEVICE;
/* 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;
for (int phase = r.anchor ? 0 : 1; phase <= 1 && rc >= 0; ++phase)
rc = submit([&] {
qu.submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> St(sycl::range<1>((size_t)(K * kCb)), hd);
sycl::local_accessor<float, 1> vec(sycl::range<1>((size_t)(4 * K)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(n_seq * Hv * nblk) * kCb, (size_t)kCb},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
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 int64_t l = (int64_t)it.get_local_id(0);
const int64_t e0 = blk * kCb, e = e0 + l;
auto grp = it.get_group();
float* qn = &vec[0];
float* kn = qn + K;
float* qraw = kn + K;
float* kraw = qraw + K;
const int64_t bos = r.cu.at(n), q_len = r.cu.at(n + 1) - bos;
if (q_len <= 0 && !r.anchor) return; /* uniform */
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]; /* the anchor phase 0 published */
}
} 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]);
auto load_state = [&](int64_t slot) {
for (int64_t ee = 0; ee < kCb; ++ee)
for (int64_t d = l; d < K; d += kCb) St[d * kCb + ee] = r.state.ld(at4(r.stv, slot, h, e0 + ee, d));
};
auto store_state = [&](int64_t slot) {
for (int64_t ee = 0; ee < kCb; ++ee)
for (int64_t d = l; d < K; d += kCb) r.state.st(at4(r.stv, slot, h, e0 + ee, d), St[d * kCb + ee]);
};
load_state(slot_in);
sycl::group_barrier(grp);
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;
auto update_col = [&](float g, float bt, float ve) {
const float dec = rexp(g);
float sk2 = 0.0f;
for (int64_t d = 0; d < K; ++d) {
St[d * kCb + l] *= dec;
sk2 += St[d * kCb + l] * kn[d];
}
const float uu = bt * (ve - sk2);
for (int64_t d = 0; d < K; ++d) St[d * kCb + l] += uu * kn[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) {
sycl::group_barrier(grp);
for (int64_t d = l; d < K; d += kCb) kn[d] = r.state.ld(f + gi * K + d);
sycl::group_barrier(grp);
if (e >= 32 * gi && e < sycl::min(V, 32 * gi + 32))
update_col(r.state.ld(f + al.gb + 2 * gi), r.state.ld(f + al.gb + 2 * gi + 1),
r.state.ld(f + al.vb + e));
}
}
sycl::group_barrier(grp);
store_state(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 (int64_t t = 0; t < q_len; ++t) {
const int64_t tok = bos + t;
const float gv = -rexp(alog) * softplus(r.av.ld(at2(r.a2, tok, h)) + dtb, r.spthr);
const float bt = sigm(r.bv.ld(at2(r.b2, tok, h)));
sycl::group_barrier(grp);
for (int64_t d = l; d < K; d += kCb) {
qraw[d] = r.q.ld(at3(r.qv, tok, hk, d));
kraw[d] = r.k.ld(at3(r.kv, tok, hk, d));
}
sycl::group_barrier(grp);
float sq = 0.0f, sk = 0.0f; /* every work-item, the same chain */
for (int64_t d = 0; d < K; ++d) {
sq += qraw[d] * qraw[d];
sk += kraw[d] * kraw[d];
}
const float qs = 1.0f / sycl::sqrt(sq + r.l2eps) * r.scale;
const float ks = 1.0f / sycl::sqrt(sk + r.l2eps);
for (int64_t d = l; d < K; d += kCb) {
qn[d] = qraw[d] * qs;
kn[d] = kraw[d] * ks;
}
sycl::group_barrier(grp);
update_col(gv, bt, r.v.ld(at3(r.vv, tok, h, e)));
float oe = 0.0f;
for (int64_t d = 0; d < K; ++d) oe += St[d * kCb + l] * qn[d];
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 (int64_t d = l; d < K; d += kCb) r.state.st(f + gi * K + d, kn[d]);
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) { /* uniform */
sycl::group_barrier(grp);
store_state(so);
}
}
});
});
});
if (rc < 0) return rc;
/* kernel B: the gated RMS norm and the fp8 group, a work-group a (token, head) */
return submit([&] {
qu.submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> ob(sycl::range<1>((size_t)V), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(Tk * Hv) * (size_t)V, (size_t)V}, [=](sycl::nd_item<1> it) {
const int64_t th = (int64_t)it.get_group(0);
const int64_t tok = th / Hv, h = th % Hv;
const int64_t e = (int64_t)it.get_local_id(0);
auto grp = it.get_group();
/* the token's sequence, and whether kernel A stepped it */
int64_t n = 0;
while (n + 1 < n_seq && r.cu.at(n + 1) <= tok) ++n;
if (!seq_active(r, n)) return; /* uniform: libref leaves these rows as they are */
const float oe = orow[th * V + e];
ob[e] = oe;
sycl::group_barrier(grp);
float sc = 0.0f;
if (e == 0) {
float sq2 = 0.0f;
for (int64_t i = 0; i < V; ++i) sq2 += ob[i] * ob[i];
sc = 1.0f / sycl::sqrt(sq2 / (float)V + r.neps);
}
sc = sycl::group_broadcast(grp, sc, 0);
float x = oe * 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) {
const float stored = r.o.ld(at3(r.ov, tok, h, e));
const float amax = sycl::reduce_over_group(grp, sycl::fabs(stored), sycl::maximum<float>());
const float qsc = amax > 0.0f ? amax * (1.0f / 448.0f) : 1.0f;
const float inv = 1.0f / qsc;
float c = stored * 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 (e == 0) r.osc.st(tok * r.osc_rs + h * r.osc_cs, qsc);
}
});
});
});
}
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,
* which the engine's decode always does; otherwise the serial form below. */
if (r.V % kCb == 0 && r.V <= kWgMaxV && r.K <= kWgMaxK && Tk > 0) return launch_recurrent_wg(qu, r, n_seq, Tk);
const int64_t need = r.V * r.K + 2 * r.K + 3 * r.V;
const int64_t work = n_seq * r.Hv;
float* ws = workspace(qu, (size_t)(work * need));
@@ -816,11 +1192,11 @@ int launch_recurrent(sycl::queue& qu, const RecIO& r, int64_t n_seq) {
int gdn_recurrent_update(const RadArgs* a, RadStream st) {
if (!a) return RAD_E_INVAL;
RecIO r{};
int64_t n_seq = 0;
if (int rc = rec_io(a, a->t, a->n_t, &r, &n_seq); rc < 0) return rc;
int64_t n_seq = 0, Tk = 0;
if (int rc = rec_io(a, a->t, a->n_t, &r, &n_seq, &Tk); rc < 0) return rc;
sycl::queue* qu = queue_of(st);
if (!qu) return RAD_E_DEVICE;
return launch_recurrent(*qu, r, n_seq);
return launch_recurrent(*qu, r, n_seq, Tk);
}
/* ------------------------------------------------------------------ gdn_gated_rmsnorm */
@@ -899,11 +1275,12 @@ int gdn_conv_recurrent_update(const RadArgs* a, RadStream st) {
if (!qu) return RAD_E_DEVICE;
const int64_t esz = (rad_dtype_bits(x->dtype) + 7) / 8;
const int64_t nq = T * Hg * K, nv = T * Hv * V;
/* The staging rides the workspace's tail-free head: q, k, v back to back, 16-byte aligned. */
/* The staging: q, k, v back to back, 16-byte aligned, in workspace slot 1 (the recurrence may
* take slot 0). */
auto up16 = [](int64_t b) { return (b + 15) / 16 * 16; };
const int64_t bq = up16(nq * esz), bv = up16(nv * esz);
char* stage = (char*)sycl::malloc_device(size_t(2 * bq + bv), *qu);
if (!stage) return refuse("gdn_conv_recurrent_update: no staging memory");
char* stage = (char*)workspace(*qu, (size_t)((2 * bq + bv) / 4 + 4), 1);
if (!stage) return RAD_E_DEVICE;
auto mk = [&](char* d, int64_t h, int64_t w) {
RadTensor t{};
t.data = d; t.dtype = x->dtype; t.rank = 3;
@@ -931,17 +1308,10 @@ int gdn_conv_recurrent_update(const RadArgs* a, RadStream st) {
a->t[11], a->t[12], a->t[13], a->t[14], a->t[15],
a->n_t > 16 ? a->t[16] : RadTensor{}, a->n_t > 17 ? a->t[17] : RadTensor{} };
RecIO r{};
int64_t n_seq = 0;
rc = rec_io(a, rt, 16, &r, &n_seq);
if (rc == RAD_OK) rc = launch_recurrent(*qu, r, n_seq);
int64_t n_seq = 0, Tk = 0;
rc = rec_io(a, rt, 16, &r, &n_seq, &Tk);
if (rc == RAD_OK) rc = launch_recurrent(*qu, r, n_seq, Tk);
}
/* The staging is freed once the queue has passed the two launches. */
try {
qu->wait();
} catch (const sycl::exception& e) {
rc = refuse("SYCL: %s", e.what());
}
sycl::free(stage, *qu);
return rc;
}