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

+24 -17
View File
@@ -1154,10 +1154,10 @@ int ref_gemm_nt_bias_shape(const RadParam* p, int n_p, int operand, RadOpdDesc*
* The fp8 grid is stated by the checkpoint: E4M3 [N][K] against a bf16 [ceil(N/128)][ceil(K/128)]
* plane, `group` carrying the 128. w4a8 and w8a8 are the affine encoding's two planes, codes and
* an f16 scale per (row, `group` of K), against int8 codes in the step order with one f32 scale a
* row. `a_sum` (asymmetric grids) is absent, and so is `b_ref` except at mxfp4a8. Activation
* scales are drawn in (0, 1): a quantiser's are never negative, and an MXFP4 kernel treats a
* block whose scale is not positive as empty (docs/OPS.md). */
static int gemm_q_shape(const RadParam* p, int n_p, int operand, bool bias, RadOpdDesc* out) {
* row. `a_sum` (asymmetric grids) is absent, and so is `b_ref` except at mxfp4a8 and m2xfp4.
* Activation scales are drawn in (0, 1): a quantiser's are never negative, and an MXFP4 kernel
* treats a block whose scale is not positive as empty (docs/OPS.md). */
int gemm_q_shape(const RadParam* p, int n_p, int operand, bool bias, RadOpdDesc* out) {
const int64_t M = gi(p, n_p, "M", 0), N = gi(p, n_p, "N", 0), K = gi(p, n_p, "K", 0);
const int64_t group = gi(p, n_p, "group", 0);
const char* q = gs(p, n_p, "dtype");
@@ -1165,11 +1165,11 @@ static int gemm_q_shape(const RadParam* p, int n_p, int operand, bool bias, RadO
const bool f32s = is("fp8a8f32");
const bool a8 = is("fp8a8") || f32s, a16 = is("fp8a16"), w4 = is("w4a8"), w8 = is("w8a8");
/* i8a8h is i8a8's operands; its rotation is in the values. */
const bool i8 = is("i8a8") || is("i8a8h"), mx = is("mxfp4a8");
if ((!a8 && !a16 && !w4 && !w8 && !i8 && !mx) || N <= 0 || K <= 0 || group <= 0)
const bool i8 = is("i8a8") || is("i8a8h"), mx = is("mxfp4a8"), m2 = is("m2xfp4");
if ((!a8 && !a16 && !w4 && !w8 && !i8 && !mx && !m2) || N <= 0 || K <= 0 || group <= 0)
return RAD_E_UNSUPPORTED;
/* Neither has a bias form in any architecture. */
if (bias && (i8 || mx)) return RAD_E_UNSUPPORTED;
if (bias && (i8 || mx || m2)) return RAD_E_UNSUPPORTED;
/* The bias form requires `a_scale`, so the fp8a16 reading (bf16 activation, no scale) cannot
* be expressed there. */
if (bias && a16) return RAD_E_UNSUPPORTED;
@@ -1177,20 +1177,27 @@ static int gemm_q_shape(const RadParam* p, int n_p, int operand, bool bias, RadO
const int64_t kb = (K + group - 1) / group;
const int64_t nb = (N + group - 1) / group;
Opd o;
if (i8 || mx) {
if (i8 || mx || m2) {
/* i8a8: quant_act_i8g's row-major codes and f32 scale per (row, `group` of K) against int8
* codes with a bf16 scale per (row, `group` of K). mxfp4a8: quant_act_fp8's pair at 128
* against e2m1 codes with an E8M0 exponent per (row, `group` = 32 of K); `b_ref` is the same
* exponent plane, which an mxfp4 kernel reduces to a per-row reference at load. */
const int64_t akb = mx ? (K + 127) / 128 : kb;
o.t((uint32_t)(mx ? RAD_F8E4M3 : RAD_I8), { M, K }); /* a */
o.t_fill((uint32_t)RAD_F32, { M, akb }, (uint32_t)RAD_FILL_SIGMOID, 0); /* a_scale */
o.t((uint32_t)(mx ? RAD_FP4E2M1 : RAD_I8), { N, K }); /* b */
o.t((uint32_t)(mx ? RAD_E8M0 : RAD_BF16), { N, kb }); /* b_scale */
o.t((uint32_t)RAD_BF16, { M, N }); /* y */
o.skip(); /* a_sum */
if (mx) o.t((uint32_t)RAD_E8M0, { N, kb }); /* b_ref */
* exponent plane, which an mxfp4 kernel reduces to a per-row reference at load. m2xfp4:
* the mxfp4a8 operands with the metadata plane riding `b`, whose (codes,meta) selection
* reads back as the two planes' bytes concatenated (encoding.cpp) -- the codes rows, then
* the meta rows -- which ref_gemm.cpp's own arm decodes against `b_scale`. */
const int64_t akb = (mx || m2) ? (K + 127) / 128 : kb;
o.t((uint32_t)((mx || m2) ? RAD_F8E4M3 : RAD_I8), { M, K }); /* a */
o.t_fill((uint32_t)RAD_F32, { M, akb }, (uint32_t)RAD_FILL_SIGMOID, 0); /* a_scale */
if (m2)
o.t((uint32_t)RAD_U8, { N * (K / 2 + K / 32) }); /* b -- codes then meta bytes */
else
o.t((uint32_t)(mx ? RAD_FP4E2M1 : RAD_I8), { N, K }); /* b */
o.t((uint32_t)((mx || m2) ? RAD_E8M0 : RAD_BF16), { N, kb }); /* b_scale */
o.t((uint32_t)RAD_BF16, { M, N }); /* y */
o.skip(); /* a_sum */
if (mx || m2) o.t((uint32_t)RAD_E8M0, { N, kb }); /* b_ref */
else o.skip();
if (m2 && (group != 32 || K % 32)) return RAD_E_UNSUPPORTED;
return o.finish("gemm_nt_q", operand, out);
}
if (w4 || w8) {