libxe2: the w2a8 draft head on int8 DPAS with u2 B operands, codes tiled at load -- 2.56 -> 1.66 ms

This card's DPAS takes 2-bit B operands against s8 A, 16 codes a dword from the low end (probe
dpas_u2.cpp), so the drafter's 2-bit vocabulary head needs no decode: gemm_zp_layout stores the u2
codes tiled ("xe2.zp.u2.t16": a 16-column tile's 32-column step is 128 bytes, [dword][lane],
each dword the column's codes at the activation's stored positions -- quant_act_i8's step order is
folded into the layout), and an ESIMD kernel (xe2_gemm_zp_esimd.cpp) feeds them to DPAS as they
lie, against the activation staged in local memory as the A operand. Each group's sum is exact
in int32 and the terms are added in group order, as zp_dpas and libref do. The exact kernel and
the SIMT GEMV read the tiled form too.

Checked: 20 calls on the engine's real inputs, the ESIMD kernel against the SIMT GEMV on the same
bytes, 0 of 1738240 outputs differ each; greedy output identical (rad-kbench has no w2a8 recipe
on this container).

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-26 03:18:30 +01:00
1 parent 5d09eb98b5
commit 34ae231e97
3 files changed
+205 -4

No files matched your search

+72 -4
View File
@@ -96,13 +96,53 @@ int64_t row_stride(const RadTensor* t, int64_t cols) {
return t->stride[t->rank - 2];
}
constexpr const char* kTagU2 = "xe2.zp.u2.t16";
/* Tiling the u2 codes plane into its stored form and back (zp_tiled, xe2_ops.h). */
void tile_u2(const uint8_t* rows, uint32_t* t, int64_t N, int64_t K, bool to_tiled) {
const int64_t rb = K / 4;
for (int64_t n = 0; n < N; ++n)
for (int64_t s = 0; s < K / 32; ++s)
for (int r = 0; r < 2; ++r) {
uint32_t& w = t[zpt_dword(n, s, r, K)];
if (to_tiled) w = 0;
for (int u = 0; u < 16; ++u) {
const int pos = 16 * r + u;
const int64_t k = s * 32 + ((pos & ~12) | ((pos & 4) << 1) | ((pos & 8) >> 1));
if (to_tiled) w |= rad_load_bits(rows + n * rb, 2, k) << (2 * u);
else rad_store_bits(const_cast<uint8_t*>(rows) + n * rb, 2, k, (w >> (2 * u)) & 3u);
}
}
}
} // namespace
/* ------------------------------------------------------------------ the layout hooks */
/* libref's ref_layout_gemm_q, line for line: `b_scale` (operand 3) joined as "scale" and "zero",
* one each per (row, `group` of K), over codes of the grid's width. */
* one each per (row, `group` of K), over codes of the grid's width. One addition: the u2 codes
* (operand 2) stored tiled where zp_tiled holds ("xe2.zp.u2.t16", xe2_ops.h), the int8 DPAS's u2
* B operand as it lies; libref reads them as they are, so a weight libxe2 claims this way is
* libxe2's alone. */
int gemm_zp_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out) {
if (operand == 2) {
const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr));
if (!zg) return RAD_E_DTYPE;
if (zg->codes != RAD_U2 || n != 1 || !enc || sel[0] < 0 || sel[0] >= enc->n_planes ||
std::strncmp(enc->plane[sel[0]].role, "codes", RAD_ENC_STR) != 0)
return RAD_E_UNSUPPORTED;
const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0);
const long long group = rad_param_geti(p, n_p, "group", 0);
if (!zp_tiled(2, N, K, group)) return RAD_E_UNSUPPORTED;
if (planes[0].shape[0] != N || planes[0].shape[1] != K) return RAD_E_SHAPE;
out->tag = kTagU2;
out->dtype = RAD_U2;
out->rank = 1;
out->shape[0] = N * K;
out->align = 0;
out->bytes = N * K / 4;
return RAD_OK;
}
if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED;
const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr));
const long long group = rad_param_geti(p, n_p, "group", 0);
@@ -127,6 +167,11 @@ int gemm_zp_relayout(const RadParam* p, int n_p, int operand, const RadEncoding*
RadLayout lay{};
const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
if (operand == 2) {
if (dst_bytes != lay.bytes) return RAD_E_SHAPE;
tile_u2((const uint8_t*)planes[0].data, (uint32_t*)dst, planes[0].shape[0], planes[0].shape[1], true);
return RAD_OK;
}
return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes);
}
@@ -136,6 +181,12 @@ int gemm_zp_unrelayout(const RadParam* p, int n_p, int operand, const RadEncodin
RadLayout lay{};
const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
if (operand == 2) {
if (src_bytes != lay.bytes) return RAD_E_SHAPE;
tile_u2((const uint8_t*)planes[0].data, (uint32_t*)const_cast<void*>(src), planes[0].shape[0],
planes[0].shape[1], false);
return RAD_OK;
}
return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n);
}
@@ -177,6 +228,7 @@ int gemm_nt_q_zp(const RadArgs* a, RadStream st) {
const char *sab = (const char*)AS->data, *sumb = (const char*)ASUM->data;
const uint32_t sadt = AS->dtype, sumdt = ASUM->dtype, ydt = Y->dtype;
char* yb = (char*)Y->data;
const bool tl = zp_tiled(cbits, N, K, group); /* the stored form gemm_zp_layout asked for */
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const size_t wn = 64;
@@ -197,7 +249,8 @@ int gemm_nt_q_zp(const RadArgs* a, RadStream st) {
int64_t is = 0;
for (int64_t k = g * group; k < (g + 1) * group; ++k) {
const int64_t pos = (k & ~(int64_t)12) | ((k & 4) << 1) | ((k & 8) >> 1);
is += (int64_t)ar[pos] * (int32_t)rad_load_bits(crow, cbits, k);
const uint32_t code = tl ? zpt_code((const uint32_t*)bb, n, k, K) : rad_load_bits(crow, cbits, k);
is += (int64_t)ar[pos] * (int32_t)code;
}
const float sub = (float)is - (float)z * rad_load_f32(sumb, sumdt, (g * mpad + m) * asum_s);
acc += sub * sa * sb;
@@ -231,6 +284,7 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
int64_t K, int64_t group, int mm) {
const int64_t G = K / group;
const size_t groups = (size_t)((N + 15) / 16);
const bool tl = zp_tiled(cbits, N, K, group);
return submit([&] {
syclex::nd_launch(q, sycl::nd_range<1>{groups * 256, 256},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
@@ -251,7 +305,8 @@ int zp_gemv(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
for (int m = 0; m < MM; ++m) is[m] = 0.0f;
for (int64_t k0 = g * group; k0 < (g + 1) * group; k0 += 16) {
float cf[16];
for (int j = 0; j < 16; ++j) cf[j] = (float)rad_load_bits(crow, cbits, k0 + j);
for (int j = 0; j < 16; ++j)
cf[j] = (float)(tl ? zpt_code((const uint32_t*)bb, n, k0 + j, K) : rad_load_bits(crow, cbits, k0 + j));
for (int m = 0; m < MM; ++m) {
if (m >= mm) break;
/* the step order: logical column j of the step sits at byte
@@ -425,6 +480,12 @@ int zp_dpas(sycl::queue& q, const int8_t* ab, int64_t ars, const char* sab, uint
} // namespace
/* xe2_gemm_zp_esimd.cpp */
constexpr int64_t kZpEsimdMaxK = 8192;
int zp_esimd(sycl::queue& q, const int8_t* ab, int64_t ars, const float* sa, int64_t sa_rs, const float* asum,
int64_t mpad, const uint32_t* bt, 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);
int gemm_nt_q_zp_gemv(const RadArgs* a, RadStream st) {
if (exact_only()) return gemm_nt_q_zp(a, st);
if (!a || a->n_t < 5 || !a->t[0].data || !a->t[2].data || !a->t[4].data) return RAD_E_INVAL;
@@ -457,7 +518,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 % 2 == 0 &&
const bool tl = zp_tiled(cbits, N, K, group);
/* tiled u2 codes: int8 DPAS with the codes as its u2 B operand (xe2_gemm_zp_esimd.cpp) */
if (tl && M <= 8 && K <= kZpEsimdMaxK && AS->dtype == RAD_F32 && ASUM->dtype == RAD_F32 && asum_s == 1 &&
(Y->dtype == RAD_F32 || Y->dtype == RAD_BF16) && rad_ptr_aligned(B->data, 64) && !std::getenv("XE2_ZP_GEMV"))
return zp_esimd(*q, (const int8_t*)A->data, ars, (const float*)AS->data, sa_rs, (const float*)ASUM->data,
mpad, (const uint32_t*)B->data, (const char*)BS->data, (char*)Y->data, Y->dtype, ys_r,
ys_c, N, K, group, (int)M);
if (!tl && M <= 8 && (cbits == 2 || cbits == 4) && group % 32 == 0 && K * 8 <= 64 * 1024 && ars % 2 == 0 &&
brow % 16 == 0 && rad_ptr_aligned(B->data, 16) &&
!std::getenv("XE2_ZP_GEMV")) {
auto dp = cbits == 2 ? zp_dpas<2> : zp_dpas<4>;