-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathevaluate_mteb_random_docs.py
262 lines (227 loc) · 7.49 KB
/
evaluate_mteb_random_docs.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
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
import argparse
import gc
import os
import random
import time
import datasets
import torch
# from cde.lib import cluster_dataset
from cde.lib.embed import DenseEncoder
from cde.lib.model_configs import MODEL_FOLDER_DICT
from cde.lib.utils import analyze_utils
from mteb import MTEB
os.environ['OPENBLAS_NUM_THREADS'] = '16'
TASK_LIST_CLASSIFICATION = [
"AmazonCounterfactualClassification",
"AmazonPolarityClassification",
"AmazonReviewsClassification",
"Banking77Classification",
"EmotionClassification",
"ImdbClassification",
"MassiveIntentClassification",
"MassiveScenarioClassification",
"MTOPDomainClassification",
"MTOPIntentClassification",
"ToxicConversationsClassification",
"TweetSentimentExtractionClassification",
]
TASK_LIST_CLUSTERING = [
"ArxivClusteringP2P",
"ArxivClusteringS2S",
"BiorxivClusteringP2P",
"BiorxivClusteringS2S",
"MedrxivClusteringP2P",
"MedrxivClusteringS2S",
"RedditClustering",
"RedditClusteringP2P",
"StackExchangeClustering",
"StackExchangeClusteringP2P",
"TwentyNewsgroupsClustering",
]
TASK_LIST_PAIR_CLASSIFICATION = [
"SprintDuplicateQuestions",
"TwitterSemEval2015",
"TwitterURLCorpus",
]
TASK_LIST_RERANKING = [
"AskUbuntuDupQuestions",
"MindSmallReranking",
"SciDocsRR",
"StackOverflowDupQuestions",
]
TASK_LIST_RETRIEVAL = [
"HotpotQA",
"ArguAna",
"ClimateFEVER",
"CQADupstackAndroidRetrieval",
"CQADupstackEnglishRetrieval",
"CQADupstackGamingRetrieval",
"CQADupstackGisRetrieval",
"CQADupstackMathematicaRetrieval",
"CQADupstackPhysicsRetrieval",
"CQADupstackProgrammersRetrieval",
"CQADupstackStatsRetrieval",
"CQADupstackTexRetrieval",
"CQADupstackUnixRetrieval",
"CQADupstackWebmastersRetrieval",
"CQADupstackWordpressRetrieval",
"DBPedia",
"FiQA2018",
"NFCorpus",
"NQ",
"QuoraRetrieval",
"SCIDOCS",
"SciFact",
"Touche2020",
"TRECCOVID",
"FEVER",
"MSMARCO",
]
# TASK_LIST_RETRIEVAL = ["SCIDOCS", "SciFact", "NFCorpus", "TRECCOVID", "Touche2020"] # Small datasets.
# TASK_LIST_RETRIEVAL = ["TRECCOVID"]
# TASK_LIST_RETRIEVAL = ["FiQA2018"]
# TASK_LIST_RETRIEVAL = [
# "ArguAna",
# "NFCorpus",
# "SCIDOCS",
# "TRECCOVID",
# "SciFact",
# "FiQA2018",
# "Touche2020",
# ] # Small datasets.
# TASK_LIST_RETRIEVAL = ["QuoraRetrieval"]
# TASK_LIST_RETRIEVAL = ["NFCorpus"]
# TASK_LIST_RETRIEVAL = ["ArguAna"]
TASK_LIST_STS = [
"BIOSSES",
"SICK-R",
"STS12",
"STS13",
"STS14",
"STS15",
"STS16",
"STS17",
"STS22",
"STSBenchmark",
"SummEval",
]
task2prefix = {}
for task in TASK_LIST_CLASSIFICATION:
task2prefix[task] = {"query": "classification", "document": "classification"}
for task in TASK_LIST_CLUSTERING:
task2prefix[task] = {"query": "clustering", "document": "clustering"}
for task in TASK_LIST_PAIR_CLASSIFICATION:
task2prefix[task] = {"query": "classification", "document": "classification"}
for task in TASK_LIST_RERANKING:
task2prefix[task] = {"query": "classification", "document": "classification"}
for task in TASK_LIST_RETRIEVAL:
task2prefix[task] = {"query": "search_query", "document": "search_document"}
for task in TASK_LIST_STS:
task2prefix[task] = {"query": "classification", "document": "classification"}
task2prefix["QuoraRetrieval"] = {"query": "search_query", "document": "search_query"}
TASK_LIST = (
TASK_LIST_CLASSIFICATION
+ TASK_LIST_CLUSTERING
+ TASK_LIST_PAIR_CLASSIFICATION
+ TASK_LIST_RERANKING
+ TASK_LIST_RETRIEVAL
+ TASK_LIST_STS
)
# TASK_LIST = TASK_LIST_RETRIEVAL
# TASK_LIST = TASK_LIST_STS
# TASK_LIST = TASK_LIST_CLUSTERING
# TASK_LIST = TASK_LIST_PAIR_CLASSIFICATION
# TASK_LIST = TASK_LIST_RERANKING
# TASK_LIST = ["Touche2020"]
def parse_args() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Process model key")
parser.add_argument(
"model_key",
help="The key for the model",
type=str,
choices=MODEL_FOLDER_DICT.keys()
)
return parser.parse_args()
NORMALIZE_EMBEDS = False
def main():
args = parse_args()
model_folder = MODEL_FOLDER_DICT[args.model_key]
trainer, (model_args, data_args, training_args) = analyze_utils.load_trainer_from_checkpoint_and_args(
model_folder=model_folder,
load_from_checkpoint=True,
return_args=True
)
trainer.model.eval()
trainer._hn_filter_model = None
gc.collect()
torch.cuda.empty_cache()
datasets.enable_caching()
mteb_encoder = DenseEncoder(
model_name_or_path=trainer.model.config.embedder,
encoder=trainer.model.second_stage_model,
max_seq_length=trainer.model.config.max_seq_length,
query_prefix="", # Set later
document_prefix="", # Set later
normalize_embeds=NORMALIZE_EMBEDS,
default_doc_prefix=True,
)
corpus_documents = open("text_data/random_docs.txt", "r").readlines()
corpus_documents = random.choices(corpus_documents, k=trainer.model.config.transductive_corpus_size)
dataset_inputs = mteb_encoder.tokenizer(
corpus_documents,
return_tensors="pt",
max_length=trainer.model.config.max_seq_length,
padding=True,
truncation=True,
).to(training_args.device)
with torch.no_grad(), torch.autocast("cuda", dtype=torch.bfloat16):
dataset_embeddings = trainer.model.first_stage_model(
**dataset_inputs
)
hidden_dim = dataset_embeddings.shape[-1]
dataset_embeddings = dataset_embeddings.reshape((1, -1, hidden_dim)) # flatten for multiple contextual tokens
dataset_embeddings = dataset_embeddings.to(torch.float32).cpu().numpy()
random.Random(time.time()).shuffle(TASK_LIST)
for task_idx, task in enumerate(TASK_LIST):
prefixes = task2prefix[task]
mteb_encoder.document_prefix = (prefixes["document"] + ": ") if data_args.use_prefix else ""
mteb_encoder.query_prefix = (prefixes["query"] + ": ") if data_args.use_prefix else ""
mteb_encoder.normalize_embeds = "Clustering" in task
print(f"[{task}] Set prefixes to {mteb_encoder.query_prefix} and {mteb_encoder.document_prefix}")
print(f"Beginning {task} ({task_idx+1} / {len(TASK_LIST)})")
evaluation = MTEB(
tasks=[task],
task_langs=["en"],
embedder_rerank="sentence-transformers/gtr-t5-base",
)
split = "dev" if task == "MSMARCO" else "test"
##################################################
trainer.model.first_stage_model.cuda()
##################################################
mteb_encoder.model_kwargs = {
"dataset_embeddings": dataset_embeddings,
"null_dataset_embedding": False,
# ""
}
# breakpoint()
results = evaluation.run(
mteb_encoder,
output_folder=os.path.join("results_mteb", "random_documents", args.model_key),
batch_size=512,
# batch_size=128,
corpus_chunk_size=500_000,
verbosity=2,
eval_splits=[split]
)
print(task)
print("\t", results)
if len(results):
results_dict = results[0].to_dict()["scores"][split][0]
try:
print("main_score =>", results_dict["main_score"])
except KeyError:
print(results_dict)
continue
print()
if __name__ == '__main__':
main()