GinnM commited on
Commit
44c054e
·
verified ·
1 Parent(s): 1881711

Upload tokenizer

Browse files
special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "<cls>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "<eos>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "mask_token": {
17
+ "content": "<mask>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "pad_token": {
24
+ "content": "<pad>",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "<unk>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
tokenization_transformer.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import List, Optional
3
+ from transformers.tokenization_utils import PreTrainedTokenizer
4
+ from pathlib import Path
5
+
6
+ VOCAB_FILES_NAMES = {"vocab_file": "vocab.txt"}
7
+
8
+
9
+ def load_vocab_file(vocab_file):
10
+ with open(vocab_file, "r") as f:
11
+ lines = f.read().splitlines()
12
+ return [l.strip() for l in lines]
13
+
14
+
15
+ class VPLMTokenizer(PreTrainedTokenizer):
16
+ """
17
+ Constructs a VPLM tokenizer.
18
+ """
19
+
20
+ vocab_files_names = VOCAB_FILES_NAMES
21
+ model_input_names = ["input_ids", "attention_mask"]
22
+
23
+ def __init__(
24
+ self,
25
+ vocab_file=None,
26
+ unk_token="<unk>",
27
+ cls_token="<cls>",
28
+ pad_token="<pad>",
29
+ mask_token="<mask>",
30
+ eos_token="<eos>",
31
+ **kwargs,
32
+ ):
33
+ if vocab_file is None:
34
+ vocab_file = Path(__file__).parent / "vocab.txt"
35
+ self.all_tokens = load_vocab_file(vocab_file)
36
+ self._id_to_token = dict(enumerate(self.all_tokens))
37
+ self._token_to_id = {tok: ind for ind, tok in enumerate(self.all_tokens)}
38
+ super().__init__(
39
+ unk_token=unk_token,
40
+ cls_token=cls_token,
41
+ pad_token=pad_token,
42
+ mask_token=mask_token,
43
+ eos_token=eos_token,
44
+ **kwargs,
45
+ )
46
+
47
+ # TODO, all the tokens are added? But they are also part of the vocab... bit strange.
48
+ # none of them are special, but they all need special splitting.
49
+
50
+ self.unique_no_split_tokens = self.all_tokens
51
+ self._update_trie(self.unique_no_split_tokens)
52
+
53
+ def _convert_id_to_token(self, index: int) -> str:
54
+ return self._id_to_token.get(index, self.unk_token)
55
+
56
+ def _convert_token_to_id(self, token: str) -> int:
57
+ return self._token_to_id.get(token, self._token_to_id.get(self.unk_token))
58
+
59
+ def _tokenize(self, text, **kwargs):
60
+ return text.split()
61
+
62
+ def get_vocab(self):
63
+ base_vocab = self._token_to_id.copy()
64
+ base_vocab.update(self.added_tokens_encoder)
65
+ return base_vocab
66
+
67
+ def token_to_id(self, token: str) -> int:
68
+ return self._token_to_id.get(token, self._token_to_id.get(self.unk_token))
69
+
70
+ def id_to_token(self, index: int) -> str:
71
+ return self._id_to_token.get(index, self.unk_token)
72
+
73
+ def build_inputs_with_special_tokens(
74
+ self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
75
+ ) -> List[int]:
76
+ cls = [self.cls_token_id]
77
+ sep = [self.eos_token_id]
78
+ if token_ids_1 is None:
79
+ if self.eos_token_id is None:
80
+ return cls + token_ids_0
81
+ else:
82
+ return cls + token_ids_0 + sep
83
+ elif self.eos_token_id is None:
84
+ raise ValueError("Cannot tokenize multiple sequences when EOS token is not set!")
85
+ return cls + token_ids_0 + sep + token_ids_1 + sep # Multiple inputs always have an EOS token
86
+
87
+ def get_special_tokens_mask(
88
+ self, token_ids_0: List, token_ids_1: Optional[List] = None, already_has_special_tokens: bool = False
89
+ ) -> List[int]:
90
+ """
91
+ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
92
+ special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods.
93
+
94
+ Args:
95
+ token_ids_0 (`List[int]`):
96
+ List of ids of the first sequence.
97
+ token_ids_1 (`List[int]`, *optional*):
98
+ List of ids of the second sequence.
99
+ already_has_special_tokens (`bool`, *optional*, defaults to `False`):
100
+ Whether or not the token list is already formatted with special tokens for the model.
101
+
102
+ Returns:
103
+ A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
104
+ """
105
+ if already_has_special_tokens:
106
+ if token_ids_1 is not None:
107
+ raise ValueError(
108
+ "You should not supply a second sequence if the provided sequence of "
109
+ "ids is already formatted with special tokens for the model."
110
+ )
111
+
112
+ return [1 if token in self.all_special_ids else 0 for token in token_ids_0]
113
+ mask = [1] + ([0] * len(token_ids_0)) + [1]
114
+ if token_ids_1 is not None:
115
+ mask += [0] * len(token_ids_1) + [1]
116
+ return mask
117
+
118
+ def save_vocabulary(self, save_directory, filename_prefix):
119
+ vocab_file = os.path.join(save_directory, (filename_prefix + "-" if filename_prefix else "") + "vocab.txt")
120
+ with open(vocab_file, "w") as f:
121
+ f.write("\n".join(self.all_tokens))
122
+ return (vocab_file,)
123
+
124
+ @property
125
+ def vocab_size(self) -> int:
126
+ return len(self.all_tokens)
127
+
128
+ VPLMTokenizer.register_for_auto_class("AutoTokenizer")
tokenizer_config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<pad>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<cls>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "<eos>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "23": {
28
+ "content": "<mask>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "24": {
36
+ "content": "<unk>",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "auto_map": {
45
+ "AutoTokenizer": [
46
+ "tokenization_transformer.VPLMTokenizer",
47
+ null
48
+ ]
49
+ },
50
+ "clean_up_tokenization_spaces": true,
51
+ "cls_token": "<cls>",
52
+ "eos_token": "<eos>",
53
+ "mask_token": "<mask>",
54
+ "model_max_length": 1000000000000000019884624838656,
55
+ "pad_token": "<pad>",
56
+ "tokenizer_class": "VPLMTokenizer",
57
+ "unk_token": "<unk>"
58
+ }
vocab.txt ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <pad>
2
+ <cls>
3
+ <eos>
4
+ L
5
+ A
6
+ G
7
+ V
8
+ S
9
+ E
10
+ R
11
+ T
12
+ I
13
+ D
14
+ P
15
+ K
16
+ Q
17
+ N
18
+ F
19
+ Y
20
+ M
21
+ H
22
+ W
23
+ C
24
+ <mask>
25
+ <unk>
26
+ <null_0>
27
+ <null_1>
28
+ <null_2>
29
+ <null_3>
30
+ <null_4>
31
+ <null_5>
32
+ <null_6>
33
+ <null_7>
34
+ <null_8>
35
+ <null_9>
36
+ -