Add grader module used by verification kit
Browse files"""Deterministic grading for binary-reasoning-v1 items. Pure functions, no framework deps."""
import json
import re
JUDGE_FAMILIES = {"func_summary", "arch_semantics_qa", "isa_semantics_qa", "pe_import_reasoning", "pe_structure_qa"}
def norm_symbol(s: str) -> str:
s = s.strip().lower()
s = re.sub(r"^[*&]+", "", s)
s = re.sub(r"\(.*\)$", "", s).strip()
s = re.sub(r"\.+$", "", s) # sentence-final dot captured by token regex
return s
def _answer_symbol(answer) -> str:
if isinstance(answer, dict):
return answer.get("symbol", "")
return str(answer)
def grade_func_id_exact(reply: str, answer) -> float:
sym = norm_symbol(_answer_symbol(answer))
if not sym:
return 0.0
accepted = {sym}
if isinstance(answer, dict):
accepted |= {norm_symbol(a) for a in answer.get("aliases", []) if a}
toks = [norm_symbol(t) for t in re.findall(r"[A-Za-z_][A-Za-z0-9_.]+", reply)]
return 1.0 if accepted & set(toks) else 0.0
def grade_func_id_mc(reply: str, answer: dict) -> float:
correct = answer.get("correct", "")
opts = answer.get("options", [])
r = reply.strip()
m = re.search(r"\b([A-D])\b", r[:80], re.IGNORECASE)
if m:
idx = ord(m.group(1).upper()) - 65
if 0 <= idx < len(opts):
return float(norm_symbol(opts[idx]) == norm_symbol(correct))
names = [norm_symbol(o) for o in re.findall(r"[A-Za-z_][A-Za-z0-9_.]{3,}", r.lower())]
return 1.0 if norm_symbol(correct) in names else 0.0
def grade_tcfp(reply: str, answer: dict) -> float:
strs = answer.get("toolchain_strings", [])
if not strs:
return 0.0
hits = sum(1 for s in strs if s.lower() in reply.lower())
return hits / len(strs)
def needs_judge(family: str, answer) -> bool:
if family in JUDGE_FAMILIES:
return True
if family == "func_id_exact":
sym = _answer_symbol(answer)
# descriptive/inferred answers (contain prose) cannot be string-matched
return (" " in sym.strip()) or ("inferred" in sym.lower())
return False
def grade_agentic(reply: str, answer) -> float:
flag = _answer_symbol(answer)
return 1.0 if flag and flag in reply else 0.0
def grade_crossarch(reply: str, answer) -> float:
if isinstance(answer, str):
try:
answer = json.loads(answer)
except Exception:
answer = {"same": "true" in answer.lower(),
"symbol": re.sub(r'.*?"symbol"\s*:\s*"([^"]+)".*', r"\1", answer) if '"symbol"' in answer else ""}
want = bool(answer.get("same"))
r = reply.lower()
neg = bool(re.search(r"\bnot\s+(the\s+)?same\b|\bdifferent\b|\bno\b", r))
pos = not neg and bool(re.search(r"\bsame\b|\byes\b|\btrue\b|match", r))
if not (neg or pos):
return 0.0 # abstention / no clear stance
if pos != want:
return 0.0
if want:
sym = norm_symbol(answer.get("symbol", ""))
toks = [norm_symbol(t) for t in re.findall(r"[A-Za-z_][A-Za-z0-9_.]+", reply)]
if sym not in toks:
return 0.0
return 1.0
def grade_library_ident(reply: str, answer) -> float:
ans = _answer_symbol(answer)
toks = [t for t in re.split(r"\s+", ans) if len(t) >= 2]
r = reply.lower()
return 1.0 if all(t.lower() in r for t in toks) else 0.0
def grade_item(family: str, reply: str, answer) -> float:
if needs_judge(family, answer):
return 0.0 # handled by rubric judge in taskset.py
if family == "library_ident":
return grade_library_ident(reply, answer)
if family == "crossarch_equivalence":
return grade_crossarch(reply, answer)
if family == "agentic_flag_hunt":
return grade_agentic(reply, answer)
if family == "func_id_exact":
return grade_func_id_exact(reply, answer)
if family == "func_id_mc":
return grade_func_id_mc(reply, answer)
if family == "toolchain_fp":
return grade_tcfp(reply, answer)
return 0.0
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Deterministic grading for binary-reasoning-v1 items. Pure functions, no framework deps."""
|
| 2 |
+
import json
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
JUDGE_FAMILIES = {"func_summary", "arch_semantics_qa", "isa_semantics_qa", "pe_import_reasoning", "pe_structure_qa"}
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def norm_symbol(s: str) -> str:
|
| 9 |
+
s = s.strip().lower()
|
| 10 |
+
s = re.sub(r"^[*&]+", "", s)
|
| 11 |
+
s = re.sub(r"\(.*\)$", "", s).strip()
|
| 12 |
+
s = re.sub(r"\.+$", "", s) # sentence-final dot captured by token regex
|
| 13 |
+
return s
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _answer_symbol(answer) -> str:
|
| 17 |
+
if isinstance(answer, dict):
|
| 18 |
+
return answer.get("symbol", "")
|
| 19 |
+
return str(answer)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def grade_func_id_exact(reply: str, answer) -> float:
|
| 23 |
+
sym = norm_symbol(_answer_symbol(answer))
|
| 24 |
+
if not sym:
|
| 25 |
+
return 0.0
|
| 26 |
+
accepted = {sym}
|
| 27 |
+
if isinstance(answer, dict):
|
| 28 |
+
accepted |= {norm_symbol(a) for a in answer.get("aliases", []) if a}
|
| 29 |
+
toks = [norm_symbol(t) for t in re.findall(r"[A-Za-z_][A-Za-z0-9_.]+", reply)]
|
| 30 |
+
return 1.0 if accepted & set(toks) else 0.0
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def grade_func_id_mc(reply: str, answer: dict) -> float:
|
| 34 |
+
correct = answer.get("correct", "")
|
| 35 |
+
opts = answer.get("options", [])
|
| 36 |
+
r = reply.strip()
|
| 37 |
+
m = re.search(r"\b([A-D])\b", r[:80], re.IGNORECASE)
|
| 38 |
+
if m:
|
| 39 |
+
idx = ord(m.group(1).upper()) - 65
|
| 40 |
+
if 0 <= idx < len(opts):
|
| 41 |
+
return float(norm_symbol(opts[idx]) == norm_symbol(correct))
|
| 42 |
+
names = [norm_symbol(o) for o in re.findall(r"[A-Za-z_][A-Za-z0-9_.]{3,}", r.lower())]
|
| 43 |
+
return 1.0 if norm_symbol(correct) in names else 0.0
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def grade_tcfp(reply: str, answer: dict) -> float:
|
| 47 |
+
strs = answer.get("toolchain_strings", [])
|
| 48 |
+
if not strs:
|
| 49 |
+
return 0.0
|
| 50 |
+
hits = sum(1 for s in strs if s.lower() in reply.lower())
|
| 51 |
+
return hits / len(strs)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def needs_judge(family: str, answer) -> bool:
|
| 55 |
+
if family in JUDGE_FAMILIES:
|
| 56 |
+
return True
|
| 57 |
+
if family == "func_id_exact":
|
| 58 |
+
sym = _answer_symbol(answer)
|
| 59 |
+
# descriptive/inferred answers (contain prose) cannot be string-matched
|
| 60 |
+
return (" " in sym.strip()) or ("inferred" in sym.lower())
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def grade_agentic(reply: str, answer) -> float:
|
| 65 |
+
flag = _answer_symbol(answer)
|
| 66 |
+
return 1.0 if flag and flag in reply else 0.0
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def grade_crossarch(reply: str, answer) -> float:
|
| 70 |
+
if isinstance(answer, str):
|
| 71 |
+
try:
|
| 72 |
+
answer = json.loads(answer)
|
| 73 |
+
except Exception:
|
| 74 |
+
answer = {"same": "true" in answer.lower(),
|
| 75 |
+
"symbol": re.sub(r'.*?"symbol"\s*:\s*"([^"]+)".*', r"\1", answer) if '"symbol"' in answer else ""}
|
| 76 |
+
want = bool(answer.get("same"))
|
| 77 |
+
r = reply.lower()
|
| 78 |
+
neg = bool(re.search(r"\bnot\s+(the\s+)?same\b|\bdifferent\b|\bno\b", r))
|
| 79 |
+
pos = not neg and bool(re.search(r"\bsame\b|\byes\b|\btrue\b|match", r))
|
| 80 |
+
if not (neg or pos):
|
| 81 |
+
return 0.0 # abstention / no clear stance
|
| 82 |
+
if pos != want:
|
| 83 |
+
return 0.0
|
| 84 |
+
if want:
|
| 85 |
+
sym = norm_symbol(answer.get("symbol", ""))
|
| 86 |
+
toks = [norm_symbol(t) for t in re.findall(r"[A-Za-z_][A-Za-z0-9_.]+", reply)]
|
| 87 |
+
if sym not in toks:
|
| 88 |
+
return 0.0
|
| 89 |
+
return 1.0
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def grade_library_ident(reply: str, answer) -> float:
|
| 93 |
+
ans = _answer_symbol(answer)
|
| 94 |
+
toks = [t for t in re.split(r"\s+", ans) if len(t) >= 2]
|
| 95 |
+
r = reply.lower()
|
| 96 |
+
return 1.0 if all(t.lower() in r for t in toks) else 0.0
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def grade_item(family: str, reply: str, answer) -> float:
|
| 100 |
+
if needs_judge(family, answer):
|
| 101 |
+
return 0.0 # handled by rubric judge in taskset.py
|
| 102 |
+
if family == "library_ident":
|
| 103 |
+
return grade_library_ident(reply, answer)
|
| 104 |
+
if family == "crossarch_equivalence":
|
| 105 |
+
return grade_crossarch(reply, answer)
|
| 106 |
+
if family == "agentic_flag_hunt":
|
| 107 |
+
return grade_agentic(reply, answer)
|
| 108 |
+
if family == "func_id_exact":
|
| 109 |
+
return grade_func_id_exact(reply, answer)
|
| 110 |
+
if family == "func_id_mc":
|
| 111 |
+
return grade_func_id_mc(reply, answer)
|
| 112 |
+
if family == "toolchain_fp":
|
| 113 |
+
return grade_tcfp(reply, answer)
|
| 114 |
+
return 0.0
|
| 115 |
+
Add grader module used by verification kit
|