File size: 9,765 Bytes
d6f2919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0afe74a
 
 
 
d6f2919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import torch
import base64
import tiktoken
from typing import Collection, Optional, Dict, List, Set, Tuple, Union
from transformers import PreTrainedTokenizer
from transformers.utils import PaddingStrategy
from transformers.tokenization_utils import PreTrainedTokenizer


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+"""


class SPTokenizer:
    def __init__(self, model_path):
        self.vocab_file = model_path
        self.pad_token = '<pad>'
        self.unk_token = '<unk>'
        self.mask_token = '<mask>'
        self.eod_token = '<eod>'
        self.eop_token = '<eop>'
        self.im_start_token = '<|im_start|>'
        self.im_end_token = '<|im_end|>'

        ## special_tokens
        self.SPECIAL_TOKENS = (
            self.pad_token,
            self.unk_token,
            self.mask_token,
            self.eod_token,
            self.eop_token,
            '[space2]', '[space3]', '[space4]', '[space8]',
            self.im_start_token, self.im_end_token
        )
        self.bulid_tokenizer()
        self.out = self.output_core_token()
        
        self.token2strs = {
            "[space2]": "  ",
            "[space3]": "   ",
            "[space4]": "    ",
            "[space8]": "        ",
        }
        self.str2tokens = {v: k for k, v in self.token2strs.items()}
        self.sorted_strs = sorted(list(self.str2tokens.keys()),
                                  key=lambda x: len(x), reverse=True)
        
        ## skip_special_tokens
        self.decode_skip_special_tokens = [
            self.pad_token,
            self.unk_token,
            self.mask_token,
            self.eod_token,
            self.eop_token,
            self.im_start_token,
            self.im_end_token]
        self.decode_skip_special_tokens_ids = [self.convert_token_to_id(token) for token in self.decode_skip_special_tokens]

    def _load_tiktoken_bpe(self, tiktoken_bpe_file: str):
        with open(tiktoken_bpe_file, "rb") as f:
            contents = f.read()
        return {
            base64.b64decode(token): int(rank)
            for token, rank in (line.split() for line in contents.splitlines() if line)
        }
    
    def bulid_tokenizer(self):
        mergeable_ranks = self._load_tiktoken_bpe(self.vocab_file)
        special_tokens = {
            token: index
            for index, token in enumerate(
                self.SPECIAL_TOKENS, start=len(mergeable_ranks)
            )
        }
        encode = tiktoken.Encoding(
            "zhinao",
            pat_str=PAT_STR,
            mergeable_ranks=mergeable_ranks,
            special_tokens=special_tokens
        )
        decoder = {v: k for k, v in mergeable_ranks.items()}
        decoder.update({v: k for k, v in special_tokens.items()})
        decoder_token2id = {v: k for k, v in decoder.items()}
    
        self.tokenizer = encode
        self.decoder = decoder
        self.decoder_token2id = decoder_token2id
        self.num_tokens = len(mergeable_ranks) + len(self.SPECIAL_TOKENS)

    def output_core_token(self):
        """output special tokens"""
        out = {}
        for t in self.SPECIAL_TOKENS:
            out[t] = self.convert_token_to_id(t)
        return out

    def tokenize(
            self, 
            text, 
            allowed_special: Union[Set, str] = "all",
            disallowed_special: Union[Collection, str] = ()):
        tokens = []
        text = self.convert(text)
        for idx in self.tokenizer.encode(text, allowed_special=allowed_special, disallowed_special=disallowed_special):
            tokens.append(self.decoder[idx])
        return tokens

    def encode(self, text, allowed_special="all", disallowed_special=()):
        """text to id"""
        text = self.convert(text)
        return self.tokenizer.encode(text, allowed_special=allowed_special, disallowed_special=disallowed_special)
    
    def decode(self, ids, errors="replace"):
        """id to text"""
        text = self.tokenizer.decode(ids, errors=errors)
        return self.deconvert(text)

    def decode_tokens(self, tokens: List[str]) -> str:
        """
        Converts a sequence of tokens in a single string.
        """
        text = ""
        temp = b""
        for t in tokens:
            if isinstance(t, str):
                if temp:
                    text += temp.decode("utf-8", errors="replace")
                    temp = b""
                text += t
            elif isinstance(t, bytes):
                temp += t
            else:
                raise TypeError("token should only be of type bytes or str")
        if temp:
            text += temp.decode("utf-8", errors="replace")
        return self.deconvert(text)

    def convert_id_to_token(self, idx):
        return self.decoder[idx]
    
    def convert_token_to_id(self, token):
        return self.decoder_token2id[token]

    def convert(self, text):
        """将文本的特殊字符转换成特殊token"""
        for k in ["[br]", "<br>"]:
            text = text.replace(k, "\n")
        for k in self.sorted_strs:
            if k in text:
                text = text.replace(k, self.str2tokens[k])
        return text

    def deconvert(self, text):
        """将解码文本恢复原始字符"""
        for t in self.token2strs:
            if t in text:
                text = text.replace(t, self.token2strs[t])
        return text


class ZhinaoTokenizer(PreTrainedTokenizer):
    vocab_files_names = {"vocab_file": "vocab/360.tiktoken"}
    model_input_names = ["input_ids", "attention_mask"]

    def __init__(self, vocab_file, padding_side="left", clean_up_tokenization_spaces=False, **kwargs):
        self.name = "ZhinaoTokenizer"
        self.errors = "replace"
        self.vocab_file = vocab_file
        self.tokenizer = SPTokenizer(model_path=vocab_file)
        try:
            kwargs.pop('eos_token')
            kwargs.pop('pad_token')
            kwargs.pop('unk_token')
        except:
            pass
        super().__init__(padding_side=padding_side, clean_up_tokenization_spaces=clean_up_tokenization_spaces, **kwargs)
        self.pad_token_id = self.tokenizer.convert_token_to_id(self.tokenizer.pad_token)
        self.eod_id = self.tokenizer.convert_token_to_id(self.tokenizer.eod_token)
        self.im_start_id = self.tokenizer.convert_token_to_id(self.tokenizer.im_start_token)
        self.im_end_id = self.tokenizer.convert_token_to_id(self.tokenizer.im_end_token)
        from icecream import ic
        ic(
            self.eos_token_id,
            self.pad_token_id,
            self.im_start_id,
            self.im_end_id)

    @property
    def unk_token(self) -> str:
        return self.tokenizer.unk_token

    @property
    def pad_token(self) -> str:
        return self.tokenizer.pad_token

    @property
    def eos_token(self) -> str:
        return self.tokenizer.eod_token

    @property
    def eos_token_id(self):
        return self.tokenizer.convert_token_to_id(self.tokenizer.eod_token)

    @property
    def eop_token(self) -> str:
        return self.tokenizer.eop_token

    @property
    def eop_token_id(self):
        return self.tokenizer.convert_token_to_id(self.tokenizer.eop_token)

    @property
    def vocab_size(self):
        return self.tokenizer.num_tokens
    
    @property
    def vocab(self):
        return self.get_vocab()

    def get_vocab(self):
        """ Returns vocab as a dict """
        vocab = {self._convert_id_to_token(i): i for i in range(self.vocab_size)}
        vocab.update(self.added_tokens_encoder)
        return vocab
    
    def tokenize(
        self,
        text: str,
        allowed_special: Union[Set, str] = "all",
        disallowed_special: Union[Collection, str] = (),
    ) -> List[Union[bytes, str]]:
        tokens = []
        for t in self.tokenizer.encode(
            text, allowed_special=allowed_special, disallowed_special=disallowed_special
        ):
            tokens.append(self.tokenizer.decoder[t])
        return tokens
    
    def _decode(
        self,
        token_ids: Union[int, List[int]],
        skip_special_tokens: bool = False,
        errors: str = None,
        **kwargs,
    ) -> str:
        if isinstance(token_ids, int):
            token_ids = [token_ids]
        if skip_special_tokens:
            token_ids = [i for i in token_ids if i not in self.tokenizer.decode_skip_special_tokens_ids]
        return self.tokenizer.decode(token_ids, errors=errors or self.errors)

    def _tokenize(self, text, **kwargs):
        raise NotImplementedError

    def _convert_token_to_id(self, token):
        """ Converts a token (str) in an id using the vocab. """
        return self.tokenizer.convert_token_to_id(token)

    def _convert_id_to_token(self, index):
        """Converts an index (integer) in a token (str) using the vocab. """
        return self.tokenizer.convert_id_to_token(index)

    def convert_tokens_to_string(self, tokens: List[str]) -> str:
        """
        Converts a sequence of tokens in a single string.
        """
        return self.tokenizer.decode_tokens(tokens)

    def save_vocabulary(self, save_directory, filename_prefix=None):
        """Save only the vocabulary of the tokenizer (vocabulary). """
        if os.path.isdir(save_directory):
            vocab_file = os.path.join(save_directory, self.vocab_files_names["vocab_file"])
        else:
            vocab_file = save_directory

        with open(self.vocab_file, 'rb') as fin:
            proto_str = fin.read()

        os.makedirs(save_directory + "/vocab", exist_ok=True)
        with open(vocab_file, "wb") as writer:
            writer.write(proto_str)

        return (vocab_file,)