Download scripts/extract_legion_run.py from HuggingFaceSwarms/smallmath-matharena-empty-answers: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/datasets/HuggingFaceSwarms/smallmath-matharena-empty-answers/resolve/main/scripts/extract_legion_run.py
- Command line
-
hf download hf://datasets/HuggingFaceSwarms/smallmath-matharena-empty-answers/scripts/extract_legion_run.py
-
curl -L -o extract_legion_run.py https://huggingface.co/datasets/HuggingFaceSwarms/smallmath-matharena-empty-answers/resolve/main/scripts/extract_legion_run.py
2.5 kB
| """Extract the per-task fields of our Legion Pi run that build.py needs into source/legion_pi_per_task.jsonl. | |
| Input: a Legion sweep root (its rows.jsonl plus the Harbor Trial folders of each cell's selected attempt). The | |
| output keeps only task-level numbers and the submitted answer: no trajectories, hosts, paths or tokens. | |
| Usage: python scripts/extract_legion_run.py SWEEP_ROOT [OUT] (OUT defaults to source/legion_pi_per_task.jsonl) | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| root = Path(sys.argv[1]) | |
| out = Path(sys.argv[2]) if len(sys.argv) > 2 else Path(__file__).resolve().parent.parent / "source" / "legion_pi_per_task.jsonl" | |
| def load_traj(path): | |
| raw = path.read_text() | |
| try: | |
| return json.loads(raw) | |
| except ValueError: | |
| # Harbor's scrubber can leave "\[REDACTED]" inside a JSON escape; re-escape it to parse. | |
| return json.loads(raw.replace("\\[REDACTED]", "\\\\[REDACTED]")) | |
| rows = [] | |
| for line in (root / "rows.jsonl").read_text().splitlines(): | |
| cell = json.loads(line) | |
| assert cell["status"] == "completed", cell["selector"] | |
| att = Path(cell["selected_attempt"]) | |
| trials = sorted((root / att / "results" / att.name / "agent-001" / "trials").iterdir()) | |
| assert len(trials) == 1, (cell["selector"], trials) | |
| t = trials[0] | |
| res = json.loads((t / "result.json").read_text()) | |
| grade = json.loads((t / "verifier" / "grade.json").read_text()) | |
| reward = res["verifier_result"]["rewards"]["reward"] | |
| assert bool(reward) == bool(grade["passed"]), cell["selector"] | |
| sub = grade.get("submission") | |
| steps = load_traj(t / "agent" / "trajectory.json")["steps"] | |
| agent = [s for s in steps if s.get("source") == "agent" and (s.get("extra") or {}).get("stop_reason")] | |
| rows.append({ | |
| "task": cell["selector"], | |
| "submission": sub.strip() if isinstance(sub, str) else None, | |
| "correct": bool(reward), | |
| "grade_why": grade.get("why"), | |
| "output_tokens": res["agent_result"]["n_output_tokens"], | |
| "llm_calls": len(agent), | |
| "tool_calls": sum(len(s.get("tool_calls") or []) for s in steps if s.get("source") == "agent"), | |
| "length_stops": sum(s["extra"]["stop_reason"] == "length" for s in agent), | |
| }) | |
| rows.sort(key=lambda r: r["task"]) | |
| assert len(rows) == len({r["task"] for r in rows}) == 600, len(rows) | |
| out.parent.mkdir(parents=True, exist_ok=True) | |
| out.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in rows)) | |
| print(f"wrote {len(rows)} rows to {out}") | |