File size: 12,337 Bytes
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
700ae47
 
 
 
 
15b8cd7
 
 
 
 
 
 
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
700ae47
 
 
15b8cd7
700ae47
 
 
15b8cd7
700ae47
 
 
 
 
15b8cd7
700ae47
 
15b8cd7
700ae47
 
 
 
 
 
 
 
 
 
 
15b8cd7
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
 
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
 
700ae47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15b8cd7
700ae47
 
 
 
 
 
15b8cd7
700ae47
 
 
 
15b8cd7
700ae47
15b8cd7
700ae47
 
 
 
 
15b8cd7
700ae47
 
 
 
15b8cd7
700ae47
15b8cd7
700ae47
 
15b8cd7
700ae47
 
 
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
285
286
287
288
289
290
# Copyright 2024 The Qwen team, Alibaba Group and The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tokenization classes for Qwen2."""
from typing import List, Optional

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers
from tokenizers.models import BPE
from tokenizers.processors import TemplateProcessing


VOCAB_FILES_NAMES = {
    "vocab_file": "vocab.json",
    "merges_file": "merges.txt",
    "tokenizer_file": "tokenizer.json",
}

MAX_MODEL_INPUT_SIZES = {"qwen/qwen-tokenizer": 32768}

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

from packaging.version import Version
import transformers

if Version(transformers.__version__) >= Version("5.0.0"):
    from transformers import TokenizersBackend

    class Qwen2Tokenizer(TokenizersBackend):
        vocab_files_names = VOCAB_FILES_NAMES
        model_input_names = ["input_ids", "attention_mask"]
        model = BPE

        def __init__(

            self,

            vocab: str | dict[str, int] | None = None,

            merges: str | list[str] | None = None,

            unk_token: str = "<|endoftext|>",

            bos_token=None,

            eos_token: str = "<|endoftext|>",

            pad_token: str = "<|endoftext|>",

            add_prefix_space=None,

            add_eos_token=True,

            **kwargs,

        ):
            self.add_prefix_space = add_prefix_space if add_prefix_space is not None else False
            self._vocab = (
                vocab
                if vocab is not None
                else {
                    "<|endoftext|>": 0,
                }
            )
            self._merges = merges or []
            self._tokenizer = Tokenizer(
                BPE(
                    vocab=self._vocab,
                    merges=self._merges,
                    dropout=None,
                    unk_token=None,
                    continuing_subword_prefix="",
                    end_of_word_suffix="",
                    fuse_unk=False,
                    byte_fallback=False,
                )
            )
            self._tokenizer.decoder = decoders.ByteLevel()
            self._tokenizer.normalizer = normalizers.NFC()
            self._tokenizer.pre_tokenizer = pre_tokenizers.Sequence(
                [
                    pre_tokenizers.Split(
                        Regex(PRETOKENIZE_REGEX),
                        behavior="isolated",
                        invert=False,
                    ),
                    pre_tokenizers.ByteLevel(
                        add_prefix_space=self.add_prefix_space,
                        use_regex=False,
                    ),
                ]
            )

            super().__init__(
                unk_token=unk_token,
                bos_token=bos_token,
                eos_token=eos_token,
                pad_token=pad_token,
                add_prefix_space=add_prefix_space,
                **kwargs,
            )

            self.add_tokens([AddedToken(token, special=True) for token in self.all_special_tokens])
            self._add_eos_token = add_eos_token
            self.update_post_processor()

        @property
        def add_eos_token(self):
            return self._add_eos_token

        def update_post_processor(self):
            eos = self.eos_token
            eos_token_id = self.eos_token_id
            if eos is None and self.add_eos_token:
                raise ValueError("add_eos_token = True but eos_token = None")

            single = f"$A:0{(' ' + eos + ':0') if self.add_eos_token else ''}"
            pair = f"{single} $B:1{(' ' + eos + ':1') if self.add_eos_token else ''}"

            special_tokens = []
            if self.add_eos_token:
                special_tokens.append((eos, eos_token_id))
            self._tokenizer.post_processor = TemplateProcessing(
                single=single, pair=pair, special_tokens=special_tokens
            )

