okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9adf4e01a
main
1"""Minimal GGUF v3 header reader - no ggml, no gguf-py. 2 3Written because the `gguf` PyPI package cannot open this artifact: its `GGMLQuantizationType` 4enum has no member for type 42 (`Q2_0`), which is newer than the library. That is itself a P0.S6 5finding - the format this engine is specialized for is ahead of the standard tooling. 6 7This is the reference implementation for `src/artifact/` (architecture §15: "GGUF v3 reader (mmap, 8no ggml dependency)"). It parses the header only; tensor data is never read. 9""" 10from __future__ import annotations 11 12import dataclasses 13import pathlib 14import struct 15from typing import Any 16 17GGUF_MAGIC = 0x46554747 # "GGUF" little-endian 18 19# ggml_type values. 42 = Q2_0, the GSQ-RCO routed-expert encoding, absent from gguf-py as of 0.19. 20GGML_TYPES: dict[int, str] = { 21 0: "F32", 1: "F16", 2: "Q4_0", 3: "Q4_1", 4: "Q4_2", 5: "Q4_3", 6: "Q5_0", 7: "Q5_1", 22 8: "Q8_0", 9: "Q8_1", 10: "Q2_K", 11: "Q3_K", 12: "Q4_K", 13: "Q5_K", 14: "Q6_K", 23 15: "Q8_K", 16: "IQ2_XXS", 17: "IQ2_XS", 18: "IQ3_XXS", 19: "IQ1_S", 20: "IQ4_NL", 24 21: "IQ3_S", 22: "IQ2_S", 23: "IQ4_XS", 24: "I8", 25: "I16", 26: "I32", 27: "I64", 25 28: "F64", 29: "IQ1_M", 30: "BF16", 31: "Q4_0_4_4", 32: "Q4_0_4_8", 33: "Q4_0_8_8", 26 34: "TQ1_0", 35: "TQ2_0", 36: "IQ4_NL_4_4", 37: "IQ4_NL_4_8", 38: "IQ4_NL_8_8", 27 39: "MXFP4", 40: "NVFP4", 41: "Q4_0_8_8", 42: "Q2_0", 28} 29 30# GGUF metadata value type ids 31GGUF_META = { 32 0: ("u8", 1), 1: ("i8", 1), 2: ("u16", 2), 3: ("i16", 2), 4: ("u32", 4), 5: ("i32", 4), 33 6: ("f32", 4), 7: ("bool", 1), 8: ("string", None), 9: ("array", None), 34 10: ("u64", 8), 11: ("i64", 8), 12: ("f64", 8), 35} 36 37# ggml block geometry for the encodings this model uses: (block elements, bytes per block) 38# This is what makes a per-tensor byte count checkable, and it is the contract the kernels share. 39BLOCK_GEOMETRY: dict[str, tuple[int, int]] = { 40 "F32": (1, 4), "F16": (1, 2), "BF16": (1, 2), "F64": (1, 8), 41 "Q4_0": (32, 18), "Q4_1": (32, 20), "Q5_0": (32, 22), "Q5_1": (32, 24), 42 "Q8_0": (32, 34), "Q8_1": (32, 36), 43 "Q2_K": (256, 84), "Q3_K": (256, 110), "Q4_K": (256, 144), "Q5_K": (256, 176), 44 "Q6_K": (256, 210), "Q8_K": (256, 292), 45 "IQ2_XXS": (256, 66), "IQ2_XS": (256, 74), "IQ3_XXS": (256, 98), "IQ1_S": (256, 50), 46 "IQ4_NL": (32, 18), "IQ3_S": (256, 110), "IQ2_S": (256, 82), "IQ4_XS": (256, 136), 47 "IQ1_M": (256, 56), "Q2_0": (64, 18), "MXFP4": (32, 17), "NVFP4": (64, 36), 48} 49 50 51@dataclasses.dataclass 52class TensorInfo: 53 name: str 54 shape: list[int] 55 type_id: int 56 type_name: str 57 offset: int 58 59 @property 60 def elements(self) -> int: 61 n = 1 62 for d in self.shape: 63 n *= d 64 return n 65 66 def expected_bytes(self) -> int | None: 67 geom = BLOCK_GEOMETRY.get(self.type_name) 68 if geom is None: 69 return None 70 block_elems, block_bytes = geom 71 if self.elements % block_elems: 72 return None 73 return self.elements // block_elems * block_bytes 74 75 76class GGUFFile: 77 def __init__(self, path: pathlib.Path): 78 self.path = pathlib.Path(path) 79 self.metadata: dict[str, Any] = {} 80 self.tensors: list[TensorInfo] = [] 81 self.version = 0 82 self.alignment = 32 83 with self.path.open("rb") as fh: 84 self._parse(fh) 85 self.data_start = self._data_start 86 87 # ------------------------------------------------------------------ internals 88 def _parse(self, fh) -> None: 89 magic = struct.unpack("<I", fh.read(4))[0] 90 if magic != GGUF_MAGIC: 91 raise ValueError(f"{self.path.name}: not a GGUF file (magic {magic:#x})") 92 self.version, n_tensors, n_kv = struct.unpack("<IQQ", fh.read(20)) 93 if self.version != 3: 94 raise ValueError(f"{self.path.name}: GGUF v{self.version}, this reader handles v3") 95 for _ in range(n_kv): 96 key = self._str(fh) 97 self.metadata[key] = self._value(fh) 98 for _ in range(n_tensors): 99 name = self._str(fh) 100 (n_dims,) = struct.unpack("<I", fh.read(4)) 101 shape = list(struct.unpack(f"<{n_dims}Q", fh.read(8 * n_dims))) 102 type_id, offset = struct.unpack("<IQ", fh.read(12)) 103 self.tensors.append(TensorInfo(name, shape, type_id, 104 GGML_TYPES.get(type_id, f"type{type_id}"), offset)) 105 align = self.metadata.get("general.alignment") 106 if isinstance(align, int) and align: 107 self.alignment = align 108 pos = fh.tell() 109 self._data_start = (pos + self.alignment - 1) // self.alignment * self.alignment 110 111 def _str(self, fh) -> str: 112 (n,) = struct.unpack("<Q", fh.read(8)) 113 return fh.read(n).decode("utf-8", "replace") 114 115 def _value(self, fh): 116 (t,) = struct.unpack("<I", fh.read(4)) 117 name, size = GGUF_META[t] 118 if name == "string": 119 return self._str(fh) 120 if name == "array": 121 (et,) = struct.unpack("<I", fh.read(4)) 122 (count,) = struct.unpack("<Q", fh.read(8)) 123 ename, esize = GGUF_META[et] 124 if ename == "string": 125 return [self._str(fh) for _ in r