diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 03ddb1ac..c95d67bf 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -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 diff --git a/libxe2/xe2_gdn_esimd.cpp b/libxe2/xe2_gdn_esimd.cpp new file mode 100644 index 00000000..86210bf6 --- /dev/null +++ b/libxe2/xe2_gdn_esimd.cpp @@ -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 + +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 mma(esimd::simd c, esimd::simd b, + esimd::simd 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 mma(esimd::simd c, esimd::simd b, + esimd::simd a) SYCL_ESIMD_FUNCTION { + return xmx::dpas<8, 8, float, float, uint16_t, uint16_t, bf16t::bf16, bf16t::bf16>(c, b, a); +} + +template +inline esimd::simd bf16_of(esimd::simd v) SYCL_ESIMD_FUNCTION { + using namespace esimd; + simd u = v.template bit_cast_view(); + return simd((u + 0x7FFFu + ((u >> 16) & 1u)) >> 16); +} +template +inline esimd::simd f32_of(esimd::simd h) SYCL_ESIMD_FUNCTION { + using namespace esimd; + simd u = simd(h) << 16; + return u.template bit_cast_view(); +} + +/* 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 vnni_half(esimd::simd c) SYCL_ESIMD_FUNCTION { + using namespace esimd; + simd b = bf16_of<128>(c); + simd 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(); +} + +/* An 8 x 16 tile's per-row factor (8 values) spread over its 128 elements. */ +inline esimd::simd rows8(esimd::simd f) SYCL_ESIMD_FUNCTION { + esimd::simd 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 cols16(esimd::simd f) SYCL_ESIMD_FUNCTION { + esimd::simd 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 lane(0, 1); + const simd e128(0, 1); + const simd 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 gj, bj; +#pragma unroll + for (int s = 0; s < 4; ++s) { + const simd jj = lane + 16 * s; + const simd_mask<16> ok = jj < L, nok = jj >= L; + simd gv = gather(gp, simd(jj) * (uint32_t)(a.gs0 * 4), ok); + simd bv = gather(bp, simd(jj) * (uint32_t)(a.bs0 * 4), ok); + gv.merge(simd(glast), nok); + bv.merge(simd(0.0f), nok); + gj.template select<16, 1>(16 * s) = gv; + bj.template select<16, 1>(16 * s) = bv; + } + const simd egj = exp(gj); + simd decl = exp(simd(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 gi; + { + const simd ii = simd(0, 1) + r0; + const simd_mask<8> ok = ii < L, nok = ii >= L; + gi = gather(gp, simd(ii) * (uint32_t)(a.gs0 * 4), ok); + gi.merge(simd(glast), nok); + } + simd qf = exp(gi) * a.scale; + qf.merge(simd(0.0f), simd(0, 1) + r0 >= L); + const simd qfb = rows8(qf), gib = rows8(gi); + const simd irow = row8 + r0; + + /* Q' and R = Q K^T */ + simd 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 qa = load_2d(qh, 255u, TH, qp, ks * 16, (int)(tok0 + r0)); + simd qs = f32_of<128>(qa) * qfb; + qs.merge(simd(0.0f), irow >= L); + store_2d(Qp, 255u, 63u, 255u, ks * 16, r0, bf16_of<128>(qs)); +#pragma unroll + for (int jt = 0; jt < 4; ++jt) { + if (jt < njt) { + simd kb = load_2d((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 jcol = col16 + 16 * jt; + simd dg = gib - cols16(gj.template select<16, 1>(16 * jt)); + dg.merge(simd(0.0f), jcol > irow); /* hidden: keep exp finite */ + simd r = Rc[jt] * exp(dg) * a.scale; + r.merge(simd(0.0f), (jcol > irow) | (irow >= L)); + store_2d(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 cw = bj * egj; +#pragma unroll + for (int pass = 0; pass < 4; ++pass) { + const bool isw = pass < 2; + const int d0 = (pass & 1) * 4; + simd 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 a16 = load_2d(Ah, 127u, TH, Ap, s * 16, (int)(tok0 + r0)); + simd af = f32_of<128>(a16); + af.merge(simd(0.0f), (col16 + 16 * s > irow) | (irow >= L)); + simd sc = isw ? cols16(cw.template select<16, 1>(16 * s)) * -1.0f + : cols16(bj.template select<16, 1>(16 * s)); + const simd aa = bf16_of<128>(af * sc); +#pragma unroll + for (int dt = 0; dt < 4; ++dt) { + simd b = isw + ? load_2d(kh, 255u, TH, kp, (d0 + dt) * 16, (int)(tok0 + 16 * s)) + : load_2d(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 e256(0, 1); + b.merge(simd(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(Wp, 255u, 63u, 255u, (d0 + dt) * 16, r0, bf16_of<128>(Cc[dt])); + else store_2d(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(0.0f), simd(0, 1) >= L); +#pragma unroll 1 + for (int s = 0; s < 4; ++s) { + simd kt = load_2d((const uint32_t*)kh, 255u, TH, kp, 8 * t, + (int)(tok0 + 16 * s)); + simd k16 = kt.template bit_cast_view(); + const simd dc = decl.template select<16, 1>(16 * s); + const simd_mask<16> past = lane + 16 * s >= L; + simd tile; /* [16 rows d][16 columns j] */ +#pragma unroll + for (int p = 0; p < 8; ++p) { + simd lo = f32_of<16>(k16.template select<16, 2>(32 * p)) * dc; + simd hi = f32_of<16>(k16.template select<16, 2>(32 * p + 1)) * dc; + lo.merge(simd(0.0f), past); + hi.merge(simd(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(KTp, 127u, 127u, 127u, 16 * s, 16 * t, + simd(tile.template select<128, 1>(0))); + store_2d(KTp, 127u, 127u, 127u, 16 * s, 16 * t + 8, + simd(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 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(simd(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 (&St)[2][2], uint32_t off) SYCL_ESIMD_FUNCTION { + using namespace esimd; +#pragma unroll + for (int ne = 0; ne < 2; ++ne) { + slm_block_store(off + ne * 512, vnni_half(St[0][ne])); + slm_block_store(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(); + 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 lane(0, 1); + /* the state slice, St[m][ne]: rows d 16t + 8m .. +7, columns e e0 + 16ne .. +15 */ + simd 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(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 Vc[2], Oc[2]; +#pragma unroll + for (int ne = 0; ne < 2; ++ne) { + Vc[ne] = load_2d(Up, 511u, 63u, 511u, e0 + 16 * ne, r0); + Oc[ne] = 0.0f; + } +#pragma unroll + for (int ks = 0; ks < KK / 16; ++ks) { + simd aw = load_2d(Wp, 255u, 63u, 255u, ks * 16, r0); + simd aq = load_2d(Qp, 255u, 63u, 255u, ks * 16, r0); +#pragma unroll + for (int ne = 0; ne < 2; ++ne) { + simd b = slm_block_load(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(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 ar = load_2d(Rp, 127u, 63u, 127u, s * 16, r0); +#pragma unroll + for (int ne = 0; ne < 2; ++ne) { + simd b = slm_block_load(kVb + (uint32_t)((s * 2 + ne) * 512)); + Oc[ne] = mma(Oc[ne], b, ar); + } + } +#pragma unroll + for (int ne = 0; ne < 2; ++ne) { + simd ob = bf16_of<128>(Oc[ne]); + if (r0 + 8 <= L) { + store_2d(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(oh + (tok0 + r0 + r) * a.os0 + e0 + 16 * ne, + simd(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 elv = exp(simd(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 b0 = slm_block_load(kVb + (uint32_t)((s * 2 + 0) * 512)); + simd b1 = slm_block_load(kVb + (uint32_t)((s * 2 + 1) * 512)); +#pragma unroll + for (int m = 0; m < 2; ++m) { + simd ak = load_2d(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(hb, state_off(a.hts2, a.hts3, t, e0, m, ne, r), + simd(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