Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.
Python aiavx512cpu-inferenceedge-aiggufinference-enginellama-cppllmllm-inferencelocal-ailocal-llmno-gpuon-device-aiopenai-apiopenai-compatiblequantizationqwenself-hostedspeculative-decodingternary
fix(model): reject incompatible MTP sidecars before writing output (#118)
Afloatdfc0488e5426
diff --git a/scripts/graft_mtp.py b/scripts/graft_mtp.pyindex 7d2c6bd..7876e78 100644--- a/scripts/graft_mtp.py+++ b/scripts/graft_mtp.py@@ -35,6 +35,13 @@ def main() -> None:if base.endianess != side.endianess:p.error("base and sidecar must have the same byte order")arch = bytes(base.fields["general.architecture"].parts[-1]).decode()+ side_arch = bytes(side.fields["general.architecture"].parts[-1]).decode()+ if side_arch != arch:+ p.error(f"sidecar architecture {side_arch!r} does not match base architecture {arch!r}")+ embedding_key = f"{arch}.embedding_length"+ if embedding_key in base.fields and embedding_key in side.fields:+ if int(base.fields[embedding_key].parts[-1][0]) != int(side.fields[embedding_key].parts[-1][0]):+ p.error("sidecar embedding length must match the base model")n_layer = int(base.fields[f"{arch}.block_count"].parts[-1][0])graft = [t for t in side.tensors if t.name.startswith(f"blk.{n_layer}.")]if not graft:diff --git a/tests/test_graft_sidecar_compatibility.py b/tests/test_graft_sidecar_compatibility.pynew file mode 100644index 0000000..d8ab365--- /dev/null+++ b/tests/test_graft_sidecar_compatibility.py@@ -0,0 +1,62 @@+from pathlib import Path+import subprocess+import sys+import tempfile+import unittest++try:+ import gguf+ import numpy as np+except ImportError:+ raise unittest.SkipTest('install gguf and numpy to exercise real GGUF files')++ROOT = Path(__file__).resolve().parents[1]+++class GraftSidecarCompatibility(unittest.TestCase):+ def write_model(self, path, arch, layers, name, embedding_length):+ writer = gguf.GGUFWriter(str(path), arch)+ writer.add_uint32(f'{arch}.block_count', layers)+ writer.add_uint32(f'{arch}.embedding_length', embedding_length)+ writer.add_tensor(name, np.ones(embedding_length, dtype=np.float32))+ writer.write_header_to_file()+ writer.write_kv_data_to_file()+ writer.write_tensors_to_file()+ writer.close()++ def run_graft(self, root):+ return subprocess.run([sys.executable, '-I', str(ROOT / 'scripts/graft_mtp.py'),+ '--gguf-py', str(Path(gguf.__file__).parent.parent), str(root / 'base.gguf'),+ str(root / 'side.gguf'), str(root / 'out.gguf')],+ capture_output=True, text=True, timeout=15)++ def test_incompatible_sidecars_fail_before_output_is_replaced(self):+ for arch, dimensions, diagnostic in (('llama', 4, 'architecture'),+ ('qwen35', 8, 'embedding length')):+ with self.subTest(arch=arch, dimensions=dimensions), tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ self.write_model(root / 'base.gguf', 'qwen35', 1, 'blk.0.attn_norm.weight', 4)+ self.write_model(root / 'side.gguf', arch, 2, 'blk.1.ffn_norm.weight', dimensions)+ output = root / 'out.gguf'+ output.write_bytes(b'previous model')+ result = self.run_graft(root)+ self.assertNotEqual(result.returncode, 0)+ self.assertIn(diagnostic, result.stderr)+ self.assertEqual(output.read_bytes(), b'previous model')++ def test_compatible_sidecar_keeps_model_metadata_and_tensors(self):+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ self.write_model(root / 'base.gguf', 'qwen35', 1, 'blk.0.attn_norm.weight', 4)+ self.write_model(root / 'side.gguf', 'qwen35', 2, 'blk.1.ffn_norm.weight', 4)+ result = self.run_graft(root)+ self.assertEqual(result.returncode, 0, result.stderr)+ reader = gguf.GGUFReader(str(root / 'out.gguf'))+ self.assertEqual(reader.fields['qwen35.embedding_length'].parts[-1][0], 4)+ self.assertEqual(len(reader.tensors), 2)+ for tensor in reader.tensors:+ np.testing.assert_array_equal(tensor.data, [1, 1, 1, 1])+++if __name__ == '__main__':+ unittest.main()