Files
leleka/scripts/offline/ggufgen.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 0d073e08aa feat(native-pack): match iq_pack.py conversions, add --compat-bf16/--base/--experts-bin and the exhaustive differential
- ple_conv1d inexact F32->F16 is accepted and recorded exact=false, as the oracle does
- conversions.json now carries method/exact/max_abs_err/source_shards like iq_pack.py
- opt-in --compat-bf16 (dequantize + RNE to BF16/F16/F32), --base (index_from_base, extra.bin),
  --experts-bin (+ experts.bin.src.json); receipts list the optional files present
- scripts/offline/diff_native_pack.py: 2700+ cases against the unmodified tools/iq_pack.py
- unit tests: real golden expert tables, numpy/gguf-py F16/BF16 rounding vectors
- fix three pre-existing clippy lints in leleka-model-format

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:20:15 +02:00

54 lines
2.3 KiB
Python

"""Synthetic GGUF v3 writer for the offline differential harnesses (small fixtures only, no model data).
Metadata values are written from (key, type, value) triples; `type` is one of the 13 GGUF value types spelled
u8 i8 u16 i16 u32 i32 f32 bool str u64 i64 f64, or ('array', element_type). Tensors are (name, dims, ggml_type,
raw_bytes) with dims fastest-first, laid out sequentially at the file's alignment, as llama.cpp writes them.
"""
import struct
SCALARS = {'u8': 'B', 'i8': 'b', 'u16': 'H', 'i16': 'h', 'u32': 'I', 'i32': 'i', 'f32': 'f', 'bool': '?',
'u64': 'Q', 'i64': 'q', 'f64': 'd'}
TYPE_ID = {'u8': 0, 'i8': 1, 'u16': 2, 'i16': 3, 'u32': 4, 'i32': 5, 'f32': 6, 'bool': 7, 'str': 8,
'array': 9, 'u64': 10, 'i64': 11, 'f64': 12}
def string(s):
b = s.encode() if isinstance(s, str) else s
return struct.pack('<Q', len(b)) + b
def scalar(kind, value):
if kind == 'str':
return string(value)
return struct.pack('<' + SCALARS[kind], value)
def encode_value(kind, value):
"""-> (type id word, payload)"""
if isinstance(kind, tuple):
assert kind[0] == 'array'
element = kind[1]
payload = struct.pack('<IQ', TYPE_ID[element], len(value)) + b''.join(scalar(element, v) for v in value)
return TYPE_ID['array'], payload
return TYPE_ID[kind], scalar(kind, value)
def write_gguf(path, tensors, metadata=(), alignment=32, version=3, trailing=0):
"""tensors: [(name, dims, ggml_type, raw)]; metadata: [(key, kind, value)]. `general.alignment` is NOT added
automatically: pass it in `metadata` when `alignment` is not 32."""
metadata = list(metadata)
offsets, body = [], b''
for _, _, _, raw in tensors:
offsets.append(len(body))
body += raw + bytes(-len(raw) % alignment)
out = b'GGUF' + struct.pack('<IQQ', version, len(tensors), len(metadata))
for key, kind, value in metadata:
tid, payload = encode_value(kind, value)
out += string(key) + struct.pack('<I', tid) + payload
for (name, dims, kind, _), off in zip(tensors, offsets):
out += string(name) + struct.pack('<I', len(dims)) + struct.pack('<' + 'Q' * len(dims), *dims)
out += struct.pack('<IQ', kind, off)
out += bytes(-len(out) % alignment)
path.write_bytes(out + body + bytes(trailing))
return len(out) # data_start