Archived
format: import ParoQuant int5-bitplane and z-lab's int4 (u5/u4 codes, f16 scale, u8 zero, givens tables)
This commit is contained in:
1 parent
9af15f3a6e
commit
e45bb596f4
3 files changed
+235
-2
No files matched your search
+129
-1
@@ -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
|
||||
|
||||
@@ -56,6 +56,13 @@ struct SrcTensor {
|
||||
* the K columns one row of its scale tensor covers. 0 for any other tensor. */
|
||||
int32_t gptq_bits = 0;
|
||||
int64_t gptq_group = 0;
|
||||
/* ParoQuant's int5-bitplane (Import::ParoI5): `data` is the AWQ-packed low nibbles qweight
|
||||
* [K, N/8] and the tensor described is the logical [N, K] u5 code matrix; `aux` are the fifth-bit
|
||||
* plane qweight_hi [K, N/32], and the zero points qzeros [G, N/8] and qzeros_hi [G, N/32].
|
||||
* z-lab's int4 (Import::ParoI4) is the same without the fifth-bit planes (aux[0], aux[2] null). */
|
||||
bool int5 = false;
|
||||
int pbits = 0; /* 5 for int5-bitplane, 4 for z-lab's plain AWQ-packed int4 */
|
||||
const SrcTensor* aux[3] = { nullptr, nullptr, nullptr };
|
||||
|
||||
int64_t numel() const {
|
||||
int64_t n = 1;
|
||||
@@ -133,7 +140,7 @@ public:
|
||||
* this does not read stays None, with the reason in import_refusal(). */
|
||||
/* Its quantization_config keys are then renamed `imported_quantization_config.*`: they
|
||||
* describe the tool, not a served format, and a plugin reads the latter there. */
|
||||
enum class Import { None, Gptq, QuarkMx, ParoMx };
|
||||
enum class Import { None, Gptq, QuarkMx, ParoMx, ParoI5, ParoI4 };
|
||||
Import imports() const { return import_; }
|
||||
const std::string& import_refusal() const { return import_why_; }
|
||||
|
||||
|
||||
@@ -834,3 +834,101 @@ TEST(a_paroquant_mxfp4_weight_carries_its_rotations_as_parts) {
|
||||
}
|
||||
remove_checkpoint(dir);
|
||||
}
|
||||
|
||||
/* PAROQUANT INT5-BITPLANE: the low nibbles AWQ-packed along N (qweight [K, N/8], eight columns a
|
||||
* word in the order 0,2,4,6,1,3,5,7), a fifth-bit plane (qweight_hi [K, N/32], bit n % 32) and the
|
||||
* same two packings of the zero points. The logical [N, K] weight comes out as u5 codes (a bit
|
||||
* stream along K), an f16 scale and a u8 zero a 128 of K, plus the rotation as `givens` tables. */
|
||||
TEST(a_paroquant_int5_weight_is_relaid_row_by_row) {
|
||||
const int64_t N = 64, K = 256, G = K / 128;
|
||||
uint32_t rng = 777;
|
||||
auto next = [&]() { rng = rng * 1664525u + 1013904223u; return rng >> 8; };
|
||||
std::vector<int> code((size_t)(N * K)), zero((size_t)(N * G));
|
||||
for (auto& c : code) c = (int)(next() % 32);
|
||||
for (auto& z : zero) z = (int)(next() % 32);
|
||||
static const int kOrder[8] = { 0, 2, 4, 6, 1, 3, 5, 7 };
|
||||
auto pack_awq = [&](const std::vector<int>& v, int64_t rows) { // v[n * rows + r] -> [rows, N/8]
|
||||
T t{ "", "I32", { rows, N / 8 }, {} };
|
||||
t.bytes.assign((size_t)(rows * N / 8 * 4), 0);
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
int j = 0;
|
||||
while (kOrder[j] != n % 8) ++j;
|
||||
uint32_t w;
|
||||
uint8_t* p = t.bytes.data() + (r * (N / 8) + n / 8) * 4;
|
||||
std::memcpy(&w, p, 4);
|
||||
w |= (uint32_t)(v[(size_t)(n * rows + r)] & 15) << (4 * j);
|
||||
std::memcpy(p, &w, 4);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
auto pack_hi = [&](const std::vector<int>& v, int64_t rows) {
|
||||
T t{ "", "I32", { rows, N / 32 }, {} };
|
||||
t.bytes.assign((size_t)(rows * N / 32 * 4), 0);
|
||||
for (int64_t r = 0; r < rows; ++r)
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
uint32_t w;
|
||||
uint8_t* p = t.bytes.data() + (r * (N / 32) + n / 32) * 4;
|
||||
std::memcpy(&w, p, 4);
|
||||
w |= (uint32_t)((v[(size_t)(n * rows + r)] >> 4) & 1) << (n % 32);
|
||||
std::memcpy(p, &w, 4);
|
||||
}
|
||||
return t;
|
||||
};
|
||||
T qw = pack_awq(code, K), qwh = pack_hi(code, K), qz = pack_awq(zero, G), qzh = pack_hi(zero, G);
|
||||
qw.name = "q.qweight"; qwh.name = "q.qweight_hi"; qz.name = "q.qzeros"; qzh.name = "q.qzeros_hi";
|
||||
T sc{ "q.scales", "F16", { G, N }, {} };
|
||||
for (int64_t i = 0; i < G * N; ++i) {
|
||||
const uint16_t h = rad_f32_to_f16(0.01f * (float)(1 + next() % 50));
|
||||
sc.bytes.push_back((uint8_t)(h & 0xff));
|
||||
sc.bytes.push_back((uint8_t)(h >> 8));
|
||||
}
|
||||
T pr{ "q.pairs", "I16", { 2, K }, {} };
|
||||
for (int64_t s = 0; s < 2; ++s)
|
||||
for (int64_t i = 0; i < K; ++i) {
|
||||
const int16_t v = (int16_t)(i % 128 ^ 1); // adjacent swap: a pairing
|
||||
pr.bytes.push_back((uint8_t)(v & 0xff));
|
||||
pr.bytes.push_back((uint8_t)(v >> 8));
|
||||
}
|
||||
T th{ "q.theta", "F16", { 2, K / 2 }, {} }, cs{ "q.channel_scales", "F16", { 1, K }, {} };
|
||||
for (int64_t i = 0; i < 2 * K / 2; ++i) { const uint16_t h = rad_f32_to_f16(0.3f); th.bytes.push_back(h & 0xff); th.bytes.push_back(h >> 8); }
|
||||
for (int64_t i = 0; i < K; ++i) { const uint16_t h = rad_f32_to_f16(1.5f); cs.bytes.push_back(h & 0xff); cs.bytes.push_back(h >> 8); }
|
||||
const std::string dir = write_checkpoint(
|
||||
{ qw, qwh, qz, qzh, sc, pr, th, cs },
|
||||
"{\"model_type\":\"qwen3_5\",\"quantization_config\":{\"quant_method\":\"paroquant\","
|
||||
"\"bits\":5,\"group_size\":128,\"krot\":2,\"format\":\"int5-bitplane\"}}");
|
||||
Checkpoint ck;
|
||||
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||
CHECK(ck.imports() == Checkpoint::Import::ParoI5);
|
||||
Program prog;
|
||||
prog.name_map.push_back(map("w", RAD_MAP_COPY, { "q.weight" }));
|
||||
CkptWeight w;
|
||||
std::string why;
|
||||
REQUIRE_EQ(ckpt_resolve(ck, prog, "w", false, &w, &why), RAD_OK);
|
||||
REQUIRE(w.paro && w.imported);
|
||||
CHECK_EQ(w.rows, N);
|
||||
CHECK_EQ(w.cols, K);
|
||||
REQUIRE(rad_enc_valid(&w.enc));
|
||||
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_U5);
|
||||
CHECK_EQ(w.enc.plane[1].dtype, (uint32_t)RAD_F16);
|
||||
CHECK_EQ(w.enc.plane[2].dtype, (uint32_t)RAD_U8);
|
||||
std::vector<std::vector<uint8_t>> pl((size_t)w.enc.n_planes);
|
||||
for (int k = 0; k < w.enc.n_planes; ++k) {
|
||||
int64_t pr_ = 0, pc = 0;
|
||||
rad_enc_plane_dims(&w.enc.plane[k], w.rows, w.cols, &pr_, &pc);
|
||||
pl[(size_t)k].assign((size_t)(pr_ * rad_enc_row_bytes(w.enc.plane[k].dtype, pc)) + 8, 0);
|
||||
REQUIRE_EQ(ckpt_plane_rows(w, k, 0, pr_, pl[(size_t)k].data(), &why), RAD_OK);
|
||||
}
|
||||
const int64_t rb = rad_enc_row_bytes(RAD_U5, K);
|
||||
int bad = 0;
|
||||
for (int64_t n = 0; n < N; ++n) {
|
||||
for (int64_t k = 0; k < K; ++k)
|
||||
if ((int)rad_load_f32(pl[0].data() + n * rb, RAD_U5, k) != code[(size_t)(n * K + k)]) ++bad;
|
||||
for (int64_t g = 0; g < G; ++g) {
|
||||
if (pl[2][(size_t)(n * G + g)] != zero[(size_t)(n * G + g)]) ++bad;
|
||||
if (std::memcmp(pl[1].data() + (n * G + g) * 2, sc.bytes.data() + (g * N + n) * 2, 2) != 0) ++bad;
|
||||
}
|
||||
}
|
||||
CHECK_EQ(bad, 0);
|
||||
remove_checkpoint(dir);
|
||||
}
|
||||
Reference in new issue
Block a user