-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_array_openai.py
220 lines (190 loc) · 6.87 KB
/
run_array_openai.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
"""
openai==0.11.4
"""
import argparse
import logging
import os
import pathlib
import pickle
import openai
from transformers import GPT2TokenizerFast
logging.basicConfig(level=logging.INFO)
os.environ["HF_DATASETS_OFFLINE"]="1" # 1 for offline
os.environ["TRANSFORMERS_OFFLINE"]="1" # 1 for offline
os.environ["TRANSFORMERS_CACHE"]="/gpfswork/rech/six/commun/models"
os.environ["HF_DATASETS_CACHE"]="/gpfswork/rech/six/commun/datasets"
os.environ["HF_MODULES_CACHE"]="/gpfswork/rech/six/commun/modules"
os.environ["HF_METRICS_CACHE"]="/gpfswork/rech/six/commun/metrics"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
API_KEY = "YOUR_KEY"
from mteb import MTEB
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 = [
"ArguAna",
"ClimateFEVER",
"CQADupstackAndroidRetrieval",
"CQADupstackEnglishRetrieval",
"CQADupstackGamingRetrieval",
"CQADupstackGisRetrieval",
"CQADupstackMathematicaRetrieval",
"CQADupstackPhysicsRetrieval",
"CQADupstackProgrammersRetrieval",
"CQADupstackStatsRetrieval",
"CQADupstackTexRetrieval",
"CQADupstackUnixRetrieval",
"CQADupstackWebmastersRetrieval",
"CQADupstackWordpressRetrieval",
"DBPedia",
"FEVER",
"FiQA2018",
"HotpotQA",
"MSMARCO",
"NFCorpus",
"NQ",
"QuoraRetrieval",
"SCIDOCS",
"SciFact",
"Touche2020",
"TRECCOVID",
]
TASK_LIST_STS = [
"BIOSSES",
"SICK-R",
"STS12",
"STS13",
"STS14",
"STS15",
"STS16",
"STS17",
"STS22",
"STSBenchmark",
"SummEval",
]
TASK_LIST = TASK_LIST_CLASSIFICATION + TASK_LIST_CLUSTERING + TASK_LIST_PAIR_CLASSIFICATION + TASK_LIST_RERANKING + TASK_LIST_RETRIEVAL + TASK_LIST_STS
class OpenAIEmbedder:
"""
Benchmark OpenAIs embeddings endpoint on USEB.
"""
def __init__(self, engine, task_name=None, batch_size=32, save_emb=False, **kwargs):
self.engine = engine
self.max_token_len = 2046 # 2048 - 2 special tokens
self.batch_size = batch_size
self.save_emb = False # Problematic as the filenames end up being the same
self.base_path = f"embeddings/{engine.split('/')[-1]}/"
self.tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
self.task_name = task_name
if save_emb:
assert self.task_name is not None
pathlib.Path(self.base_path).mkdir(parents=True, exist_ok=True)
def encode(self,
sentences,
decode=True,
idx=None,
**kwargs
):
openai.api_key = API_KEY
fin_embeddings = []
embedding_path = f"{self.base_path}/{self.task_name}_{sentences[0][:5]}_{sentences[-1][-5:]}.pickle"
if sentences and os.path.exists(embedding_path):
loaded = pickle.load(open(embedding_path, "rb"))
fin_embeddings = loaded["fin_embeddings"]
else:
for i in range(0, len(sentences), self.batch_size):
batch = sentences[i : i + self.batch_size]
all_tokens = []
used_indices = []
for j, txt in enumerate(batch):
tokens = self.tokenizer.encode(txt, add_special_tokens=False)
token_len = len(tokens)
if token_len == 0:
raise ValueError("Empty items should be cleaned prior to running")
if token_len > self.max_token_len:
tokens = tokens[:self.max_token_len]
# For some characters the API raises weird errors, e.g. input=[[126]]
if decode:
tokens = self.tokenizer.decode(tokens)
all_tokens.append(tokens)
used_indices.append(j)
out = [[]] * len(batch)
if all_tokens:
response = openai.Engine(id=self.engine).embeddings(input=all_tokens)
assert len(response["data"]) == len(
all_tokens
), f"Sent {len(all_tokens)}, got {len(response['data'])}"
for data in response["data"]:
idx = data["index"]
# OpenAI seems to return them ordered, but to be save use the index and insert
idx = used_indices[idx]
embedding = data["embedding"]
out[idx] = embedding
fin_embeddings.extend(out)
# Save embeddings
if fin_embeddings and self.save_emb:
dump = {
"fin_embeddings": fin_embeddings,
}
pickle.dump(dump, open(embedding_path, "wb"))
assert len(sentences) == len(fin_embeddings)
return fin_embeddings
def parse_args():
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--startid", type=int)
parser.add_argument("--endid", type=int)
parser.add_argument("--engine", type=str, default="text-similarity-ada-001")
parser.add_argument("--lang", type=str, default="en")
parser.add_argument("--taskname", type=str, default=None)
parser.add_argument("--batchsize", type=int, default=2048)
args = parser.parse_args()
return args
def main(args):
# Different batch size than the arg
# The below is used to send X embeddings to the API
# The CLI arg is how much will be saved / pickle file
for task in TASK_LIST[args.startid:args.endid]:
print("Running task: ", task)
model = OpenAIEmbedder(args.engine, task_name=task, batchsize=256, save_emb=True)
eval_splits = ["validation"] if task == "MSMARCO" else ["test"]
model_name = args.engine.split("/")[-1].split("_")[-1]
evaluation = MTEB(tasks=[task], task_langs=[args.lang])
evaluation.run(model, output_folder=f"results/{model_name}", batch_size=args.batchsize, eval_splits=eval_splits)
if __name__ == "__main__":
args = parse_args()
main(args)