Download code/score_samples.py from nzhao721/worked-examples-data: direct link, hf CLI and curl.
- Browser
- Download file 3.85 kB
-
https://huggingface.co/datasets/nzhao721/worked-examples-data/resolve/main/code/score_samples.py
- Command line
-
hf download hf://datasets/nzhao721/worked-examples-data/code/score_samples.py
-
curl -L -o score_samples.py https://huggingface.co/datasets/nzhao721/worked-examples-data/resolve/main/code/score_samples.py
3.85 kB
| """Score sampler output on a holdout with the first-answer, math-equivalence grader (gen/grader/math_equiv.py). | |
| A sample is correct when its first answer field is equivalent to the prompt's full gold (numbers compared exactly, | |
| a bare expression with math-verify, prose never). Reports Pass@N, PassRatio@N and maj@N (the most common answer among | |
| the samples is correct) overall and by source and type, plus the share of samples that state an answer, hit the token | |
| cap, and their mean length. Run it with the grader venv's Python so math-verify can be imported. The paired | |
| family-cluster analysis across arms is the released analyze_passn_eval.py, run through gen/grader/olmo_shim with the | |
| files sampler_to_preds.py writes. | |
| usage: score_samples.py SAMPLES_JSONL HOLDOUT_JSONL [--out PER_PROMPT_JSONL] [--mode direct|open] | |
| direct (default): prompt ended in "Answer:", a sample with no marker answers with its first line | |
| open: prompt was "Problem: q", a sample must state an answer after '####', 'Answer:' or 'The answer is' | |
| """ | |
| import json | |
| import sys | |
| from collections import Counter, defaultdict | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent / "grader")) | |
| import math_equiv # noqa: E402 | |
| def read_jsonl(path): | |
| with open(path, encoding="utf-8") as f: | |
| return [json.loads(line) for line in f if line.strip()] | |
| def write_jsonl(path, rows): | |
| Path(path).parent.mkdir(parents=True, exist_ok=True) | |
| with open(path, "w", encoding="utf-8", newline="\n") as f: | |
| for r in rows: | |
| f.write(json.dumps(r, ensure_ascii=False) + "\n") | |
| def answer_key(ans): | |
| c = math_equiv._clean(ans) | |
| n = math_equiv._as_number(c) | |
| return ("n", n) if n is not None else ("s", c.lower()) | |
| def main(samples, holdout, out=None, mode="direct"): | |
| strict = mode == "open" | |
| by = defaultdict(dict) | |
| for r in read_jsonl(samples): | |
| by[r["instance_id"]][r["sample"]] = r | |
| per, groups = [], defaultdict(list) | |
| for h in read_jsonl(holdout): | |
| got = by.get(h["instance_id"]) | |
| if not got: | |
| continue | |
| ks = sorted(got) | |
| answers = [math_equiv.first_answer(got[k]["text"], strict) for k in ks] | |
| flags = [math_equiv.equivalent(h["final_answer"], a) for a in answers] | |
| votes = Counter(answer_key(a) for a in answers if a) | |
| maj = False | |
| if votes: | |
| top = votes.most_common(1)[0][0] | |
| maj = flags[next(i for i, a in enumerate(answers) if a and answer_key(a) == top)] | |
| rec = {"instance_id": h["instance_id"], "family_id": h["family_id"], "type": h["type"], "n": len(flags), | |
| "any_correct": any(flags), "ratio": sum(flags) / len(flags), "maj": maj, | |
| "answered": sum(a is not None for a in answers) / len(answers), | |
| "capped": sum(got[k].get("stop") == "cap" for k in ks) / len(ks), | |
| "tokens": sum(got[k].get("new_tokens", 0) for k in ks) / len(ks)} | |
| per.append(rec) | |
| groups["all"].append(rec) | |
| groups["source " + h.get("source", h["type"].split("_")[0].lower())].append(rec) | |
| groups["type " + h["type"]].append(rec) | |
| print(f"{len(per)} prompts scored ({mode} mode)") | |
| for k in sorted(groups): | |
| g = groups[k] | |
| pct = lambda f: 100 * sum(r[f] for r in g) / len(g) # noqa: E731 | |
| print(f" {k:22s} n={len(g):6d} Pass@N {pct('any_correct'):6.2f}% PassRatio@N {pct('ratio'):6.2f}% " | |
| f"maj@N {pct('maj'):6.2f}% answered {pct('answered'):6.2f}% capped {pct('capped'):5.2f}% " | |
| f"mean tokens {sum(r['tokens'] for r in g) / len(g):6.1f}") | |
| if out: | |
| write_jsonl(out, per) | |
| if __name__ == "__main__": | |
| a = sys.argv[1:] | |
| main(a[0], a[1], a[a.index("--out") + 1] if "--out" in a else None, | |
| a[a.index("--mode") + 1] if "--mode" in a else "direct") | |