libxe2: gdn_chunk_scan on DPAS -- 1100 -> 60 ms at a 2234-token prefill, KL unchanged

The exact scan runs libref's chains one work-item each: ~12.8 GFLOP of a 2234-token prefill of
Qwen3.8-27B took 1.1 s. xe2_gdn_esimd.cpp is the chunked delta rule in flash-linear-attention's
form, in two ESIMD launches:

  prep  every (sequence, chunk, v head) at once: U = A beta V (f32), W = -A beta e^g K,
        Q' = scale e^g Q, R = scale e^{g_i - g_j} Q K^T (j <= i), KT = (e^{g_last - g} K)^T (bf16)
  scan  a work-group a (sequence, v head, 32 value columns), the f32 state slice in registers:
        V' = U + W S, O = Q' S + R V', S = e^{g_last} S + KT V', S and V' passed between the
        threads through local memory as DPAS B tiles

bf16 operands into f32, and V' regrouped (A beta V - A beta e^g K S), so it is a fast row held to
rad-kbench's gdn_chunk_scan tolerance (D13); XE2_EXACT=1 (or XE2_GDN_SCAN1=1) keeps the exact
kernel, as does any layout its 2D loads cannot take.

Checks: probe gdnp.cpp against a host copy of libref (partial chunks, 3 sequences, grouped k
heads, h0/ht): o rel_l2 3.2-3.8e-3, ht 0.5-3.0e-3 (tol 2e-2). rad-kbench fixture 30/30, worst
4.8e-3. KL against an XE2_EXACT=1 reference over the 12-document corpus (2002 positions, 2048-token
batches): mean 0.002376, top-1 96.80%, PPL x0.9991 -- with the exact scan 0.002380, 96.65%,
x0.9982. ctest -L sycl 3/3.

An ESIMD note: Lin::off indexes its shape and stride arrays at run time, which puts a private copy
of the kernel argument in memory, and a kernel that does not spill gets no scratch for it -- the
card faulted at stray addresses (device lost). cu goes in as base and stride.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-28 09:56:47 +01:00
1 parent 6969fcf8cb
commit 8e8b4352a1
2 files changed
+493

No files matched your search

