"""Reuses v2 GGUF writer. Custom architectures; no llama.cpp support.""" from pathlib import Path import sys,json,gzip,hashlib,math import numpy as np from gguf_format import Writer,GGML_I8,GGML_F16,dequantise B=Path(__file__).resolve().parents[1];sys.path.insert(0,str(B/'python')) from bhc.nlm import NLM from gguf_read import GGUF O=B/'gguf';O.mkdir(exist_ok=True);report={} def common(cfg,name,component,lic,params): w=Writer();w.string('general.architecture','aksharalp' if component=='word' else 'aksharalp-lstmp');w.string('general.name',name);w.string('general.author','Sourojit Dhua');w.string('general.license',lic);w.uint32('general.alignment',32);w.string('aksharalp.config_json',json.dumps(cfg,separators=(',',':')));w.string('aksharalp.component',component);w.uint32('aksharalp.parameter_count',params);w.boolean('aksharalp.requires_bundled_decoder',True);w.boolean('aksharalp.runs_in_llama_cpp',False);return w for la in ['hi','bn','te','kn','ta']: p=B/'models'/la;blob=(p/'xlit.q4').read_bytes();n=int.from_bytes(blob[:4],'little');meta=json.loads(blob[4:4+n]);data=blob[4+n:];np_=sum(math.prod(x[1]) for x in meta['tensors']);params=(p/'params.json').read_text();lic='apache-2.0' if la=='hi' else 'cc-by-nc-4.0' for variant in ['q4','f16']: w=common(meta['cfg'],f'AksharaLP-MultiIndic-8M {la} word {variant}','word',lic,np_);w.string('aksharalp.language',la);w.string('aksharalp.params_json',params) if la=='hi':w.string('aksharalp.pack_params_json',(p/'dict/params.json').read_text()) if variant=='q4': w.string('aksharalp.storage','byte-exact shipped xlit.q4 as I8 blob; mixed q4/q8/f16, not GGML Q4_0');w.string('aksharalp.q4.sha256',hashlib.sha256(blob).hexdigest());t=[('aksharalp.q4.blob',np.frombuffer(blob,np.int8),GGML_I8)] else: w.string('aksharalp.storage','F16 materialization of shipped quantized weights; original reranker JSON is retained at exact precision in metadata, external dictionaries required');t=[(k,a.astype(np.float16),GGML_F16) for k,sh,a in dequantise(meta,data)] dest=O/f'aksharalp-{la}-word-{variant}.gguf';w.write(dest,t);GGUF(dest).validate();report[dest.name]=dict(raw=dest.stat().st_size,gzip9=len(gzip.compress(dest.read_bytes(),9,mtime=0)),sha256=hashlib.sha256(dest.read_bytes()).hexdigest()) p=B/'models/hi/sentence/nlm.q8';blob=p.read_bytes();n=int.from_bytes(blob[:4],'little');meta=json.loads(blob[4:4+n]);lm=NLM.load(p) for variant in ['q8','q4','f16']: w=common(meta['cfg'],f'AksharaLP-MultiIndic-8M hi sentence {variant}','sentence','apache-2.0',2770752);w.string('aksharalp.language','hi');w.string('aksharalp.tokenizer_json',json.dumps(meta['vocab'],ensure_ascii=False,separators=(',',':'))) if variant=='f16': w.string('aksharalp.storage','F16 materialization of shipped q8 sentence LM');t=[(k,a.astype(np.float16),GGML_F16) for k,a in lm.W.items()] else: bb=blob if variant=='q8' else (B/'models/hi/sentence/nlm.q4').read_bytes();w.string('aksharalp.storage',f'byte-exact existing nlm.{variant} file as I8 blob; custom grouped low-bit storage');w.string('aksharalp.source.sha256',hashlib.sha256(bb).hexdigest());w.string('aksharalp.variant_note','q8 is the shipped test-measured default; q4 is validation-parity-only, no test accuracy claim');t=[('aksharalp.nlm.blob',np.frombuffer(bb,np.int8),GGML_I8)] dest=O/f'aksharalp-hi-sentence-{variant}.gguf';w.write(dest,t);GGUF(dest).validate();report[dest.name]=dict(raw=dest.stat().st_size,gzip9=len(gzip.compress(dest.read_bytes(),9,mtime=0)),sha256=hashlib.sha256(dest.read_bytes()).hexdigest()) (B/'evaluation').mkdir(exist_ok=True);(B/'evaluation/gguf_sizes.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))