sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / tools / adapter / reference.py
| 1 | """Independent GGUF directory reader and numeric oracle. Test dependency: gguf==0.17.1.""" |
| 2 | import hashlib |
| 3 | import pathlib |
| 4 | import struct |
| 5 | import sys |
| 6 | |
| 7 | from gguf import GGUFReader |
| 8 | |
| 9 | |
| 10 | def inspect(path): |
| 11 | reader = GGUFReader(path) |
| 12 | def field(name): |
| 13 | return reader.get_field(name).contents() |
| 14 | tensor = next(t for t in reader.tensors if t.name == "blk.0.attn_q.weight") |
| 15 | assert tensor.tensor_type.name == "Q8_0" |
| 16 | width = int(tensor.shape[0]) |
| 17 | assert width % 32 == 0 |
| 18 | length = width // 32 * 34 |
| 19 | with open(path, "rb") as stream: |
| 20 | stream.seek(tensor.data_offset) |
| 21 | row = stream.read(length) |
| 22 | # Integral activations and signed bytes give exact integer block sums. |
| 23 | total = 0.0 |
| 24 | for start in range(0, length, 34): |
| 25 | scale, *weights = struct.unpack_from("<e32b", row, start) |
| 26 | base = start // 34 * 32 |
| 27 | total += scale * sum(q * ((base + j) % 7 - 3) for j, q in enumerate(weights)) |
| 28 | bits, = struct.unpack("<I", struct.pack("<f", total)) |
| 29 | print(field("general.architecture"), field("tokenizer.ggml.model"), field("general.file_type"), |
| 30 | width, tensor.data_offset, length, pathlib.Path(path).stat().st_size, |
| 31 | hashlib.sha256(row).hexdigest(), bits) |
| 32 | |
| 33 | |
| 34 | for model in sys.argv[1:]: |
| 35 | inspect(model) |