#!/usr/bin/env python3 """Build the released CircuitQA corpus from the template questions and the repository-disjoint split. For every record the design's full source is recovered from its upstream repository (accepted only if it matches the text the question was generated from), the record's backend is run on that source with the question's own parameters (timing and power optimisation questions use the delay and power targets), and the answer is computed from the backend output with the question template. Records are kept only if the backend measures the design: records whose backend reports an error, simulations whose generated testbench does not compile or run, cross-stage mappings whose synthesis produced no netlist, and toggle-rate questions about a signal that is not in the recorded waveform are not included. Simulation activity is measured from the VCD waveform. Steps: python3 corpus_v2.py candidates -> splits/v2/candidates.jsonl (resolve with corpus_resolve.py) python3 corpus_v2.py calls -> splits/v2/calls.jsonl (run with mt_ground.py) python3 corpus_v2.py build -> splits/v2/{train,val,test}.jsonl """ import json, re, sys, os, hashlib, collections REV = os.path.dirname(os.path.abspath(__file__)); os.chdir(REV) os.makedirs("splits/v2", exist_ok=True) SPLITS = ("train", "val", "test") def did(r): return hashlib.sha256((r["source_file"] + "::" + r["module_name"]).encode()).hexdigest()[:12] def target(r): t = r["stage1_target"]; t = json.loads(t) if isinstance(t, str) else t return t["tool"], t.get("parameters") or {} def ckey(d, tool, p): return hashlib.sha256(json.dumps([d, {"tool": tool, "parameters": p}], sort_keys=True).encode()).hexdigest()[:16] mode = sys.argv[1] if mode == "candidates": seen = {} for s in SPLITS: for l in open(f"splits/s3_repo_disjoint.{s}.jsonl"): r = json.loads(l); k = did(r) if k not in seen: seen[k] = {"design_id": k, "split": s, "source_file": r["source_file"], "module_name": r["module_name"], "stored": r["verilog_code"] or ""} with open("splits/v2/candidates.jsonl", "w") as f: for c in seen.values(): f.write(json.dumps(c) + "\n") print("unique designs", len(seen)) elif mode == "calls": designs = {json.loads(l)["design_id"] for l in open("splits/v2/designs_full.jsonl")} calls = {} for s in SPLITS: for l in open(f"splits/s3_repo_disjoint.{s}.jsonl"): r = json.loads(l); d = did(r) if d not in designs: continue tool, p = target(r); k = ckey(d, tool, p) calls[k] = {"key": k, "design_id": d, "tool": tool, "parameters": p} with open("splits/v2/calls.jsonl", "w") as f: for c in calls.values(): f.write(json.dumps(c) + "\n") print("unique calls", len(calls), collections.Counter(c["tool"] for c in calls.values())) elif mode == "build": sys.path.insert(0, REV) import generate_qa_v7_full_tools as G import importlib.util _spec = importlib.util.spec_from_file_location("eda_release", os.path.join(REV, "eda_tools.py")) OPT = importlib.util.module_from_spec(_spec); _spec.loader.exec_module(OPT) code = {json.loads(l)["design_id"]: json.loads(l)["verilog_code"] for l in open("splits/v2/designs_full.jsonl")} res = {} for l in open("splits/v2/results.jsonl"): r = json.loads(l); res[r["key"]] = r["result"] stats = {} for s in SPLITS: out, why = [], collections.Counter(); changed = 0 for l in open(f"splits/s3_repo_disjoint.{s}.jsonl"): r = json.loads(l); d = did(r) if d not in code: why["source_not_recovered"] += 1; continue tool, p = target(r); t = res.get(ckey(d, tool, p)) if t is None: why["not_grounded"] += 1; continue if r["subcategory"] in ("delay_opt", "power_opt"): # the question asks for timing/power advice: run the optimisation backend for that target p = {"target_metric": "delay" if r["subcategory"] == "delay_opt" else "power"} t = OPT.optimization_advisor(code[d], p["target_metric"]) r = dict(r, stage1_target=json.dumps({"tool": tool, "parameters": p})); why["opt_target_set"] += 1 if t.get("status") == "error": why["backend_error"] += 1; continue ms = t.get("mapping_statistics") or {} if tool == "cross_stage_mapper" and not ms.get("netlist_wires") and not ms.get("netlist_cells"): why["synthesis_no_netlist"] += 1; continue # synthesis failed: nothing to map against if tool == "simulation_tool" and t.get("status") == "estimated": why["simulation_not_measurable"] += 1; continue signal = None m = re.match(r"What is the toggle rate of signal (\S+) in module", r["question"]) if m: signal = m.group(1) if signal not in (t.get("toggle_rates") or {}): # signal not in the recorded waveform: no measured value why["toggle_signal_not_recorded"] += 1; continue ans = G.V7Generator._generate_answer(None, r["category"], r["subcategory"], r["question"], t, r["module_name"], signal, p.get("graph_type", "CDFG"), p.get("target_metric", "area")) essential = {k: v for k, v in t.items() if k not in ["synthesis_log", "simulation_log", "nodes", "edges"]} ctx = f"Question: {r['question']}\n\nTool: {tool}\nResult: {json.dumps(essential)}" if len(ctx) > 1500: ctx = ctx[:1500] + "..." changed += ans != r["answer"] out.append(dict(r, answer=ans, verilog_code=code[d], tool_result=t, tool_parameters=p, stage2_context=ctx, corpus_version="release")) why["kept"] += 1 with open(f"splits/v2/{s}.jsonl", "w") as f: for o in out: f.write(json.dumps(o) + "\n") n = sum(why.values()); stats[s] = {"v1": n, **why, "answer_changed": changed} print(s, json.dumps(stats[s])) json.dump(stats, open("splits/v2/stats.json", "w"), indent=1)