yairschiff commited on
Commit
be6d2b4
1 Parent(s): 3bfe232

Upload tokenizer

Browse files
special_tokens_map.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "[BOS]",
3
+ "cls_token": "[CLS]",
4
+ "eos_token": "[SEP]",
5
+ "mask_token": "[MASK]",
6
+ "pad_token": "[PAD]",
7
+ "sep_token": "[SEP]",
8
+ "unk_token": "[UNK]"
9
+ }
tokenization_caduceus.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Character tokenizer for Hugging Face.
2
+
3
+ """
4
+
5
+ from typing import List, Optional, Dict, Sequence, Tuple
6
+
7
+ from transformers import PreTrainedTokenizer
8
+
9
+
10
+ class CaduceusTokenizer(PreTrainedTokenizer):
11
+ model_input_names = ["input_ids"]
12
+
13
+ def __init__(self,
14
+ model_max_length: int,
15
+ characters: Sequence[str] = ("A", "C", "G", "T", "N"),
16
+ complement_map=None,
17
+ bos_token="[BOS]",
18
+ eos_token="[SEP]",
19
+ sep_token="[SEP]",
20
+ cls_token="[CLS]",
21
+ pad_token="[PAD]",
22
+ mask_token="[MASK]",
23
+ unk_token="[UNK]",
24
+ **kwargs):
25
+ """Character tokenizer for Hugging Face transformers.
26
+
27
+ Adapted from https://huggingface.co/LongSafari/hyenadna-tiny-1k-seqlen-hf/blob/main/tokenization_hyena.py
28
+ Args:
29
+ model_max_length (int): Model maximum sequence length.
30
+ characters (Sequence[str]): List of desired characters. Any character which
31
+ is not included in this list will be replaced by a special token called
32
+ [UNK] with id=6. Following is a list of the special tokens with
33
+ their corresponding ids:
34
+ "[CLS]": 0
35
+ "[SEP]": 1
36
+ "[BOS]": 2
37
+ "[MASK]": 3
38
+ "[PAD]": 4
39
+ "[RESERVED]": 5
40
+ "[UNK]": 6
41
+ an id (starting at 7) will be assigned to each character.
42
+ complement_map (Optional[Dict[str, str]]): Dictionary with string complements for each character.
43
+ """
44
+ if complement_map is None:
45
+ complement_map = {"A": "T", "C": "G", "G": "C", "T": "A"}
46
+ self.characters = characters
47
+ self.model_max_length = model_max_length
48
+
49
+ self._vocab_str_to_int = {
50
+ "[CLS]": 0,
51
+ "[SEP]": 1,
52
+ "[BOS]": 2,
53
+ "[MASK]": 3,
54
+ "[PAD]": 4,
55
+ "[RESERVED]": 5,
56
+ "[UNK]": 6,
57
+ **{ch: i + 7 for i, ch in enumerate(self.characters)},
58
+ }
59
+ self._vocab_int_to_str = {v: k for k, v in self._vocab_str_to_int.items()}
60
+ add_prefix_space = kwargs.pop("add_prefix_space", False)
61
+ padding_side = kwargs.pop("padding_side", "left")
62
+
63
+ self._complement_map = {}
64
+ for k, v in self._vocab_str_to_int.items():
65
+ complement_id = self._vocab_str_to_int[complement_map[k]] if k in complement_map.keys() else v
66
+ self._complement_map[self._vocab_str_to_int[k]] = complement_id
67
+
68
+ super().__init__(
69
+ bos_token=bos_token,
70
+ eos_token=eos_token,
71
+ sep_token=sep_token,
72
+ cls_token=cls_token,
73
+ pad_token=pad_token,
74
+ mask_token=mask_token,
75
+ unk_token=unk_token,
76
+ add_prefix_space=add_prefix_space,
77
+ model_max_length=model_max_length,
78
+ padding_side=padding_side,
79
+ **kwargs,
80
+ )
81
+
82
+ @property
83
+ def vocab_size(self) -> int:
84
+ return len(self._vocab_str_to_int)
85
+
86
+ @property
87
+ def complement_map(self) -> Dict[int, int]:
88
+ return self._complement_map
89
+
90
+ def _tokenize(self, text: str, **kwargs) -> List[str]:
91
+ return list(text.upper()) # Convert all base pairs to uppercase
92
+
93
+ def _convert_token_to_id(self, token: str) -> int:
94
+ return self._vocab_str_to_int.get(token, self._vocab_str_to_int["[UNK]"])
95
+
96
+ def _convert_id_to_token(self, index: int) -> str:
97
+ return self._vocab_int_to_str[index]
98
+
99
+ def convert_tokens_to_string(self, tokens):
100
+ return "".join(tokens) # Note: this operation has lost info about which base pairs were originally lowercase
101
+
102
+ def get_special_tokens_mask(
103
+ self,
104
+ token_ids_0: List[int],
105
+ token_ids_1: Optional[List[int]] = None,
106
+ already_has_special_tokens: bool = False,
107
+ ) -> List[int]:
108
+ if already_has_special_tokens:
109
+ return super().get_special_tokens_mask(
110
+ token_ids_0=token_ids_0,
111
+ token_ids_1=token_ids_1,
112
+ already_has_special_tokens=True,
113
+ )
114
+
115
+ result = ([0] * len(token_ids_0)) + [1]
116
+ if token_ids_1 is not None:
117
+ result += ([0] * len(token_ids_1)) + [1]
118
+ return result
119
+
120
+ def build_inputs_with_special_tokens(
121
+ self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
122
+ ) -> List[int]:
123
+ sep = [self.sep_token_id]
124
+ # cls = [self.cls_token_id]
125
+ result = token_ids_0 + sep
126
+ if token_ids_1 is not None:
127
+ result += token_ids_1 + sep
128
+ return result
129
+
130
+ def get_vocab(self) -> Dict[str, int]:
131
+ return self._vocab_str_to_int
132
+
133
+ # Fixed vocabulary with no vocab file
134
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple:
135
+ return ()
tokenizer_config.json ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "added_tokens_decoder": {
4
+ "0": {
5
+ "content": "[CLS]",
6
+ "lstrip": false,
7
+ "normalized": false,
8
+ "rstrip": false,
9
+ "single_word": false,
10
+ "special": true
11
+ },
12
+ "1": {
13
+ "content": "[SEP]",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false,
18
+ "special": true
19
+ },
20
+ "2": {
21
+ "content": "[BOS]",
22
+ "lstrip": false,
23
+ "normalized": false,
24
+ "rstrip": false,
25
+ "single_word": false,
26
+ "special": true
27
+ },
28
+ "3": {
29
+ "content": "[MASK]",
30
+ "lstrip": false,
31
+ "normalized": false,
32
+ "rstrip": false,
33
+ "single_word": false,
34
+ "special": true
35
+ },
36
+ "4": {
37
+ "content": "[PAD]",
38
+ "lstrip": false,
39
+ "normalized": false,
40
+ "rstrip": false,
41
+ "single_word": false,
42
+ "special": true
43
+ },
44
+ "6": {
45
+ "content": "[UNK]",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false,
50
+ "special": true
51
+ }
52
+ },
53
+ "auto_map": {
54
+ "AutoTokenizer": [
55
+ "tokenization_caduceus.CaduceusTokenizer",
56
+ null
57
+ ]
58
+ },
59
+ "bos_token": "[BOS]",
60
+ "clean_up_tokenization_spaces": true,
61
+ "cls_token": "[CLS]",
62
+ "eos_token": "[SEP]",
63
+ "mask_token": "[MASK]",
64
+ "model_max_length": 512,
65
+ "pad_token": "[PAD]",
66
+ "padding_side": "left",
67
+ "sep_token": "[SEP]",
68
+ "tokenizer_class": "CaduceusTokenizer",
69
+ "unk_token": "[UNK]"
70
+ }