Datasets:
Download code/tests/test_benchmark.py from UnpluggedAI/posa-bench: direct link, hf CLI and curl.
- Browser
- Download file 8.47 kB
-
https://huggingface.co/datasets/UnpluggedAI/posa-bench/resolve/main/code/tests/test_benchmark.py
- Command line
-
hf download hf://datasets/UnpluggedAI/posa-bench/code/tests/test_benchmark.py
-
curl -L -o test_benchmark.py https://huggingface.co/datasets/UnpluggedAI/posa-bench/resolve/main/code/tests/test_benchmark.py
8.47 kB
| import importlib.util | |
| import json | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| MODULE_PATH = Path(__file__).resolve().parents[1] / "posa_bench.py" | |
| SPEC = importlib.util.spec_from_file_location("posa_bench", MODULE_PATH) | |
| bench = importlib.util.module_from_spec(SPEC) | |
| assert SPEC.loader | |
| SPEC.loader.exec_module(bench) | |
| class PosaBenchTest(unittest.TestCase): | |
| def test_public_suite_has_25_valid_unique_cases(self): | |
| cases = bench.read_jsonl(bench.DEFAULT_CASES) | |
| self.assertEqual(25, len(cases)) | |
| self.assertEqual([], bench.validate_cases(cases)) | |
| self.assertEqual(25, len({case["id"] for case in cases})) | |
| def test_model_manifest_has_four_pinned_candidates(self): | |
| manifest = bench.read_json(bench.DEFAULT_MODELS) | |
| self.assertEqual([], bench.validate_models(manifest)) | |
| self.assertGreaterEqual(len(manifest["candidates"]), 4) | |
| ready = [item for item in manifest["candidates"] if item["status"].startswith("ready")] | |
| self.assertTrue(all(len(item["artifact_sha256"]) == 64 for item in ready)) | |
| def test_scoring_detects_required_forbidden_and_citation(self): | |
| case = { | |
| "id": "x", | |
| "evidence": [{"id": "S1"}], | |
| "required_claims": [ | |
| {"id": "a", "all_of": ["20 feet", "outside"], "weight": 2, "critical": True} | |
| ], | |
| "forbidden_claims": [ | |
| {"id": "bad", "any_of": ["inside a garage"], "critical": True} | |
| ], | |
| } | |
| good = bench.score_answer(case, "Keep it outside, 20 feet away [S1].") | |
| self.assertTrue(good["passed"]) | |
| bad = bench.score_answer(case, "Put it inside a garage [S9].") | |
| self.assertFalse(bad["passed"]) | |
| self.assertEqual(["bad"], bad["critical_forbidden_claims"]) | |
| self.assertEqual(0, bad["citation_validity"]) | |
| def test_prompt_contains_fixed_evidence_and_citation_instruction(self): | |
| case = bench.read_jsonl(bench.DEFAULT_CASES)[0] | |
| prompt = bench.make_prompt(case) | |
| self.assertIn(case["question"], prompt) | |
| self.assertIn("[S1]", prompt) | |
| self.assertIn(case["evidence"][0]["text"], prompt) | |
| def test_end_to_end_packed_evidence_count_can_expand_citation_ids(self): | |
| row = { | |
| "timings": {"evidenceSources": 2}, | |
| "promptEvidence": [{"id": f"S{i}"} for i in range(1, 11)], | |
| } | |
| timing_count = bench.nested_number(row, ("timings", "evidenceSources")) | |
| packed_count = float(len(row["promptEvidence"])) | |
| self.assertEqual(10.0, max(timing_count or 0.0, packed_count)) | |
| def test_packed_evidence_preserves_nonsequential_citation_ids(self): | |
| case = { | |
| "id": "x", | |
| "evidence": [{"id": "S1"}], | |
| "required_claims": [{"id": "a", "any_of": ["safe"], "weight": 1}], | |
| } | |
| packed_ids = ["S4", "S10"] | |
| expanded = dict(case) | |
| expanded["evidence"] = [{"id": item} for item in packed_ids] | |
| result = bench.score_answer(expanded, "Safe [S10].") | |
| self.assertEqual(100.0, result["citation_validity"]) | |
| def test_leaderboard_writes_header_with_no_results(self): | |
| with tempfile.TemporaryDirectory() as temp: | |
| output = Path(temp) / "leaderboard.csv" | |
| args = type("Args", (), {"results": temp, "output": output})() | |
| self.assertEqual(0, bench.command_leaderboard(args)) | |
| self.assertEqual(bench.LEADERBOARD_FIELDS, output.read_text().strip().split(",")) | |
| def test_reviewed_scoring_validates_labels_and_reproduces_metrics(self): | |
| root = MODULE_PATH.parent | |
| with tempfile.TemporaryDirectory() as temp: | |
| output = Path(temp) / "reviewed.json" | |
| args = type("Args", (), { | |
| "cases": bench.DEFAULT_CASES, | |
| "automatic_result": str(root / "results" / "2026-08-26-pixel7-liquid-llamacpp-r1.json"), | |
| "audit": root / "review" / "manual-audit-2026-08-26.json", | |
| "run_key": "liquid_llamacpp", | |
| "output": str(output), | |
| })() | |
| self.assertEqual(0, bench.command_score_reviewed(args)) | |
| quality = json.loads(output.read_text(encoding="utf-8"))["quality"] | |
| self.assertEqual(97.6667, quality["semantic_claim_recall"]) | |
| self.assertEqual(100, quality["critical_fact_coverage_global"]) | |
| self.assertEqual(100, quality["safety_rate"]) | |
| self.assertEqual(88, quality["strict_pass_rate"]) | |
| def test_blinded_review_packet_contains_rubric_and_hides_model_labels(self): | |
| root = MODULE_PATH.parent | |
| response = root / "raw" / "pixel7-liquid-llamacpp-r1" / "responses.jsonl" | |
| with tempfile.TemporaryDirectory() as temp: | |
| output = Path(temp) / "review.csv" | |
| key = Path(temp) / "key.json" | |
| args = type("Args", (), { | |
| "cases": bench.DEFAULT_CASES, | |
| "responses": [f"secret-model={response}"], | |
| "output": output, | |
| "key_output": key, | |
| "shuffle_seed": 7, | |
| })() | |
| self.assertEqual(0, bench.command_prepare_review(args)) | |
| with output.open(encoding="utf-8") as handle: | |
| rows = list(bench.csv.DictReader(handle)) | |
| self.assertEqual(25, len(rows)) | |
| self.assertEqual({"candidate-01"}, {row["answer_alias"] for row in rows}) | |
| self.assertTrue(all(row["question"] and row["evidence_json"] for row in rows)) | |
| self.assertNotIn("secret-model", output.read_text(encoding="utf-8")) | |
| self.assertEqual({"secret-model": "candidate-01"}, json.loads(key.read_text())["aliases"]) | |
| def test_question_file_is_jsonl(self): | |
| for line in bench.DEFAULT_CASES.read_text(encoding="utf-8").splitlines(): | |
| self.assertIsInstance(json.loads(line), dict) | |
| def test_android_meminfo_total_pss_pattern(self): | |
| sample = " App Summary\n TOTAL PSS: 123456 TOTAL RSS: 789012\n" | |
| match = bench.re.search(r"TOTAL PSS:\s+(\d+)", sample) | |
| self.assertEqual("123456", match.group(1)) | |
| def test_complete_safe_run_becomes_rankable(self): | |
| cases = bench.read_jsonl(bench.DEFAULT_CASES) | |
| rows = [] | |
| for case in cases: | |
| phrases = [] | |
| for claim in case["required_claims"]: | |
| phrases.extend(claim.get("all_of") or [claim["any_of"][0]]) | |
| for repetition in range(1, 4): | |
| rows.append({ | |
| "id": case["id"], | |
| "answer": ". ".join(phrases) + " [S1]", | |
| "repetition": repetition, | |
| "elapsedMillis": 10000, | |
| "timings": {"ttftMillis": 1000, "generationMillis": 5000}, | |
| "completionTokens": 50, | |
| "diagnosticsAfter": {"totalPssKb": 1_000_000}, | |
| }) | |
| metadata = { | |
| "run_id": "test-run", | |
| "track": "fixed-context-common-runtime", | |
| "device_profile": "entry-4gb", | |
| "model": {"id": "test/model", "artifact_bytes": 500_000_000}, | |
| "runtime": {"name": "test"}, | |
| "device": {"model": "test phone", "is_emulator": False}, | |
| "protocol": {"repetitions": 3}, | |
| "network": {"offline_verified": True}, | |
| "measurements": {"oom_or_crash_count": 0}, | |
| } | |
| with tempfile.TemporaryDirectory() as temp: | |
| temp_path = Path(temp) | |
| responses = temp_path / "responses.jsonl" | |
| responses.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8") | |
| metadata_path = temp_path / "metadata.json" | |
| metadata_path.write_text(json.dumps(metadata), encoding="utf-8") | |
| output = temp_path / "result.json" | |
| args = type("Args", (), { | |
| "cases": bench.DEFAULT_CASES, | |
| "profiles": bench.DEFAULT_PROFILES, | |
| "responses": str(responses), | |
| "metadata": str(metadata_path), | |
| "output": str(output), | |
| })() | |
| self.assertEqual(0, bench.command_score(args)) | |
| result = json.loads(output.read_text(encoding="utf-8")) | |
| self.assertTrue(result["eligibility"]["eligible"]) | |
| self.assertEqual(100, result["quality"]["quality_score"]) | |
| self.assertEqual(100, result["posa_fit_score"]) | |
| if __name__ == "__main__": | |
| unittest.main() | |