libxe2: givens_dense_quant on register DPAS for rows <= 16 -- 64 -> 24 us a decode/verify call

At a verify's 8 rows the float-pair form ran 80 work-groups (n=5120, two parts), each walking its
rows in turn. A (part, group)'s rotation is now one small GEMM on dpas_f16: the rows' inputs
fp16(x / D) in local memory as A columns, a lane's B column its output's row of T, T read once for
every row; then libref's quantiser. The prefill form's arithmetic (fp16 activation, f32 sums), held
to the same tolerance: 400/400 live oracle issues agree (rel at most 4.5e-3, p99 4e-4). Per
speculative round 18.5 -> ~5.5 ms. XE2_GIVENS_PAIR=1 keeps the float-pair form (rel 0).

Also: the BetterBench row for b0e34c6 (decode 28.9, prefill 131.3 @2000) and the round's notes.

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 14:27:06 +02:00
1 parent c55ca689fe
commit 6579556715
2 files changed
+121 -1

No files matched your search

+74
View File
@@ -14,6 +14,7 @@
#include "xe2_common.h"
#include "xe2_ops.h"
#include <cstdlib>
#include <cstring>
namespace xe2 {
@@ -104,6 +105,79 @@ int givens_dense_quant(const RadArgs* a, RadStream st) {
});
});
}
if (tdt == RAD_F16 && rad_ptr_aligned(T->data, 16) && rows <= 16 && !exact_only() &&
!std::getenv("XE2_GIVENS_PAIR")) {
/* Decode and the verify (rows <= 16): a (part, group)'s rotation as one small GEMM on
* register DPAS (dpas_f16), y[r][j] = sum_i T[j][i] fp16(x[r][i] / D[i]) -- the prefill
* form's arithmetic (activation rounded once to fp16, f32 sums), held to the same
* tolerance and the KL bound. A work-group a (part, group): the rows' inputs in local
* memory as A columns, sub-group s the outputs 16 s .. 16 s + 15, a lane's B column its
* output's row of T (32 contiguous bytes a K16 step), so T is read once for all rows; then
* libref's quantiser a (row, part, group). The float-pair form (XE2_GIVENS_PAIR=1) walked
* the rows in turn on 80 work-groups at n = 5120. */
const int mt = rows <= 8 ? 1 : 2;
const int64_t mp = mt * 8;
return submit([&] {
q->submit([&](sycl::handler& h) {
sycl::local_accessor<uint16_t, 1> at(sycl::range<1>((size_t)(kG * 16)), h);
sycl::local_accessor<float, 1> red(sycl::range<1>((size_t)(8 * 16)), h);
h.parallel_for(sycl::nd_range<1>{(size_t)(P * ngrp) * kG, kG},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const int64_t gid = (int64_t)it.get_group(0);
const int64_t gi = gid % ngrp, p = gid / ngrp;
const int t = (int)it.get_local_id(0);
const sycl::sub_group sg = it.get_sub_group();
const int sgi = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
const int64_t c = gi * kG + t;
const float dsc = rad_load_f32(db, ddt, p * n + c);
for (int r = 0; r < mp; ++r) { /* at[i][r]: lane k's A column is 16 bytes */
const float v = r < rows ? rad_load_f32(xb, xdt, r * xrs + c * xcs) / dsc : 0.0f;
at[t * mp + r] = sycl::bit_cast<uint16_t>((sycl::half)v);
}
sycl::group_barrier(it.get_group());
const int j = sgi * 16 + lane;
const uint16_t* trow = (const uint16_t*)tb + ((p * ngrp + gi) * kG + j) * kG;
xe2_float8 acc[2] = { xe2_float8(0.0f), xe2_float8(0.0f) };
for (int k0 = 0; k0 < kG; k0 += 16) {
const sycl::vec<int, 4> b0 = *(const sycl::vec<int, 4>*)(trow + k0);
const sycl::vec<int, 4> b1 = *(const sycl::vec<int, 4>*)(trow + k0 + 8);
xe2_int8 b;
for (int u = 0; u < 4; ++u) { b[u] = b0[u]; b[u + 4] = b1[u]; }
for (int i = 0; i < mt; ++i) {
const sycl::vec<uint16_t, 8> av = *(const sycl::vec<uint16_t, 8>*)&at[(k0 + lane) * mp + i * 8];
xe2_short8 a;
for (int m = 0; m < 8; ++m) a[m] = (short)av[m];
acc[i] = dpas_f16(a, b, acc[i]);
}
}
/* each row's amax over the group's 128 outputs: the sub-groups' maxima in
* local memory, read by all */
for (int i = 0; i < mt; ++i) {
for (int m = 0; m < 8; ++m) {
const float am = sycl::reduce_over_group(sg, sycl::fabs(acc[i][m]), sycl::maximum<float>());
if (lane == 0) red[sgi * 16 + i * 8 + m] = am;
}
}
sycl::group_barrier(it.get_group());
for (int i = 0; i < mt; ++i)
for (int m = 0; m < 8; ++m) {
const int64_t r = i * 8 + m;
if (r >= rows) break;
float amax = 0.0f;
for (int u = 0; u < 8; ++u) amax = sycl::fmax(amax, red[u * 16 + i * 8 + m]);
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
const float inv = 1.0f / scl;
float v = acc[i][m] * 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 (t == 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