okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
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 os 14import pathlib 15import struct 16from typing import Any 17 18GGUF_MAGIC = 0x46554747 # "GGUF" little-endian 19 20# ggml_type values. 42 = Q2_0, the GSQ-RCO routed-expert encoding, absent from gguf-py as of 0.19. 21GGML_TYPES :dict [int ,str ]= { 220 :"F32" ,1 :"F16" ,2 :"Q4_0" ,3 :"Q4_1" ,4 :"Q4_2" ,5 :"Q4_3" ,6 :"Q5_0" ,7 :"Q5_1" , 238 :"Q8_0" ,9 :"Q8_1" ,10 :"Q2_K" ,11 :"Q3_K" ,12 :"Q4_K" ,13 :"Q5_K" ,14 :"Q6_K" , 2415 :"Q8_K" ,16 :"IQ2_XXS" ,17 :"IQ2_XS" ,18 :"IQ3_XXS" ,19 :"IQ1_S" ,20 :"IQ4_NL" , 2521 :"IQ3_S" ,22 :"IQ2_S" ,23 :"IQ4_XS" ,24 :"I8" ,25 :"I16" ,26 :"I32" ,27 :"I64" , 2628 :"F64" ,29 :"IQ1_M" ,30 :"BF16" ,31 :"Q4_0_4_4" ,32 :"Q4_0_4_8" ,33 :"Q4_0_8_8" , 2734 :"TQ1_0" ,35 :"TQ2_0" ,36 :"IQ4_NL_4_4" ,37 :"IQ4_NL_4_8" ,38 :"IQ4_NL_8_8" , 2839 :"MXFP4" ,40 :"NVFP4" ,41 :"Q4_0_8_8" ,42 :"Q2_0" , 29# Agention's llama.cpp fork (agentionai/llama.cpp): the APR trellis expert types (128-value blocks) 30143 :"PTQ1_0" ,144 :"TQ2_T" ,145 :"TQK6" ,146 :"TQK7" , 31# pair-trellis family added for Gyro-L (agentionai llama.cpp release/gyro-l) 32147 :"TQP5" ,148 :"TQP6" ,149 :"TQP7" , 33} 34 35# GGUF metadata value type ids 36GGUF_META = { 370 : ("u8" ,1 ),1 : ("i8" ,1 ),2 : ("u16" ,2 ),3 : ("i16" ,2 ),4 : ("u32" ,4 ),5 : ("i32" ,4 ), 386 : ("f32" ,4 ),7 : ("bool" ,1 ),8 : ("string" ,None ),9 : ("array" ,None ), 3910 : ("u64" ,8 ),11 : ("i64" ,8 ),12 : ("f64" ,8 ), 40} 41 42# ggml block geometry for the encodings this model uses: (block elements, bytes per block) 43# This is what makes a per-tensor byte count checkable, and it is the contract the kernels share. 44BLOCK_GEOMETRY :dict [str ,tuple [int ,int ]]= { 45"F32" : (1 ,4 ),"F16" : (1 ,2 ),"BF16" : (1 ,2 ),"F64" : (1 ,8 ), 46# one value per block: the ISTA-DAS DeepSeek-V4.1 first shard carries the Engram's hash parameters as integer tensors 47"I8" : (1 ,1 ),"I16" : (1 ,2 ),"I32" : (1 ,4 ),"I64" : (1 ,8 ), 48"Q4_0" : (32 ,18 ),"Q4_1" : (32 ,20 ),"Q5_0" : (32 ,22 ),"Q5_1" : (32 ,24 ), 49"Q8_0" : (32 ,34 ),"Q8_1" : (32 ,36 ), 50"Q2_K" : (256 ,84 ),"Q3_K" : (256 ,110 ),"Q4_K" : (256 ,144 ),"Q5_K" : (256 ,176 ), 51"Q6_K" : (256 ,210 ),"Q8_K" : (256 ,292 ), 52"IQ2_XXS" : (256 ,66 ),"IQ2_XS" : (256 ,74 ),"IQ3_XXS" : (256 ,98 ),"IQ1_S" : (256 ,50 ), 53"IQ4_NL" : (32 ,18 ),"IQ3_S" : (256 ,110 ),"IQ2_S" : (256 ,82 ),"IQ4_XS" : (256 ,136 ), 54"IQ1_M" : (256 ,56 ),"Q2_0" : (64 ,18 ),"MXFP4" : (32 ,17 ),"NVFP4" : (64 ,36 ), 55# fp16 scale + trellis stream (32 steps x 8 / 6 / 7 bits); PTQ1_0: 26 B of trits + fp16 scale 56"TQ2_T" : (128 ,34 ),"TQK6" : (128 ,26 ),"TQK7" : (128 ,30 ),"PTQ1_0" : (128 ,28 ), 57# TQP5/6/7: fp16 scale + 8*K bytes of a 64-step x K-bit circular stream (K = 5/6/7) 58"TQP5" : (128 ,42 ),"TQP6" : (128 ,50 ),"TQP7" : (128 ,58 ), 59} 60 61 62@ dataclasses . dataclass 63class TensorInfo : 64name :str 65shape :list [int ] 66type_id :int 67type_name :str 68offset :int 69 70@property 71def elements (self )-> int : 72n = 1 73for d in self .shape : 74n *= d 75return n 76 77def expected_bytes (self )-> int | None : 78geom = BLOCK_GEOMETRY .get (self .type_name ) 79if geom is None : 80return None 81block_elems ,block_bytes = geom 82if self .elements % block_elems : 83return None 84return self .elements // block_elems * block_bytes 85 86 87_META_FMT = {"u8" :"B" ,"i8" :"b" ,"u16" :"H" ,"i16" :"h" ,"u32" :"I" ,"i32" :"i" , 88"f32" :"f" ,"bool" :"?" ,"u64" :"Q" ,"i64" :"q" ,"f64" :"d" } 89 90 91class GGUFFile : 92def __init__ (self ,path :pathlib .Path ): 93self .path = pathlib .Path (path ) 94self .metadata :dict [str ,Any ]= {} 95self .tensors :list [TensorInfo ]= [] 96self .version = 0 97self .alignment = 32 98with self .path .open ("rb" )as fh : 99self ._size = os .fstat (fh .fileno ()).st_size 100self ._pos = 0 101try : 102self ._parse (fh ) 103except ValueError as e : 104raise ValueError (f" { self . path . name } : { e } " )from None 105self .data_start = self ._data_start 106 107# ------------------------------------------------------------------ internals 108def _read (self ,fh ,n :int )-> bytes : 109left = self ._size - self ._pos 110if n < 0 or n > left : 111raise ValueError (f"header ends early: { n :, } bytes needed at offset { self . _pos :, } , { left :, } left" ) 112raw = fh .read (n ) 113self ._pos += len (raw ) 114if len (raw )!= n : 115raise ValueError (f"header ends early at offset { self . _pos :, } " ) 116return raw 117 118def _unpack (self ,fh ,fmt :str )-> tuple : 119return struct .unpack (fmt ,self ._read (fh ,struct .calcsize (fmt ))) 120 121def _count (self ,fh ,count :int ,min_bytes :int ,what :str )-> int : 122if count * min_bytes > self ._size - self ._pos : 123raise ValueError (f" { count :, } { what } cannot fit in the { self . _size - self . _pos :, } bytes left" ) 124return count 125 126def _parse (self ,fh )-> None : 127 (magic ,)= self ._unpack (fh ,"<I" ) 128if magic != GGUF_MAGIC : 129raise ValueError (f"not a GGUF file (magic { magic :#x } )" ) 130self .version ,n_tensors ,n_kv = self ._unpack (fh ,"<IQQ" ) 131if self .version != 3 : 132raise ValueError (f"GGUF v { self . version } , this reader handles v3" ) 133for _ in range (self ._count (fh ,n_kv ,12 ,"metadata entries" )):# key length (8) + value type (4) 134key = self ._str (fh ) 135self .metadata [key ]= self ._value (fh ) 136for _ in range (self ._count (fh ,n_tensors ,24 ,"tensors" )):# name length, n_dims, type, offset 137name = self ._str (fh ) 138 (n_dims ,)= self ._unpack (fh ,"<I" ) 139shape = list (self ._unpack (fh ,f"< { self . _count ( fh , n_dims , 8 , 'dimensions' ) } Q" )) 140type_id ,offset = self ._unpack (fh ,"<IQ" ) 141self .tensors .append (TensorInfo (name ,shape ,type_id , 142GGML_TYPES .get (type_id ,f"type { type_id } " ),offset )) 143align = self .metadata .get ("general.alignment" ) 144if align is not None : 145if type (align )is not int or align <= 0 or align % 8 : 146raise ValueError ("general.alignment must be a positive multiple of 8" ) 147self .alignment = align 148pos = self ._pos 149self ._data_start = (pos + self .alignment - 1 )// self .alignment * self .alignment 150 151def _str (self ,fh )-> str : 152 (n ,)= self ._unpack (fh ,"<Q" ) 153return self ._read (fh ,n ).decode ("utf-8" ,"replace" ) 154 155def _meta_type (self ,t :int )-> tuple [str ,int | None ]: 156if t not in GGUF_META : 157raise ValueError (f"unknown metadata value type { t } " ) 158return GGUF_META [t ] 159 160def _value (self ,fh ): 161 (t ,)= self ._unpack (fh ,"<I" ) 162name ,size = self ._meta_type (t ) 163if name == "string" : 164return self ._str (fh ) 165if name == "array" : 166et ,count = self ._unpack (fh ,"<IQ" ) 167ename ,esize = self ._meta_type (et ) 168if ename == "array" : 169raise ValueError ("nested metadata arrays are not supported" ) 170if ename == "string" : 171return [self ._str (fh )for _ in range (self ._count (fh ,count ,8 ,"array strings" ))] 172self ._count (fh ,count ,esize ,"array elements" ) 173return list (self ._unpack (fh ,f"< { count } { _META_FMT [ ename ] } " )) 174return self ._unpack (fh ,f"< { _META_FMT [ name ] } " )[0 ] 175 176# ------------------------------------------------------------------ helpers 177def by_type (self )-> dict [str ,int ]: 178out :dict [str ,int ]= {} 179for t in self .tensors : 180out [t .type_name ]= out .get (t .type_name ,0 )+ 1 181return dict (sorted (out .items (),key = lambda kv :- kv [1 ])) 182 183def find (self ,needle :str )-> list [TensorInfo ]: 184return [t for t in self .tensors if needle in t .name ]