else:
    from transformers.models.qwen2.tokenization_qwen2 import Qwen2Tokenizer as OriginalQwen2Tokenizer

    class Qwen2Tokenizer(OriginalQwen2Tokenizer):
        """

        Construct a Qwen2 tokenizer. Based on byte-level Byte-Pair-Encoding.



        Same with GPT2Tokenizer, this tokenizer has been trained to treat spaces like parts of the tokens so a word will

        be encoded differently whether it is at the beginning of the sentence (without space) or not:



        ```python

        >>> from transformers import Qwen2Tokenizer



        >>> tokenizer = Qwen2Tokenizer.from_pretrained("Qwen/Qwen-tokenizer")

        >>> tokenizer("Hello world")["input_ids"]

        [9707, 1879]



        >>> tokenizer(" Hello world")["input_ids"]

        [21927, 1879]

        ```

        This is expected.



        You should not use GPT2Tokenizer instead, because of the different pretokenization rules.



        This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to

        this superclass for more information regarding those methods.



        Args:

            vocab_file (`str`):

                Path to the vocabulary file.

            merges_file (`str`):

                Path to the merges file.

            errors (`str`, *optional*, defaults to `"replace"`):

                Paradigm to follow when decoding bytes to UTF-8. See

                [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.

            unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`):

                The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this

                token instead.

            bos_token (`str`, *optional*):

                The beginning of sequence token. Not applicable for this tokenizer.

            eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):

                The end of sequence token.

            pad_token (`str`, *optional*, defaults to `"<|endoftext|>"`):

                The token used for padding, for example when batching sequences of different lengths.

            clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):

                Whether or not the model should cleanup the spaces that were added when splitting the input text during the

                tokenization process. Not applicable to this tokenizer, since tokenization does not add spaces.

            split_special_tokens (`bool`, *optional*, defaults to `False`):

                Whether or not the special tokens should be split during the tokenization process. The default behavior is

                to not split special tokens. This means that if `<|endoftext|>` is the `eos_token`, then `tokenizer.tokenize("<|endoftext|>") =

                ['<|endoftext|>`]. Otherwise, if `split_special_tokens=True`, then `tokenizer.tokenize("<|endoftext|>")` will be give `['<',

                '|', 'endo', 'ft', 'ext', '|', '>']`. This argument is only supported for `slow` tokenizers for the moment.

            add_eos_token (`bool`, *optional*, defaults to `False`):

                Whether or not to add an `eos_token` at the end of sequences.

        """

        def __init__(

                self,

                vocab_file,

                merges_file,

                errors="replace",

                unk_token="<|endoftext|>",

                bos_token=None,

                eos_token="<|endoftext|>",

                pad_token="<|endoftext|>",

                clean_up_tokenization_spaces=False,

                split_special_tokens=False,

                add_eos_token=False,

                **kwargs,

        ):
            # The add_eos_token code was inspired by the LlamaTokenizer
            self.add_eos_token = add_eos_token

            super().__init__(
                vocab_file=vocab_file,
                merges_file=merges_file,
                errors=errors,
                unk_token=unk_token,
                bos_token=bos_token,
                eos_token=eos_token,
                pad_token=pad_token,
                clean_up_tokenization_spaces=clean_up_tokenization_spaces,
                split_special_tokens=split_special_tokens,
                add_eos_token=add_eos_token,
                **kwargs,
            )

        def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
            eos_token_id = [self.eos_token_id] if self.add_eos_token else []

            output = token_ids_0 + eos_token_id

            if token_ids_1 is not None:
                output = output + token_ids_1 + eos_token_id

            return output

        def get_special_tokens_mask(

                self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None,

                already_has_special_tokens: bool = False

        ) -> List[int]:
            """

            Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding

            special tokens using the tokenizer `prepare_for_model` method.



            Args:

                token_ids_0 (`List[int]`):

                    List of IDs.

                token_ids_1 (`List[int]`, *optional*):

                    Optional second list of IDs for sequence pairs.

                already_has_special_tokens (`bool`, *optional*, defaults to `False`):

                    Whether or not the token list is already formatted with special tokens for the model.



            Returns:

                `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.

            """
            if already_has_special_tokens:
                return super().get_special_tokens_mask(
                    token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
                )

            eos_token_id = [1] if self.add_eos_token else []

            if token_ids_1 is None:
                return ([0] * len(token_ids_0)) + eos_token_id
            return (
                    ([0] * len(token_ids_0))
                    + eos_token_id
                    + ([0] * len(token_ids_1))
                    + eos_token_id
            )

        def create_token_type_ids_from_sequences(

                self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None

        ) -> List[int]:
            """

            Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT

            sequence pair mask has the following format:



            ```

            0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1

            | first sequence    | second sequence |

            ```



            if token_ids_1 is None, only returns the first portion of the mask (0s).



            Args:

                token_ids_0 (`List[int]`):

                    List of ids.

                token_ids_1 (`List[int]`, *optional*):

                    Optional second list of IDs for sequence pairs.



            Returns:

                `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).

            """
            eos_token_id = [self.eos_token_id] if self.add_eos_token else []

            output = [0] * len(token_ids_0 + eos_token_id)

            if token_ids_1 is not None:
                output += [1] * len(token_ids_1 + eos_token_id)

            return output

__all__ = ["Qwen2Tokenizer"]