Archived
Where N % 16 == 0 and K % 128 == 0 the layout hook asks for "xe2.m2.tiled16": a 16-column tile's 128-column step is 1 KiB, dword w of its columns side by side, the ESIMD kernel's [word][lane] registers as they lie (four block loads in place of four d32x4 gathers). M = 1 goes to the same kernel when tiled. Every m2 reader in libxe2 (exact, SIMT GEMV, jm and SIMT DPAS forms, prefill decode) reads the tiled form; unrelayout inverts it for the oracle. rad-kbench against libref: 70 checked, 70 passed (worst rel_l2 1.2e-3). M = 8: gate_up 240 -> 222, down 140 -> 119, N=1024 50 -> 25 us. Greedy output identical (8 prompts x 64 tokens); 512-token decode 31.8 -> 33.3 tok/s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018EpFgqQidSnH8zo7boVZC1
233 lines
11 KiB
C++
233 lines
11 KiB
C++
/* xe2_gemm_m2.cpp -- gemm_nt_q at dtype m2xfp4 (the M2XFP4 grid of Qwen3.8-27B-PARO-M2XFP4).
|
|
* Correctness first: libref's gemm_m2 (ref_gemm.cpp) per output --
|
|
*
|
|
* w[k] = (e2m1(code[n][k]) * 2^(e[n][k/32] - 127)) * (1 + meta field / 4)
|
|
* acc = acc + (a[m][k] * a_scale[m][k/128]) * w[k] for ascending k, f32
|
|
*
|
|
* -- the meta byte's two-bit field [2s, 2s+2) over subgroups s of eight columns. The activation is
|
|
* givens_dense_quant's E4M3 with a scale per 128 columns; the weight's Givens rotation is the
|
|
* activation's, so the planes are read as they are stored. Parts (`pb1`, `pb2`, a fused projection
|
|
* rotated per piece): weight rows [0, e1), [e1, e2), [e2, N) read copies 0, 1, 2 of the activation
|
|
* and its scale, side by side, as libref's gemm_m2 and libr4d's mx_parts do.
|
|
*
|
|
* `b` reaches the kernel as the weight's codes, then its meta rows, `b_scale` as the E8M0 plane
|
|
* [N, K/32]. The layout hooks are libref's m2 arm (ref_layout_gemm_q, ref_planes.h) -- codes rows
|
|
* then meta rows, "m2.codes.meta" -- except where N % 16 == 0 and K % 128 == 0: there the codes are
|
|
* stored tiled ("xe2.m2.tiled16", xe2_ops.h), the verify GEMM's registers one block load a step
|
|
* (246 -> 229 us at gate_up's shape, probe m2d.cpp), and every kernel here reads that form.
|
|
*/
|
|
#include "xe2_common.h"
|
|
#include "xe2_ops.h"
|
|
|
|
#include "rad_planes.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
|
|
namespace xe2 {
|
|
|
|
namespace {
|
|
|
|
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];
|
|
}
|
|
|
|
int64_t last_stride(const RadTensor* t) { return t->rank ? t->stride[t->rank - 1] : 1; }
|
|
|
|
bool sel_roles(const RadEncoding* enc, const int* sel, int n, const char* r0, const char* r1) {
|
|
if (!enc || !sel || n != 2) return false;
|
|
for (int i = 0; i < 2; ++i)
|
|
if (sel[i] < 0 || sel[i] >= enc->n_planes) return false;
|
|
return std::strncmp(enc->plane[sel[0]].role, r0, RAD_ENC_STR) == 0 &&
|
|
std::strncmp(enc->plane[sel[1]].role, r1, RAD_ENC_STR) == 0;
|
|
}
|
|
|
|
/* libref's m2_enc_planes: three tiled planes over 32-column groups, untransformed or under a
|
|
* "givens" transform with its tables beside them. */
|
|
bool m2_enc_planes(const RadEncoding* enc) {
|
|
if (!rad_enc_is(enc, "m2xfp4")) return false;
|
|
if (enc->transform[0] && std::strncmp(enc->transform, "givens", RAD_ENC_STR) != 0) return false;
|
|
const RadEncPlane* sc = rad_enc_plane(enc, "scale");
|
|
const RadEncPlane* mt = rad_enc_plane(enc, "meta");
|
|
const bool planes = enc->plane[0].kind == RAD_PLANE_TILED &&
|
|
enc->plane[0].dtype == RAD_FP4E2M1 && enc->plane[0].block[0] == 1 &&
|
|
enc->plane[0].block[1] == 1 && sc && mt && sc->kind == RAD_PLANE_TILED &&
|
|
sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == 32 &&
|
|
mt->kind == RAD_PLANE_TILED && mt->dtype == RAD_U8 && mt->block[0] == 1 &&
|
|
mt->block[1] == 32;
|
|
if (!planes) return false;
|
|
if (!enc->transform[0]) return enc->n_planes == 3;
|
|
return enc->n_planes >= 6 && rad_enc_plane(enc, "t.pairs") &&
|
|
rad_enc_plane(enc, "t.angle") && rad_enc_plane(enc, "t.scale");
|
|
}
|
|
|
|
constexpr const char* kTag = "m2.codes.meta";
|
|
constexpr const char* kTagT = "xe2.m2.tiled16";
|
|
|
|
/* Copies the codes plane from row-major [N][K/2] to the tiled form (xe2_ops.h), or back. */
|
|
void tile_codes(const uint8_t* src, uint8_t* dst, int64_t N, int64_t K, bool to_tiled) {
|
|
const int64_t rb = K / 2;
|
|
for (int64_t n = 0; n < N; ++n)
|
|
for (int64_t b = 0; b < rb; b += 4) {
|
|
const int64_t t = m2t_off(n, b, K), r = n * rb + b;
|
|
std::memcpy(dst + (to_tiled ? t : r), src + (to_tiled ? r : t), 4);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
/* ------------------------------------------------------------------ the layout hooks */
|
|
int gemm_m2_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
|
|
const RadTensor* planes, int n, RadLayout* out) {
|
|
const char* dt = rad_param_gets(p, n_p, "dtype", nullptr);
|
|
if (!dt || std::strcmp(dt, "m2xfp4") != 0) return RAD_E_DTYPE;
|
|
const long long group = rad_param_geti(p, n_p, "group", 0);
|
|
const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0);
|
|
if (!m2_enc_planes(enc) || group != 32 || N <= 0 || K <= 0 || K % 32) return RAD_E_DTYPE;
|
|
if (operand == 2) {
|
|
if (!sel_roles(enc, sel, n, "codes", "meta")) return RAD_E_DTYPE;
|
|
if (planes[0].shape[0] != N || planes[0].shape[1] != K || planes[1].shape[0] != N ||
|
|
planes[1].shape[1] != K / 32)
|
|
return RAD_E_SHAPE;
|
|
out->tag = m2_tiled(N, K) ? kTagT : kTag;
|
|
out->dtype = RAD_U8;
|
|
out->rank = 1;
|
|
out->shape[0] = N * (K / 2 + K / 32);
|
|
out->align = 0;
|
|
out->bytes = out->shape[0];
|
|
return RAD_OK;
|
|
}
|
|
if (operand == 3 || operand == 6) {
|
|
if (n != 1 || sel[0] < 0 || sel[0] >= enc->n_planes ||
|
|
std::strncmp(enc->plane[sel[0]].role, "scale", RAD_ENC_STR) != 0)
|
|
return RAD_E_DTYPE;
|
|
return RAD_E_UNSUPPORTED; /* one plane, stored as it is */
|
|
}
|
|
return RAD_E_UNSUPPORTED;
|
|
}
|
|
|
|
int gemm_m2_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_m2_layout(p, n_p, operand, enc, sel, planes, n, &lay);
|
|
if (st != RAD_OK) return st;
|
|
const bool tiled = lay.tag && std::strcmp(lay.tag, kTagT) == 0;
|
|
if (!lay.tag || (!tiled && std::strcmp(lay.tag, kTag) != 0))
|
|
return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes);
|
|
if (dst_bytes != lay.bytes) return RAD_E_SHAPE;
|
|
uint8_t* d = (uint8_t*)dst;
|
|
for (int i = 0; i < n; ++i) {
|
|
const int64_t b = planes[i].shape[0] * rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
|
|
if (i == 0 && tiled)
|
|
tile_codes((const uint8_t*)planes[0].data, d, planes[0].shape[0], planes[0].shape[1], true);
|
|
else
|
|
std::memcpy(d, planes[i].data, (size_t)b);
|
|
d += b;
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
int gemm_m2_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_m2_layout(p, n_p, operand, enc, sel, planes, n, &lay);
|
|
if (st != RAD_OK) return st;
|
|
const bool tiled = lay.tag && std::strcmp(lay.tag, kTagT) == 0;
|
|
if (!lay.tag || (!tiled && std::strcmp(lay.tag, kTag) != 0))
|
|
return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n);
|
|
if (src_bytes != lay.bytes) return RAD_E_SHAPE;
|
|
const uint8_t* s = (const uint8_t*)src;
|
|
for (int i = 0; i < n; ++i) {
|
|
const int64_t b = planes[i].shape[0] * rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
|
|
if (i == 0 && tiled)
|
|
tile_codes(s, (uint8_t*)planes[0].data, planes[0].shape[0], planes[0].shape[1], false);
|
|
else
|
|
std::memcpy(planes[i].data, s, (size_t)b);
|
|
s += b;
|
|
}
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ the kernel */
|
|
int gemm_nt_q_m2(const RadArgs* a, RadStream st) {
|
|
if (!a || a->n_t < 5) 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), *BS = rad_arg_in(a, 3);
|
|
if (!A->data || !B->data || !Y->data) return RAD_E_INVAL;
|
|
if (rad_arg_in(a, 5)) return RAD_E_UNSUPPORTED; /* a_sum belongs to the asym grids */
|
|
if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8 || !AS || !BS || !AS->data || !BS->data ||
|
|
BS->dtype != RAD_E8M0 || BS->rank < 2)
|
|
return RAD_E_DTYPE;
|
|
const int64_t K = rad_args_geti_or(a, "K", 0), N = rad_args_geti_or(a, "N", 0);
|
|
if (K <= 0 || K % 32 || N <= 0) return RAD_E_SHAPE;
|
|
const int64_t M = rad_tensor_numel(Y) / N;
|
|
if (M <= 0) return RAD_E_SHAPE;
|
|
const int64_t kb = K / 32, akb = (K + 127) / 128;
|
|
const int64_t pb1 = rad_args_geti_or(a, "pb1", 0), pb2 = rad_args_geti_or(a, "pb2", 0);
|
|
const bool parted = pb1 > 0 || pb2 > 0;
|
|
const int64_t e1 = pb1 > 0 ? std::min<int64_t>(pb1, N) : N;
|
|
const int64_t e2 = pb2 > 0 ? std::max<int64_t>(e1, std::min<int64_t>(pb2, N)) : N;
|
|
const int64_t parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
|
|
if (parted && ((e1 % 16) || (e2 % 16))) return RAD_E_SHAPE;
|
|
if (parted && e1 == e2 && e2 < N) return RAD_E_SHAPE;
|
|
const int64_t acols = parts * K, scols = parts * akb;
|
|
/* `b` is the stored codes-then-meta blob: a rank-1 u8 of its bytes (the shape hook's
|
|
* description), or the weight's logical [N, K] u8 view of the same bytes (the engine's). */
|
|
const bool blob = (B->rank == 1 && rad_tensor_numel(B) == N * (K / 2 + kb)) ||
|
|
(B->rank == 2 && B->shape[0] == N && B->shape[1] == K);
|
|
if (!blob) return RAD_E_SHAPE;
|
|
if (rad_tensor_numel(A) < M * acols ||
|
|
rad_tensor_numel(BS) < N * kb || rad_tensor_numel(AS) < M * scols)
|
|
return RAD_E_SHAPE;
|
|
if (A->rank >= 2 && A->shape[A->rank - 1] != acols) return RAD_E_SHAPE;
|
|
const int64_t ars = row_stride(A, acols), acs = last_stride(A);
|
|
const int64_t srs = row_stride(AS, scols), scs = last_stride(AS);
|
|
const int64_t brs = row_stride(BS, kb);
|
|
const int64_t yrs = row_stride(Y, N), ycs = last_stride(Y);
|
|
const char *ab = (const char*)A->data, *sb = (const char*)AS->data;
|
|
const uint8_t* codes0 = (const uint8_t*)B->data;
|
|
const uint8_t* meta0 = codes0 + N * (K / 2);
|
|
const uint8_t* expe0 = (const uint8_t*)BS->data;
|
|
char* yb = (char*)Y->data;
|
|
const uint32_t adt = A->dtype, sdt = AS->dtype, ydt = Y->dtype;
|
|
const bool tl = m2_tiled(N, K); /* the stored form gemm_m2_layout asked for */
|
|
sycl::queue* q = queue_of(st);
|
|
if (!q) return RAD_E_DEVICE;
|
|
const int64_t total = M * N;
|
|
constexpr size_t wg = 128;
|
|
const size_t groups = (size_t)((total + (int64_t)wg - 1) / (int64_t)wg);
|
|
return submit([&] {
|
|
syclex::nd_launch(*q, sycl::nd_range<1>{groups * wg, wg}, [=](sycl::nd_item<1> it) {
|
|
const int64_t g = (int64_t)it.get_global_id(0);
|
|
if (g >= total) return;
|
|
const int64_t n = g % N, m = g / N;
|
|
const int64_t part = n < e1 ? 0 : (n < e2 ? 1 : 2);
|
|
const int64_t a0 = m * ars + part * K * acs, s0 = m * srs + part * akb * scs;
|
|
const uint8_t* meta = meta0 + n * kb;
|
|
const uint8_t* expe = expe0 + n * brs;
|
|
constexpr float e2m1[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };
|
|
float acc = 0.0f;
|
|
for (int64_t k = 0; k < K; ++k) {
|
|
const uint8_t cb = tl ? codes0[m2t_off(n, k >> 1, K)] : codes0[n * (K / 2) + (k >> 1)];
|
|
const uint8_t c = (uint8_t)((cb >> (4 * (k & 1))) & 0xF);
|
|
const float cv = (c & 8) ? -e2m1[c & 7] : e2m1[c & 7];
|
|
const float ev = sycl::ldexp(1.0f, (int)expe[k >> 5] - 127);
|
|
const float km = 1.0f + 0.25f * (float)((meta[k >> 5] >> (2 * ((k & 31) >> 3))) & 3);
|
|
const float w = cv * ev * km;
|
|
acc += rad_load_f32(ab, adt, a0 + k * acs) * rad_load_f32(sb, sdt, s0 + (k >> 7) * scs) * w;
|
|
}
|
|
rad_store_f32(yb, ydt, m * yrs + n * ycs, acc);
|
|
});
|
|
});
|
|
}
|
|
|
|
} // namespace xe2
|