liufanfanlff commited on
Commit
4c0bb59
·
verified ·
1 Parent(s): 0363527

Upload folder using huggingface_hub

Browse files
config.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "",
3
+ "architectures": [
4
+ "C3QwenForCausalLM"
5
+ ],
6
+ "auto_map": {
7
+ "AutoConfig": "modeling_C3.C3Config",
8
+ "AutoModel": "modeling_C3.C3QwenForCausalLM"
9
+ },
10
+ "attention_dropout": 0.0,
11
+ "bos_token_id": 151643,
12
+ "contexts_compression_llm_hidden_size": 1536,
13
+ "eos_token_id": 151643,
14
+ "hidden_act": "silu",
15
+ "hidden_size": 2048,
16
+ "im_end_token": 151858,
17
+ "im_patch_token": 151859,
18
+ "im_start_token": 151857,
19
+ "initializer_range": 0.02,
20
+ "intermediate_size": 11008,
21
+ "latent_token_len": 32,
22
+ "max_position_embeddings": 32768,
23
+ "max_window_layers": 36,
24
+ "model_type": "C3",
25
+ "num_attention_heads": 16,
26
+ "num_hidden_layers": 36,
27
+ "num_key_value_heads": 2,
28
+ "rms_norm_eps": 1e-06,
29
+ "rope_scaling": null,
30
+ "rope_theta": 1000000.0,
31
+ "sliding_window": null,
32
+ "tie_word_embeddings": true,
33
+ "torch_dtype": "bfloat16",
34
+ "transformers_version": "4.49.0.dev0",
35
+ "use_cache": true,
36
+ "use_im_start_end": true,
37
+ "use_mrope": false,
38
+ "use_sliding_window": false,
39
+ "vocab_size": 151860
40
+ }
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "eos_token_id": 151643,
4
+ "max_new_tokens": 2048,
5
+ "transformers_version": "4.49.0.dev0"
6
+ }
llm1/config.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/mnt/dolphinfs/ssd_pool/docker/user/hadoop-basecv-hl/hadoop-basecv/user/liufanfan/MM_out/C3_latent32 /checkpoint-1/llm1",
3
+ "architectures": [
4
+ "Qwen2ForCausalLM"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 151643,
8
+ "eos_token_id": 151643,
9
+ "hidden_act": "silu",
10
+ "hidden_size": 1536,
11
+ "initializer_range": 0.02,
12
+ "intermediate_size": 8960,
13
+ "max_position_embeddings": 32768,
14
+ "max_window_layers": 28,
15
+ "model_type": "qwen2",
16
+ "num_attention_heads": 12,
17
+ "num_hidden_layers": 28,
18
+ "num_key_value_heads": 2,
19
+ "rms_norm_eps": 1e-06,
20
+ "rope_scaling": null,
21
+ "rope_theta": 1000000.0,
22
+ "sliding_window": null,
23
+ "tie_word_embeddings": true,
24
+ "torch_dtype": "bfloat16",
25
+ "transformers_version": "4.49.0.dev0",
26
+ "use_cache": true,
27
+ "use_mrope": false,
28
+ "use_sliding_window": false,
29
+ "vocab_size": 151936
30
+ }
llm1/generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 151643,
3
+ "eos_token_id": 151643,
4
+ "max_new_tokens": 2048,
5
+ "transformers_version": "4.49.0.dev0"
6
+ }
llm1/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:394aeb11953f424f18a19e867fa8f6c35c9b6c8324ece3e9d1bc4ea2d2f5b34a
3
+ size 3087467144
llm1/qwen.tiktoken ADDED
The diff for this file is too large to render. See raw diff
 
