File size: 10,215 Bytes
61a25fe | 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 | from __future__ import annotations
import argparse
import json
import math
import re
import time
from collections import Counter, defaultdict
from pathlib import Path
from typing import Callable
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
ROOT = Path(__file__).resolve().parent
DATA_PATH = ROOT / "benchmark-data.json"
CROSS_DATA_PATH = DATA_PATH
DEFAULT_OUTPUT = ROOT / "my-results-monolingual.json"
SEMANTIC_MODELS = {
"multilingual-e5-small": {
"repo_id": "intfloat/multilingual-e5-small",
"query_prefix": "query: ",
"passage_prefix": "passage: ",
"languages": 94,
"license": "MIT",
"role": "efficient multilingual retrieval model",
},
"multilingual-minilm": {
"repo_id": "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
"query_prefix": "",
"passage_prefix": "",
"languages": 50,
"license": "Apache-2.0",
"role": "compact multilingual sentence similarity baseline",
},
"labse": {
"repo_id": "sentence-transformers/LaBSE",
"query_prefix": "",
"passage_prefix": "",
"languages": 110,
"license": "Apache-2.0",
"role": "high-coverage multilingual embedding reference",
},
}
def load_rows(path: Path = DATA_PATH) -> list[dict]:
payload = json.loads(path.read_text(encoding="utf-8"))
split = "cross-language" if getattr(load_rows, "use_cross", False) else "monolingual"
return payload["splits"][split]["rows"]
def language_of(row: dict) -> str:
return row.get("language") or row.get("query_language") or "unknown"
def pair_of(row: dict) -> str | None:
q = row.get("query_language")
p = row.get("passage_language")
return f"{q}->{p}" if q and p else None
def candidate_passages(row: dict) -> list[str]:
return [row["positive_passage"], row["hard_negative"], row["easy_negative"]]
def _result_record(row: dict, scores: list[float], elapsed_ms: float) -> dict:
order = np.argsort(np.asarray(scores))[::-1].tolist()
rank = order.index(0) + 1
return {
"id": row["id"],
"language": language_of(row),
"language_pair": pair_of(row),
"domain": row["domain"],
"positive_rank": rank,
"scores": [float(x) for x in scores],
"latency_ms": float(elapsed_ms),
}
def evaluate_scorer(rows: list[dict], scorer: Callable[[str, list[str]], list[float]]) -> list[dict]:
out = []
for row in rows:
passages = candidate_passages(row)
t0 = time.perf_counter()
scores = scorer(row["query"], passages)
elapsed_ms = (time.perf_counter() - t0) * 1000
out.append(_result_record(row, scores, elapsed_ms))
return out
def char_tfidf_scorer(query: str, passages: list[str]) -> list[float]:
vec = TfidfVectorizer(analyzer="char_wb", ngram_range=(3, 5), sublinear_tf=True)
matrix = vec.fit_transform(passages + [query])
return cosine_similarity(matrix[-1], matrix[:-1]).ravel().tolist()
def word_tfidf_scorer(query: str, passages: list[str]) -> list[float]:
vec = TfidfVectorizer(analyzer="word", ngram_range=(1, 2), sublinear_tf=True, lowercase=True)
matrix = vec.fit_transform(passages + [query])
return cosine_similarity(matrix[-1], matrix[:-1]).ravel().tolist()
def hybrid_tfidf_scorer(query: str, passages: list[str]) -> list[float]:
char_scores = np.asarray(char_tfidf_scorer(query, passages))
word_scores = np.asarray(word_tfidf_scorer(query, passages))
return (0.6 * char_scores + 0.4 * word_scores).tolist()
def tokenize(text: str) -> list[str]:
return re.findall(r"\b\w+\b", text.lower(), flags=re.UNICODE)
def bm25_scorer(query: str, passages: list[str], k1: float = 1.5, b: float = 0.75) -> list[float]:
docs = [tokenize(p) for p in passages]
q = tokenize(query)
n = len(docs)
avgdl = sum(len(d) for d in docs) / max(n, 1)
df = Counter()
for d in docs:
for term in set(d):
df[term] += 1
scores = []
for doc in docs:
tf = Counter(doc)
dl = len(doc)
score = 0.0
for term in q:
if term not in tf:
continue
idf = math.log(1 + (n - df[term] + 0.5) / (df[term] + 0.5))
denom = tf[term] + k1 * (1 - b + b * dl / max(avgdl, 1e-9))
score += idf * (tf[term] * (k1 + 1)) / denom
scores.append(score)
return scores
def summarize(name: str, kind: str, results: list[dict], *, metadata: dict | None = None, load_seconds: float | None = None) -> dict:
n = len(results)
by_language = {}
by_domain = {}
by_pair = {}
for field, target in [("language", by_language), ("domain", by_domain), ("language_pair", by_pair)]:
for value in sorted({r[field] for r in results}):
part = [r for r in results if r[field] == value]
target[value] = {
"n": len(part),
"accuracy_at_1": sum(r["positive_rank"] == 1 for r in part) / len(part),
"mrr": sum(1 / r["positive_rank"] for r in part) / len(part),
}
return {
"name": name,
"kind": kind,
"status": "executed",
"n": n,
"accuracy_at_1": sum(r["positive_rank"] == 1 for r in results) / n,
"mrr": sum(1 / r["positive_rank"] for r in results) / n,
"avg_latency_ms": sum(r["latency_ms"] for r in results) / n,
"load_seconds": load_seconds,
"by_language": by_language,
"by_domain": by_domain,
"by_language_pair": {k:v for k,v in by_pair.items() if k is not None},
"metadata": metadata or {},
}
def run_lexical(rows: list[dict]) -> list[dict]:
scorers = {
"char-tfidf": char_tfidf_scorer,
"word-tfidf": word_tfidf_scorer,
"hybrid-tfidf": hybrid_tfidf_scorer,
"bm25": bm25_scorer,
}
return [summarize(name, "lexical", evaluate_scorer(rows, scorer)) for name, scorer in scorers.items()]
def run_semantic(rows: list[dict], key: str) -> dict:
spec = SEMANTIC_MODELS[key]
try:
from sentence_transformers import SentenceTransformer
except ImportError as exc:
raise RuntimeError("sentence-transformers is required for semantic benchmarks") from exc
t0 = time.perf_counter()
model = SentenceTransformer(spec["repo_id"])
load_seconds = time.perf_counter() - t0
results = []
for row in rows:
passages = candidate_passages(row)
q = spec["query_prefix"] + row["query"]
docs = [spec["passage_prefix"] + p for p in passages]
t1 = time.perf_counter()
q_emb = model.encode([q], normalize_embeddings=True)
p_emb = model.encode(docs, normalize_embeddings=True)
scores = (q_emb @ p_emb.T)[0].tolist()
elapsed_ms = (time.perf_counter() - t1) * 1000
results.append(_result_record(row, scores, elapsed_ms))
return summarize(key, "semantic", results, metadata=spec, load_seconds=load_seconds)
def configured_semantic_placeholders() -> list[dict]:
return [
{
"name": key,
"kind": "semantic",
"status": "configured-not-executed",
"n": 0,
"accuracy_at_1": None,
"mrr": None,
"avg_latency_ms": None,
"load_seconds": None,
"by_language": {},
"by_domain": {},
"metadata": spec,
}
for key, spec in SEMANTIC_MODELS.items()
]
def main() -> None:
parser = argparse.ArgumentParser(description="Benchmark multilingual retrieval methods on Research Retrieval Eval Mini.")
parser.add_argument("--models", nargs="*", default=["lexical"], help="lexical, all, or semantic keys")
parser.add_argument("--split", choices=["monolingual", "cross-language"], default="monolingual")
parser.add_argument("--output", type=Path, default=None)
args = parser.parse_args()
data_path = DATA_PATH if args.split == "monolingual" else CROSS_DATA_PATH
load_rows.use_cross = args.split == "cross-language"
rows = load_rows(data_path)
output = args.output or (DEFAULT_OUTPUT if args.split == "monolingual" else ROOT / "my-results-cross-language.json")
requested = set(args.models)
models = run_lexical(rows) if ("lexical" in requested or "all" in requested) else []
semantic_keys = list(SEMANTIC_MODELS) if "all" in requested else [x for x in requested if x in SEMANTIC_MODELS]
if not semantic_keys and ("lexical" in requested):
models.extend(configured_semantic_placeholders())
else:
for key in semantic_keys:
try:
models.append(run_semantic(rows, key))
except Exception as exc:
spec = SEMANTIC_MODELS[key]
models.append({
"name": key,
"kind": "semantic",
"status": "error",
"error": str(exc),
"n": 0,
"accuracy_at_1": None,
"mrr": None,
"avg_latency_ms": None,
"load_seconds": None,
"by_language": {},
"by_domain": {},
"metadata": spec,
})
payload = {
"benchmark": "Research Retrieval Eval Mini",
"split": args.split,
"dataset_rows": len(rows),
"languages": sorted({language_of(r) for r in rows}),
"language_pairs": sorted({pair_of(r) for r in rows if pair_of(r)}),
"domains": sorted({r["domain"] for r in rows}),
"metrics": ["accuracy_at_1", "mrr", "avg_latency_ms"],
"models": models,
"interpretation_note": "This is a small diagnostic benchmark. Results should not be generalized to broader retrieval quality without larger external evaluation.",
}
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(json.dumps(payload, indent=2, ensure_ascii=False), encoding="utf-8")
print(json.dumps(payload, indent=2, ensure_ascii=False))
if __name__ == "__main__":
main()
|