Upload 12 files
Browse files- .pytest_cache/.gitignore +2 -0
- .pytest_cache/CACHEDIR.TAG +4 -0
- .pytest_cache/README.md +8 -0
- .pytest_cache/v/cache/nodeids +6 -0
- baselines/README.md +13 -0
- baselines/msmarco_scale/BASELINE_COMPARISON.json +96 -0
- baselines/msmarco_scale/README.md +11 -0
- baselines/msmarco_scale/run_bm25_test_streaming_effectiveness.py +145 -0
- baselines/run_local_lexical.py +49 -0
- configs/msmarco_fullscale_lock.json +25 -0
- configs/rag_top10_current.json +47 -0
- data/README.md +20 -0
.pytest_cache/.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by pytest automatically.
|
| 2 |
+
*
|
.pytest_cache/CACHEDIR.TAG
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Signature: 8a477f597d28d172789f06886806bc55
|
| 2 |
+
# This file is a cache directory tag created by pytest.
|
| 3 |
+
# For information about cache directory tags, see:
|
| 4 |
+
# https://bford.info/cachedir/spec.html
|
.pytest_cache/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pytest cache directory #
|
| 2 |
+
|
| 3 |
+
This directory contains data from the pytest's cache plugin,
|
| 4 |
+
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.
|
| 5 |
+
|
| 6 |
+
**Do not** commit this to version control.
|
| 7 |
+
|
| 8 |
+
See [the docs](https://docs.pytest.org/en/stable/how-to/cache.html) for more information.
|
.pytest_cache/v/cache/nodeids
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
"tests/test_rag_utils.py::test_minmax_hi_is_bounded_and_monotone",
|
| 3 |
+
"tests/test_rag_utils.py::test_rag_top10_ranker_runs_on_toy_index",
|
| 4 |
+
"tests/test_rag_utils.py::test_zscore_constant_is_zero",
|
| 5 |
+
"tests/test_toy.py::test_toy_build_and_search"
|
| 6 |
+
]
|
baselines/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Baselines
|
| 2 |
+
|
| 3 |
+
The result tables keep all baseline families used during development: TF-IDF, BM25, MiniLM/FAISS, Contriever, SPLADE++, BGE, ColBERT, and the historical version of OURS.
|
| 4 |
+
|
| 5 |
+
`run_local_lexical.py` reproduces local TF-IDF and an effectiveness-oriented BM25 sanity check. Its BM25 latency is **not** a production inverted-index benchmark and is labelled accordingly.
|
| 6 |
+
|
| 7 |
+
For neural/vector baselines, use the authors' released models and index libraries. Their effectiveness values in `results/beir/rag_top10_pool_sweep_scifact_treccovid.json` are retained as the comparison ledger from the experiment campaign. Before a publication-quality speed claim, rerun every baseline on the same CPU and report both:
|
| 8 |
+
|
| 9 |
+
- query encoder time;
|
| 10 |
+
- index/search time;
|
| 11 |
+
- end-to-end query time.
|
| 12 |
+
|
| 13 |
+
Do not compare an end-to-end sparse retrieval number with ANN-only latency that excludes query embedding inference.
|
baselines/msmarco_scale/BASELINE_COMPARISON.json
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"dataset": "TREC Deep Learning 2019 Passage / MS MARCO v1 passage corpus",
|
| 3 |
+
"corpus_size": 8841823,
|
| 4 |
+
"queries": 43,
|
| 5 |
+
"ours": {
|
| 6 |
+
"source": "local frozen implementation; TRAIN-fold selected parameters; no post-test tuning",
|
| 7 |
+
"ndcg10_trec_eval_linear_gain": 0.35912864519537124,
|
| 8 |
+
"mrr10_binary_rel_ge_2": 0.5196382428940569,
|
| 9 |
+
"p10_binary_rel_ge_2": 0.28837209302325584,
|
| 10 |
+
"r100_binary_rel_ge_2": 0.32448471642019855,
|
| 11 |
+
"r1000_binary_rel_ge_2": 0.5097617112453317,
|
| 12 |
+
"route_recall_macro_binary_rel_ge_2": 0.7011717028045845,
|
| 13 |
+
"pool2000_recall_macro_binary_rel_ge_2": 0.5414967376369709,
|
| 14 |
+
"median_ms_local_warm": 86.52526099831448,
|
| 15 |
+
"p95_ms_local_warm": 163.08966510223397,
|
| 16 |
+
"avg_routed_docs": 86049.18604651163,
|
| 17 |
+
"deployable_index_bytes": 2629229405
|
| 18 |
+
},
|
| 19 |
+
"local_same_tokenizer_bm25_sanity": {
|
| 20 |
+
"note": "Effectiveness-only streaming full-corpus scan; NOT an indexed query-latency measurement. Lowercase regex tokenizer, no stemming, k1=.9 b=.4.",
|
| 21 |
+
"ndcg10_trec_eval_linear_gain": 0.4845300219597866,
|
| 22 |
+
"mrr10_binary_rel_ge_2": 0.6200535252860835,
|
| 23 |
+
"p10_binary_rel_ge_2": 0.38372093023255816,
|
| 24 |
+
"r100_binary_rel_ge_2": 0.465414572538343,
|
| 25 |
+
"full_corpus_parallel_scan_seconds": 321.1634156703949
|
| 26 |
+
},
|
| 27 |
+
"published_same_test": [
|
| 28 |
+
{
|
| 29 |
+
"method": "BM25 default",
|
| 30 |
+
"index": "Lucene/Anserini sparse",
|
| 31 |
+
"ndcg10": 0.5058,
|
| 32 |
+
"r1000": 0.7501,
|
| 33 |
+
"p10": 0.4116,
|
| 34 |
+
"rr_nist": 0.7036
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"method": "BM25 + RM3",
|
| 38 |
+
"index": "Lucene/Anserini sparse",
|
| 39 |
+
"ndcg10": 0.518,
|
| 40 |
+
"r1000": 0.7998,
|
| 41 |
+
"p10": 0.4372,
|
| 42 |
+
"rr_nist": 0.6683
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"method": "BM25 + PRF default",
|
| 46 |
+
"index": "traditional sparse",
|
| 47 |
+
"ndcg10": 0.5372
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"method": "BM25 + PRF tuned",
|
| 51 |
+
"index": "traditional sparse",
|
| 52 |
+
"ndcg10": 0.5536
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"method": "ANCE",
|
| 56 |
+
"index": "FAISS Flat",
|
| 57 |
+
"ndcg10": 0.6452,
|
| 58 |
+
"r1000": 0.7554
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"method": "BGE-base-en-v1.5",
|
| 62 |
+
"index": "FAISS Flat float32",
|
| 63 |
+
"ndcg10": 0.7016,
|
| 64 |
+
"r1000": 0.8427
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"method": "BGE-base-en-v1.5",
|
| 68 |
+
"index": "Lucene HNSW",
|
| 69 |
+
"ndcg10": 0.7016,
|
| 70 |
+
"r1000": 0.8441
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"method": "BGE-base-en-v1.5",
|
| 74 |
+
"index": "Lucene HNSW int8",
|
| 75 |
+
"ndcg10": 0.7017,
|
| 76 |
+
"r1000": 0.8436
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"method": "TCT-ColBERT-V2-HN+",
|
| 80 |
+
"index": "FAISS Flat",
|
| 81 |
+
"ndcg10": 0.7204,
|
| 82 |
+
"r1000": 0.8261
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"method": "OPQIVFPQ SiamBERT",
|
| 86 |
+
"index": "FAISS OPQ-IVF-PQ, 256 B/doc",
|
| 87 |
+
"ndcg10": 0.613,
|
| 88 |
+
"r1000": 0.675,
|
| 89 |
+
"note": "different encoder; not apples-to-apples with BGE"
|
| 90 |
+
}
|
| 91 |
+
],
|
| 92 |
+
"metric_notes": {
|
| 93 |
+
"ndcg": "trec_eval ndcg_cut uses raw qrel grade as gain; official TREC-DL19 comparison uses graded judgments.",
|
| 94 |
+
"binary": "NIST passage grade 1 is Related but NOT relevant; binary metrics use grades >=2."
|
| 95 |
+
}
|
| 96 |
+
}
|
baselines/msmarco_scale/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Baseline notes — full MS MARCO / TREC-DL19
|
| 2 |
+
|
| 3 |
+
`run_bm25_test_streaming_effectiveness.py` is a local effectiveness sanity check over all 8,841,823 passages. It is deliberately **not** reported as an indexed BM25 latency measurement because it scans the corpus.
|
| 4 |
+
|
| 5 |
+
`BASELINE_COMPARISON.json` records the locally measured frozen-system result and published same-test reference rows. Published rows should be cited to NIST/Pyserini/the original dense-retrieval papers in manuscripts.
|
| 6 |
+
|
| 7 |
+
TREC-DL19 passage judgments require two evaluation conventions:
|
| 8 |
+
- `nDCG@10`: retain graded qrel values (trec_eval `ndcg_cut` uses raw relevance values as gains);
|
| 9 |
+
- binary MRR/P/Recall/Hit: grade 1 is only *Related*, not relevant, therefore use relevance >= 2.
|
| 10 |
+
|
| 11 |
+
The local frozen-system evaluator in `../scale_experiments/msmarco_test_baseline_compare.py` returns top 1000 and applies the correct binary threshold.
|
baselines/msmarco_scale/run_bm25_test_streaming_effectiveness.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import gzip,pickle,json,re,csv,math,time,heapq,glob,os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from collections import Counter,defaultdict
|
| 5 |
+
from multiprocessing import Pool, get_context
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
ROOT=Path('/mnt/data'); WORK=ROOT/'msmarco_scale_work'; OUT=WORK/'baselines'; OUT.mkdir(exist_ok=True)
|
| 9 |
+
N=8_841_823; TOPK=1000
|
| 10 |
+
# Same token boundary as frozen system / sklearn token_pattern for query terms.
|
| 11 |
+
TOKEN_RE=re.compile(r'(?u)\b\w\w+\b')
|
| 12 |
+
|
| 13 |
+
# Load qids and query texts
|
| 14 |
+
qrels_rows=[]; qids=set()
|
| 15 |
+
with open(ROOT/'test.tsv',newline='') as f:
|
| 16 |
+
r=csv.DictReader(f,delimiter='\t')
|
| 17 |
+
for x in r:
|
| 18 |
+
q=str(x['query-id']); d=int(x['corpus-id']); rel=float(x['score']); qids.add(q); qrels_rows.append((q,d,rel))
|
| 19 |
+
qids=sorted(qids,key=lambda x:int(x)); qindex={q:i for i,q in enumerate(qids)}
|
| 20 |
+
texts={}
|
| 21 |
+
with open(ROOT/'queries.jsonl') as f:
|
| 22 |
+
for line in f:
|
| 23 |
+
o=json.loads(line); q=str(o['_id'])
|
| 24 |
+
if q in qids: texts[q]=o['text']
|
| 25 |
+
assert len(texts)==len(qids)==43
|
| 26 |
+
qtoks={q:TOKEN_RE.findall(texts[q].lower()) for q in qids}
|
| 27 |
+
allterms=sorted(set(t for z in qtoks.values() for t in z))
|
| 28 |
+
|
| 29 |
+
# full-corpus known df from the exact 50k vocabulary build
|
| 30 |
+
with gzip.open(WORK/'final_vocab_50k.pkl.gz','rb') as f:z=pickle.load(f)
|
| 31 |
+
terms50=z['terms'].tolist(); df50=np.asarray(z['df']); term2df={t:int(df50[i]) for i,t in enumerate(terms50)}
|
| 32 |
+
oov=sorted(t for t in allterms if t not in term2df)
|
| 33 |
+
print('queries',len(qids),'query terms',len(allterms),'oov',oov,flush=True)
|
| 34 |
+
|
| 35 |
+
# locate 36 shards in order
|
| 36 |
+
def shard_path(i):
|
| 37 |
+
hits=list(ROOT.glob(f'corpus_{i:04d}.jsonl*.gz'))
|
| 38 |
+
if i==35 and len(hits)==0: hits=list((ROOT/'restored').glob(f'corpus_{i:04d}.jsonl*.gz'))
|
| 39 |
+
assert len(hits)==1,(i,hits)
|
| 40 |
+
return str(hits[0])
|
| 41 |
+
SHARDS=[shard_path(i) for i in range(36)]
|
| 42 |
+
|
| 43 |
+
# Exact OOV df with one cheap corpus scan, only 10 rare strings.
|
| 44 |
+
OOV_PAT=re.compile(r'(?u)\b(?:'+'|'.join(re.escape(t) for t in sorted(oov,key=len,reverse=True))+r')\b') if oov else None
|
| 45 |
+
|
| 46 |
+
def count_oov_one(args):
|
| 47 |
+
sid,path=args; c=Counter(); n=0; t0=time.time()
|
| 48 |
+
with gzip.open(path,'rt',encoding='utf-8') as f:
|
| 49 |
+
for line in f:
|
| 50 |
+
o=json.loads(line); tx=((o.get('title') or '')+' '+(o.get('text') or '')).lower(); n+=1
|
| 51 |
+
if OOV_PAT:
|
| 52 |
+
m=set(OOV_PAT.findall(tx))
|
| 53 |
+
for t in m:c[t]+=1
|
| 54 |
+
return sid,n,dict(c),time.time()-t0
|
| 55 |
+
|
| 56 |
+
if oov:
|
| 57 |
+
t0=time.time();
|
| 58 |
+
with get_context('fork').Pool(processes=8) as p:
|
| 59 |
+
rr=p.map(count_oov_one,list(enumerate(SHARDS)))
|
| 60 |
+
oo=Counter();
|
| 61 |
+
for sid,n,c,sec in rr: oo.update(c)
|
| 62 |
+
for t in oov: term2df[t]=int(oo[t])
|
| 63 |
+
print('oov df',dict(oo),'sec',time.time()-t0,flush=True)
|
| 64 |
+
|
| 65 |
+
# Robertson BM25 idf; base parameters match common Anserini MS MARCO defaults.
|
| 66 |
+
def make_idf():
|
| 67 |
+
return {t:math.log(1.0+(N-term2df[t]+0.5)/(term2df[t]+0.5)) for t in allterms}
|
| 68 |
+
idf=make_idf()
|
| 69 |
+
# Query term multiplicity mapping term -> [(query index,multiplicity)]
|
| 70 |
+
t2q=defaultdict(list)
|
| 71 |
+
for q in qids:
|
| 72 |
+
c=Counter(qtoks[q])
|
| 73 |
+
for t,m in c.items(): t2q[t].append((qindex[q],m))
|
| 74 |
+
# compiled union matcher exact same word boundaries, avoids tokenizing irrelevant words
|
| 75 |
+
PAT=re.compile(r'(?u)\b(?:'+'|'.join(re.escape(t) for t in sorted(allterms,key=len,reverse=True))+r')\b')
|
| 76 |
+
avgdl=float(json.load(open(WORK/'full_index/meta.json'))['avg_doc_length'])
|
| 77 |
+
# existing exact tokenizer document lengths
|
| 78 |
+
DL_PATH=str(WORK/'full_index/doc_lengths.u16')
|
| 79 |
+
|
| 80 |
+
# Each shard returns top1000 per q. Global top1000 is exactly merge of shard top1000.
|
| 81 |
+
def score_shard(args):
|
| 82 |
+
sid,path,k1,b=args
|
| 83 |
+
dlarr=np.memmap(DL_PATH,dtype=np.uint16,mode='r',shape=(N,))
|
| 84 |
+
heaps=[[] for _ in qids]
|
| 85 |
+
n=0; matched=0; t0=time.time()
|
| 86 |
+
with gzip.open(path,'rt',encoding='utf-8') as f:
|
| 87 |
+
for line in f:
|
| 88 |
+
o=json.loads(line); d=int(o['_id']); tx=((o.get('title') or '')+' '+(o.get('text') or '')).lower(); n+=1
|
| 89 |
+
mm=PAT.findall(tx)
|
| 90 |
+
if not mm: continue
|
| 91 |
+
matched+=1; tf=Counter(mm); norm=k1*(1.0-b+b*(float(dlarr[d])/avgdl))
|
| 92 |
+
qs={}
|
| 93 |
+
for term,freq in tf.items():
|
| 94 |
+
w=idf[term]*((freq*(k1+1.0))/(freq+norm))
|
| 95 |
+
for qi,qm in t2q[term]: qs[qi]=qs.get(qi,0.0)+w*qm
|
| 96 |
+
for qi,sc in qs.items():
|
| 97 |
+
h=heaps[qi]; item=(float(sc),-d) # lower docid wins exact tie
|
| 98 |
+
if len(h)<TOPK: heapq.heappush(h,item)
|
| 99 |
+
elif item>h[0]: heapq.heapreplace(h,item)
|
| 100 |
+
return sid,heaps,n,matched,time.time()-t0
|
| 101 |
+
|
| 102 |
+
def retrieve(k1,b,label):
|
| 103 |
+
t0=time.time()
|
| 104 |
+
with get_context('fork').Pool(processes=8) as p:
|
| 105 |
+
rr=p.map(score_shard,[(i,SHARDS[i],k1,b) for i in range(36)])
|
| 106 |
+
globalh=[[] for _ in qids]
|
| 107 |
+
for sid,heaps,n,matched,sec in rr:
|
| 108 |
+
for qi,h in enumerate(heaps):
|
| 109 |
+
gh=globalh[qi]
|
| 110 |
+
for item in h:
|
| 111 |
+
if len(gh)<TOPK: heapq.heappush(gh,item)
|
| 112 |
+
elif item>gh[0]: heapq.heapreplace(gh,item)
|
| 113 |
+
run={}
|
| 114 |
+
for qi,q in enumerate(qids):
|
| 115 |
+
arr=sorted(globalh[qi],reverse=True); run[q]=[-negd for sc,negd in arr]
|
| 116 |
+
sec=time.time()-t0
|
| 117 |
+
print(label,'retrieval scan sec',sec,flush=True)
|
| 118 |
+
return run,sec
|
| 119 |
+
|
| 120 |
+
# TREC-compliant eval: passage level 1 = related/not relevant for binary metrics. nDCG remains graded.
|
| 121 |
+
qrels=defaultdict(dict)
|
| 122 |
+
for q,d,r in qrels_rows:qrels[q][d]=r
|
| 123 |
+
|
| 124 |
+
def evaluate(run):
|
| 125 |
+
vals=defaultdict(list)
|
| 126 |
+
for q in qids:
|
| 127 |
+
qr=qrels[q]; rank=run.get(q,[])
|
| 128 |
+
binary={d for d,r in qr.items() if r>=2.0}; den=max(1,len(binary))
|
| 129 |
+
h10=sum(d in binary for d in rank[:10]); h100=sum(d in binary for d in rank[:100])
|
| 130 |
+
vals['P@10'].append(h10/10); vals['R@10'].append(h10/den); vals['R@100'].append(h100/den); vals['Hit@10'].append(float(h10>0)); vals['Hit@100'].append(float(h100>0))
|
| 131 |
+
rr=0.0
|
| 132 |
+
for i,d in enumerate(rank[:10],1):
|
| 133 |
+
if d in binary: rr=1.0/i;break
|
| 134 |
+
vals['MRR@10'].append(rr)
|
| 135 |
+
obs=[qr.get(d,0.0) for d in rank[:10]]; ideal=sorted(qr.values(),reverse=True)[:10]
|
| 136 |
+
dcg=sum(r/math.log2(i+2) for i,r in enumerate(obs)); idcg=sum(r/math.log2(i+2) for i,r in enumerate(ideal)); vals['nDCG@10'].append(dcg/idcg if idcg else 0.0)
|
| 137 |
+
return {k:float(np.mean(v)) for k,v in vals.items()} | {'n_queries':len(qids),'n_binary_relevant':sum(r>=2 for _,_,r in qrels_rows)}
|
| 138 |
+
|
| 139 |
+
if __name__=='__main__':
|
| 140 |
+
# Common MS MARCO/Anserini-like BM25 and a conventional default variant for sensitivity.
|
| 141 |
+
out={'tokenizer':'lowercase regex (?u)\\b\\w\\w+\\b; no stemming','N':N,'avgdl':avgdl,'oov_df':{t:term2df[t] for t in oov}}
|
| 142 |
+
for k1,b,label in [(0.9,0.4,'bm25_k1_0.9_b_0.4'),(1.2,0.75,'bm25_k1_1.2_b_0.75')]:
|
| 143 |
+
run,sec=retrieve(k1,b,label); met=evaluate(run); out[label]={'k1':k1,'b':b,'metrics':met,'full_corpus_parallel_scan_seconds':sec,'run_top1000':run}; print(label,met,flush=True)
|
| 144 |
+
json.dump(out,open(OUT/'bm25_local_test.json','w'),indent=2)
|
| 145 |
+
print('SAVED',OUT/'bm25_local_test.json',flush=True)
|
baselines/run_local_lexical.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
"""Local lexical references for sanity checking, not the headline speed baseline.
|
| 3 |
+
|
| 4 |
+
The BM25 implementation here is an effectiveness implementation over the loaded
|
| 5 |
+
corpus. Do not present its latency as a production inverted-index latency.
|
| 6 |
+
"""
|
| 7 |
+
import argparse,json,time,math,re
|
| 8 |
+
from collections import Counter
|
| 9 |
+
import numpy as np
|
| 10 |
+
from scipy import sparse
|
| 11 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 12 |
+
from geomretrieval import load_beir_zip,load_beir_directory,evaluate_run
|
| 13 |
+
|
| 14 |
+
def load_dataset(path,split):
|
| 15 |
+
return load_beir_zip(path,split) if str(path).lower().endswith('.zip') else load_beir_directory(path,split)
|
| 16 |
+
|
| 17 |
+
def topk(x,k):
|
| 18 |
+
k=min(k,len(x)); ii=np.argpartition(x,-k)[-k:] if k<len(x) else np.arange(len(x)); return ii[np.argsort(x[ii])[::-1]]
|
| 19 |
+
|
| 20 |
+
def tfidf(ds):
|
| 21 |
+
v=TfidfVectorizer(dtype=np.float32,norm='l2'); X=v.fit_transform(ds.corpus_texts); run={}; times=[]
|
| 22 |
+
qids=[q for q in ds.qrels if q in ds.queries]
|
| 23 |
+
for qid in qids:
|
| 24 |
+
t=time.perf_counter(); q=v.transform([ds.queries[qid]]); s=(X@q.T).toarray().ravel(); ii=topk(s,100); times.append((time.perf_counter()-t)*1000);run[qid]=[ds.corpus_ids[i] for i in ii]
|
| 25 |
+
m=evaluate_run(run,ds.qrels,ks=(10,100),ndcg_k=10,mrr_k=10,exp_gain=False);m['median_ms']=float(np.median(times));m['p95_ms']=float(np.percentile(times,95));return m
|
| 26 |
+
|
| 27 |
+
def bm25_effectiveness(ds,k1=.9,b=.4):
|
| 28 |
+
tok=lambda s: re.findall(r'(?u)\\b\\w\\w+\\b',s.lower())
|
| 29 |
+
docs=[tok(x) for x in ds.corpus_texts]; N=len(docs); lens=np.array([len(x) for x in docs],np.float32);avg=max(1,float(lens.mean()));df=Counter()
|
| 30 |
+
for x in docs: df.update(set(x))
|
| 31 |
+
postings={}
|
| 32 |
+
for di,x in enumerate(docs):
|
| 33 |
+
c=Counter(x)
|
| 34 |
+
for t,tf in c.items(): postings.setdefault(t,[]).append((di,tf))
|
| 35 |
+
run={};times=[]
|
| 36 |
+
for qid in [q for q in ds.qrels if q in ds.queries]:
|
| 37 |
+
t0=time.perf_counter();score=np.zeros(N,np.float32)
|
| 38 |
+
for term in tok(ds.queries[qid]):
|
| 39 |
+
plist=postings.get(term,()); n=df.get(term,0);idf=math.log(1+(N-n+.5)/(n+.5))
|
| 40 |
+
for d,tf in plist:
|
| 41 |
+
den=tf+k1*(1-b+b*lens[d]/avg);score[d]+=idf*(tf*(k1+1))/den
|
| 42 |
+
ii=topk(score,100);times.append((time.perf_counter()-t0)*1000);run[qid]=[ds.corpus_ids[i] for i in ii]
|
| 43 |
+
m=evaluate_run(run,ds.qrels,ks=(10,100),ndcg_k=10,mrr_k=10,exp_gain=False);m['median_ms_effectiveness_impl']=float(np.median(times));m['p95_ms_effectiveness_impl']=float(np.percentile(times,95));return m
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
p=argparse.ArgumentParser();p.add_argument('dataset');p.add_argument('--split',default='test');p.add_argument('--output',default=None);a=p.parse_args();ds=load_dataset(a.dataset,a.split)
|
| 47 |
+
out={'tfidf':tfidf(ds),'bm25':bm25_effectiveness(ds)};print(json.dumps(out,indent=2));
|
| 48 |
+
if a.output: open(a.output,'w').write(json.dumps(out,indent=2))
|
| 49 |
+
if __name__=='__main__':main()
|
configs/msmarco_fullscale_lock.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"protocol": "Final architecture lock after five disjoint 1000-query TRAIN folds. A parameter change is accepted only when it improves nDCG@10 on all five folds; DEV and TEST are not used for this final lock.",
|
| 3 |
+
"frozen_params": {
|
| 4 |
+
"preselection_eta": 1.0,
|
| 5 |
+
"preselection_idf_power": 1.0,
|
| 6 |
+
"P": 2000,
|
| 7 |
+
"gamma_tail": 0.25,
|
| 8 |
+
"lambda_M": 0.125,
|
| 9 |
+
"final_idf_power": 2.0,
|
| 10 |
+
"final_length_b": 0.1,
|
| 11 |
+
"coordination_alpha": 0.25,
|
| 12 |
+
"lambda_lex": 4.0,
|
| 13 |
+
"lambda_sem": 0.3,
|
| 14 |
+
"rare_topk": 3,
|
| 15 |
+
"rare_coverage_weight": 1.0,
|
| 16 |
+
"h": 0,
|
| 17 |
+
"S": 16
|
| 18 |
+
},
|
| 19 |
+
"remaining_train_sweeps": {
|
| 20 |
+
"length_coordination": "No change. Current b=0.1, alpha=0.25 had the highest five-fold mean nDCG@10 (0.1616578464).",
|
| 21 |
+
"final_weights": "No change. w_sem=0.5 increased mean by 0.00024547 but improved only 3/5 folds and had worst-fold delta -0.00036559; rejected.",
|
| 22 |
+
"rare_weight": "No change. top3 weight 1.25 increased mean by 0.00017519 but improved only 3/5 folds and had worst-fold delta -0.00024270; rejected."
|
| 23 |
+
},
|
| 24 |
+
"test_policy": "test.tsv evaluated once after this lock; no post-test tuning."
|
| 25 |
+
}
|
configs/rag_top10_current.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"representation": {
|
| 3 |
+
"F": 4,
|
| 4 |
+
"B": 64,
|
| 5 |
+
"S": 16,
|
| 6 |
+
"L": 12,
|
| 7 |
+
"tau": 20,
|
| 8 |
+
"beta": -0.2
|
| 9 |
+
},
|
| 10 |
+
"routing": {
|
| 11 |
+
"gamma_tail": 0.25,
|
| 12 |
+
"lambda_membership": 0.125
|
| 13 |
+
},
|
| 14 |
+
"early_rescue": {
|
| 15 |
+
"idf_power": 1.0,
|
| 16 |
+
"length_b": 0.2
|
| 17 |
+
},
|
| 18 |
+
"final_chunk_score": {
|
| 19 |
+
"idf_power": 2.0,
|
| 20 |
+
"length_b": 0.1,
|
| 21 |
+
"coordination_alpha": 0.25,
|
| 22 |
+
"lambda_lex": 4.0,
|
| 23 |
+
"lambda_sem": 0.3,
|
| 24 |
+
"rare_topk": 3,
|
| 25 |
+
"rare_weight": 1.0,
|
| 26 |
+
"semantic_k": 16
|
| 27 |
+
},
|
| 28 |
+
"top10_set_selection": {
|
| 29 |
+
"branch_quality_top_docs": 3,
|
| 30 |
+
"eligible_high_quality_branches": 10,
|
| 31 |
+
"lambda_diversity": 0.1,
|
| 32 |
+
"rank1_pure_relevance": true,
|
| 33 |
+
"repeated_branches_allowed": true
|
| 34 |
+
},
|
| 35 |
+
"rag_pool": {
|
| 36 |
+
"tested": [
|
| 37 |
+
25,
|
| 38 |
+
50,
|
| 39 |
+
100,
|
| 40 |
+
200,
|
| 41 |
+
500,
|
| 42 |
+
2000
|
| 43 |
+
],
|
| 44 |
+
"large_route_provisional": 100,
|
| 45 |
+
"note": "Pool size is a RAG top-10 handle, not a deep-recall constant. Small-route corpora may prefer no additional pruning."
|
| 46 |
+
}
|
| 47 |
+
}
|
data/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Data
|
| 2 |
+
|
| 3 |
+
Datasets are intentionally not committed to the repository.
|
| 4 |
+
|
| 5 |
+
The code accepts either an extracted standard BEIR directory or a standard BEIR `.zip` containing:
|
| 6 |
+
|
| 7 |
+
```text
|
| 8 |
+
corpus.jsonl
|
| 9 |
+
queries.jsonl
|
| 10 |
+
qrels/test.tsv
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
Examples used in the current repository:
|
| 14 |
+
|
| 15 |
+
```text
|
| 16 |
+
data/scifact.zip
|
| 17 |
+
data/trec-covid.zip
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
For full MS MARCO scale reproduction, follow `docs/REPRODUCIBILITY.md` and the shard manifest in `manifests/`.
|