Files

53 lines
2.5 KiB
Python

"""Read only public GGUF headers to locate quantization differences."""
import collections, json, math, struct, sys
from pathlib import Path
root = Path('/opt/athena-deck-dev/runtime/state')
pid = int(sys.argv[1])
cmd = [x.decode() for x in Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0') if x]
assert cmd[1] == '/app/qwen_native_worker.py'
original = root/Path(cmd[3]).relative_to('/var/lib/deck')
candidate = root/'experiments/tts-codec-q4-20261022/qwen-tokenizer-12hz-Q4_K_M.gguf'
def inspect(path):
with path.open('rb') as source:
def number(fmt):
return struct.unpack('<'+fmt, source.read(struct.calcsize('<'+fmt)))[0]
def string():
return source.read(number('Q')).decode()
def skip(kind):
if kind == 8:
source.seek(number('Q'), 1)
elif kind == 9:
element, length = number('I'), number('Q')
for _ in range(length):
skip(element)
else:
source.seek({0:1,1:1,2:2,3:2,4:4,5:4,6:4,7:1,10:8,11:8,12:8}[kind],1)
assert source.read(4) == b'GGUF'
assert number('I') == 3
tensors, entries = number('Q'), number('Q')
for _ in range(entries):
string()
skip(number('I'))
groups = collections.defaultdict(lambda:collections.Counter())
layout = {}
for _ in range(tensors):
name = string()
dims = [number('Q') for _ in range(number('I'))]
kind, offset = number('I'), number('Q')
group = 'decoder' if name.startswith('tok_dec.') else 'encoder' if name.startswith('tok_enc.') else 'other'
layout[name] = (dims,kind)
block, size = {0:(1,4),1:(1,2),8:(32,34),12:(256,144),13:(256,176),14:(256,210)}[kind]
groups[group][str(kind)] += math.prod(dims)//block*size
return dict(file_bytes=path.stat().st_size, weight_bytes_by_group_and_ggml_type=dict(groups)),layout
old, a = inspect(original)
new, b = inspect(candidate)
assert set(a) == set(b) and all(a[k][0] == b[k][0] for k in a)
changed = collections.Counter('decoder' if k.startswith('tok_dec.') else 'encoder' if k.startswith('tok_enc.') else 'other'
for k in a if a[k][1] != b[k][1])
result = dict(q8=old,q4=new,changed_tensor_types_by_group=dict(changed),same_tensor_names_and_shapes=True)
Path(__file__).with_name('codec-headers.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps(result),flush=True)