format: import ParoQuant int5-bitplane and z-lab's int4 (u5/u4 codes, f16 scale, u8 zero, givens tables)

This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent 9af15f3a6e
commit e45bb596f4
3 files changed
+235 -2

No files matched your search

+129 -1
View File
@@ -543,6 +543,17 @@ 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") {
/* z-lab's ParoQuant at integer widths, in the "int5-bitplane" packing: AWQ nibbles of the
* low four bits plus a bit plane for the fifth, group 128, asymmetric, with the learned
* rotations beside. Other widths and packings are not read. */
if (qc("format") == "int5-bitplane" && qc("bits") == "5" && qc("group_size") == "128")
import_ = Import::ParoI5;
else if (qc("format").empty() && qc("bits") == "4" && qc("group_size") == "128")
import_ = Import::ParoI4; /* z-lab's own release: AWQ-packed int4, asymmetric */
else
import_why_ = "ParoQuant here is not the int5-bitplane format with 5-bit codes in groups "
"of 128, 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
@@ -575,6 +586,51 @@ int Checkpoint::import_piece(const std::string& name, const SrcTensor** codes,
return RAD_OK;
}
if (import_ == Import::ParoI5 || import_ == Import::ParoI4) {
const bool i5 = import_ == Import::ParoI5;
/* PARO int5: `X.weight` is X.qweight/.qweight_hi (codes), X.scales (f16 [G, N]) and
* X.qzeros/.qzeros_hi (zero points). The view carries the logical [N, K] u5 matrix; the
* planes are re-laid row by row by ckpt_plane_rows. */
const std::string suffix = ".weight";
if (name.size() <= suffix.size() ||
name.compare(name.size() - suffix.size(), suffix.size(), suffix) != 0)
return RAD_E_NOTFOUND;
const std::string base = name.substr(0, name.size() - suffix.size());
const SrcTensor* qw = find(base + ".qweight");
if (!qw) return RAD_E_NOTFOUND;
const SrcTensor* hi = i5 ? find(base + ".qweight_hi") : nullptr;
const SrcTensor* sc = find(base + ".scales");
const SrcTensor* qz = find(base + ".qzeros");
const SrcTensor* qzh = i5 ? find(base + ".qzeros_hi") : nullptr;
if ((i5 && (!hi || !qzh)) || !sc || !qz || qw->rad_dtype != RAD_I32 ||
qz->rad_dtype != RAD_I32 || sc->rad_dtype != RAD_F16 || qw->shape.size() != 2 ||
qz->shape.size() != 2 || sc->shape.size() != 2 ||
(i5 && (hi->rad_dtype != RAD_I32 || qzh->rad_dtype != RAD_I32 ||
hi->shape.size() != 2 || qzh->shape.size() != 2)))
return refuse(RAD_E_FORMAT, "'" + qw->name + "' lacks an int32 " +
(i5 ? "qweight_hi, qzeros_hi, " : "") +
"qzeros and an f16 scales beside it");
const int64_t K = qw->shape[0], N = qw->shape[1] * 8, G = sc->shape[0];
if (N % 32 || K % 128 || G * 128 != K || sc->shape[1] != N || qz->shape[0] != G ||
qz->shape[1] != N / 8 ||
(i5 && (hi->shape[0] != K || hi->shape[1] != N / 32 || qzh->shape[0] != G ||
qzh->shape[1] != N / 32)))
return refuse(RAD_E_SHAPE, "'" + qw->name + "' planes do not fit a [" +
std::to_string(N) + ", " + std::to_string(K) + "] weight");
SrcTensor c = *qw;
c.name = name;
c.rad_dtype = i5 ? RAD_U5 : RAD_U4;
c.shape = { N, K };
c.int5 = true;
c.pbits = i5 ? 5 : 4;
c.aux[0] = hi; c.aux[1] = qz; c.aux[2] = qzh;
sliced_.push_back(std::move(c));
*codes = &sliced_.back();
*scale = sc;
imported_[name] = { *codes, *scale };
return RAD_OK;
}
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
@@ -991,6 +1047,25 @@ int ckpt_resolve(Checkpoint& ck, const Program& prog, const std::string& name, b
(gguf_dtype(p0.ggml) ? gguf_dtype(p0.ggml)->name : "block") + " tensor";
} else if (!same) {
out->why = "its pieces are stored in different dtypes";
} else if (p0.int5) {
/* PARO int5-bitplane: u5 codes, an f16 scale and a u8 zero point a 128 of K, then the
* rotation tables. Joined only along rows (gate|up), as every imported form is. */
bool fits = d == 0 || out->pieces.size() == 1;
for (const SrcTensor* pk : out->pieces) fits = fits && pk->int5 && pk->pbits == p0.pbits;
if (!fits) {
out->why = "its int5 pieces are joined along a column, across their groups";
} else {
rad_enc_clear(&out->enc);
rad_enc_copy_str(out->enc.scheme, "affine");
rad_enc_add_plane(&out->enc, "codes", p0.pbits == 5 ? RAD_U5 : RAD_U4, 1, 1);
rad_enc_add_plane(&out->enc, "scale", RAD_F16, 1, 128);
rad_enc_add_plane(&out->enc, "zero", RAD_U8, 1, 128);
const int rc = paro_attach(ck, out, why);
if (rc != RAD_OK) return rc;
out->why = std::string("it is ParoQuant int") + (p0.pbits == 5 ? "5" : "4") +
", which rad-convert imports with its zero points and rotation tables -- "
"serve the container it writes";
}
} else if (p0.gptq_bits) {
/* GPTQ-PACKED, IMPORTED: one width and one group throughout, joined only along rows --
* a column join would put two pieces' groups in one scale block. */
@@ -1314,7 +1389,60 @@ 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.paro && k >= 2) {
if (w.paro && w.pieces[0]->int5 && k <= 2) {
/* PARO int5-bitplane, RE-LAID a row of the logical [N, K] matrix at a time. The checkpoint
* packs along N: qweight [K, N/8] holds eight low nibbles per word in AWQ order (word
* column c of a group of eight sits in nibble j, ORDER[j] == c), and qweight_hi [K, N/32]
* the fifth bit of 32 columns, bit n % 32. The zero points are the same two packings over
* [G, N]. */
auto nib = [](int c) { return c % 2 == 0 ? c / 2 : 4 + c / 2; }; /* AWQ order */
auto u32 = [](const SrcTensor& t, int64_t i) {
uint32_t v;
std::memcpy(&v, (const uint8_t*)t.data + i * 4, 4);
return v;
};
int64_t base = 0;
for (size_t pi = 0; pi < w.pieces.size(); ++pi) {
const SrcTensor* pc = w.pieces[pi];
const int64_t Np = pc->shape[0], K = pc->shape[1], G = K / 128;
const SrcTensor *hip = pc->aux[0], *qzp = pc->aux[1], *qzhp = pc->aux[2];
const SrcTensor& sc = *w.scales[pi];
for (int64_t r = std::max(r0, base); r < std::min(r0 + nr, base + Np); ++r) {
const int64_t n = r - base;
const int cn = (int)(n % 8);
if (k == 0) {
const int pb = pc->pbits;
const int64_t rb = (K * pb + 7) / 8;
std::memset(o, 0, (size_t)rb);
for (int64_t kk = 0; kk < K; ++kk) {
const uint32_t lo = (u32(*pc, kk * (Np / 8) + n / 8) >> (4 * nib(cn))) & 15u;
const uint32_t h = pb == 5 ? (u32(*hip, kk * (Np / 32) + n / 32) >> (n % 32)) & 1u : 0u;
const uint32_t code = lo | (h << 4);
const int64_t bit = kk * pb;
const uint32_t sh = (uint32_t)(bit & 7);
const uint32_t v = code << sh;
o[bit >> 3] |= (uint8_t)(v & 0xFFu);
if (sh + pb > 8) o[(bit >> 3) + 1] |= (uint8_t)(v >> 8);
}
o += rb;
} else if (k == 1) {
for (int64_t g = 0; g < G; ++g) {
std::memcpy(o, (const uint8_t*)sc.data + (g * Np + n) * 2, 2);
o += 2;
}
} else {
for (int64_t g = 0; g < G; ++g) {
const uint32_t lo = (u32(*qzp, g * (Np / 8) + n / 8) >> (4 * nib(cn))) & 15u;
const uint32_t h = pc->pbits == 5 ? (u32(*qzhp, g * (Np / 32) + n / 32) >> (n % 32)) & 1u : 0u;
*o++ = (uint8_t)(lo | (h << 4));
}
}
}
base += Np;
}
return RAD_OK;
}
if (w.paro && std::strncmp(w.enc.plane[k].role, "t.", 2) == 0) {
/* 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