Archived
libxe2: givens_dense_quant on XMX for prefill rows -- 2.07 -> 0.70 s over a 1792-token prefill
For 17 or more rows the rotation is a batch of GEMMs, one a (part, 128-column group): the input fp16(x / D) per part, the f16 rotation as B, a batched form of the tiled fp16 XMX kernel (xmx_f16_batched) into f32, then libref's quantiser a (row, part, group). The input rounded once to fp16 where libref sums in double: a fast kernel. KL against the exact reference: mean 0.0037, top-1 97.2% (0.0035 / 95.6% before: within the noise of 250 positions). Up to 16 rows the float-pair kernel stays. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
This commit is contained in:
1 parent
57f02bc12d
commit
5720a958e4
3 files changed
+127
No files matched your search
@@ -60,6 +60,50 @@ int givens_dense_quant(const RadArgs* a, RadStream st) {
|
||||
const uint32_t xdt = X->dtype, tdt = T->dtype, ddt = D->dtype, qdt = Q->dtype, sdt = S->dtype;
|
||||
sycl::queue* q = queue_of(st);
|
||||
if (!q) return RAD_E_DEVICE;
|
||||
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows >= 17 && !exact_only() &&
|
||||
xcs == 1 && rad_tensor_is_contiguous(T)) {
|
||||
/* Prefill: the rotation as a batch of GEMMs on XMX, one a (part, group):
|
||||
* y[r][j] = sum_i T[p][gi][j][i] * fp16(x[r][gi*128 + i] / D[p][gi*128 + i]). The
|
||||
* activation rounded once to fp16 (2^-11) where libref sums in double: a fast kernel, held
|
||||
* to the tolerance and the KL bound. Then libref's quantiser a (row, part, group). */
|
||||
const int64_t mp = (rows + 127) / 128 * 128;
|
||||
const int64_t in_e = P * mp * n; /* fp16 inputs [P][mp][n] */
|
||||
const int64_t inbytes = (in_e * 2 + 255) / 256 * 256;
|
||||
float* ws = workspace(*q, (size_t)(inbytes / 4 + P * ngrp * mp * kG + 64), 7);
|
||||
if (!ws) return RAD_E_DEVICE;
|
||||
sycl::half* in16 = (sycl::half*)ws;
|
||||
float* yv = (float*)((char*)ws + inbytes); /* [P * ngrp][mp][128] */
|
||||
int rc = submit([&] {
|
||||
q->parallel_for(sycl::range<1>((size_t)in_e), [=](sycl::id<1> id) {
|
||||
const int64_t c = (int64_t)id[0] % n, r = ((int64_t)id[0] / n) % mp, p = (int64_t)id[0] / (n * mp);
|
||||
in16[id[0]] = r < rows ? (sycl::half)(rad_load_f32(xb, xdt, r * xrs + c) / rad_load_f32(db, ddt, p * n + c))
|
||||
: (sycl::half)0.0f;
|
||||
});
|
||||
});
|
||||
if (rc < 0) return rc;
|
||||
/* batch b = p * ngrp + gi: A = in16[p] + gi * 128 (row stride n), B = T[p][gi] [128][128] */
|
||||
for (int64_t p = 0; p < P && rc >= 0; ++p)
|
||||
rc = xmx_f16_batched(*q, in16 + p * mp * n, n, kG, (const sycl::half*)tb + p * ngrp * kG * kG, kG,
|
||||
kG * kG, yv + p * ngrp * mp * kG, mp * kG, rows, kG, kG, ngrp);
|
||||
if (rc < 0) return rc;
|
||||
return submit([&] {
|
||||
q->parallel_for(sycl::nd_range<1>{(size_t)(rows * P * ngrp) * kG, kG}, [=](sycl::nd_item<1> it) {
|
||||
const int64_t gid = (int64_t)it.get_group(0);
|
||||
const int64_t gi = gid % ngrp, p = (gid / ngrp) % P, r = gid / (ngrp * P);
|
||||
const int j = (int)it.get_local_id(0);
|
||||
const float y = yv[((p * ngrp + gi) * mp + r) * kG + j];
|
||||
const float amax = sycl::reduce_over_group(it.get_group(), sycl::fabs(y), sycl::maximum<float>());
|
||||
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
|
||||
const float inv = 1.0f / scl;
|
||||
float v = y * inv;
|
||||
if (v > qmax) v = qmax;
|
||||
if (v < -qmax) v = -qmax;
|
||||
if (i8) v = sycl::rint(v);
|
||||
rad_store_f32(qb, qdt, r * qrs + (p * n + gi * kG + j) * qcs, v);
|
||||
if (j == 0) rad_store_f32(sb, sdt, r * srs + (p * ngrp + gi) * scs, scl);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16)) {
|
||||
/* The fast form. A work-group a (part, group) and up to kRows rows; sub-group s takes the
|
||||
* outputs j = 16 s .. 16 s + 15, and for each its lanes read row j of T in one coalesced
|
||||
|
||||
Reference in new issue
Block a user