pack_gguf: accept older DeepSeek GGUF metadata names (#3955)
Older converters write the Engram scalars as engram.key_length and engram.pad_id, and omit the sparse-attention geometry. The packer now reads the old names only when the canonical engram.head_dim and engram.pad_token_id are absent, and a new --config-json option fills the omitted geometry from the source model's config.json. A value that disagrees with metadata already in the GGUF stops the pack with a clear message. The weights are never rewritten and a set that already has the canonical names packs exactly as before. Tests: LegacyModelMetadata (2 new, 5 in the file) in tests/terminal/test_pack_gguf_engram.py. Not covered: the author reports the older Q2_K set still needs dense Q2/Q3 and Engram Q2_K tensor support in the engine; this does not add it.
This commit is contained in:
1 parent
a0fc88abd6
commit
fb966cff37
2 files changed
+77
-1
No files matched your search
@@ -56,5 +56,51 @@ class EngramHeader(unittest.TestCase):
|
||||
self.assertFalse(PG.is_engram_file(PG.Gguf(tiny_gguf(Path(d) / "m.gguf", "deepseek41"))))
|
||||
|
||||
|
||||
class LegacyModelMetadata(unittest.TestCase):
|
||||
def test_legacy_engram_names_write_the_same_hash_table(self):
|
||||
common = {
|
||||
"general.architecture": "deepseek41",
|
||||
"deepseek41.engram.max_ngram_size": 2,
|
||||
"deepseek41.engram.head_count": 1,
|
||||
"deepseek41.engram.token_map": [0, 1, 2],
|
||||
"deepseek41.engram.multipliers": [3, 5],
|
||||
"deepseek41.engram.primes": [7],
|
||||
"deepseek41.engram.offsets": [0],
|
||||
}
|
||||
legacy = {**common, "deepseek41.engram.layer_ids": [1],
|
||||
"deepseek41.engram.key_length": 256, "deepseek41.engram.pad_id": 2}
|
||||
current = {**common, "deepseek41.engram.layers": [1],
|
||||
"deepseek41.engram.head_dim": 256, "deepseek41.engram.pad_token_id": 2,
|
||||
"deepseek41.engram.key_length": 999, "deepseek41.engram.pad_id": 999}
|
||||
PG.int_tensors_to_engram([], legacy)
|
||||
PG.int_tensors_to_engram([], current)
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
a, b = Path(d) / "legacy.bin", Path(d) / "current.bin"
|
||||
PG.write_engram(a, legacy, 3)
|
||||
PG.write_engram(b, current, 3)
|
||||
self.assertEqual(a.read_bytes(), b.read_bytes())
|
||||
self.assertEqual(struct.unpack("<4s6i", a.read_bytes()[:28]),
|
||||
(b"DSE1", 3, 1, 2, 1, 256, 2))
|
||||
|
||||
def test_source_config_restores_omitted_sparse_geometry(self):
|
||||
kv = {"general.architecture": "deepseek41", "deepseek41.block_count": 40,
|
||||
"deepseek41.embedding_length": 5120, "deepseek41.expert_count": 384}
|
||||
cfg = {"text_config": {"num_hidden_layers": 40, "hidden_size": 5120,
|
||||
"n_routed_experts": 384, "kv_source_layer_ids": [2, 8, 14, 20],
|
||||
"index_source_layer_ids": [2, 8, 14, 20, 24, 28, 32, 36],
|
||||
"candidate_source_layer_id": 20, "candidate_topk_blocks": 2048,
|
||||
"candidate_block_size": 8}}
|
||||
PG.apply_source_config(kv, cfg)
|
||||
geometry = PG.geometry(kv, 129280)
|
||||
self.assertEqual(geometry["kv_source_layer_ids"], [2, 8, 14, 20])
|
||||
self.assertEqual(geometry["index_source_layer_ids"], [2, 8, 14, 20, 24, 28, 32, 36])
|
||||
self.assertEqual(geometry["candidate_source_layer_id"], 20)
|
||||
self.assertEqual(geometry["candidate_topk_blocks"], 2048)
|
||||
self.assertEqual(geometry["candidate_block_size"], 8)
|
||||
cfg["text_config"]["candidate_block_size"] = 16
|
||||
with self.assertRaisesRegex(SystemExit, "candidate_block_size"):
|
||||
PG.apply_source_config(kv, cfg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -20,6 +20,9 @@ from there when it is present. Nor a vision tower: vision_pack.py makes it from
|
||||
(--vision: another folder), and the pack takes it and its geometry (vision.<key>, image_token_id) from there when it is
|
||||
present; without it the engine reads text only.
|
||||
|
||||
For older GGUF converters that omit sparse-attention geometry, pass --config-json /path/to/the/source/model/config.json.
|
||||
Existing metadata must agree with that configuration; the weights are never rewritten.
|
||||
|
||||
A quant that leaves the two Engram tables out (v1.1.16's Maya-S; Maya's quants hold them since) takes them from a set
|
||||
of GGUF files that holds them, beside it: --engram, else ../Engram/ (unsloth's MXFP8 shard of them, where setups before
|
||||
v1.1.17 put it) when it is whole.
|
||||
@@ -253,6 +256,29 @@ def ple_to_engram(kv: dict):
|
||||
kv["%s.engram.%s" % (a, k)] = v
|
||||
|
||||
|
||||
def apply_source_config(kv: dict, config: dict):
|
||||
"""Fill sparse-attention geometry omitted by older converters from the model's own config.json."""
|
||||
a = kv["general.architecture"]
|
||||
text = config.get("text_config", config)
|
||||
fields = {
|
||||
"embedding_length": "hidden_size", "block_count": "num_hidden_layers",
|
||||
"expert_count": "n_routed_experts", "attention.compress_ratios": "compress_ratios",
|
||||
"attention.kv_source_layers": "kv_source_layer_ids",
|
||||
"attention.indexer.source_layers": "index_source_layer_ids",
|
||||
"attention.indexer.candidate_source_layer": "candidate_source_layer_id",
|
||||
"attention.indexer.candidate_top_k_blocks": "candidate_topk_blocks",
|
||||
"attention.indexer.candidate_block_size": "candidate_block_size",
|
||||
}
|
||||
for name, source in fields.items():
|
||||
if source not in text:
|
||||
continue
|
||||
keys = ["%s.%s" % (a, k) for k in (name, *ALIASES.get(name, ()))]
|
||||
for key in keys:
|
||||
if key in kv and kv[key] != text[source]:
|
||||
raise SystemExit("source config %s disagrees with GGUF %s" % (source, key))
|
||||
kv.setdefault(keys[0], text[source])
|
||||
|
||||
|
||||
def int_tensors_to_engram(gs, kv: dict):
|
||||
"""The IST-DAS sets carry the Engram's hash parameters as integer tensors of the first shard (engram_token_map I32,
|
||||
engram_primes / engram_offsets / engram_multipliers I64, one row a layer) instead of metadata: read them into the
|
||||
@@ -276,7 +302,8 @@ def int_tensors_to_engram(gs, kv: dict):
|
||||
# the IST-DAS spelling of the Engram's scalar keys -> the dotted ones this pack reads
|
||||
for dotted, theirs in (("engram.layers", "engram.layer_ids"), ("engram.max_ngram_size", "engram_max_ngram_size"),
|
||||
("engram.head_count", "engram_n_heads"), ("engram.head_dim", "engram_head_dim"),
|
||||
("engram.pad_token_id", "engram_pad_token_id")):
|
||||
("engram.pad_token_id", "engram_pad_token_id"),
|
||||
("engram.head_dim", "engram.key_length"), ("engram.pad_token_id", "engram.pad_id")):
|
||||
if "%s.%s" % (a, dotted) not in kv and "%s.%s" % (a, theirs) in kv:
|
||||
kv["%s.%s" % (a, dotted)] = kv["%s.%s" % (a, theirs)]
|
||||
|
||||
@@ -413,6 +440,7 @@ def main() -> int:
|
||||
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument("--model", required=True, help="the directory of the GGUF's shards")
|
||||
ap.add_argument("--out", help="default: <model>/maya-ds4")
|
||||
ap.add_argument("--config-json", help="the exact source model config.json, to fill sparse-attention geometry omitted by older GGUF converters")
|
||||
ap.add_argument("--vision", help="vision_pack.py's folder (default: <model>/../vision; taken when it is there)")
|
||||
ap.add_argument("--dspark-gguf", help="unsloth's DSpark drafter GGUF (dspark-*-Q8_0.gguf, architecture dflash): its three "
|
||||
"blocks become mtp.0-2 beside the trunk's tensors (default: the first dspark-*.gguf "
|
||||
@@ -461,6 +489,8 @@ def main() -> int:
|
||||
if kv is None:
|
||||
arch = next((g.kv.get("general.architecture") for g in gs), None)
|
||||
raise SystemExit("%s: not a DeepSeek-V4.1 GGUF (architecture %r)" % (model, arch))
|
||||
if a.config_json:
|
||||
apply_source_config(kv, json.loads(pathlib.Path(a.config_json).expanduser().read_text()))
|
||||
split = int(kv.get("split.count", 1))
|
||||
if split != len(shards):
|
||||
counts = sorted({m.group(1) for m in (re.search(r"-of-(\d{5})\.gguf$", p.name) for p in shards) if m})
|
||||
|
||||
Reference in new issue
Block a user