"""quant_gguf.py - merged fp16 -> GGUF f16 -> Q4_K_M (+Q8_0). RECONSTRUCTED 2026-11-21. The original died with its box. Tooling is public and pinned here: * llama.cpp b12602 prebuilt ubuntu binaries (llama-quantize) from the GitHub release * convert_hf_to_gguf.py from the same tag's repository The published PLAN.md step 5 makes this a *publish gate*: Q4_K_M is what the 8 GB deployment target runs, so the adapter's numbers must be re-measured on the quantised weights before any download link. """ import hashlib, json, os, pathlib, subprocess, sys, time Q = pathlib.Path("/home/marimo/dragon_fsds/quant") MERGED = Q/"merged-v7" GGUF = Q/"gguf"; GGUF.mkdir(parents=True, exist_ok=True) TOOLS = pathlib.Path("/home/marimo/tools") QUANTIZE = TOOLS/"llama-cpu/llama-b12602/llama-quantize" CONVERT = pathlib.Path("/home/marimo/tools/llama.cpp-b12602/convert_hf_to_gguf.py") def log(*a): print(time.strftime("%H:%M:%S"), *a, flush=True) def sha(p): h = hashlib.sha256() with open(p, "rb") as fh: for b in iter(lambda: fh.read(1 << 22), b""): h.update(b) return h.hexdigest() t0 = time.time(); out = {} f16 = GGUF/"dragon-finance-7b-f16.gguf" if not f16.exists(): log("convert_hf_to_gguf.py ->", f16.name) r = subprocess.run([sys.executable, str(CONVERT), str(MERGED), "--outfile", str(f16), "--outtype", "f16"], env={**os.environ, "PYTHONPATH": "/home/marimo/tools/llama.cpp-b12602:/home/marimo/tools/llama.cpp-b12602/gguf-py"}, capture_output=True, text=True) log(" rc", r.returncode, (r.stdout or r.stderr).strip().splitlines()[-1][:200] if (r.stdout or r.stderr) else "") if r.returncode != 0: print(r.stdout[-3000:]); print(r.stderr[-3000:]); raise SystemExit(1) log(f"f16 GGUF: {f16.stat().st_size/1e9:.2f} GB") out["f16"] = {"path": str(f16), "bytes": f16.stat().st_size, "sha256": sha(f16)} for tag in ("Q4_K_M", "Q8_0"): dst = GGUF/f"dragon-finance-7b-{tag}.gguf" if not dst.exists(): log("llama-quantize ->", dst.name) r = subprocess.run([str(QUANTIZE), str(f16), str(dst), tag], capture_output=True, text=True) log(" rc", r.returncode, r.stdout.strip().splitlines()[-1][:120] if r.stdout else "") if r.returncode != 0: print(r.stdout[-2000:]); print(r.stderr[-2000:]); continue out[tag.lower()] = {"path": str(dst), "bytes": dst.stat().st_size, "sha256": sha(dst)} log(f"{tag}: {dst.stat().st_size/1e9:.2f} GB") out["tooling"] = {"llama_cpp": "b12602 (github release, ubuntu-x64)", "quantize": str(QUANTIZE), "converter": str(CONVERT), "converter_sha256": sha(CONVERT)} out["seconds"] = round(time.time()-t0, 1); out["at"] = time.strftime("%Y-%m-%d %H:%M:%S") out["note"] = "RECONSTRUCTED script: the original quant_gguf.py was never published." (Q/"QUANT.json").write_text(json.dumps(out, indent=1)) log("QUANT.json written; DONE")