This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/libxe2/xe2_gemm_w4.cpp
T
claudeandClaude Opus 5.5 6abca01f17 libxe2: w4a16 decode GEMM in 16-row blocks, each group's codes decoded once
gemm_nt_q at w4a16_asym (M <= 64) ran 8-row blocks, each reading and decoding the whole 4-bit weight:
16 rows paid twice, 64 rows eight times. launch16x2 takes two 8-row bands against each group's codes,
loaded and decoded once, the activation loaded 32 columns at a time so both bands fit the default
register file (no spill). The per-row arithmetic is launch16<8>'s. XE2_W4_RB8=1 keeps 8-row blocks.

Qwen3.8-27B int4 (RedHatAI, as shipped), MTP depth 3, Arc Pro B65: 4 concurrent 256-token streams
97.6-99.2 -> 109.4-110.0 tok/s aggregate (+12%); one stream unchanged (43.1 -> 43.2). XE2_W4_CHECK=50
against libref's loop on the host: rel_l2 <= 6.6e-5 on every shape, tens of ties in ~10^5 outputs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 01:53:59 +01:00

484 lines
22 KiB
C++

/* xe2_gemm_w4.cpp -- gemm_nt_q over the 4-bit zero-point grids of an int4 W4A16 release
* (docs/OPS.md): u4 codes, one f16 scale and one integer zero per (row, 128 of K), against the
* input cast to f16 up to 64 rows a step (w4a16_asym) or quant_act_i8g's row-major int8 codes with
* an f32 scale per (row, 128) above (w4a8i8_asym). libref's gemm_zp (ref_gemm.cpp) per output:
*
* w4a16_asym sub = sum over ascending k of a * (code - zero), each product exact in f32
* acc += sub * scale
* w4a8i8_asym sub = sum(a * (code - zero)), exact integers
* acc += (sub * a_scale[m][g]) * scale
*
* Where w4_tiled holds (xe2_ops.h) the planes are stored as the DPAS kernels read them: the codes
* ("xe2.w4.t16") as a 16-column tile's dwords side by side, the scale and zero ("xe2.w4.sz.t16") as
* one dword a (column, group). Every libxe2 reader takes that form and unrelayout inverts it
* exactly; elsewhere the planes are libref's (the codes as they are, b_scale joined).
*
* gemm_nt_q_w4 the correctness-first form: libref's loop per output
* gemm_nt_q_w4a16 M <= 64: fp16 DPAS over the codes decoded in registers, the group sums
* scaled in f32 (xe2_gemm_w4_esimd.cpp); not libref's order
* gemm_nt_q_w4a8i8 int8 DPAS with the codes as its u4 B operand: each group's integer sum is
* exact and the terms are added in group order -- libref's result bit for bit
*
* rad-kbench and the op oracle cannot draw these grids (libref describes no tensor holding a scale
* and a zero, docs/OPS.md), so XE2_W4_CHECK=n compares the first n calls of each form against
* libref's loop on the host, on the engine's own operands: the exact forms must agree bit for bit,
* the fp16 form within the stated rel_l2.
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include "xe2_zp.h"
#include "rad_planes.h"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
namespace xe2 {
/* xe2_gemm_w4_esimd.cpp */
int w4a16_esimd(sycl::queue& q, int rc, const void* a, bool a_bf16, int64_t ars, int mm, const uint32_t* ct,
const uint32_t* sz, int64_t N, int64_t K, int splits, char* yb, uint32_t ydt, int64_t yrs);
int w4a8i8_esimd(sycl::queue& q, const uint8_t* ap, const int32_t* asumt, const float* sat, int64_t mpad,
const uint32_t* ct, const uint32_t* sz, int64_t M, int64_t N, int64_t K, char* yb, uint32_t ydt,
int64_t yrs);
namespace {
constexpr const char* kTagC = "xe2.w4.t16";
constexpr const char* kTagS = "xe2.w4.sz.t16";
enum class W4 { None, F16, I8G };
W4 w4_kind(const char* dt) {
if (!dt) return W4::None;
if (std::strcmp(dt, "w4a16_asym") == 0) return W4::F16;
if (std::strcmp(dt, "w4a8i8_asym") == 0) return W4::I8G;
return W4::None;
}
/* A dword of eight codes in rad_load_bits's order (code u at bits 4u) to the stored order (the even
* codes in the low half, the odd ones in the high half) and back. */
inline uint32_t w4_pack(uint32_t x) {
uint32_t e = x & 0x0F0F0F0Fu, o = (x >> 4) & 0x0F0F0F0Fu;
e = (e | (e >> 4)) & 0x00FF00FFu;
e = (e | (e >> 8)) & 0x0000FFFFu;
o = (o | (o >> 4)) & 0x00FF00FFu;
o = (o | (o >> 8)) & 0x0000FFFFu;
return e | (o << 16);
}
inline uint32_t w4_unpack(uint32_t w) {
uint32_t e = w & 0xFFFFu, o = w >> 16;
e = (e | (e << 8)) & 0x00FF00FFu;
e = (e | (e << 4)) & 0x0F0F0F0Fu;
o = (o | (o << 8)) & 0x00FF00FFu;
o = (o | (o << 4)) & 0x0F0F0F0Fu;
return e | (o << 4);
}
void tile_codes(const uint8_t* rows, uint32_t* t, int64_t N, int64_t K, bool to_tiled) {
const int64_t rb = K / 2;
for (int64_t n = 0; n < N; ++n) {
uint8_t* r = const_cast<uint8_t*>(rows) + n * rb;
for (int64_t d = 0; d < K / 8; ++d) {
uint32_t x;
if (to_tiled) {
std::memcpy(&x, r + 4 * d, 4);
t[w4t_dword(n, d, K)] = w4_pack(x);
} else {
x = w4_unpack(t[w4t_dword(n, d, K)]);
std::memcpy(r + 4 * d, &x, 4);
}
}
}
}
/* The scale plane (f16) and the zero plane (u4 or u8), [N, G] each, to the stored dwords and back. */
void tile_sz(const RadTensor& sp, const RadTensor& zp, uint32_t* t, int64_t N, int64_t G, bool to_tiled) {
const int64_t srb = rad_enc_row_bytes(sp.dtype, G), zrb = rad_enc_row_bytes(zp.dtype, G);
const int zbits = zp.dtype == RAD_U8 ? 8 : 4;
for (int64_t n = 0; n < N; ++n) {
uint16_t* srow = (uint16_t*)((char*)sp.data + n * srb);
uint8_t* zrow = (uint8_t*)zp.data + n * zrb;
for (int64_t g = 0; g < G; ++g) {
uint32_t& w = t[w4s_dword(n, g, G)];
if (to_tiled) {
const uint32_t z = rad_load_bits(zrow, zbits, g);
w = (uint32_t)srow[g] | (uint32_t)rad_f32_to_f16(1024.0f + (float)z) << 16;
} else {
srow[g] = (uint16_t)(w & 0xFFFFu);
rad_store_bits(zrow, zbits, g, (uint32_t)((int32_t)rad_f16_to_f32((uint16_t)(w >> 16)) - 1024));
}
}
}
}
bool codes_sel(const RadEncoding* enc, const int* sel, int n) {
return n == 1 && enc && sel[0] >= 0 && sel[0] < enc->n_planes &&
std::strncmp(enc->plane[sel[0]].role, "codes", RAD_ENC_STR) == 0 && enc->plane[sel[0]].dtype == RAD_U4;
}
} // namespace
/* ------------------------------------------------------------------ the layout hooks */
/* Operand 2 (the codes) and 3 (b_scale) tiled where w4_tiled holds; elsewhere libref's forms,
* through the zero-point grids' hook (gemm_zp_layout), which also validates b_scale's planes. */
int gemm_w4_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, RadLayout* out) {
if (w4_kind(rad_param_gets(p, n_p, "dtype", nullptr)) == W4::None) return RAD_E_DTYPE;
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);
const bool tl = w4_tiled(N, K, group);
if (operand == 2) {
if (!codes_sel(enc, sel, n) || !tl) return RAD_E_UNSUPPORTED; /* stored as it is */
if (planes[0].shape[0] != N || planes[0].shape[1] != K) return RAD_E_SHAPE;
out->tag = kTagC;
out->dtype = RAD_U4;
out->rank = 1;
out->shape[0] = N * K;
out->align = 0;
out->bytes = N * K / 2;
return RAD_OK;
}
if (operand != 3) return RAD_E_UNSUPPORTED;
const int st = gemm_zp_layout(p, n_p, operand, enc, sel, planes, n, out);
if (st != RAD_OK || !tl) return st;
const int64_t G = K / group;
if (planes[0].shape[0] != N || planes[0].shape[1] != G || planes[1].shape[0] != N || planes[1].shape[1] != G)
return RAD_E_SHAPE;
if (planes[0].dtype != RAD_F16 || (planes[1].dtype != RAD_U4 && planes[1].dtype != RAD_U8)) return RAD_E_DTYPE;
out->tag = kTagS;
out->dtype = RAD_U32;
out->rank = 1;
out->shape[0] = N * G;
out->align = 0;
out->bytes = N * G * 4;
return RAD_OK;
}
int gemm_w4_relayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* planes, int n, void* dst, int64_t dst_bytes) {
RadLayout lay{};
const int st = gemm_w4_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
if (!lay.tag || (std::strcmp(lay.tag, kTagC) && std::strcmp(lay.tag, kTagS)))
return gemm_zp_relayout(p, n_p, operand, enc, sel, planes, n, dst, dst_bytes);
if (dst_bytes != lay.bytes) return RAD_E_SHAPE;
const int64_t N = planes[0].shape[0];
if (operand == 2) tile_codes((const uint8_t*)planes[0].data, (uint32_t*)dst, N, planes[0].shape[1], true);
else tile_sz(planes[0], planes[1], (uint32_t*)dst, N, planes[0].shape[1], true);
return RAD_OK;
}
int gemm_w4_unrelayout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const void* src, int64_t src_bytes, const RadTensor* planes, int n) {
RadLayout lay{};
const int st = gemm_w4_layout(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
if (!lay.tag || (std::strcmp(lay.tag, kTagC) && std::strcmp(lay.tag, kTagS)))
return gemm_zp_unrelayout(p, n_p, operand, enc, sel, src, src_bytes, planes, n);
if (src_bytes != lay.bytes) return RAD_E_SHAPE;
const int64_t N = planes[0].shape[0];
uint32_t* t = (uint32_t*)const_cast<void*>(src);
if (operand == 2) tile_codes((const uint8_t*)planes[0].data, t, N, planes[0].shape[1], false);
else tile_sz(planes[0], planes[1], t, N, planes[0].shape[1], false);
return RAD_OK;
}
/* ------------------------------------------------------------------ the call */
namespace {
struct W4Call {
W4 kind = W4::None;
int64_t M = 0, N = 0, K = 0, group = 0, G = 0;
bool tl = false;
const char* ab = nullptr;
uint32_t adt = 0;
int64_t ars = 0;
const char* sab = nullptr; /* w4a8i8_asym's activation scales */
uint32_t sadt = 0;
int64_t sa_rs = 0, sa_cs = 0;
const uint8_t* bb = nullptr;
const char* bsb = nullptr;
char* yb = nullptr;
uint32_t ydt = 0;
int64_t ys_r = 0, ys_c = 0;
};
int w4_parse(const RadArgs* a, W4Call* c) {
if (!a || a->n_t < 5 || !a->t[0].data || !a->t[2].data || !a->t[4].data) return RAD_E_INVAL;
const RadTensor *A = &a->t[0], *B = &a->t[2], *Y = &a->t[4];
const RadTensor* AS = rad_arg_in(a, 1);
const RadTensor* BS = rad_arg_in(a, 3);
c->kind = w4_kind(rad_args_gets_or(a, "dtype", nullptr));
if (c->kind == W4::None)
return refuse("gemm_nt_q (4-bit zero-point): dtype '%s' is not served (w4a16_asym, w4a8i8_asym are)",
rad_args_gets_or(a, "dtype", ""));
if (rad_arg_in(a, 5) || rad_arg_in(a, 6)) return refuse("gemm_nt_q (4-bit zero-point): a_sum and b_ref are other grids'");
if (rad_args_geti_or(a, "pb1", 0) > 0 || rad_args_geti_or(a, "pb2", 0) > 0)
return refuse("gemm_nt_q (4-bit zero-point): parts (pb1, pb2) are the rotated grids'");
c->K = rad_args_geti_or(a, "K", B->rank >= 2 ? B->shape[B->rank - 1] : 0);
c->N = rad_args_geti_or(a, "N", B->rank >= 2 ? B->shape[B->rank - 2] : 0);
c->group = rad_args_geti_or(a, "group", 0);
if (c->K <= 0 || c->N <= 0 || c->group <= 0 || c->K % c->group) return RAD_E_SHAPE;
c->G = c->K / c->group;
c->M = rad_tensor_numel(Y) / c->N;
if (c->M <= 0 || rad_tensor_numel(A) < c->M * c->K || rad_tensor_numel(B) < c->N * c->K) return RAD_E_SHAPE;
if (!BS) return RAD_E_INVAL;
if (B->dtype != RAD_U4) return RAD_E_DTYPE;
if (!rad_tensor_is_contiguous(B)) return RAD_E_STRIDE;
if (A->rank && A->stride[A->rank - 1] != 1) return RAD_E_STRIDE;
if (c->kind == W4::F16) {
if (A->dtype != RAD_F16 && A->dtype != RAD_BF16 && A->dtype != RAD_F32) return RAD_E_DTYPE;
} else {
if (A->dtype != RAD_I8) return RAD_E_DTYPE;
if (!AS || rad_tensor_numel(AS) < c->M * c->G) return RAD_E_INVAL;
c->sab = (const char*)AS->data;
c->sadt = AS->dtype;
c->sa_rs = row_stride(AS, c->G);
c->sa_cs = AS->rank ? AS->stride[AS->rank - 1] : 1;
}
c->tl = w4_tiled(c->N, c->K, c->group);
c->ab = (const char*)A->data;
c->adt = A->dtype;
c->ars = row_stride(A, c->K);
c->bb = (const uint8_t*)B->data;
c->bsb = (const char*)BS->data;
c->yb = (char*)Y->data;
c->ydt = Y->dtype;
c->ys_r = row_stride(Y, c->N);
c->ys_c = Y->rank ? Y->stride[Y->rank - 1] : 1;
return RAD_OK;
}
/* libref's loop for output (m, n), on the device or the host, from either stored form. */
struct W4Ref {
W4Call c;
inline uint32_t code(int64_t n, int64_t k) const {
return c.tl ? w4t_code((const uint32_t*)c.bb, n, k, c.K) : rad_load_bits(c.bb + n * (c.K / 2), 4, k);
}
inline float out(int64_t m, int64_t n) const {
DPlane sc, zp;
if (!c.tl) {
sc = find_plane(c.bsb, "scale");
zp = find_plane(c.bsb, "zero");
if (!sc.p || !zp.p) return 0.0f; /* not a joined b_scale: the layout hook guarantees one */
}
float acc = 0.0f;
for (int64_t g = 0; g < c.G; ++g) {
float sb;
int32_t z;
if (c.tl) {
const uint32_t w = ((const uint32_t*)c.bsb)[w4s_dword(n, g, c.G)];
sb = rad_f16_to_f32((uint16_t)(w & 0xFFFFu));
z = (int32_t)rad_f16_to_f32((uint16_t)(w >> 16)) - 1024;
} else {
sb = sc.at(n, g);
z = (int32_t)zp.at(n, g);
}
const int64_t k0 = g * c.group;
if (c.kind == W4::F16) {
float sub = 0.0f;
for (int64_t k = k0; k < k0 + c.group; ++k)
sub += rad_load_f32(c.ab, c.adt, m * c.ars + k) * (float)((int32_t)code(n, k) - z);
acc += sub * sb;
} else {
int64_t is = 0;
const int8_t* ar = (const int8_t*)c.ab + m * c.ars;
for (int64_t k = k0; k < k0 + c.group; ++k) is += (int64_t)ar[k] * ((int32_t)code(n, k) - z);
const float sub = (float)is;
acc += sub * rad_load_f32(c.sab, c.sadt, m * c.sa_rs + g * c.sa_cs) * sb;
}
}
return acc;
}
};
/* XE2_W4_CHECK=n: the first n calls of each form, against libref's loop on the host. */
int check_budget() {
static const int n = [] {
const char* e = std::getenv("XE2_W4_CHECK");
return e ? std::atoi(e) : 0;
}();
return n;
}
void check_call(sycl::queue& q, const W4Call& c, const char* form, bool exact, std::atomic<int>& calls) {
if (calls.fetch_add(1) >= check_budget() || !c.tl) return;
q.wait();
const int64_t M = c.M, N = c.N, K = c.K, G = c.G;
auto pull = [&](const void* p, size_t bytes) {
std::vector<uint8_t> h(bytes);
q.memcpy(h.data(), p, bytes).wait();
return h;
};
const size_t ae = c.kind == W4::F16 ? (c.adt == RAD_F32 ? 4 : 2) : 1;
const std::vector<uint8_t> A = pull(c.ab, (size_t)((M - 1) * c.ars + K) * ae);
const std::vector<uint8_t> C = pull(c.bb, (size_t)(N * K / 2));
const std::vector<uint8_t> S = pull(c.bsb, (size_t)(N * G * 4));
std::vector<uint8_t> SA;
if (c.kind == W4::I8G) SA = pull(c.sab, (size_t)((M - 1) * c.sa_rs + (G - 1) * c.sa_cs + 1) * 4);
const size_t ye = c.ydt == RAD_F32 ? 4 : 2;
const std::vector<uint8_t> Yv = pull(c.yb, (size_t)((M - 1) * c.ys_r + (N - 1) * c.ys_c + 1) * ye);
W4Ref r{c};
r.c.ab = (const char*)A.data();
r.c.bb = C.data();
r.c.bsb = (const char*)S.data();
r.c.sab = (const char*)SA.data();
/* about 2^19 outputs: every row up to 64 of them, columns a stride apart */
int64_t seen = 0, differ = 0;
double e2 = 0, r2 = 0, emax = 0, rmax = 0;
const int64_t mstep = std::max<int64_t>(1, M / 64);
const int64_t nstep = std::max<int64_t>(1, (M / mstep) * N / (1 << 19)) | 1;
for (int64_t m = 0; m < M; m += mstep)
for (int64_t n = 0; n < N; n += nstep) {
const float ref = round_dt(c.ydt, r.out(m, n)); /* libref's answer as y holds it */
const float got = rad_load_f32(Yv.data(), c.ydt, m * c.ys_r + n * c.ys_c);
++seen;
if (std::memcmp(&got, &ref, 4) != 0) ++differ;
e2 += ((double)got - ref) * ((double)got - ref);
r2 += (double)ref * ref;
emax = std::max(emax, std::fabs((double)got - ref));
rmax = std::max(rmax, std::fabs((double)ref));
}
std::fprintf(stderr, "libxe2: w4 check %s M=%lld N=%lld K=%lld: %lld of %lld outputs differ from libref's loop%s, "
"rel_l2 %.3e, max |d| %.3e of max |ref| %.3e\n",
form, (long long)M, (long long)N, (long long)K, (long long)differ, (long long)seen,
exact ? " (an exact form: must be 0)" : "", r2 > 0 ? std::sqrt(e2 / r2) : 0.0, emax, rmax);
}
/* The decode kernel's splits of K a tile: the divisor of G closest to G / 8, about 8 groups a thread
* (probe w4d.cpp: G = 40 best at 5, G = 136 at 8-17, G = 48 at 4-8); a tile's splits are one
* work-group, at most 32. */
int w4_splits(int64_t G) {
if (const char* e = std::getenv("XE2_W4_SPLITS")) {
const int64_t s = std::clamp<int64_t>(std::atoi(e), 1, G);
if (G % s == 0 && s <= 32) return (int)s;
}
int best = 1;
for (int64_t s = 1; s <= 32 && s <= G; ++s)
if (G % s == 0 && std::llabs(G / s - 8) <= std::llabs(G / best - 8)) best = (int)s;
return best;
}
std::atomic<int> g_check_exact{0}, g_check_a16{0}, g_check_a8{0};
} // namespace
/* ------------------------------------------------------------------ the exact form */
int gemm_nt_q_w4(const RadArgs* a, RadStream st) {
W4Call c;
if (int rc = w4_parse(a, &c); rc != RAD_OK) return rc;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const W4Ref r{c};
const size_t wn = 64;
const size_t nn = (size_t)((c.N + (int64_t)wn - 1) / (int64_t)wn) * wn;
const int rc = submit([&] {
syclex::nd_launch(*q, sycl::nd_range<2>{{(size_t)c.M, nn}, {1, wn}}, [=](sycl::nd_item<2> it) {
const int64_t m = (int64_t)it.get_global_id(0), n = (int64_t)it.get_global_id(1);
if (n >= r.c.N) return;
rad_store_f32(r.c.yb, r.c.ydt, m * r.c.ys_r + n * r.c.ys_c, r.out(m, n));
});
});
if (rc == RAD_OK) check_call(*q, c, "exact", true, g_check_exact);
return rc;
}
/* ------------------------------------------------------------------ w4a16_asym, M <= 64 */
int gemm_nt_q_w4a16(const RadArgs* a, RadStream st) {
if (exact_only() || std::getenv("XE2_W4_EXACT")) return gemm_nt_q_w4(a, st);
W4Call c;
if (int rc = w4_parse(a, &c); rc != RAD_OK) return rc;
/* A bf16 activation (arch/common since upstream 492d33c, which dropped the separate cast) is
* narrowed to f16 round-to-nearest-even at the kernel's activation load: the cast it replaced,
* bit for bit. */
const bool a_bf16 = c.adt == RAD_BF16;
if (c.kind != W4::F16 || !c.tl || (c.adt != RAD_F16 && !a_bf16) || c.M > 64 || c.ys_c != 1 ||
(c.ydt != RAD_BF16 && c.ydt != RAD_F32) || (c.ars * 2) % 16 || !rad_ptr_aligned(c.ab, 64) ||
!rad_ptr_aligned(c.bb, 64) || !rad_ptr_aligned(c.bsb, 64))
return gemm_nt_q_w4(a, st);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const uint16_t* a16 = (const uint16_t*)c.ab;
const int64_t ars = c.ars;
const int splits = w4_splits(c.G);
/* blocks of 16 rows (two 8-row bands on each decoded group), a tail of 8 or fewer as before;
* XE2_W4_RB8=1 keeps 8-row blocks */
static const bool rb8 = std::getenv("XE2_W4_RB8") != nullptr;
for (int64_t m0 = 0; m0 < c.M;) {
const int64_t left = c.M - m0;
const int mm = (int)std::min<int64_t>(rb8 || left <= 8 ? 8 : 16, left);
const int rcount = mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : (mm <= 8 ? 8 : 16)));
const int rc = w4a16_esimd(*q, rcount, a16 + m0 * ars, a_bf16, ars, mm, (const uint32_t*)c.bb, (const uint32_t*)c.bsb,
c.N, c.K, splits, c.yb + m0 * c.ys_r * (c.ydt == RAD_F32 ? 4 : 2), c.ydt, c.ys_r);
if (rc != RAD_OK) return rc;
m0 += mm;
}
check_call(*q, c, "w4a16 fp16 DPAS", false, g_check_a16);
return RAD_OK;
}
/* ------------------------------------------------------------------ w4a8i8_asym */
/* The activation's prepass: quant_act_i8g's row-major codes with each 8 bytes reordered evens first
* (the u4 B operand's nibble order, w4_tiled), each (row, group)'s sum of codes (exact, for the
* zero), and the row scales, both transposed to [G][mpad] so a thread's rows of a group are one
* block load. A sub-group a (row, group), a lane 8 codes. */
int gemm_nt_q_w4a8i8(const RadArgs* a, RadStream st) {
if (exact_only() || std::getenv("XE2_W4_EXACT")) return gemm_nt_q_w4(a, st);
W4Call c;
if (int rc = w4_parse(a, &c); rc != RAD_OK) return rc;
if (c.kind != W4::I8G || !c.tl || c.N % 32 || c.ys_c != 1 || (c.ydt != RAD_BF16 && c.ydt != RAD_F32) ||
c.ars % 16 || c.ars < c.K || !rad_ptr_aligned(c.ab, 8) || !rad_ptr_aligned(c.bb, 64) ||
!rad_ptr_aligned(c.bsb, 64))
return gemm_nt_q_w4(a, st);
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
const int64_t M = c.M, K = c.K, G = c.G, mpad = (M + 31) / 32 * 32;
float* ws0 = workspace(*q, (size_t)((mpad * K + 3) / 4), 4);
float* ws1 = workspace(*q, (size_t)(2 * G * mpad), 5);
if (!ws0 || !ws1) return RAD_E_DEVICE;
uint8_t* ap = (uint8_t*)ws0;
int32_t* asumt = (int32_t*)ws1;
float* sat = ws1 + G * mpad;
const int8_t* ab = (const int8_t*)c.ab;
const int64_t ars = c.ars;
const char* sab = c.sab;
const uint32_t sadt = c.sadt;
const int64_t sa_rs = c.sa_rs, sa_cs = c.sa_cs;
int rc = submit([&] {
syclex::nd_launch(*q, sycl::nd_range<1>{(size_t)((M * G + 15) / 16 * 16) * kSub, 256},
[=](sycl::nd_item<1> it) [[sycl::reqd_sub_group_size(kSub)]] {
const sycl::sub_group sg = it.get_sub_group();
const int64_t i = (int64_t)it.get_global_id(0) / kSub;
if (i >= M * G) return; /* uniform over the sub-group */
const int64_t m = i / G, g = i % G;
const int lane = (int)sg.get_local_linear_id();
const uint64_t v = *(const uint64_t*)(ab + m * ars + g * 128 + lane * 8);
uint64_t o = 0;
int32_t s = 0;
for (int u = 0; u < 8; ++u) {
const uint64_t b = (v >> (8 * u)) & 0xFFu;
o |= b << (8 * ((u & 1) * 4 + (u >> 1)));
s += (int32_t)(int8_t)b;
}
*(uint64_t*)(ap + m * K + g * 128 + lane * 8) = o;
s = sycl::reduce_over_group(sg, s, sycl::plus<int32_t>());
if (lane == 0) {
asumt[g * mpad + m] = s;
sat[g * mpad + m] = rad_load_f32(sab, sadt, m * sa_rs + g * sa_cs);
}
});
});
if (rc != RAD_OK) return rc;
rc = w4a8i8_esimd(*q, ap, asumt, sat, mpad, (const uint32_t*)c.bb, (const uint32_t*)c.bsb, M, c.N, K, c.yb,
c.ydt, c.ys_r);
if (rc == RAD_OK) check_call(*q, c, "w4a8i8 int8 DPAS", true, g_check_a8);
return rc;
}
} // namespace xe2