-
Notifications
You must be signed in to change notification settings - Fork 1
/
rerank_score.py
207 lines (168 loc) · 7.72 KB
/
rerank_score.py
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
import argparse
import tempfile
import json
import os
import numpy as np
import torch
QRELS = {
"dl19-passage": "dl19-passage",
"dl20": "dl20-passage",
"trec-covid": "beir-v1.0.0-trec-covid-test",
"bioasq": "beir-v1.0.0-bioasq-test",
"nfcorpus": "beir-v1.0.0-nfcorpus-test",
"hotpotqa": "beir-v1.0.0-hotpotqa-test",
"fiqa": "beir-v1.0.0-fiqa-test",
"signal1m": "beir-v1.0.0-signal1m-test",
"trec-news": "beir-v1.0.0-trec-news-test",
"robust04": "beir-v1.0.0-robust04-test",
"arguana": "beir-v1.0.0-arguana-test",
"webis-touche2020": "beir-v1.0.0-webis-touche2020-test",
"quora": "beir-v1.0.0-quora-test",
"dbpedia-entity": "beir-v1.0.0-dbpedia-entity-test",
"scidocs": "beir-v1.0.0-scidocs-test",
"fever": "beir-v1.0.0-fever-test",
"climate-fever": "beir-v1.0.0-climate-fever-test",
"scifact": "beir-v1.0.0-scifact-test",
}
def calculate_top_k_recall(top_k_results, string_prefix=""):
n_docs = len(top_k_results[list(top_k_results.keys())[0]])
print(string_prefix, f"n_docs: {n_docs}")
for k, results in top_k_results.items():
print(f"Top-{k}: {np.mean(results) * 100:.2f}")
print()
def calculate_hard_score(ctx, rescore_type):
if rescore_type == "answerable":
return 1.0 if ctx['option_probs'][0] > ctx['option_probs'][1] else 0.0
elif rescore_type == "1-5":
return np.argmax(ctx['option_probs']) + 1
def calculate_soft_score(ctx, rescore_type, normalize_prob=False, softmax_prob=False):
if normalize_prob:
total_prob = sum(ctx['option_probs'])
if total_prob < 1e-5:
return None
ctx['option_probs'] = [p / total_prob for p in ctx['option_probs']]
elif softmax_prob:
ctx['option_probs'] = torch.softmax(torch.tensor(ctx['option_logits']), dim=-1).tolist()
if rescore_type == "answerable":
return ctx['option_probs'][0]
elif rescore_type == "1-5":
return sum([(i + 1) * p for i, p in enumerate(ctx['option_probs'])])
def calculate_gen_score(ctx, rescore_type):
lm_output = ctx['lm_output']
if rescore_type == "answerable":
if lm_output.strip().lower() == "yes":
return 1.0
elif lm_output.strip().lower() == "no":
return 0.0
else:
return None
elif rescore_type == "1-5":
try:
return float(lm_output.strip())
except ValueError:
return None
def rerank(outputs, rescore_type, normalize_prob=False, softmax_prob=False, argmax=False, reverse=False):
illegal_count = 0
for question in outputs:
for ctx in question['ctxs']:
if "lm_output" in ctx:
score = calculate_gen_score(ctx, rescore_type)
elif argmax:
score = calculate_hard_score(ctx, rescore_type)
elif "option_probs" in ctx or "option_logits" in ctx:
score = calculate_soft_score(ctx, rescore_type, normalize_prob, softmax_prob)
else:
print("Illegal format. Reranking aborted.")
exit(1)
if score is None:
illegal_count += 1
score = 0.0
ctx['score'] = score if not reverse else -score
question['ctxs'] = sorted(question['ctxs'], key=lambda x: x['score'], reverse=True)
return illegal_count
def evaluate_trec_eval(outputs, dataset):
# Write to trec_eval format
temp_file = tempfile.NamedTemporaryFile(delete=False).name
with open(temp_file, 'w') as trec_file:
for question in outputs:
qid = question['id'] if 'id' in question else question['q_id']
for i, ctx in enumerate(question['ctxs']):
trec_file.write(f"{qid} Q0 {ctx['id']} {i + 1} {ctx['score']} rerank\n")
from trec_eval import run_trec_eval
run_trec_eval(['', '-c', '-m', 'ndcg_cut.10', QRELS[dataset], temp_file])
run_trec_eval(['', '-c', '-m', 'recall.10', QRELS[dataset], temp_file])
def evaluate(outputs, per_language=False):
top_k_results = {k: [] for k in [1, 5, 20, 100]}
top_k_results_per_language = {}
for question in outputs:
rank = 10000
for i, ctx in enumerate(question['ctxs']):
if ctx['has_answer']:
rank = i + 1
break
for k in top_k_results.keys():
top_k_results[k].append(1 if rank <= k else 0)
lang = question['lang']
if lang not in top_k_results_per_language:
top_k_results_per_language[lang] = {k: [] for k in [1, 5, 20, 100]}
for k in top_k_results_per_language[lang].keys():
top_k_results_per_language[lang][k].append(1 if rank <= k else 0)
if per_language:
for lang in top_k_results_per_language.keys():
calculate_top_k_recall(
top_k_results_per_language[lang],
string_prefix=f"{lang} questions"
)
calculate_top_k_recall(top_k_results, string_prefix="All questions")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("file", help="retriever output file")
parser.add_argument("--output_file", default=None, help="If specified, will write the reranked output to this file")
parser.add_argument("--rescore_type", default="1-5", choices=["1-5", "answerable"],
help="which type of rescoring to perform")
parser.add_argument("--normalize", action='store_true', help="whether to normalize the probabilities")
parser.add_argument("--softmax", action='store_true', help="whether to softmax the logits")
parser.add_argument("--argmax", action='store_true', help="whether to argmax the logits")
parser.add_argument("--trec_eval", action='store_true', help="whether to use trec_eval")
parser.add_argument("--dataset", type=str, default=None, help="dataset name in case of trec_eval")
parser.add_argument("--no_before", action='store_true', help="if enabled, will not show original performance")
parser.add_argument("--per_language", action='store_true', help="whether show per language results")
parser.add_argument("--reverse", action='store_true', help="whether to reverse the order of the results")
args = parser.parse_args()
if args.normalize and args.softmax:
raise ValueError("You can only use one of --normalize and --softmax")
if not os.path.exists(args.file):
# for parsing purpose
print("ndcg_cut_10\t\tFileNotFound")
print("recall_100\t\tFileNotFound")
exit(1)
print("Loading retriever output file from", args.file)
with open(args.file) as jsonfile:
outputs = json.load(jsonfile)
eval_type = "Generation" if outputs[0]['ctxs'][0].get('lm_output') else "Soft"
print("-----------------------------------")
print("Rescoring type:", args.rescore_type)
print("Evaluation type:", eval_type)
print("Normalize:", args.normalize)
print("Softmax:", args.softmax)
print("Argmax:", args.argmax)
print("Trec eval:", args.trec_eval)
print("# contexts:", len(outputs[0]['ctxs']))
print("-----------------------------------")
if not args.no_before:
print("Before reranking")
if args.trec_eval:
evaluate_trec_eval(outputs, args.dataset)
else:
evaluate(outputs, args.per_language)
illegal_count = rerank(outputs, args.rescore_type, args.normalize, args.softmax, args.argmax, args.reverse)
print("After reranking")
if args.trec_eval:
evaluate_trec_eval(outputs, args.dataset)
else:
evaluate(outputs, args.per_language)
if args.output_file is not None:
print("Writing reranked output to", args.output_file)
with open(args.output_file, 'w') as jsonfile:
json.dump(outputs, jsonfile)
print(f"Illegal count: {illegal_count}")