add eval/run_minif2f.py
Browse files- eval/run_minif2f.py +75 -0
eval/run_minif2f.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
import subprocess
|
| 6 |
+
import tempfile
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
import requests
|
| 10 |
+
from tqdm import tqdm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 14 |
+
LEAN_DIR = ROOT / "lean4"
|
| 15 |
+
SERVER = os.getenv("LEAN_LLM_SERVER", "http://localhost:8000/v1/completions")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def lean_statements() -> list[str]:
|
| 19 |
+
content = (LEAN_DIR / "MiniF2F.lean").read_text(encoding="utf-8")
|
| 20 |
+
matches = re.findall(r"theorem\s+.*?:=\s+by\s+sorry", content, flags=re.DOTALL)
|
| 21 |
+
return [m.strip() for m in matches]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def call_llm(stmt: str) -> list[dict]:
|
| 25 |
+
response = requests.post(
|
| 26 |
+
SERVER,
|
| 27 |
+
json={"prompt": stmt, "temperature": 0.0, "stop": ["<|user|>"]},
|
| 28 |
+
timeout=120,
|
| 29 |
+
)
|
| 30 |
+
response.raise_for_status()
|
| 31 |
+
text = response.json()["choices"][0]["text"].strip()
|
| 32 |
+
return json.loads(text) if text else []
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def verify_with_lean(steps: list[dict]) -> bool:
|
| 36 |
+
with tempfile.NamedTemporaryFile(mode="w", suffix=".lean", dir=LEAN_DIR, delete=False, encoding="utf-8") as f:
|
| 37 |
+
f.write("import MiniF2F\n\n")
|
| 38 |
+
f.write("namespace Scratch\n\n")
|
| 39 |
+
for s in steps:
|
| 40 |
+
name = s.get("name", "h_main")
|
| 41 |
+
statement = s.get("statement", "True")
|
| 42 |
+
tactic = s.get("tactic", "sorry")
|
| 43 |
+
f.write(f"theorem {name} : {statement} := by\n {tactic}\n\n")
|
| 44 |
+
f.write("end Scratch\n")
|
| 45 |
+
tmp = Path(f.name)
|
| 46 |
+
try:
|
| 47 |
+
result = subprocess.run(
|
| 48 |
+
["lake", "env", "lean", str(tmp.name)],
|
| 49 |
+
cwd=LEAN_DIR,
|
| 50 |
+
capture_output=True,
|
| 51 |
+
text=True,
|
| 52 |
+
timeout=60,
|
| 53 |
+
)
|
| 54 |
+
return result.returncode == 0
|
| 55 |
+
finally:
|
| 56 |
+
if tmp.exists():
|
| 57 |
+
tmp.unlink()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def main() -> None:
|
| 61 |
+
stmts = lean_statements()
|
| 62 |
+
print(f"testing {len(stmts)} theorems")
|
| 63 |
+
passed = 0
|
| 64 |
+
for stmt in tqdm(stmts):
|
| 65 |
+
try:
|
| 66 |
+
steps = call_llm(stmt)
|
| 67 |
+
if verify_with_lean(steps):
|
| 68 |
+
passed += 1
|
| 69 |
+
except Exception as exc:
|
| 70 |
+
print(f"error: {exc}")
|
| 71 |
+
print(f"passed: {passed}/{len(stmts)}")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
main()
|