M2XFP4 (MXFP4v2): quantiser, gfx1201 WMMA kernels, host rows, df2 wiring, .rad packer (Import::ParoM2), stability fixes (counters-in-scratch x3, aql publish fences), kbench rows, runbook (REMAINING.md)

This commit is contained in:
hugypufy committed 2026-10-23 12:25:59 +01:00
1 parent 9eaf10da6e
commit ea0908596b
60 files changed
+4619 -193

No files matched your search

+54
View File
@@ -299,6 +299,45 @@ static int gemm_q_drive(const Mat& ma, const Mat& mb, const Scales* SA, const Sc
return gemm_drive(ma, mb, SA, SB, Y, bias, M, N, K);
}
/* gemm_nt_q at dtype m2xfp4, the m2xfp4 quantiser's own scheme (libquant): `b` is the weight's
* codes and meta planes concatenated as stored (avx_planes.h's m2 arm -- the codes rows, then the
* meta rows), `b_scale` the E8M0 exponent plane [N, K/32] and `b_ref` the same plane, as the
* mxfp4 grid's. The exact element is e2m1 code . 2^(e-127) . (1 + k_s/4), the subgroup multiplier
* k_s the meta byte's two-bit field [2s, 2s+2) over subgroups s of eight columns: the product
* runs with the weight's group at that eight and the meta folded into the scale stream
* (Scales::m2), the same law ref states element by element (ref_gemm.cpp's gemm_m2) and
* libr4d's staging table applies on the card. */
static int gemm_m2(const RadTensor* A, const RadTensor* as_, const RadTensor* B,
const RadTensor* bs_, const RadTensor* Y, int64_t M, int64_t N, int64_t K) {
if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8 || B->rank != 1) return RAD_E_DTYPE;
if (!as_ || !bs_ || bs_->rank < 2) return RAD_E_DTYPE;
if (K <= 0 || K % 32 || N <= 0) return RAD_E_SHAPE;
if (numel(B) != N * (K / 2 + K / 32)) return RAD_E_SHAPE;
Scales SB = make_scales(bs_, N, K, true);
if (!SB.p || SB.group != 32 || SB.rblk != 1) return RAD_E_SHAPE;
SB.group = 8;
SB.e8m0 = true;
SB.m2 = true;
SB.m2_meta = (const uint8_t*)B->data + N * (K / 2);
SB.m2_mrs = K / 32;
/* The codes as their own [N, K] operand over the concatenation's head: nibble elements, a
* row K/2 bytes, dense. */
RadTensor Bc{};
Bc.data = B->data;
Bc.dtype = RAD_FP4E2M1;
Bc.rank = 2;
Bc.shape[0] = N;
Bc.shape[1] = K;
Bc.stride[0] = K;
Bc.stride[1] = 1;
Scales SA = make_scales(as_, M, K, false);
const Mat ma = as_mat(A, K), mb = as_mat(&Bc, K);
return gemm_q_drive(ma, mb, &SA, &SB, Y, nullptr, M, N, K);
}
/* gemm_nt_bias's epilogue (docs/OPS.md): activation of the stored product, then the residual add,
* each step read from and stored to the output's dtype (the unfused chain, as in libref). */
static void gemm_epilogue(const RadTensor* Y, const RadTensor* res, int act, int64_t M, int64_t N) {
@@ -975,6 +1014,20 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
* to the grids that remove a zero through it, `b_ref` to an e2m1 weight. */
const avxp::ZpGrid* zg = avxp::zp_grid(p_str(a, "dtype", nullptr));
if (zg && bias_operand) return RAD_E_UNSUPPORTED;
/* M2XFP4: the quantiser's own scheme, decoded by gemm_m2 from its concatenated planes (whose
* byte count is its own, not N x K). Parts (pb1/pb2) are the rotated grids'; the m2 declare
* makes none, and a case carrying them is declined rather than answered unsplit, as libref
* declines them. Its `b_ref` is the mxfp4 grid's, accepted and ignored beside the scheme. */
if (!bias_operand && p_str(a, "dtype", nullptr) &&
std::strcmp(p_str(a, "dtype", nullptr), "m2xfp4") == 0) {
if (rad_arg_in(a, 5)) return RAD_E_UNSUPPORTED; /* a_sum belongs to the asym grids */
if (p_int(a, "pb1", 0) > 0 || p_int(a, "pb2", 0) > 0) return RAD_E_UNSUPPORTED;
const int64_t K = p_int(a, "K", 0), N = p_int(a, "N", 0);
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
const int64_t M = numel(Y) / N;
if (M <= 0) return RAD_E_SHAPE;
return gemm_m2(A, as_, B, bs_, Y, M, N, K);
}
if (!bias_operand) {
if (rad_arg_in(a, 5) && (!zg || zg->act != avxp::ZP_I8_ROW)) return RAD_E_UNSUPPORTED;
if (rad_arg_in(a, 6) && B->dtype != RAD_FP4E2M1) return RAD_E_UNSUPPORTED;
@@ -988,6 +1041,7 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
if (numel(A) < M * K || numel(B) < N * K) return RAD_E_SHAPE;
if (bias && numel(bias) < N) return RAD_E_SHAPE;
/* quant_act_i8's codes arrive with the middle two dwords of every 16-byte step exchanged;
* exchange them back into a contiguous copy. */
RadTensor Ac;