jupyterjazz
commited on
Commit
•
9b5c148
1
Parent(s):
851184a
feat: support fast tokenizer
Browse filesSigned-off-by: jupyterjazz <[email protected]>
- tokenizer.py +84 -77
tokenizer.py
CHANGED
@@ -1,89 +1,96 @@
|
|
1 |
import torch
|
2 |
import numpy as np
|
3 |
-
from transformers import RobertaTokenizer, BatchEncoding
|
4 |
import warnings
|
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 |
-
return apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
67 |
-
else:
|
68 |
-
try:
|
69 |
-
shape = torch.tensor(batch_encoding['input_ids']).shape
|
70 |
-
except:
|
71 |
-
raise ValueError(
|
72 |
-
"Unable to create tensor, you should probably "
|
73 |
-
"activate truncation and/or padding with "
|
74 |
-
"'padding=True' 'truncation=True' to have batched "
|
75 |
-
"tensors with the same length."
|
76 |
-
)
|
77 |
-
if isinstance(batch_encoding['input_ids'], list):
|
78 |
-
return (
|
79 |
-
apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
80 |
-
).tolist()
|
81 |
-
elif isinstance(batch_encoding['input_ids'], np.array):
|
82 |
-
return (
|
83 |
-
apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
84 |
-
).numpy()
|
85 |
-
else:
|
86 |
-
warnings.warn(
|
87 |
-
'input_ids is not a torch tensor, numpy array, or list. Returning torch tensor'
|
88 |
-
)
|
89 |
return apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
import numpy as np
|
3 |
+
from transformers import RobertaTokenizer, BatchEncoding, RobertaTokenizerFast
|
4 |
import warnings
|
5 |
|
6 |
|
7 |
+
def get_tokenizer(parent_class):
|
8 |
+
class TokenizerClass(parent_class):
|
9 |
+
def __init__(self, *args, **kwargs):
|
10 |
+
"""
|
11 |
+
JinaTokenizer extends the RobertaTokenizer class to include task_type_ids in
|
12 |
+
the batch encoding.
|
13 |
+
The task_type_ids are used to pass instruction information to the model.
|
14 |
+
A task_type should either be an integer or a sequence of integers with the same
|
15 |
+
length as the batch size.
|
16 |
+
"""
|
17 |
+
super().__init__(*args, **kwargs)
|
18 |
|
19 |
+
def __call__(self, *args, task_type=None, **kwargs):
|
20 |
+
batch_encoding = super().__call__(*args, **kwargs)
|
21 |
+
if task_type is not None:
|
22 |
+
batch_encoding = BatchEncoding(
|
23 |
+
{
|
24 |
+
'task_type_ids': self._get_task_type_ids(batch_encoding, task_type),
|
25 |
+
**batch_encoding,
|
26 |
+
},
|
27 |
+
tensor_type=kwargs.get('return_tensors'),
|
28 |
+
)
|
29 |
+
return batch_encoding
|
30 |
|
31 |
+
def _batch_encode_plus(self, *args, task_type=None, **kwargs):
|
32 |
+
batch_encoding = super()._batch_encode_plus(*args, **kwargs)
|
33 |
+
if task_type is not None:
|
34 |
+
batch_encoding = BatchEncoding(
|
35 |
+
{
|
36 |
+
'task_type_ids': self._get_task_type_ids(batch_encoding, task_type),
|
37 |
+
**batch_encoding,
|
38 |
+
},
|
39 |
+
tensor_type=kwargs.get('return_tensors'),
|
40 |
+
)
|
41 |
+
return batch_encoding
|
42 |
|
43 |
+
def _encode_plus(self, *args, task_type=None, **kwargs):
|
44 |
+
batch_encoding = super()._encode_plus(*args, **kwargs)
|
45 |
+
if task_type is not None:
|
46 |
+
batch_encoding = BatchEncoding(
|
47 |
+
{
|
48 |
+
'task_type_ids': self._get_task_type_ids(batch_encoding, task_type),
|
49 |
+
**batch_encoding,
|
50 |
+
},
|
51 |
+
tensor_type=kwargs.get('return_tensors'),
|
52 |
+
)
|
53 |
+
return batch_encoding
|
54 |
|
55 |
+
@staticmethod
|
56 |
+
def _get_task_type_ids(batch_encoding: BatchEncoding, task_type):
|
57 |
|
58 |
+
def apply_task_type(m, x):
|
59 |
+
x = torch.tensor(x)
|
60 |
+
assert (
|
61 |
+
len(x.shape) == 0 or x.shape[0] == m.shape[0]
|
62 |
+
), 'The shape of task_type does not match the size of the batch.'
|
63 |
+
return m * x if len(x.shape) == 0 else m * x[:, None]
|
64 |
|
65 |
+
if isinstance(batch_encoding['input_ids'], torch.Tensor):
|
66 |
+
shape = batch_encoding['input_ids'].shape
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
return apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
68 |
+
else:
|
69 |
+
try:
|
70 |
+
shape = torch.tensor(batch_encoding['input_ids']).shape
|
71 |
+
except:
|
72 |
+
raise ValueError(
|
73 |
+
"Unable to create tensor, you should probably "
|
74 |
+
"activate truncation and/or padding with "
|
75 |
+
"'padding=True' 'truncation=True' to have batched "
|
76 |
+
"tensors with the same length."
|
77 |
+
)
|
78 |
+
if isinstance(batch_encoding['input_ids'], list):
|
79 |
+
return (
|
80 |
+
apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
81 |
+
).tolist()
|
82 |
+
elif isinstance(batch_encoding['input_ids'], np.array):
|
83 |
+
return (
|
84 |
+
apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
85 |
+
).numpy()
|
86 |
+
else:
|
87 |
+
warnings.warn(
|
88 |
+
'input_ids is not a torch tensor, numpy array, or list. Returning torch tensor'
|
89 |
+
)
|
90 |
+
return apply_task_type(torch.ones(shape, dtype=torch.long), task_type)
|
91 |
+
|
92 |
+
return TokenizerClass
|
93 |
+
|
94 |
+
|
95 |
+
JinaTokenizer = get_tokenizer(RobertaTokenizer)
|
96 |
+
JinaTokenizerFast = get_tokenizer(RobertaTokenizerFast)
|