File size: 11,103 Bytes
6c5f29f | 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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | from __future__ import annotations
import argparse
import json
import random
import statistics
from pathlib import Path
from typing import Iterable
DEFAULT_METHODS = [
"dense_budgeted_bsc",
"dense_rag_e5",
"heuristic_bsc",
"ld_agent_proxy",
"memorybank_proxy",
"dense_budgeted_replay",
"replay_only_router",
"fifo_replay",
]
METHOD_LABELS = {
"dense_budgeted_bsc": "OracleMem writer + dense retrieval",
"dense_rag_e5": "Full raw-store dense retrieval",
"heuristic_bsc": "OracleMem writer + lexical retrieval",
"ld_agent_proxy": "LD-Agent proxy",
"memorybank_proxy": "MemoryBank proxy",
"dense_budgeted_replay": "Budgeted raw replay + dense retrieval",
"replay_only_router": "Budgeted raw replay router",
"fifo_replay": "FIFO raw replay",
"uniform_replay": "Uniform raw replay",
}
def _csv(value: str) -> list[str]:
return [part.strip() for part in value.split(",") if part.strip()]
def _recall_at(row: dict, k: int) -> float:
gold = set(row.get("gold_session_ids", []))
pred = set(row.get("predicted_session_ids", [])[:k])
if not gold:
return 0.0
return len(gold & pred) / len(gold)
def _recall(row: dict) -> float:
return _recall_at(row, 5)
def _rr_at(row: dict, k: int) -> float:
gold = set(row.get("gold_session_ids", []))
if not gold:
return 0.0
for rank, session_id in enumerate(row.get("predicted_session_ids", [])[:k], start=1):
if session_id in gold:
return 1.0 / rank
return 0.0
def _rr(row: dict) -> float:
return _rr_at(row, 5)
def _mean(values: Iterable[float]) -> float:
values = list(values)
if not values:
return 0.0
return float(sum(values) / len(values))
def _ci(values: list[float], *, rng: random.Random, n_bootstrap: int) -> list[float]:
if not values:
return [0.0, 0.0]
if len(values) == 1 or n_bootstrap <= 0:
value = float(values[0])
return [value, value]
means = []
size = len(values)
for _ in range(n_bootstrap):
sample = [values[rng.randrange(size)] for _ in range(size)]
means.append(sum(sample) / size)
means.sort()
lo = means[int(0.025 * (len(means) - 1))]
hi = means[int(0.975 * (len(means) - 1))]
return [float(lo), float(hi)]
def summarize_method(rows: list[dict], focus_types: set[str], *, rng: random.Random, n_bootstrap: int) -> dict:
recalls = [_recall(row) for row in rows]
rrs = [_rr(row) for row in rows]
focus_rows = [row for row in rows if row.get("question_type") in focus_types]
focus_recalls = [_recall(row) for row in focus_rows]
focus_rrs = [_rr(row) for row in focus_rows]
focus_recall_at_1 = [_recall_at(row, 1) for row in focus_rows]
focus_recall_at_3 = [_recall_at(row, 3) for row in focus_rows]
by_type: dict[str, list[dict]] = {}
for row in rows:
by_type.setdefault(row.get("question_type", "unknown"), []).append(row)
per_type = {}
for question_type, type_rows in sorted(by_type.items()):
type_recalls = [_recall(row) for row in type_rows]
type_rrs = [_rr(row) for row in type_rows]
per_type[question_type] = {
"n": len(type_rows),
"recall_at_5": _mean(type_recalls),
"mrr_at_5": _mean(type_rrs),
"recall_at_5_ci95": _ci(type_recalls, rng=rng, n_bootstrap=n_bootstrap),
}
return {
"n": len(rows),
"overall_recall_at_5": _mean(recalls),
"overall_mrr_at_5": _mean(rrs),
"focus_n": len(focus_rows),
"focus_recall_at_5": _mean(focus_recalls),
"focus_recall_at_1": _mean(focus_recall_at_1),
"focus_recall_at_3": _mean(focus_recall_at_3),
"focus_mrr_at_5": _mean(focus_rrs),
"focus_recall_at_5_ci95": _ci(focus_recalls, rng=rng, n_bootstrap=n_bootstrap),
"per_type": per_type,
}
def build_summary(retrieval_rows: dict, methods: list[str], focus_types: set[str], n_bootstrap: int, seed: int) -> dict:
rng = random.Random(seed)
metrics = {}
missing_methods = []
for method in methods:
rows = retrieval_rows.get(method)
if rows is None:
missing_methods.append(method)
continue
metrics[method] = summarize_method(rows, focus_types, rng=rng, n_bootstrap=n_bootstrap)
baseline = metrics.get("dense_rag_e5")
raw_baseline = metrics.get("dense_budgeted_replay")
for method, row in metrics.items():
if baseline is not None:
row["delta_focus_vs_full_dense_rag"] = row["focus_recall_at_5"] - baseline["focus_recall_at_5"]
if raw_baseline is not None:
row["delta_focus_vs_budgeted_raw_dense"] = row["focus_recall_at_5"] - raw_baseline["focus_recall_at_5"]
return {
"source": "LongMemEval-S frozen retrieval artifact",
"metric_basis": "gold answer_session_ids retrieval only; no answer generation and no exact OPT",
"focus_types": sorted(focus_types),
"methods": methods,
"missing_methods": missing_methods,
"bootstrap_samples": n_bootstrap,
"metrics": metrics,
}
def write_markdown(output_dir: Path, summary: dict) -> None:
metrics = summary["metrics"]
focus_types = ", ".join(f"`{item}`" for item in summary["focus_types"])
lines = [
"# LongMemEval-S Focus Report",
"",
f"- Source: {summary['source']}",
f"- Focus types: {focus_types}",
f"- Metric basis: {summary['metric_basis']}",
"- Scope: retrieval-only. This report does not measure abstention, answer accuracy, stale answers, or ratio to OPT.",
"",
"## Focus Retrieval",
"",
"| Method | Overall R@5 | Focus R@5 | Focus 95% CI | Focus MRR@5 | Delta vs full dense RAG | Delta vs budgeted raw dense |",
"|---|---:|---:|---:|---:|---:|---:|",
]
for method in summary["methods"]:
if method not in metrics:
continue
row = metrics[method]
label = METHOD_LABELS.get(method, method)
lo, hi = row["focus_recall_at_5_ci95"]
lines.append(
"| "
+ label
+ f" | {row['overall_recall_at_5']:.4f}"
+ f" | {row['focus_recall_at_5']:.4f}"
+ f" | [{lo:.4f}, {hi:.4f}]"
+ f" | {row['focus_mrr_at_5']:.4f}"
+ f" | {row.get('delta_focus_vs_full_dense_rag', 0.0):+.4f}"
+ f" | {row.get('delta_focus_vs_budgeted_raw_dense', 0.0):+.4f}"
+ " |"
)
lines.extend(
[
"",
"## Focus Retrieval K-Sweep",
"",
"This artifact contains top-5 retrieval ids, so the sweep reports R@1/R@3/R@5 and MRR@5. R@10 requires regenerating retrieval rows with `topk=10`.",
"",
"| Method | Focus R@1 | Focus R@3 | Focus R@5 | Focus MRR@5 |",
"|---|---:|---:|---:|---:|",
]
)
for method in summary["methods"]:
if method not in metrics:
continue
row = metrics[method]
label = METHOD_LABELS.get(method, method)
lines.append(
f"| {label} | {row['focus_recall_at_1']:.4f} | {row['focus_recall_at_3']:.4f} | "
f"{row['focus_recall_at_5']:.4f} | {row['focus_mrr_at_5']:.4f} |"
)
lines.extend(
[
"",
"## Per-Type Retrieval",
"",
"| Method | Knowledge-update R@5 | Temporal-reasoning R@5 | Multi-session R@5 |",
"|---|---:|---:|---:|",
]
)
for method in summary["methods"]:
if method not in metrics:
continue
row = metrics[method]
per_type = row["per_type"]
label = METHOD_LABELS.get(method, method)
ku = per_type.get("knowledge-update", {}).get("recall_at_5", 0.0)
tr = per_type.get("temporal-reasoning", {}).get("recall_at_5", 0.0)
ms = per_type.get("multi-session", {}).get("recall_at_5", 0.0)
lines.append(f"| {label} | {ku:.4f} | {tr:.4f} | {ms:.4f} |")
lines.extend(
[
"",
"## Interpretation",
"",
"- The strongest budgeted memory writer in this artifact is `dense_budgeted_bsc` (reported as OracleMem writer + dense retrieval), which exceeds full raw-store dense retrieval on the focused update/temporal slice.",
"- The comparison is retrieval-only and uses LongMemEval-S gold answer-session ids; it should be cited as external transfer evidence, not as an oracle-ratio result.",
"- LongMemEval-S in this local pipeline does not expose an abstention category, so abstention and stale-answer claims still require a separate reader/evaluation run.",
]
)
if summary["missing_methods"]:
lines.extend(["", f"Missing methods: `{', '.join(summary['missing_methods'])}`"])
output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / "REPORT.md").write_text("\n".join(lines) + "\n", encoding="utf-8")
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--summary-json", type=Path, default=Path("llm_memory_validation/competitor_run_v2/summary.json"))
parser.add_argument("--retrieval-rows-json", type=Path, default=Path("llm_memory_validation/competitor_run_v2/retrieval_rows.json"))
parser.add_argument("--output-dir", type=Path, default=Path("llm_memory_validation/longmemeval_focus_report"))
parser.add_argument("--focus-types", type=_csv, default=_csv("knowledge-update,temporal-reasoning"))
parser.add_argument("--methods", type=_csv, default=DEFAULT_METHODS)
parser.add_argument("--bootstrap", type=int, default=2000)
parser.add_argument("--seed", type=int, default=0)
args = parser.parse_args()
if not args.retrieval_rows_json.exists():
raise FileNotFoundError(args.retrieval_rows_json)
retrieval_rows = json.loads(args.retrieval_rows_json.read_text(encoding="utf-8"))
summary = build_summary(
retrieval_rows=retrieval_rows,
methods=args.methods,
focus_types=set(args.focus_types),
n_bootstrap=args.bootstrap,
seed=args.seed,
)
if args.summary_json.exists():
source_summary = json.loads(args.summary_json.read_text(encoding="utf-8"))
summary["retriever_model"] = source_summary.get("retriever_model")
summary["topk"] = source_summary.get("topk")
summary["reported_baselines"] = source_summary.get("reported_baselines", {})
args.output_dir.mkdir(parents=True, exist_ok=True)
(args.output_dir / "summary.json").write_text(json.dumps(summary, indent=2), encoding="utf-8")
write_markdown(args.output_dir, summary)
print(json.dumps(summary, indent=2))
if __name__ == "__main__":
main()
|