Archived
core/format: a Quark export whose excluded layers stay block-FP8; DeepSeek's expert counts
GLM-5.3-Flash's Quark export is MXFP4 throughout except the layers it excludes, attention and the MTP layer, which it keeps as 128x128 E4M3 block-FP8 with F32 scales and names in layer_quant_config. Every nonempty override map was refused. Each override is now checked: MXFP4 (fp4, per_group 32, e8m0) or block-FP8 (fp8_e4m3, per_block 128 128) passes, anything else -- including a null or empty weight, which flattening used to drop -- is refused by name. The expert counts read n_routed_experts and n_shared_experts as DeepSeek-style configs spell them (GLM-5's included). checkpoint_test covers both spellings, the mixed export, and the refusals.
This commit is contained in:
1 parent
11334c8ed5
commit
385754f44f
2 files changed
+128
-5
No files matched your search
@@ -60,6 +60,13 @@ static void flatten_json(const nlohmann::json& j, const std::string& prefix, Met
|
|||||||
for (auto it = j.begin(); it != j.end(); ++it) {
|
for (auto it = j.begin(); it != j.end(); ++it) {
|
||||||
const std::string key = prefix.empty() ? it.key() : prefix + "." + it.key();
|
const std::string key = prefix.empty() ? it.key() : prefix + "." + it.key();
|
||||||
const nlohmann::json& v = it.value();
|
const nlohmann::json& v = it.value();
|
||||||
|
/* Retain otherwise invisible Quark weight overrides so import validation can reject
|
||||||
|
* null/empty schemes instead of accidentally inheriting global MXFP4 packing. */
|
||||||
|
if (key.rfind("quantization_config.layer_quant_config.", 0) == 0 &&
|
||||||
|
it.key() == "weight" && (v.is_null() || (v.is_object() && v.empty()))) {
|
||||||
|
out->kv(key, v.is_null() ? "null" : "{}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (v.is_object()) flatten_json(v, key, out);
|
if (v.is_object()) flatten_json(v, key, out);
|
||||||
else if (v.is_boolean()) out->kv(key, v.get<bool>() ? "1" : "0");
|
else if (v.is_boolean()) out->kv(key, v.get<bool>() ? "1" : "0");
|
||||||
else if (v.is_number_integer()) out->kv(key, fmt("%lld", (long long)v.get<int64_t>()));
|
else if (v.is_number_integer()) out->kv(key, fmt("%lld", (long long)v.get<int64_t>()));
|
||||||
@@ -230,9 +237,10 @@ int meta_from_config_json(const std::string& dir, MetaOwn* out) {
|
|||||||
m.n_ff = gi("intermediate_size", gi("moe_intermediate_size", 0));
|
m.n_ff = gi("intermediate_size", gi("moe_intermediate_size", 0));
|
||||||
m.n_vocab = gi("vocab_size", 0);
|
m.n_vocab = gi("vocab_size", 0);
|
||||||
m.n_ctx_train = gi("max_position_embeddings", 0);
|
m.n_ctx_train = gi("max_position_embeddings", 0);
|
||||||
m.n_expert = gi("num_experts", gi("num_local_experts", 0));
|
m.n_expert = gi("num_experts", gi("num_local_experts", gi("n_routed_experts", 0)));
|
||||||
m.n_expert_used = gi("num_experts_per_tok", 0);
|
m.n_expert_used = gi("num_experts_per_tok", 0);
|
||||||
m.n_expert_shared = gi("shared_expert_intermediate_size", 0) ? 1 : 0;
|
m.n_expert_shared = gi("n_shared_experts", gi("num_shared_experts",
|
||||||
|
gi("shared_expert_intermediate_size", 0) ? 1 : 0));
|
||||||
m.rms_eps = (float)gf("rms_norm_eps", gf("norm_eps", 1e-6)); /* LFM2: norm_eps */
|
m.rms_eps = (float)gf("rms_norm_eps", gf("norm_eps", 1e-6)); /* LFM2: norm_eps */
|
||||||
m.rope_theta = (float)gf("rope_parameters.rope_theta", gf("rope_theta", 10000.0));
|
m.rope_theta = (float)gf("rope_parameters.rope_theta", gf("rope_theta", 10000.0));
|
||||||
/* Rotary scaling stays in the free-form keys with its type (arch/common/rad_arch.h,
|
/* Rotary scaling stays in the free-form keys with its type (arch/common/rad_arch.h,
|
||||||
@@ -703,13 +711,45 @@ void Checkpoint::detect_import() {
|
|||||||
else import_why_ = "GPTQ's checkpoint_format '" + cf + "' is not one this reads; 'gptq' is";
|
else import_why_ = "GPTQ's checkpoint_format '" + cf + "' is not one this reads; 'gptq' is";
|
||||||
} else if (qm == "quark") {
|
} else if (qm == "quark") {
|
||||||
const std::string w = "global_quant_config.weight.";
|
const std::string w = "global_quant_config.weight.";
|
||||||
|
/* Quark can retain block-FP8 attention/MTP weights alongside MXFP4 experts.
|
||||||
|
* Validate every override rather than rejecting every nonempty override map.
|
||||||
|
* Keep a lookup here: large exports have tens of thousands of flattened fields. */
|
||||||
|
std::map<std::string, std::string> fields;
|
||||||
|
for (size_t i = 0; i < meta_.keys.size(); ++i) fields.emplace(meta_.keys[i], meta_.vals[i]);
|
||||||
|
const std::string lp = "quantization_config.layer_quant_config.";
|
||||||
|
const std::string suffix = ".weight.dtype";
|
||||||
|
std::map<std::string, bool> layers;
|
||||||
|
for (const auto& [k, v] : fields)
|
||||||
|
if (k.rfind(lp, 0) == 0 && k.size() > lp.size() + suffix.size() &&
|
||||||
|
k.compare(k.size() - suffix.size(), suffix.size(), suffix) == 0)
|
||||||
|
layers.emplace(k.substr(0, k.size() - suffix.size()) + ".", true);
|
||||||
|
bool overrides_ok = true;
|
||||||
|
for (const auto& [layer, unused] : layers) {
|
||||||
|
auto f = [&](const char* key) {
|
||||||
|
auto it = fields.find(layer + "weight." + key);
|
||||||
|
return it == fields.end() ? std::string() : it->second;
|
||||||
|
};
|
||||||
|
const bool mx = f("dtype") == "fp4" && f("qscheme") == "per_group" &&
|
||||||
|
f("group_size") == "32" && f("scale_format") == "e8m0";
|
||||||
|
const bool fp8 = f("dtype") == "fp8_e4m3" && f("qscheme") == "per_block" &&
|
||||||
|
f("block_size") == "128 128";
|
||||||
|
overrides_ok = overrides_ok && (mx || fp8);
|
||||||
|
}
|
||||||
|
/* A partial/null weight override must not disappear merely because it has no dtype. */
|
||||||
|
for (const auto& [k, v] : fields) if (k.rfind(lp, 0) == 0) {
|
||||||
|
bool covered = false;
|
||||||
|
for (size_t dot = k.rfind('.'); dot != std::string::npos;
|
||||||
|
dot = dot ? k.rfind('.', dot - 1) : std::string::npos)
|
||||||
|
if (layers.count(k.substr(0, dot + 1))) { covered = true; break; }
|
||||||
|
overrides_ok = overrides_ok && covered;
|
||||||
|
}
|
||||||
if (qc(w + "dtype") == "fp4" && qc(w + "qscheme") == "per_group" &&
|
if (qc(w + "dtype") == "fp4" && qc(w + "qscheme") == "per_group" &&
|
||||||
qc(w + "group_size") == "32" && qc(w + "scale_format") == "e8m0" &&
|
qc(w + "group_size") == "32" && qc(w + "scale_format") == "e8m0" &&
|
||||||
!any_under("layer_quant_config.") && !any_under("layer_type_quant_config."))
|
overrides_ok && !any_under("layer_type_quant_config."))
|
||||||
import_ = Import::QuarkMx;
|
import_ = Import::QuarkMx;
|
||||||
else
|
else
|
||||||
import_why_ = "Quark's weight scheme here is not MXFP4 (fp4, per_group 32, e8m0 scales) "
|
import_why_ = "Quark requires global MXFP4 (fp4, per_group 32, e8m0 scales), with "
|
||||||
"throughout, which is the one this reads";
|
"only MXFP4 or 128x128 E4M3 block-FP8 layer overrides";
|
||||||
} else if (qm == "compressed-tensors") {
|
} else if (qm == "compressed-tensors") {
|
||||||
/* compressed-tensors states each scheme as a config group with a format. Three are read:
|
/* compressed-tensors states each scheme as a config group with a format. Three are read:
|
||||||
* pack-quantized grouped int4, symmetric (e.g. Google's Gemma 4 QAT w4a16-ct)
|
* pack-quantized grouped int4, symmetric (e.g. Google's Gemma 4 QAT w4a16-ct)
|
||||||
|
|||||||
@@ -128,6 +128,20 @@ TEST(the_metadata_names_the_architecture_and_the_quantisation) {
|
|||||||
CHECK_EQ(ck.dir(), dir);
|
CHECK_EQ(ck.dir(), dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(routed_expert_metadata_accepts_the_deepseek_and_glm_config_spellings) {
|
||||||
|
const std::string dir = write_checkpoint({ bf16("x", {2, 2}, 0) },
|
||||||
|
"{\"model_type\":\"glm5_next\",\"text_config\":{\"hidden_size\":4096,"
|
||||||
|
"\"num_attention_heads\":64,\"head_dim\":0,\"n_routed_experts\":288,"
|
||||||
|
"\"n_shared_experts\":1,\"num_experts_per_tok\":8}}");
|
||||||
|
Checkpoint ck;
|
||||||
|
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||||
|
CHECK_EQ(ck.meta().arch, std::string("glm5next"));
|
||||||
|
CHECK_EQ(ck.meta().m.n_expert, (int64_t)288);
|
||||||
|
CHECK_EQ(ck.meta().m.n_expert_shared, (int64_t)1);
|
||||||
|
CHECK_EQ(ck.meta().m.n_expert_used, (int64_t)8);
|
||||||
|
CHECK_EQ(ck.meta().m.head_dim, (int64_t)0);
|
||||||
|
}
|
||||||
|
|
||||||
/* The descriptor derives from quant_method and fmt whoever set them, including rad-convert's
|
/* The descriptor derives from quant_method and fmt whoever set them, including rad-convert's
|
||||||
* --set: a bf16 checkpoint converted to block fp8 states that format and selects the fp8 plugin,
|
* --set: a bf16 checkpoint converted to block fp8 states that format and selects the fp8 plugin,
|
||||||
* so the container never names one format and selects another's plugin. */
|
* so the container never names one format and selects another's plugin. */
|
||||||
@@ -613,6 +627,75 @@ TEST(a_quark_mxfp4_weight_is_its_codes_and_exponents) {
|
|||||||
CHECK(w.trivial && rad_enc_is(&w.enc, "plain"));
|
CHECK(w.trivial && rad_enc_is(&w.enc, "plain"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* GLM-5.3-Flash's Quark export keeps excluded attention and MTP weights block-FP8, with F32
|
||||||
|
* `_scale` companions. They multiply the codes; reading the global MXFP4 scheme into these
|
||||||
|
* weights, or overlooking the scale because it is not called `_scale_inv`, corrupts the model. */
|
||||||
|
TEST(quark_refuses_unsupported_or_incomplete_layer_weight_overrides) {
|
||||||
|
for (const char* override_config : {
|
||||||
|
"{\"p\":{\"weight\":{\"dtype\":\"fp8_e4m3\",\"qscheme\":\"per_block\",\"block_size\":[64,128]}}}",
|
||||||
|
"{\"p\":{\"weight\":{\"dtype\":\"int8\",\"qscheme\":\"per_block\",\"block_size\":[128,128]}}}",
|
||||||
|
"{\"p\":{\"weight\":null}}",
|
||||||
|
"{\"p\":{\"weight\":{\"dtype\":\"fp8_e4m3\",\"qscheme\":\"per_block\",\"block_size\":[128,128]}},"
|
||||||
|
"\"other\":{\"weight\":{\"qscheme\":\"per_block\"}}}" }) {
|
||||||
|
const std::string config = std::string("{\"quantization_config\":{\"quant_method\":\"quark\","
|
||||||
|
"\"global_quant_config\":{\"weight\":{\"dtype\":\"fp4\",\"qscheme\":\"per_group\","
|
||||||
|
"\"group_size\":32,\"scale_format\":\"e8m0\"}},\"layer_quant_config\":") +
|
||||||
|
override_config + "}}";
|
||||||
|
const std::string dir = write_checkpoint({ bf16("x", { 2, 2 }, 0) }, config.c_str());
|
||||||
|
Checkpoint ck;
|
||||||
|
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||||
|
CHECK(ck.imports() != Checkpoint::Import::QuarkMx);
|
||||||
|
CHECK(!ck.import_refusal().empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(a_quark_checkpoint_preserves_excluded_block_fp8_weights) {
|
||||||
|
T s{ "p.weight_scale", "F32", { 2, 2 }, std::vector<uint8_t>(4 * sizeof(float)) };
|
||||||
|
for (int i = 0; i < 4; ++i) rad_store_f32(s.bytes.data(), RAD_F32, i, 0.25f * (i + 1));
|
||||||
|
T bad = s;
|
||||||
|
bad.name = "bad.weight_scale";
|
||||||
|
bad.shape = { 1, 4 }; /* same number of scales, wrong block geometry */
|
||||||
|
const std::string dir = write_checkpoint(
|
||||||
|
{ fp8("p.weight", { 256, 256 }), s, fp8("bad.weight", { 256, 256 }), bad,
|
||||||
|
bf16("norm.weight", { 4 }, 1.0f) },
|
||||||
|
"{\"model_type\":\"glm5_next\",\"quantization_config\":{\"quant_method\":\"quark\","
|
||||||
|
"\"global_quant_config\":{\"weight\":{\"dtype\":\"fp4\",\"qscheme\":\"per_group\","
|
||||||
|
"\"group_size\":32,\"scale_format\":\"e8m0\"}},"
|
||||||
|
"\"layer_quant_config\":{\"p\":{\"weight\":{\"dtype\":\"fp8_e4m3\","
|
||||||
|
"\"qscheme\":\"per_block\",\"block_size\":[128,128]}}}}}");
|
||||||
|
Checkpoint ck;
|
||||||
|
REQUIRE_EQ(ck.open(dir), RAD_OK);
|
||||||
|
REQUIRE(ck.imports() == Checkpoint::Import::QuarkMx);
|
||||||
|
Program prog;
|
||||||
|
prog.name_map.push_back(map("p", RAD_MAP_COPY, { "p.weight" }));
|
||||||
|
prog.name_map.push_back(map("bad", RAD_MAP_COPY, { "bad.weight" }));
|
||||||
|
prog.name_map.push_back(map("norm", RAD_MAP_COPY, { "norm.weight" }));
|
||||||
|
CkptWeight w;
|
||||||
|
std::string why;
|
||||||
|
REQUIRE_EQ(ckpt_resolve(ck, prog, "p", false, &w, &why), RAD_OK);
|
||||||
|
REQUIRE(w.trivial);
|
||||||
|
REQUIRE_EQ(w.enc.n_planes, 2);
|
||||||
|
CHECK_EQ(w.enc.plane[0].dtype, (uint32_t)RAD_F8E4M3);
|
||||||
|
CHECK_EQ(w.enc.plane[1].dtype, (uint32_t)RAD_F32);
|
||||||
|
CHECK_EQ(w.enc.plane[1].block[0], (int64_t)128);
|
||||||
|
CHECK_EQ(w.enc.plane[1].block[1], (int64_t)128);
|
||||||
|
std::vector<float> f(256);
|
||||||
|
for (int64_t r : { (int64_t)0, (int64_t)129 }) {
|
||||||
|
REQUIRE_EQ(ckpt_rows_f32(w, r, 1, f.data(), &why), RAD_OK);
|
||||||
|
for (int64_t c : { (int64_t)0, (int64_t)127, (int64_t)128, (int64_t)255 })
|
||||||
|
CHECK_EQ(f[(size_t)c], ((float)((r * 256 + c) % 7) - 3.0f) *
|
||||||
|
(0.25f * (float)(r / 128 * 2 + c / 128 + 1)));
|
||||||
|
}
|
||||||
|
std::vector<uint8_t> scales(s.bytes.size());
|
||||||
|
REQUIRE_EQ(ckpt_plane_rows(w, 1, 0, 2, scales.data(), &why), RAD_OK);
|
||||||
|
CHECK_EQ(std::memcmp(scales.data(), s.bytes.data(), scales.size()), 0);
|
||||||
|
REQUIRE_EQ(ckpt_resolve(ck, prog, "bad", false, &w, &why), RAD_OK);
|
||||||
|
CHECK(!w.trivial);
|
||||||
|
CHECK(w.why.find("scale plane") != std::string::npos);
|
||||||
|
REQUIRE_EQ(ckpt_resolve(ck, prog, "norm", false, &w, &why), RAD_OK);
|
||||||
|
CHECK(w.trivial && rad_enc_is(&w.enc, "plain"));
|
||||||
|
}
|
||||||
|
|
||||||
/* A directory merging two checkpoints (a finetune's changed tensors beside links to every base
|
/* A directory merging two checkpoints (a finetune's changed tensors beside links to every base
|
||||||
* shard) holds some tensors in two shards. The index decides which copy is read; a name in two
|
* shard) holds some tensors in two shards. The index decides which copy is read; a name in two
|
||||||
* shards that the index does not settle is refused, not read from whichever shard sorts first. */
|
* shards that the index does not settle is refused, not read from whichever shard sorts first. */
|
||||||
|
|||||||
Reference in new issue
Block a user