okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
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 ): 18b = s .encode () 19return 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.""" 24kv = _str ("general.name" )+ struct .pack ("<I" ,8 )+ _str ("t" ) 25tinfo = b"" 26off = 0 27for name ,n ,type_id in tensors : 28tinfo += _str (name )+ struct .pack ("<I" ,1 )+ struct .pack ("<Q" ,n ) 29tinfo += struct .pack ("<I" ,type_id )+ struct .pack ("<Q" ,off ) 30off += ((n * VALUE_BYTES [type_id ]+ 31 )// 32 )* 32 31head = b"GGUF" + struct .pack ("<IQQ" ,3 ,len (tensors ),1 )+ kv + tinfo 32body = head 33if tensors : 34body += b"\0" * ((32 - len (head )% 32 )% 32 ) 35body += b"\0" * pad_data 36path .write_bytes (body ) 37return len (head ) 38 39 40class ShardCheck (unittest .TestCase ): 41def setUp (self ): 42self .d = Path (tempfile .mkdtemp ()) 43 44def test_metadata_only_shard_without_padding_is_whole (self ): 45p = self .d / "m-00001-of-00006.gguf" 46n = gguf (p ) 47self .assertNotEqual (n % 32 ,0 ,"the test file must end unaligned, like the real shard" ) 48setup .check_shards ([p ])# must not stop setup 49 50def test_truncated_tensor_data_is_still_refused (self ): 51p = self .d / "t-00002-of-00006.gguf" 52gguf (p ,tensors = [("w" ,64 ,0 )],pad_data = 100 )# needs 256 bytes of data, has 100 53with self .assertRaises (SystemExit ): 54setup .check_shards ([p ]) 55 56def 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. 59p = self .d / "i-00001-of-00004.gguf" 60gguf (p ,tensors = [("engram_primes" ,3 ,27 )],pad_data = 24 )# exactly its 24 bytes 61self .assertTrue (setup .own_gguf_is_whole (p )) 62setup .check_shards ([p ])# must not stop setup 63 64def test_a_shard_cut_inside_an_integer_tensor_is_still_refused (self ): 65p = self .d / "j-00001-of-00004.gguf" 66gguf (p ,tensors = [("engram_primes" ,3 ,27 )],pad_data = 16 )# needs 24 bytes of data, has 16 67with self .assertRaises (SystemExit ): 68setup .check_shards ([p ]) 69 70 71if __name__ == "__main__" : 72unittest .main ()