KaleiNeely commited on
Commit
c620bab
1 Parent(s): eff8064

Upload 4 files

Browse files
special_tokens_map.json CHANGED
@@ -1 +1,6 @@
1
- {}
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<s>",
3
+ "eos_token": "<s>",
4
+ "pad_token": "<s>",
5
+ "unk_token": "<s>"
6
+ }
tokenization_rwkv5.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2024 The HuggingFace Inc. team.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Tokenization classes for RWKV5."""
16
+
17
+ import os
18
+ from typing import TYPE_CHECKING, List, Optional, Tuple
19
+ import re
20
+
21
+ from transformers.tokenization_utils import AddedToken, PreTrainedTokenizer
22
+ from transformers.utils import logging
23
+
24
+
25
+ if TYPE_CHECKING:
26
+ pass
27
+
28
+ logger = logging.get_logger(__name__)
29
+
30
+ VOCAB_FILES_NAMES = {
31
+ "vocab_file": "vocab.txt",
32
+ }
33
+ PRETRAINED_VOCAB_FILES_MAP = {
34
+ "vocab_file": {
35
+ "ArthurZ/rwkv-5-utf": "https://huggingface.co/ArthurZ/rwkv-5-utf/blob/main/vocab.txt",
36
+ },
37
+ }
38
+
39
+
40
+
41
+ def whitespace_tokenize(text):
42
+ """Runs basic whitespace cleaning and splitting on a piece of text.
43
+ The separators are kept
44
+ """
45
+ text = text.strip()
46
+ if not text:
47
+ return []
48
+ tokens = re.split(b"(?= )", text)
49
+ return tokens
50
+
51
+
52
+ class WordpieceTokenizer(object):
53
+ """Runs WordPiece tokenization."""
54
+
55
+ def __init__(self, vocab, unk_token, max_input_chars_per_word=100):
56
+ self.vocab = vocab
57
+ self.unk_token = unk_token
58
+ self.max_input_chars_per_word = max_input_chars_per_word
59
+
60
+ def tokenize(self, text):
61
+ """
62
+ Tokenizes a piece of text into its word pieces. This uses a greedy longest-match-first algorithm to perform
63
+ tokenization using the given vocabulary.
64
+
65
+ For example, `input = "unaffable"` wil return as output `["un", "##aff", "##able"]`.
66
+
67
+ Args:
68
+ text: A single token or whitespace separated tokens. This should have
69
+ already been passed through *BasicTokenizer*.
70
+
71
+ Returns:
72
+ A list of wordpiece tokens.
73
+ """
74
+
75
+ output_tokens = []
76
+ for token in whitespace_tokenize(text):
77
+ chars = list(token)
78
+ if len(chars) > self.max_input_chars_per_word:
79
+ output_tokens.append(self.unk_token)
80
+ continue
81
+
82
+ is_bad = False
83
+ start = 0
84
+ sub_tokens = []
85
+ while start < len(chars):
86
+ end = len(chars)
87
+ cur_substr = None
88
+ while start < end:
89
+ substr = bytes(chars[start:end])
90
+ if substr in self.vocab:
91
+ cur_substr = substr
92
+ break
93
+ end -= 1
94
+ if cur_substr is None:
95
+ is_bad = True
96
+ break
97
+ sub_tokens.append(cur_substr.decode())
98
+ start = end
99
+
100
+ if is_bad:
101
+ output_tokens.append(self.unk_token)
102
+ else:
103
+ output_tokens.extend(sub_tokens)
104
+ return output_tokens
105
+
106
+
107
+ class Rwkv5Tokenizer(PreTrainedTokenizer):
108
+ vocab_files_names = VOCAB_FILES_NAMES
109
+ pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
110
+ max_model_input_sizes = {"ArthurZ/rwkv-5-utf": 2048}
111
+
112
+ model_input_names = ["input_ids", "attention_mask"]
113
+
114
+ def __init__(self, vocab_file, bos_token="<s>", eos_token="<s>", unk_token="<s>", pad_token="<s>",**kwargs):
115
+ if not os.path.isfile(vocab_file):
116
+ raise ValueError(
117
+ f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained"
118
+ " model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"
119
+ )
120
+
121
+ with open(vocab_file, "r") as reader:
122
+ tokens = reader.readlines()
123
+ vocab = {}
124
+ for index, token in enumerate(tokens):
125
+ token = eval(token.rstrip("\n"))
126
+ vocab[token] = index
127
+
128
+ self.add_bos_token = True
129
+ self.encoder = vocab
130
+ self.decoder = {v: k for k, v in vocab.items()}
131
+ self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.encoder, unk_token=str(unk_token))
132
+ self._added_tokens_decoder = {0: AddedToken(str(bos_token))}
133
+ super().__init__(bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, pad_token=pad_token, **kwargs)
134
+
135
+ @property
136
+ def vocab_size(self):
137
+ return len(self.encoder)
138
+
139
+ def get_vocab(self):
140
+ vocab = {str(self.convert_ids_to_tokens(i)): i for i in range(self.vocab_size)}
141
+ vocab.update(self.added_tokens_encoder)
142
+ return vocab
143
+
144
+ def _tokenize(self, text, split_special_tokens=False):
145
+ return self.wordpiece_tokenizer.tokenize(text.encode("utf-8"))
146
+
147
+ def _convert_token_to_id(self, token):
148
+ """Converts a token (byte) to an id using the vocab."""
149
+ if not isinstance(token, bytes):
150
+ token = token.encode("utf-8", errors="replace")
151
+ return self.encoder.get(token, self.unk_token_id)
152
+
153
+ def _convert_id_to_token(self, index):
154
+ """Converts an index (integer) in a token (byte) using the vocab."""
155
+ token = self.decoder.get(index, self.unk_token)
156
+ if isinstance(token, (bytes)):
157
+ token = token.decode("utf-8", errors="replace")
158
+ return token
159
+
160
+ def convert_tokens_to_string(self, tokens):
161
+ """Converts a sequence of tokens (bytes) in a single string. Additional tokens are encoded to bytes"""
162
+ out_string = b"".join([k.encode(errors="replace") if isinstance(k, str) else k for k in tokens]).decode(
163
+ "utf-8"
164
+ )
165
+ return out_string
166
+
167
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
168
+ index = 0
169
+ if os.path.isdir(save_directory):
170
+ vocab_file = os.path.join(
171
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
172
+ )
173
+ else:
174
+ vocab_file = (filename_prefix + "-" if filename_prefix else "") + save_directory
175
+ with open(vocab_file, "w") as writer:
176
+ for token, token_index in sorted(self.encoder.items(), key=lambda kv: kv[1]):
177
+ if index != token_index:
178
+ logger.warning(
179
+ f"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
180
+ " Please check that the vocabulary is not corrupted!"
181
+ )
182
+ index = token_index
183
+ writer.write(str(token) + "\n")
184
+ index += 1
185
+ return (vocab_file,)
186
+
187
+ def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
188
+ if self.add_bos_token:
189
+ bos_token_ids = [self.bos_token_id]
190
+ else:
191
+ bos_token_ids = []
192
+
193
+ output = bos_token_ids + token_ids_0
194
+
195
+ if token_ids_1 is None:
196
+ return output
197
+
198
+ return output + bos_token_ids + token_ids_1
199
+
200
+ def get_special_tokens_mask(
201
+ self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
202
+ ) -> List[int]:
203
+ """
204
+ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
205
+ special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods.
206
+
207
+ Args:
208
+ token_ids_0 (`List[int]`):
209
+ List of IDs.
210
+ token_ids_1 (`List[int]`, *optional*):
211
+ Optional second list of IDs for sequence pairs.
212
+ already_has_special_tokens (`bool`, *optional*, defaults to `False`):
213
+ Whether or not the token list is already formatted with special tokens for the model.
214
+
215
+ Returns:
216
+ `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
217
+ """
218
+ if already_has_special_tokens:
219
+ return super().get_special_tokens_mask(
220
+ token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
221
+ )
222
+
223
+ if not self.add_bos_token:
224
+ return super().get_special_tokens_mask(
225
+ token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=False
226
+ )
227
+
228
+ if token_ids_1 is None:
229
+ return [1] + ([0] * len(token_ids_0))
230
+ return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1))
tokenizer_config.json CHANGED
@@ -1,12 +1,23 @@
1
  {
2
- "name_or_path": "rwkv-world",
3
- "add_prefix_space": false,
4
- "tokenizer_class": "RWKVWorldTokenizer",
5
- "use_fast": false,
6
  "auto_map": {
7
  "AutoTokenizer": [
8
- "tokenization_rwkv_world.RWKVWorldTokenizer",
9
  null
10
  ]
11
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
 
1
  {
 
 
 
 
2
  "auto_map": {
3
  "AutoTokenizer": [
4
+ "tokenization_rwkv5.Rwkv5Tokenizer",
5
  null
6
  ]
7
+ },
8
+ "added_tokens_decoder": {
9
+ "0": {
10
+ "content": "<s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false,
15
+ "special": false
16
+ }
17
+ },
18
+ "bos_token": "<s>",
19
+ "clean_up_tokenization_spaces": true,
20
+ "eos_token": "<s>",
21
+ "model_max_length": 1000000000000000019884624838656,
22
+ "unk_token": "<s>"
23
  }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff