format: import ParoQuant MXFP4 checkpoints (givens transform with parts, per-row fp8 scale)

quant_method paroquant_mxfp4 is Quark's MXFP4 planes in a rotated basis plus z-lab's learned
Givens rotations. rad-convert reads the codes and exponents byte for byte and builds the
encoding's givens tables (t.pairs/t.angle/t.scale) from pairs/theta/channel_scales; a fused
gate|up or qkv|z gets one set of tables a checkpoint tensor (new t.part table). The core decoder
undoes the per-part rotation. fp8 tensors with one f32 scale a row are read exactly.
This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent 89cee7ce41
commit 3402acf82d
6 files changed
+458 -24

No files matched your search

+163 -6
View File
@@ -543,6 +543,15 @@ void Checkpoint::detect_import() {
else
import_why_ = "Quark's weight scheme here is not MXFP4 (fp4, per_group 32, e8m0 scales) "
"throughout, which is the one this reads";
} else if (qm == "paroquant_mxfp4") {
/* QUARK'S MXFP4 PLANES IN A ROTATED BASIS: e2m1 codes, an e8m0 exponent a 32 of K, and per
* projection the learned Givens rotations (`pairs`, `theta`) and channel scales in groups
* of 128 -- z-lab's ParoQuant, retrained for the MXFP4 grid. */
if (qc("format") == "mxfp4" && qc("mxfp4_block") == "32" && qc("group_size") == "128")
import_ = Import::ParoMx;
else
import_why_ = "ParoQuant's MXFP4 here is not format mxfp4 with 32-element blocks and "
"rotation groups of 128, which is the one this reads";
}
/* THE TOOL'S DESCRIPTION IS KEPT, UNDER ANOTHER NAME. A plugin reads quantization_config as
* the statement of the format it serves (qwen35_fp8 checks quant_method is "fp8"), and an
@@ -566,7 +575,7 @@ int Checkpoint::import_piece(const std::string& name, const SrcTensor** codes,
return RAD_OK;
}
if (import_ == Import::QuarkMx) {
if (import_ == Import::QuarkMx || import_ == Import::ParoMx) {
/* QUARK'S MXFP4 IS THE ENCODING AS IT STANDS: two codes a byte, element 2j in the low
* nibble, and one biased exponent a 32 of K -- `fp4_e2m1*e8m0[1x32]`. A view renames the
* dtypes and the logical width; no byte moves. Its excluded layers are plain tensors. */
@@ -761,7 +770,7 @@ std::string ckpt_tensor_of(const SrcTensor& t) {
bool ckpt_reads_any(const CkptWeight& w, const std::unordered_set<std::string>& names,
std::unordered_set<std::string>* hit) {
bool any = false;
for (const auto* list : { &w.pieces, &w.scales })
for (const auto* list : { &w.pieces, &w.scales, &w.pq_pairs, &w.pq_theta, &w.pq_scale })
for (const SrcTensor* t : *list) {
if (!t) continue;
const std::string n = ckpt_tensor_of(*t);
@@ -773,6 +782,90 @@ bool ckpt_reads_any(const CkptWeight& w, const std::unordered_set<std::string>&
return any;
}
namespace {
/* The ROTATION GROUP of a ParoQuant checkpoint: pairs address columns within a group of 128. */
constexpr int64_t kParoGroup = 128;
/* "model.layers.0.mlp.down_proj.weight" -> "model.layers.0.mlp.down_proj" */
std::string paro_base(const SrcTensor& piece) {
std::string n = ckpt_tensor_of(piece);
const std::string suffix = ".weight";
return n.size() > suffix.size() && n.compare(n.size() - suffix.size(), suffix.size(), suffix) == 0
? n.substr(0, n.size() - suffix.size()) : n;
}
} /* namespace */
namespace {
/* PARTS: EACH PIECE OF A ROTATED WEIGHT HAS ITS OWN ROTATION. A fused gate_up or q|k|v is several
* checkpoint projections, each learned with its own pairs, angles and channel scales, so the
* fused weight is one row PART per piece (abi/rad_encoding.h, "givens"). Gives the encoding its
* four tables -- t.pairs and t.angle stage-major per part, t.scale a row per part, t.part the end
* row of each -- and refuses a rotation that is not what the kernels read: a pair that leaves its
* group of 128, or a stage that does not use every column of a group exactly once. */
int paro_attach(Checkpoint& ck, CkptWeight* out, std::string* why) {
auto refuse = [&](std::string m) {
if (why) *why = std::move(m);
return (int)RAD_E_FORMAT;
};
const int64_t K = out->cols, P = (int64_t)out->pieces.size();
if (K % kParoGroup) return refuse("'" + out->name + "' has " + std::to_string(K) +
" columns, which is not whole rotation groups of 128");
int64_t S = 0;
out->pq_pairs.clear(); out->pq_theta.clear(); out->pq_scale.clear();
for (const SrcTensor* piece : out->pieces) {
const std::string base = paro_base(*piece);
const SrcTensor* pr = ck.find(base + ".pairs");
const SrcTensor* th = ck.find(base + ".theta");
const SrcTensor* cs = ck.find(base + ".channel_scales");
if (!pr || !th || !cs)
return refuse("'" + piece->name + "' is a ParoQuant MXFP4 weight and '" + base +
"' lacks its pairs, theta or channel_scales");
if (pr->rad_dtype != RAD_I16 || th->rad_dtype != RAD_F16 || cs->rad_dtype != RAD_F16 ||
pr->shape.size() != 2 || th->shape.size() != 2 || cs->shape.size() != 2 ||
pr->shape[1] != K || th->shape[0] != pr->shape[0] || th->shape[1] != K / 2 ||
cs->shape[0] != 1 || cs->shape[1] != K || pr->shape[0] < 1)
return refuse("'" + base + "' pairs/theta/channel_scales do not fit a " +
std::to_string(K) + "-column weight (int16 [S, K], f16 [S, K/2], f16 [1, K])");
if (S == 0) S = pr->shape[0];
if (pr->shape[0] != S)
return refuse("the pieces of '" + out->name + "' have different numbers of rotation stages");
/* every stage is a perfect matching of each group's 128 columns */
const int16_t* pv = (const int16_t*)pr->data;
for (int64_t st = 0; st < S; ++st)
for (int64_t g = 0; g < K / kParoGroup; ++g) {
bool seen[kParoGroup] = {};
for (int64_t i = 0; i < kParoGroup; ++i) {
const int v = pv[st * K + g * kParoGroup + i];
if (v < 0 || v >= kParoGroup || seen[v])
return refuse("'" + pr->name + "' stage " + std::to_string(st) + " group " +
std::to_string(g) + " is not a pairing of its 128 columns");
seen[v] = true;
}
}
out->pq_pairs.push_back(pr);
out->pq_theta.push_back(th);
out->pq_scale.push_back(cs);
}
RadEncoding& e = out->enc;
rad_enc_copy_str(e.transform, "givens");
rad_enc_add_table(&e, "t.pairs", RAD_I32, P * S, K);
rad_enc_add_table(&e, "t.angle", RAD_F32, P * S, K / 2);
rad_enc_add_table(&e, "t.scale", RAD_F32, P, K);
if (P > 1) rad_enc_add_table(&e, "t.part", RAD_I32, 1, P);
if (!rad_enc_valid(&e)) return refuse("the rotation tables of '" + out->name + "' are malformed");
out->imported = true;
out->paro = true;
out->trivial = false;
out->why = "it is ParoQuant MXFP4, which rad-convert imports with its rotation tables -- "
"serve the container it writes";
return RAD_OK;
}
} /* namespace */
int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, bool optional,
CkptWeight* out, std::string* why) {
auto refuse = [&](int rc, std::string m) {
@@ -808,6 +901,14 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
for (const Src1& s1 : srcs) {
const SrcTensor* st = ck.find(s1.name);
const SrcTensor* sc = st ? ck.find(scale_name(s1.name)) : nullptr;
/* ParoQuant's fp8 heads and embedding carry ONE f32 SCALE A ROW (`X.weight_scale`, [N]),
* not a block plane: Quark's `_scale` naming for a per-channel weight. */
if (st && !sc && ck.imports() == Checkpoint::Import::ParoMx &&
(st->rad_dtype == RAD_F8E4M3 || st->rad_dtype == RAD_F8E5M2) && st->shape.size() == 2) {
const SrcTensor* pr = ck.find(std::string(s1.name) + "_scale");
if (pr && pr->rad_dtype == RAD_F32 && pr->shape.size() == 1 && pr->shape[0] == st->shape[0])
sc = pr;
}
/* A WEIGHT THE CHECKPOINT SHIPS IN AN IMPORTED FORM is read through that form's views. */
{
const SrcTensor *ic = nullptr, *is = nullptr;
@@ -923,6 +1024,10 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
rad_enc_copy_str(out->enc.scheme, "affine");
rad_enc_add_plane(&out->enc, "codes", RAD_FP4E2M1, 1, 1);
rad_enc_add_plane(&out->enc, "scale", RAD_E8M0, 1, 32);
if (ck.imports() == Checkpoint::Import::ParoMx) {
const int rc = paro_attach(ck, out, why);
if (rc != RAD_OK) return rc;
}
}
} else if ((dt == RAD_F8E4M3 || dt == RAD_F8E5M2) && scaled) {
/* BLOCK-SCALED FP8, byte for byte: the codes and the scale plane are the planes of
@@ -944,7 +1049,10 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
const int64_t want_r = (r + br - 1) / br, want_c = (c + bc - 1) / bc;
int64_t sr = 0, scl = 0;
rad_enc_view((uint32_t)sk.shape.size(), sk.shape.data(), &sr, &scl);
if (sk.rad_dtype != sdt || sr != want_r || scl != want_c) {
const bool per_row = sk.shape.size() == 1; /* one scale a row; see above */
if (per_row && sk.shape[0] == r && sk.rad_dtype == sdt) {
/* fits */
} else if (per_row || sk.rad_dtype != sdt || sr != want_r || scl != want_c) {
out->trivial = false;
out->why = "'" + sk.name + "' is not the [" + std::to_string(want_r) + ", " +
std::to_string(want_c) + "] " + std::to_string(br) + "x" +
@@ -959,7 +1067,8 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
rad_enc_clear(&out->enc);
rad_enc_copy_str(out->enc.scheme, "affine");
rad_enc_add_plane(&out->enc, "codes", dt, 1, 1);
rad_enc_add_plane(&out->enc, "scale", sdt, br, bc);
if (out->scales[0]->shape.size() == 1) rad_enc_add_plane(&out->enc, "scale", sdt, 1, 0);
else rad_enc_add_plane(&out->enc, "scale", sdt, br, bc);
}
} else if (!unscaled) {
out->why = "some of its pieces carry a scale plane and some do not";
@@ -1127,6 +1236,11 @@ int ckpt_rows_f32(const CkptWeight& w, int64_t r0, int64_t nr, float* out, std::
return rc;
};
if (r0 < 0 || nr < 0 || r0 + nr > w.rows) return refuse(RAD_E_SHAPE, "rows out of range");
/* A ROTATED WEIGHT's codes are in the rotated basis; its f32 value would be the unrotated one,
* which only a decoder over the container's tables computes. Nothing quantises it again. */
if (w.paro)
return refuse(RAD_E_UNSUPPORTED, "'" + w.name + "' is a ParoQuant weight, stored rotated; "
"it is kept as it is and has no f32 view here");
Joined j;
j.init(w.pieces, w.concat_dim);
/* the runs come in order, so the cursor is the elements already written */
@@ -1165,13 +1279,15 @@ int ckpt_rows_f32(const CkptWeight& w, int64_t r0, int64_t nr, float* out, std::
* are that piece's. */
if (!w.scales.empty() && w.scales[0]) {
const int64_t br = w.enc.n_planes > 1 ? w.enc.plane[1].block[0] : 128;
const int64_t bc = w.enc.n_planes > 1 ? w.enc.plane[1].block[1] : 128;
/* a block extent of 0 is the whole extent: a scale a row, one column of scales */
const int64_t bc0 = w.enc.n_planes > 1 ? w.enc.plane[1].block[1] : 128;
int64_t base = 0;
for (size_t k = 0; k < w.pieces.size(); ++k) {
const SrcTensor& pk = *w.pieces[k];
const SrcTensor& sk = *w.scales[k];
int64_t pr = 0, pc = 0;
rad_enc_view((uint32_t)pk.shape.size(), pk.shape.data(), &pr, &pc);
const int64_t bc = bc0 > 0 ? bc0 : pc;
const int64_t sc = (pc + bc - 1) / bc;
for (int64_t r = std::max(r0, base); r < std::min(r0 + nr, base + pr); ++r)
for (int64_t c = 0; c < w.cols; ++c)
@@ -1198,7 +1314,47 @@ int ckpt_plane_rows(const CkptWeight& w, int k, int64_t r0, int64_t nr, void* ds
const uint32_t dt = w.enc.plane[k].dtype;
const int64_t eb = rad_dtype_bits(dt) / 8;
uint8_t* o = (uint8_t*)dst;
if (w.imported) {
if (w.paro && k >= 2) {
/* A ROTATION TABLE, built from the checkpoint's own tensors a row at a time: the pairs
* from group-local to whole-row columns, the angles widened, the channel scale inverted
* (the checkpoint multiplies the activation by it; the encoding's t.scale is the factor
* the stored row was MULTIPLIED by, so the activation is divided), the part ends. */
const RadEncPlane& pl = w.enc.plane[k];
const int64_t P = (int64_t)w.pieces.size(), S = w.pq_pairs[0]->shape[0], K = w.cols;
const std::string role = pl.role;
for (int64_t r = r0; r < r0 + nr; ++r) {
if (role == "t.pairs") {
const int16_t* pv = (const int16_t*)w.pq_pairs[(size_t)(r / S)]->data + (r % S) * K;
int32_t* d = (int32_t*)o;
for (int64_t c = 0; c < K; ++c)
d[c] = (int32_t)((c / kParoGroup) * kParoGroup + pv[c]);
o += K * 4;
} else if (role == "t.angle") {
const SrcTensor& th = *w.pq_theta[(size_t)(r / S)];
float* d = (float*)o;
for (int64_t c = 0; c < K / 2; ++c)
d[c] = rad_load_f32(th.data, th.rad_dtype, (r % S) * (K / 2) + c);
o += K / 2 * 4;
} else if (role == "t.scale") {
const SrcTensor& cs = *w.pq_scale[(size_t)r];
float* d = (float*)o;
for (int64_t c = 0; c < K; ++c) d[c] = 1.0f / rad_load_f32(cs.data, cs.rad_dtype, c);
o += K * 4;
} else if (role == "t.part") {
int32_t* d = (int32_t*)o;
int64_t end = 0;
for (int64_t q = 0; q < P; ++q) {
end += w.pieces[(size_t)q]->shape[0];
d[q] = (int32_t)end;
}
o += P * 4;
} else {
return refuse(RAD_E_STATE, "plane '" + role + "' of a ParoQuant weight is not a table");
}
}
return RAD_OK;
}
if (w.imported && !w.paro) {
/* GPTQ-PACKED, RE-LAID: a row of codes is a column of the qweight, its words' bytes in
* order -- two codes a byte for int4, low nibble first, as the encoding packs them -- each
* offset to signed by flipping the top bit of every field; a row of scales is a column of
@@ -1249,6 +1405,7 @@ int ckpt_plane_rows(const CkptWeight& w, int k, int64_t r0, int64_t nr, void* ds
for (const SrcTensor* s : src) {
int64_t sr = 0, scl = 0;
rad_enc_view((uint32_t)s->shape.size(), s->shape.data(), &sr, &scl);
if (s->shape.size() == 1) { sr = s->shape[0]; scl = 1; } /* one scale a row */
const int64_t lo = std::max(r0, base), hi = std::min(r0 + nr, base + sr);
if (hi > lo) {
std::memcpy(o, (const uint8_t*)s->data + (lo - base) * scl * eb,