Skip to content

Commit b802c43

Browse files
justinchubyCopilot
andcommitted
Use one unreleased packed GGUF manifest schema
Represent every package with v1 source and model records, including single models and MTP chains. Remove the legacy flat-model branch and v2 compatibility path while preserving immutable-generation publication and strict source integrity. Co-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent aefdeff commit b802c43

3 files changed

Lines changed: 27 additions & 35 deletions

File tree

‎docs/api/build_from_gguf.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Keep uniquely named sources in the output directory; load/save verifies their
4545
digests and bindings. Multi-component saves publish atomically through
4646
`gguf-packed.json`. Transcoding, new weight sharding, and included policy/adapter
4747
artifacts are unsupported.
48+
The manifest records source filenames, sizes and hashes for integrity, plus each
49+
component's graph file and MTP depth for loading.
4850

4951
Standalone lossless mmproj supports `janus_pro`:
5052

‎src/mobius/integrations/gguf/_packed.py‎

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -975,12 +975,8 @@ def _save_packed_package(package, directory, *, external_data, max_shard_size_by
975975
)
976976
if not models:
977977
raise ValueError("Cannot save an empty GGUF package; build at least one component.")
978-
legacy = len(models) == 1 and package.mtp_head is None and models[0][1] == "model"
979978
generation = uuid.uuid4().hex
980-
filenames = [
981-
"model.onnx" if legacy else f"component-{generation}-{index}.onnx"
982-
for index in range(len(models))
983-
]
979+
filenames = [f"component-{generation}-{index}.onnx" for index in range(len(models))]
984980
reserved = {
985981
*filenames,
986982
_MANIFEST,
@@ -1006,7 +1002,7 @@ def _save_packed_package(package, directory, *, external_data, max_shard_size_by
10061002
p.name
10071003
for p in root.iterdir()
10081004
if p.name not in allowed
1009-
and re.fullmatch(r"component-(?:[0-9a-f]{32}-)?[0-9]+\.onnx", p.name) is None
1005+
and re.fullmatch(r"component-[0-9a-f]{32}-[0-9]+\.onnx", p.name) is None
10101006
}
10111007
if unexpected:
10121008
raise ValueError(
@@ -1043,17 +1039,16 @@ def _save_packed_package(package, directory, *, external_data, max_shard_size_by
10431039
for (_, _, model), filename in zip(models, filenames, strict=True):
10441040
ir.save(model, stage / filename)
10451041
manifest = {
1046-
"format": "mobius.gguf-packed.v1" if legacy else "mobius.gguf-packed.v2",
1042+
"format": "mobius.gguf-packed.v1",
10471043
"sources": {
10481044
name: {k: v for k, v in info.items() if k != "path"}
10491045
for name, info in sources.items()
10501046
},
10511047
}
1052-
if not legacy:
1053-
manifest["models"] = [
1054-
{"depth": level, "component": name, "file": filename}
1055-
for (level, name, _), filename in zip(models, filenames, strict=True)
1056-
]
1048+
manifest["models"] = [
1049+
{"depth": level, "component": name, "file": filename}
1050+
for (level, name, _), filename in zip(models, filenames, strict=True)
1051+
]
10571052
(stage / _MANIFEST).write_text(
10581053
json.dumps(manifest, indent=2) + "\n",
10591054
encoding="utf-8",
@@ -1109,8 +1104,6 @@ def packed_model_filenames(directory) -> list[str] | None:
11091104
"GGUF manifest is invalid JSON; restore the original manifest."
11101105
) from error
11111106
if isinstance(payload, dict) and payload.get("format") == "mobius.gguf-packed.v1":
1112-
filenames = ["model.onnx"]
1113-
elif isinstance(payload, dict) and payload.get("format") == "mobius.gguf-packed.v2":
11141107
records = payload.get("models")
11151108
if not isinstance(records, list) or not records:
11161109
raise ValueError(
@@ -1121,7 +1114,7 @@ def packed_model_filenames(directory) -> list[str] | None:
11211114
filename = record.get("file") if isinstance(record, dict) else None
11221115
if (
11231116
not isinstance(filename, str)
1124-
or re.fullmatch(r"component-(?:[0-9a-f]{32}-)?[0-9]+\.onnx", filename) is None
1117+
or re.fullmatch(r"component-[0-9a-f]{32}-[0-9]+\.onnx", filename) is None
11251118
):
11261119
raise ValueError("Invalid GGUF component filename; restore the manifest.")
11271120
if filename in filenames:
@@ -1157,7 +1150,7 @@ def restore_packed_sources(package, directory) -> None:
11571150
) from error
11581151
if (
11591152
not isinstance(payload, dict)
1160-
or payload.get("format") not in {"mobius.gguf-packed.v1", "mobius.gguf-packed.v2"}
1153+
or payload.get("format") != "mobius.gguf-packed.v1"
11611154
or not isinstance(payload.get("sources"), dict)
11621155
or not payload["sources"]
11631156
):
@@ -1196,13 +1189,7 @@ def restore_packed_sources(package, directory) -> None:
11961189
f"GGUF source {location!r} bytes changed; restore the original file."
11971190
)
11981191
sources[location] = {**info, "path": str(source)}
1199-
if payload["format"] == "mobius.gguf-packed.v2":
1200-
_restore_components(package, payload.get("models"))
1201-
elif len(package) != 1:
1202-
raise ValueError(
1203-
"GGUF v1 manifest requires one model; restore the original package "
1204-
"or rebuild a multi-component v2 package."
1205-
)
1192+
_restore_components(package, payload.get("models"))
12061193
models = []
12071194
current = package
12081195
while current is not None:
@@ -1244,8 +1231,7 @@ def _restore_components(package, records) -> None:
12441231
or not isinstance(record["component"], str)
12451232
or not record["component"]
12461233
or not isinstance(record["file"], str)
1247-
or re.fullmatch(r"component-(?:[0-9a-f]{32}-)?[0-9]+\.onnx", record["file"])
1248-
is None
1234+
or re.fullmatch(r"component-[0-9a-f]{32}-[0-9]+\.onnx", record["file"]) is None
12491235
):
12501236
raise ValueError("Invalid GGUF component record; restore the manifest or rebuild.")
12511237
name = record["file"].removesuffix(".onnx")

‎src/mobius/integrations/gguf/_packed_test.py‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
pytestmark = pytest.mark.opset29
2121

2222

23+
def _saved_model(directory: Path) -> Path:
24+
manifest = json.loads((directory / "gguf-packed.json").read_text())
25+
return directory / manifest["models"][0]["file"]
26+
27+
2328
def _has_schema29(name: str) -> bool:
2429
return onnx.defs.has(name, 29) and onnx.defs.get_schema(name, 29).since_version == 29
2530

@@ -235,8 +240,8 @@ def test_all_stored_reader_formats(tmp_path: Path, qtype: str, validate_schema:
235240
assert any(n.op_type == "Scan" for n in model.graph.all_nodes())
236241
if not validate_schema:
237242
return
238-
onnx.checker.check_model(str(tmp_path / "model.onnx"), full_check=True)
239-
evaluator = ReferenceEvaluator(str(tmp_path / "model.onnx"))
243+
onnx.checker.check_model(str(_saved_model(tmp_path)), full_check=True)
244+
evaluator = ReferenceEvaluator(str(_saved_model(tmp_path)))
240245
indices = np.array([[1, 0, 1], [-1, -2, 0]], dtype=np.int64)
241246
actual = evaluator.run(["out"], {"ids": indices})[0]
242247
if qtype == "Q4_0":
@@ -319,7 +324,8 @@ def test_manifest_integrity_and_failed_save_preserve_model(
319324
with override_flags(experimental_opset29=True):
320325
package = build_from_gguf(source, execution_provider="onnx-standard")
321326
package.save(str(root))
322-
original_model = (root / "model.onnx").read_bytes()
327+
original_model_path = _saved_model(root)
328+
original_model = original_model_path.read_bytes()
323329
manifest = root / "gguf-packed.json"
324330
original_manifest = manifest.read_text(encoding="utf-8")
325331
corrupt = json.loads(original_manifest)
@@ -338,7 +344,7 @@ def test_manifest_integrity_and_failed_save_preserve_model(
338344
initializer.const_value = ir.tensor(np.zeros(1, dtype=np.uint8))
339345
with pytest.raises(ValueError, match="binding"):
340346
package.save(str(root))
341-
assert (root / "model.onnx").read_bytes() == original_model
347+
assert original_model_path.read_bytes() == original_model
342348
assert not (root / ".gguf-packed.lock").exists()
343349

344350

@@ -418,7 +424,7 @@ def test_packed_model_schema(packed_sources: dict[str, Path], architecture: str)
418424
with override_flags(experimental_opset29=True):
419425
package = build_from_gguf(source, execution_provider="onnx-standard")
420426
package.save(str(source.parent))
421-
onnx.checker.check_model(str(source.parent / "model.onnx"), full_check=True)
427+
onnx.checker.check_model(str(_saved_model(source.parent)), full_check=True)
422428

423429

424430
@requires_packed_schema
@@ -440,7 +446,7 @@ def test_packed_next_token_matches_float_source(
440446
dense = build_from_gguf(source, keep_quantized=False, execution_provider="onnx-standard")
441447
dense_dir = tmp_path / "dense"
442448
dense.save(str(dense_dir))
443-
reference = ReferenceEvaluator(str(tmp_path / "model.onnx"))
449+
reference = ReferenceEvaluator(str(_saved_model(tmp_path)))
444450
dense_reference = ReferenceEvaluator(str(dense_dir / "model.onnx"))
445451
runtime = ort.InferenceSession(str(dense_dir / "model.onnx"))
446452
state = {}
@@ -496,7 +502,7 @@ def test_original_mtp_composition_roundtrip(
496502
restored.save(source.parent, progress_bar=False)
497503
assert source.read_bytes() == before
498504
manifest = json.loads((source.parent / "gguf-packed.json").read_text())
499-
assert manifest["format"] == "mobius.gguf-packed.v2"
505+
assert manifest["format"] == "mobius.gguf-packed.v1"
500506
assert len(manifest["sources"]) == 1
501507
assert {row["depth"] for row in manifest["models"]} == {0, 1}
502508
for model in (restored["model"], restored.mtp_head["model"]):
@@ -761,9 +767,7 @@ def _run(
761767
result.append(np.einsum("bhk,bhkm->bhm", q[:, step], state) * scale)
762768
return np.stack(result, axis=1).reshape(batch, length, 64), state
763769

764-
reference = ReferenceEvaluator(
765-
str(source.parent / "model.onnx"), new_ops=[LinearAttention]
766-
)
770+
reference = ReferenceEvaluator(str(_saved_model(source.parent)), new_ops=[LinearAttention])
767771
dense = build_from_gguf(source, keep_quantized=False, execution_provider="onnx-standard")
768772
dense_dir = source.parent / "dense"
769773
dense.save(dense_dir, progress_bar=False)

0 commit comments

Comments
 (0)