From 21c01f9930653f3d25c4e4ec9d55721469184039 Mon Sep 17 00:00:00 2001 From: hugypufy Date: Sun, 25 Oct 2026 11:59:30 +0000 Subject: [PATCH] libref, libavx, core/format: gemm_nt_q at m2xfp4 -- a libref arm of its own over the codes and meta concatenated as the weight libref serves gemm_nt_q at dtype m2xfp4 against the E4M3 activation and its per-128 scales, from ref_m2xfp4.cpp: libref keeps one row an op, so the gemm_nt_q row takes its launch, shape and layout hooks from there, each answering m2xfp4 and passing every other dtype to the hook it had. The layout joins the codes and meta planes into one U8 operand (codes rows, then meta rows) and the GEMM decodes it element by element. libavx refuses the encoding, so the host backend falls through to libref. The core's selection reader hands out the same codes+meta form for an m2xfp4 encoding, so the oracle and fixtures can build the operand. quant_m2xfp_test runs the row over the quantiser's planes; OPS.md gains the operand. Co-Authored-By: Claude Opus 5.5 --- core/format/encoding.cpp | 34 ++++++ docs/OPS.md | 12 ++- libavx/avx_planes.h | 2 + libref/ref_m2xfp4.cpp | 214 +++++++++++++++++++++++++++++++++++++ libref/ref_registry.cpp | 27 ++++- tests/quant_m2xfp_test.cpp | 93 +++++++++++++++- 6 files changed, 375 insertions(+), 7 deletions(-) create mode 100644 libref/ref_m2xfp4.cpp diff --git a/core/format/encoding.cpp b/core/format/encoding.cpp index d95ffe93..7dde4eec 100644 --- a/core/format/encoding.cpp +++ b/core/format/encoding.cpp @@ -434,6 +434,40 @@ int enc_selection_logical(const RadEncoding& e, const int* sel, int n_sel, const return RAD_OK; } } + /* An m2xfp4 weight's codes with its meta plane, without the scale: the selected planes' + * canonical bytes concatenated, as stored (the codes rows, then the meta rows). Read by a + * kernel that decodes the scheme itself beside the E8M0 exponent plane its b_scale operand + * carries; there is nothing to decode here, because the scale is what the selection leaves + * out. */ + if (rad_enc_is(&e, "m2xfp4") && dt == RAD_U8) { + int codes = -1; + bool meta = true; + for (int k = 0; k < n_sel && meta; ++k) { + const RadEncPlane& p = e.plane[sel[k]]; + if (p.kind != RAD_PLANE_TILED || p.block[0] != 1 || p.block[1] < 1 || + rad_enc_role_in_chain(p.role, "scale")) + meta = false; + else if (!std::strcmp(p.role, "codes")) { + if (codes >= 0 || p.block[1] != 1) meta = false; /* one code a value */ + else codes = k; + } + } + if (meta && codes >= 0) { + int64_t bytes = 0; + for (int k = 0; k < n_sel; ++k) + bytes += rows[k] * rad_enc_row_bytes(e.plane[sel[k]].dtype, cols[k]); + if (bytes != n) + return refuse("its planes hold " + std::to_string(bytes) + + " bytes and the reader wants " + std::to_string(n)); + uint8_t* dst = (uint8_t*)out; + for (int k = 0; k < n_sel; ++k) { + const int64_t rb = rad_enc_row_bytes(e.plane[sel[k]].dtype, cols[k]); + std::memcpy(dst, data[k], (size_t)(rows[k] * rb)); + dst += rows[k] * rb; + } + return RAD_OK; + } + } if (n_sel != e.n_planes) return refuse("it takes " + std::to_string(n_sel) + " of its encoding's " + std::to_string(e.n_planes) + " planes, which decode to nothing on their own"); diff --git a/docs/OPS.md b/docs/OPS.md index e318ea90..fcf0be59 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -41,8 +41,9 @@ here is such a choice. - Where an op takes `dtype`, it is a string naming the operand combination, so a weight format and an activation format share one key: `bf16`, `f16`/`fp16`, `f32`/`fp32`, `u8`, `fp8_e4m3`, `fp8a8`, `fp8a8f32`, `fp8a16`, `i8a8`, `i8a8h`, `w4`, `w4a8`, `w4a8_asym`, `w4a8h`, `w8a8`, - `w4a16`, `w8a16`, `w2a8`, `mxfp4a8`, `w5a16`, `w4a16_asym`, `w5a8i8`, `w4a8i8_asym`, and the - codebook expert forms `w4nla8h`, `w4nl64a8h`, `w4nl32a8h`, `w5nl64a8h` (`docs/MOE-W4.md`). + `w4a16`, `w8a16`, `w2a8`, `mxfp4a8`, `m2xfp4`, `w5a16`, `w4a16_asym`, `w5a8i8`, `w4a8i8_asym`, + and the codebook expert forms `w4nla8h`, `w4nl64a8h`, `w4nl32a8h`, `w5nl64a8h` + (`docs/MOE-W4.md`). - `w4a8h` is `w4a8` with a `group`-wide Walsh–Hadamard along K baked into the stored weight (`docs/MOE-W4.md`). - `i8a8` is an i8 weight with a bf16 scale per (row, 128 of K) against an int8 activation on @@ -720,6 +721,13 @@ At `i8a8` they are `quant_act_i8g`'s row-major codes and per-(row, 128 of K) sca weight with a bf16 scale per (row, 128 of K); at `mxfp4a8`, `quant_act_fp8`'s pair against e2m1 codes with an E8M0 exponent per (row, 32 of K). +At `m2xfp4` -- the `m2xfp4` quantiser's own scheme, not an affine grid -- `b` is that scheme's +codes and meta planes concatenated as stored (the codes rows, then the meta rows; the form the +core's selection reader hands out, core/format/encoding.cpp) and `b_scale` the E8M0 plane as at +`mxfp4a8`. The exact element is `e2m1(code) . 2^(e-127) . (1 + k_s/4)`, `k_s` the meta byte's +two-bit field over subgroups of eight columns (libquant's `lq_m2xfp.cpp`). `b_ref` is the same +reference exponent plane as at `mxfp4a8`. + `a_sum` removes an asymmetric grid's zero point and `b_ref` is the per-row reference exponent an MXFP4 kernel folds its block exponents against. `b_ref` is derived from `b_scale` and does not change the product. Both host libraries accept `b_ref` beside an e2m1 weight and ignore it, and diff --git a/libavx/avx_planes.h b/libavx/avx_planes.h index 3b201740..d7db6b80 100644 --- a/libavx/avx_planes.h +++ b/libavx/avx_planes.h @@ -163,6 +163,8 @@ static inline int avx_layout_mtp(const RadParam*, int, int operand, const RadEnc static inline int avx_layout_gemm_q(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, const RadTensor* planes, int n, RadLayout* out) { + /* libref serves M2XFP4 (ref_m2xfp4.cpp); refusing it here passes the op on to it. */ + if (enc && rad_enc_is(enc, "m2xfp4")) return RAD_E_DTYPE; 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); diff --git a/libref/ref_m2xfp4.cpp b/libref/ref_m2xfp4.cpp new file mode 100644 index 00000000..8f7b7940 --- /dev/null +++ b/libref/ref_m2xfp4.cpp @@ -0,0 +1,214 @@ +/* ref_m2xfp4.cpp -- gemm_nt_q at dtype m2xfp4: M2XFP's metadata-augmented MXFP4 (arXiv + * 2601.19213), the scheme libquant's `m2xfp4` quantiser writes. + * + * The weight is three TILED planes over 32-column groups: e2m1 codes, an E8M0 exponent per group + * with the adaptive bias already folded in, and a meta byte per group whose two-bit field k_s + * [2s, 2s+2) refines the exponent of subgroup s, columns [8s, 8s+8). The exact element is + * e2m1 code . 2^(e-127) . (1 + k_s/4). + * + * gemm_nt_q's weight operand takes the codes and the meta, stored as the two planes' canonical + * bytes concatenated -- the codes rows, then the meta rows -- the form the core's selection reader + * hands a kernel that decodes the scheme itself (core/format/encoding.cpp). `b_scale` and `b_ref` + * are the exponent plane as it lies; `b_ref` is accepted as at mxfp4a8 and does not change the + * product. + * + * libref keeps one row an op, so ref_gemm_nt_q's row (ref_registry.cpp) takes its hooks from the + * end of this file: each answers dtype m2xfp4 here and hands every other dtype, unchanged, to the + * hook the row had (ref_gemm.cpp, ref_shapes.cpp, ref_planes.h). + */ +#include +#include +#include "ref_common.h" +#include "ref_gemm.h" +#include "ref_ops.h" +#include "ref_planes.h" + +using namespace ref; + +namespace { + +/* The plain scheme as libquant writes it: no transform; codes [1 x 1], scale and meta [1 x 32]. */ +bool m2_enc_planes(const RadEncoding* enc) { + if (!enc || !rad_enc_is(enc, "m2xfp4") || enc->transform[0] || enc->n_planes != 3) + return false; + const RadEncPlane* sc = rad_enc_plane(enc, "scale"); + const RadEncPlane* mt = rad_enc_plane(enc, "meta"); + return 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; +} + +/* Each product is one f32 multiply and each output one f32 accumulation in k order: the exact + * decode's own arithmetic, since the multipliers take the codes off the integer grid an mxfp4 + * group sub-sum would use. */ +int m2_gemm(const RadArgs* a) { + if (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_ = t_in(a, 1), *bs_ = t_in(a, 3); + if (!A->data || !B->data || !Y->data) return RAD_E_INVAL; + if (t_in(a, 5)) return RAD_E_UNSUPPORTED; /* a_sum belongs to the asym grids */ + /* Parts (pb1/pb2) are the rotated grids'; a plain m2xfp4 linear has none, and a case carrying + * them is declined rather than answered unsplit. */ + if (p_int(a, "pb1", 0) > 0 || p_int(a, "pb2", 0) > 0) return RAD_E_UNSUPPORTED; + if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8 || B->rank != 1) return RAD_E_DTYPE; + if (!as_ || !bs_ || bs_->dtype != RAD_E8M0 || bs_->rank < 2) return RAD_E_DTYPE; + const int64_t K = p_int(a, "K", 0), N = p_int(a, "N", 0); + if (K <= 0 || K % 32 || N <= 0) return RAD_E_SHAPE; + const int64_t M = numel(Y) / N; + if (M <= 0) return RAD_E_SHAPE; + const int64_t kb = K / 32, akb = (K + 127) / 128; + if (numel(A) < M * K || numel(B) != N * (K / 2 + kb) || numel(bs_) < N * kb || + numel(as_) < M * akb) + return RAD_E_SHAPE; + const int64_t ars = row_stride(A, K), acs = laststride(A); + const int64_t srs = row_stride(as_, akb), scs = laststride(as_); + const int64_t brs = row_stride(bs_, kb); + const int64_t ys_r = row_stride(Y, N), ys_c = laststride(Y); + const uint8_t* meta0 = (const uint8_t*)B->data + N * (K / 2); + REF_PAR_FOR(N) + for (int64_t n = 0; n < N; ++n) { + const uint8_t* codes = (const uint8_t*)B->data + n * (K / 2); + const uint8_t* meta = meta0 + n * kb; + const uint8_t* expe = (const uint8_t*)bs_->data + n * brs; + /* The weight row decoded once, so the meta and exponent reads are not repeated per + * activation row. */ + std::vector w((size_t)K); + for (int64_t k = 0; k < K; ++k) { + const uint8_t c = (uint8_t)((codes[k >> 1] >> (4 * (k & 1))) & 0xF); + const float cv = mxfp4_elem_to_f32(c); + const float ev = e8m0_to_f32(expe[k >> 5]); + const float km = 1.0f + 0.25f * (float)((meta[k >> 5] >> (2 * ((k & 31) >> 3))) & 3); + w[(size_t)k] = cv * ev * km; + } + for (int64_t m = 0; m < M; ++m) { + const int64_t ab = m * ars; + float acc = 0.0f; + for (int64_t k = 0; k < K; ++k) + acc += ldt(A, ab + k * acs) * ldt(as_, m * srs + (k >> 7) * scs) * w[(size_t)k]; + stt(Y, m * ys_r + n * ys_c, acc); + } + } + return RAD_OK; +} + +/* mxfp4a8's operands (ref_shapes.cpp), the weight being the codes and meta concatenated. */ +int m2_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { + const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0); + if (rad_param_geti(p, n_p, "group", 0) != 32 || N <= 0 || K <= 0 || K % 32) + return RAD_E_UNSUPPORTED; + std::vector q(p, p + n_p); + for (RadParam& r : q) + if (r.key && std::strcmp(r.key, "dtype") == 0) r.sval = "mxfp4a8"; + const int rc = ref_gemm_nt_q_shape(q.data(), n_p, operand, out); + if (rc != RAD_OK || operand != 2) return rc; + out->dtype = (uint32_t)RAD_U8; + out->rank = 1; + out->shape[0] = N * (K / 2 + K / 32); + out->shape[1] = 0; + return RAD_OK; +} + +/* The weight operand is codes and meta, concatenated; the scale and reference operands are the + * exponent plane, stored as it is. Any other encoding is refused, a transform included. */ +int m2_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel, + const RadTensor* planes, int n, RadLayout* out) { + 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.codes.meta"; + 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 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 = m2_layout(p, n_p, operand, enc, sel, planes, n, &lay); + if (st != RAD_OK) return st; + 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]); + std::memcpy(d, planes[i].data, (size_t)b); + d += b; + } + return RAD_OK; +} + +int 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 = m2_layout(p, n_p, operand, enc, sel, planes, n, &lay); + if (st != RAD_OK) return st; + 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]); + std::memcpy(planes[i].data, s, (size_t)b); + s += b; + } + return RAD_OK; +} + +} /* namespace */ + +/* ---- ref_gemm_nt_q's row hooks: m2xfp4 here, every other dtype the row's own. */ + +static bool is_m2xfp4(const RadParam* p, int n_p) { + const char* dt = rad_param_gets(p, n_p, "dtype", nullptr); + return dt && std::strcmp(dt, "m2xfp4") == 0; +} + +int ref_gemm_nt_q_m2(const RadArgs* a, RadStream s) { + return is_m2xfp4(a->p, a->n_p) ? m2_gemm(a) : ref_gemm_nt_q(a, s); +} + +int ref_gemm_nt_q_m2_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { + return is_m2xfp4(p, n_p) ? m2_shape(p, n_p, operand, out) + : ref_gemm_nt_q_shape(p, n_p, operand, out); +} + +int ref_layout_gemm_q_m2(const RadParam* p, int n_p, int operand, const RadEncoding* enc, + const int* sel, const RadTensor* planes, int n, RadLayout* out) { + return is_m2xfp4(p, n_p) ? m2_layout(p, n_p, operand, enc, sel, planes, n, out) + : ref_layout_gemm_q(p, n_p, operand, enc, sel, planes, n, out); +} + +int ref_relayout_gemm_q_m2(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) { + return is_m2xfp4(p, n_p) + ? m2_relayout(p, n_p, operand, enc, sel, planes, n, dst, dst_bytes) + : ref_relayout_planes(p, n_p, operand, enc, sel, planes, n, dst, + dst_bytes); +} + +int ref_unrelayout_gemm_q_m2(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) { + return is_m2xfp4(p, n_p) + ? m2_unrelayout(p, n_p, operand, enc, sel, src, src_bytes, planes, n) + : ref_unrelayout_planes(p, n_p, operand, enc, sel, src, + src_bytes, planes, n); +} diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index 07c7db77..873bc9c2 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -2061,6 +2061,24 @@ static int ref_check_gemm_q(const char* op, const RadArgs* a) { }, \ nullptr, lay, ref::ref_relayout_planes, fn##_shape, ref::ref_unrelayout_planes } +/* gemm_nt_q's row: the hooks above with dtype m2xfp4 answered first (ref_m2xfp4.cpp). */ +int ref_gemm_nt_q_m2(const RadArgs*, RadStream); +REF_SHAPE_FN(ref_gemm_nt_q_m2); +int ref_layout_gemm_q_m2(const RadParam*, int, int, const RadEncoding*, const int*, + const RadTensor*, int, RadLayout*); +int ref_relayout_gemm_q_m2(const RadParam*, int, int, const RadEncoding*, const int*, + const RadTensor*, int, void*, int64_t); +int ref_unrelayout_gemm_q_m2(const RadParam*, int, int, const RadEncoding*, const int*, + const void*, int64_t, const RadTensor*, int); +#define ROWL_GQ(nm, opname, fam, what, dts, check, fn, lay, rel, unrel) \ + { nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \ + nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, \ + [](const RadArgs* a, RadStream s) -> int { \ + const int rc = check(opname, a); \ + return rc < 0 ? rc : fn(a, s); \ + }, \ + nullptr, lay, rel, fn##_shape, unrel } + #define DT_FLOAT "bf16 f16 f32 fp8_e4m3 fp8_e5m2 i8 i32, chosen per operand from its own dtype" #define DT_ANY "any dtype rad_types.h names, per operand" #define DT_FLOAT_CB DT_FLOAT "; or u4 / u5 codes into an f32 table, joined (w4nl* / w5nl*)" @@ -2236,10 +2254,11 @@ SIMPLE("ref_dequant", "dequant", "quant", "group-scaled dequantisation", DT_ANY, SIMPLE("ref_gemm_nt", "gemm_nt", "gemm", "C = A B^T, f32 accumulate", DT_FLOAT, ref_gemm_nt), SIMPLE("ref_gemm_nt_bias", "gemm_nt_bias", "gemm", "C = A B^T + bias", DT_FLOAT, ref_gemm_nt_bias), -ROWL("ref_gemm_nt_q", "gemm_nt_q", "gemm", "C = A B^T with group-scaled quantised operands", - "i8 / fp8 / float A; w4, w2, mxfp4, i8 or float B; u2 / u4 / u5 codes with an f16 scale " - "and integer zero per group, joined; f32 accumulate", - ref_check_gemm_q, ref_gemm_nt_q, ref::ref_layout_gemm_q), +ROWL_GQ("ref_gemm_nt_q", "gemm_nt_q", "gemm", "C = A B^T with group-scaled quantised operands", + "i8 / fp8 / float A; w4, w2, mxfp4, m2xfp4, i8 or float B; u2 / u4 / u5 codes with an f16 " + "scale and integer zero per group, joined; f32 accumulate", + ref_check_gemm_q, ref_gemm_nt_q_m2, ref_layout_gemm_q_m2, ref_relayout_gemm_q_m2, + ref_unrelayout_gemm_q_m2), SIMPLE("ref_gemm_nt_q_bias", "gemm_nt_q_bias", "gemm", "quantised C = A B^T + bias", "i8 / fp8 / float A; w4, w2, mxfp4, i8 or float B; f32 accumulate", ref_gemm_nt_q_bias), SIMPLE("ref_lora_shrink", "lora_shrink", "gemm", "t = x A[slot]^T per row, base rows untouched", diff --git a/tests/quant_m2xfp_test.cpp b/tests/quant_m2xfp_test.cpp index d9f55246..b18854b7 100644 --- a/tests/quant_m2xfp_test.cpp +++ b/tests/quant_m2xfp_test.cpp @@ -10,12 +10,15 @@ * scheme refines: the search's candidate set contains that quantiser's choice for every group, * so it can only tie or win, and the wins are where the format earns its half bit. The wide case * holds the same property one bias wider: wide=1 sweeps b in {-2, ..., 2} and can only tie or - * beat adaptive, its planes decoding bit for bit as the equation does. + * beat adaptive, its planes decoding bit for bit as the equation does. The last case hands the + * planes to libref's gemm_nt_q row at m2xfp4 (ref_m2xfp4.cpp) and holds its product to the same + * hand decode. */ #include "quant_fixture.h" #include +#include #include #include #include @@ -333,4 +336,92 @@ TEST(m2xfp4_wide_bias_sweep_contains_the_narrow_one_and_round_trips_bit_exact) { CHECK(same_planes(off, both)); } +/* libref's gemm_nt_q row at m2xfp4 over the quantiser's own planes: its layout hook takes codes and + * meta for the weight and the exponent plane as stored, its relayout concatenates the two, and + * the product is the hand decode's against an exact e4m3 activation and its per-128 scales, + * summed in double. N = 40 and M = 13 are not multiples of anything; K = 384 is three activation + * scale blocks. */ +TEST(m2xfp4_libref_gemm_reads_the_planes_as_the_decode_does) { + NEED_LIBQUANT(); + const std::string lib = home() + "/kernels/libref.so"; + void* so = dlopen(lib.c_str(), RTLD_NOW | RTLD_LOCAL); + if (!so) ARTIFACT_MISSING(lib + " (does not load: " + dlerror() + ")"); + const auto count = (int (*)(void))dlsym(so, "rad_kernel_count"); + const auto at = (const RadKernelInfo* (*)(int))dlsym(so, "rad_kernel_at"); + REQUIRE(count && at); + const RadKernelInfo* k = nullptr; + for (int i = 0; i < count(); ++i) + if (std::strcmp(at(i)->name, "ref_gemm_nt_q") == 0) k = at(i); + REQUIRE(k && k->layout && k->relayout && k->launch); + + const int64_t M = 13, N = 40, K = 384, kb = K / 32, akb = K / 128; + const RadQuantizerInfo* q = quantizer("m2xfp4"); + REQUIRE(q); + Opts o; + m2xfp_opts(o, 1); + Quantised w = quantise(q, o, kName, N, K, draw(N * K, 61, 8.0f)); + REQUIRE_EQ(w.status, RAD_OK); + const std::vector wd = ref_decode(w, N, K); + + auto tensor = [](void* d, uint32_t dt, std::initializer_list s) { + RadTensor t{}; + t.data = d; + t.dtype = dt; + for (int64_t e : s) t.shape[t.rank++] = e; + int64_t st = 1; + for (int i = (int)t.rank - 1; i >= 0; --i) { + t.stride[i] = st; + st *= t.shape[i]; + } + return t; + }; + Opts g; + g.i("M", M).i("N", N).i("K", K).i("group", 32).s("dtype", "m2xfp4"); + const RadTensor pl[2] = { tensor(w.planes[0].data(), RAD_FP4E2M1, { N, K }), + tensor(w.planes[2].data(), RAD_U8, { N, kb }) }; + const RadTensor sp = tensor(w.planes[1].data(), RAD_E8M0, { N, kb }); + const int sel[2] = { 0, 2 }, ssel[1] = { 1 }; + RadLayout lay{}; + CHECK_EQ(k->layout(g.data(), g.n(), 3, &w.enc, ssel, &sp, 1, &lay), (int)RAD_E_UNSUPPORTED); + REQUIRE_EQ(k->layout(g.data(), g.n(), 2, &w.enc, sel, pl, 2, &lay), (int)RAD_OK); + REQUIRE_EQ(lay.bytes, N * (K / 2 + kb)); + std::vector b((size_t)lay.bytes); + REQUIRE_EQ(k->relayout(g.data(), g.n(), 2, &w.enc, sel, pl, 2, b.data(), lay.bytes), + (int)RAD_OK); + + /* integer codes in [-13, 13], which e4m3 holds exactly */ + std::vector a((size_t)(M * K)); + std::vector as((size_t)(M * akb)), y((size_t)(M * N), NAN); + for (int64_t i = 0; i < M * K; ++i) + rad_store_f32(a.data(), RAD_F8E4M3, i, (float)((i * 7) % 27 - 13)); + for (int64_t i = 0; i < M * akb; ++i) as[(size_t)i] = 0.003f * (float)(1 + i % 5); + RadTensor t[7] = { tensor(a.data(), RAD_F8E4M3, { M, K }), + tensor(as.data(), RAD_F32, { M, akb }), + tensor(b.data(), RAD_U8, { lay.bytes }), + sp, + tensor(y.data(), RAD_F32, { M, N }), + RadTensor{}, + sp }; + RadArgs args{}; + args.t = t; + args.n_t = 7; + args.p = g.data(); + args.n_p = g.n(); + REQUIRE_EQ(k->launch(&args, nullptr), (int)RAD_OK); + + double err = 0, mag = 0; + for (int64_t m = 0; m < M; ++m) + for (int64_t n = 0; n < N; ++n) { + double r = 0; + for (int64_t c = 0; c < K; ++c) + r += (double)rad_load_f32(a.data(), RAD_F8E4M3, m * K + c) * + (double)as[(size_t)(m * akb + c / 128)] * (double)wd[(size_t)(n * K + c)]; + err = std::max(err, std::fabs((double)y[(size_t)(m * N + n)] - r)); + mag = std::max(mag, std::fabs(r)); + } + CHECK(mag > 0); + CHECK(err <= 1e-5 * mag); + dlclose(so); +} + RAD_TEST_MAIN()