+22
View File
@@ -136,6 +136,13 @@ size_t groups_for(int64_t n, size_t wg) { return (size_t)((n + (int64_t)wg - 1)
} // namespace
/* xe2_gdn_esimd.cpp */
int gdn_chunk_scan_dpas(sycl::queue& q, const uint16_t* qp, int64_t qs0, int64_t qs1, const uint16_t* kp, int64_t ks0,
int64_t ks1, const uint16_t* vp, int64_t vs0, int64_t vs1, const uint16_t* Ap, int64_t As0,
int64_t As1, const float* g, int64_t gs0, int64_t gs1, const float* beta, int64_t bs0,
int64_t bs1, uint16_t* op, int64_t os0, int64_t os1, const float* h0, const int64_t* h0s,
float* ht, const int64_t* hts, const RadTensor* cu, const char* si, uint32_t sidt, int64_t sw,
int64_t T, int64_t N, int64_t H, int64_t Hg, float scale);
/* ------------------------------------------------------------------ gdn_conv_prep */
int gdn_conv_prep(const RadArgs* a, RadStream st) {
@@ -696,6 +703,21 @@ 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;
/* The DPAS form (xe2_gdn_esimd.cpp) for the engine's operands: tolerance-held, so not under
* XE2_EXACT=1; it refuses a layout its 2D loads cannot take, and the exact kernels run. */
if (K == 128 && V == 128 && chunk == 64 && !exact_only() && !std::getenv("XE2_GDN_SCAN1") && qs.dt == RAD_BF16 &&
ks.dt == RAD_BF16 && vs.dt == RAD_BF16 && As.dt == RAD_BF16 && os.dt == RAD_BF16 && gs.dt == RAD_F32 &&
bs.dt == RAD_F32 && (!has_h0 || h0s.dt == RAD_F32) && (!has_ht || hts.dt == RAD_F32) && qv.s2 == 1 &&
kv.s2 == 1 && vv.s2 == 1 && Av.s2 == 1 && ov.s2 == 1) {
const int64_t h0st[4] = { h0v.s0, h0v.s1, h0v.s2, h0v.s3 }, htst[4] = { htv.s0, htv.s1, htv.s2, htv.s3 };
const int rc = gdn_chunk_scan_dpas(*qu, (const uint16_t*)qs.p, qv.s0, qv.s1, (const uint16_t*)ks.p, kv.s0, kv.s1,
(const uint16_t*)vs.p, vv.s0, vv.s1, (const uint16_t*)As.p, Av.s0, Av.s1,
(const float*)gs.p, gv.s0, gv.s1, (const float*)bs.p, bv.s0, bv.s1,
(uint16_t*)os.p, ov.s0, ov.s1, has_h0 ? (const float*)h0s.p : nullptr, h0st,
has_ht ? (float*)hts.p : nullptr, htst, cu, has_si ? sis.p : nullptr, sis.dt,
sw, T, N, H, Hg, scale);
if (rc != RAD_E_UNSUPPORTED) return rc;
}
if (K == 128 && V % 32 == 0 && chunk == 64 && !std::getenv("XE2_GDN_SCAN1")) {
/* A work-group of 1024 a (sequence, v head, block of 32 value columns), the chunk's k, q,
* the block's slices of v, S, W, V' and the sA rows R in local memory (~124 KB). Each phase
+471
View File
@@ -0,0 +1,471 @@
/* xe2_gdn_esimd.cpp -- gdn_chunk_scan on DPAS, the prefill form.
*
* The exact kernel (xe2_gdn.cpp) runs libref's chains one work-item each: ~12.8 GFLOP of a
* 2234-token prefill of Qwen3.8-27B took 1.1 s. Here the chunked delta rule in the form
* flash-linear-attention uses, in two launches:
*
* P every (sequence, chunk, v head) at once, a work-group of 8 threads, thread t rows 8t..8t+7:
* U = A diag(beta) V f32 [64][128]
* W = -A diag(beta e^g) K bf16 [64][128]
* Q' = scale e^{g_i} Q bf16 [64][128]
* R = scale e^{g_i - g_j} Q K^T, j <= i bf16 [64][64]
* KT = (e^{g_last - g_j} K)^T bf16 [128][64]
* (A from gdn_kkt_solve; rows and columns past the chunk's length are zero)
* S a work-group a (sequence, v head, 32 value columns): the state slice S[d][e] (128 x 32 f32)
* in registers, thread t its rows d 16t..16t+15; chunk by chunk
* V' = U + W S (S as bf16)
* O = Q' S + R V' (V' as bf16)
* S = e^{g_last} S + KT V'
* S and V' pass between the threads through local memory as DPAS B tiles (VNNI).
*
* libref's V' = A (beta (V - e^g K S)) is regrouped as A beta V - A beta e^g K S, and the products
* take bf16 operands into f32: not libref's order or precision, so this is a fast row held to
* rad-kbench's gdn_chunk_scan tolerance and the KL bound (D13); XE2_EXACT=1 keeps the exact kernel.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include <sycl/ext/intel/esimd.hpp>
namespace xe2 {
namespace {
namespace esimd = sycl::ext::intel::esimd;
namespace xmx = sycl::ext::intel::esimd::xmx;
namespace intelex = sycl::ext::intel::experimental;
constexpr int CK = 64, KK = 128, VV = 128, EB = 32;
/* P's outputs for one (chunk slot, v head), in bytes */
constexpr int64_t kOffW = 0, kOffU = kOffW + CK * KK * 2, kOffQ = kOffU + CK * VV * 4;
constexpr int64_t kOffR = kOffQ + CK * KK * 2, kOffKT = kOffR + CK * CK * 2, kBlk = kOffKT + KK * CK * 2;
struct ScanArgs {
const uint16_t *q, *k, *v, *A;
int64_t qs0, qs1, ks0, ks1, vs0, vs1, As0, As1;
const float *g, *beta;
int64_t gs0, gs1, bs0, bs1;
uint16_t* o;
int64_t os0, os1;
const float* h0;
float* ht;
int64_t h0s0, h0s1, h0s2, h0s3, hts0, hts1, hts2, hts3;
/* cu and state_idx as base and element stride: no array in these arguments is indexed at run
* time (Lin::off's would be), since that puts the copy in private memory and an ESIMD kernel
* that does not spill gets no scratch for it -- a page fault (probe gdnp.cpp) */
const char* cu;
uint32_t cudt;
int64_t cus;
const char* si;
uint32_t sidt;
int64_t sw;
int64_t T, N, H, Hg;
float scale;
int64_t cu_at(int64_t i) const { return ld_index(cudt, cu, i * cus); }
};
using bf16t = xmx::dpas_argument_type;
inline esimd::simd<float, 128> mma(esimd::simd<float, 128> c, esimd::simd<uint32_t, 128> b,
esimd::simd<uint16_t, 128> a) SYCL_ESIMD_FUNCTION {
return xmx::dpas<8, 8, float, float, uint32_t, uint16_t, bf16t::bf16, bf16t::bf16>(c, b, a);
}
inline esimd::simd<float, 128> mma(esimd::simd<float, 128> c, esimd::simd<uint16_t, 256> b,
esimd::simd<uint16_t, 128> a) SYCL_ESIMD_FUNCTION {
return xmx::dpas<8, 8, float, float, uint16_t, uint16_t, bf16t::bf16, bf16t::bf16>(c, b, a);
}
template <int N>
inline esimd::simd<uint16_t, N> bf16_of(esimd::simd<float, N> v) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint32_t, N> u = v.template bit_cast_view<uint32_t>();
return simd<uint16_t, N>((u + 0x7FFFu + ((u >> 16) & 1u)) >> 16);
}
template <int N>
inline esimd::simd<float, N> f32_of(esimd::simd<uint16_t, N> h) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint32_t, N> u = simd<uint32_t, N>(h) << 16;
return u.template bit_cast_view<float>();
}
/* A C tile's 8 rows as half a DPAS B tile: dword [p][n] = rows (2p, 2p + 1) of column n, bf16. */
inline esimd::simd<uint32_t, 64> vnni_half(esimd::simd<float, 128> c) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint16_t, 128> b = bf16_of<128>(c);
simd<uint16_t, 128> o;
#pragma unroll
for (int p = 0; p < 4; ++p) {
o.template select<16, 2>(32 * p) = b.template select<16, 1>(32 * p);
o.template select<16, 2>(32 * p + 1) = b.template select<16, 1>(32 * p + 16);
}
return o.template bit_cast_view<uint32_t>();
}
/* An 8 x 16 tile's per-row factor (8 values) spread over its 128 elements. */
inline esimd::simd<float, 128> rows8(esimd::simd<float, 8> f) SYCL_ESIMD_FUNCTION {
esimd::simd<float, 128> r;
#pragma unroll
for (int i = 0; i < 8; ++i) r.template select<16, 1>(16 * i) = (float)f[i];
return r;
}
/* ... and a per-column factor (16 values). */
inline esimd::simd<float, 128> cols16(esimd::simd<float, 16> f) SYCL_ESIMD_FUNCTION {
esimd::simd<float, 128> r;
#pragma unroll
for (int i = 0; i < 8; ++i) r.template select<16, 1>(16 * i) = f;
return r;
}
int launch_prep(sycl::queue& q, const ScanArgs& a, char* ws, int64_t C) {
syclex::properties props{ intelex::grf_size<256> };
return submit([&] {
q.parallel_for(sycl::nd_range<1>{ (size_t)(a.N * C * a.H) * 8, 8 }, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
const int t = (int)it.get_local_id(0);
const int64_t wg = (int64_t)it.get_group(0);
const int64_t h = wg % a.H, c = (wg / a.H) % C, n = wg / (a.H * C);
const int64_t bos = a.cu_at(n), slen = a.cu_at(n + 1) - bos;
const int64_t c0 = c * CK;
if (c0 >= slen) return;
const int L = (int)(slen - c0 < CK ? slen - c0 : CK);
const int64_t tok0 = bos + c0;
const int64_t slot = tok0 / CK + n;
char* blk = ws + (slot * a.H + h) * kBlk;
uint16_t* Wp = (uint16_t*)(blk + kOffW);
float* Up = (float*)(blk + kOffU);
uint16_t* Qp = (uint16_t*)(blk + kOffQ);
uint16_t* Rp = (uint16_t*)(blk + kOffR);
uint16_t* KTp = (uint16_t*)(blk + kOffKT);
const int64_t hk = h / (a.H / a.Hg);
const uint16_t* qh = a.q + hk * a.qs1;
const uint16_t* kh = a.k + hk * a.ks1;
const uint16_t* vh = a.v + h * a.vs1;
const uint16_t* Ah = a.A + h * a.As1;
const unsigned TH = (unsigned)(a.T - 1);
const unsigned qp = (unsigned)(a.qs0 * 2 - 1), kp = (unsigned)(a.ks0 * 2 - 1);
const unsigned vp = (unsigned)(a.vs0 * 2 - 1), Ap = (unsigned)(a.As0 * 2 - 1);
const int r0 = 8 * t;
const int njt = t / 2 + 1; /* the j steps of 16 that reach the thread's rows (j <= i) */
const simd<int32_t, 16> lane(0, 1);
const simd<int32_t, 128> e128(0, 1);
const simd<int32_t, 128> row8 = e128 >> 4, col16 = e128 & 15;
/* g and beta at the chunk's positions: past L, g_last and 0 */
const float* gp = a.g + tok0 * a.gs0 + h * a.gs1;
const float* bp = a.beta + tok0 * a.bs0 + h * a.bs1;
const float glast = gp[(int64_t)(L - 1) * a.gs0];
simd<float, 64> gj, bj;
#pragma unroll
for (int s = 0; s < 4; ++s) {
const simd<int32_t, 16> jj = lane + 16 * s;
const simd_mask<16> ok = jj < L, nok = jj >= L;
simd<float, 16> gv = gather<float, 16>(gp, simd<uint32_t, 16>(jj) * (uint32_t)(a.gs0 * 4), ok);
simd<float, 16> bv = gather<float, 16>(bp, simd<uint32_t, 16>(jj) * (uint32_t)(a.bs0 * 4), ok);
gv.merge(simd<float, 16>(glast), nok);
bv.merge(simd<float, 16>(0.0f), nok);
gj.template select<16, 1>(16 * s) = gv;
bj.template select<16, 1>(16 * s) = bv;
}
const simd<float, 64> egj = exp(gj);
simd<float, 64> decl = exp(simd<float, 64>(glast) - gj); /* e^{g_last - g_j}, <= 1 */
/* the thread's rows' g, and e^{g_i} scale for Q' (rows past L zero) */
simd<float, 8> gi;
{
const simd<int32_t, 8> ii = simd<int32_t, 8>(0, 1) + r0;
const simd_mask<8> ok = ii < L, nok = ii >= L;
gi = gather<float, 8>(gp, simd<uint32_t, 8>(ii) * (uint32_t)(a.gs0 * 4), ok);
gi.merge(simd<float, 8>(glast), nok);
}
simd<float, 8> qf = exp(gi) * a.scale;
qf.merge(simd<float, 8>(0.0f), simd<int32_t, 8>(0, 1) + r0 >= L);
const simd<float, 128> qfb = rows8(qf), gib = rows8(gi);
const simd<int32_t, 128> irow = row8 + r0;
/* Q' and R = Q K^T */
simd<float, 128> Rc[4];
#pragma unroll
for (int jt = 0; jt < 4; ++jt) Rc[jt] = 0.0f;
#pragma unroll 1
for (int ks = 0; ks < KK / 16; ++ks) {
simd<uint16_t, 128> qa = load_2d<uint16_t, 16, 8, 1, false>(qh, 255u, TH, qp, ks * 16, (int)(tok0 + r0));
simd<float, 128> qs = f32_of<128>(qa) * qfb;
qs.merge(simd<float, 128>(0.0f), irow >= L);
store_2d<uint16_t, 16, 8>(Qp, 255u, 63u, 255u, ks * 16, r0, bf16_of<128>(qs));
#pragma unroll
for (int jt = 0; jt < 4; ++jt) {
if (jt < njt) {
simd<uint32_t, 128> kb = load_2d<uint32_t, 8, 16, 1, true>((const uint32_t*)kh, 255u, TH, kp, ks * 8,
(int)(tok0 + 16 * jt));
Rc[jt] = mma(Rc[jt], kb, qa);
}
}
}
#pragma unroll
for (int jt = 0; jt < 4; ++jt) {
if (jt < njt) {
const simd<int32_t, 128> jcol = col16 + 16 * jt;
simd<float, 128> dg = gib - cols16(gj.template select<16, 1>(16 * jt));
dg.merge(simd<float, 128>(0.0f), jcol > irow); /* hidden: keep exp finite */
simd<float, 128> r = Rc[jt] * exp(dg) * a.scale;
r.merge(simd<float, 128>(0.0f), (jcol > irow) | (irow >= L));
store_2d<uint16_t, 16, 8>(Rp, 127u, 63u, 127u, 16 * jt, r0, bf16_of<128>(r));
}
}
/* W = -A diag(beta e^g) K and U = A diag(beta) V, j steps of 16 up to the thread's rows; four
* passes of four column tiles (eight live C tiles spilled at 256 registers) */
simd<float, 64> cw = bj * egj;
#pragma unroll
for (int pass = 0; pass < 4; ++pass) {
const bool isw = pass < 2;
const int d0 = (pass & 1) * 4;
simd<float, 128> Cc[4];
#pragma unroll
for (int dt = 0; dt < 4; ++dt) Cc[dt] = 0.0f;
#pragma unroll 1
for (int s = 0; s < njt; ++s) {
simd<uint16_t, 128> a16 = load_2d<uint16_t, 16, 8, 1, false>(Ah, 127u, TH, Ap, s * 16, (int)(tok0 + r0));
simd<float, 128> af = f32_of<128>(a16);
af.merge(simd<float, 128>(0.0f), (col16 + 16 * s > irow) | (irow >= L));
simd<float, 128> sc = isw ? cols16(cw.template select<16, 1>(16 * s)) * -1.0f
: cols16(bj.template select<16, 1>(16 * s));
const simd<uint16_t, 128> aa = bf16_of<128>(af * sc);
#pragma unroll
for (int dt = 0; dt < 4; ++dt) {
simd<uint16_t, 256> b = isw
? load_2d<uint16_t, 16, 16, 1, false, true>(kh, 255u, TH, kp, (d0 + dt) * 16, (int)(tok0 + 16 * s))
: load_2d<uint16_t, 16, 16, 1, false, true>(vh, 255u, TH, vp, (d0 + dt) * 16, (int)(tok0 + 16 * s));
if (L < CK) { /* rows past the chunk: another sequence's, or past T */
const simd<int32_t, 256> e256(0, 1);
b.merge(simd<uint16_t, 256>(0), (((e256 >> 5) << 1) | (e256 & 1)) + 16 * s >= L);
}
Cc[dt] = mma(Cc[dt], b, aa);
}
}
#pragma unroll
for (int dt = 0; dt < 4; ++dt) {
if (isw) store_2d<uint16_t, 16, 8>(Wp, 255u, 63u, 255u, (d0 + dt) * 16, r0, bf16_of<128>(Cc[dt]));
else store_2d<float, 16, 8>(Up, 511u, 63u, 511u, (d0 + dt) * 16, r0, Cc[dt]);
}
}
/* KT: the thread's rows d 16t..16t+15, K^T by a transposed load, the decay a column */
decl.merge(simd<float, 64>(0.0f), simd<int32_t, 64>(0, 1) >= L);
#pragma unroll 1
for (int s = 0; s < 4; ++s) {
simd<uint32_t, 128> kt = load_2d<uint32_t, 8, 16, 1, true>((const uint32_t*)kh, 255u, TH, kp, 8 * t,
(int)(tok0 + 16 * s));
simd<uint16_t, 256> k16 = kt.template bit_cast_view<uint16_t>();
const simd<float, 16> dc = decl.template select<16, 1>(16 * s);
const simd_mask<16> past = lane + 16 * s >= L;
simd<uint16_t, 256> tile; /* [16 rows d][16 columns j] */
#pragma unroll
for (int p = 0; p < 8; ++p) {
simd<float, 16> lo = f32_of<16>(k16.template select<16, 2>(32 * p)) * dc;
simd<float, 16> hi = f32_of<16>(k16.template select<16, 2>(32 * p + 1)) * dc;
lo.merge(simd<float, 16>(0.0f), past);
hi.merge(simd<float, 16>(0.0f), past);
tile.template select<16, 1>(32 * p) = bf16_of<16>(lo);
tile.template select<16, 1>(32 * p + 16) = bf16_of<16>(hi);
}
store_2d<uint16_t, 16, 8>(KTp, 127u, 127u, 127u, 16 * s, 16 * t,
simd<uint16_t, 128>(tile.template select<128, 1>(0)));
store_2d<uint16_t, 16, 8>(KTp, 127u, 127u, 127u, 16 * s, 16 * t + 8,
simd<uint16_t, 128>(tile.template select<128, 1>(128)));
}
});
});
}
/* The byte offsets in h0 / ht ([slot][head][e][d]) of a state tile's row r: d 16t + 8m + r, e from
* e0 + 16ne. */
inline esimd::simd<uint32_t, 16> state_off(int64_t s2, int64_t s3, int t, int e0, int m, int ne, int r) SYCL_ESIMD_FUNCTION {
using namespace esimd;
const int64_t d = 16 * t + 8 * m + r;
return simd<uint32_t, 16>(simd<int32_t, 16>(0, 1) + (e0 + 16 * ne)) * (uint32_t)(s2 * 4) + (uint32_t)(d * s3 * 4);
}
/* The thread's state rows as the B tiles of the k step 16t..16t+15, one a column tile. */
inline void put_state(esimd::simd<float, 128> (&St)[2][2], uint32_t off) SYCL_ESIMD_FUNCTION {
using namespace esimd;
#pragma unroll
for (int ne = 0; ne < 2; ++ne) {
slm_block_store<uint32_t, 64>(off + ne * 512, vnni_half(St[0][ne]));
slm_block_store<uint32_t, 64>(off + ne * 512 + 256, vnni_half(St[1][ne]));
}
}
int launch_scan(sycl::queue& q, const ScanArgs& a, const char* ws) {
syclex::properties props{ intelex::grf_size<256> };
constexpr int NB = VV / EB;
constexpr uint32_t kSb = 0, kVb = (KK / 16) * 2 * 512, kSlm = kVb + (CK / 16) * 2 * 512;
return submit([&] {
q.parallel_for(sycl::nd_range<1>{ (size_t)(a.N * a.H * NB) * 8, 8 }, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
slm_init<kSlm>();
const int t = (int)it.get_local_id(0);
const int64_t wg = (int64_t)it.get_group(0);
const int64_t eb = wg % NB, h = (wg / NB) % a.H, n = wg / (NB * a.H);
const int64_t bos = a.cu_at(n), slen = a.cu_at(n + 1) - bos;
const int64_t sn = a.si ? ld_index(a.sidt, a.si, n * a.sw) : n;
if (sn < 0) return; /* uniform over the work-group */
const int e0 = (int)eb * EB;
const int r0 = 8 * t;
const simd<int32_t, 16> lane(0, 1);
/* the state slice, St[m][ne]: rows d 16t + 8m .. +7, columns e e0 + 16ne .. +15 */
simd<float, 128> St[2][2];
/* its elements' offsets in h0 / ht, [slot][head][e][d] */
#pragma unroll
for (int m = 0; m < 2; ++m)
#pragma unroll
for (int ne = 0; ne < 2; ++ne) {
St[m][ne] = 0.0f;
if (a.h0) {
const float* hb = a.h0 + sn * a.h0s0 + h * a.h0s1;
#pragma unroll
for (int r = 0; r < 8; ++r)
St[m][ne].template select<16, 1>(16 * r) = gather<float, 16>(hb, state_off(a.h0s2, a.h0s3, t, e0, m, ne, r));
}
}
put_state(St, kSb + (uint32_t)(t * 2 * 512));
barrier();
const unsigned op = (unsigned)(a.os0 * 2 - 1);
uint16_t* oh = a.o + h * a.os1;
for (int64_t c0 = 0; c0 < slen; c0 += CK) {
const int L = (int)(slen - c0 < CK ? slen - c0 : CK);
const int64_t tok0 = bos + c0;
const int64_t slot = tok0 / CK + n;
const char* blk = ws + (slot * a.H + h) * kBlk;
const uint16_t* Wp = (const uint16_t*)(blk + kOffW);
const float* Up = (const float*)(blk + kOffU);
const uint16_t* Qp = (const uint16_t*)(blk + kOffQ);
const uint16_t* Rp = (const uint16_t*)(blk + kOffR);
const uint16_t* KTp = (const uint16_t*)(blk + kOffKT);
/* V' = U + W S and Q' S, the thread's rows 8t..8t+7 */
simd<float, 128> Vc[2], Oc[2];
#pragma unroll
for (int ne = 0; ne < 2; ++ne) {
Vc[ne] = load_2d<float, 16, 8, 1, false>(Up, 511u, 63u, 511u, e0 + 16 * ne, r0);
Oc[ne] = 0.0f;
}
#pragma unroll
for (int ks = 0; ks < KK / 16; ++ks) {
simd<uint16_t, 128> aw = load_2d<uint16_t, 16, 8, 1, false>(Wp, 255u, 63u, 255u, ks * 16, r0);
simd<uint16_t, 128> aq = load_2d<uint16_t, 16, 8, 1, false>(Qp, 255u, 63u, 255u, ks * 16, r0);
#pragma unroll
for (int ne = 0; ne < 2; ++ne) {
simd<uint32_t, 128> b = slm_block_load<uint32_t, 128>(kSb + (uint32_t)((ks * 2 + ne) * 512));
Vc[ne] = mma(Vc[ne], b, aw);
Oc[ne] = mma(Oc[ne], b, aq);
}
}
#pragma unroll
for (int ne = 0; ne < 2; ++ne)
slm_block_store<uint32_t, 64>(kVb + (uint32_t)(((t / 2) * 2 + ne) * 512 + (t % 2) * 256), vnni_half(Vc[ne]));
barrier();
/* O += R V' */
#pragma unroll
for (int s = 0; s < CK / 16; ++s) {
if (s > t / 2) continue;
simd<uint16_t, 128> ar = load_2d<uint16_t, 16, 8, 1, false>(Rp, 127u, 63u, 127u, s * 16, r0);
#pragma unroll
for (int ne = 0; ne < 2; ++ne) {
simd<uint32_t, 128> b = slm_block_load<uint32_t, 128>(kVb + (uint32_t)((s * 2 + ne) * 512));
Oc[ne] = mma(Oc[ne], b, ar);
}
}
#pragma unroll
for (int ne = 0; ne < 2; ++ne) {
simd<uint16_t, 128> ob = bf16_of<128>(Oc[ne]);
if (r0 + 8 <= L) {
store_2d<uint16_t, 16, 8>(oh, 255u, (unsigned)(a.T - 1), op, e0 + 16 * ne, (int)(tok0 + r0), ob);
} else {
#pragma unroll
for (int r = 0; r < 8; ++r)
if (r0 + r < L)
block_store<uint16_t, 16>(oh + (tok0 + r0 + r) * a.os0 + e0 + 16 * ne,
simd<uint16_t, 16>(ob.template select<16, 1>(16 * r)));
}
}
/* S = e^{g_last} S + KT V' */
const float gl = a.g[(tok0 + L - 1) * a.gs0 + h * a.gs1];
const simd<float, 16> elv = exp(simd<float, 16>(gl));
const float el = elv[0];
#pragma unroll
for (int m = 0; m < 2; ++m)
#pragma unroll
for (int ne = 0; ne < 2; ++ne) St[m][ne] *= el;
#pragma unroll
for (int s = 0; s < CK / 16; ++s) {
simd<uint32_t, 128> b0 = slm_block_load<uint32_t, 128>(kVb + (uint32_t)((s * 2 + 0) * 512));
simd<uint32_t, 128> b1 = slm_block_load<uint32_t, 128>(kVb + (uint32_t)((s * 2 + 1) * 512));
#pragma unroll
for (int m = 0; m < 2; ++m) {
simd<uint16_t, 128> ak = load_2d<uint16_t, 16, 8, 1, false>(KTp, 127u, 127u, 127u, s * 16, 16 * t + 8 * m);
St[m][0] = mma(St[m][0], b0, ak);
St[m][1] = mma(St[m][1], b1, ak);
}
}
put_state(St, kSb + (uint32_t)(t * 2 * 512));
barrier();
}
if (a.ht) {
float* hb = a.ht + sn * a.hts0 + h * a.hts1;
#pragma unroll
for (int m = 0; m < 2; ++m)
#pragma unroll
for (int ne = 0; ne < 2; ++ne)
#pragma unroll
for (int r = 0; r < 8; ++r)
scatter<float, 16>(hb, state_off(a.hts2, a.hts3, t, e0, m, ne, r),
simd<float, 16>(St[m][ne].template select<16, 1>(16 * r)));
}
});
});
}
bool al64(const void* p) { return ((uintptr_t)p & 63u) == 0; }
} // namespace
/* gdn_chunk_scan's DPAS form; RAD_E_UNSUPPORTED when the call is not one it serves (the caller then
* runs the exact kernel). Operands as xe2_gdn.cpp hands them: bf16 q, k ([T, Hg, 128]), v, o
* ([T, H, 128]) and A ([T, H, 64]) with unit last strides, f32 g, beta ([T, H]) and states
* ([slots, H, 128 (e), 128 (d)]). */
int gdn_chunk_scan_dpas(sycl::queue& q, const uint16_t* qp, int64_t qs0, int64_t qs1, const uint16_t* kp, int64_t ks0,
int64_t ks1, const uint16_t* vp, int64_t vs0, int64_t vs1, const uint16_t* Ap, int64_t As0,
int64_t As1, const float* g, int64_t gs0, int64_t gs1, const float* beta, int64_t bs0,
int64_t bs1, uint16_t* op, int64_t os0, int64_t os1, const float* h0, const int64_t* h0s,
float* ht, const int64_t* hts, const RadTensor* cu, const char* si, uint32_t sidt, int64_t sw,
int64_t T, int64_t N, int64_t H, int64_t Hg, float scale) {
/* 2D block loads: 64-byte aligned surfaces, pitches a multiple of 16 bytes */
auto surf = [](const uint16_t* p, int64_t s0, int64_t s1) {
return al64(p) && (s1 * 2) % 64 == 0 && (s0 * 2) % 16 == 0 && s0 * 2 >= 256;
};
if (!surf(qp, qs0, qs1) || !surf(kp, ks0, ks1) || !surf(vp, vs0, vs1) || !surf(op, os0, os1))
return RAD_E_UNSUPPORTED;
if (!al64(Ap) || (As1 * 2) % 64 != 0 || (As0 * 2) % 16 != 0 || As0 * 2 < 128) return RAD_E_UNSUPPORTED;
if (T >= (int64_t)1 << 24 || H % Hg != 0) return RAD_E_UNSUPPORTED;
ScanArgs a{};
a.q = qp; a.k = kp; a.v = vp; a.A = Ap;
a.qs0 = qs0; a.qs1 = qs1; a.ks0 = ks0; a.ks1 = ks1; a.vs0 = vs0; a.vs1 = vs1; a.As0 = As0; a.As1 = As1;
a.g = g; a.beta = beta; a.gs0 = gs0; a.gs1 = gs1; a.bs0 = bs0; a.bs1 = bs1;
a.o = op; a.os0 = os0; a.os1 = os1;
a.h0 = h0; a.ht = ht;
if (h0) { a.h0s0 = h0s[0]; a.h0s1 = h0s[1]; a.h0s2 = h0s[2]; a.h0s3 = h0s[3]; }
if (ht) { a.hts0 = hts[0]; a.hts1 = hts[1]; a.hts2 = hts[2]; a.hts3 = hts[3]; }
const Lin cl = Lin::of(cu);
if (!cl.contig && cl.rank != 1) return RAD_E_UNSUPPORTED;
a.cu = cl.base; a.cudt = cl.dtype; a.cus = cl.contig ? 1 : cl.stride[0];
a.si = si; a.sidt = sidt; a.sw = sw;
a.T = T; a.N = N; a.H = H; a.Hg = Hg; a.scale = scale;
const int64_t C = (T + CK - 1) / CK;
/* chunk slots: a chunk starting at token s of sequence n is slot s / 64 + n (unique: see the
* scan), fewer than C + N + 1 */
const int64_t bytes = (C + N + 1) * H * kBlk;
char* ws = (char*)workspace(q, (size_t)(bytes / 4), 9);
if (!ws) return RAD_E_DEVICE;
if (int rc = launch_prep(q, a, ws, C); rc < 0) return rc;
return launch_scan(q, a, ws);
}
} // namespace xe2