libxe2: the speculative round's selection ops -- w2a8 head on int8 DPAS, dflash_select a sub-group a sequence, row_topk in two stages

The DFlash2 drafter's 2-bit vocabulary head (N=248320) forms each group's integer sum on int8 DPAS
(8 x 32 by 32 x 16), the u2 codes unpacked straight into the packed B tile; a lane owns a column
and adds libref's group terms in order: 24.0 -> 4.2 ms a round, bit-identical to the libref-checked
sub-group form on 63 live calls. dflash_select scores the 16 candidates in parallel lanes, each in
libref's order, lane 0 taking the first strict maximum: 11.6 -> 0.5 ms. row_topk takes each of 128
chunks' top R, then the top R of the winners: 3.2 -> 0.95 ms. Both exact against libref (fixture,
engine oracle).

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 12:01:34 +02:00
1 parent db9a464ac4
commit 99fd4ea59a
2 files changed
+248 -2

No files matched your search

+114
View File
@@ -19,6 +19,7 @@
#include "rad_planes.h"
#include <cstdlib>
#include <cstring>
namespace xe2 {
@@ -287,6 +288,111 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
});
}
/* The same group terms with the integer sums on XMX: an int8 DPAS (8 x 32 by 32 x 16) forms each
* group's sum of a * code exactly in int32 for up to 8 rows and 16 columns at once. The codes
* unpack straight into the packed B operand: four codes along k are one dword, and the step
* order's swap of bits 2 and 3 of k moves whole dwords, so the activation is used as stored.
* A sub-group owns 16 columns and walks the groups in order, each lane adding its column's terms
* as the per-output form does -- libref's chain, at one read of the weight. */
constexpr int kZpDpasSgs = 16;
template <int CBITS>
inline uint32_t zp_dword(const uint8_t* crow, int64_t d) { /* the 4 codes of logical dword d */
if constexpr (CBITS == 2) {
const uint32_t b = crow[d];
return (b & 3u) | ((b >> 2) & 3u) << 8 | ((b >> 4) & 3u) << 16 | ((b >> 6) & 3u) << 24;
} else {
const uint32_t h = (uint32_t)crow[2 * d] | (uint32_t)crow[2 * d + 1] << 8;
return (h & 15u) | ((h >> 4) & 15u) << 8 | ((h >> 8) & 15u) << 16 | ((h >> 12) & 15u) << 24;
}
}
template <int CBITS>
int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint32_t sadt, int64_t sa_rs,
const char* sumb, uint32_t sumdt, int64_t asum_s, int64_t mpad, const uint8_t* bb, int64_t brow,
const char* bsb, char* yb, uint32_t ydt, int64_t ys_r, int64_t ys_c, int64_t N, int64_t K,
int64_t group, int mm) {
namespace jm = sycl::ext::oneapi::experimental::matrix;
const int64_t G = K / group;
const size_t wgs = (size_t)((N + 16 * kZpDpasSgs - 1) / (16 * kZpDpasSgs));
return submit([&] {
q.submit([&](sycl::handler& h) {
sycl::local_accessor<int8_t, 1> As(sycl::range<1>((size_t)(8 * K)), h);
sycl::local_accessor<uint32_t, 1> Bs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h);
sycl::local_accessor<int32_t, 1> Cs(sycl::range<1>((size_t)kZpDpasSgs * 8 * 16), h);
h.parallel_for(sycl::nd_range<1>{wgs * kZpDpasSgs * kSub, (size_t)kZpDpasSgs * kSub},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int s = (int)sg.get_group_linear_id(), lane = (int)sg.get_local_linear_id();
const int t = (int)it.get_local_id(0);
for (int64_t i = t; i < 2 * K; i += kZpDpasSgs * kSub) { /* 8 rows, 4 bytes at a time */
const int64_t m = i / (K / 4), c = (i % (K / 4)) * 4;
uint32_t v = 0u;
if (m < mm) v = *(const uint32_t*)(ab + m * ars + c);
*(uint32_t*)&As[m * K + c] = v;
}
sycl::group_barrier(it.get_group());
const int64_t n0 = ((int64_t)it.get_group(0) * kZpDpasSgs + s) * 16;
if (n0 >= N) return;
const DPlane sc = find_plane(bsb, "scale"), zp = find_plane(bsb, "zero");
if (!sc.p || !zp.p) return;
const int64_t n = n0 + lane;
const bool live = n < N;
const uint8_t* crow = bb + (live ? n : n0) * brow;
float sa[8], acc[8];
for (int m = 0; m < 8; ++m) {
sa[m] = m < mm ? rad_load_f32(sab, sadt, m * sa_rs) : 0.0f;
acc[m] = 0.0f;
}
uint32_t* bt = &Bs[s * 8 * 16];
int32_t* ct = &Cs[s * 8 * 16];
auto lb = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>((int8_t*)bt);
auto lc = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(ct);
jm::joint_matrix<sycl::sub_group, int8_t, jm::use::a, 8, 32, jm::layout::row_major> ma;
jm::joint_matrix<sycl::sub_group, int8_t, jm::use::b, 32, 16, jm::layout::ext_intel_packed> mb;
jm::joint_matrix<sycl::sub_group, int32_t, jm::use::accumulator, 8, 16> mc;
for (int64_t g = 0; g < G; ++g) {
jm::joint_matrix_fill(sg, mc, 0);
for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 32) {
/* stored dword r of this step holds logical dword (r & ~3) | swap(r & 3) */
for (int r = 0; r < 8; ++r) {
const int ld = (r & ~3) | ((r & 1) << 1) | ((r & 2) >> 1);
bt[r * 16 + lane] = zp_dword<CBITS>(crow, k0 / 4 + ld);
}
sycl::group_barrier(sg);
auto la = sycl::address_space_cast<sycl::access::address_space::local_space,
sycl::access::decorated::no>(&As[k0]);
jm::joint_matrix_load(sg, ma, la, (size_t)K);
jm::joint_matrix_load(sg, mb, lb, 64);
jm::joint_matrix_mad(sg, mc, ma, mb, mc);
sycl::group_barrier(sg);
}
jm::joint_matrix_store(sg, mc, lc, 16, jm::layout::row_major);
sycl::group_barrier(sg);
if (live) {
const float sb = sc.at(n, g);
const float z = (float)(int32_t)zp.at(n, g);
for (int m = 0; m < 8; ++m) {
if (m >= mm) break;
const float sub = (float)ct[m * 16 + lane] - z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s);
const float term = sub * sa[m] * sb;
acc[m] += term;
}
}
sycl::group_barrier(sg);
}
if (live)
for (int m = 0; m < 8; ++m) {
if (m >= mm) break;
rad_store_f32(yb, ydt, m * ys_r + n * ys_c, acc[m]);
}
});
});
});
}
} // namespace
int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) {
@@ -321,6 +427,14 @@ int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) {
const int64_t sa_rs = row_stride(AS, 1);
const int64_t asum_s = ASUM->rank ? ASUM->stride[ASUM->rank - 1] : 1;
const int64_t ys_r = row_stride(Y, N), ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1;
if (M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 4 == 0 &&
!std::getenv("XE2_ZP_GEMV")) {
auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>;
int rc = dp(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,
(const char*)ASUM->data, ASUM->dtype, asum_s, mpad, (const uint8_t*)B->data, brow,
(const char*)BS->data, (char*)Y->data, Y->dtype, ys_r, ys_c, N, K, group, (int)M);
return rc;
}
auto go = [&](auto tag) {
constexpr int MM = decltype(tag)::value;
return zp_gemv<MM>(*q, (const int8_t*)A->data, ars, (const char*)AS->data, AS->dtype, sa_rs,