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

+66
View File
@@ -376,6 +376,64 @@ static int gemm_zp(const ZpGrid& zg, const RadTensor* A, const RadTensor* as_,
return RAD_OK;
}
/* ------------------------------------------------------------------ M2XFP4 */
/* 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 (the shape hook's m2xfp4 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
* (libr4d's r4d_m2xfp4.h folds the same law into its staging table); each product is one f32
* multiply, each row 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. */
static const float kM2E2M1[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };
static int gemm_m2(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 */
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<float> 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 = (c & 8 ? -kM2E2M1[c & 7] : kM2E2M1[c & 7]);
const float ev = std::ldexpf(1.0f, (int)expe[k >> 5] - 127);
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;
}
static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
const int nt = bias_operand ? 6 : 5;
if (a->n_t < nt || !a->t[0].data || !a->t[2].data || !a->t[nt - 1].data) return RAD_E_INVAL;
@@ -391,6 +449,14 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
* only. */
const ZpGrid* zg = 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.
* Parts (pb1/pb2) are the rotated grids'; the m2 declare makes none, and a case carrying
* them is declined rather than answered unsplit. */
if (!bias_operand && p_str(a, "dtype", nullptr) &&
std::strcmp(p_str(a, "dtype", nullptr), "m2xfp4") == 0) {
if (p_int(a, "pb1", 0) > 0 || p_int(a, "pb2", 0) > 0) return RAD_E_UNSUPPORTED;
return gemm_m2(a);
}
if (!bias_operand) {
if (t_in(a, 5) && (!zg || zg->act != ZP_I8_ROW)) return RAD_E_UNSUPPORTED;
if (t_in(a, 6) && B->dtype != RAD_FP4E2M1) return RAD_E_UNSUPPORTED;