omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / bench / score.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Evaluate MultiPL-E completion files with eval_om and summarise pass@k. |
| 4 | |
| 5 | OMEGA_OMC=/path/to/omc score.py [--expect OM_STATUS[:CASE]] DIR [DIR ...] |
| 6 | |
| 7 | For every ``DIR/<name>.json`` this writes ``DIR/<name>.results.json`` in the |
| 8 | format of MultiPL-E ``evaluation/src/main.py`` (the completion record without |
| 9 | ``completions``, plus ``results``: program, stdout, stderr, exit_code, status, |
| 10 | timestamp; eval_om also adds om_status / om_case), then prints one JSON summary |
| 11 | line per DIR and writes it to ``DIR/summary.json``. The program is |
| 12 | prompt + completion + "\\n" + tests, exactly as upstream builds it. pass@k is |
| 13 | upstream pass_k.py's unbiased estimator, counting status "OK" with exit_code 0. |
| 14 | |
| 15 | Foreign-harness tooling, not first-party Omega; outside every product path. |
| 16 | """ |
| 17 | |
| 18 | import collections |
| 19 | import json |
| 20 | import sys |
| 21 | import tempfile |
| 22 | import time |
| 23 | from pathlib import Path |
| 24 | |
| 25 | sys.path.insert(0, str(Path(__file__).resolve().parent / "multipl-e")) |
| 26 | import eval_om # noqa: E402 |
| 27 | |
| 28 | |
| 29 | def estimator(n: int, c: int, k: int) -> float: |
| 30 | """1 - C(n-c, k) / C(n, k), computed as pass_k.py does, without numpy.""" |
| 31 | if n - c < k: |
| 32 | return 1.0 |
| 33 | prod = 1.0 |
| 34 | for i in range(n - c + 1, n + 1): |
| 35 | prod *= 1.0 - k / i |
| 36 | return 1.0 - prod |
| 37 | |
| 38 | |
| 39 | def evaluate(problem: dict) -> list: |
| 40 | results = [] |
| 41 | for completion in problem["completions"]: |
| 42 | program = problem["prompt"] + completion + "\n" + problem["tests"] |
| 43 | with tempfile.NamedTemporaryFile(suffix=".om") as f: |
| 44 | f.write(program.encode("utf-8")) |
| 45 | f.flush() |
| 46 | r = eval_om.eval_script(Path(f.name)) |
| 47 | results.append({"program": program, **r, "timestamp": int(time.time())}) |
| 48 | return results |
| 49 | |
| 50 | |
| 51 | def score_dir(d: Path) -> dict: |
| 52 | per_problem, kinds = [], collections.Counter() |
| 53 | for path in sorted(d.glob("*.json")): |
| 54 | if path.name.endswith(".results.json") or path.name == "summary.json": |
| 55 | continue |
| 56 | problem = json.loads(path.read_text()) |
| 57 | record = {k: v for k, v in problem.items() if k != "completions"} |
| 58 | record["results"] = evaluate(problem) |
| 59 | (d / (path.name[: -len(".json")] + ".results.json")).write_text(json.dumps(record, indent=2) + "\n") |
| 60 | n = len(record["results"]) |
| 61 | c = sum(1 for r in record["results"] if r["status"] == "OK" and r["exit_code"] == 0) |
| 62 | kinds.update(r["om_status"] for r in record["results"]) |
| 63 | per_problem.append({"name": problem["name"], "n": n, "c": c, |
| 64 | "om_status": [r["om_status"] for r in record["results"]]}) |
| 65 | if not per_problem: |
| 66 | raise SystemExit(f"score: no completion files in {d}") |
| 67 | summary = { |
| 68 | "dir": d.name, |
| 69 | "problems": len(per_problem), |
| 70 | "pass@1": sum(estimator(p["n"], p["c"], 1) for p in per_problem) / len(per_problem), |
| 71 | "om_status": dict(sorted(kinds.items())), |
| 72 | "per_problem": per_problem, |
| 73 | } |
| 74 | (d / "summary.json").write_text(json.dumps(summary, indent=2) + "\n") |
| 75 | return summary |
| 76 | |
| 77 | |
| 78 | def main(argv): |
| 79 | expect = None |
| 80 | if argv[:1] == ["--expect"] and len(argv) > 1: |
| 81 | expect, argv = argv[1].split(":"), argv[2:] |
| 82 | if not argv: |
| 83 | print(__doc__, file=sys.stderr) |
| 84 | return 2 |
| 85 | rc = 0 |
| 86 | for d in argv: |
| 87 | s = score_dir(Path(d)) |
| 88 | print(json.dumps({k: v for k, v in s.items() if k != "per_problem"})) |
| 89 | if expect is None: |
| 90 | continue |
| 91 | for p in s["per_problem"]: |
| 92 | results = json.loads((Path(d) / (p["name"] + ".results.json")).read_text())["results"] |
| 93 | for r in results: |
| 94 | got = [r["om_status"]] + ([str(r["om_case"])] if len(expect) > 1 else []) |
| 95 | if got != expect: |
| 96 | print(f"score: FAIL {p['name']}: expected {':'.join(expect)}, got {':'.join(got)}") |
| 97 | rc = 1 |
| 98 | return rc |
| 99 | |
| 100 | |
| 101 | if __name__ == "__main__": |
| 102 | sys.exit(main(sys.argv[1:])) |