libxe2: the int4 W4A16 release's GEMMs -- w4a16_asym on fp16 DPAS, w4a8i8_asym on int8 DPAS, quant_act_i8g

For qwen3.8-27b-w4a16-rtn (u4 codes, f16 scale and u4 zero per 128 of K): the engine casts a
linear's input to f16 for gemm_nt_q at w4a16_asym up to 64 rows a step, and quantises it with
quant_act_i8g for w4a8i8_asym above. None had a device kernel.

- The planes are stored tiled at load (xe2_ops.h, w4_tiled): a 16-column tile's codes side by
  side, the even codes of each dword in its low half so (w >> 4s) & 0x000F000F is a VNNI pair, and
  the scale with f16(1024 + zero) one dword a (column, group). unrelayout inverts both exactly.
- gemm_nt_q_w4: libref's gemm_zp loop per output, the exact form (XE2_EXACT=1, XE2_W4_EXACT=1).
- w4a16_asym, M <= 64: ESIMD, a thread a tile; (code | 0x6400) less f16(1024 + zero) is
  code - zero exactly; eight fp16 DPAS of RC = 1, 2, 4 or 8 rows a group, scaled in f32 a group.
  Narrow outputs split K. Probe w4d.cpp, incompressible codes: gate_up (N 34816, K 5120) 173 us at
  M = 1 (546 GB/s), 190 us at M = 4.
