circuitqa / code /mt_plan.py
drawais's picture
CircuitQA release
676defa
Raw History Blame Contribute Delete
2.71 kB
#!/usr/bin/env python3
"""Multi-tool set, step 2 (after grounding every backend variant):
draw backend combinations per design ONLY from calls that succeeded on it.
80% two-backend, 20% three-backend; PER distinct combinations per design.
Parameters: graph type CDFG/AIG/DFG 50/25/25, optimisation target area/delay/power 50/25/25,
restricted to variants that succeeded.
python3 mt_plan.py -> splits/mt/{train,val,test}.plan.jsonl
"""
import json, random, hashlib, itertools, collections
OK = {"success", "estimated"}
PER = {"train": 2, "val": 2, "test": 2}
W = {"CDFG": .5, "AIG": .25, "DFG": .25, "area": .5, "delay": .25, "power": .25}
rnd = random.Random(2026)
designs = {json.loads(l)["design_id"]: json.loads(l) for l in open("splits/mt/designs_full.jsonl")}
good = collections.defaultdict(lambda: collections.defaultdict(list)) # did -> tool -> [call]
for l in open("splits/mt/results_full.jsonl"):
r = json.loads(l)
if r["result"].get("status") in OK:
good[r["design_id"]][r["tool"]].append({"tool": r["tool"], "parameters": r["parameters"], "key": r["key"]})
stats = collections.Counter()
for split in ("train", "val", "test"):
out = []
ids = sorted(d for d in designs if designs[d]["split"] == split)
for did in ids:
tools = sorted(good[did])
if len(tools) < 2: stats[(split, "fewer_than_2_tools")] += 1; continue
combos = list(itertools.combinations(tools, 2)); tri = list(itertools.combinations(tools, 3))
chosen = set(); tries = 0
while len(chosen) < min(PER[split], len(combos) + len(tri)) and tries < 50:
tries += 1
pool = tri if (tri and rnd.random() < 0.2) else combos
combo = rnd.choice(pool)
if combo in chosen: continue
chosen.add(combo)
calls = []
for t in combo:
vs = good[did][t]
v = rnd.choices(vs, [W.get(next(iter(c["parameters"].values()), ""), 1) if c["parameters"] else 1 for c in vs])[0] \
if t in ("graph_extractor", "optimization_advisor") else vs[0]
calls.append(v)
d = designs[did]
out.append({"plan_id": hashlib.sha256(json.dumps([did, combo]).encode()).hexdigest()[:12], "design_id": did,
"split": split, "module_name": d["module_name"], "source_file": d["source_file"], "calls": calls})
stats[(split, f"{len(combo)}-tool")] += 1
with open(f"splits/mt/{split}.plan.jsonl", "w") as f:
for o in out: f.write(json.dumps(o) + "\n")
stats[(split, "designs")] = len({o["design_id"] for o in out}); stats[(split, "plans")] = len(out)
for k, v in sorted(stats.items()): print(k, v)