trentdoney commited on
Commit
d9003b7
·
verified ·
1 Parent(s): 7ff2cfd

Upload src/evaluator.py

Browse files
Files changed (1) hide show
  1. src/evaluator.py +115 -0
src/evaluator.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """BrainCore Memory Benchmark — main evaluation harness."""
2
+
3
+ import argparse
4
+ import importlib
5
+ import json
6
+ import time
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ from metrics import (
11
+ aggregate,
12
+ exact_match,
13
+ semantic_placeholder_score,
14
+ temporal_order_score,
15
+ contradiction_resolution_score,
16
+ latency_ms,
17
+ )
18
+
19
+
20
+ def load_dataset(path: str) -> list[dict]:
21
+ sessions = []
22
+ with open(path, "r", encoding="utf-8") as f:
23
+ for line in f:
24
+ line = line.strip()
25
+ if line:
26
+ sessions.append(json.loads(line))
27
+ return sessions
28
+
29
+
30
+ def evaluate_session(session: dict, adapter: Any, top_k: int = 1) -> list[dict]:
31
+ """Ingest memories then run every query. Returns per-query result dicts."""
32
+ adapter.ingest(session.get("memories", []))
33
+ storage = adapter.storage_bytes()
34
+
35
+ results = []
36
+ for q in session.get("queries", []):
37
+ t0 = time.perf_counter()
38
+ retrieved = adapter.retrieve(q["query_text"], top_k=top_k)
39
+ t1 = time.perf_counter()
40
+
41
+ pred_text = " ".join(m.get("text", "") for m in retrieved)
42
+ ref_text = q.get("expected_answer", "")
43
+
44
+ em = exact_match(pred_text, ref_text)
45
+ sem = semantic_placeholder_score(pred_text, ref_text)
46
+
47
+ qtype = q.get("query_type", "retrieval")
48
+ to_score = temporal_order_score(retrieved, q.get("required_memory_ids", []))
49
+
50
+ # Contradiction: if the dataset marks the latest revised memory, use it.
51
+ latest_id = q.get("latest_memory_id") if qtype == "contradiction" else None
52
+ cr_score = contradiction_resolution_score(retrieved, latest_id)
53
+
54
+ # For pure retrieval/temporal without explicit contradiction, cr_score is neutral.
55
+ if qtype != "contradiction":
56
+ cr_score = 1.0
57
+
58
+ results.append({
59
+ "session_id": session["session_id"],
60
+ "query_id": q["query_id"],
61
+ "query_type": qtype,
62
+ "exact_match": em,
63
+ "semantic_placeholder_score": sem,
64
+ "temporal_order_score": to_score,
65
+ "contradiction_resolution_score": cr_score,
66
+ "latency_ms": latency_ms(t0, t1),
67
+ "storage_bytes": storage,
68
+ "pred": pred_text,
69
+ "ref": ref_text,
70
+ })
71
+ return results
72
+
73
+
74
+ def main():
75
+ parser = argparse.ArgumentParser(description="BrainCore Memory Benchmark Harness")
76
+ parser.add_argument("--adapter", required=True, help="Dotted path to adapter module (must expose `build() -> MemoryAdapter`)")
77
+ parser.add_argument("--dataset", required=True, help="Path to JSONL dataset")
78
+ parser.add_argument("--output", required=True, help="Path to write JSON results")
79
+ parser.add_argument("--top_k", type=int, default=1, help="Number of memories to retrieve")
80
+ args = parser.parse_args()
81
+
82
+ # Dynamically import adapter module.
83
+ module = importlib.import_module(args.adapter)
84
+ adapter = module.build()
85
+
86
+ sessions = load_dataset(args.dataset)
87
+ all_results = []
88
+ for session in sessions:
89
+ all_results.extend(evaluate_session(session, adapter, top_k=args.top_k))
90
+
91
+ summary = aggregate(all_results)
92
+
93
+ payload = {
94
+ "config": {
95
+ "adapter": args.adapter,
96
+ "dataset": args.dataset,
97
+ "top_k": args.top_k,
98
+ },
99
+ "summary": summary,
100
+ "per_query": all_results,
101
+ }
102
+
103
+ Path(args.output).parent.mkdir(parents=True, exist_ok=True)
104
+ with open(args.output, "w", encoding="utf-8") as f:
105
+ json.dump(payload, f, indent=2, ensure_ascii=False)
106
+
107
+ print("BrainCore Memory Benchmark — Summary")
108
+ print("=" * 40)
109
+ for k, v in summary.items():
110
+ print(f" {k:40s}: {v}")
111
+ print(f"\nFull results written to {args.output}")
112
+
113
+
114
+ if __name__ == "__main__":
115
+ main()