libxe2: chain-parallel gdn_chunk_scan and an fp16 m2xfp4 prefill -- 27B prefill 68 -> 91 tok/s

- gdn_chunk_scan for head_k = 128, chunk 64: a work-group of 256 a (sequence, head, block of 32
  value columns), the chunk's k, q (rows padded), the block's slices of v, S, W, V' and the sA rows
  in local memory (~124 KB); every phase of a chunk is thousands of independent chains -- W[j][e],
  V'[i][e], R[i][j], O[i][e], S[d][e] -- each one work-item's, in libref's order, A staged in local
  memory, the decays e^{g_i - g_j} (libref's double exp) once a call. Still bit-identical (rel_l2 0,
  the 27B's geometry, 1..32 rows and multi-chunk). 233 -> 96 ms a call at 1792 tokens.
- m2xfp4 at M >= 17 now fp16 on XMX: the weight times 2^(127 - e_max(n)), exact in fp16; the
  activation fp16(a * a_scale / R_m); y = acc * R_m * 2^(e_max - 127) in a new scaled fp16 XMX
  kernel (xmx_f16_scaled). fp16's 2^-11 step instead of bf16's 2^-8.

KL on the 27B against an XE2_EXACT=1 reference (every kernel exact, each matched to libref bit for
bit; 250 positions, full vocabulary): mean 0.0041 -> 0.0035, p99 0.027 -> 0.021, top-1 96.8% ->
95.6%, PPL ratio 0.998. The fast set's KL is dominated by the model's own fp8 activation quantiser
re-rounding small upstream differences.

27B prefill of 1792 tokens: 26.3 -> 19.7 s (68 -> 91 tok/s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
pascallinxweilerandClaude Opus 5.5 committed 2026-10-24 05:33:08 +02:00
1 parent 0376adeb92
commit e0bfeb54f8
6 files changed
+264 -32

No files matched your search

+148
View File
@@ -495,6 +495,154 @@ 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 (K == 128 && V % 32 == 0 && chunk == 64 && !std::getenv("XE2_GDN_SCAN1")) {
/* A work-group of 256 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
* of a chunk is a set of independent chains -- W[j][e], V'[i][e], R[i][j], O[i][e], S[d][e]
* -- each run by one work-item in libref's order, so the scan is libref's; the parallelism
* is the chains' (thousands a phase), not one column a work-item. R and the decays depend
* only on q, k, g, so each column block forms them again. */
constexpr int CB = 32, CK = 64, KK = 128, WG = 256;
constexpr int KP = KK + 1; /* k and q rows padded: the R phase's lanes read different rows */
const int64_t nblk = V / CB;
/* e^{g_i - g_j} for every (row i, chunk position j <= i of i's chunk), libref's double exp
* rounded once, a (token, head) row of 64: once here instead of once a column block. */
float* dec = workspace(*qu, (size_t)(T * H * CK + 64), 5);
if (!dec) return RAD_E_DEVICE;
if (int rc0 = submit([&] {
qu->parallel_for(sycl::range<1>((size_t)(T * H)), [=](sycl::id<1> id) {
const int64_t th = (int64_t)id[0];
const int64_t tok = th / H, h = th % H;
/* the token's sequence and its chunk start */
int64_t n = 0;
while (n + 1 < N && cuv.at(n + 1) <= tok) ++n;
const int64_t bos = cuv.at(n), c0 = bos + (tok - bos) / CK * CK;
const float gi = gs.ld(at2(gv, tok, h));
for (int64_t j = 0; j < CK; ++j) {
const int64_t tj = c0 + j;
dec[th * CK + j] = tj <= tok ? rexp(gi - gs.ld(at2(gv, tj, h))) : 0.0f;
}
});
});
rc0 < 0)
return rc0;
return submit([&] {
qu->submit([&](sycl::handler& hd) {
sycl::local_accessor<float, 1> lm(sycl::range<1>((size_t)(2 * CK * KP + 3 * CK * CB + KK * CB + CK * CK + 4 * CK)), hd);
hd.parallel_for(sycl::nd_range<1>{(size_t)(N * H * nblk) * WG, (size_t)WG}, [=](sycl::nd_item<1> it) {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t blk = gid % nblk, nh = gid / nblk;
const int64_t n = nh / H, h = nh % H, hk = h / grp;
const int64_t e0 = blk * CB;
const int t = (int)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* kc = &lm[0]; /* [CK][KP] */
float* qc = kc + CK * KP; /* [CK][KP] */
float* vc = qc + CK * KP; /* [CK][CB] */
float* Wm = vc + CK * CB; /* [CK][CB] */
float* Vp = Wm + CK * CB; /* [CK][CB] */
float* St = Vp + CK * CB; /* [KK][CB] */
float* R = St + KK * CB; /* [CK][CK] */
float* gc = R + CK * CK; /* [CK] */
float* bc = gc + CK;
float* ge = bc + CK; /* e^{g_j} */
float* gsj = ge + CK; /* e^{g_last - g_j} */
for (int idx = t; idx < KK * CB; idx += WG) {
const int d = idx / CB, e = idx % CB;
St[idx] = has_h0 ? h0s.ld(at4(h0v, sn, h, e0 + e, d)) : 0.0f;
}
for (int64_t c0 = 0; c0 < slen; c0 += CK) {
const int64_t L = (c0 + CK < slen) ? CK : slen - c0;
sycl::group_barrier(gw);
for (int idx = t; idx < (int)L * KK; idx += WG) {
const int j = idx / KK, d = idx % KK;
const int64_t tj = bos + c0 + j;
kc[j * KP + d] = ks.ld(at3(kv, tj, hk, d));
qc[j * KP + d] = qs.ld(at3(qv, tj, hk, d));
}
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int j = idx / CB, e = idx % CB;
vc[idx] = vs.ld(at3(vv, bos + c0 + j, h, e0 + e));
}
if (t < L) {
const int64_t tj = bos + c0 + t;
gc[t] = gs.ld(at2(gv, tj, h));
bc[t] = bs.ld(at2(bv, tj, h));
}
sycl::group_barrier(gw);
const float glast = gc[L - 1];
if (t < L) {
ge[t] = rexp(gc[t]);
gsj[t] = rexp(glast - gc[t]);
}
sycl::group_barrier(gw);
/* W[j][e] = beta_j (v[j][e] - e^{g_j} sum_d k[j][d] S[d][e]) */
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int j = idx / CB, e = idx % CB;
float acc = 0.0f;
for (int d = 0; d < KK; ++d) acc += kc[j * KP + d] * St[d * CB + e];
Wm[idx] = bc[j] * (vc[idx] - ge[j] * acc);
}
/* the chunk's rows of A, widened into the R buffer (R is formed after V') */
for (int idx = t; idx < (int)(L * L); idx += WG) {
const int i = idx / (int)L, j = idx % (int)L;
R[i * CK + j] = j <= i ? As.ld(at3(Av, bos + c0 + i, h, j)) : 0.0f;
}
sycl::group_barrier(gw);
/* V'[i][e] = sum_{j <= i} A[i][j] W[j][e] */
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int i = idx / CB, e = idx % CB;
float vp = 0.0f;
for (int j = 0; j <= i; ++j) vp += R[i * CK + j] * Wm[j * CB + e];
Vp[idx] = vp;
}
sycl::group_barrier(gw);
/* R[i][j] = scale e^{g_i - g_j} sum_d q[i][d] k[j][d], j <= i */
for (int idx = t; idx < (int)(L * L); idx += WG) {
const int i = idx / (int)L, j = idx % (int)L;
if (j > i) continue;
float rj = 0.0f;
for (int d = 0; d < KK; ++d) rj += qc[i * KP + d] * kc[j * KP + d];
R[i * CK + j] = scale * dec[((bos + c0 + i) * H + h) * CK + j] * rj;
}
sycl::group_barrier(gw);
/* O[i][e] = scale e^{g_i} sum_d q[i][d] S[d][e] + sum_{j <= i} R[i][j] V'[j][e] */
for (int idx = t; idx < (int)L * CB; idx += WG) {
const int i = idx / CB, e = idx % CB;
float inr = 0.0f;
for (int d = 0; d < KK; ++d) inr += qc[i * KP + d] * St[d * CB + e];
float acc = (scale * ge[i]) * inr;
for (int j = 0; j <= i; ++j) acc += R[i * CK + j] * Vp[j * CB + e];
os.st(at3(ov, bos + c0 + i, h, e0 + e), acc);
}
sycl::group_barrier(gw);
/* k[j][d] *= e^{g_last - g_j}; S[d][e] = e^{g_last} S[d][e] + sum_j V'[j][e] k[j][d] */
for (int idx = t; idx < (int)L * KK; idx += WG) {
const int j = idx / KK, d = idx % KK;
kc[j * KP + d] = gsj[j] * kc[j * KP + d];
}
sycl::group_barrier(gw);
const float el = rexp(glast);
for (int idx = t; idx < KK * CB; idx += WG) {
const int d = idx / CB, e = idx % CB;
float sr = el * St[idx];
for (int j = 0; j < (int)L; ++j) sr += Vp[j * CB + e] * kc[j * KP + d];
St[idx] = sr;
}
}
sycl::group_barrier(gw);
if (has_ht)
for (int idx = t; idx < KK * CB; idx += WG) {
const int d = idx / CB, e = idx % CB;
hts.st(at4(htv, sn, h, e0 + e, d), St[idx]);
}
});
});
});
}
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