okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
2.8 KiB72 linesraw
1"""setup.check_shards: a metadata-only GGUF shard that ends at its header (no alignment padding, #2346) is whole;
2a shard whose tensor data runs past its end is still refused, including inside an integer tensor."""
3import struct
4import sys
5import tempfile
6import unittest
7from pathlib import Path
8
9ROOT = Path(__file__).resolve().parent.parent
10sys.path.insert(0, str(ROOT))
11import setup  # noqa: E402
12
13# ggml type id -> bytes per value, for the unquantised types these tests write
14VALUE_BYTES = {0: 4, 27: 8}                     # F32, I64
15
16
17def _str(s):
18    b = s.encode()
19    return struct.pack("<Q", len(b)) + b
20
21
22def gguf(path, tensors=(), pad_data=0):
23    """A GGUF v3 file with one string key and the given (name, n_values, type_id) tensors."""
24    kv = _str("general.name") + struct.pack("<I", 8) + _str("t")
25    tinfo = b""
26    off = 0
27    for name, n, type_id in tensors:
28        tinfo += _str(name) + struct.pack("<I", 1) + struct.pack("<Q", n)
29        tinfo += struct.pack("<I", type_id) + struct.pack("<Q", off)
30        off += ((n * VALUE_BYTES[type_id] + 31) // 32) * 32
31    head = b"GGUF" + struct.pack("<IQQ", 3, len(tensors), 1) + kv + tinfo
32    body = head
33    if tensors:
34        body += b"\0" * ((32 - len(head) % 32) % 32)
35        body += b"\0" * pad_data
36    path.write_bytes(body)
37    return len(head)
38
39
40class ShardCheck(unittest.TestCase):
41    def setUp(self):
42        self.d = Path(tempfile.mkdtemp())
43
44    def test_metadata_only_shard_without_padding_is_whole(self):
45        p = self.d / "m-00001-of-00006.gguf"
46        n = gguf(p)
47        self.assertNotEqual(n % 32, 0, "the test file must end unaligned, like the real shard")
48        setup.check_shards([p])          # must not stop setup
49
50    def test_truncated_tensor_data_is_still_refused(self):
51        p = self.d / "t-00002-of-00006.gguf"
52        gguf(p, tensors=[("w", 64, 0)], pad_data=100)       # needs 256 bytes of data, has 100
53        with self.assertRaises(SystemExit):
54            setup.check_shards([p])
55
56    def test_a_complete_gguf_with_integer_tensors_is_whole(self):
57        # The ISTA-DAS DeepSeek-V4.1 first shard keeps the Engram's hash parameters as I32/I64 tensors
58        # (tools/dsv41/pack_gguf.py), so a complete file of that set must not be turned away as cut short.
59        p = self.d / "i-00001-of-00004.gguf"
60        gguf(p, tensors=[("engram_primes", 3, 27)], pad_data=24)       # exactly its 24 bytes
61        self.assertTrue(setup.own_gguf_is_whole(p))
62        setup.check_shards([p])          # must not stop setup
63
64    def test_a_shard_cut_inside_an_integer_tensor_is_still_refused(self):
65        p = self.d / "j-00001-of-00004.gguf"
66        gguf(p, tensors=[("engram_primes", 3, 27)], pad_data=16)       # needs 24 bytes of data, has 16
67        with self.assertRaises(SystemExit):
68            setup.check_shards([p])
69
70
71if __name__ == "__main__":
72    unittest.main()