-
Notifications
You must be signed in to change notification settings - Fork 27
/
vectorizer.py
423 lines (356 loc) · 13.9 KB
/
vectorizer.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import asyncio
import math
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Optional
import nltk
import torch
import torch.nn.functional as F
from nltk.tokenize import sent_tokenize
from optimum.onnxruntime import ORTModelForFeatureExtraction
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer
from transformers import (
AutoModel,
AutoTokenizer,
DPRContextEncoder,
DPRQuestionEncoder,
T5ForConditionalGeneration,
T5Tokenizer,
)
# limit transformer batch size to limit parallel inference, otherwise we run
# into memory problems
MAX_BATCH_SIZE = 25 # TODO: take from config
DEFAULT_POOL_METHOD = "masked_mean"
class VectorInputConfig(BaseModel):
pooling_strategy: Optional[str] = None
task_type: Optional[str] = None
class VectorInput(BaseModel):
text: str
config: Optional[VectorInputConfig] = None
class Vectorizer:
executor: ThreadPoolExecutor
def __init__(
self,
model_path: str,
cuda_support: bool,
cuda_core: str,
cuda_per_process_memory_fraction: float,
model_type: str,
architecture: str,
direct_tokenize: bool,
onnx_runtime: bool,
use_sentence_transformer_vectorizer: bool,
model_name: str,
trust_remote_code: bool,
):
self.executor = ThreadPoolExecutor()
if onnx_runtime:
self.vectorizer = ONNXVectorizer(model_path, trust_remote_code)
else:
if model_type == "t5" or use_sentence_transformer_vectorizer:
self.vectorizer = SentenceTransformerVectorizer(
model_path, model_name, cuda_core, trust_remote_code
)
else:
self.vectorizer = HuggingFaceVectorizer(
model_path,
cuda_support,
cuda_core,
cuda_per_process_memory_fraction,
model_type,
architecture,
direct_tokenize,
trust_remote_code,
)
async def vectorize(self, text: str, config: VectorInputConfig):
return await asyncio.wrap_future(
self.executor.submit(self.vectorizer.vectorize, text, config)
)
class SentenceTransformerVectorizer:
model: SentenceTransformer
cuda_core: str
def __init__(
self, model_path: str, model_name: str, cuda_core: str, trust_remote_code: bool
):
self.cuda_core = cuda_core
print(
f"model_name={model_name}, cache_folder={model_path} device:{self.get_device()} trust_remote_code:{trust_remote_code}"
)
self.model = SentenceTransformer(
model_name,
cache_folder=model_path,
device=self.get_device(),
trust_remote_code=trust_remote_code,
)
self.model.eval() # make sure we're in inference mode, not training
def get_device(self) -> Optional[str]:
if self.cuda_core is not None and self.cuda_core != "":
return self.cuda_core
return None
def vectorize(self, text: str, config: VectorInputConfig):
embedding = self.model.encode(
[text],
device=self.get_device(),
convert_to_tensor=False,
convert_to_numpy=True,
)
return embedding[0]
class ONNXVectorizer:
model: ORTModelForFeatureExtraction
tokenizer: AutoTokenizer
def __init__(self, model_path, trust_remote_code: bool) -> None:
onnx_path = Path(model_path)
self.model = ORTModelForFeatureExtraction.from_pretrained(
onnx_path,
file_name="model_quantized.onnx",
trust_remote_code=trust_remote_code,
)
self.tokenizer = AutoTokenizer.from_pretrained(
onnx_path, trust_remote_code=trust_remote_code
)
def mean_pooling(self, model_output, attention_mask):
token_embeddings = model_output[
0
] # First element of model_output contains all token embeddings
input_mask_expanded = (
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
)
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
input_mask_expanded.sum(1), min=1e-9
)
def vectorize(self, text: str, config: VectorInputConfig):
encoded_input = self.tokenizer(
[text], padding=True, truncation=True, return_tensors="pt"
)
# Compute token embeddings
with torch.no_grad():
model_output = self.model(**encoded_input)
# Perform pooling
sentence_embeddings = self.mean_pooling(
model_output, encoded_input["attention_mask"]
)
# Normalize embeddings
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
return sentence_embeddings[0]
class HuggingFaceVectorizer:
model: AutoModel
tokenizer: AutoTokenizer
cuda: bool
cuda_core: str
model_type: str
direct_tokenize: bool
trust_remote_code: bool
def __init__(
self,
model_path: str,
cuda_support: bool,
cuda_core: str,
cuda_per_process_memory_fraction: float,
model_type: str,
architecture: str,
direct_tokenize: bool,
trust_remote_code: bool,
):
self.cuda = cuda_support
self.cuda_core = cuda_core
self.cuda_per_process_memory_fraction = cuda_per_process_memory_fraction
self.model_type = model_type
self.direct_tokenize = direct_tokenize
self.trust_remote_code = trust_remote_code
self.model_delegate: HFModel = ModelFactory.model(
model_type, architecture, cuda_support, cuda_core, trust_remote_code
)
self.model = self.model_delegate.create_model(model_path)
if self.cuda:
self.model.to(self.cuda_core)
if self.cuda_per_process_memory_fraction:
torch.cuda.set_per_process_memory_fraction(
self.cuda_per_process_memory_fraction
)
self.model.eval() # make sure we're in inference mode, not training
self.tokenizer = self.model_delegate.create_tokenizer(model_path)
nltk.data.path.append("./nltk_data")
def tokenize(self, text: str):
return self.tokenizer(
text,
padding=True,
truncation=True,
max_length=500,
add_special_tokens=True,
return_tensors="pt",
)
def get_embeddings(self, batch_results):
return self.model_delegate.get_embeddings(batch_results)
def get_batch_results(self, tokens, text):
return self.model_delegate.get_batch_results(tokens, text)
def pool_embedding(self, batch_results, tokens, config):
return self.model_delegate.pool_embedding(batch_results, tokens, config)
def vectorize(self, text: str, config: VectorInputConfig):
with torch.no_grad():
if self.direct_tokenize:
# create embeddings without tokenizing text
tokens = self.tokenize(text)
if self.cuda:
tokens.to(self.cuda_core)
batch_results = self.get_batch_results(tokens, text)
batch_sum_vectors = self.pool_embedding(batch_results, tokens, config)
return batch_sum_vectors.detach()
else:
# tokenize text
sentences = sent_tokenize(
" ".join(
text.split(),
)
)
num_sentences = len(sentences)
number_of_batch_vectors = math.ceil(num_sentences / MAX_BATCH_SIZE)
batch_sum_vectors = 0
for i in range(0, number_of_batch_vectors):
start_index = i * MAX_BATCH_SIZE
end_index = start_index + MAX_BATCH_SIZE
tokens = self.tokenize(sentences[start_index:end_index])
if self.cuda:
tokens.to(self.cuda_core)
batch_results = self.get_batch_results(
tokens, sentences[start_index:end_index]
)
batch_sum_vectors += self.pool_embedding(
batch_results, tokens, config
)
return batch_sum_vectors.detach() / num_sentences
class HFModel:
def __init__(self, cuda_support: bool, cuda_core: str, trust_remote_code: bool):
super().__init__()
self.model = None
self.tokenizer = None
self.cuda = cuda_support
self.cuda_core = cuda_core
self.trust_remote_code = trust_remote_code
def create_tokenizer(self, model_path):
self.tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=self.trust_remote_code
)
return self.tokenizer
def create_model(self, model_path):
self.model = AutoModel.from_pretrained(
model_path, trust_remote_code=self.trust_remote_code
)
return self.model
def get_embeddings(self, batch_results):
return batch_results[0]
def get_batch_results(self, tokens, text):
return self.model(**tokens)
def pool_embedding(self, batch_results, tokens, config: VectorInputConfig):
pooling_method = self.pool_method_from_config(config)
if pooling_method == "cls":
return self.get_embeddings(batch_results)[:, 0, :].sum(0)
elif pooling_method == "masked_mean":
return self.pool_sum(
self.get_embeddings(batch_results), tokens["attention_mask"]
)
else:
raise Exception(f"invalid pooling method '{pooling_method}'")
def pool_method_from_config(self, config: VectorInputConfig):
if config is None:
return DEFAULT_POOL_METHOD
if config.pooling_strategy is None or config.pooling_strategy == "":
return DEFAULT_POOL_METHOD
return config.pooling_strategy
def get_sum_embeddings_mask(self, embeddings, input_mask_expanded):
if self.cuda:
sum_embeddings = torch.sum(embeddings * input_mask_expanded, 1).to(
self.cuda_core
)
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9).to(
self.cuda_core
)
return sum_embeddings, sum_mask
else:
sum_embeddings = torch.sum(embeddings * input_mask_expanded, 1)
sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
return sum_embeddings, sum_mask
def pool_sum(self, embeddings, attention_mask):
input_mask_expanded = (
attention_mask.unsqueeze(-1).expand(embeddings.size()).float()
)
sum_embeddings, sum_mask = self.get_sum_embeddings_mask(
embeddings, input_mask_expanded
)
sentences = sum_embeddings / sum_mask
return sentences.sum(0)
class DPRModel(HFModel):
def __init__(
self,
architecture: str,
cuda_support: bool,
cuda_core: str,
trust_remote_code: bool,
):
super().__init__(cuda_support, cuda_core, trust_remote_code)
self.model = None
self.architecture = architecture
self.trust_remote_code = trust_remote_code
def create_model(self, model_path):
if self.architecture == "DPRQuestionEncoder":
self.model = DPRQuestionEncoder.from_pretrained(
model_path, trust_remote_code=self.trust_remote_code
)
else:
self.model = DPRContextEncoder.from_pretrained(
model_path, trust_remote_code=self.trust_remote_code
)
return self.model
def get_batch_results(self, tokens, text):
return self.model(tokens["input_ids"], tokens["attention_mask"])
def pool_embedding(self, batch_results, tokens, config: VectorInputConfig):
# no pooling needed for DPR
return batch_results["pooler_output"][0]
class T5Model(HFModel):
def __init__(self, cuda_support: bool, cuda_core: str, trust_remote_code: bool):
super().__init__(cuda_support, cuda_core)
self.model = None
self.tokenizer = None
self.cuda = cuda_support
self.cuda_core = cuda_core
self.trust_remote_code = trust_remote_code
def create_model(self, model_path):
self.model = T5ForConditionalGeneration.from_pretrained(
model_path, trust_remote_code=self.trust_remote_code
)
return self.model
def create_tokenizer(self, model_path):
self.tokenizer = T5Tokenizer.from_pretrained(
model_path, trust_remote_code=self.trust_remote_code
)
return self.tokenizer
def get_embeddings(self, batch_results):
return batch_results["encoder_last_hidden_state"]
def get_batch_results(self, tokens, text):
input_ids, attention_mask = tokens["input_ids"], tokens["attention_mask"]
target_encoding = self.tokenizer(
text, padding="longest", max_length=500, truncation=True
)
labels = target_encoding.input_ids
if self.cuda:
labels = torch.tensor(labels).to(self.cuda_core)
else:
labels = torch.tensor(labels)
return self.model(
input_ids=input_ids, attention_mask=attention_mask, labels=labels
)
class ModelFactory:
@staticmethod
def model(
model_type,
architecture,
cuda_support: bool,
cuda_core: str,
trust_remote_code: bool,
):
if model_type == "t5":
return T5Model(cuda_support, cuda_core, trust_remote_code)
elif model_type == "dpr":
return DPRModel(architecture, cuda_support, cuda_core, trust_remote_code)
else:
return HFModel(cuda_support, cuda_core, trust_remote_code)