llm1/special_tokens_map.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "pad_token": {
3
+ "content": "<|endoftext|>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ }
9
+ }
llm1/tokenization_qwen.py ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Alibaba Cloud.
2
+ #
3
+ # This source code is licensed under the license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ """Tokenization classes for QWen."""
7
+
8
+ import base64
9
+ import logging
10
+ import os
11
+ import unicodedata
12
+ from typing import Collection, Dict, List, Set, Tuple, Union
13
+
14
+ import tiktoken
15
+ from transformers import PreTrainedTokenizer, AddedToken
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ VOCAB_FILES_NAMES = {"vocab_file": "qwen.tiktoken"}
21
+
22
+ 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+"""
23
+ ENDOFTEXT = "<|endoftext|>"
24
+ IMSTART = "<|im_start|>"
25
+ IMEND = "<|im_end|>"
26
+ # as the default behavior is changed to allow special tokens in
27
+ # regular texts, the surface forms of special tokens need to be
28
+ # as different as possible to minimize the impact
29
+ EXTRAS = tuple((f"<|extra_{i}|>" for i in range(205)))
30
+ SPECIAL_TOKENS = (
31
+ ENDOFTEXT,
32
+ IMSTART,
33
+ IMEND,
34
+ ) + EXTRAS
35
+
36
+
37
+ def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]:
38
+ with open(tiktoken_bpe_file, "rb") as f:
39
+ contents = f.read()
40
+ return {
41
+ base64.b64decode(token): int(rank)
42
+ for token, rank in (line.split() for line in contents.splitlines() if line)
43
+ }
44
+
45
+ class QWenTokenizer(PreTrainedTokenizer):
46
+ """QWen tokenizer."""
47
+
48
+ vocab_files_names = VOCAB_FILES_NAMES
49
+
50
+ def __init__(
51
+ self,
52
+ vocab_file,
53
+ errors="replace",
54
+ image_start_tag='<img>',
55
+ image_end_tag='</img>',
56
+ image_pad_tag='<imgpad>',
57
+ ref_start_tag='<ref>',
58
+ ref_end_tag='</ref>',
59
+ box_start_tag='<box>',
60
+ box_end_tag='</box>',
61
+ quad_start_tag='<quad>',
62
+ quad_end_tag='</quad>',
63
+ **kwargs,
64
+ ):
65
+ super().__init__(**kwargs)
66
+
67
+ self.image_start_tag = image_start_tag
68
+ self.image_end_tag = image_end_tag
69
+ self.image_pad_tag = image_pad_tag
70
+ self.ref_start_tag = ref_start_tag
71
+ self.ref_end_tag = ref_end_tag
72
+ self.box_start_tag = box_start_tag
73
+ self.box_end_tag = box_end_tag
74
+ self.quad_start_tag = quad_start_tag
75
+ self.quad_end_tag = quad_end_tag
76
+ self.IMAGE_ST = (
77
+ ref_start_tag, ref_end_tag,
78
+ box_start_tag, box_end_tag,
79
+ quad_start_tag, quad_end_tag,
80
+ image_start_tag, image_end_tag,
81
+ image_pad_tag
82
+ )
83
+
84
+ self.errors = errors # how to handle errors in decoding
85
+
86
+ self.mergeable_ranks = _load_tiktoken_bpe(vocab_file) # type: dict[bytes, int]
87
+ self.special_tokens = {
88
+ token: index
89
+ for index, token in enumerate(
90
+ SPECIAL_TOKENS + self.IMAGE_ST, start=len(self.mergeable_ranks)
91
+ )
92
+ }
93
+
94
+ self.img_start_id = self.special_tokens[self.image_start_tag]
95
+ self.img_end_id = self.special_tokens[self.image_end_tag]
96
+ self.img_pad_id = self.special_tokens[self.image_pad_tag]
97
+ self.ref_start_id = self.special_tokens[self.ref_start_tag]
98
+ self.ref_end_id = self.special_tokens[self.ref_end_tag]
99
+ self.box_start_id = self.special_tokens[self.box_start_tag]
100
+ self.box_end_id = self.special_tokens[self.box_end_tag]
101
+ self.quad_start_id = self.special_tokens[self.quad_start_tag]
102
+ self.quad_end_id = self.special_tokens[self.quad_end_tag]
103
+
104
+ enc = tiktoken.Encoding(
105
+ "Qwen",
106
+ pat_str=PAT_STR,
107
+ mergeable_ranks=self.mergeable_ranks,
108
+ special_tokens=self.special_tokens,
109
+ )
110
+ assert (
111
+ len(self.mergeable_ranks) + len(self.special_tokens) == enc.n_vocab
112
+ ), f"{len(self.mergeable_ranks) + len(self.special_tokens)} != {enc.n_vocab} in encoding"
113
+
114
+ self.decoder = {
115
+ v: k for k, v in self.mergeable_ranks.items()
116
+ } # type: dict[int, bytes|str]
117
+ self.decoder.update({v: k for k, v in self.special_tokens.items()})
118
+
119
+ self.tokenizer = enc # type: tiktoken.Encoding
120
+
121
+ self.eod_id = self.tokenizer.eot_token
122
+ self.im_start_id = self.special_tokens[IMSTART]
123
+ self.im_end_id = self.special_tokens[IMEND]
124
+
125
+ def __len__(self) -> int:
126
+ return self.tokenizer.n_vocab
127
+
128
+ def get_vocab(self) -> Dict[bytes, int]:
129
+ return self.mergeable_ranks
130
+
131
+ def convert_tokens_to_ids(
132
+ self, tokens: Union[bytes, str, List[Union[bytes, str]]]
133
+ ) -> List[int]:
134
+ ids = []
135
+ if isinstance(tokens, (str, bytes)):
136
+ if tokens in self.special_tokens:
137
+ return self.special_tokens[tokens]
138
+ else:
139
+ return self.mergeable_ranks.get(tokens)
140
+ for token in tokens:
141
+ if token in self.special_tokens:
142
+ ids.append(self.special_tokens[token])
143
+ else:
144
+ ids.append(self.mergeable_ranks.get(token))
145
+ return ids
146
+
147
+ def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int:
148
+ if not special_tokens and new_tokens:
149
+ raise ValueError('Adding regular tokens is not supported')
150
+ for token in new_tokens:
151
+ surface_form = token.content if isinstance(token, AddedToken) else token
152
+ if surface_form not in SPECIAL_TOKENS:
153
+ raise ValueError('Adding unknown special tokens is not supported')
154
+ return 0
155
+
156
+ def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]:
157
+ """
158
+ Save only the vocabulary of the tokenizer (vocabulary).
159
+
160
+ Returns:
161
+ `Tuple(str)`: Paths to the files saved.
162
+ """
163
+ file_path = os.path.join(save_directory, "qwen.tiktoken")
164
+ with open(file_path, "w", encoding="utf8") as w:
165
+ for k, v in self.mergeable_ranks.items():
166
+ line = base64.b64encode(k).decode("utf8") + " " + str(v) + "\n"
167
+ w.write(line)
168
+ return (file_path,)
169
+
170
+ def tokenize(
171
+ self,
172
+ text: str,
173
+ allowed_special: Union[Set, str] = "all",
174
+ disallowed_special: Union[Collection, str] = (),
175
+ **kwargs,
176
+ ) -> List[Union[bytes, str]]:
177
+ """
178
+ Converts a string in a sequence of tokens.
179
+
180
+ Args:
181
+ text (`str`):
182
+ The sequence to be encoded.
183
+ allowed_special (`Literal["all"]` or `set`):
184
+ The surface forms of the tokens to be encoded as special tokens in regular texts.
185
+ Default to "all".
186
+ disallowed_special (`Literal["all"]` or `Collection`):
187
+ The surface forms of the tokens that should not be in regular texts and trigger errors.
188
+ Default to an empty tuple.
189
+
190
+ kwargs (additional keyword arguments, *optional*):
191
+ Will be passed to the underlying model specific encode method.
192
+
193
+ Returns:
194
+ `List[bytes|str]`: The list of tokens.
195
+ """
196
+ tokens = []
197
+ text = unicodedata.normalize("NFC", text)
198
+
199
+ # this implementation takes a detour: text -> token id -> token surface forms
200
+ for t in self.tokenizer.encode(
201
+ text, allowed_special=allowed_special, disallowed_special=disallowed_special
202
+ ):
203
+ tokens.append(self.decoder[t])
204
+ return tokens
205
+
206
+ def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str:
207
+ """
208
+ Converts a sequence of tokens in a single string.
209
+ """
210
+ text = ""
211
+ temp = b""
212
+ for t in tokens:
213
+ if isinstance(t, str):
214
+ if temp:
215
+ text += temp.decode("utf-8", errors=self.errors)
216
+ temp = b""
217
+ text += t
218
+ elif isinstance(t, bytes):
219
+ temp += t
220
+ else:
221
+ raise TypeError("token should only be of type types or str")
222
+ if temp:
223
+ text += temp.decode("utf-8", errors=self.errors)
224
+ return text
225
+
226
+ @property
227
+ def vocab_size(self):
228
+ return self.tokenizer.n_vocab
229
+
230
+ def _convert_id_to_token(self, index: int) -> Union[bytes, str]:
231
+ """Converts an id to a token, special tokens included"""
232
+ if index in self.decoder:
233
+ return self.decoder[index]
234
+ raise ValueError("unknown ids")
235
+
236
+ def _convert_token_to_id(self, token: Union[bytes, str]) -> int:
237
+ """Converts a token to an id using the vocab, special tokens included"""
238
+ if token in self.special_tokens:
239
+ return self.special_tokens[token]
240
+ if token in self.mergeable_ranks:
241
+ return self.mergeable_ranks[token]
242
+ raise ValueError("unknown token")
243
+
244
+ def _tokenize(self, text: str, **kwargs):
245
+ """
246
+ Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based
247
+ vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
248
+
249
+ Do NOT take care of added tokens.
250
+ """
251
+ raise NotImplementedError
252
+
253
+ def _decode(
254
+ self,
255
+ token_ids: Union[int, List[int]],
256
+ skip_special_tokens: bool = False,
257
+ errors: str = None,
258
+ **kwargs,
259
+ ) -> str:
260
+ if isinstance(token_ids, int):
261
+ token_ids = [token_ids]
262
+ if skip_special_tokens:
263
+ token_ids = [i for i in token_ids if i < self.eod_id]
264
+ return self.tokenizer.decode(token_ids, errors=errors or self.errors)
llm1/tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {},
3
+ "auto_map": {
4
+ "AutoTokenizer": [
5
+ "tokenization_qwen.QWenTokenizer",
6
+ null
7
+ ]
8
+ },
9
+ "clean_up_tokenization_spaces": true,
10
+ "extra_special_tokens": {},
11
+ "model_max_length": 81920,
12
+ "pad_token": "<|endoftext|>",
13
+ "padding_side": "right",
14
+ "tokenizer_class": "QWenTokenizer"
15
+ }
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2c2d42c312c0bbd73c9c7c47ab50cd6cc69e49f4452ee05506c882433186bbb0
3
+ size 4957249008
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f5f1956104dd7c668ac4828ebe49567347bff01d03846de6d5489721ff9fc811
3
+ size 1220760840
model.safetensors.index.json ADDED
@@ -0,0 +1,444 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 6177959936
4
+ },
5
+ "weight_map": {
6
+ "model.Q.weight": "model-00002-of-00002.safetensors",
7
+ "model.embed_tokens.weight": "model-00001-of-00002.safetensors",
8
+ "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors",
9
+ "model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
10
+ "model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
11
+ "model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
12
+ "model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
13
+ "model.layers.0.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
14
+ "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
15
+ "model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
16
+ "model.layers.0.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
17
+ "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
18
+ "model.layers.0.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
19
+ "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
20
+ "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors",
21
+ "model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
22
+ "model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
23
+ "model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
24
+ "model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
25
+ "model.layers.1.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
26
+ "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
27
+ "model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
28
+ "model.layers.1.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
29
+ "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
30
+ "model.layers.1.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
31
+ "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
32
+ "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors",
33
+ "model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
34
+ "model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
35
+ "model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
36
+ "model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
37
+ "model.layers.10.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
38
+ "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
39
+ "model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
40
+ "model.layers.10.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
41
+ "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
42
+ "model.layers.10.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
43
+ "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
44
+ "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors",
45
+ "model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
46
+ "model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
47
+ "model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
48
+ "model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
49
+ "model.layers.11.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
50
+ "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
51
+ "model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
52
+ "model.layers.11.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
53
+ "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
54
+ "model.layers.11.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
55
+ "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
56
+ "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors",
57
+ "model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
58
+ "model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
59
+ "model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
60
+ "model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
61
+ "model.layers.12.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
62
+ "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
63
+ "model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
64
+ "model.layers.12.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
65
+ "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
66
+ "model.layers.12.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
67
+ "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
68
+ "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors",
69
+ "model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
70
+ "model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
71
+ "model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
72
+ "model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
73
+ "model.layers.13.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
74
+ "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
75
+ "model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
76
+ "model.layers.13.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
77
+ "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
78
+ "model.layers.13.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
79
+ "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
80
+ "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
81
+ "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
82
+ "model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
83
+ "model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
84
+ "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
85
+ "model.layers.14.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
86
+ "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
87
+ "model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
88
+ "model.layers.14.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
89
+ "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
90
+ "model.layers.14.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
91
+ "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
92
+ "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
93
+ "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
94
+ "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
95
+ "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
96
+ "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
97
+ "model.layers.15.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
98
+ "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
99
+ "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
100
+ "model.layers.15.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
101
+ "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
102
+ "model.layers.15.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
103
+ "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
104
+ "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
105
+ "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
106
+ "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
107
+ "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
108
+ "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
109
+ "model.layers.16.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
110
+ "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
111
+ "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
112
+ "model.layers.16.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
113
+ "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
114
+ "model.layers.16.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
115
+ "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
116
+ "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
117
+ "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
118
+ "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
119
+ "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
120
+ "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
121
+ "model.layers.17.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
122
+ "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
123
+ "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
124
+ "model.layers.17.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
125
+ "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
126
+ "model.layers.17.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
127
+ "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
128
+ "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors",
129
+ "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
130
+ "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
131
+ "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
132
+ "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
133
+ "model.layers.18.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
134
+ "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
135
+ "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
136
+ "model.layers.18.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
137
+ "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
138
+ "model.layers.18.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
139
+ "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
140
+ "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors",
141
+ "model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
142
+ "model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
143
+ "model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
144
+ "model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
145
+ "model.layers.19.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
146
+ "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
147
+ "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
148
+ "model.layers.19.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
149
+ "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
150
+ "model.layers.19.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
151
+ "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
152
+ "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors",
153
+ "model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
154
+ "model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
155
+ "model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
156
+ "model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
157
+ "model.layers.2.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
158
+ "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
159
+ "model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
160
+ "model.layers.2.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
161
+ "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
162
+ "model.layers.2.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
163
+ "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
164
+ "model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors",
165
+ "model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
166
+ "model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
167
+ "model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
168
+ "model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
169
+ "model.layers.20.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
170
+ "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
171
+ "model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
172
+ "model.layers.20.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
173
+ "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
174
+ "model.layers.20.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
175
+ "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
176
+ "model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors",
177
+ "model.layers.21.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
178
+ "model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
179
+ "model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
180
+ "model.layers.21.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
181
+ "model.layers.21.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
182
+ "model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
183
+ "model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
184
+ "model.layers.21.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
185
+ "model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
186
+ "model.layers.21.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
187
+ "model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
188
+ "model.layers.22.input_layernorm.weight": "model-00001-of-00002.safetensors",
189
+ "model.layers.22.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
190
+ "model.layers.22.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
191
+ "model.layers.22.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
192
+ "model.layers.22.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
193
+ "model.layers.22.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
194
+ "model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
195
+ "model.layers.22.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
196
+ "model.layers.22.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
197
+ "model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
198
+ "model.layers.22.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
199
+ "model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
200
+ "model.layers.23.input_layernorm.weight": "model-00001-of-00002.safetensors",
201
+ "model.layers.23.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
202
+ "model.layers.23.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
203
+ "model.layers.23.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
204
+ "model.layers.23.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
205
+ "model.layers.23.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
206
+ "model.layers.23.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
207
+ "model.layers.23.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
208
+ "model.layers.23.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
209
+ "model.layers.23.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
210
+ "model.layers.23.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
211
+ "model.layers.23.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
212
+ "model.layers.24.input_layernorm.weight": "model-00001-of-00002.safetensors",
213
+ "model.layers.24.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
214
+ "model.layers.24.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
215
+ "model.layers.24.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
216
+ "model.layers.24.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
217
+ "model.layers.24.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
218
+ "model.layers.24.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
219
+ "model.layers.24.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
220
+ "model.layers.24.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
221
+ "model.layers.24.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
222
+ "model.layers.24.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
223
+ "model.layers.24.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
224
+ "model.layers.25.input_layernorm.weight": "model-00001-of-00002.safetensors",
225
+ "model.layers.25.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
226
+ "model.layers.25.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
227
+ "model.layers.25.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
228
+ "model.layers.25.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
229
+ "model.layers.25.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
230
+ "model.layers.25.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
231
+ "model.layers.25.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
232
+ "model.layers.25.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
233
+ "model.layers.25.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
234
+ "model.layers.25.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
235
+ "model.layers.25.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
236
+ "model.layers.26.input_layernorm.weight": "model-00001-of-00002.safetensors",
237
+ "model.layers.26.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
238
+ "model.layers.26.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
239
+ "model.layers.26.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
240
+ "model.layers.26.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
241
+ "model.layers.26.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
242
+ "model.layers.26.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
243
+ "model.layers.26.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
244
+ "model.layers.26.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
245
+ "model.layers.26.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
246
+ "model.layers.26.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
247
+ "model.layers.26.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
248
+ "model.layers.27.input_layernorm.weight": "model-00001-of-00002.safetensors",
249
+ "model.layers.27.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
250
+ "model.layers.27.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
251
+ "model.layers.27.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
252
+ "model.layers.27.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
253
+ "model.layers.27.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
254
+ "model.layers.27.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
255
+ "model.layers.27.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
256
+ "model.layers.27.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
257
+ "model.layers.27.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
258
+ "model.layers.27.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
259
+ "model.layers.27.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
260
+ "model.layers.28.input_layernorm.weight": "model-00002-of-00002.safetensors",
261
+ "model.layers.28.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
262
+ "model.layers.28.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
263
+ "model.layers.28.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
264
+ "model.layers.28.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
265
+ "model.layers.28.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
266
+ "model.layers.28.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
267
+ "model.layers.28.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
268
+ "model.layers.28.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
269
+ "model.layers.28.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
270
+ "model.layers.28.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
271
+ "model.layers.28.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
272
+ "model.layers.29.input_layernorm.weight": "model-00002-of-00002.safetensors",
273
+ "model.layers.29.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
274
+ "model.layers.29.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
275
+ "model.layers.29.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
276
+ "model.layers.29.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
277
+ "model.layers.29.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
278
+ "model.layers.29.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
279
+ "model.layers.29.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
280
+ "model.layers.29.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
281
+ "model.layers.29.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
282
+ "model.layers.29.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
283
+ "model.layers.29.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
284
+ "model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors",
285
+ "model.layers.3.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
286
+ "model.layers.3.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
287
+ "model.layers.3.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
288
+ "model.layers.3.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
289
+ "model.layers.3.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
290
+ "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
291
+ "model.layers.3.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
292
+ "model.layers.3.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
293
+ "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
294
+ "model.layers.3.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
295
+ "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
296
+ "model.layers.30.input_layernorm.weight": "model-00002-of-00002.safetensors",
297
+ "model.layers.30.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
298
+ "model.layers.30.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
299
+ "model.layers.30.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
300
+ "model.layers.30.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
301
+ "model.layers.30.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
302
+ "model.layers.30.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
303
+ "model.layers.30.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
304
+ "model.layers.30.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
305
+ "model.layers.30.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
306
+ "model.layers.30.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
307
+ "model.layers.30.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
308
+ "model.layers.31.input_layernorm.weight": "model-00002-of-00002.safetensors",
309
+ "model.layers.31.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
310
+ "model.layers.31.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
311
+ "model.layers.31.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
312
+ "model.layers.31.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
313
+ "model.layers.31.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
314
+ "model.layers.31.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
315
+ "model.layers.31.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
316
+ "model.layers.31.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
317
+ "model.layers.31.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
318
+ "model.layers.31.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
319
+ "model.layers.31.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
320
+ "model.layers.32.input_layernorm.weight": "model-00002-of-00002.safetensors",
321
+ "model.layers.32.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
322
+ "model.layers.32.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
323
+ "model.layers.32.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
324
+ "model.layers.32.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
325
+ "model.layers.32.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
326
+ "model.layers.32.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
327
+ "model.layers.32.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
328
+ "model.layers.32.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
329
+ "model.layers.32.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
330
+ "model.layers.32.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
331
+ "model.layers.32.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
332
+ "model.layers.33.input_layernorm.weight": "model-00002-of-00002.safetensors",
333
+ "model.layers.33.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
334
+ "model.layers.33.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
335
+ "model.layers.33.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
336
+ "model.layers.33.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
337
+ "model.layers.33.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
338
+ "model.layers.33.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
339
+ "model.layers.33.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
340
+ "model.layers.33.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
341
+ "model.layers.33.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
342
+ "model.layers.33.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
343
+ "model.layers.33.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
344
+ "model.layers.34.input_layernorm.weight": "model-00002-of-00002.safetensors",
345
+ "model.layers.34.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
346
+ "model.layers.34.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
347
+ "model.layers.34.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
348
+ "model.layers.34.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
349
+ "model.layers.34.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
350
+ "model.layers.34.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
351
+ "model.layers.34.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
352
+ "model.layers.34.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
353
+ "model.layers.34.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
354
+ "model.layers.34.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
355
+ "model.layers.34.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
356
+ "model.layers.35.input_layernorm.weight": "model-00002-of-00002.safetensors",
357
+ "model.layers.35.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
358
+ "model.layers.35.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
359
+ "model.layers.35.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
360
+ "model.layers.35.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
361
+ "model.layers.35.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
362
+ "model.layers.35.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
363
+ "model.layers.35.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
364
+ "model.layers.35.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
365
+ "model.layers.35.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
366
+ "model.layers.35.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
367
+ "model.layers.35.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
368
+ "model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors",
369
+ "model.layers.4.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
370
+ "model.layers.4.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
371
+ "model.layers.4.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
372
+ "model.layers.4.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
373
+ "model.layers.4.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
374
+ "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
375
+ "model.layers.4.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
376
+ "model.layers.4.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
377
+ "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
378
+ "model.layers.4.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
379
+ "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
380
+ "model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors",
381
+ "model.layers.5.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
382
+ "model.layers.5.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
383
+ "model.layers.5.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
384
+ "model.layers.5.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
385
+ "model.layers.5.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
386
+ "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
387
+ "model.layers.5.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
388
+ "model.layers.5.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
389
+ "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
390
+ "model.layers.5.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
391
+ "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
392
+ "model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors",
393
+ "model.layers.6.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
394
+ "model.layers.6.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
395
+ "model.layers.6.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
396
+ "model.layers.6.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
397
+ "model.layers.6.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
398
+ "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
399
+ "model.layers.6.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
400
+ "model.layers.6.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
401
+ "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
402
+ "model.layers.6.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
403
+ "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
404
+ "model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors",
405
+ "model.layers.7.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
406
+ "model.layers.7.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
407
+ "model.layers.7.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
408
+ "model.layers.7.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
409
+ "model.layers.7.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
410
+ "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
411
+ "model.layers.7.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
412
+ "model.layers.7.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
413
+ "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
414
+ "model.layers.7.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
415
+ "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
416
+ "model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors",
417
+ "model.layers.8.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
418
+ "model.layers.8.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
419
+ "model.layers.8.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
420
+ "model.layers.8.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
421
+ "model.layers.8.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
422
+ "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
423
+ "model.layers.8.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
424
+ "model.layers.8.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
425
+ "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
426
+ "model.layers.8.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
427
+ "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
428
+ "model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors",
429
+ "model.layers.9.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
430
+ "model.layers.9.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
431
+ "model.layers.9.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
432
+ "model.layers.9.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
433
+ "model.layers.9.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
434
+ "model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
435
+ "model.layers.9.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
436
+ "model.layers.9.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
437
+ "model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
438
+ "model.layers.9.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
439
+ "model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
440
+ "model.mm_projector.bias": "model-00002-of-00002.safetensors",
441
+ "model.mm_projector.weight": "model-00002-of-00002.safetensors",
442
+ "model.norm.weight": "model-00002-of-00002.safetensors"
443
+ }
444
+ }
modeling_C3.py ADDED
@@ -0,0 +1,621 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoConfig, AutoModelForCausalLM, \
2
+ Qwen2Config, Qwen2Model, Qwen2ForCausalLM, \
3
+ CLIPVisionModel, CLIPImageProcessor
4
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
5
+ from typing import List, Optional, Tuple, Union
6
+ from transformers.cache_utils import Cache, DynamicCache
7
+ import torch
8
+ import torch.nn as nn
9
+ import torch.nn.functional as F
10
+ from torch.nn import CrossEntropyLoss
11
+ import os
12
+
13
+ import dataclasses
14
+ from enum import auto, Enum
15
+ from typing import List, Tuple
16
+ from transformers import StoppingCriteria
17
+ from transformers import TextStreamer
18
+
19
+ class SeparatorStyle(Enum):
20
+ """Different separator style."""
21
+ SINGLE = auto()
22
+ TWO = auto()
23
+ MPT = auto()
24
+
25
+
26
+ @dataclasses.dataclass
27
+ class Conversation:
28
+ """A class that keeps all conversation history."""
29
+ system: str
30
+ roles: List[str]
31
+ messages: List[List[str]]
32
+ offset: int
33
+ sep_style: SeparatorStyle = SeparatorStyle.SINGLE
34
+ sep: str = "<|im_end|>"
35
+ sep2: str = None
36
+ version: str = "Unknown"
37
+
38
+ skip_next: bool = False
39
+
40
+ def get_prompt(self):
41
+ if self.sep_style == SeparatorStyle.SINGLE:
42
+ ret = self.system + self.sep + '\n'
43
+ for role, message in self.messages:
44
+ if message:
45
+ if type(message) is tuple:
46
+ message, _, _ = message
47
+ ret += role + ": " + message + self.sep
48
+ else:
49
+ ret += role + ":"
50
+ return ret
51
+ elif self.sep_style == SeparatorStyle.TWO:
52
+ seps = [self.sep, self.sep2]
53
+ ret = self.system + seps[0]
54
+ for i, (role, message) in enumerate(self.messages):
55
+ if message:
56
+ if type(message) is tuple:
57
+ message, _, _ = message
58
+ ret += role + ": " + message + seps[i % 2]
59
+ else:
60
+ ret += role + ":"
61
+ return ret
62
+ if self.sep_style == SeparatorStyle.MPT:
63
+ if self.system:
64
+ ret = self.system + self.sep
65
+ else:
66
+ ret = ''
67
+ for role, message in self.messages:
68
+ if message:
69
+ if type(message) is tuple:
70
+ message, _, _ = message
71
+ ret += role + message + self.sep
72
+ else:
73
+ ret += role
74
+ return ret
75
+ else:
76
+ raise ValueError(f"Invalid style: {self.sep_style}")
77
+
78
+ def append_message(self, role, message):
79
+ self.messages.append([role, message])
80
+
81
+ def get_images(self, return_pil=False):
82
+ images = []
83
+ for i, (role, msg) in enumerate(self.messages[self.offset:]):
84
+ if i % 2 == 0:
85
+ if type(msg) is tuple:
86
+ import base64
87
+ from io import BytesIO
88
+ from PIL import Image
89
+ msg, image, image_process_mode = msg
90
+ if image_process_mode == "Pad":
91
+ def expand2square(pil_img, background_color=(122, 116, 104)):
92
+ width, height = pil_img.size
93
+ if width == height:
94
+ return pil_img
95
+ elif width > height:
96
+ result = Image.new(pil_img.mode, (width, width), background_color)
97
+ # result.paste(pil_img, (0, (width - height) // 2))
98
+ result.paste(pil_img)
99
+ return result
100
+ else:
101
+ result = Image.new(pil_img.mode, (height, height), background_color)
102
+ # result.paste(pil_img, ((height - width) // 2, 0))
103
+ result.paste(pil_img)
104
+ return result
105
+ image = expand2square(image)
106
+ elif image_process_mode == "Crop":
107
+ max_hw, min_hw = max(image.size), min(image.size)
108
+ aspect_ratio = max_hw / min_hw
109
+ max_len, min_len = 800, 400
110
+ shortest_edge = int(min(max_len / aspect_ratio, min_len, min_hw))
111
+ longest_edge = int(shortest_edge * aspect_ratio)
112
+ W, H = image.size
113
+ if H > W:
114
+ H, W = longest_edge, shortest_edge
115
+ else:
116
+ H, W = shortest_edge, longest_edge
117
+ image = image.resize((W, H))
118
+ elif image_process_mode == "Resize":
119
+ image = image.resize((224, 224))
120
+ else:
121
+ raise ValueError(f"Invalid image_process_mode: {image_process_mode}")
122
+
123
+ if return_pil:
124
+ images.append(image)
125
+ else:
126
+ buffered = BytesIO()
127
+ image.convert('RGB').save(buffered, format="JPEG")
128
+ img_b64_str = base64.b64encode(buffered.getvalue()).decode()
129
+ images.append(img_b64_str)
130
+ return images
131
+
132
+ def to_gradio_chatbot(self):
133
+ ret = []
134
+ for i, (role, msg) in enumerate(self.messages[self.offset:]):
135
+ if i % 2 == 0:
136
+ if type(msg) is tuple:
137
+ import base64
138
+ from io import BytesIO
139
+ msg, image, image_process_mode = msg
140
+ max_hw, min_hw = max(image.size), min(image.size)
141
+ aspect_ratio = max_hw / min_hw
142
+ max_len, min_len = 800, 400
143
+ shortest_edge = int(min(max_len / aspect_ratio, min_len, min_hw))
144
+ longest_edge = int(shortest_edge * aspect_ratio)
145
+ W, H = image.size
146
+ if H > W:
147
+ H, W = longest_edge, shortest_edge
148
+ else:
149
+ H, W = shortest_edge, longest_edge
150
+ image = image.resize((W, H))
151
+ # image = image.resize((224, 224))
152
+ buffered = BytesIO()
153
+ image.save(buffered, format="JPEG")
154
+ img_b64_str = base64.b64encode(buffered.getvalue()).decode()
155
+ img_str = f'<img src="data:image/png;base64,{img_b64_str}" alt="user upload image" />'
156
+ msg = msg.replace('<image>', img_str)
157
+ ret.append([msg, None])
158
+ else:
159
+ ret[-1][-1] = msg
160
+ return ret
161
+
162
+ def copy(self):
163
+ return Conversation(
164
+ system=self.system,
165
+ roles=self.roles,
166
+ messages=[[x, y] for x, y in self.messages],
167
+ offset=self.offset,
168
+ sep_style=self.sep_style,
169
+ sep=self.sep,
170
+ sep2=self.sep2)
171
+
172
+ def dict(self):
173
+ if len(self.get_images()) > 0:
174
+ return {
175
+ "system": self.system,
176
+ "roles": self.roles,
177
+ "messages": [[x, y[0] if type(y) is tuple else y] for x, y in self.messages],
178
+ "offset": self.offset,
179
+ "sep": self.sep,
180
+ "sep2": self.sep2,
181
+ }
182
+ return {
183
+ "system": self.system,
184
+ "roles": self.roles,
185
+ "messages": self.messages,
186
+ "offset": self.offset,
187
+ "sep": self.sep,
188
+ "sep2": self.sep2,
189
+ }
190
+
191
+
192
+ conv_mpt = Conversation(
193
+ system="""<|im_start|>system
194
+ You should follow the instructions carefully and explain your answers in detail.""",
195
+ # system = None,
196
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
197
+ version="mpt",
198
+ messages=(),
199
+ offset=0,
200
+ sep_style=SeparatorStyle.MPT,
201
+ sep="<|im_end|>",
202
+ )
203
+
204
+ conv_templates = {
205
+
206
+ "mpt": conv_mpt,
207
+
208
+ }
209
+
210
+
211
+
212
+
213
+ class KeywordsStoppingCriteria(StoppingCriteria):
214
+ def __init__(self, keywords, tokenizer, input_ids):
215
+ self.keywords = keywords
216
+ self.keyword_ids = [tokenizer(keyword).input_ids for keyword in keywords]
217
+ self.keyword_ids = [keyword_id[0] for keyword_id in self.keyword_ids if type(keyword_id) is list and len(keyword_id) == 1]
218
+ self.tokenizer = tokenizer
219
+ self.start_len = None
220
+ self.input_ids = input_ids
221
+
222
+ def __call__(self, output_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
223
+ if self.start_len is None:
224
+ self.start_len = self.input_ids.shape[1]
225
+ else:
226
+ for keyword_id in self.keyword_ids:
227
+ if output_ids[0, -1] == keyword_id:
228
+ return True
229
+ outputs = self.tokenizer.batch_decode(output_ids[:, self.start_len:], skip_special_tokens=True)[0]
230
+ for keyword in self.keywords:
231
+ if keyword in outputs:
232
+ return True
233
+ return False
234
+
235
+
236
+ DEFAULT_IMAGE_PATCH_TOKEN = '<imgpad>'
237
+ DEFAULT_IM_START_TOKEN = '<img>'
238
+ DEFAULT_IM_END_TOKEN = '</img>'
239
+
240
+ class C3Config(Qwen2Config):
241
+ model_type = "C3"
242
+
243
+
244
+ class C3QwenModel(Qwen2Model):
245
+ config_class = C3Config
246
+
247
+ def __init__(self, config: Qwen2Config):
248
+ super(C3QwenModel, self).__init__(config)
249
+
250
+ self.Q = nn.Embedding(config.latent_token_len , config.contexts_compression_llm_hidden_size)
251
+ self.mm_projector = nn.Linear(config.contexts_compression_llm_hidden_size, config.hidden_size)
252
+ self.llm1 = None
253
+ self.config.use_im_start_end = True
254
+
255
+ def forward(
256
+ self,
257
+ input_ids: torch.LongTensor = None,
258
+ context_ids: torch.LongTensor = None,
259
+ attention_mask: Optional[torch.Tensor] = None,
260
+ context_attention_mask: Optional[torch.Tensor] = None,
261
+ position_ids: Optional[torch.LongTensor] = None,
262
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
263
+ inputs_embeds: Optional[torch.FloatTensor] = None,
264
+ use_cache: Optional[bool] = None,
265
+ output_attentions: Optional[bool] = None,
266
+ output_hidden_states: Optional[bool] = None,
267
+ return_dict: Optional[bool] = None,
268
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
269
+
270
+ # HACK: replace back original embeddings for LLaVA pretraining
271
+ orig_embeds_params = getattr(self, 'orig_embeds_params', None)
272
+ if orig_embeds_params is not None:
273
+ with torch.no_grad():
274
+ self.get_input_embeddings().weight[:-self.num_new_tokens] = orig_embeds_params[:-self.num_new_tokens].data
275
+
276
+ if inputs_embeds is None:
277
+ inputs_embeds = self.embed_tokens(input_ids)
278
+
279
+ context_embeds = self.llm1.model.embed_tokens(context_ids)
280
+
281
+ #######encoder#######
282
+
283
+ if input_ids.shape[1] != 1 or self.training:
284
+ use_im_start_end = getattr(self.config, "use_im_start_end", -1)
285
+ im_patch_token = getattr(self.config, "im_patch_token", -1)
286
+ im_start_token = getattr(self.config, "im_start_token", -1)
287
+ im_end_token = getattr(self.config, "im_end_token", -1)
288
+ context_features = []
289
+
290
+ for i in range(context_embeds.shape[0]):
291
+ context_features.append([self.Q.weight])
292
+
293
+
294
+ use_im_start_end = True
295
+ new_context_embeds = []
296
+ image_start_tokens_list = []
297
+ for cur_context_ids, cur_context_embeds, cur_context_features in zip(context_ids, context_embeds, context_features):
298
+
299
+ if use_im_start_end:
300
+ image_start_tokens = torch.where(cur_context_ids == im_start_token)[0]
301
+ image_start_tokens_list.append(image_start_tokens)
302
+
303
+ for image_start_token_pos, per_cur_image_features in zip(image_start_tokens, cur_context_features):
304
+ per_cur_image_features = per_cur_image_features.to(device=cur_context_embeds.device)
305
+ num_patches = per_cur_image_features.shape[0]
306
+ if cur_context_ids[image_start_token_pos + num_patches + 1] != im_end_token:
307
+ raise ValueError("The image end token should follow the image start token.")
308
+
309
+ cur_context_embeds = torch.cat(
310
+ (
311
+ cur_context_embeds[:image_start_token_pos+1],
312
+ per_cur_image_features,
313
+ cur_context_embeds[image_start_token_pos + num_patches + 1:]
314
+ ),
315
+ dim=0
316
+ )
317
+ new_context_embeds.append(cur_context_embeds)
318
+ else:
319
+ raise NotImplementedError
320
+
321
+ image_start_tokens_list = torch.tensor(image_start_tokens_list)
322
+
323
+ context_embeds = torch.stack(new_context_embeds, dim=0)
324
+ llm1_hidden_states = self.llm1.forward(
325
+ input_ids=None, attention_mask=context_attention_mask, past_key_values=None,
326
+ inputs_embeds=context_embeds, use_cache=None, position_ids = None,
327
+ output_attentions=output_attentions, output_hidden_states=True,
328
+ return_dict=return_dict
329
+ )['hidden_states'][-1]
330
+ latent_contexts = []
331
+ for i, llm1_hidden_state in enumerate(llm1_hidden_states):
332
+ image_start_token_pos = image_start_tokens_list[i]
333
+ llm1_hidden_state = llm1_hidden_state[image_start_token_pos+1:image_start_token_pos + num_patches+1]
334
+ latent_contexts.append(llm1_hidden_state)
335
+
336
+ ########decoder########
337
+ latent_features = []
338
+
339
+ for latent_context in latent_contexts:
340
+ latent_context = self.mm_projector(latent_context)
341
+ latent_features.append([latent_context])
342
+
343
+
344
+ new_input_embeds = []
345
+ for cur_input_ids, cur_input_embeds, cur_latent_features in zip(input_ids, inputs_embeds, latent_features):
346
+
347
+ if use_im_start_end:
348
+ if (cur_input_ids == im_start_token).sum() != (cur_input_ids == im_end_token).sum():
349
+ raise ValueError("The number of image start tokens and image end tokens should be the same.")
350
+ image_start_tokens = torch.where(cur_input_ids == im_start_token)[0]
351
+ for image_start_token_pos, per_cur_latent_features in zip(image_start_tokens, cur_latent_features):
352
+ per_cur_latent_features = per_cur_latent_features.to(device=cur_input_embeds.device)
353
+ num_patches = per_cur_latent_features.shape[0]
354
+ if cur_input_ids[image_start_token_pos + num_patches + 1] != im_end_token:
355
+ raise ValueError("The image end token should follow the image start token.")
356
+ cur_input_embeds = torch.cat(
357
+ (
358
+ cur_input_embeds[:image_start_token_pos+1],
359
+ per_cur_latent_features,
360
+ cur_input_embeds[image_start_token_pos + num_patches + 1:]
361
+ ),
362
+ dim=0
363
+ )
364
+ new_input_embeds.append(cur_input_embeds)
365
+ else:
366
+ raise NotImplementedError
367
+
368
+ inputs_embeds = torch.stack(new_input_embeds, dim=0)
369
+
370
+ return super(C3QwenModel, self).forward(
371
+ input_ids=None, attention_mask=attention_mask, past_key_values=past_key_values,
372
+ inputs_embeds=inputs_embeds, use_cache=use_cache, position_ids = position_ids,
373
+ output_attentions=output_attentions, output_hidden_states=output_hidden_states,
374
+ return_dict=return_dict
375
+ )
376
+
377
+
378
+
379
+ class C3QwenForCausalLM(Qwen2ForCausalLM):
380
+ config_class = C3Config
381
+ # supports_gradient_checkpointing = True
382
+
383
+ def __init__(self, config):
384
+ super(Qwen2ForCausalLM, self).__init__(config)
385
+ self.model = C3QwenModel(config)
386
+
387
+ self.vocab_size = config.vocab_size
388
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
389
+
390
+ # Initialize weights and apply final processing
391
+ self.post_init()
392
+
393
+ def get_model(self):
394
+ return self.model
395
+
396
+
397
+ def forward(
398
+ self,
399
+ input_ids: torch.LongTensor = None,
400
+ context_ids: torch.LongTensor = None,
401
+ attention_mask: Optional[torch.Tensor] = None,
402
+ context_attention_mask: Optional[torch.Tensor] = None,
403
+ position_ids: Optional[torch.LongTensor] = None,
404
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
405
+ inputs_embeds: Optional[torch.FloatTensor] = None,
406
+ labels: Optional[torch.LongTensor] = None,
407
+ use_cache: Optional[bool] = None,
408
+ output_attentions: Optional[bool] = None,
409
+ output_hidden_states: Optional[bool] = None,
410
+ return_dict: Optional[bool] = None,
411
+
412
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
413
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
414
+ output_hidden_states = (
415
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
416
+ )
417
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
418
+
419
+
420
+
421
+ outputs = self.model(
422
+ input_ids=input_ids,
423
+ context_ids=context_ids,
424
+ past_key_values=past_key_values,
425
+ attention_mask=attention_mask,
426
+ context_attention_mask=context_attention_mask,
427
+ position_ids=position_ids,
428
+ inputs_embeds=inputs_embeds,
429
+ use_cache=use_cache,
430
+ output_attentions=output_attentions,
431
+ output_hidden_states=output_hidden_states,
432
+ return_dict=return_dict
433
+ )
434
+
435
+
436
+ hidden_states = outputs[0]
437
+ logits = self.lm_head(hidden_states)
438
+ logits = logits.float()
439
+
440
+ # logits
441
+
442
+ loss = None
443
+ if labels is not None:
444
+ # Shift so that tokens < n predict n
445
+ shift_logits = logits[..., :-1, :].contiguous()
446
+ shift_labels = labels[..., 1:].contiguous()
447
+ # Flatten the tokens
448
+ loss_fct = CrossEntropyLoss()
449
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
450
+ shift_labels = shift_labels.view(-1)
451
+ # Enable model parallelism
452
+ shift_labels = shift_labels.to(shift_logits.device)
453
+ loss = loss_fct(shift_logits, shift_labels)
454
+
455
+ if not return_dict:
456
+ output = (logits,) + outputs[1:]
457
+ return (loss,) + output if loss is not None else output
458
+
459
+ return CausalLMOutputWithPast(
460
+ loss=loss,
461
+ logits=logits,
462
+ past_key_values=outputs.past_key_values,
463
+ hidden_states=outputs.hidden_states,
464
+ attentions=outputs.attentions,
465
+ )
466
+
467
+
468
+ def prepare_inputs_for_generation(
469
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
470
+ ):
471
+ # Omit tokens covered by past_key_values
472
+ if past_key_values is not None:
473
+ if isinstance(past_key_values, Cache):
474
+ cache_length = past_key_values.get_seq_length()
475
+ past_length = past_key_values.seen_tokens
476
+ #max_cache_length = past_key_values.get_max_length()
477
+ max_cache_length = None
478
+ else:
479
+ cache_length = past_length = past_key_values[0][0].shape[2]
480
+ max_cache_length = None
481
+
482
+ # Keep only the unprocessed tokens:
483
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
484
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
485
+ # input)
486
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
487
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
488
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
489
+ # input_ids based on the past_length.
490
+ elif past_length < input_ids.shape[1]:
491
+ input_ids = input_ids[:, past_length:]
492
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
493
+
494
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
495
+ if (
496
+ max_cache_length is not None
497
+ and attention_mask is not None
498
+ and cache_length + input_ids.shape[1] > max_cache_length
499
+ ):
500
+ attention_mask = attention_mask[:, -max_cache_length:]
501
+
502
+ position_ids = kwargs.get("position_ids", None)
503
+ if attention_mask is not None and position_ids is None:
504
+ # create position_ids on the fly for batch generation
505
+ position_ids = attention_mask.long().cumsum(-1) - 1
506
+ position_ids.masked_fill_(attention_mask == 0, 1)
507
+ if past_key_values:
508
+ position_ids = position_ids[:, -input_ids.shape[1] :]
509
+
510
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
511
+ if inputs_embeds is not None and past_key_values is None:
512
+ model_inputs = {"inputs_embeds": inputs_embeds}
513
+ else:
514
+ model_inputs = {"input_ids": input_ids}
515
+
516
+ model_inputs.update(
517
+ {
518
+ "position_ids": position_ids,
519
+ "past_key_values": past_key_values,
520
+ "use_cache": kwargs.get("use_cache"),
521
+ "attention_mask": attention_mask,
522
+ #"images": kwargs.get("images", None),
523
+ "context_ids": kwargs.get("context_ids", None),
524
+ }
525
+ )
526
+ return model_inputs
527
+
528
+ @classmethod
529
+ def from_pretrained(
530
+ cls,
531
+ pretrained_model_name_or_path,
532
+ *model_args,
533
+ **kwargs,
534
+ ):
535
+
536
+ model = super().from_pretrained(
537
+ pretrained_model_name_or_path, *model_args, **kwargs
538
+ )
539
+ llm1_path = os.path.join(pretrained_model_name_or_path, "llm1")
540
+ print(f"Loading llm1 from path: {llm1_path}")
541
+
542
+ dtype = kwargs.get("torch_dtype", torch.float16)
543
+ device = kwargs.get("device_map", "auto")
544
+
545
+ llm1 = Qwen2ForCausalLM.from_pretrained(
546
+ llm1_path,
547
+ use_safetensors=kwargs.get("use_safetensors", True),
548
+ torch_dtype=dtype,
549
+ device_map=device,
550
+ )
551
+ model.model.llm1 = llm1
552
+ print("Successfully loaded and attached llm1.")
553
+
554
+
555
+ return model
556
+
557
+ def initialize_special_tokenizer(
558
+ self,
559
+ tokenizer,
560
+ device="cuda"
561
+ ):
562
+ config = self.get_model().config
563
+ self.resize_token_embeddings(len(tokenizer))
564
+ config.im_patch_token = tokenizer.convert_tokens_to_ids([DEFAULT_IMAGE_PATCH_TOKEN])[0]
565
+ config.use_im_start_end = True
566
+
567
+ if config.use_im_start_end:
568
+ self.resize_token_embeddings(len(tokenizer))
569
+ config.im_start_token, config.im_end_token = tokenizer.convert_tokens_to_ids([DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN])
570
+
571
+ def chat(self, tokenizer, context, prompt):
572
+
573
+ self.initialize_special_tokenizer(tokenizer)
574
+
575
+ qs = prompt
576
+ qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_PATCH_TOKEN*self.get_model().config.latent_token_len + DEFAULT_IM_END_TOKEN + '\n' + qs
577
+
578
+
579
+ context = context + DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_PATCH_TOKEN*self.get_model().config.latent_token_len + DEFAULT_IM_END_TOKEN
580
+
581
+ conv_mode = "mpt"
582
+
583
+ conv = conv_templates[conv_mode].copy()
584
+ conv.append_message(conv.roles[0], qs)
585
+ conv.append_message(conv.roles[1], None)
586
+ prompt = conv.get_prompt()
587
+ inputs = tokenizer([prompt])
588
+ inputs_context = tokenizer([context])
589
+ input_ids = torch.as_tensor(inputs.input_ids).cuda()
590
+ inputs_context_ids = torch.as_tensor(inputs_context.input_ids).cuda()
591
+
592
+ stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
593
+ keywords = [stop_str]
594
+ stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
595
+ streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
596
+
597
+
598
+ with torch.autocast("cuda", dtype=torch.bfloat16):
599
+ output_ids = self.generate(
600
+ input_ids,
601
+ context_ids=inputs_context_ids,
602
+ do_sample=False,
603
+ num_beams = 1,
604
+ no_repeat_ngram_size = 20,
605
+ streamer=streamer,
606
+ max_new_tokens=4096,
607
+ stopping_criteria=[stopping_criteria]
608
+ )
609
+
610
+ outputs = tokenizer.decode(output_ids[0, input_ids.shape[1]:]).strip()
611
+
612
+ if outputs.endswith(stop_str):
613
+ outputs = outputs[:-len(stop_str)]
614
+ outputs = outputs.strip()
615
+ return outputs
616
+
617
+
618
+
619
+ AutoConfig.register("C3", C3Config)
620
+ AutoModelForCausalLM.register(C3Config, C3QwenForCausalLM)
621
+
qwen.tiktoken ADDED
The diff for this file is too large to render. See raw diff
 
special_tokens_map.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "pad_token": {
3
+ "content": "<|endoftext|>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ }
9
+ }
tokenization_qwen.py ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Alibaba Cloud.
2
+ #
3
+ # This source code is licensed under the license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ """Tokenization classes for QWen."""
7
+
8
+ import base64
9
+ import logging
10
+ import os
11
+ import unicodedata
12
+ from typing import Collection, Dict, List, Set, Tuple, Union
13
+
14
+ import tiktoken
15
+ from transformers import PreTrainedTokenizer, AddedToken
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ VOCAB_FILES_NAMES = {"vocab_file": "qwen.tiktoken"}
21
+
22
+ 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+"""
23
+ ENDOFTEXT = "<|endoftext|>"
24
+ IMSTART = "<|im_start|>"
25
+ IMEND = "<|im_end|>"
26
+ # as the default behavior is changed to allow special tokens in
27
+ # regular texts, the surface forms of special tokens need to be
28
+ # as different as possible to minimize the impact
29
+ EXTRAS = tuple((f"<|extra_{i}|>" for i in range(205)))
30
+ SPECIAL_TOKENS = (
31
+ ENDOFTEXT,
32
+ IMSTART,
33
+ IMEND,
34
+ ) + EXTRAS
35
+
36
+
37
+ def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]:
38
+ with open(tiktoken_bpe_file, "rb") as f:
39
+ contents = f.read()
40
+ return {
41
+ base64.b64decode(token): int(rank)
42
+ for token, rank in (line.split() for line in contents.splitlines() if line)
43
+ }
44
+
45
+ class QWenTokenizer(PreTrainedTokenizer):
46
+ """QWen tokenizer."""
47
+
48
+ vocab_files_names = VOCAB_FILES_NAMES
49
+
50
+ def __init__(
51
+ self,
52
+ vocab_file,
53
+ errors="replace",
54
+ image_start_tag='<img>',
55
+ image_end_tag='</img>',
56
+ image_pad_tag='<imgpad>',
57
+ ref_start_tag='<ref>',
58
+ ref_end_tag='</ref>',
59
+ box_start_tag='<box>',
60
+ box_end_tag='</box>',
61
+ quad_start_tag='<quad>',
62
+ quad_end_tag='</quad>',
63
+ **kwargs,
64
+ ):
65
+ super().__init__(**kwargs)
66
+
67
+ self.image_start_tag = image_start_tag
68
+ self.image_end_tag = image_end_tag
69
+ self.image_pad_tag = image_pad_tag
70
+ self.ref_start_tag = ref_start_tag
71
+ self.ref_end_tag = ref_end_tag
72
+ self.box_start_tag = box_start_tag
73
+ self.box_end_tag = box_end_tag
74
+ self.quad_start_tag = quad_start_tag
75
+ self.quad_end_tag = quad_end_tag
76
+ self.IMAGE_ST = (
77
+ ref_start_tag, ref_end_tag,
78
+ box_start_tag, box_end_tag,
79
+ quad_start_tag, quad_end_tag,
80
+ image_start_tag, image_end_tag,
81
+ image_pad_tag
82
+ )
83
+
84
+ self.errors = errors # how to handle errors in decoding
85
+
86
+ self.mergeable_ranks = _load_tiktoken_bpe(vocab_file) # type: dict[bytes, int]
87
+ self.special_tokens = {
88
+ token: index
89
+ for index, token in enumerate(
90
+ SPECIAL_TOKENS + self.IMAGE_ST, start=len(self.mergeable_ranks)
91
+ )
92
+ }
93
+
94
+ self.img_start_id = self.special_tokens[self.image_start_tag]
95
+ self.img_end_id = self.special_tokens[self.image_end_tag]
96
+ self.img_pad_id = self.special_tokens[self.image_pad_tag]
97
+ self.ref_start_id = self.special_tokens[self.ref_start_tag]
98
+ self.ref_end_id = self.special_tokens[self.ref_end_tag]
99
+ self.box_start_id = self.special_tokens[self.box_start_tag]
100
+ self.box_end_id = self.special_tokens[self.box_end_tag]
101
+ self.quad_start_id = self.special_tokens[self.quad_start_tag]
102
+ self.quad_end_id = self.special_tokens[self.quad_end_tag]
103
+
104
+ enc = tiktoken.Encoding(
105
+ "Qwen",
106
+ pat_str=PAT_STR,
107
+ mergeable_ranks=self.mergeable_ranks,
108
+ special_tokens=self.special_tokens,
109
+ )
110
+ assert (
111
+ len(self.mergeable_ranks) + len(self.special_tokens) == enc.n_vocab
112
+ ), f"{len(self.mergeable_ranks) + len(self.special_tokens)} != {enc.n_vocab} in encoding"
113
+
114
+ self.decoder = {
115
+ v: k for k, v in self.mergeable_ranks.items()
116
+ } # type: dict[int, bytes|str]
117
+ self.decoder.update({v: k for k, v in self.special_tokens.items()})
118
+
119
+ self.tokenizer = enc # type: tiktoken.Encoding
120
+
121
+ self.eod_id = self.tokenizer.eot_token
122
+ self.im_start_id = self.special_tokens[IMSTART]
123
+ self.im_end_id = self.special_tokens[IMEND]
124
+
125
+ def __len__(self) -> int:
126
+ return self.tokenizer.n_vocab
127
+
128
+ def get_vocab(self) -> Dict[bytes, int]:
129
+ return self.mergeable_ranks
130
+
131
+ def convert_tokens_to_ids(
132
+ self, tokens: Union[bytes, str, List[Union[bytes, str]]]
133
+ ) -> List[int]:
134
+ ids = []
135
+ if isinstance(tokens, (str, bytes)):
136
+ if tokens in self.special_tokens:
137
+ return self.special_tokens[tokens]
138
+ else:
139
+ return self.mergeable_ranks.get(tokens)
140
+ for token in tokens:
141
+ if token in self.special_tokens:
142
+ ids.append(self.special_tokens[token])
143
+ else:
144
+ ids.append(self.mergeable_ranks.get(token))
145
+ return ids
146
+
147
+ def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int:
148
+ if not special_tokens and new_tokens:
149
+ raise ValueError('Adding regular tokens is not supported')
150
+ for token in new_tokens:
151
+ surface_form = token.content if isinstance(token, AddedToken) else token
152
+ if surface_form not in SPECIAL_TOKENS:
153
+ raise ValueError('Adding unknown special tokens is not supported')
154
+ return 0
155
+
156
+ def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]:
157
+ """
158
+ Save only the vocabulary of the tokenizer (vocabulary).
159
+
160
+ Returns:
161
+ `Tuple(str)`: Paths to the files saved.
162
+ """
163
+ file_path = os.path.join(save_directory, "qwen.tiktoken")
164
+ with open(file_path, "w", encoding="utf8") as w:
165
+ for k, v in self.mergeable_ranks.items():
166
+ line = base64.b64encode(k).decode("utf8") + " " + str(v) + "\n"
167
+ w.write(line)
168
+ return (file_path,)
169
+
170
+ def tokenize(
171
+ self,
172
+ text: str,
173
+ allowed_special: Union[Set, str] = "all",
174
+ disallowed_special: Union[Collection, str] = (),
175
+ **kwargs,
176
+ ) -> List[Union[bytes, str]]:
177
+ """
178
+ Converts a string in a sequence of tokens.
179
+
180
+ Args:
181
+ text (`str`):
182
+ The sequence to be encoded.
183
+ allowed_special (`Literal["all"]` or `set`):
184
+ The surface forms of the tokens to be encoded as special tokens in regular texts.
185
+ Default to "all".
186
+ disallowed_special (`Literal["all"]` or `Collection`):
187
+ The surface forms of the tokens that should not be in regular texts and trigger errors.
188
+ Default to an empty tuple.
189
+
190
+ kwargs (additional keyword arguments, *optional*):
191
+ Will be passed to the underlying model specific encode method.
192
+
193
+ Returns:
194
+ `List[bytes|str]`: The list of tokens.
195
+ """
196
+ tokens = []
197
+ text = unicodedata.normalize("NFC", text)
198
+
199
+ # this implementation takes a detour: text -> token id -> token surface forms
200
+ for t in self.tokenizer.encode(
201
+ text, allowed_special=allowed_special, disallowed_special=disallowed_special
202
+ ):
203
+ tokens.append(self.decoder[t])
204
+ return tokens
205
+
206
+ def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str:
207
+ """
208
+ Converts a sequence of tokens in a single string.
209
+ """
210
+ text = ""
211
+ temp = b""
212
+ for t in tokens:
213
+ if isinstance(t, str):
214
+ if temp:
215
+ text += temp.decode("utf-8", errors=self.errors)
216
+ temp = b""
217
+ text += t
218
+ elif isinstance(t, bytes):
219
+ temp += t
220
+ else:
221
+ raise TypeError("token should only be of type types or str")
222
+ if temp:
223
+ text += temp.decode("utf-8", errors=self.errors)
224
+ return text
225
+
226
+ @property
227
+ def vocab_size(self):
228
+ return self.tokenizer.n_vocab
229
+
230
+ def _convert_id_to_token(self, index: int) -> Union[bytes, str]:
231
+ """Converts an id to a token, special tokens included"""
232
+ if index in self.decoder:
233
+ return self.decoder[index]
234
+ raise ValueError("unknown ids")
235
+
236
+ def _convert_token_to_id(self, token: Union[bytes, str]) -> int:
237
+ """Converts a token to an id using the vocab, special tokens included"""
238
+ if token in self.special_tokens:
239
+ return self.special_tokens[token]
240
+ if token in self.mergeable_ranks:
241
+ return self.mergeable_ranks[token]
242
+ raise ValueError("unknown token")
243
+
244
+ def _tokenize(self, text: str, **kwargs):
245
+ """
246
+ Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based
247
+ vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
248
+
249
+ Do NOT take care of added tokens.
250
+ """
251
+ raise NotImplementedError
252
+
253
+ def _decode(
254
+ self,
255
+ token_ids: Union[int, List[int]],
256
+ skip_special_tokens: bool = False,
257
+ errors: str = None,
258
+ **kwargs,
259
+ ) -> str:
260
+ if isinstance(token_ids, int):
261
+ token_ids = [token_ids]
262
+ if skip_special_tokens:
263
+ token_ids = [i for i in token_ids if i < self.eod_id]
264
+ return self.tokenizer.decode(token_ids, errors=errors or self.errors)
tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {},
3
+ "auto_map": {
4
+ "AutoTokenizer": [
5
+ "tokenization_qwen.QWenTokenizer",
6
+ null
7
+ ]
8
+ },
9
+ "clean_up_tokenization_spaces": true,
10
+ "extra_special_tokens": {},
11
+ "model_max_length": 81920,
12
+ "pad_token": "<|endoftext|>",
13
+ "padding_side": "right",
14
+ "tokenizer_class": "QWenTokenizer"
15
+ }