"""Make the GGUF quants, score every text quant (with the f16 mmproj) through llama-server, pick the smallest pair that passes, check a q8_0 mmproj with it, and build the one-file bundle from the recommended pair. python scripts/quants.py # needs out/turbo-kev-gguf/*-BF16.gguf + mmproj f16 (bash run.sh export) A quant ships when (vs results/eval-whisper.json and results/eval-fused.json = BF16 + f16 mmproj): gates G1, G2 (both sets), G3 pass, and G1/G2 WERs are within 0.5 points of BF16. Quants are tried from large to small and the search stops at the first failure (quality falls with size). Results: results/quants.json, results/eval-gguf--.json. """ from __future__ import annotations import json import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) from tk import paths # noqa: E402 G = ROOT / "out" / "turbo-kev-gguf" NAME = "Turbo-Kev-Dictate" QUANTS = ["Q8_0", "Q6_K", "Q5_K_M", "Q4_K_M", "Q3_K_M", "Q2_K"] SETS = ["dis-test", "fleurs-en-test", "ls-clean-300", "say-test"] KEYS = ["G1_dis_wer_vs_clean", "G2_fleurs-en-test_wer", "G2_ls-clean-300_wer"] LLAMA = Path.home() / ".cache/spot-snake/llama.cpp" GGUF_PY = Path.home() / ".venvs/spot-snake-gguf/bin/python" def evaluate(label: str, text: Path, mm: Path) -> dict: f = paths.RESULTS / f"eval-{label}.json" if not f.exists(): subprocess.run([sys.executable, "-m", "tk.evaluate", "--label", label, "--gguf", str(text), "--mmproj", str(mm), "--sets", *SETS], check=True, cwd=ROOT, env={**os.environ, "PYTHONPATH": str(ROOT)}) return json.loads(f.read_text())["scores"] def ok(sc: dict, ref: dict) -> tuple[bool, dict]: g = sc.get("gates", {}) need = ["G1", "G2_fleurs-en-test", "G2_ls-clean-300", "G3"] why = {k: g.get(k) for k in need} for k in KEYS: why[f"{k}_vs_bf16"] = round(sc[k] - ref[k], 3) gates_ok = all(g.get(k) for k in need) if os.environ.get("TK_FAILED_GATE_OK") == "1": # user decision 2026-11-23: BF16 itself fails gates; a quant ships if it is no worse than BF16 rg = ref.get("gates", {}) gates_ok = all(g.get(k) or not rg.get(k) for k in need) passed = gates_ok and all(sc[k] <= ref[k] + 0.5 for k in KEYS) return passed, why def main() -> None: bf16 = G / f"{NAME}-BF16.gguf" mm16 = G / f"mmproj-{NAME}-f16.gguf" ref = json.loads((paths.RESULTS / "eval-fused.json").read_text())["scores"] rule = __doc__.split("A quant ships")[1].split("Results")[0].strip() if os.environ.get("TK_FAILED_GATE_OK") == "1": rule = ("when (vs BF16 + f16 mmproj, which itself fails gates): no gate that BF16 passes fails, and G1/G2 WERs " "are within 0.5 points of BF16.") res = {"rule": rule, "bf16": {k: ref[k] for k in KEYS}, "text": {}, "mmproj": {}} best = "BF16" for q in QUANTS: dst = G / f"{NAME}-{q}.gguf" if not dst.exists(): subprocess.run(["llama-quantize", str(bf16), str(dst), q], check=True, capture_output=True) sc = evaluate(f"gguf-{q}-f16", dst, mm16) passed, why = ok(sc, ref) res["text"][q] = {"pass": passed, "size_mb": round(dst.stat().st_size / 1e6), **why, **{k: sc[k] for k in KEYS}, "G3_prompts_kept_pct": sc.get("G3_prompts_kept_pct")} print(q, res["text"][q], flush=True) if not passed: break best = q res["text_recommended"] = best text_best = G / f"{NAME}-{best}.gguf" mm8 = G / f"mmproj-{NAME}-q8_0.gguf" if not mm8.exists(): subprocess.run([str(GGUF_PY), str(LLAMA / "convert_hf_to_gguf.py"), str(ROOT / "out" / "turbo-kev"), "--mmproj", "--outtype", "q8_0", "--outfile", str(mm8)], check=True, capture_output=True) sc = evaluate(f"gguf-{best}-q8_0", text_best, mm8) passed, why = ok(sc, ref) res["mmproj"]["q8_0"] = {"pass": passed, "with": best, "size_mb": round(mm8.stat().st_size / 1e6), **why, **{k: sc[k] for k in KEYS}} res["mmproj"]["f16"] = {"size_mb": round(mm16.stat().st_size / 1e6)} mm_best = mm8 if passed else mm16 res["mmproj_recommended"] = "q8_0" if passed else "f16" res["recommended_pair"] = [text_best.name, mm_best.name] # one-file bundle of the recommended pair (never 'mmproj' or a quant tag in its name: the Hub would serve it # as the projector, see gguf-round notes) bundle = G / f"{NAME}-bundle.gguf" bundle.unlink(missing_ok=True) subprocess.run([sys.executable, str(ROOT / "scripts" / "gguf_bundle.py"), "pack", str(bundle), str(text_best), str(mm_best), "--name", NAME], check=True) chk = ROOT / "runs" / "bundle-check" subprocess.run(["rm", "-rf", str(chk)], check=True) subprocess.run([sys.executable, str(ROOT / "scripts" / "gguf_bundle.py"), "unpack", str(bundle), "-o", str(chk)], check=True) same = all(subprocess.run(["cmp", "-s", str(p), str(chk / p.name)]).returncode == 0 for p in (text_best, mm_best)) res["bundle"] = {"file": bundle.name, "size_mb": round(bundle.stat().st_size / 1e6), "unpack_byte_identical": same} subprocess.run(["rm", "-rf", str(chk)], check=True) (paths.RESULTS / "quants.json").write_text(json.dumps(res, indent=2)) print(json.dumps({k: res[k] for k in ("text_recommended", "mmproj_recommended", "recommended_pair", "bundle")}, indent=1)) if __name__ == "__main__": sys.exit(main())