S3RL4 commited on
Commit
8f63ef2
·
verified ·
1 Parent(s): d6e4a89

Add gold-answer sanity check

Browse files

#!/usr/bin/env python3
"""Gold-answer sanity check: built-in gold replies should score 1 on deterministic tasks."""
import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "environment" / "src"))
from binary_reasoning_v1.grading import _answer_symbol, grade_item, needs_judge, norm_symbol # noqa: E402


def resolve_path():
if len(sys.argv) > 1:
return Path(sys.argv[1])
for p in (ROOT / "sample_free.jsonl", ROOT / "environment/src/binary_reasoning_v1/items.jsonl"):
if p.exists():
return p
raise SystemExit("no sample JSONL found; pass one as argv[1]")


def load_rows(path):
rows = []
for line in path.read_text(encoding="utf-8").splitlines():
if line.strip():
rows.append(json.loads(line))
return rows


def parse_json_answer(ans):
if isinstance(ans, dict):
return ans
if isinstance(ans, str):
try:
obj = json.loads(ans)
if isinstance(obj, dict):
return obj
except Exception:
pass
return {}


def gold_reply(row):
fam = row.get("family", "")
ans = row.get("answer")
if fam == "func_id_mc" and isinstance(ans, dict):
opts = ans.get("options", [])
correct = norm_symbol(ans.get("correct", ""))
for i, opt in enumerate(opts[:4]):
if norm_symbol(opt) == correct:
return "ABCD"[i]
return str(ans.get("correct", ""))
if fam == "toolchain_fp" and isinstance(ans, dict):
return "\n".join(str(s) for s in ans.get("toolchain_strings", []))
if fam == "crossarch_equivalence":
obj = parse_json_answer(ans)
if bool(obj.get("same")):
return f"Same function: {obj.get('symbol', '')}"
return "Not the same."
sym = _answer_symbol(ans)
return str(sym or ans or "")


def main():
path = resolve_path()
rows = load_rows(path)
passed = failed = skipped = 0
fails = []
for row in rows:
fam = row.get("family", "")
ans = row.get("answer")
if needs_judge(fam, ans):
skipped += 1
continue
score = grade_item(fam, gold_reply(row), ans)
if score == 1.0:
passed += 1
else:
failed += 1
fails.append((row.get("id"), fam, score))
print(f"file={path}")
print(f"summary: gold pass={passed}/{passed + failed} fail={failed} skipped_judge={skipped}")
for f in fails[:10]:
print("FAIL", *f)
return 0 if failed == 0 else 1


if __name__ == "__main__":
raise SystemExit(main())

verification_kit/gold.py/verification_kit/gold.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Gold-answer sanity check: built-in gold replies should score 1 on deterministic tasks."""
3
+ import json
4
+ import sys
5
+ from pathlib import Path
6
+
7
+ ROOT = Path(__file__).resolve().parents[1]
8
+ sys.path.insert(0, str(ROOT / "environment" / "src"))
9
+ from binary_reasoning_v1.grading import _answer_symbol, grade_item, needs_judge, norm_symbol # noqa: E402
10
+
11
+
12
+ def resolve_path():
13
+ if len(sys.argv) > 1:
14
+ return Path(sys.argv[1])
15
+ for p in (ROOT / "sample_free.jsonl", ROOT / "environment/src/binary_reasoning_v1/items.jsonl"):
16
+ if p.exists():
17
+ return p
18
+ raise SystemExit("no sample JSONL found; pass one as argv[1]")
19
+
20
+
21
+ def load_rows(path):
22
+ rows = []
23
+ for line in path.read_text(encoding="utf-8").splitlines():
24
+ if line.strip():
25
+ rows.append(json.loads(line))
26
+ return rows
27
+
28
+
29
+ def parse_json_answer(ans):
30
+ if isinstance(ans, dict):
31
+ return ans
32
+ if isinstance(ans, str):
33
+ try:
34
+ obj = json.loads(ans)
35
+ if isinstance(obj, dict):
36
+ return obj
37
+ except Exception:
38
+ pass
39
+ return {}
40
+
41
+
42
+ def gold_reply(row):
43
+ fam = row.get("family", "")
44
+ ans = row.get("answer")
45
+ if fam == "func_id_mc" and isinstance(ans, dict):
46
+ opts = ans.get("options", [])
47
+ correct = norm_symbol(ans.get("correct", ""))
48
+ for i, opt in enumerate(opts[:4]):
49
+ if norm_symbol(opt) == correct:
50
+ return "ABCD"[i]
51
+ return str(ans.get("correct", ""))
52
+ if fam == "toolchain_fp" and isinstance(ans, dict):
53
+ return "\n".join(str(s) for s in ans.get("toolchain_strings", []))
54
+ if fam == "crossarch_equivalence":
55
+ obj = parse_json_answer(ans)
56
+ if bool(obj.get("same")):
57
+ return f"Same function: {obj.get('symbol', '')}"
58
+ return "Not the same."
59
+ sym = _answer_symbol(ans)
60
+ return str(sym or ans or "")
61
+
62
+
63
+ def main():
64
+ path = resolve_path()
65
+ rows = load_rows(path)
66
+ passed = failed = skipped = 0
67
+ fails = []
68
+ for row in rows:
69
+ fam = row.get("family", "")
70
+ ans = row.get("answer")
71
+ if needs_judge(fam, ans):
72
+ skipped += 1
73
+ continue
74
+ score = grade_item(fam, gold_reply(row), ans)
75
+ if score == 1.0:
76
+ passed += 1
77
+ else:
78
+ failed += 1
79
+ fails.append((row.get("id"), fam, score))
80
+ print(f"file={path}")
81
+ print(f"summary: gold pass={passed}/{passed + failed} fail={failed} skipped_judge={skipped}")
82
+ for f in fails[:10]:
83
+ print("FAIL", *f)
84
+ return 0 if failed == 0 else 1
85
+
86
+
87
+ if __name__ == "__main__":
88
+ raise SystemExit(main())
89
+ Add gold-answer sanity check