tjellm commited on
Commit
8180b4e
·
verified ·
1 Parent(s): 59d32b3

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ onnx/directml/phi-3-small-128k-instruct-int4/model.onnx.data filter=lfs diff=lfs merge=lfs -text
onnx/directml/phi-3-small-128k-instruct-int4/cl100k_base.tiktoken ADDED
The diff for this file is too large to render. See raw diff
 
onnx/directml/phi-3-small-128k-instruct-int4/genai_config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model": {
3
+ "bos_token_id": 100257,
4
+ "context_length": 131072,
5
+ "decoder": {
6
+ "session_options": {
7
+ "log_id": "onnxruntime-genai",
8
+ "provider_options": [
9
+ {
10
+ "cuda": {
11
+ "enable_cuda_graph": "0"
12
+ }
13
+ }
14
+ ]
15
+ },
16
+ "filename": "model.onnx",
17
+ "head_size": 128,
18
+ "hidden_size": 4096,
19
+ "inputs": {
20
+ "input_ids": "input_ids",
21
+ "attention_mask": "attention_mask",
22
+ "past_key_names": "past_key_values.%d.key",
23
+ "past_value_names": "past_key_values.%d.value"
24
+ },
25
+ "outputs": {
26
+ "logits": "logits",
27
+ "present_key_names": "present.%d.key",
28
+ "present_value_names": "present.%d.value"
29
+ },
30
+ "num_attention_heads": 32,
31
+ "num_hidden_layers": 32,
32
+ "num_key_value_heads": 8
33
+ },
34
+ "eos_token_id": [
35
+ 100257,
36
+ 100266
37
+ ],
38
+ "pad_token_id": 100257,
39
+ "type": "phi3small",
40
+ "vocab_size": 100352
41
+ },
42
+ "search": {
43
+ "diversity_penalty": 0.0,
44
+ "do_sample": false,
45
+ "early_stopping": true,
46
+ "length_penalty": 1.0,
47
+ "max_length": 131072,
48
+ "min_length": 0,
49
+ "no_repeat_ngram_size": 0,
50
+ "num_beams": 1,
51
+ "num_return_sequences": 1,
52
+ "past_present_share_buffer": true,
53
+ "repetition_penalty": 1.0,
54
+ "temperature": 1.0,
55
+ "top_k": 1,
56
+ "top_p": 1.0
57
+ }
58
+ }
onnx/directml/phi-3-small-128k-instruct-int4/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e34b84e87c6b2f23fb0609514ea2a95999da5d05701aeb3f60dec9e87143789e
3
+ size 35992630
onnx/directml/phi-3-small-128k-instruct-int4/model.onnx.data ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:257b30d1fe983aba711c68ca56e06aa7daebea4f11f429c5587c09a43ea8c46c
3
+ size 5020684416
onnx/directml/phi-3-small-128k-instruct-int4/special_tokens_map.json ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ {
2
+ "bos_token": "<|endoftext|>",
3
+ "eos_token": "<|endoftext|>",
4
+ "pad_token": "<|endoftext|>"
5
+ }
onnx/directml/phi-3-small-128k-instruct-int4/tokenization_phi3_small.py ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://huggingface.co/Qwen/Qwen-7B-Chat/blob/main/tokenization_qwen.py
2
+ import os
3
+ from typing import Collection, List, Optional, Dict, Set, Tuple, Union
4
+
5
+ from functools import cached_property
6
+
7
+ import base64
8
+
9
+ from transformers import PreTrainedTokenizer, AddedToken, AutoConfig
10
+ from transformers.models.auto.tokenization_auto import get_tokenizer_config
11
+ import tiktoken
12
+
13
+
14
+ """
15
+ This tokenizer is almost identical to tiktoken.get_encoding("cl100k_base")
16
+ with a few additional special tokens to support the ChatML format.
17
+
18
+ TODO(bapatra): Right now, I do not save the special tokens to the vocab file.
19
+ Maybe in the future, that would be useful? Can add that support later.
20
+
21
+ """
22
+
23
+ def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]:
24
+ with open(tiktoken_bpe_file, "rb") as f:
25
+ contents = f.read()
26
+ return {
27
+ base64.b64decode(token): int(rank)
28
+ for token, rank in (line.split() for line in contents.splitlines() if line)
29
+ }
30
+
31
+ # On the megatron codebase, we pad vocabularies to ensure matrix multiplication is fast.
32
+ # this in turn causes some indices to be empty. We account for these empty indices by adding
33
+ # dummy tokens to the tokenizer.
34
+
35
+ EFFECTIVE_PADDED_VOCAB_SIZE = 100352
36
+ ACTUAL_VOCAB_SIZE = 100276
37
+
38
+
39
+ DUMMY_TOKENS = {
40
+ f"<|dummy_id_{11 + offset}|>": 100276 + offset
41
+ for offset in range(1, EFFECTIVE_PADDED_VOCAB_SIZE - ACTUAL_VOCAB_SIZE)
42
+ }
43
+
44
+ SPECIAL_TOKENS = {
45
+ # tiktoken.get_encoding("cl100k_base")._special_tokens
46
+ '<|endoftext|>': 100257,
47
+ '<|fim_prefix|>': 100258,
48
+ '<|fim_middle|>': 100259,
49
+ '<|fim_suffix|>': 100260,
50
+ # Special tokens for post-training
51
+ "<|system|>": 100261,
52
+ "<|user|>": 100262,
53
+ "<|assistant|>": 100263,
54
+ # Dummy unused tokens
55
+ "<|dummy_id_0|>": 100264,
56
+ "<|dummy_id_1|>": 100265,
57
+ # Special tokens for post-training continued
58
+ "<|end|>": 100266,
59
+ # Some dummy tokens, so that tokenization is contiguous and does not cause issues
60
+ # Note that the 100256th token of tiktoken.get_encoding("cl100k_base") does not
61
+ # actually map to anything. So we use a dummy token here.
62
+ "<|dummy_id_2|>": 100256,
63
+ # Likewise, tokens from 100267 to 100275 are also unused
64
+ "<|dummy_id_3|>": 100267,
65
+ "<|dummy_id_4|>": 100268,
66
+ "<|dummy_id_5|>": 100269,
67
+ "<|dummy_id_6|>": 100270,
68
+ "<|dummy_id_7|>": 100271,
69
+ "<|dummy_id_8|>": 100272,
70
+ "<|dummy_id_9|>": 100273,
71
+ "<|dummy_id_10|>": 100274,
72
+ "<|dummy_id_11|>": 100275,
73
+ # The final end of prompt token
74
+ # (unused, but present as a part of tiktoken.get_encoding("cl100k_base")._special_tokens)
75
+ '<|endofprompt|>': 100276,
76
+ # Dummy tokens to account for padding of the tokenizer
77
+ # We pad to ensure tensor cores are used for vocab multiplication
78
+ **DUMMY_TOKENS
79
+ }
80
+
81
+ class Phi3SmallTokenizer(PreTrainedTokenizer):
82
+ vocab_files_names = {
83
+ "vocab_file": "cl100k_base.tiktoken"
84
+ }
85
+
86
+ model_input_names: List[str] = ["input_ids", "attention_mask"]
87
+ padding_side = "left"
88
+
89
+ def __init__(
90
+ self,
91
+ vocab_file: Optional[str] = None,
92
+ errors: str = "replace",
93
+ **kwargs
94
+ ) -> None:
95
+ # PreTrainedTokenizer's init calls _add_tokens, which in turn checks
96
+ # if the token is present in `self.special_tokens``. Hence instantiating it here.
97
+ # The way Qwen gets around this is by checking against SPECIAL_TOKENS
98
+ # But I think it's better to check against the objects own `special_tokens`
99
+ # in case we eventually want to allow the tokenizer to have special tokens.
100
+ self.special_tokens = SPECIAL_TOKENS
101
+
102
+ super().__init__(**kwargs)
103
+ self.errors = errors
104
+
105
+ base = tiktoken.get_encoding("cl100k_base")
106
+ if vocab_file is None:
107
+ self.mergeable_ranks: Dict[bytes, int] = base._mergeable_ranks
108
+ else:
109
+ self.mergeable_ranks = _load_tiktoken_bpe(vocab_file)
110
+
111
+ self.pat_str = base._pat_str
112
+
113
+ enc = tiktoken.Encoding(
114
+ name="phi3small",
115
+ pat_str=self.pat_str,
116
+ mergeable_ranks=self.mergeable_ranks,
117
+ special_tokens=self.special_tokens,
118
+ )
119
+ self.tokenizer = enc
120
+
121
+ self.decoder: Dict[int, bytes] = {
122
+ v: k for k, v in self.mergeable_ranks.items()
123
+ }
124
+ self.decoder.update({v: k for k, v in self.special_tokens.items()})
125
+
126
+ self.eod_id = self.tokenizer.eot_token
127
+ self._eos_token = self._convert_id_to_token(self.eod_id)
128
+
129
+ # Setting the bos_token to be the same as the eos_token
130
+ # Note that this is **not** the correct thing to do, and is done
131
+ # just so that some of the downstream libraries do not break.
132
+ self._bos_token = self._eos_token
133
+
134
+ # Assign the special tokens to class variables
135
+ self.system_id = self.special_tokens["<|system|>"]
136
+ self.user_id = self.special_tokens["<|user|>"]
137
+ self.assistant_id = self.special_tokens["<|assistant|>"]
138
+ self.end_id = self.special_tokens["<|end|>"]
139
+
140
+ @cached_property
141
+ def dummy_token_indices(self) -> List[int]:
142
+ # There are some additional special tokens in the cl100k_base tokenizer
143
+ # that we do not use. Hence, we also consider them to be dummy tokens.
144
+ additional_tokens = [
145
+ "<|fim_prefix|>",
146
+ "<|fim_middle|>",
147
+ "<|fim_suffix|>",
148
+ "<|endofprompt|>"
149
+ ]
150
+ dummy_token_indices = [index for token, index in self.special_tokens.items() if "dummy_id" in token]
151
+ dummy_token_indices.extend([self.special_tokens[token] for token in additional_tokens])
152
+ return sorted(dummy_token_indices)
153
+
154
+ def __getstate__(self):
155
+ state = self.__dict__.copy()
156
+ del state["tokenizer"]
157
+ return state
158
+
159
+ def __setstate__(self, state):
160
+ self.__dict__ = state
161
+ enc = tiktoken.Encoding(
162
+ name="cl100k_im",
163
+ pat_str=self.pat_str,
164
+ mergeable_ranks=self.mergeable_ranks,
165
+ special_tokens=self.special_tokens,
166
+ )
167
+ self.tokenizer = enc
168
+
169
+ def __len__(self):
170
+ return self.tokenizer.n_vocab
171
+
172
+ @classmethod
173
+ def from_pretrained(
174
+ cls,
175
+ pretrained_model_name_or_path: Union[str, os.PathLike],
176
+ *init_inputs,
177
+ **kwargs,
178
+ ):
179
+ cls_kwargs = kwargs
180
+ # First try to load from the tokenization config if it exists
181
+ tokenization_config = get_tokenizer_config(pretrained_model_name_or_path, **kwargs)
182
+ if tokenization_config:
183
+ cls_kwargs = {
184
+ **tokenization_config,
185
+ **cls_kwargs
186
+ }
187
+ else:
188
+ config = AutoConfig.from_pretrained(pretrained_model_name_or_path, trust_remote_code=True)
189
+ cls_kwargs["model_max_length"] = config.max_position_embeddings
190
+ return cls(**cls_kwargs)
191
+
192
+ def get_vocab(self) -> Dict[Union[str, bytes], int]:
193
+ return {**self.mergeable_ranks, **self.special_tokens}
194
+
195
+ def convert_tokens_to_ids(
196
+ self,
197
+ tokens: Union[bytes, str, List[Union[bytes, str]]]
198
+ ) -> Union[int, List[int]]:
199
+ ids = []
200
+ if isinstance(tokens, (str, bytes)):
201
+ if tokens in self.special_tokens:
202
+ return self.special_tokens[tokens]
203
+ else:
204
+ return self.mergeable_ranks.get(tokens)
205
+ ids: List[int] = []
206
+ for token in tokens:
207
+ ids.append(self.convert_tokens_to_ids(token))
208
+ return ids
209
+
210
+ def _add_tokens(
211
+ self,
212
+ new_tokens: Union[List[str], List[AddedToken]],
213
+ special_tokens: bool = False,
214
+ ) -> int:
215
+ if not special_tokens and new_tokens:
216
+ raise ValueError("Only special tokens can be added to this tokenizer")
217
+ for token in new_tokens:
218
+ surface_form = token.content if isinstance(token, AddedToken) else token
219
+ if surface_form not in self.special_tokens:
220
+ raise ValueError(
221
+ "For now, we do not support unknown special tokens\n"
222
+ "In the future, if there is a need for this, we can add special tokens to the tokenizer\n"
223
+ "starting from rank 100261 - 100263 and then 100266 - 100275.\n"
224
+ "And finally, we can re-construct the enc object back\n"
225
+ )
226
+ return 0
227
+
228
+ def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]:
229
+ file_path = os.path.join(save_directory, "cl100k_base.tiktoken")
230
+ with open(file_path, "w") as f:
231
+ for token, rank in self.mergeable_ranks.items():
232
+ line = base64.b64encode(token).decode("utf-8") + " " + str(rank) + "\n"
233
+ f.write(line)
234
+ return (file_path,)
235
+
236
+ def tokenize(
237
+ self,
238
+ text: str,
239
+ allowed_special: Union[Set, str] = "all",
240
+ disallowed_special: Union[Collection, str] = (),
241
+ **kwargs
242
+ ) -> List[Union[bytes, str]]:
243
+ tokens: List[Union[bytes, str]] = []
244
+ for token_id in self.tokenizer.encode(
245
+ text, allowed_special=allowed_special, disallowed_special=disallowed_special
246
+ ):
247
+ tokens.append(self.decoder[token_id])
248
+ return tokens
249
+
250
+ def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str:
251
+ """
252
+ Converts a sequence of tokens in a single string.
253
+ """
254
+ text = ""
255
+ temp = b""
256
+ for t in tokens:
257
+ if isinstance(t, str):
258
+ if temp:
259
+ text += temp.decode("utf-8", errors=self.errors)
260
+ temp = b""
261
+ text += t
262
+ elif isinstance(t, bytes):
263
+ temp += t
264
+ else:
265
+ raise TypeError("token should only be of type types or str")
266
+ if temp:
267
+ text += temp.decode("utf-8", errors=self.errors)
268
+ return text
269
+
270
+ @property
271
+ def vocab_size(self):
272
+ return self.tokenizer.n_vocab
273
+
274
+ @property
275
+ def eos_token_id(self) -> int:
276
+ return self.eod_id
277
+
278
+ def _convert_id_to_token(self, index: int) -> Union[bytes, str]:
279
+ """Converts an id to a token, special tokens included"""
280
+ if index in self.decoder:
281
+ return self.decoder[index]
282
+ raise ValueError("unknown ids")
283
+
284
+ def _convert_token_to_id(self, token: Union[bytes, str]) -> int:
285
+ """Converts a token to an id using the vocab, special tokens included"""
286
+ if token in self.special_tokens:
287
+ return self.special_tokens[token]
288
+ if token in self.mergeable_ranks:
289
+ return self.mergeable_ranks[token]
290
+ raise ValueError("unknown token")
291
+
292
+ def _tokenize(self, text: str, **kwargs):
293
+ """
294
+ Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based
295
+ vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
296
+ Do NOT take care of added tokens.
297
+ """
298
+ raise NotImplementedError
299
+
300
+ def _decode(
301
+ self,
302
+ token_ids: Union[int, List[int]],
303
+ skip_special_tokens: bool = False,
304
+ errors: str = None,
305
+ **kwargs,
306
+ ) -> str:
307
+ if isinstance(token_ids, int):
308
+ token_ids = [token_ids]
309
+ if skip_special_tokens:
310
+ token_ids = [i for i in token_ids if i < self.eod_id]
311
+ return self.tokenizer.decode(token_ids, errors=errors or self.errors)
312
+
313
+
onnx/directml/phi-3-small-128k-instruct-int4/tokenizer_config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_commit_hash": null,
3
+ "_from_auto": true,
4
+ "added_tokens_decoder": {},
5
+ "auto_map": {
6
+ "AutoTokenizer": [
7
+ "tokenization_phi3_small.Phi3SmallTokenizer",
8
+ null
9
+ ]
10
+ },
11
+ "bos_token": "<|endoftext|>",
12
+ "cache_dir": "./cache_dir",
13
+ "chat_template": "{{ bos_token }}{% for message in messages %}{{'<|' + message['role'] + '|>' + '\n' + message['content'] + '<|end|>\n' }}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% else %}{{ eos_token }}{% endif %}",
14
+ "clean_up_tokenization_spaces": true,
15
+ "eos_token": "<|endoftext|>",
16
+ "model_max_length": 131072,
17
+ "pad_token": "<|endoftext|>",
18
+ "token": true,
19
+ "tokenizer_class": "Phi3SmallTokenizer",
20
+ "trust_remote_code": true
21
+ }