yzhangcs commited on
Commit
2b60083
·
verified ·
1 Parent(s): 48e394a

Upload RWKV6ForCausalLM

Browse files
config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "fla-hub/rwkv6-1.6B-finch",
3
+ "architectures": [
4
+ "RWKV6ForCausalLM"
5
+ ],
6
+ "attn_mode": "chunk",
7
+ "bos_token_id": 1,
8
+ "clamp_min": null,
9
+ "conv_size": 4,
10
+ "eos_token_id": 2,
11
+ "expand_k": 1,
12
+ "expand_v": 1,
13
+ "fuse_cross_entropy": true,
14
+ "fuse_norm": true,
15
+ "gate_low_rank_dim": 64,
16
+ "hidden_act": "sqrelu",
17
+ "hidden_ratio": 3.5,
18
+ "hidden_size": 2048,
19
+ "initializer_range": 0.02,
20
+ "intermediate_size": null,
21
+ "max_position_embeddings": 2048,
22
+ "model_type": "rwkv6",
23
+ "norm_bias": true,
24
+ "norm_eps": 1e-06,
25
+ "norm_first": true,
26
+ "num_heads": 32,
27
+ "num_hidden_layers": 24,
28
+ "pad_token_id": 0,
29
+ "proj_low_rank_dim": 32,
30
+ "share_conv_kernel": true,
31
+ "tie_word_embeddings": false,
32
+ "torch_dtype": "bfloat16",
33
+ "transformers_version": "4.41.2",
34
+ "use_cache": true,
35
+ "use_glu": false,
36
+ "vocab_size": 65536
37
+ }
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 1,
4
+ "eos_token_id": 2,
5
+ "transformers_version": "4.41.2"
6
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:db508532c15e96d89566080cc39cadfa517d4cba83f782a59faa7f304b00da0a
3
+ size 3199810944
special_tokens_map.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "<s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "<s>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "unk_token": {
24
+ "content": "<s>",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ }
30
+ }
tokenization_rwkv5.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import re
19
+ from typing import TYPE_CHECKING, List, Optional, Tuple
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
+ def whitespace_tokenize(text):
41
+ """Runs basic whitespace cleaning and splitting on a piece of text.
42
+ The separators are kept
43
+ """
44
+ text = text.strip()
45
+ if not text:
46
+ return []
47
+ tokens = re.split(b"(?= )", text)
48
+ return tokens
49
+
50
+
51
+ class WordpieceTokenizer(object):
52
+ """Runs WordPiece tokenization."""
53
+
54
+ def __init__(self, vocab, unk_token):
55
+ self.vocab = vocab
56
+ self.unk_token = unk_token
57
+
58
+ def tokenize(self, text):
59
+ """
60
+ Tokenizes a piece of text into its word pieces. This uses a greedy longest-match-first algorithm to perform
61
+ tokenization using the given vocabulary.
62
+
63
+ For example, `input = "unaffable"` wil return as output `["un", "##aff", "##able"]`.
64
+
65
+ Args:
66
+ text: A single token or whitespace separated tokens. This should have
67
+ already been passed through *BasicTokenizer*.
68
+
69
+ Returns:
70
+ A list of wordpiece tokens.
71
+ """
72
+
73
+ output_tokens = []
74
+ for token in whitespace_tokenize(text):
75
+ chars = list(token)
76
+ is_bad = False
77
+ start = 0
78
+ sub_tokens = []
79
+ while start < len(chars):
80
+ end = len(chars)
81
+ cur_substr = None
82
+ while start < end:
83
+ substr = bytes(chars[start:end])
84
+ if substr in self.vocab:
85
+ cur_substr = substr
86
+ break
87
+ end -= 1
88
+ if cur_substr is None:
89
+ is_bad = True
90
+ break
91
+ try:
92
+ cur_substr = cur_substr.decode()
93
+ except UnicodeDecodeError:
94
+ cur_substr = str(cur_substr)
95
+ sub_tokens.append(cur_substr)
96
+ start = end
97
+ if is_bad:
98
+ output_tokens.append(self.unk_token)
99
+ else:
100
+ output_tokens.extend(sub_tokens)
101
+ return output_tokens
102
+
103
+
104
+ class Rwkv5Tokenizer(PreTrainedTokenizer):
105
+ vocab_files_names = VOCAB_FILES_NAMES
106
+ pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
107
+ max_model_input_sizes = {"ArthurZ/rwkv-5-utf": 2048}
108
+
109
+ model_input_names = ["input_ids", "attention_mask"]
110
+
111
+ def __init__(self, vocab_file, bos_token="<s>", eos_token="<s>", unk_token="<s>", **kwargs):
112
+ if not os.path.isfile(vocab_file):
113
+ raise ValueError(
114
+ f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained"
115
+ " model use `tokenizer = BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`"
116
+ )
117
+
118
+ with open(vocab_file, "r") as reader:
119
+ tokens = reader.readlines()
120
+ vocab = {}
121
+ for index, token in enumerate(tokens):
122
+ token = eval(token.rstrip("\n"))
123
+ vocab[token] = index
124
+
125
+ self.add_bos_token = True
126
+ self.encoder = vocab
127
+ self.decoder = {v: k for k, v in vocab.items()}
128
+ self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.encoder, unk_token=str(unk_token))
129
+ self._added_tokens_decoder = {0: AddedToken(str(bos_token))}
130
+ super().__init__(bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, **kwargs)
131
+
132
+ @property
133
+ def vocab_size(self):
134
+ return len(self.encoder)
135
+
136
+ def get_vocab(self):
137
+ vocab = {str(self.convert_ids_to_tokens(i)): i for i in range(self.vocab_size)}
138
+ vocab.update(self.added_tokens_encoder)
139
+ return vocab
140
+
141
+ def _tokenize(self, text, split_special_tokens=False):
142
+ return self.wordpiece_tokenizer.tokenize(text.encode("utf-8"))
143
+
144
+ def _convert_token_to_id(self, token):
145
+ """Converts a token (byte) to an id using the vocab."""
146
+ if token.startswith("b'\\"):
147
+ token = eval(token)
148
+ elif not isinstance(token, bytes):
149
+ token = token.encode("utf-8", errors="replace")
150
+ return self.encoder.get(token, self.unk_token_id)
151
+
152
+ def _convert_id_to_token(self, index):
153
+ """Converts an index (integer) in a token (byte) using the vocab."""
154
+ token = self.decoder.get(index, self.unk_token)
155
+ if isinstance(token, (bytes)):
156
+ token = token.decode("utf-8", errors="replace")
157
+ return token
158
+
159
+ def convert_tokens_to_string(self, tokens):
160
+ """Converts a sequence of tokens (bytes) in a single string. Additional tokens are encoded to bytes"""
161
+ out_string = b"".join([k.encode(errors="replace") if isinstance(k, str) else k for k in tokens]).decode(
162
+ "utf-8"
163
+ )
164
+ return out_string
165
+
166
+ def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]:
167
+ index = 0
168
+ if os.path.isdir(save_directory):
169
+ vocab_file = os.path.join(
170
+ save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
171
+ )
172
+ else:
173
+ vocab_file = (filename_prefix + "-" if filename_prefix else "") + save_directory
174
+ with open(vocab_file, "w") as writer:
175
+ for token, token_index in sorted(self.encoder.items(), key=lambda kv: kv[1]):
176
+ if index != token_index:
177
+ logger.warning(
178
+ f"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
179
+ " Please check that the vocabulary is not corrupted!"
180
+ )
181
+ index = token_index
182
+ writer.write(str(token) + "\n")
183
+ index += 1
184
+ return (vocab_file,)
185
+
186
+ def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
187
+ if self.add_bos_token:
188
+ bos_token_ids = [self.bos_token_id]
189
+ else:
190
+ bos_token_ids = []
191
+
192
+ output = bos_token_ids + token_ids_0
193
+
194
+ if token_ids_1 is None:
195
+ return output
196
+
197
+ return output + bos_token_ids + token_ids_1
198
+
199
+ def get_special_tokens_mask(
200
+ self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
201
+ ) -> List[int]:
202
+ """
203
+ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding
204
+ special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods.
205
+
206
+ Args:
207
+ token_ids_0 (`List[int]`):
208
+ List of IDs.
209
+ token_ids_1 (`List[int]`, *optional*):
210
+ Optional second list of IDs for sequence pairs.
211
+ already_has_special_tokens (`bool`, *optional*, defaults to `False`):
212
+ Whether or not the token list is already formatted with special tokens for the model.
213
+
214
+ Returns:
215
+ `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
216
+ """
217
+ if already_has_special_tokens:
218
+ return super().get_special_tokens_mask(
219
+ token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
220
+ )
221
+
222
+ if not self.add_bos_token:
223
+ return super().get_special_tokens_mask(
224
+ token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=False
225
+ )
226
+
227
+ if token_ids_1 is None:
228
+ return [1] + ([0] * len(token_ids_0))
229
+ return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1))
tokenizer_config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "added_tokens_decoder": {
4
+ "0": {
5
+ "content": "<s>",
6
+ "lstrip": false,
7
+ "normalized": false,
8
+ "rstrip": false,
9
+ "single_word": false,
10
+ "special": true
11
+ }
12
+ },
13
+ "auto_map": {
14
+ "AutoTokenizer": [
15
+ "tokenization_rwkv5.Rwkv5Tokenizer",
16
+ null
17
+ ]
18
+ },
19
+ "bos_token": "<s>",
20
+ "clean_up_tokenization_spaces": true,
21
+ "eos_token": "<s>",
22
+ "model_max_length": 1000000000000000019884624838656,
23
+ "pad_token": "<s>",
24
+ "tokenizer_class": "Rwkv5Tokenizer",
25
+ "unk_token": "<s>",
26
+ "use_fast": false
27
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff