libxe2: gdn_kkt_solve a work-group a chunk, row terms spread over 256 work-items -- 1.51 -> 0.45 s a 1882-token prefill

kkt walked a head's chunks and rows in turn on 48 work-groups, its working rows in global
memory. Chunks are independent here: a work-group now takes one (sequence, head, chunk) with the
chunk's k rows in local memory, forms every row term beta_i (k_i . k_j) e^{g_i - g_j} first (each
its sum over d in order, 256 work-items), then solves row by row from local memory. libref's chains
and order: fixture 90/90 at rel_l2 0. (Measured on the way: the double exp is not the cost; k as
f32 in local memory halves the resident work-groups and is slower.)

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 16:21:01 +02:00
1 parent 905a7ec242
commit 1a61aac915
1 file changed
+79
+79
View File
@@ -400,6 +400,85 @@ 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 == 64 && K <= 128 && !std::getenv("XE2_KKT_SERIAL")) {
/* A work-group a (sequence, v head, chunk): chunks are independent here. The chunk's k rows
* (bf16, exact) in local memory; first every row term beta_i (k_i . k_j) e^{g_i - g_j},
* j < i -- each its sum over d in order -- spread over 256 work-items; then the solve, row
* by row: work-item j < 64 forms s[j] = delta - sum over l in order of row[l] A[l][j], all
* in local memory. libref's chains and order. The form below walked a head's chunks and rows
* in turn on 48 work-groups with its rows in global memory (31 ms a 1882-token call). */
constexpr int CK = 64, KMAX = 128;
constexpr int KR = KMAX + 2; /* k rows padded: neighbouring lanes read different rows */
constexpr int KWG = 256; /* the row terms spread over 256 work-items; the solve uses 64 */
const int64_t cmax = (T + CK - 1) / CK; /* a sequence has at most T tokens */
const bool typed = ks.dt == RAD_BF16 && bs.dt == RAD_F32 && gs.dt == RAD_F32 && As.dt == RAD_BF16 &&
!std::getenv("XE2_GDN_GENERIC");
auto launch = [&](auto ty) {
constexpr bool TY = decltype(ty)::value;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<uint16_t, 1> kc(sycl::range<1>((size_t)(CK * KR)), hd);
sycl::local_accessor<float, 1> kt(sycl::range<1>((size_t)(CK * CK)), hd);
sycl::local_accessor<float, 1> am(sycl::range<1>((size_t)(CK * CK)), hd);
sycl::local_accessor<float, 1> gb(sycl::range<1>((size_t)(2 * CK)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H * cmax) * KWG, (size_t)KWG}, [=](sycl::nd_item<1> it) {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t ci = gid % cmax, nh = gid / cmax;
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int t = (int)it.get_local_id(0), j = t;
auto grpw = it.get_group();
const int64_t bos = cuv.at(n), slen = cuv.at(n + 1) - bos;
const int64_t c0 = ci * CK;
if (c0 >= slen) return; /* uniform */
const int L = (int)((c0 + CK < slen) ? CK : slen - c0);
auto LK = [&](int64_t o) -> uint16_t {
if constexpr (TY) return ((const uint16_t*)ks.p)[o];
else return rad_f32_to_bf16(ks.ld(o)); /* only bf16 k reaches here exactly */
};
auto LF = [&](const DT& d, int64_t o) -> float {
if constexpr (TY) return ((const float*)d.p)[o];
else return d.ld(o);
};
if (t < L) {
const int64_t tj = bos + c0 + t;
gb[t] = LF(gs, at2(gv, tj, h));
gb[CK + t] = LF(bs, at2(bv, tj, h));
}
for (int64_t e = t; e < (int64_t)L * K; e += KWG) { /* lanes along d: coalesced */
const int64_t jr = e / K, d = e % K;
kc[jr * KR + d] = LK(at3(kv, bos + c0 + jr, hk, d));
}
sycl::group_barrier(grpw);
for (int p = t; p < L * L; p += KWG) {
const int i = p / L, jj = p % L;
if (jj >= i) continue;
float rj = 0.0f;
for (int64_t d = 0; d < K; ++d)
rj += rad_bf16_to_f32(kc[i * KR + d]) * rad_bf16_to_f32(kc[jj * KR + d]);
kt[i * CK + jj] = gb[CK + i] * rj * rexp(gb[i] - gb[jj]);
}
sycl::group_barrier(grpw);
for (int i = 0; i < L; ++i) {
if (j < CK) {
float v = 0.0f;
if (j <= i) {
float sj = (i == j) ? 1.0f : 0.0f;
for (int l = j; l < i; ++l) sj -= kt[i * CK + l] * am[l * CK + j];
v = sj;
}
am[i * CK + j] = v;
const int64_t o = at3(Av, bos + c0 + i, h, j);
if constexpr (TY) ((uint16_t*)As.p)[o] = rad_f32_to_bf16(v);
else As.st(o, v);
}
sycl::group_barrier(grpw);
}
});
});
});
};
if (typed || ks.dt == RAD_BF16) return typed ? launch(std::true_type{}) : launch(std::false_type{});
}
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,