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;
+13
View File
@@ -69,9 +69,22 @@ struct Scales {
int64_t group; /* K elements per scale */
int64_t rblk; /* operand rows per scale row; 1 unless the grid is block-scaled */
bool e8m0; /* the scale stream is OCP e8m0 exponent bytes, not floats */
/* M2XFP4 (libquant's `m2xfp4`, lq_m2xfp.cpp): `group` is 8, so `g` names a subgroup. The
* E8M0 byte is one per four subgroups and the meta plane's two-bit field refines it, the
* scale of subgroup g being e8m0(scale[g >> 2]) . (1 + k/4) with k the field's value -- the
* quantiser's own decode, which this states where it folds the product. */
const uint8_t* m2_meta = nullptr; /* [rows][K/32] meta bytes, canonical rows */
int64_t m2_mrs = 0; /* bytes between meta rows */
bool m2 = false;
/* The scale of group `g` of `row`. */
inline float grp(int64_t row, int64_t g) const {
if (m2) {
const int64_t gi = g >> 2;
const int64_t i = (rblk > 1 ? row / rblk : row) * rs + gi * cs;
const int k = (m2_meta[row * m2_mrs + gi] >> (2 * (g & 3))) & 3;
return e8m0_to_f32(((const uint8_t*)p)[i]) * (1.0f + 0.25f * (float)k);
}
int64_t i = (rblk > 1 ? row / rblk : row) * rs + g * cs;
return e8m0 ? e8m0_to_f32(((const uint8_t*)p)[i]) : ld_dt(dt, p, i);
}
+101 -2
View File
@@ -182,15 +182,70 @@ static inline int ref_layout_mtp(const RadParam*, int, int operand, const RadEnc
return layout_scaled(operand, 4, 5, enc, sel, planes, n, out);
}
/* M2XFP4, the `m2xfp4` quantiser's own scheme (libquant): three TILED planes over 32-column
* groups, and gemm_nt_q's weight operand takes the codes and the meta. Their stored form is the
* two planes' canonical bytes concatenated -- the codes' rows, then the meta's -- the form the
* core's selection reader hands a kernel that decodes the scheme itself beside the scale stream
* another operand carries (core/format/encoding.cpp), and the one both libraries' m2 arms read.
* libavx/avx_planes.h states the same rules. */
static inline bool m2_enc_planes(const RadEncoding* enc) {
if (!rad_enc_is(enc, "m2xfp4")) return false;
/* A rotated weight (ParoQuant) carries the same three planes in the basis its "givens"
* transform names, with the rotation's tables beside them; the rotation is the activation's,
* so the planes are read as they are. */
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");
}
/* gemm_nt_q: `b_scale` (operand 3) as the scale and zero of the zero-point grid `dtype` names,
* one each per (row, `group` of K), over codes of that grid's width. The codes (operand 2) are one
* plane and stored as they are. A "givens" transform is the activation's (givens_rotate,
* givens_dense_quant), so the planes are read the same way under it. */
* givens_dense_quant), so the planes are read the same way under it. At `m2xfp4` the weight
* operand takes codes and meta concatenated (above); the scale and reference operands are single
* planes, stored as they are. */
static inline int ref_layout_gemm_q(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) {
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;
}
if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED;
const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr));
const ZpGrid* zg = zp_grid(dt);
const long long group = rad_param_geti(p, n_p, "group", 0);
if (!zg || group <= 0 || !rad_enc_is(enc, "affine")) return RAD_E_DTYPE;
if (enc->transform[0] && std::strncmp(enc->transform, "givens", RAD_ENC_STR) != 0)
@@ -208,6 +263,50 @@ static inline int ref_layout_gemm_q(const RadParam* p, int n_p, int operand,
return rad_planes_layout(enc, sel, planes, n, out);
}
/* gemm_nt_q's own relayout: the m2 arm's concatenated form beside the zero-point grids' joined
* one, each as its layout hook laid it out. */
template <RadLayoutFn L>
static int ref_relayout_gemm_q(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 = L(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
if (lay.tag && std::strcmp(lay.tag, "m2.codes.meta") == 0) {
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 rb = rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
const int64_t b = planes[i].shape[0] * rb;
std::memcpy(d, planes[i].data, (size_t)b);
d += b;
}
return RAD_OK;
}
return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes);
}
template <RadLayoutFn L>
static int ref_unrelayout_gemm_q(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 = L(p, n_p, operand, enc, sel, planes, n, &lay);
if (st != RAD_OK) return st;
if (lay.tag && std::strcmp(lay.tag, "m2.codes.meta") == 0) {
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 rb = rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
const int64_t b = planes[i].shape[0] * rb;
std::memcpy(planes[i].data, s, (size_t)b);
s += b;
}
return RAD_OK;
}
return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n);
}
/* RadRelayoutFn and RadUnrelayoutFn for any of the layout hooks above: the hook decides whether
* the planes are joined, and the copies are rad_planes.h's. */
template <RadLayoutFn L>
+14 -3
View File
@@ -2003,6 +2003,17 @@ static int ref_check_gemm_q(const char* op, const RadArgs* a) {
}, \
nullptr, lay, ref::ref_relayout_planes<lay>, fn##_shape, ref::ref_unrelayout_planes<lay> }
/* gemm_nt_q: its zero-point grids join scale and zero (ref_planes.h) while its m2xfp4 arm
* concatenates codes and meta, so the row carries the hook's own relayout pair. */
#define ROWL_GQ(nm, opname, fam, what, dts, check, fn, lay) \
{ 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, ref::ref_relayout_gemm_q<lay>, fn##_shape, ref::ref_unrelayout_gemm_q<lay> }
#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*)"
@@ -2178,9 +2189,9 @@ 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",
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, ref::ref_layout_gemm_q),
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),
+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) {