Googlu15's picture
full matrix driver
1524bac verified
Raw History Blame Contribute Delete
2.4 kB
"""Full Fig 6a matrix: all policies x 4 capacities x 2 arrival rates, warmup separated."""
from __future__ import annotations
import json
import sys
sys.path.insert(0, "/work/iac-repo")
sys.path.insert(0, "/work")
from iac.data import build_trace, load_conversations # noqa: E402
from simulate import WINDOWS, load_inputs, run, summarize # noqa: E402
RATES = [0.05, 0.3]
CAPS = [80000, 110000, 140000, 170000]
# (policy, threshold) pairs; tbs swept to its best threshold in a second pass
POLICIES = [("iac", None), ("iac_full", None), ("lru", None), ("lfu", None),
("gdsf", None), ("lpc", None), ("tbs", 120.0)]
def main():
reqs0, token_ids, stats = load_inputs(
"/work/traces/sharechat_r3", "/work/artifacts/tokens_llama3.npz",
"/work/artifacts/replay_intents.json", "/work/artifacts/replay_lpc_p.json",
"/work/artifacts/stats_sharechat.json")
remap = {(r["conv_id"], r["turn"]): (r["s"], r["e"]) for r in reqs0}
src = {(r["conv_id"], r["turn"]): r for r in reqs0}
convs = load_conversations("/work/data/splits/sharechat/replay.jsonl.gz")
results = {}
for rate in RATES:
trace = build_trace(convs, rate=rate, seed=0)
reqs = []
for r in trace.requests:
se = remap.get((r.conv_id, r.turn))
if se is None:
continue
s0 = src[(r.conv_id, r.turn)]
reqs.append({"req_id": r.req_id, "conv_id": r.conv_id, "turn": r.turn,
"arrival": r.arrival, "next_arrival": r.next_arrival,
"s": se[0], "e": se[1], "intent": s0["intent"], "p": s0["p"]})
reqs.sort(key=lambda r: r["arrival"])
for policy, thr in POLICIES:
tag = policy if thr is None else f"{policy}(thr={thr:g})"
for cap in CAPS:
records = run(policy, cap, reqs, token_ids, stats, thr or 0.0, 40.0)
key = f"{tag}@{cap}@rate{rate}"
results[key] = {w: summarize(records, lo, hi, we) for w, lo, hi, we in WINDOWS}
s = results[key]["warmup300"]
print(f"{key}: warmup300={s['hit_rate']:.4f} full={results[key]['full']['hit_rate']:.4f}", flush=True)
with open("/work/results/final.json", "w") as f:
json.dump(results, f, indent=1)
print("done", flush=True)
if __name__ == "__main__":
main()