omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / bench / stub_model.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Stub "model" for the Omega MultiPL-E harness: writes MultiPL-E completion files |
| 4 | without calling any model server. |
| 5 | |
| 6 | stub_model.py MODE OUTDIR [--prompts bench/prompts/humaneval-om.jsonl] |
| 7 | |
| 8 | One ``<name>.json`` per problem, in the layout ``multipl_e.completions`` writes |
| 9 | (name, language, temperature, top_p, max_tokens, prompt, tests, completions, |
| 10 | stop_tokens), one completion each. Mode ``stub`` covers every problem in the |
| 11 | prompts file; the other modes cover the problems with a hand solution in |
| 12 | bench/solutions/. |
| 13 | |
| 14 | MODE completion expected om_status |
| 15 | hand bench/solutions/HumanEval_<n>.om pass |
| 16 | wrong a constant of the return type (0 / 0.0 / false) wrong_answer |
| 17 | invalid text that is not Omega compile_error |
| 18 | trap trap 5 trap, case 5 |
| 19 | timeout three nested loops of 2^24 (about 2^72 steps) timeout |
| 20 | stub trap 14 (the translator's prompt stub) trap, case 14 |
| 21 | |
| 22 | Foreign-harness tooling, not first-party Omega; outside every product path. |
| 23 | """ |
| 24 | |
| 25 | import json |
| 26 | import re |
| 27 | import sys |
| 28 | from pathlib import Path |
| 29 | |
| 30 | BENCH = Path(__file__).resolve().parent |
| 31 | MODES = ("hand", "wrong", "invalid", "trap", "timeout", "stub") |
| 32 | _RET = re.compile(r"^fn \S+ (?:.* )?to (\w+)$", re.M) |
| 33 | |
| 34 | |
| 35 | def ret_type(prompt: str) -> str: |
| 36 | return list(_RET.finditer(prompt))[-1].group(1) |
| 37 | |
| 38 | |
| 39 | def default(t: str) -> str: |
| 40 | return {"bool": "false", "f64": "0.0"}.get(t, "0") |
| 41 | |
| 42 | |
| 43 | def completion(mode: str, problem: dict, hand: str) -> str: |
| 44 | t = ret_type(problem["prompt"]) |
| 45 | if mode == "hand": |
| 46 | return hand |
| 47 | if mode == "wrong": |
| 48 | return f"ret {default(t)}\nend\n" |
| 49 | if mode == "invalid": |
| 50 | return "this is ( not Omega\nend\n" |
| 51 | if mode == "trap": |
| 52 | return "trap 5\nend\n" |
| 53 | if mode == "timeout": |
| 54 | return ( |
| 55 | "n = ulong 0\n" |
| 56 | "for i ulong 16777216\nfor j ulong 16777216\nfor k ulong 16777216\n" |
| 57 | "n = xor n k\nend\nend\nend\n" |
| 58 | f"ret {default(t)}\nend\n" |
| 59 | ) |
| 60 | if mode == "stub": |
| 61 | return "trap 14\nend\n" |
| 62 | raise ValueError(mode) |
| 63 | |
| 64 | |
| 65 | def main(argv): |
| 66 | if len(argv) < 2 or argv[0] not in MODES: |
| 67 | print(__doc__, file=sys.stderr) |
| 68 | return 2 |
| 69 | mode, out = argv[0], Path(argv[1]) |
| 70 | prompts = Path(argv[3]) if len(argv) > 3 and argv[2] == "--prompts" else BENCH / "prompts/humaneval-om.jsonl" |
| 71 | problems = [json.loads(line) for line in prompts.read_text().splitlines()] |
| 72 | if mode == "stub": |
| 73 | work = [(p, "") for p in problems] |
| 74 | else: |
| 75 | by_number = {p["name"].split("_")[1]: p for p in problems} |
| 76 | sols = sorted((BENCH / "solutions").glob("HumanEval_*.om"), key=lambda s: int(s.stem.split("_")[1])) |
| 77 | work = [] |
| 78 | for sol in sols: |
| 79 | p = by_number.get(sol.stem.split("_")[1]) |
| 80 | if p is None: |
| 81 | print(f"stub_model: {sol.name} has no translated prompt", file=sys.stderr) |
| 82 | return 1 |
| 83 | work.append((p, sol.read_text())) |
| 84 | if not work: |
| 85 | print("stub_model: nothing to write", file=sys.stderr) |
| 86 | return 1 |
| 87 | out.mkdir(parents=True, exist_ok=True) |
| 88 | for p, hand in work: |
| 89 | record = { |
| 90 | "name": p["name"], |
| 91 | "language": p["language"], |
| 92 | "temperature": 0.0, |
| 93 | "top_p": 1.0, |
| 94 | "max_tokens": 0, |
| 95 | "prompt": p["prompt"], |
| 96 | "tests": p["tests"], |
| 97 | "completions": [completion(mode, p, hand)], |
| 98 | "stop_tokens": p["stop_tokens"], |
| 99 | "stub_mode": mode, |
| 100 | } |
| 101 | (out / f"{p['name']}.json").write_text(json.dumps(record, indent=2) + "\n") |
| 102 | print(f"stub_model: {mode}: {len(work)} problems -> {out}") |
| 103 | return 0 |
| 104 | |
| 105 | |
| 106 | if __name__ == "__main__": |
| 107 | sys.exit(main(sys.argv[1:])) |