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

+110 -15
View File
@@ -1907,15 +1907,104 @@ int Replay::rerecord() {
return 0;
}
/* The gemm_nt_q mx/m2 family, re-encoded for a row the recording's container never fed: the
* m2xfp4 rows sit at the m2xfp4 encoding, which no in-tree container carries, and the OCP rows'
* mxfp4a8 cases exist only at containers this fixture was not recorded from. Moving a case
* between the two families keeps the codes and exponent planes -- the same planes at both -- and
* draws the meta plane beside them, whose every byte is a valid multiplier, so the reference
* records at the row's own encoding. `d` already carries the reference's description of the
* operand at the target dtype (`sh`); this fills its encoding and selection. False when the row
* is not after one of the two families, the base encoding is the same family or neither, or the
* planes do not line up: the caller's strict shape check then governs. */
static bool reencode_gemmx_weight(const KfOperand& b, const char* row_dtype, const OpdShape& sh,
KfOperand* d, std::string* why) {
if (!b.has_enc || !row_dtype) return false;
const bool to_m2 = std::strcmp(row_dtype, "m2xfp4") == 0;
const bool to_mx = std::strcmp(row_dtype, "mxfp4a8") == 0;
if (!to_m2 && !to_mx) return false;
auto bad = [&](const char* w) { if (why) *why = w; return false; };
/* The base family: the m2xfp4 quantiser's own scheme, or affine MXFP4 (codes plus one E8M0
* exponent a group of 32). */
const RadEncoding& e = b.enc;
const bool base_m2 = rad_enc_is(&e, "m2xfp4") && e.n_planes == 3;
const bool base_mx = rad_enc_is(&e, "affine") && e.n_planes >= 2;
if (base_m2 == base_mx) return bad("its weight is neither the mxfp4a8 nor the m2xfp4 grid");
if (to_m2 && base_m2) return false; /* same family: the plain copy serves */
if (to_mx && base_mx) return false;
const int ic = 0;
const int is = rad_enc_find(&e, "scale");
const int im = base_m2 ? rad_enc_find(&e, "meta") : -1;
if (is < 0 || (base_m2 && im < 0)) return bad("its encoding has no exponent (or meta) plane");
if (e.transform[0]) return bad("its weight carries a transform the target family does not");
if (e.plane[ic].dtype != RAD_FP4E2M1 || e.plane[ic].block[0] != 1 ||
e.plane[is].block[0] != 1 || e.plane[is].block[1] != 32 ||
std::strncmp(e.plane[ic].role, "codes", RAD_ENC_STR) != 0)
return bad("its codes and exponent planes are not the MXFP4 grid's");
/* The target encoding: the same codes and exponents, the meta plane beside them. */
RadEncoding t{};
rad_enc_copy_str(t.scheme, to_m2 ? "m2xfp4" : "affine");
rad_enc_add_plane(&t, "codes", RAD_FP4E2M1, 1, 1);
rad_enc_add_plane(&t, "scale", RAD_E8M0, 1, 32);
if (to_m2) rad_enc_add_plane(&t, "meta", RAD_U8, 1, 32);
if (!rad_enc_valid(&t)) return bad("the re-encoded weight is not well formed");
/* What this operand reads, by the planes it selected: the weight (the codes, and the meta at
* m2) or a scale stream (b_scale and b_ref select the exponent plane). Its extents come from
* the plane it selected, in that plane's own elements. */
const bool codes_opd = std::find(b.sel.begin(), b.sel.end(), ic) != b.sel.end();
int64_t N = 0, kb = 0;
for (size_t k = 0; k < b.sel.size(); ++k) {
if (b.sel[k] == ic) { N = b.sel_rows[k]; kb = b.sel_cols[k] / 32; }
if (b.sel[k] == is) { N = b.sel_rows[k]; kb = b.sel_cols[k]; }
}
if (N <= 0 || kb <= 0) return bad("its planes are not a whole number of exponent groups");
const int64_t K = kb * 32;
auto fits = [&](const char* what, uint32_t dt, std::initializer_list<int64_t> shape) {
if (sh.dtype != dt || sh.shape != std::vector<int64_t>(shape)) {
if (why)
*why = std::string("the reference describes ") + what + " differently than the " +
(to_m2 ? "m2xfp4" : "mxfp4a8") + " grid implies";
return false;
}
return true;
};
std::vector<int> sel;
std::vector<int64_t> sel_rows, sel_cols;
if (codes_opd) {
if (to_m2) {
if (!fits("the weight", RAD_U8, { N * (K / 2 + kb) })) return false;
sel = { 0, 2 }; sel_rows = { N, N }; sel_cols = { K, kb };
} else {
if (!fits("the weight", RAD_FP4E2M1, { N, K })) return false;
sel = { 0 }; sel_rows = { N }; sel_cols = { K };
}
} else {
if (!fits("the scale stream", RAD_E8M0, { N, kb })) return false;
sel = { 1 }; sel_rows = { N }; sel_cols = { kb };
}
d->has_enc = true;
d->enc = t;
d->sel = std::move(sel);
d->sel_rows = std::move(sel_rows);
d->sel_cols = std::move(sel_cols);
return true;
}
/* ================================================================== the row-driven fixture
*
* A recording reaches only the rows its container's geometry selects; a row behind another band,
* tuned point or dtype never runs. For every row of every library under test, this moves an
* existing case of the row's op into the row's constraints (kb::satisfy_constraints) and records
* the reference's answer there. Only cases whose operand shapes the reference can describe from the
* parameters alone, and whose weights keep their shapes, are moved: a weight of another shape would
* need an encoding the base case does not carry. Replay the result with --all-rows, since a row
* a higher-priority row shadows is reached only that way. No container and no card are needed. */
* tuned point or dtype never runs. For every row of every library under test, this moves the
* fixtures' cases into the row's constraints (kb::satisfy_constraints) and records the reference's
* answer at every distinct geometry that lands there -- one case serves every row sharing it, and
* a row keeps sweeping instead of stopping at the first base case, so a band's whole M ladder is
* covered. Only cases whose operand shapes the reference can describe from the parameters alone,
* and whose weights keep their shapes (or re-encode, above), are moved: a weight of another shape
* would need an encoding the base case does not carry. Replay the result with --all-rows, since a
* row a higher-priority row shadows is reached only that way. No container and no card are
* needed. */
int Replay::record_rows() {
Kfixture out;
out.oracle = ref_name_;
@@ -1923,7 +2012,7 @@ int Replay::record_rows() {
for (size_t fi = 0; fi < fx_.size(); ++fi)
out.source += (out.source.empty() ? "rows from " : " + ") + fx_[fi].source;
std::set<std::string> seen;
int rows_total = 0, rows_recorded = 0;
int rows_total = 0, rows_served = 0, rows_recorded = 0;
std::map<std::string, int> why_not;
for (const KernelRow& r : reg_->rows()) {
if (!r.info || !r.info->launch || !r.info->op) continue;
@@ -1939,10 +2028,9 @@ int Replay::record_rows() {
if (!schema) continue;
++rows_total;
std::string last = "no recorded case of its op";
bool done = false;
for (size_t fi = 0; fi < fx_.size() && !done; ++fi) {
bool any = false;
for (size_t fi = 0; fi < fx_.size(); ++fi) {
for (const KfCase& base : fx_[fi].cases) {
if (done) break;
if (base.op != r.info->op || (int)base.opd.size() != schema->n_operands) continue;
Geometry g = case_geometry(base);
if (!kb::satisfy_constraints(r.info, &g)) {
@@ -1986,6 +2074,12 @@ int Replay::record_rows() {
d.idx_const = sh[k].idx_const;
d.fill_chunk = sh[k].fill_chunk;
if (d.role == RAD_OPD_WEIGHT || d.role == RAD_OPD_WTAB) {
/* The mx/m2 pair re-encodes between families; everything else must keep
* the weight the base case recorded. */
std::string rwhy;
if (!d.absent && b.has_enc &&
reencode_gemmx_weight(b, g.get_s("dtype"), sh[k], &d, &rwhy))
continue;
if (d.absent != b.absent || d.dtype != b.dtype || d.shape != b.shape) {
weights_kept = false;
break;
@@ -2015,17 +2109,18 @@ int Replay::record_rows() {
}
c.seed = rad_case_seed(base.seed, r.info->name, 0, r.info->domain, 0);
const std::string key = kb::cap_key(c, "", "", 0, g);
if (seen.count(key)) { done = true; ++rows_recorded; break; }
if (seen.count(key)) { any = true; continue; }
KfCase rc;
std::string why;
if (record_case(c, &rc, &why) != RAD_OK) { last = why; continue; }
seen.insert(key);
out.cases.push_back(std::move(rc));
++rows_recorded;
done = true;
any = true;
}
}
if (!done) why_not[std::string(r.plugin) + "/" + r.info->name + " (" + last + ")"]++;
if (any) ++rows_served;
else why_not[std::string(r.plugin) + "/" + r.info->name + " (" + last + ")"]++;
}
if (!why_not.empty()) {
std::printf("rows without a case\n");
@@ -2036,8 +2131,8 @@ int Replay::record_rows() {
RAD_ERR("--record-rows %s: %s", o_.record_rows_path.c_str(), rad_strerror(rc));
return 1;
}
std::printf("recorded %zu case(s) for %d of %d row(s) to %s; replay it with --all-rows\n",
out.cases.size(), rows_recorded, rows_total, o_.record_rows_path.c_str());
std::printf("recorded %zu case(s) reaching %d of %d row(s) to %s; replay it with --all-rows\n",
out.cases.size(), rows_served, rows_total, o_.record_rows_path.c_str());
return 0;
}