From ae456dfe5ccc4bccadc150c6cfce7fc8345da106 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 20 Oct 2026 16:03:23 +0200 Subject: [PATCH] iq_pack: an interrupted experts.bin write continues at the next layer (finished layers are flushed and recorded, the last kept one is checked against the GGUF) instead of starting from zero; says so --- tools/iq_pack.py | 36 ++++++++++++++++- tools/test_iq_pack.py | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) diff --git a/tools/iq_pack.py b/tools/iq_pack.py index b48af1aa..b33723a0 100644 --- a/tools/iq_pack.py +++ b/tools/iq_pack.py @@ -731,16 +731,48 @@ def main() -> int: # experts.bin, and an experts.bin without its sidecar is never reused sidecar.unlink(missing_ok=True) part = out / "experts.bin.tmp" - with open(part, "wb") as fo: - for l, gt, dt, off, blob, ts in layout: + progress = out / "experts.bin.tmp.json" + # A stop part-way (a kill, a power cut) used to cost the whole write again (40 GB, minutes). The finished layers are kept: + # after every layer the file is flushed to disk and experts.bin.tmp.json says how many layers are in it and which source they were + # cut from. The next run continues at that layer when the source is the same and the last kept layer still matches the GGUF. + start = 0 + if part.exists(): + done = read_json(progress) + k = done.get("layers") if isinstance(done, dict) and done.get("want") == want else None + if isinstance(k, int) and 0 < k <= len(layout) and part.stat().st_size >= (layout[k][3] if k < len(layout) else offset): + l, gt, dt, off, blob, ts = layout[k - 1] + last = layer_blobs(l, blob, ts) + if last is not None: + with open(part, "rb") as f: + f.seek(off) + kept = f.read(n_expert * blob) + if kept == last.tobytes(): + start = k + if start: + print("the earlier packing was interrupted: keeping its first %d of %d layers and continuing with layer %d" + % (start, len(layout), start), flush=True) + else: + print("the earlier packing was interrupted and cannot be continued: starting it again", flush=True) + resume_at = layout[start][3] if start < len(layout) else offset + with open(part, "r+b" if start else "wb") as fo: + if start: + fo.truncate(resume_at) # whatever followed the last recorded layer is a partial write + fo.seek(resume_at) + for l, gt, dt, off, blob, ts in layout[start:]: chunk = layer_blobs(l, blob, ts) if chunk is None: return 1 fo.write(chunk.tobytes()) + fo.flush() + os.fsync(fo.fileno()) + note = out / "experts.bin.tmp.json.new" + note.write_text(json.dumps({"want": want, "layers": l + 1}), encoding="utf-8") + note.replace(progress) if l % 8 == 0: print(" layer %2d %-8s/%-7s blob %8d at %.2f GiB" % (l, ts[0].type_name, ts[2].type_name, blob, off / 2**30), flush=True) part.replace(path) + progress.unlink(missing_ok=True) side_tmp = out / "experts.bin.src.json.tmp" side_tmp.write_text(json.dumps(want, indent=2) + "\n", encoding="utf-8") side_tmp.replace(sidecar) diff --git a/tools/test_iq_pack.py b/tools/test_iq_pack.py index 580bf274..0e0f5e8b 100644 --- a/tools/test_iq_pack.py +++ b/tools/test_iq_pack.py @@ -515,6 +515,95 @@ class SplitArtifactTests(unittest.TestCase): self.assertEqual(rc, 0, log) self.assertEqual((pack / "experts.bin").read_bytes(), expected_experts(iq_pack.Model(paths[0]))) + def test_interrupted_pack_continues_at_the_next_layer(self): + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp: + root = Path(tmp) + paths = split_model(root) + pack = root / "pack" + real = np.concatenate + calls = [] + + def dies_on_layer_1(parts, axis): + calls.append(1) + if len(calls) == 2: + raise KeyboardInterrupt("killed") + return real(parts, axis=axis) + with patch.object(iq_pack.np, "concatenate", dies_on_layer_1): + with self.assertRaises(KeyboardInterrupt): + run_pack(paths[0], pack, "--experts-bin") + want = expected_experts(iq_pack.Model(paths[0])) + lines = [ln.split() for ln in (pack / "native_experts.txt").read_text().splitlines() if ln and not ln.startswith("#")] + one_layer = int(lines[1][3]) # where layer 1 starts + self.assertFalse((pack / "experts.bin").exists()) + self.assertEqual((pack / "experts.bin.tmp").stat().st_size, one_layer) # layer 0 is on disk + self.assertEqual(json.loads((pack / "experts.bin.tmp.json").read_text())["layers"], 1) + # a partial write after the recorded layer (the kill came mid-write) is cut off + with open(pack / "experts.bin.tmp", "ab") as f: + f.write(b"garbage") + written = [] + real_layer = iq_pack.np.concatenate + + def counting(parts, axis): + written.append(1) + return real_layer(parts, axis=axis) + with patch.object(iq_pack.np, "concatenate", counting): + rc, log = run_pack(paths[0], pack, "--experts-bin") + self.assertEqual(rc, 0, log) + self.assertIn("keeping its first 1 of 2 layers and continuing with layer 1", log) + self.assertEqual((pack / "experts.bin").read_bytes(), want) + self.assertEqual(len(written), 2) # one layer re-read to check the kept one, one layer written + self.assertFalse((pack / "experts.bin.tmp").exists()) + self.assertFalse((pack / "experts.bin.tmp.json").exists()) + self.assertEqual(json.loads((pack / "experts.bin.src.json").read_text())["bytes"], len(want)) + + def test_interrupted_pack_of_another_model_is_not_continued(self): + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp: + root = Path(tmp) + (root / "a").mkdir() + (root / "b").mkdir() + other = split_model(root / "a", name="other", seed=100) + paths = split_model(root / "b") + pack = root / "pack" + real = np.concatenate + calls = [] + + def dies_on_layer_1(parts, axis): + calls.append(1) + if len(calls) == 2: + raise KeyboardInterrupt("killed") + return real(parts, axis=axis) + with patch.object(iq_pack.np, "concatenate", dies_on_layer_1): + with self.assertRaises(KeyboardInterrupt): + run_pack(other[0], pack, "--experts-bin") + rc, log = run_pack(paths[0], pack, "--experts-bin") # the same geometry, other weights + self.assertEqual(rc, 0, log) + self.assertIn("cannot be continued: starting it again", log) + self.assertEqual((pack / "experts.bin").read_bytes(), expected_experts(iq_pack.Model(paths[0]))) + + def test_a_kept_layer_that_no_longer_matches_is_not_trusted(self): + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp: + root = Path(tmp) + paths = split_model(root) + pack = root / "pack" + real = np.concatenate + calls = [] + + def dies_on_layer_1(parts, axis): + calls.append(1) + if len(calls) == 2: + raise KeyboardInterrupt("killed") + return real(parts, axis=axis) + with patch.object(iq_pack.np, "concatenate", dies_on_layer_1): + with self.assertRaises(KeyboardInterrupt): + run_pack(paths[0], pack, "--experts-bin") + data = bytearray((pack / "experts.bin.tmp").read_bytes()) + data[0] ^= 0xFF # the disk lost a bit + (pack / "experts.bin.tmp").write_bytes(bytes(data)) + rc, log = run_pack(paths[0], pack, "--experts-bin") + self.assertEqual(rc, 0, log) + self.assertIn("cannot be continued", log) + self.assertEqual((pack / "experts.bin").read_bytes(), expected_experts(iq_pack.Model(paths[0]))) + def test_stale_same_size_experts_bin(self): with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmp: root = Path(tmp)