| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- # 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 tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers
- from tokenizers.models import BPE
- from ...tokenization_utils_tokenizers import TokenizersBackend
- from ...utils import logging
- logger = logging.get_logger(__name__)
- 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+"""
- 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,
- **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])
- __all__ = ["Qwen2Tokenizer"]
|