Archived
- gdn_kkt_solve on DPAS (~0.4 ms at 2048 tokens against 7.7), the int8 head's DPAS forms at 9-16 rows (verify past depth 7), the plain block norm (RadBuildCtx::plain_norm, set on the SYCL backend: `add` + `rmsnorm` where no reader takes the E4M3 codes) with the fast rmsnorm rows. - rmsnorm's bf16 rows up to 8192 wide: a work-item eight elements by one 16-byte load (the generic form walked 20 dependent loads a work-item): 24.7 -> 3.3 us a decode norm, ~3.5 ms a round. - w4a16: a bf16 activation narrowed to fp16 at the kernel's own load (the separate cast launch before every GEMM is gone), bit for bit the same. - RADIANCE_MTP_DRAFT_VOCAB=N: the 2-bit draft head over the vocabulary's first N ids (a prefix shortlist, GRIMOIRE's idea); the u2 layout hook takes an op N below the weight's rows. 131072: greedy acceptance identical on the decode proxy, draft head 650 -> 351 us a pass. KL against the exact-kernel reference (2002 positions): 0.002386, top-1 97.30% (was 0.002376 / 96.80%). Decode proxy (greedy, depth 5, mtp4): 72.0 -> 74.3 tok/s with the prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
668 lines
34 KiB
C++
668 lines
34 KiB
C++
/* 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)));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- gdn_kkt_solve on DPAS */
|
|
struct KktArgs {
|
|
const uint16_t* k;
|
|
int64_t ks0, ks1;
|
|
const float *g, *beta;
|
|
int64_t gs0, gs1, bs0, bs1;
|
|
uint16_t* A;
|
|
int64_t As0, As1;
|
|
const char* cu; /* base and element stride, as ScanArgs's */
|
|
uint32_t cudt;
|
|
int64_t cus;
|
|
int64_t T, N, H, Hg;
|
|
int64_t cu_at(int64_t i) const { return ld_index(cudt, cu, i * cus); }
|
|
};
|
|
|
|
/* C = A B over 16 x 16 f32 blocks, row-major, each row's sum over l in order. */
|
|
inline esimd::simd<float, 256> mm16(esimd::simd<float, 256> A, esimd::simd<float, 256> B) SYCL_ESIMD_FUNCTION {
|
|
using namespace esimd;
|
|
simd<float, 256> C;
|
|
#pragma unroll
|
|
for (int i = 0; i < 16; ++i) {
|
|
simd<float, 16> acc = 0.0f;
|
|
#pragma unroll
|
|
for (int l = 0; l < 16; ++l) acc += (float)A[16 * i + l] * B.template select<16, 1>(16 * l);
|
|
C.template select<16, 1>(16 * i) = acc;
|
|
}
|
|
return C;
|
|
}
|
|
|
|
/* The local-memory offset of X block (I, J), J <= I: 1 KiB blocks in row order. */
|
|
inline uint32_t xoff(int I, int J) { return (uint32_t)((I * (I + 1) / 2 + J) * 1024); }
|
|
|
|
/* A work-group of 4 threads a (sequence, chunk, v head), thread t the rows 16t..16t+15:
|
|
* M[i][j] = (beta_i (k_i . k_j)) e^{g_i - g_j}, j < i: k_i . k_j on DPAS (bf16 k, exact
|
|
* products, f32 sums), the factors in f32
|
|
* X = (I + M)^{-1} by 16 x 16 blocks: the diagonal block by forward substitution in registers,
|
|
* then X_tJ = -X_tt sum_{K=J}^{t-1} M_tK X_KJ, a level (t - J) a barrier, the blocks
|
|
* passed through local memory -- all in f32
|
|
* Not libref's order (it substitutes row by row over the whole chunk): held to gdn_kkt_solve's
|
|
* tolerance; XE2_EXACT=1 keeps the exact kernel. */
|
|
int launch_kkt(sycl::queue& q, const KktArgs& a, int64_t C) {
|
|
syclex::properties props{ intelex::grf_size<256> };
|
|
/* local memory: the X blocks (I, J), J <= I, at I (I + 1) / 2 + J, 1 KiB each */
|
|
constexpr uint32_t kBlkB = 16 * 16 * 4;
|
|
return submit([&] {
|
|
q.parallel_for(sycl::nd_range<1>{ (size_t)(a.N * C * a.H) * 4, 4 }, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
|
|
using namespace esimd;
|
|
slm_init<10 * kBlkB>();
|
|
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; /* uniform over the work-group */
|
|
const int L = (int)(slen - c0 < CK ? slen - c0 : CK);
|
|
const int64_t tok0 = bos + c0;
|
|
const int64_t hk = h / (a.H / a.Hg);
|
|
const uint16_t* kh = a.k + hk * a.ks1;
|
|
const unsigned TH = (unsigned)(a.T - 1), kp = (unsigned)(a.ks0 * 2 - 1);
|
|
const int r0 = 16 * t;
|
|
const simd<int32_t, 16> lane(0, 1);
|
|
|
|
/* g at the chunk's positions (past L, g_last), beta at the thread's rows */
|
|
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;
|
|
#pragma unroll
|
|
for (int s = 0; s < 4; ++s) {
|
|
const simd<int32_t, 16> jj = lane + 16 * s;
|
|
simd<float, 16> gv = gather<float, 16>(gp, simd<uint32_t, 16>(jj) * (uint32_t)(a.gs0 * 4), jj < L);
|
|
gv.merge(simd<float, 16>(glast), jj >= L);
|
|
gj.template select<16, 1>(16 * s) = gv;
|
|
}
|
|
simd<float, 16> gi, bi;
|
|
{
|
|
const simd<int32_t, 16> ii = lane + r0;
|
|
gi = gather<float, 16>(gp, simd<uint32_t, 16>(ii) * (uint32_t)(a.gs0 * 4), ii < L);
|
|
bi = gather<float, 16>(bp, simd<uint32_t, 16>(ii) * (uint32_t)(a.bs0 * 4), ii < L);
|
|
gi.merge(simd<float, 16>(glast), ii >= L);
|
|
bi.merge(simd<float, 16>(0.0f), ii >= L);
|
|
}
|
|
|
|
/* M's blocks of the thread's rows, Mb[J] for J <= t: k_i . k_j on DPAS */
|
|
simd<float, 128> Cc[4][2];
|
|
#pragma unroll
|
|
for (int J = 0; J < 4; ++J) Cc[J][0] = Cc[J][1] = 0.0f;
|
|
#pragma unroll 1
|
|
for (int ks = 0; ks < KK / 16; ++ks) {
|
|
simd<uint16_t, 128> a0 = load_2d<uint16_t, 16, 8, 1, false>(kh, 255u, TH, kp, ks * 16, (int)(tok0 + r0));
|
|
simd<uint16_t, 128> a1 = load_2d<uint16_t, 16, 8, 1, false>(kh, 255u, TH, kp, ks * 16, (int)(tok0 + r0 + 8));
|
|
#pragma unroll
|
|
for (int J = 0; J < 4; ++J) {
|
|
if (J > t) continue;
|
|
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 * J));
|
|
Cc[J][0] = mma(Cc[J][0], kb, a0);
|
|
Cc[J][1] = mma(Cc[J][1], kb, a1);
|
|
}
|
|
}
|
|
simd<float, 256> Mb[4], Md;
|
|
#pragma unroll
|
|
for (int J = 0; J < 4; ++J) {
|
|
if (J > t) continue;
|
|
simd<float, 256> m;
|
|
m.template select<128, 1>(0) = Cc[J][0];
|
|
m.template select<128, 1>(128) = Cc[J][1];
|
|
const simd<float, 16> gjJ = gj.template select<16, 1>(16 * J);
|
|
const simd<int32_t, 16> jc = lane + 16 * J;
|
|
#pragma unroll
|
|
for (int r = 0; r < 16; ++r) {
|
|
const int i = r0 + r;
|
|
const simd_mask<16> hide = (jc >= i) | (i >= L);
|
|
simd<float, 16> dg = (float)gi[r] - gjJ;
|
|
dg.merge(simd<float, 16>(0.0f), hide); /* keep exp finite where hidden */
|
|
simd<float, 16> v = ((float)bi[r] * m.template select<16, 1>(16 * r)) * exp(dg);
|
|
v.merge(simd<float, 16>(0.0f), hide);
|
|
m.template select<16, 1>(16 * r) = v;
|
|
}
|
|
Mb[J] = m;
|
|
if (J == t) Md = m;
|
|
}
|
|
/* the diagonal block's inverse: row r = e_r - sum_{l < r} M[r][l] X[l] */
|
|
simd<float, 256> Xd = 0.0f;
|
|
#pragma unroll
|
|
for (int r = 0; r < 16; ++r) {
|
|
simd<float, 16> row = 0.0f;
|
|
row[r] = 1.0f;
|
|
#pragma unroll
|
|
for (int l = 0; l < r; ++l) row -= (float)Md[16 * r + l] * Xd.template select<16, 1>(16 * l);
|
|
Xd.template select<16, 1>(16 * r) = row;
|
|
}
|
|
slm_block_store<float, 256>(xoff(t, t), Xd);
|
|
/* the off-diagonal blocks, a level a barrier */
|
|
#pragma unroll
|
|
for (int lvl = 1; lvl < 4; ++lvl) {
|
|
barrier();
|
|
if (t >= lvl) {
|
|
const int J = t - lvl;
|
|
simd<float, 256> S = 0.0f;
|
|
#pragma unroll
|
|
for (int K = 0; K < 4; ++K) {
|
|
if (K < J || K >= t) continue;
|
|
simd<float, 256> xkj = slm_block_load<float, 256>(xoff(K, J));
|
|
S += mm16(Mb[K], xkj);
|
|
}
|
|
slm_block_store<float, 256>(xoff(t, J), mm16(Xd, S) * -1.0f);
|
|
}
|
|
}
|
|
barrier();
|
|
/* the thread's rows of A: X_tJ for J <= t, zero right of the diagonal block */
|
|
uint16_t* Ah = a.A + h * a.As1;
|
|
const unsigned Ap = (unsigned)(a.As0 * 2 - 1);
|
|
#pragma unroll
|
|
for (int J = 0; J < 4; ++J) {
|
|
simd<float, 256> x = 0.0f;
|
|
if (J <= t) x = slm_block_load<float, 256>(xoff(t, J));
|
|
simd<uint16_t, 256> xb = bf16_of<256>(x);
|
|
#pragma unroll
|
|
for (int m = 0; m < 2; ++m) {
|
|
const int rb = r0 + 8 * m;
|
|
if (rb + 8 <= L) {
|
|
store_2d<uint16_t, 16, 8>(Ah, 127u, TH, Ap, 16 * J, (int)(tok0 + rb),
|
|
simd<uint16_t, 128>(xb.template select<128, 1>(128 * m)));
|
|
} else {
|
|
#pragma unroll
|
|
for (int r = 0; r < 8; ++r)
|
|
if (rb + r < L)
|
|
block_store<uint16_t, 16>(Ah + (tok0 + rb + r) * a.As0 + 16 * J,
|
|
simd<uint16_t, 16>(xb.template select<16, 1>(128 * m + 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);
|
|
}
|
|
|
|
/* gdn_kkt_solve's DPAS form; RAD_E_UNSUPPORTED when the call is not one it serves. bf16 k
|
|
* ([T, Hg, 128]) and A ([T, H, 64]) with unit last strides, f32 g and beta ([T, H]). */
|
|
int gdn_kkt_solve_dpas(sycl::queue& q, const uint16_t* kp, int64_t ks0, int64_t ks1, const float* g, int64_t gs0,
|
|
int64_t gs1, const float* beta, int64_t bs0, int64_t bs1, uint16_t* Ap, int64_t As0,
|
|
int64_t As1, const RadTensor* cu, int64_t T, int64_t N, int64_t H, int64_t Hg) {
|
|
if (!al64(kp) || (ks1 * 2) % 64 != 0 || (ks0 * 2) % 16 != 0 || ks0 * 2 < 256) 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;
|
|
KktArgs a{};
|
|
a.k = kp; a.ks0 = ks0; a.ks1 = ks1;
|
|
a.g = g; a.gs0 = gs0; a.gs1 = gs1; a.beta = beta; a.bs0 = bs0; a.bs1 = bs1;
|
|
a.A = Ap; a.As0 = As0; a.As1 = As1;
|
|
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.T = T; a.N = N; a.H = H; a.Hg = Hg;
|
|
return launch_kkt(q, a, (T + CK - 1) / CK);
|
|
}
|
|
|
|
} // namespace xe2
|