#!/usr/bin/env python3 """Multi-tool set, step 3: write one compositional question + reference answer per plan from the grounded results of ALL its backend calls. python3 mt_qa.py --split test --provider claude-cli --model claude-opus-5-5 --out splits/mt/test.jsonl python3 mt_qa.py --split train --provider glm --model glm-5.3 --out splits/mt/train.jsonl Use of a provider's outputs, in particular to train or fine-tune models, is subject to that provider's terms; check them, and obtain any authorization they require, before such use. Only plans whose every call succeeded are used. The writer sees exactly the context the decoder will see (same per-backend head/tail cap). Validation: every number in the answer occurs in that context; the answer uses a value unique to each backend block; the question names the module, states the graph type / non-default optimisation target, and never names a backend function. Records follow the corpus schema; stage1_target is a JSON LIST of calls. """ import argparse, hashlib, json, os, re, subprocess, sys, tempfile, time, urllib.request from concurrent.futures import ThreadPoolExecutor, as_completed TOTAL_CAP = 2700 # all blocks together; fits the training window (ctx_cap) and is # far below circuitqa_infer TOOL_CAP (8000), so train/infer/writer see the same text CTX_CAP = 3300 # per-record stage2_context cap for training (question + blocks) OK = {"success", "estimated"} FUNCS = ["synthesis_tool", "simulation_tool", "graph_extractor", "static_analyzer", "optimization_advisor", "cross_stage_mapper"] LABEL = {"synthesis_tool": "synthesis", "simulation_tool": "simulation", "graph_extractor": "graph extraction", "static_analyzer": "static analysis", "optimization_advisor": "optimization analysis", "cross_stage_mapper": "RTL-to-netlist mapping"} SYS = ("You write evaluation questions for a hardware-design assistant. You are given the outputs of " "several EDA analysis backends run on one Verilog module. Write ONE realistic question that a " "hardware engineer could ask about this module and that can only be answered by combining " "information from ALL of the given backend outputs (for example: relate or compare figures from " "different analyses, check one result against another, weigh a design decision using several " "results, or ask for a short multi-part summary). Then write the reference answer.\n" "Rules for the question: mention the module name exactly; sound natural; do not name tools or " "software functions (no 'synthesis_tool', 'static_analyzer', no 'backend', no 'tool output'); " "if a graph analysis is involved, state its graph type (CDFG, AIG or DFG); if an optimization " "analysis targets delay or power, say so.\n" "Rules for the answer: 1 to 3 sentences; name the module exactly; every number you write must be copied exactly from the " "outputs; use at least one specific value from EACH output; no numbers that are not in the outputs " "(no computed sums, ratios or percentages unless that exact number is in the outputs).\n" 'Return only JSON: {"question": "...", "answer": "..."}') GLM_URL = "https://api.z.ai/api/paas/v4/chat/completions" CWD = tempfile.mkdtemp(prefix="mtqa-") NUM = re.compile(r"(? CTX_CAP: last = "ctx_too_long" if last is None: tr = "\n\n".join(b for _, b in bl) calls = [{"tool": c["tool"], "parameters": c["parameters"]} for c in p["calls"]] return None, { "question": q, "answer": ans, "source_file": p["source_file"], "module_name": p["module_name"], "verilog_code": code[p["design_id"]][:3000], "tool_name": "+".join(c["tool"] for c in calls), "tool_parameters": [c["parameters"] for c in calls], "tool_result": tr, "category": "multi_tool", "subcategory": "+".join(c["tool"] for c in calls), "difficulty": "compositional", "question_hash": hashlib.sha256((p["plan_id"] + q).encode()).hexdigest()[:12], "stage1_target": json.dumps(calls), "stage2_context": f"Question: {q}\n\n{tr}", "plan_id": p["plan_id"], "design_id": p["design_id"], "split": a.split, "writer_model": a.model, "n_calls": len(calls), "ctx_cap": CTX_CAP} return last, None t0 = time.time(); n = 0 with ThreadPoolExecutor(a.concurrency) as ex, open(a.out, "a") as f: futs = [ex.submit(work, p) for p in todo] for fu in as_completed(futs): err, rec = fu.result(); n += 1 if rec: f.write(json.dumps(rec) + "\n"); f.flush(); why["ok"] += 1 else: why[err] += 1 if n % 50 == 0: print(f" {n}/{len(todo)} {dict(why)} {time.time()-t0:.0f}s", flush=True) print(json.dumps(dict(why)), flush=True) if __name__ == "__main__": main()