File size: 4,310 Bytes
6ec9472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""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)