Download code/gen_tables.py from mkvn/quantization-cache-amplification: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/datasets/mkvn/quantization-cache-amplification/resolve/main/code/gen_tables.py
- Command line
-
hf download hf://datasets/mkvn/quantization-cache-amplification/code/gen_tables.py
-
curl -L -o gen_tables.py https://huggingface.co/datasets/mkvn/quantization-cache-amplification/resolve/main/code/gen_tables.py
4.31 kB
| """Generate the paper's LaTeX tables directly from the measurement artefacts.""" | |
| import glob, json, os | |
| RES = os.path.join(os.path.dirname(__file__), "..", "results") | |
| PAP = os.path.join(os.path.dirname(__file__), "..", "paper") | |
| def load(n, d=None): | |
| p = os.path.join(RES, n) | |
| return json.load(open(p)) if os.path.exists(p) else d | |
| def w(name, s): | |
| open(os.path.join(PAP, name), "w").write(s) | |
| print("wrote", name) | |
| def tab_quality(): | |
| runs = {} | |
| for p in glob.glob(os.path.join(RES, "quant_*.json")): | |
| r = json.load(open(p)) | |
| runs[r["config"]["tag"]] = r | |
| fp = load("fp16_ppl.json") | |
| order = [ | |
| ("bf16 (reference)", None, None), | |
| ("RTN uniform, group 128", "rtn3", "scalar baseline"), | |
| ("RTN uniform, group 128", "rtn2", "scalar baseline"), | |
| ("RVQ, data-free", "noldlq15", "no LDLQ"), | |
| ("RVQ + LDLQ, no rotation", "northt15", "no incoherence proc."), | |
| ("RVQ + RHT + LDLQ (ours)", "main10", ""), | |
| ("RVQ + RHT + LDLQ (ours)", "main15", ""), | |
| ("RVQ + RHT + LDLQ (ours)", "main20", ""), | |
| ("\\quad + frequency-cond. alloc.", "freq15", "rate-matched"), | |
| ] | |
| L = ["\\begin{tabular}{llrr}", "\\toprule", | |
| "Method & Note & Bits/weight & PPL $\\downarrow$ \\\\", "\\midrule"] | |
| for name, tag, note in order: | |
| if tag is None: | |
| if fp: | |
| L.append(f"{name} & --- & 16.00 & {fp['ppl']:.2f} \\\\") | |
| L.append("\\midrule") | |
| continue | |
| if tag in runs: | |
| r = runs[tag] | |
| L.append(f"{name} & {note} & {r['avg_bits']:.2f} & {r['ppl']:.2f} \\\\") | |
| L += ["\\bottomrule", "\\end{tabular}"] | |
| w("tab_quality.tex", "\n".join(L)) | |
| def tab_amp(): | |
| pr = load("projection.json") | |
| if not pr: | |
| return | |
| L = ["\\begin{tabular}{rrrrrl}", "\\toprule", | |
| "Rate & Footprint & Expert & Resident & Cache & Fits \\\\", | |
| "(bits) & (GB) & (MB) & slots & fraction & 294\\,GB? \\\\", "\\midrule"] | |
| for a in pr["amplification"]: | |
| fits = "yes" if a["model_gb"] < 294 else "\\textbf{no}" | |
| L.append(f"{a['bits']:.1f} & {a['model_gb']:,.0f} & {a['expert_mb']:.2f} & " | |
| f"{a['dram_experts']:,} & {a['frac']*100:.2f}\\% & {fits} \\\\") | |
| L += ["\\bottomrule", "\\end{tabular}"] | |
| w("tab_amp.tex", "\n".join(L)) | |
| def tab_proj(): | |
| pr = load("projection.json") | |
| if not pr: | |
| return | |
| ws = pr.get("token_working_set", 512) | |
| rows = [r for r in pr["projection"] if r["batch"] in (1, 32)] | |
| L = ["\\begin{tabular}{rrrrrrr}", "\\toprule", | |
| "Rate & Cache & Recency & Hit & Fetch & \\multicolumn{2}{c}{Tokens/s} \\\\", | |
| "\\cmidrule(lr){6-7}", | |
| "(bits) & slots & viable? & rate & MB/token & batch 1 & batch 32 \\\\", | |
| "\\midrule"] | |
| seen = set() | |
| for r in sorted(rows, key=lambda x: -x["rate_bits"]): | |
| if r["rate_bits"] in seen: | |
| continue | |
| seen.add(r["rate_bits"]) | |
| b32 = next(x for x in pr["projection"] | |
| if x["rate_bits"] == r["rate_bits"] and x["batch"] == 32) | |
| ok = "yes" if r["cap_slots"] >= ws else "\\textbf{no}" | |
| L.append(f"{r['rate_bits']:.1f} & {r['cap_slots']:,} & {ok} & " | |
| f"{r['hit_rate']*100:.1f}\\% & {r['bytes_per_token_mb']:,.0f} & " | |
| f"{r['tok_s']:.2f} & {b32['tok_s']:.2f} \\\\") | |
| L += ["\\bottomrule", "\\end{tabular}"] | |
| w("tab_proj.tex", "\n".join(L)) | |
| def tab_policy(): | |
| cp = load("cache_policy.json") | |
| if not cp: | |
| return | |
| ws = cp["token_working_set"] | |
| L = ["\\begin{tabular}{rrrrrr}", "\\toprule", | |
| "Capacity & Slots & LRU & Static-freq. & Hybrid & Analytic \\\\", | |
| "\\midrule"] | |
| for r in cp["policies"]: | |
| mark = "$^\\dagger$" if r["cap"] < ws else "" | |
| L.append(f"{r['frac']*100:.1f}\\%{mark} & {r['cap']:,} & " | |
| f"{r['lru']*100:.1f}\\% & {r['static']*100:.1f}\\% & " | |
| f"{r['hybrid']*100:.1f}\\% & {r['analytic_static']*100:.1f}\\% \\\\") | |
| L += ["\\bottomrule", "\\end{tabular}"] | |
| w("tab_policy.tex", "\n".join(L)) | |
| if __name__ == "__main__": | |
| for f in [tab_quality, tab_amp, tab_proj, tab_policy]: | |
| try: | |
| f() | |
| except Exception as e: | |
| print("skip", f.__name__, type(e).__name__, e) | |