- w4a8i8_asym: int8 DPAS takes the tiled codes as its u4 B operand (nibble i meets A's byte i), so
  a prepass reorders the activation's bytes of each 8 evens first and forms each group's sum of
  codes; the group's integer sum is exact and its term ((sum - zero * asum) * a_scale) * scale is
  added in group order.
- quant_act_i8g: quant_act_fp8's form at libref's int8 rule (top 127).

rad-kbench and the op oracle cannot draw the zero-point grids, so XE2_W4_CHECK=n checks the first
n calls of each form against libref's loop on the host, on the engine's operands. On the 27B: the
exact form 0 of 81920 and 0 of 25600 outputs differ; w4a8i8 (M = 2048) 0 of 349568 and 0 of 327680;
w4a16 (M = 4) 0 of 65536 and 2 of 20480 (bf16 ties, rel_l2 5e-9). quant_act_i8g: rad-kbench
fixture 153/153 (quant_act*), the 27B 96/96, rel_l2 0. ctest -L sycl 3/3; the host tier's five
failures are the baseline's (FFmpeg, CPU budgets).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
claudeandClaude Opus 5.5 committed 2026-10-28 00:06:22 +01:00
1 parent ab0175564b
commit efdfc4a570
7 files changed
+892 -48

No files matched your search

+493
View File
@@ -0,0 +1,493 @@
/* 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 sycl::half* a, int64_t ars, int mm, const uint32_t* ct,
const uint32_t* sz, int64_t N, int64_t K, int splits, float* pbuf, int64_t prow, int64_t pm,
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 split-K partials [splits][pm][N] summed in split order into y. */
int reduce_splits(sycl::queue& q, const float* pbuf, int splits, int64_t M, int64_t N, char* yb, uint32_t ydt,
int64_t ys_r, int64_t ys_c) {
return submit([&] {
syclex::nd_launch(q, sycl::nd_range<1>{(size_t)((M * N + 255) / 256 * 256), 256}, [=](sycl::nd_item<1> it) {
const int64_t i = (int64_t)it.get_global_id(0);
if (i >= M * N) return;
const int64_t m = i / N, n = i % N;
float s = 0.0f;
for (int sp = 0; sp < splits; ++sp) s += pbuf[(sp * M + m) * N + n];
rad_store_f32(yb, ydt, m * ys_r + n * ys_c, s);
});
});
}
/* Threads a 16-column tile for the decode kernel: enough tiles alone fill the card's 1280 thread
* slots; narrower outputs split K (probe w4d.cpp: N = 5120 best at two, N = 1024 at ten). */
int w4_splits(int64_t N, int64_t G) {
if (const char* e = std::getenv("XE2_W4_SPLITS")) return (int)std::clamp<int64_t>(std::atoi(e), 1, G);
const int64_t ntile = N / 16;
int64_t s = ntile >= 640 ? 1 : (ntile >= 256 ? 2 : std::min<int64_t>(10, (1280 + ntile - 1) / ntile));
return (int)std::clamp<int64_t>(s, 1, G);
}
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;
if (c.kind != W4::F16 || !c.tl || c.adt != RAD_F16 || 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 int splits = w4_splits(c.N, c.G);
float* pbuf = nullptr;
if (splits > 1) {
pbuf = workspace(*q, (size_t)(splits * c.M * c.N), 3);
if (!pbuf) return RAD_E_DEVICE;
}
const sycl::half* a16 = (const sycl::half*)c.ab;
for (int64_t m0 = 0; m0 < c.M; m0 += 8) {
const int mm = (int)std::min<int64_t>(8, c.M - m0);
const int rcount = mm == 1 ? 1 : (mm <= 2 ? 2 : (mm <= 4 ? 4 : 8));
const int rc = w4a16_esimd(*q, rcount, a16 + m0 * c.ars, c.ars, mm, (const uint32_t*)c.bb,
(const uint32_t*)c.bsb, c.N, c.K, splits, pbuf, m0, c.M,
c.yb + m0 * c.ys_r * (c.ydt == RAD_F32 ? 4 : 2), c.ydt, c.ys_r);
if (rc != RAD_OK) return rc;
}
if (splits > 1) {
if (int rc = reduce_splits(*q, pbuf, splits, c.M, c.N, c.yb, c.ydt, c.ys_r, c.ys_c); rc != RAD_OK) return rc;
}
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
+247
View File
@@ -0,0 +1,247 @@
/* xe2_gemm_w4_esimd.cpp -- the 4-bit zero-point GEMMs in ESIMD, over the planes stored tiled
* (xe2_ops.h, w4_tiled; xe2_gemm_w4.cpp has the forms and their checks).
*
* w4a16_esimd, M <= 64 in blocks of 8 rows: a thread owns a 16-column tile and walks its split of K
* a group (128 columns) at a time -- the group's codes are 1 KiB, four block loads. Each dword w of
* eight codes decodes into four VNNI pairs, ((w >> 4s) & 0x000F000F) | 0x64006400 being the pair
* (1024 + code) in fp16, and one 32-wide fp16 subtract of the column's 1024 + zero leaves
* code - zero, exact. Eight fp16 DPAS a group, RC rows of the activation each (RC = 1, 2, 4 or 8:
* the DPAS repeat count, so a decode row costs one row of the systolic array, not eight), sum the
* group in f32, which is scaled by the column's scale once and added to the total. The activation
* comes from row loads for RC <= 4 and from 2D block loads at RC = 8, which return the DPAS A
* operand as it lies (probe w4d.cpp: M = 8 at gate_up's shape 466 -> 210 us; at M = 4 row loads
* were faster). At RC = 1 a group's codes are loaded before the previous group is computed (narrow
* outputs: N = 5120, K = 6144 82 -> 35 us; at RC > 1 the extra registers cost more than it hid).
* Narrow outputs split K (the caller's choice); partials go to pbuf for its reduction.
*
* w4a8i8_esimd, any M: int8 DPAS with the tiled codes as its u4 B operand, 8 codes a dword, against
* the prepass's activation (bytes of each 8 evens first, the codes' nibble order). A thread owns 32
* rows by 32 columns, four 8-row blocks by two tiles: a group is four K-32 steps of eight DPAS into
* int32 -- exact -- and then each output's term ((sum - zero * asum) * a_scale) * scale is added to
* its f32 total in group order: libref's result bit for bit. */
#include "xe2_common.h"
#include <sycl/ext/intel/esimd.hpp>
namespace xe2 {
namespace {
namespace esimd = sycl::ext::intel::esimd;
namespace xmx = sycl::ext::intel::esimd::xmx;
using half = sycl::half;
/* f32 to bf16, rounded to nearest even as rad_f32_to_bf16; NaN quieted */
inline esimd::simd<uint16_t, 16> to_bf16(esimd::simd<float, 16> v) SYCL_ESIMD_FUNCTION {
using namespace esimd;
simd<uint32_t, 16> u = v.template bit_cast_view<uint32_t>();
simd<uint32_t, 16> h = (u + 0x7FFFu + ((u >> 16) & 1u)) >> 16;
h.merge((u >> 16) | 0x0040u, (u & 0x7FFFFFFFu) > 0x7F800000u);
return simd<uint16_t, 16>(h);
}
template <int RC, bool PF, bool A2D>
int launch16(sycl::queue& q, const half* a, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz, int64_t N,
int64_t K, int splits, float* pbuf, int64_t prow, int64_t pm, char* yb, uint32_t ydt, int64_t yrs) {
const int64_t ntile = N / 16, G = K / 128;
const int64_t per = (G + splits - 1) / splits;
const int64_t nthr = ntile * splits;
constexpr int kWg = 8;
const int64_t ng = (nthr + kWg - 1) / kWg;
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(ng * kWg), (size_t)kWg}, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
const int64_t tid = (int64_t)it.get_global_id(0);
if (tid >= nthr) return;
const int64_t tile = tid % ntile, sp = tid / ntile;
const int64_t g0 = sp * per, g1 = G < g0 + per ? G : g0 + per;
const uint32_t* cb = ct + tile * (K / 8) * 16;
const uint32_t* sb = sz + tile * G * 16;
const unsigned sw = (unsigned)(K * 2 - 1), sh = (unsigned)(mm - 1), spt = (unsigned)(ars * 2 - 1);
auto ldc = [&](int64_t g, simd<uint32_t, 256>& cw, simd<uint32_t, 16>& szw) SYCL_ESIMD_FUNCTION {
#pragma unroll
for (int qd = 0; qd < 4; ++qd) cw.template select<64, 1>(qd * 64) = block_load<uint32_t, 64>(cb + g * 256 + qd * 64);
szw = block_load<uint32_t, 16>(sb + g * 16);
};
simd<float, RC * 16> tot = 0.0f;
simd<uint32_t, 256> cw;
simd<uint32_t, 16> szw;
if (g0 < g1) ldc(g0, cw, szw);
for (int64_t g = g0; g < g1; ++g) {
simd<uint32_t, 256> cwn;
simd<uint32_t, 16> szn;
if constexpr (PF) {
if (g + 1 < g1) ldc(g + 1, cwn, szn);
}
simd<half, RC * 128> Ar; /* A2D: [pair of steps][step][row][16]; else [row][128] */
if constexpr (A2D) {
#pragma unroll
for (int j2 = 0; j2 < 4; ++j2)
Ar.template select<RC * 32, 1>(j2 * RC * 32) =
load_2d<half, 16, RC, 2, false>(a, sw, sh, spt, (int)(g * 128 + j2 * 32), 0);
} else {
#pragma unroll
for (int m = 0; m < RC; ++m) {
if (m < mm) Ar.template select<128, 1>(m * 128) = block_load<half, 128>(a + m * ars + g * 128);
else Ar.template select<128, 1>(m * 128) = 0;
}
}
simd<uint32_t, 16> zzd = (szw >> 16) | (szw & 0xFFFF0000u);
const simd<half, 32> zz = zzd.template bit_cast_view<half>();
simd<float, RC * 16> cg = 0.0f;
#pragma unroll
for (int j = 0; j < 8; ++j) {
simd<half, 256> B;
#pragma unroll
for (int t = 0; t < 2; ++t) {
const simd<uint32_t, 16> w = cw.template select<16, 1>((2 * j + t) * 16);
#pragma unroll
for (int s = 0; s < 4; ++s) {
simd<uint32_t, 16> d = ((w >> (4 * s)) & 0x000F000Fu) | 0x64006400u;
B.template select<32, 1>((4 * t + s) * 32) = d.template bit_cast_view<half>() - zz;
}
}
simd<half, RC * 16> A;
if constexpr (A2D) {
A = Ar.template select<RC * 16, 1>((j / 2) * RC * 32 + (j & 1) * RC * 16);
} else {
#pragma unroll
for (int m = 0; m < RC; ++m) A.template select<16, 1>(m * 16) = Ar.template select<16, 1>(m * 128 + 16 * j);
}
cg = xmx::dpas<8, RC, float>(cg, B, A);
}
simd<uint32_t, 16> lo = szw & 0xFFFFu;
simd<uint16_t, 16> s16 = lo;
const simd<float, 16> sc = simd<half, 16>(s16.template bit_cast_view<half>());
#pragma unroll
for (int m = 0; m < RC; ++m) tot.template select<16, 1>(m * 16) += cg.template select<16, 1>(m * 16) * sc;
if constexpr (PF) {
cw = cwn;
szw = szn;
} else if (g + 1 < g1) {
ldc(g + 1, cw, szw);
}
}
const int64_t n0 = tile * 16;
#pragma unroll
for (int m = 0; m < RC; ++m) {
if (m >= mm) break;
const simd<float, 16> v = tot.template select<16, 1>(m * 16);
if (splits > 1) {
block_store<float, 16>(pbuf + ((sp * pm) + prow + m) * N + n0, v);
} else if (ydt == RAD_F32) {
block_store<float, 16>((float*)yb + m * yrs + n0, v);
} else {
block_store<uint16_t, 16>((uint16_t*)yb + m * yrs + n0, to_bf16(v));
}
}
});
});
}
int launch_i8(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) {
const int64_t G = K / 128, RG = mpad / 32, CG = N / 32;
const int64_t nthr = RG * CG;
constexpr int kWg = 8;
const int64_t ng = (nthr + kWg - 1) / kWg;
namespace intelex = sycl::ext::intel::experimental;
syclex::properties props{intelex::grf_size<256>};
return submit([&] {
q.parallel_for(sycl::nd_range<1>{(size_t)(ng * kWg), (size_t)kWg}, props, [=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {
using namespace esimd;
const int64_t t = (int64_t)it.get_global_id(0);
if (t >= nthr) return;
const int64_t rg = t % RG, cg = t / RG;
const int64_t r0 = rg * 32, tile0 = cg * 2;
const unsigned sw = (unsigned)(K - 1), sh = (unsigned)(M - 1);
simd<float, 128> acc[4][2];
#pragma unroll
for (int i = 0; i < 4; ++i)
#pragma unroll
for (int j = 0; j < 2; ++j) acc[i][j] = 0.0f;
for (int64_t g = 0; g < G; ++g) {
simd<int32_t, 128> c[4][2];
#pragma unroll
for (int i = 0; i < 4; ++i)
#pragma unroll
for (int j = 0; j < 2; ++j) c[i][j] = 0;
#pragma unroll
for (int ks = 0; ks < 4; ++ks) {
/* 32 rows of the step's 32 bytes: [row][8 dwords], row block i its 256 bytes */
simd<uint32_t, 256> A4 = load_2d<uint32_t, 8, 32, 1, false>((const uint32_t*)ap, sw, sh, sw,
(int)((g * 128 + ks * 32) / 4), (int)r0);
simd<uint32_t, 64> B[2];
#pragma unroll
for (int j = 0; j < 2; ++j)
B[j] = block_load<uint32_t, 64>(ct + ((tile0 + j) * (K / 8) + g * 16 + ks * 4) * 16);
#pragma unroll
for (int i = 0; i < 4; ++i) {
const simd<uint32_t, 64> Ai = A4.template select<64, 1>(i * 64);
#pragma unroll
for (int j = 0; j < 2; ++j)
c[i][j] = xmx::dpas<8, 8, int32_t, int32_t, uint32_t, uint32_t, xmx::dpas_argument_type::u4,
xmx::dpas_argument_type::s8>(c[i][j], B[j], Ai);
}
}
simd<int32_t, 32> as = block_load<int32_t, 32>(asumt + g * mpad + r0);
simd<float, 32> sv = block_load<float, 32>(sat + g * mpad + r0);
#pragma unroll
for (int j = 0; j < 2; ++j) {
simd<uint32_t, 16> w = block_load<uint32_t, 16>(sz + ((tile0 + j) * G + g) * 16);
simd<uint32_t, 16> lo = w & 0xFFFFu, hi = w >> 16;
simd<uint16_t, 16> s16 = lo, z16 = hi;
const simd<float, 16> sb = simd<half, 16>(s16.template bit_cast_view<half>());
const simd<int32_t, 16> z = simd<int32_t, 16>(simd<float, 16>(simd<half, 16>(z16.template bit_cast_view<half>()))) - 1024;
#pragma unroll
for (int i = 0; i < 4; ++i)
#pragma unroll
for (int m = 0; m < 8; ++m) {
const int r = i * 8 + m;
const simd<int32_t, 16> is = c[i][j].template select<16, 1>(m * 16) - z * (int32_t)as[r];
const simd<float, 16> sub = is;
simd<float, 16> tt = sub * (float)sv[r];
tt = tt * sb;
acc[i][j].template select<16, 1>(m * 16) += tt;
}
}
}
#pragma unroll
for (int i = 0; i < 4; ++i)
#pragma unroll
for (int m = 0; m < 8; ++m) {
const int64_t row = r0 + i * 8 + m;
if (row >= M) continue;
#pragma unroll
for (int j = 0; j < 2; ++j) {
const int64_t n0 = (tile0 + j) * 16;
const simd<float, 16> v = acc[i][j].template select<16, 1>(m * 16);
if (ydt == RAD_F32) block_store<float, 16>((float*)yb + row * yrs + n0, v);
else block_store<uint16_t, 16>((uint16_t*)yb + row * yrs + n0, to_bf16(v));
}
}
});
});
}
} // namespace
int w4a16_esimd(sycl::queue& q, int rc, const half* a, int64_t ars, int mm, const uint32_t* ct, const uint32_t* sz,
int64_t N, int64_t K, int splits, float* pbuf, int64_t prow, int64_t pm, char* yb, uint32_t ydt,
int64_t yrs) {
switch (rc) {
case 1: return launch16<1, true, false>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
case 2: return launch16<2, false, false>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
case 4: return launch16<4, false, false>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, yrs);
default: return launch16<8, false, true>(q, a, ars, mm, ct, sz, N, K, splits, pbuf, prow, pm, yb, ydt, 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) {
return launch_i8(q, ap, asumt, sat, mpad, ct, sz, M, N, K, yb, ydt, yrs);
}
} // namespace xe2
+1 -42
View File
@@ -16,6 +16,7 @@
*/
#include "xe2_common.h"
#include "xe2_ops.h"
#include "xe2_zp.h"
#include "rad_planes.h"
@@ -54,48 +55,6 @@ bool sel_roles(const RadEncoding* enc, const int* sel, int n, const char* r0, co
std::strncmp(enc->plane[sel[1]].role, r1, RAD_ENC_STR) == 0;
}
/* One plane of the joined operand, found on the device. */
struct DPlane {
const char* p = nullptr;
uint32_t dt = 0;
int64_t row_bytes = 0;
float at(int64_t i, int64_t j) const { return rad_load_f32(p + i * row_bytes, dt, j); }
};
inline bool role_is(const char* role, const char* want) {
for (int i = 0; i < RAD_ENC_STR; ++i) {
if (role[i] != want[i]) return false;
if (!want[i]) return true;
}
return true;
}
inline DPlane find_plane(const char* base, const char* role) {
DPlane v;
const RadPlanesHeader* h = (const RadPlanesHeader*)base;
if (h->magic != RAD_PLANES_MAGIC) return v;
const RadPlanesEntry* e = (const RadPlanesEntry*)(base + sizeof(RadPlanesHeader));
for (int i = 0; i < h->n_planes && i < RAD_ENC_MAX_PLANES; ++i)
if (role_is(e[i].role, role)) {
v.p = base + e[i].offset;
v.dt = e[i].dtype;
v.row_bytes = e[i].row_bytes;
return v;
}
return v;
}
int64_t row_stride(const RadTensor* t, int64_t cols) {
if (!t || t->rank < 2) return cols;
int64_t span = 1;
for (int i = (int)t->rank - 1; i >= 0; --i) {
span *= t->shape[i];
if (span == cols) return (i > 0) ? t->stride[i - 1] : cols;
if (span > cols) break;
}
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). */
+20 -6
View File
@@ -179,8 +179,12 @@ int rmsnorm(const RadArgs* a, RadStream st) {
});
}
/* ------------------------------------------------------------------ quant_act_fp8 */
int quant_act_fp8(const RadArgs* a, RadStream st) {
/* ------------------------------------------------------------------ quant_act_fp8, quant_act_i8g */
namespace {
/* Codes and a scale amax / top per group: quant_act_fp8 (top 448, E4M3 codes) and quant_act_i8g
* (top 127, int8 codes rounded to nearest even and saturating) -- libref's q8_group either way. */
int quant_act_groups(const RadArgs* a, RadStream st, float top, const char* what) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const int64_t n = rad_args_geti_or(a, "n", 0);
if (n <= 0) return RAD_E_SHAPE;
@@ -188,11 +192,11 @@ int quant_act_fp8(const RadArgs* a, RadStream st) {
if (g <= 0 || g > n) g = n;
const int64_t ngrp = (n + g - 1) / g;
Rows x, qv, sv;
if (int rc = rows_view(&a->t[0], n, &x, "quant_act_fp8 x"); rc < 0) return rc;
if (int rc = rows_view(&a->t[0], n, &x, what); rc < 0) return rc;
const int64_t rows = x.rows;
if (rows <= 0 || rad_tensor_numel(&a->t[1]) < rows * n) return RAD_E_SHAPE;
if (int rc = rows_view(&a->t[1], n, &qv, "quant_act_fp8 q"); rc < 0) return rc;
if (int rc = scale_view(&a->t[2], rows, ngrp, &sv, "quant_act_fp8 scale"); rc < 0) return rc;
if (int rc = rows_view(&a->t[1], n, &qv, what); rc < 0) return rc;
if (int rc = scale_view(&a->t[2], rows, ngrp, &sv, what); rc < 0) return rc;
sycl::queue* q = queue_of(st);
if (!q) return RAD_E_DEVICE;
constexpr int kSubs = (int)(kWg / kSub);
@@ -208,13 +212,23 @@ int quant_act_fp8(const RadArgs* a, RadStream st) {
if (gi >= ngrp) return; /* uniform over the sub-group */
auto v = [&](int64_t i) { return rad_load_f32(x.base, x.dtype, r * x.pitch + i); };
const int64_t k0 = gi * g, len = sycl::min(g, n - k0);
const float sc = quant_group(sg, v, k0, len, 448.0f, qv.base, qv.dtype, r * qv.pitch);
const float sc = quant_group(sg, v, k0, len, top, qv.base, qv.dtype, r * qv.pitch);
if (sg.get_local_linear_id() == 0)
rad_store_f32(sv.base, sv.dtype, r * sv.pitch + gi, sc);
});
});
}
} // namespace
int quant_act_fp8(const RadArgs* a, RadStream st) { return quant_act_groups(a, st, 448.0f, "quant_act_fp8"); }
int quant_act_i8g(const RadArgs* a, RadStream st) {
if (rad_args_have(a, 3) && a->t[1].dtype != RAD_I8)
return refuse("quant_act_i8g: codes are int8, not dtype %u", a->t[1].dtype);
return quant_act_groups(a, st, 127.0f, "quant_act_i8g");
}
/* ------------------------------------------------------------------ rmsnorm_quant_fp8 */
int rmsnorm_quant_fp8(const RadArgs* a, RadStream st) {
if (!a || a->n_t < 5) return RAD_E_INVAL;
+39
View File
@@ -135,4 +135,43 @@ inline uint32_t zpt_code(const uint32_t* t, int64_t n, int64_t k, int64_t K) {
return (t[zpt_dword(n, k >> 5, p >> 4, K)] >> (2 * (p & 15))) & 3u;
}
/* The 4-bit zero-point grids' planes tiled (xe2_gemm_w4.cpp; w4a16_asym, w4a8i8_asym) where N % 16
* == 0, K % 128 == 0 and the group is 128. Codes ("xe2.w4.t16"): dword ((n >> 4) * K/8 + d) * 16 +
* (n & 15) holds column n's codes 8d .. 8d + 7, the even ones in the low half and the odd ones in
* the high half, each in order: nibble s is code 8d + 2s and nibble s + 4 code 8d + 2s + 1 (s < 4).
* So (w >> 4s) & 0x000F000F is the fp16 DPAS's VNNI pair (2s, 2s + 1) once 0x6400 is or-ed into
* each half (1024 + code, exact), and a u4 B operand of the int8 DPAS -- which meets A's byte i with
* B's nibble i -- wants the activation's bytes of each 8 evens first. A tile's 128 columns of K (a
* group) are 1 KiB. Scale and zero ("xe2.w4.sz.t16"): dword ((n >> 4) * G + g) * 16 + (n & 15) is
* the f16 scale | f16(1024 + zero) << 16, a tile's group 64 bytes. */
inline bool w4_tiled(int64_t N, int64_t K, int64_t group) {
return N > 0 && N % 16 == 0 && K > 0 && K % 128 == 0 && group == 128;
}
inline int64_t w4t_dword(int64_t n, int64_t d, int64_t K) { return (((n >> 4) * (K / 8) + d) << 4) + (n & 15); }
inline uint32_t w4t_code(const uint32_t* t, int64_t n, int64_t k, int64_t K) {
const int u = (int)(k & 7);
return (t[w4t_dword(n, k >> 3, K)] >> ((u & 1) * 16 + 4 * (u >> 1))) & 15u;
}
inline int64_t w4s_dword(int64_t n, int64_t g, int64_t G) { return (((n >> 4) * G + g) << 4) + (n & 15); }
int gemm_nt_q_w4(const RadArgs* a, RadStream s);
int gemm_nt_q_w4a16(const RadArgs* a, RadStream s);
int gemm_nt_q_w4a8i8(const RadArgs* a, RadStream s);
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);
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);
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);
int quant_act_i8g(const RadArgs* a, RadStream s);
/* The joined head xe2_head_esimd.cpp serves at M <= 8 (an int4 release's int8 head: i8 codes, one
* float scale per (row, 128 of K)); head_dpas leaves exactly these to it. Both read the planes on
* the card, so the test is shared rather than decided on the host. */
inline bool head_i8_fast(uint32_t cdt, int64_t crb, int64_t sbr, int64_t sbc, uint32_t sdt, int64_t K) {
return cdt == RAD_I8 && crb == K && K % 128 == 0 && sbr <= 1 && sbc == 128 &&
(sdt == RAD_BF16 || sdt == RAD_F16 || sdt == RAD_F32);
}
} // namespace xe2
+36
View File
@@ -25,6 +25,11 @@ const RadConstraint cFp8Gated[] = { RAD_CIN("dtype", "fp8a8 fp8a8f32 fp8a16"), R
/* The zero-point grids over quant_act_i8's activation (xe2_gemm_zp.cpp). */
const RadConstraint cZpI8Dtype[] = { RAD_CIN("dtype", "w2a8 w4a8_asym") };
const RadConstraint cZpGemv[] = { RAD_CIN("dtype", "w2a8 w4a8_asym"), RAD_CLE("M", 16) };
/* The 4-bit zero-point grids of an int4 W4A16 release (xe2_gemm_w4.cpp): one stored form, so every
* row of either dtype shares gemm_w4_layout. */
const RadConstraint cW4Dtype[] = { RAD_CIN("dtype", "w4a16_asym w4a8i8_asym") };
const RadConstraint cW4a16[] = { RAD_CIN("dtype", "w4a16_asym"), RAD_CLE("M", 64) };
const RadConstraint cW4a8i8[] = { RAD_CIN("dtype", "w4a8i8_asym") };
/* The M2XFP4 grid over givens_dense_quant's fp8 activation (xe2_gemm_m2.cpp). */
const RadConstraint cM2Dtype[] = { RAD_CIN("dtype", "m2xfp4") };
/* The fp8 head's decode band. */
@@ -83,6 +88,11 @@ const RadKernelInfo kKernels[] = {
.shape = "any n and group",
.dtypes = "float in, E4M3 codes, f32 scales",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_fp8 },
{ .name = "xe2_quant_act_i8g", .op = "quant_act_i8g", .family = "quant",
.computes = "row-major int8 codes and an f32 scale amax/127 per group, libref's q8_group",
.shape = "any n and group",
.dtypes = "float in, i8 codes, f32 scales",
.domain = RAD_DOMAIN_DEVICE, .launch = xe2::quant_act_i8g },
{ .name = "xe2_gated_quant_fp8", .op = "gated_quant_fp8", .family = "quant",
.computes = "silu(gate)*up or sigmoid(gate)*up over packed gate_up, bf16 narrowing, E4M3 "
"codes and an f32 scale per group",
@@ -279,6 +289,32 @@ const RadKernelInfo kKernels[] = {
.domain = RAD_DOMAIN_DEVICE, .constraints = cZpI8Dtype, .n_constraints = 1,
.launch = xe2::gemm_nt_q_zp, .layout = xe2::gemm_zp_layout,
.relayout = xe2::gemm_zp_relayout, .unrelayout = xe2::gemm_zp_unrelayout },
{ .name = "xe2_gemm_nt_q_w4a16", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over u4 codes with an f16 scale and zero per (row, 128): codes decoded to "
"code - zero in fp16 registers, fp16 DPAS a group into f32, scaled once a group",
.shape = "M <= 64 (blocks of 8 rows); N a multiple of 16, K of 128, group 128 (else the exact form)",
.dtypes = "w4a16_asym: u4 codes, f16 scale and u4/u8 zero per (row, 128 of K), f16 activation",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cW4a16, .n_constraints = 2,
.launch = xe2::gemm_nt_q_w4a16, .layout = xe2::gemm_w4_layout,
.relayout = xe2::gemm_w4_relayout, .unrelayout = xe2::gemm_w4_unrelayout },
{ .name = "xe2_gemm_nt_q_w4a8i8", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over u4 codes against an int8 activation: int8 DPAS with the codes as its "
"u4 B operand, exact group sums, libref's group terms in order -- libref's result",
.shape = "any M; N a multiple of 32, K of 128, group 128 (else the exact form)",
.dtypes = "w4a8i8_asym: u4 codes, f16 scale and u4/u8 zero per (row, 128 of K), row-major int8 "
"activation with an f32 scale per (row, 128)",
.domain = RAD_DOMAIN_DEVICE, .priority = 10, .constraints = cW4a8i8, .n_constraints = 1,
.launch = xe2::gemm_nt_q_w4a8i8, .layout = xe2::gemm_w4_layout,
.relayout = xe2::gemm_w4_relayout, .unrelayout = xe2::gemm_w4_unrelayout },
{ .name = "xe2_gemm_nt_q_w4_ref", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the 4-bit zero-point grids, libref's gemm_zp per output; the "
"correctness-first form",
.shape = "any M, N; K a multiple of group",
.dtypes = "w4a16_asym (f16 activation), w4a8i8_asym (int8 activation, f32 scale per (row, "
"group)): u4 codes, f16 scale and u4/u8 zero per (row, group)",
.domain = RAD_DOMAIN_DEVICE, .constraints = cW4Dtype, .n_constraints = 1,
.launch = xe2::gemm_nt_q_w4, .layout = xe2::gemm_w4_layout,
.relayout = xe2::gemm_w4_relayout, .unrelayout = xe2::gemm_w4_unrelayout },
{ .name = "xe2_gemm_nt_q_m2_gemv", .op = "gemm_nt_q", .family = "gemm",
.computes = "Y = A B^T over the M2XFP4 grid for M <= 16: e2m1 to fp16 by bits, fp16 activation "
"and subgroup sums, f32 group sums; libref's products regrouped",
+56
View File
@@ -0,0 +1,56 @@
/* xe2_zp.h -- what the zero-point GEMMs share (xe2_gemm_zp.cpp, xe2_gemm_w4.cpp): `b_scale`'s
* joined planes found on the device, and an operand's row stride. */
#pragma once
#include "xe2_common.h"
#include "rad_planes.h"
namespace xe2 {
/* One plane of the joined operand, found on the device. */
struct DPlane {
const char* p = nullptr;
uint32_t dt = 0;
int64_t row_bytes = 0;
float at(int64_t i, int64_t j) const { return rad_load_f32(p + i * row_bytes, dt, j); }
};
inline bool role_is(const char* role, const char* want) {
for (int i = 0; i < RAD_ENC_STR; ++i) {
if (role[i] != want[i]) return false;
if (!want[i]) return true;
}
return true;
}
/* The plane of `role` in the joined operand at `base` (abi/rad_planes.h): a header, an entry per
* plane, then the planes. The header lives in device memory, so the kernel finds its planes itself:
* a launch must not read device memory from the host. */
inline DPlane find_plane(const char* base, const char* role) {
DPlane v;
const RadPlanesHeader* h = (const RadPlanesHeader*)base;
if (h->magic != RAD_PLANES_MAGIC) return v;
const RadPlanesEntry* e = (const RadPlanesEntry*)(base + sizeof(RadPlanesHeader));
for (int i = 0; i < h->n_planes && i < RAD_ENC_MAX_PLANES; ++i)
if (role_is(e[i].role, role)) {
v.p = base + e[i].offset;
v.dt = e[i].dtype;
v.row_bytes = e[i].row_bytes;
return v;
}
return v;
}
/* The element distance between rows of `t` read as rows of `cols`. */
inline int64_t row_stride(const RadTensor* t, int64_t cols) {
if (!t || t->rank < 2) return cols;
int64_t span = 1;
for (int i = (int)t->rank - 1; i >= 0; --i) {
span *= t->shape[i];
if (span == cols) return (i > 0) ? t->stride[i - 1] : cols;
if (span > cols) break;
}
return t->stride[t->rank - 2];
}
} // namespace xe2