-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_budget_sweep.py
More file actions
232 lines (192 loc) · 8.12 KB
/
Copy pathbenchmark_budget_sweep.py
File metadata and controls
232 lines (192 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""Token-budget sweep: bm25 vs engram_bm25 on LongMemEval (no full_context).
Runs benchmark_longmem.py at TOKEN_BUDGET ∈ {500, 1000, 1500, 2000} and writes
benchmark_budget_sweep_results.md. Reuses benchmark_longmem_raw.json for budget=2000.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
from datetime import datetime, timezone
from typing import Dict, List, Optional
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
SWEEP_BUDGETS = [500, 1000, 1500, 2000]
SWEEP_RETRIEVERS = ["bm25", "engram_bm25"]
MEMORY_TYPES = ["knowledge-update", "multi-session", "temporal-reasoning"]
DEFAULT_RAW_2000 = os.path.join(_SCRIPT_DIR, "benchmark_longmem_raw.json")
OUT_MD = os.path.join(_SCRIPT_DIR, "benchmark_budget_sweep_results.md")
def raw_path_for_budget(budget: int) -> str:
if budget == 2000:
return DEFAULT_RAW_2000
return os.path.join(_SCRIPT_DIR, f"benchmark_longmem_raw_b{budget}.json")
def load_payload(path: str) -> Optional[dict]:
if not os.path.isfile(path):
return None
with open(path, encoding="utf-8") as f:
return json.load(f)
def summarize_budget(payload: dict, budget: int) -> dict:
"""Extract overall + memory-type accuracies for bm25 / engram_bm25."""
summary = payload.get("summary", {})
overall_rows = {r["retriever"]: r for r in summary.get("overall", [])}
by_type = {r["question_type"]: r for r in summary.get("by_type", [])}
row = {
"budget": budget,
"bm25_acc": overall_rows.get("bm25", {}).get("accuracy"),
"bm25_correct": overall_rows.get("bm25", {}).get("correct"),
"bm25_valid": overall_rows.get("bm25", {}).get("valid"),
"engram_acc": overall_rows.get("engram_bm25", {}).get("accuracy"),
"engram_correct": overall_rows.get("engram_bm25", {}).get("correct"),
"engram_valid": overall_rows.get("engram_bm25", {}).get("valid"),
"engram_avg_ctx": overall_rows.get("engram_bm25", {}).get("avg_ctx_tokens"),
"bm25_avg_ctx": overall_rows.get("bm25", {}).get("avg_ctx_tokens"),
"by_type": {},
}
for qtype in MEMORY_TYPES:
tr = by_type.get(qtype, {})
row["by_type"][qtype] = {
"bm25": tr.get("bm25"),
"engram_bm25": tr.get("engram_bm25"),
}
return row
def run_budget(budget: int) -> None:
env = os.environ.copy()
env["TOKEN_BUDGET"] = str(budget)
env["LONGMEM_RETRIEVERS"] = ",".join(SWEEP_RETRIEVERS)
print(f"\n=== Running budget={budget} → {raw_path_for_budget(budget)} ===")
subprocess.run(
[sys.executable, os.path.join(_SCRIPT_DIR, "benchmark_longmem.py")],
cwd=_SCRIPT_DIR,
env=env,
check=True,
)
def count_api_calls(payload: dict) -> int:
"""Answer + judge per retriever row computed in this payload."""
n = 0
for rec in payload.get("results", []):
for name in SWEEP_RETRIEVERS:
r = rec.get("retrievers", {}).get(name, {})
if r.get("correct") is not None and not r.get("error"):
n += 2 # answer + judge
return n
def build_honest_read(rows: List[dict]) -> str:
parts: List[str] = []
wins = []
for r in rows:
b = r["budget"]
ba = r.get("bm25_acc")
ea = r.get("engram_acc")
if ba is None or ea is None:
continue
if ea > ba + 1e-9:
wins.append(b)
elif abs(ea - ba) < 1e-9:
parts.append(f"At {b} tokens, bm25 and engram_bm25 tie ({ba:.0%}).")
if wins:
parts.insert(
0,
f"engram_bm25 beats bm25 at budget(s): {', '.join(str(w) for w in wins)}.",
)
else:
parts.insert(
0,
"engram_bm25 does **not** beat bm25 at any tested budget — they tie or bm25 leads.",
)
r2000 = next((r for r in rows if r["budget"] == 2000), None)
if r2000:
ba = r2000.get("bm25_acc", 0)
ea = r2000.get("engram_acc", 0)
parts.append(f"At 2000 tokens (baseline): both {ba:.0%} ({r2000.get('bm25_correct')}/{r2000.get('bm25_valid')}).")
for qtype in MEMORY_TYPES:
type_bits = []
for r in rows:
bt = r.get("by_type", {}).get(qtype, {})
ba = bt.get("bm25")
ea = bt.get("engram_bm25")
if ba is None or ea is None:
continue
if ea > ba + 1e-9:
type_bits.append(f"budget {r['budget']}: engram {ea:.0%} > bm25 {ba:.0%}")
elif abs(ea - ba) < 1e-9:
type_bits.append(f"budget {r['budget']}: tie {ea:.0%}")
else:
type_bits.append(f"budget {r['budget']}: bm25 {ba:.0%} > engram {ea:.0%}")
if type_bits:
parts.append(f"**{qtype}:** " + "; ".join(type_bits) + ".")
return " ".join(parts)
def write_report(rows: List[dict], total_calls: int, skip_run: bool) -> None:
md = f"""# LongMemEval Token-Budget Sweep — bm25 vs engram_bm25
**Date:** {datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")}
**Dataset:** longmemeval_s_cleaned.json (48 questions, seed=42, 8/type × 6 types)
**Retrievers:** `bm25`, `engram_bm25` only (no `full_context`)
**Model:** openai/gpt-4o-mini (answer + judge)
**Hypothesis tested:** Engram concept-activation prior keeps accuracy higher than plain BM25 at low token budgets.
## Accuracy vs budget
| Budget (tokens) | bm25 acc | engram_bm25 acc | avg ctx tokens (engram_bm25) |
|----------------:|---------:|----------------:|-----------------------------:|
"""
for r in rows:
ba = r.get("bm25_acc")
ea = r.get("engram_acc")
avg = r.get("engram_avg_ctx")
ba_s = f"{ba:.1%} ({r.get('bm25_correct')}/{r.get('bm25_valid')})" if ba is not None else "—"
ea_s = f"{ea:.1%} ({r.get('engram_correct')}/{r.get('engram_valid')})" if ea is not None else "—"
avg_s = f"{avg:,.0f}" if avg is not None else "—"
md += f"| {r['budget']} | {ba_s} | {ea_s} | {avg_s} |\n"
md += f"""
## Memory-heavy categories (per budget)
| Budget | knowledge-update | multi-session | temporal-reasoning |
|-------:|-----------------:|--------------:|-------------------:|
"""
for r in rows:
cells = []
for qtype in MEMORY_TYPES:
bt = r.get("by_type", {}).get(qtype, {})
ba = bt.get("bm25")
ea = bt.get("engram_bm25")
if ba is None or ea is None:
cells.append("—")
elif abs((ea or 0) - (ba or 0)) < 1e-9:
cells.append(f"{ea:.0%} (tie)")
elif (ea or 0) > (ba or 0):
cells.append(f"E {ea:.0%} > B {ba:.0%}")
else:
cells.append(f"B {ba:.0%} > E {ea:.0%}")
md += f"| {r['budget']} | {' | '.join(cells)} |\n"
honest = build_honest_read(rows)
md += f"""
## Honest read
{honest}
## Run notes
- Budget 2000 rows reused from `{os.path.basename(DEFAULT_RAW_2000)}` (existing Round 3 cache).
- Other budgets write separate raw files: `benchmark_longmem_raw_b{{budget}}.json`.
- Approx LLM calls this sweep (answer+judge, bm25+engram only): **{total_calls}** (~${total_calls * 0.00078:.2f} est. at ~2000 ctx).
- skip_run={skip_run}
"""
with open(OUT_MD, "w", encoding="utf-8") as f:
f.write(md)
print(f"Wrote {OUT_MD}")
def main() -> None:
skip_run = "--report-only" in sys.argv
rows: List[dict] = []
total_calls = 0
for budget in SWEEP_BUDGETS:
path = raw_path_for_budget(budget)
if budget == 2000:
payload = load_payload(path)
if payload is None:
raise FileNotFoundError(f"Missing 2000-budget cache: {path}")
rows.append(summarize_budget(payload, budget))
continue
if not skip_run:
run_budget(budget)
payload = load_payload(path)
if payload is None:
print(f"WARNING: no results at {path} after run")
continue
rows.append(summarize_budget(payload, budget))
total_calls += count_api_calls(payload)
rows.sort(key=lambda r: r["budget"])
write_report(rows, total_calls, skip_run)
if __name__ == "__main__":
main()