-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieval_eval.py
More file actions
360 lines (316 loc) · 12.5 KB
/
retrieval_eval.py
File metadata and controls
360 lines (316 loc) · 12.5 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from __future__ import annotations
import argparse, orjson, json, os, statistics, itertools
from pathlib import Path
from collections import defaultdict
from typing import List, Dict, Tuple
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
import faiss
import torch
from transformers import (
AutoModel,
AutoTokenizer,
)
def load_docs(path: str) -> Tuple[List[int], List[str], Dict[int, List[int]]]:
doc_ids, texts, link_dict = [], [], {}
with open(path, "r", encoding="utf-8") as f:
for line in f:
j = orjson.loads(line)
doc_id = j["doc_id"]
semantic_id = j.get("semantic_id", "").strip()
body = j.get("chapter_body") or j.get("text") or ""
if semantic_id:
text = f"법령: {semantic_id}\n{body}"
else:
text = body
doc_ids.append(doc_id)
texts.append(text)
link_dict[doc_id] = j.get("matched_doc_id_merged") or []
return doc_ids, texts, link_dict
def load_queries(path: str) -> Tuple[List[str], List[List[int]]]:
qs, rel_lists = [], []
with open(path, "r", encoding="utf-8") as f:
for line in f:
j = orjson.loads(line)
if j.get("has_matched_docs"):
qs.append(j["question"])
rel_lists.append(j["matched_doc_id"])
return qs, rel_lists
def build_tfidf(docs: List[str], max_feats: int):
from sklearn.feature_extraction.text import TfidfVectorizer
vect = TfidfVectorizer(
ngram_range=(1, 2),
max_df=0.95,
min_df=2,
max_features=max_feats,
strip_accents="unicode",
sublinear_tf=True,
)
tfidf = vect.fit_transform(docs)
return vect, tfidf
def search_tfidf(vect, tfidf, query: str, topk: int):
q_vec = vect.transform([query])
scores = q_vec @ tfidf.T # [1, n_docs]
row = scores.toarray().ravel()
idx = np.argpartition(-row, range(topk))[:topk]
idx = idx[np.argsort(-row[idx])]
return idx, row[idx]
def build_bm25(docs: List[str], k1: float, b: float):
from rank_bm25 import BM25Okapi
tok_docs = [d.replace("\n", " ").split() for d in docs]
bm25 = BM25Okapi(tok_docs, k1=k1, b=b)
return bm25, tok_docs
def search_bm25(bm25, tokens, query: str, topk: int):
q_tok = query.split()
scores = bm25.get_scores(q_tok)
idx = np.argpartition(-scores, range(topk))[:topk]
idx = idx[np.argsort(-scores[idx])]
return idx, scores[idx]
def build_bge_index(
docs: List[str],
model_name: str,
device: str,
batch_size: int,
) -> Tuple[faiss.IndexFlatIP, np.ndarray, "SentenceTransformer"]:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(model_name, device=device)
model.max_seq_length = 2048
dim = model.get_sentence_embedding_dimension()
embs = np.empty((len(docs), dim), dtype="float32")
for s in tqdm(range(0, len(docs), batch_size), desc=f"Embedding docs ({model_name})"):
batch = docs[s : s + batch_size]
embs[s : s + batch_size] = model.encode(
batch,
batch_size=len(batch),
convert_to_numpy=True,
normalize_embeddings=True,
show_progress_bar=False,
)
index = faiss.IndexFlatIP(dim)
index.add(embs)
return index, embs, model
def search_bge(index, model, query: str, topk: int):
q_emb = model.encode([query], convert_to_numpy=True, normalize_embeddings=True)
scores, idx = index.search(q_emb, topk)
return idx.flatten(), scores.flatten()
def _encode_passages(
texts: List[str],
model: "torch.nn.Module",
tokenizer: "AutoTokenizer",
device: str,
batch_size: int,
) -> np.ndarray:
model.eval()
all_embs = []
with torch.no_grad():
try:
for s in tqdm(range(0, len(texts), batch_size), desc="Embedding docs (DPR)"):
batch = texts[s : s + batch_size]
inputs = tokenizer(
batch,
return_tensors="pt",
padding=True,
truncation=True,
max_length=1024,
).to(device)
emb = model(**inputs).last_hidden_state[:, 0, :] # CLS
all_embs.append(emb.cpu())
except:
for s in tqdm(range(0, len(texts), batch_size), desc="Embedding docs (DPR)"):
batch = texts[s : s + batch_size]
inputs = tokenizer(
batch,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
).to(device)
emb = model(**inputs).last_hidden_state[:, 0, :] # CLS
all_embs.append(emb.cpu())
embs = torch.cat(all_embs, dim=0).contiguous().numpy().astype("float32")
faiss.normalize_L2(embs) # cosine ≒ inner‑product
return embs
def build_dpr_index(
docs: List[str],
context_encoder_path: str,
device: str,
batch_size: int,
) -> Tuple[faiss.IndexFlatIP, "AutoModel", "AutoTokenizer"]:
if not os.path.isdir(context_encoder_path):
raise FileNotFoundError(f"[DPR] context_encoder_path not found: {context_encoder_path}")
ctx_tok = AutoTokenizer.from_pretrained(context_encoder_path, local_files_only=True)
ctx_model = AutoModel.from_pretrained(context_encoder_path, local_files_only=True).to(device)
dim = ctx_model.config.hidden_size
embs = _encode_passages(docs, ctx_model, ctx_tok, device, batch_size)
index = faiss.IndexFlatIP(dim)
index.add(embs)
del ctx_model
torch.cuda.empty_cache()
return index
def search_dpr(
index: faiss.IndexFlatIP,
query: str,
q_model: "AutoModel",
q_tok: "AutoTokenizer",
device: str,
topk: int,
):
q_model.eval()
with torch.no_grad():
inp = q_tok(
query,
return_tensors="pt",
padding=True,
truncation=True,
max_length=1024,
).to(device)
q_emb = q_model(**inp).last_hidden_state[:, 0, :].cpu().numpy().astype("float32")
faiss.normalize_L2(q_emb)
scores, idx = index.search(q_emb, topk)
return idx.flatten(), scores.flatten()
def expand_with_links(
retrieved_ids: List[int],
link_dict: Dict[int, List[int]],
topk: int
) -> List[int]:
seen = set()
expanded: List[int] = []
for did in retrieved_ids:
if len(expanded) >= topk:
break
if did not in seen:
expanded.append(did)
seen.add(did)
for linked in link_dict.get(did, []):
if len(expanded) >= topk:
break
if linked not in seen:
expanded.append(linked)
seen.add(linked)
return expanded
def evaluate(
run: Dict[int, List[int]],
qrels: List[List[int]],
ks=(1, 2, 3, 5, 10, 20, 100),
) -> Dict[str, float]:
recalls = {k: [] for k in ks}
rr = []
for qid, rel in enumerate(qrels):
rel_set = set(rel)
retrieved = run[qid]
rank = next((i + 1 for i, d in enumerate(retrieved) if d in rel_set), None)
rr.append(0 if rank is None else 1 / rank)
for k in ks:
hit = len([d for d in retrieved[:k] if d in rel_set])
recalls[k].append(hit / len(rel_set))
metrics = {f"R@{k}": statistics.mean(recalls[k]) for k in ks}
metrics["MRR"] = statistics.mean(rr)
return metrics
def main() -> None:
p = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument("--docs", required=True)
p.add_argument("--queries", required=True)
p.add_argument("--topk", type=int, default=100)
p.add_argument("--batch_size", type=int, default=256)
p.add_argument("--device", default="cuda:0")
p.add_argument("--methods", default="tfidf,bm25,bge,dpr")
p.add_argument("--expand_links", action="store_true")
p.add_argument("--tfidf_max_features", type=int, default=120_000)
p.add_argument("--bm25_k1", type=float, default=1.5)
p.add_argument("--bm25_b", type=float, default=0.75)
p.add_argument("--bge_model_name", default="BAAI/bge-m3")
p.add_argument("--dpr_context_encoder_path")
p.add_argument("--dpr_question_encoder_path")
args = p.parse_args()
doc_ids, doc_texts, link_dict = load_docs(args.docs)
queries, rel_lists = load_queries(args.queries)
print(f"Loaded {len(doc_ids)} documents, and {len(queries)} queries.")
id2idx = {d: i for i, d in enumerate(doc_ids)}
results, rows = {}, []
wanted = [m.strip().lower() for m in args.methods.split(",")]
if "tfidf" in wanted:
print("\n🟢 TF‑IDF Indexing")
tf_vect, tf_mat = build_tfidf(doc_texts, args.tfidf_max_features)
tf_run = {}
for qid, q in enumerate(tqdm(queries, desc="TF‑IDF Search")):
idx, _ = search_tfidf(tf_vect, tf_mat, q, args.topk)
retrieved = [doc_ids[i] for i in idx]
if args.expand_links:
retrieved = expand_with_links(retrieved, link_dict, args.topk)
tf_run[qid] = retrieved
metrics = evaluate(tf_run, rel_lists)
results["TF‑IDF"] = metrics
rows.append(metrics)
print("TF‑IDF:", json.dumps(metrics, ensure_ascii=False, indent=2))
if "bm25" in wanted:
print("\n🟢 BM25 Indexing (k1={:.2f}, b={:.2f})".format(args.bm25_k1, args.bm25_b))
bm25, tok_docs = build_bm25(doc_texts, args.bm25_k1, args.bm25_b)
bm_run = {}
for qid, q in enumerate(tqdm(queries, desc="BM25 Search")):
idx, _ = search_bm25(bm25, tok_docs, q, args.topk)
retrieved = [doc_ids[i] for i in idx]
if args.expand_links:
retrieved = expand_with_links(retrieved, link_dict, args.topk)
bm_run[qid] = retrieved
metrics = evaluate(bm_run, rel_lists)
results["BM25"] = metrics
rows.append(metrics)
print("BM25:", json.dumps(metrics, ensure_ascii=False, indent=2))
if "bge" in wanted:
print(f"\n🟢 {args.bge_model_name} Embedding")
bge_index, _, bge_model = build_bge_index(
doc_texts,
model_name=args.bge_model_name,
device=args.device,
batch_size=args.batch_size,
)
bge_run = {}
for qid, q in enumerate(tqdm(queries, desc=f"{args.bge_model_name} Search")):
idx, _ = search_bge(bge_index, bge_model, q, args.topk)
retrieved = [doc_ids[i] for i in idx]
if args.expand_links:
retrieved = expand_with_links(retrieved, link_dict, args.topk)
bge_run[qid] = retrieved
metrics = evaluate(bge_run, rel_lists)
results["BGE‑m3"] = metrics
rows.append(metrics)
print("BGE‑m3:", json.dumps(metrics, ensure_ascii=False, indent=2))
if "dpr" in wanted:
print("\n🟢 DPR Indexing")
dpr_index = build_dpr_index(
doc_texts,
context_encoder_path=args.dpr_context_encoder_path,
device=args.device,
batch_size=args.batch_size,
)
if not os.path.isdir(args.dpr_question_encoder_path):
raise FileNotFoundError(f"[DPR] question_encoder_path not found: {args.dpr_question_encoder_path}")
dpr_q_tok = AutoTokenizer.from_pretrained(args.dpr_question_encoder_path, local_files_only=True)
dpr_q_model = AutoModel.from_pretrained(args.dpr_question_encoder_path, local_files_only=True).to(args.device)
dpr_run = {}
for qid, q in enumerate(tqdm(queries, desc="DPR Search")):
idx, _ = search_dpr(
dpr_index,
q,
dpr_q_model,
dpr_q_tok,
device=args.device,
topk=args.topk,
)
retrieved = [doc_ids[i] for i in idx]
if args.expand_links:
retrieved = expand_with_links(retrieved, link_dict, args.topk)
dpr_run[qid] = retrieved
metrics = evaluate(dpr_run, rel_lists)
results["DPR"] = metrics
rows.append(metrics)
print("DPR:", json.dumps(metrics, ensure_ascii=False, indent=2))
df = pd.DataFrame(rows, index=list(results))
print("\n=== Final Results ===")
print(df.round(4).to_markdown())
out = Path("ir_metrics.csv")
df.to_csv(out, index=True)
print(f"\n➡️ Results saved to {out}.")
if __name__ == "__main__":
main()