g-h-chen commited on
Commit
77130f1
1 Parent(s): e2a8201

upload tokenization_arcade100k.py

Browse files
Files changed (1) hide show
  1. tokenization_arcade100k.py +276 -0
tokenization_arcade100k.py ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright (c) 2023 Alibaba Cloud & Stability AI.
3
+ #
4
+ # Tongyi Qianwen LICENSE AGREEMENT:
5
+ # https://github.com/QwenLM/Qwen/blob/5aa84bdfd3237b37f01bc88cd49b3279b9a71d0b/Tongyi%20Qianwen%20LICENSE%20AGREEMENT
6
+ """Tokenization classes for Arcade100k."""
7
+
8
+ import base64
9
+ import os
10
+ import unicodedata
11
+ from typing import Collection, Dict, List, Set, Tuple, Union
12
+
13
+ import tiktoken
14
+ from transformers.utils import logging
15
+ from transformers import PreTrainedTokenizer, AddedToken
16
+
17
+ logger = logging.get_logger(__name__)
18
+
19
+ VOCAB_FILES_NAMES = {"vocab_file": "arcade100k.tiktoken"}
20
+ NAME = "arcade100k"
21
+
22
+
23
+ def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]:
24
+ with open(tiktoken_bpe_file, "rb") as f:
25
+ contents = f.read()
26
+ return {
27
+ base64.b64decode(token): int(rank)
28
+ for token, rank in (line.split() for line in contents.splitlines() if line)
29
+ }
30
+
31
+
32
+ ENDOFTEXT = "<|endoftext|>"
33
+ FIM = [
34
+ "<|fim_prefix|>",
35
+ "<|fim_middle|>",
36
+ "<|fim_suffix|>",
37
+ "<|fim_pad|>",
38
+ ]
39
+ # `StarCoder` Tokens
40
+ CODE = [
41
+ "<gh_stars>",
42
+ "<filename>",
43
+ "<issue_start>",
44
+ "<issue_comment>",
45
+ "<issue_closed>",
46
+ "<jupyter_start>",
47
+ "<jupyter_text>",
48
+ "<jupyter_code>",
49
+ "<jupyter_output>",
50
+ "<empty_output>",
51
+ "<commit_before>",
52
+ "<commit_msg>",
53
+ "<commit_after>",
54
+ "<reponame>",
55
+ ]
56
+ CHAT = [
57
+ "<|im_start|>", # Chat: Input message start
58
+ "<|im_end|>", # Chat: Input message end
59
+ ]
60
+ PAUSE = "<|pause|>" # Think before you speak (https://arxiv.org/abs/2310.02226)
61
+ REGISTERS = [
62
+ f"<|reg{i}|>" for i in range(0, 8)
63
+ ] # Register 0 sink token (https://arxiv.org/abs/2309.17453)
64
+ ENDOFPROMPT = "<|endofprompt|>"
65
+ SPECIAL_TOKENS_NAMES = (
66
+ [ENDOFTEXT]
67
+ + FIM
68
+ + CODE
69
+ + [ENDOFPROMPT]
70
+ + CHAT
71
+ + [PAUSE]
72
+ + REGISTERS
73
+ + ["<|extra0|>"]
74
+ )
75
+ START_ID = 100257
76
+ SPECIAL_TOKENS = {t: START_ID + i for i, t in enumerate(SPECIAL_TOKENS_NAMES)}
77
+
78
+
79
+ def _arcade100k(vocab_file: str):
80
+ mergeable_ranks = _load_tiktoken_bpe(vocab_file)
81
+
82
+ return {
83
+ "name": NAME,
84
+ "pat_str": r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+""",
85
+ "mergeable_ranks": mergeable_ranks,
86
+ "special_tokens": SPECIAL_TOKENS,
87
+ }
88
+
89
+
90
+ class Arcade100kTokenizer(PreTrainedTokenizer):
91
+ """
92
+ Construct a Arcade100k tokenizer backed by `tiktoken`.
93
+
94
+ Args:
95
+ vocab_file (`str`):
96
+ Path to the vocabulary file.
97
+ errors (`str`, *optional*, defaults to `"replace"`):
98
+ How to handle errors in decoding UTF-8 byte sequences.
99
+ WARNING: the default behaviour of this function is lossy, since decoded bytes are not
100
+ guaranteed to be valid UTF-8. You can control this behaviour using the `errors` parameter,
101
+ for instance, setting `errors=strict`.
102
+ """
103
+
104
+ vocab_files_names = VOCAB_FILES_NAMES
105
+ model_input_names = ["input_ids", "attention_mask"]
106
+
107
+ def __init__(
108
+ self,
109
+ vocab_file: str,
110
+ errors: str = "replace",
111
+ **kwargs,
112
+ ):
113
+ super().__init__(errors=errors, **kwargs)
114
+ self._tiktoken_config = _arcade100k(vocab_file)
115
+ self.tokenizer = tiktoken.Encoding(**self._tiktoken_config)
116
+
117
+ # TODO: Remove this assertion
118
+ assert (
119
+ len(self.tokenizer._mergeable_ranks)
120
+ + len(self.tokenizer._special_tokens)
121
+ + 1
122
+ == self.tokenizer.n_vocab
123
+ ), f"{len(self.tokenizer._mergeable_ranks) + len(self.tokenizer._special_tokens)} != {self.tokenizer.n_vocab} in encoding"
124
+
125
+ self.decoder = {i: n for n, i in self.tokenizer._mergeable_ranks.items()}
126
+ self.decoder.update({i: n for n, i in self.tokenizer._special_tokens.items()})
127
+ self.eos_token = self.decoder[self.tokenizer.eot_token]
128
+ self.pad_token = self.decoder[self.tokenizer.eot_token]
129
+ # Expose for convenience
130
+ self.mergeable_ranks = self.tokenizer._mergeable_ranks
131
+ self.special_tokens = self.tokenizer._special_tokens
132
+
133
+ def __len__(self):
134
+ return self.tokenizer.n_vocab
135
+
136
+ @property
137
+ def vocab_size(self):
138
+ return self.tokenizer.n_vocab
139
+
140
+ def get_vocab(self) -> Dict[bytes, int]:
141
+ return self.tokenizer._mergeable_ranks
142
+
143
+ def convert_tokens_to_ids(
144
+ self, tokens: Union[bytes, str, List[Union[bytes, str]]]
145
+ ) -> List[int]:
146
+ ids = []
147
+ if isinstance(tokens, (str, bytes)):
148
+ if tokens in self.tokenizer._special_tokens:
149
+ return self.tokenizer._special_tokens[tokens]
150
+ else:
151
+ return self.tokenizer._mergeable_ranks.get(tokens)
152
+ for token in tokens:
153
+ if token in self.tokenizer._special_tokens:
154
+ ids.append(self.tokenizer._special_tokens[token])
155
+ else:
156
+ ids.append(self.tokenizer._mergeable_ranks.get(token))
157
+ return ids
158
+
159
+ def _add_tokens(
160
+ self,
161
+ new_tokens: Union[List[str], List[AddedToken]],
162
+ special_tokens: bool = False,
163
+ ) -> int:
164
+ if not special_tokens and new_tokens:
165
+ raise ValueError("Adding regular tokens is not supported")
166
+ for token in new_tokens:
167
+ surface_form = token.content if isinstance(token, AddedToken) else token
168
+ if surface_form not in SPECIAL_TOKENS:
169
+ raise ValueError("Adding unknown special tokens is not supported")
170
+ return 0
171
+
172
+ def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]:
173
+ """
174
+ Save only the vocabulary of the tokenizer (vocabulary).
175
+
176
+ Returns:
177
+ `Tuple(str)`: Paths to the files saved.
178
+ """
179
+ file_path = os.path.join(save_directory, "arcade100k.tiktoken")
180
+ with open(file_path, "w", encoding="utf8") as w:
181
+ for k, v in self.tokenizer._mergeable_ranks.items():
182
+ line = base64.b64encode(k).decode("utf8") + " " + str(v) + "\n"
183
+ w.write(line)
184
+ return (file_path,)
185
+
186
+ def tokenize(
187
+ self,
188
+ text: str,
189
+ allowed_special: Union[Set, str] = "all",
190
+ disallowed_special: Union[Collection, str] = (),
191
+ **kwargs,
192
+ ) -> List[Union[bytes, str]]:
193
+ """
194
+ Converts a string in a sequence of tokens.
195
+
196
+ Args:
197
+ text (`str`):
198
+ The sequence to be encoded.
199
+ allowed_special (`Literal["all"]` or `set`):
200
+ The surface forms of the tokens to be encoded as special tokens in regular texts.
201
+ Default to "all".
202
+ disallowed_special (`Literal["all"]` or `Collection`):
203
+ The surface forms of the tokens that should not be in regular texts and trigger errors.
204
+ Default to an empty tuple.
205
+
206
+ kwargs (additional keyword arguments, *optional*):
207
+ Will be passed to the underlying model specific encode method.
208
+
209
+ Returns:
210
+ `List[bytes|str]`: The list of tokens.
211
+ """
212
+ tokens = []
213
+ text = unicodedata.normalize("NFC", text)
214
+
215
+ # this implementation takes a detour: text -> token id -> token surface forms
216
+ for t in self.tokenizer.encode(
217
+ text, allowed_special=allowed_special, disallowed_special=disallowed_special
218
+ ):
219
+ tokens.append(self.decoder[t])
220
+ return tokens
221
+
222
+ def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str:
223
+ """
224
+ Converts a sequence of tokens in a single string.
225
+ """
226
+ text = ""
227
+ temp = b""
228
+ for t in tokens:
229
+ if isinstance(t, str):
230
+ if temp:
231
+ text += temp.decode("utf-8", errors=self.errors)
232
+ temp = b""
233
+ text += t
234
+ elif isinstance(t, bytes):
235
+ temp += t
236
+ else:
237
+ raise TypeError("token should only be of type types or str")
238
+ if temp:
239
+ text += temp.decode("utf-8", errors=self.errors)
240
+ return text
241
+
242
+ def _convert_id_to_token(self, index: int) -> Union[bytes, str]:
243
+ """Converts an id to a token, special tokens included"""
244
+ if index in self.decoder:
245
+ return self.decoder[index]
246
+ raise ValueError("unknown ids")
247
+
248
+ def _convert_token_to_id(self, token: Union[bytes, str]) -> int:
249
+ """Converts a token to an id using the vocab, special tokens included"""
250
+ if token in self.tokenizer._special_tokens:
251
+ return self.tokenizer._special_tokens[token]
252
+ if token in self.tokenizer._mergeable_ranks:
253
+ return self.tokenizer._mergeable_ranks[token]
254
+ raise ValueError("unknown token")
255
+
256
+ def _tokenize(self, text: str, **kwargs):
257
+ """
258
+ Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based
259
+ vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
260
+
261
+ Do NOT take care of added tokens.
262
+ """
263
+ raise NotImplementedError
264
+
265
+ def _decode(
266
+ self,
267
+ token_ids: Union[int, List[int]],
268
+ skip_special_tokens: bool = False,
269
+ errors: str = None,
270
+ **kwargs,
271
+ ) -> str:
272
+ if isinstance(token_ids, int):
273
+ token_ids = [token_ids]
274
+ if skip_special_tokens:
275
+ token_ids = [i for i in token_ids if i < self.tokenizer.eot_token]
276
+ return self.tokenizer.decode(token_ids)