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

+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>