-
Notifications
You must be signed in to change notification settings - Fork 8
/
evaluate_mteb.py
218 lines (195 loc) · 8.13 KB
/
evaluate_mteb.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
import argparse
import gc
import os
import random
import time
import datasets
import torch
import transformers
# from cde.lib import cluster_dataset
from cde.lib.embed import DenseEncoder
from cde.lib.eval.mteb import (
TASK_LIST_STS,
TASK_LIST,
task2prefix_short,
task2prefix_long,
TASK_LIST_RETRIEVAL,
TASK_LIST_CLUSTERING,
TASK_LIST_PAIR_CLASSIFICATION,
TASK_LIST_RERANKING
)
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'
TASKS_BY_CATEGORY = {
"retrieval": TASK_LIST_RETRIEVAL,
"retrieval_tiny": ["NFCorpus", "ArguAna"],
"sts": TASK_LIST_STS,
"clustering": TASK_LIST_CLUSTERING,
"pair_classification": TASK_LIST_PAIR_CLASSIFICATION,
"reranking": TASK_LIST_RERANKING,
"all": TASK_LIST,
}
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()
)
parser.add_argument(
"--batch_size",
help="Batch size for evaluation",
type=int,
default=512,
)
parser.add_argument(
"--tasks",
choices=TASKS_BY_CATEGORY.keys(),
default="all",
)
return parser.parse_args()
def main():
args = parse_args()
model_folder = MODEL_FOLDER_DICT.get(args.model_key, model_folder)
model, (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,
load_entire_trainer=False
)
model.eval()
gc.collect()
torch.cuda.empty_cache()
datasets.enable_caching()
if hasattr(model.config, "dataset_backbone"):
model_name_or_path = model.config.dataset_backbone
else:
model_name_or_path = model.config.embedder
mteb_encoder = DenseEncoder(
model_name_or_path=model_name_or_path,
encoder=model.second_stage_model,
max_seq_length=model.config.max_seq_length,
query_prefix="", # Set later
document_prefix="", # Set later
normalize_embeds=False, # Set later
default_doc_prefix=True,
)
first_stage_tokenizer = transformers.AutoTokenizer.from_pretrained(model.config.embedder)
second_stage_tokenizer = transformers.AutoTokenizer.from_pretrained(vars(model.config).get("dataset_backbone") or model.config.embedder)
tasks = TASKS_BY_CATEGORY[args.tasks]
random.Random(time.time()).shuffle(tasks)
for task_idx, task in enumerate(tasks):
document_prefix = ""
query_prefix = ""
if data_args.use_prefix:
short_prefixes = task2prefix_short[task]
is_symmetric = (short_prefixes["query"] == short_prefixes["document"])
if data_args.use_short_prefix:
document_prefix = (short_prefixes["document"] + ": ") if data_args.use_prefix else ""
query_prefix = (short_prefixes["query"] + ": ") if data_args.use_prefix else ""
else:
query_prefix = task2prefix_long[task] + second_stage_tokenizer.bos_token + " "
document_prefix = query_prefix if is_symmetric else ""
mteb_encoder.document_prefix = document_prefix
mteb_encoder.query_prefix = query_prefix
print(f"Set prefixes to {mteb_encoder.query_prefix} and {mteb_encoder.document_prefix}")
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(tasks)})")
evaluation = MTEB(
tasks=[task],
task_langs=["en"],
embedder_rerank="sentence-transformers/gtr-t5-base",
)
split = "dev" if task == "MSMARCO" else "test"
####################################################################################################
evaluation.tasks[0].load_data()
if hasattr(evaluation.tasks[0], 'corpus') and split in evaluation.tasks[0].corpus:
corpus = evaluation.tasks[0].corpus[split]
documents = random.choices(list(corpus.values()), k=model.config.transductive_corpus_size)
corpus_documents = [
mteb_encoder.document_prefix + '{} {}'.format(doc.get('title', ''), doc['text']).strip()
for doc in documents
]
elif hasattr(evaluation.tasks[0], 'dataset'):
if isinstance(evaluation.tasks[0].dataset, datasets.Dataset):
dataset = evaluation.tasks[0].dataset
elif split in evaluation.tasks[0].dataset:
dataset = evaluation.tasks[0].dataset[split]
else:
dataset = next(iter(evaluation.tasks[0].dataset.values()))
if split in dataset:
dataset = dataset[split]
else:
dataset = next(iter(dataset.values()))
column_names = set(dataset.column_names)
if {"sentence1", "sentence2"} <= column_names:
documents = dataset["sentence1"] + dataset["sentence2"]
elif {"sentences"} <= column_names:
documents = dataset["sentences"]
elif {"text"} <= column_names:
documents = dataset["text"]
elif {"query", "positive"} <= column_names:
documents = dataset["positive"]
else:
raise ValueError(f"No corpus or dataset - got {column_names}")
if isinstance(documents[0], list):
documents = [sentence for doc in documents for sentence in doc]
documents = random.choices(documents, k=model.config.transductive_corpus_size)
corpus_documents = [
mteb_encoder.document_prefix + doc
for doc in documents
]
else:
raise ValueError("No corpus or dataset available")
##################################################
print(f"Got {len(corpus_documents)} documents...")
assert len(corpus_documents) == model.config.transductive_corpus_size
print(corpus_documents[2])
model.first_stage_model.cuda()
dataset_inputs = first_stage_tokenizer(
corpus_documents,
return_tensors="pt",
max_length=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 = 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()
##################################################
mteb_encoder.model_kwargs = {
"dataset_embeddings": dataset_embeddings,
"null_dataset_embedding": False,
# ""
}
results = evaluation.run(
mteb_encoder,
output_folder=os.path.join("results_mteb", "fixed", args.model_key),
corpus_chunk_size=500_000,
verbosity=2,
eval_splits=[split],
# encode_kwargs={"batch_size": args.batch_size, "num_workers": 0},
# encode_kwargs={"batch_size": args.batch_size, "num_workers": 1},
encode_kwargs={"batch_size": args.batch_size, "num_workers": 8 },
)
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()