| """Emit paper/numbers.tex so every quantity quoted in the paper is read |
| directly from the measurement artefacts rather than transcribed by hand.""" |
| import glob, json, os, sys |
|
|
| RES = os.path.join(os.path.dirname(__file__), "..", "results") |
| OUT = os.path.join(os.path.dirname(__file__), "..", "paper", "numbers.tex") |
|
|
|
|
| def load(n, default=None): |
| p = os.path.join(RES, n) |
| return json.load(open(p)) if os.path.exists(p) else default |
|
|
|
|
| def fmt(x, d=2): |
| return f"{x:,.{d}f}" |
|
|
|
|
| def main(): |
| M = {} |
| io = load("io_bench.json") |
| if io: |
| rnd = io["random"] |
| best = max(rnd, key=lambda r: r["mb_s"]) |
| M["SeqRead"] = fmt(io["host"]["seq_read_mb_s"] / 1000, 2) |
| M["RandBest"] = fmt(best["mb_s"] / 1000, 2) |
| M["RandBestBlock"] = str(best["block_kb"] // 1024) |
| M["RandBestThreads"] = str(best["threads"]) |
| one = [r for r in rnd if r["block_kb"] == 1024 and r["threads"] == 1][0] |
| M["RandOneThread"] = fmt(one["mb_s"] / 1000, 2) |
| M["RamCopy"] = fmt(io["host"]["ram_copy_GBs"], 1) |
| M["PCIe"] = fmt(io["host"]["h2d_16MB_GBs"], 2) |
| r64 = [r for r in rnd if r["block_kb"] == 64 and r["threads"] == 1][0] |
| M["RandSmall"] = fmt(r64["mb_s"] / 1000, 2) |
|
|
| db = load("decode_bench.json") |
| if db: |
| d = [x for x in db["decode"] if abs(x["bits"] - 1.5) < 1e-6][0] |
| M["DecodePacked"] = fmt(d["packed_GBs"], 2) |
| M["DecodeFpEquiv"] = fmt(d["fp16_equiv_GBs"], 1) |
| M["DecodeGw"] = fmt(d["weights_per_s"] / 1e9, 2) |
| M["PeakTflops"] = fmt(max(v["tflops"] for v in db["matmul"].values()), 2) |
| M["GPUName"] = db["gpu"] |
|
|
| pr = load("projection.json") |
| if pr: |
| P = pr["params"] |
| M["TotalParams"] = fmt(P["total"] / 1e9, 1) |
| M["ActiveParams"] = fmt(P["active"] / 1e9, 1) |
| M["ExpertSlots"] = f"{int(P['n_slots']):,}" |
| amp = {round(a["bits"], 2): a for a in pr["amplification"]} |
| M["FootprintFP"] = fmt(amp[16]["model_gb"], 0) |
| M["FootprintOnePFive"] = fmt(amp[1.5]["model_gb"], 0) |
| M["FootprintTwo"] = fmt(amp[2.0]["model_gb"], 0) |
| M["FootprintOne"] = fmt(amp[1.0]["model_gb"], 0) |
| M["ExpertMBfp"] = fmt(amp[16]["expert_mb"], 1) |
| M["ExpertMB"] = fmt(amp[1.5]["expert_mb"], 2) |
| M["CacheFracFP"] = fmt(amp[16]["frac"] * 100, 2) |
| M["CacheFrac"] = fmt(amp[1.5]["frac"] * 100, 2) |
| M["CacheExpertsFP"] = f"{amp[16]['dram_experts']:,}" |
| M["CacheExperts"] = f"{amp[1.5]['dram_experts']:,}" |
| M["Amplification"] = fmt(amp[1.5]["frac"] / amp[16]["frac"], 1) |
| M["ZipfS"] = fmt(pr["zipf_s"], 2) |
| rows = {(r["rate_bits"], r["batch"]): r for r in pr["projection"]} |
| b1 = rows[(1.5, 1)] |
| M["HitRate"] = fmt(b1["hit_rate"] * 100, 1) |
| M["TokSecOne"] = fmt(b1["tok_s"], 2) |
| M["MBperTok"] = fmt(b1["bytes_per_token_mb"], 0) |
| M["IOatExpert"] = fmt(b1["io_bw_gbs"], 2) |
| M["ResidentGB"] = fmt(b1["resident_gb"], 2) |
| M["TokSecEight"] = fmt(rows[(1.5, 8)]["tok_s"], 2) |
| M["TokSecBatch"] = fmt(rows[(1.5, 32)]["tok_s"], 2) |
| M["TokSecTwoBit"] = fmt(rows[(2.0, 1)]["tok_s"], 2) |
| M["TokSecOneBit"] = fmt(rows[(1.0, 1)]["tok_s"], 2) |
| M["TokSecOneBitBatch"] = fmt(rows[(1.0, 32)]["tok_s"], 2) |
| fp16 = rows[(16.0, 1)] |
| M["TokSecFP"] = fmt(fp16["tok_s"], 2) |
| M["HitRateFP"] = fmt(fp16["hit_rate"] * 100, 1) |
| M["SlotsFP"] = f"{fp16['cap_slots']:,}" |
| M["SpeedupOverFP"] = fmt(b1["tok_s"] / fp16["tok_s"], 1) |
| M["AmpFactor"] = fmt(b1["tok_s"] / fp16["tok_s"] / (16.0 / 1.5), 2) |
| M["ReqDecode"] = fmt(b1["io_bw_gbs"], 2) |
| if "DecodePacked" in M: |
| M["DecodeGap"] = fmt(b1["io_bw_gbs"] / float(M["DecodePacked"]), 0) |
| M["FusedBw"] = fmt(b1["io_bw_gbs"] / 1.5 * 16 * 2, 0) |
| M["FusedRatio"] = fmt(b1["io_bw_gbs"] / 1.5 * 16 * 2 / 88.2, 2) |
| M["ZipfBias"] = fmt(pr.get("zipf_bias_pp", 0), 2) |
|
|
| cv = load("cache_validation.json") |
| if cv: |
| M["TraceTokens"] = f"{cv['tokens']:,}" |
| M["MassTopQ"] = fmt(cv["mass_top25pct"] * 100, 1) |
| M["ReuseMean"] = fmt(sum(cv["reuse_prev_token"]) / len(cv["reuse_prev_token"]) * 100, 1) |
| d = {r["batch"]: r for r in cv["distinct_per_batch"]} |
| for b, w_ in [(1, "One"), (8, "Eight"), (32, "ThirtyTwo")]: |
| if b in d: |
| M["Distinct" + w_] = fmt(d[b]["measured"], 1) |
| ws = cv["working_set"] |
| for k in ("1", "16", "64", "1024"): |
| if k in ws: |
| M["WorkSet" + {"1": "One", "16": "Sixteen", "64": "SixtyFour", |
| "1024": "Kilo"}[k]] = fmt(ws[k], 1) |
|
|
| cp = load("cache_policy.json") |
| if cp: |
| M["ZipfSmeas"] = fmt(cp["zipf_s"], 2) |
| M["TokenWS"] = f"{cp['token_working_set']:,}" |
| M["TokenWSFrac"] = fmt(cp["ws_frac"] * 100, 1) |
| M["Slots"] = f"{cp['n_slots']:,}" |
| M["StaticMAE"] = fmt(cp["mae_analytic_static"] * 100, 2) |
| M["ZipfMAE"] = fmt(cp["mae_analytic_zipf"] * 100, 2) |
| h = {round(r["frac"], 3): r for r in cp["policies"]} |
| for k, name in [(0.02, "Two"), (0.05, "Five"), (0.10, "Ten"), |
| (0.125, "Twelve"), (0.15, "Fifteen"), (0.25, "TwentyFive"), |
| (0.40, "Forty")]: |
| if k in h: |
| M["Lru" + name] = fmt(h[k]["lru"] * 100, 1) |
| M["Static" + name] = fmt(h[k]["static"] * 100, 1) |
| M["Hybrid" + name] = fmt(h[k]["hybrid"] * 100, 1) |
|
|
| fp = load("fp16_ppl.json") |
| if fp: |
| M["PPLfp"] = fmt(fp["ppl"], 2) |
|
|
| runs = {} |
| for p in glob.glob(os.path.join(RES, "quant_*.json")): |
| r = json.load(open(p)) |
| runs[r["config"]["tag"]] = r |
| for tag, name in [("main15", "Ours"), ("main20", "OursTwo"), |
| ("main10", "OursOne"), ("main25", "OursTwoFive"), |
| ("noldlq15", "NoLdlq"), ("northt15", "NoRht"), |
| ("rtn2", "RtnTwo"), ("rtn3", "RtnThree"), |
| ("freq15", "Freq")]: |
| if tag in runs: |
| M["PPL" + name] = fmt(runs[tag]["ppl"], 2) |
| M["Bits" + name] = fmt(runs[tag]["avg_bits"], 2) |
| M["NumRuns"] = str(len(runs)) |
|
|
| os.makedirs(os.path.dirname(OUT), exist_ok=True) |
| with open(OUT, "w") as f: |
| f.write("% auto-generated by code/gen_numbers.py -- do not edit\n") |
| for k, v in sorted(M.items()): |
| f.write("\\newcommand{\\n%s}{%s}\n" % (k, v)) |
| print(f"wrote {OUT} with {len(M)} macros") |
| for k, v in sorted(M.items()): |
| print(f" {k} = {v}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|