Upload tokenizer
Browse files- special_tokens_map.json +23 -0
- tokenization_llama.py +215 -0
- tokenizer.model +3 -0
- tokenizer_config.json +39 -0
special_tokens_map.json
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<s>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"eos_token": {
|
10 |
+
"content": "</s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"unk_token": {
|
17 |
+
"content": "<unk>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
}
|
23 |
+
}
|
tokenization_llama.py
ADDED
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
|
21 |
+
"""Tokenization classes for LLaMA."""
|
22 |
+
import os
|
23 |
+
import re
|
24 |
+
from shutil import copyfile
|
25 |
+
from typing import Any, Dict, List, Optional, Tuple
|
26 |
+
|
27 |
+
import sentencepiece as spm
|
28 |
+
|
29 |
+
from transformers.tokenization_utils import PreTrainedTokenizer
|
30 |
+
from transformers.utils import logging
|
31 |
+
|
32 |
+
|
33 |
+
logger = logging.get_logger(__name__)
|
34 |
+
|
35 |
+
VOCAB_FILES_NAMES = {"vocab_file": "tokenizer.model"}
|
36 |
+
|
37 |
+
PRETRAINED_VOCAB_FILES_MAP = {}
|
38 |
+
|
39 |
+
|
40 |
+
class LLaMATokenizer(PreTrainedTokenizer):
|
41 |
+
"""
|
42 |
+
Construct a LLaMA tokenizer. Based on byte-level Byte-Pair-Encoding.
|
43 |
+
|
44 |
+
Args:
|
45 |
+
vocab_file (`str`):
|
46 |
+
Path to the vocabulary file.
|
47 |
+
"""
|
48 |
+
|
49 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
50 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
51 |
+
model_input_names = ["input_ids", "attention_mask"]
|
52 |
+
|
53 |
+
def __init__(
|
54 |
+
self,
|
55 |
+
vocab_file,
|
56 |
+
unk_token="",
|
57 |
+
bos_token=" ⁇ ",
|
58 |
+
eos_token="",
|
59 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
60 |
+
add_bos_token=True,
|
61 |
+
add_eos_token=False,
|
62 |
+
**kwargs,
|
63 |
+
):
|
64 |
+
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
|
65 |
+
super().__init__(bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, **kwargs)
|
66 |
+
self.vocab_file = vocab_file
|
67 |
+
self.add_bos_token = add_bos_token
|
68 |
+
self.add_eos_token = add_eos_token
|
69 |
+
self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
|
70 |
+
self.sp_model.Load(vocab_file)
|
71 |
+
|
72 |
+
""" Initialisation"""
|
73 |
+
|
74 |
+
@property
|
75 |
+
def vocab_size(self):
|
76 |
+
"""Returns vocab size"""
|
77 |
+
return self.sp_model.get_piece_size()
|
78 |
+
|
79 |
+
@property
|
80 |
+
def bos_token_id(self) -> Optional[int]:
|
81 |
+
return self.sp_model.bos_id()
|
82 |
+
|
83 |
+
@property
|
84 |
+
def eos_token_id(self) -> Optional[int]:
|
85 |
+
return self.sp_model.eos_id()
|
86 |
+
|
87 |
+
def get_vocab(self):
|
88 |
+
"""Returns vocab as a dict"""
|
89 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
90 |
+
vocab.update(self.added_tokens_encoder)
|
91 |
+
return vocab
|
92 |
+
|
93 |
+
def _tokenize(self, text):
|
94 |
+
"""Returns a tokenized string."""
|
95 |
+
return self.sp_model.encode(text, out_type=str)
|
96 |
+
|
97 |
+
def _convert_token_to_id(self, token):
|
98 |
+
"""Converts a token (str) in an id using the vocab."""
|
99 |
+
return self.sp_model.piece_to_id(token)
|
100 |
+
|
101 |
+
def _convert_id_to_token(self, index):
|
102 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
103 |
+
token = self.sp_model.IdToPiece(index)
|
104 |
+
return token
|
105 |
+
|
106 |
+
def convert_tokens_to_string(self, tokens):
|
107 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
108 |
+
current_sub_tokens = []
|
109 |
+
out_string = ""
|
110 |
+
prev_is_special = False
|
111 |
+
for token in tokens:
|
112 |
+
# make sure that special tokens are not decoded using sentencepiece model
|
113 |
+
if token in self.all_special_tokens:
|
114 |
+
if not prev_is_special:
|
115 |
+
out_string += " "
|
116 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
117 |
+
prev_is_special = True
|
118 |
+
current_sub_tokens = []
|
119 |
+
else:
|
120 |
+
current_sub_tokens.append(token)
|
121 |
+
prev_is_special = False
|
122 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
123 |
+
return out_string.strip()
|
124 |
+
|
125 |
+
def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
126 |
+
"""
|
127 |
+
Save the vocabulary and special tokens file to a directory.
|
128 |
+
|
129 |
+
Args:
|
130 |
+
save_directory (`str`):
|
131 |
+
The directory in which to save the vocabulary.
|
132 |
+
|
133 |
+
Returns:
|
134 |
+
`Tuple(str)`: Paths to the files saved.
|
135 |
+
"""
|
136 |
+
if not os.path.isdir(save_directory):
|
137 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
138 |
+
return
|
139 |
+
out_vocab_file = os.path.join(
|
140 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
141 |
+
)
|
142 |
+
|
143 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
|
144 |
+
copyfile(self.vocab_file, out_vocab_file)
|
145 |
+
elif not os.path.isfile(self.vocab_file):
|
146 |
+
with open(out_vocab_file, "wb") as fi:
|
147 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
148 |
+
fi.write(content_spiece_model)
|
149 |
+
|
150 |
+
return (out_vocab_file,)
|
151 |
+
|
152 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
153 |
+
if self.add_bos_token:
|
154 |
+
bos_token_ids = [self.bos_token_id]
|
155 |
+
else:
|
156 |
+
bos_token_ids = []
|
157 |
+
|
158 |
+
output = bos_token_ids + token_ids_0
|
159 |
+
|
160 |
+
if token_ids_1 is not None:
|
161 |
+
output = output + token_ids_1
|
162 |
+
|
163 |
+
if self.add_eos_token:
|
164 |
+
output = output + [self.eos_token_id]
|
165 |
+
|
166 |
+
return output
|
167 |
+
|
168 |
+
def get_special_tokens_mask(
|
169 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
170 |
+
) -> List[int]:
|
171 |
+
"""
|
172 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
173 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
174 |
+
|
175 |
+
Args:
|
176 |
+
token_ids_0 (`List[int]`):
|
177 |
+
List of IDs.
|
178 |
+
token_ids_1 (`List[int]`, *optional*):
|
179 |
+
Optional second list of IDs for sequence pairs.
|
180 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
181 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
182 |
+
|
183 |
+
Returns:
|
184 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
185 |
+
"""
|
186 |
+
if already_has_special_tokens:
|
187 |
+
return super().get_special_tokens_mask(
|
188 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
189 |
+
)
|
190 |
+
|
191 |
+
if token_ids_1 is None:
|
192 |
+
return [1] + ([0] * len(token_ids_0)) + [1]
|
193 |
+
return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1]
|
194 |
+
|
195 |
+
def create_token_type_ids_from_sequences(
|
196 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
197 |
+
) -> List[int]:
|
198 |
+
"""
|
199 |
+
Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make
|
200 |
+
use of token type ids, therefore a list of zeros is returned.
|
201 |
+
|
202 |
+
Args:
|
203 |
+
token_ids_0 (`List[int]`):
|
204 |
+
List of IDs.
|
205 |
+
token_ids_1 (`List[int]`, *optional*):
|
206 |
+
Optional second list of IDs for sequence pairs.
|
207 |
+
|
208 |
+
Returns:
|
209 |
+
`List[int]`: List of zeros.
|
210 |
+
"""
|
211 |
+
eos = [self.eos_token_id]
|
212 |
+
|
213 |
+
if token_ids_1 is None:
|
214 |
+
return len(token_ids_0 + eos) * [0]
|
215 |
+
return len(token_ids_0 + eos + token_ids_1 + eos) * [0]
|
tokenizer.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
|
3 |
+
size 499723
|
tokenizer_config.json
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"auto_map": {
|
3 |
+
"AutoTokenizer": [
|
4 |
+
"tokenization_llama.LLaMATokenizer",
|
5 |
+
null
|
6 |
+
]
|
7 |
+
},
|
8 |
+
"bos_token": {
|
9 |
+
"__type": "AddedToken",
|
10 |
+
"content": "<s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"clean_up_tokenization_spaces": false,
|
17 |
+
"eos_token": {
|
18 |
+
"__type": "AddedToken",
|
19 |
+
"content": "</s>",
|
20 |
+
"lstrip": false,
|
21 |
+
"normalized": false,
|
22 |
+
"rstrip": false,
|
23 |
+
"single_word": false
|
24 |
+
},
|
25 |
+
"legacy": false,
|
26 |
+
"model_max_length": 1000000000000000019884624838656,
|
27 |
+
"pad_token": null,
|
28 |
+
"special_tokens_map_file": "/ssd4tb/huggingface_cache/hub/models--meta-llama--Llama-2-7b-chat-hf/snapshots/0d52e200fc7ba73089b86c1b5727267dccf65311/special_tokens_map.json",
|
29 |
+
"tokenizer_class": "LLaMATokenizer",
|
30 |
+
"unk_token": {
|
31 |
+
"__type": "AddedToken",
|
32 |
+
"content": "<unk>",
|
33 |
+
"lstrip": false,
|
34 |
+
"normalized": false,
|
35 |
+
"rstrip": false,
|
36 |
+
"single_word": false
|
37 |
+
},
|
38 |
+
"use_fast": false
|
39 |
+
}
|