|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# Copyright 2024 The Qwen team, Alibaba Group and The HuggingFace Inc. team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Tokenization classes for Qwen2.""" |
| 16 | + |
| 17 | +from typing import Optional, Tuple |
| 18 | + |
| 19 | +from ..tokenizer_utils import AddedToken |
| 20 | +from ..tokenizer_utils_fast import PretrainedTokenizerFast |
| 21 | +from .tokenizer import Qwen2Tokenizer |
| 22 | + |
| 23 | +VOCAB_FILES_NAMES = { |
| 24 | + "vocab_file": "vocab.json", |
| 25 | + "merges_file": "merges.txt", |
| 26 | + "tokenizer_file": "tokenizer.json", |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +MAX_MODEL_INPUT_SIZES = {"qwen/qwen-tokenizer": 32768} |
| 31 | + |
| 32 | + |
| 33 | +class Qwen2TokenizerFast(PretrainedTokenizerFast): |
| 34 | + """ |
| 35 | + Construct a "fast" Qwen2 tokenizer (backed by PaddleNLP's *tokenizers* library). Based on byte-level |
| 36 | + Byte-Pair-Encoding. |
| 37 | +
|
| 38 | + Same with GPT2Tokenizer, this tokenizer has been trained to treat spaces like parts of the tokens so a word will |
| 39 | + be encoded differently whether it is at the beginning of the sentence (without space) or not: |
| 40 | +
|
| 41 | + ```python |
| 42 | + >>> from transformers import Qwen2TokenizerFast |
| 43 | +
|
| 44 | + >>> tokenizer = Qwen2TokenizerFast.from_pretrained("Qwen/Qwen-tokenizer") |
| 45 | + >>> tokenizer("Hello world")["input_ids"] |
| 46 | + [9707, 1879] |
| 47 | +
|
| 48 | + >>> tokenizer(" Hello world")["input_ids"] |
| 49 | + [21927, 1879] |
| 50 | + ``` |
| 51 | + This is expected. |
| 52 | +
|
| 53 | + This tokenizer inherits from [`PreTrainedTokenizerFast`] which contains most of the main methods. Users should |
| 54 | + refer to this superclass for more information regarding those methods. |
| 55 | +
|
| 56 | + Args: |
| 57 | + vocab_file (`str`, *optional*): |
| 58 | + Path to the vocabulary file. |
| 59 | + merges_file (`str`, *optional*): |
| 60 | + Path to the merges file. |
| 61 | + tokenizer_file (`str`, *optional*): |
| 62 | + Path to [tokenizers](https://github.com/huggingface/tokenizers) file (generally has a .json extension) that |
| 63 | + contains everything needed to load the tokenizer. |
| 64 | + unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`): |
| 65 | + The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this |
| 66 | + token instead. Not applicable to this tokenizer. |
| 67 | + bos_token (`str`, *optional*): |
| 68 | + The beginning of sequence token. Not applicable for this tokenizer. |
| 69 | + eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`): |
| 70 | + The end of sequence token. |
| 71 | + pad_token (`str`, *optional*, defaults to `"<|endoftext|>"`): |
| 72 | + The token used for padding, for example when batching sequences of different lengths. |
| 73 | + """ |
| 74 | + |
| 75 | + vocab_files_names = VOCAB_FILES_NAMES |
| 76 | + resource_files_names = VOCAB_FILES_NAMES |
| 77 | + model_input_names = ["input_ids", "attention_mask"] |
| 78 | + slow_tokenizer_class = Qwen2Tokenizer |
| 79 | + |
| 80 | + def __init__( |
| 81 | + self, |
| 82 | + vocab_file=None, |
| 83 | + merges_file=None, |
| 84 | + tokenizer_file=None, |
| 85 | + unk_token="<|endoftext|>", |
| 86 | + bos_token=None, |
| 87 | + eos_token="<|endoftext|>", |
| 88 | + pad_token="<|endoftext|>", |
| 89 | + **kwargs, |
| 90 | + ): |
| 91 | + # We need to at least pass vocab_file and merges_file to base class |
| 92 | + # in case a slow tokenizer needs to be initialized; other can be |
| 93 | + # configured through files. |
| 94 | + # following GPT2TokenizerFast, also adding unk_token, bos_token, and eos_token |
| 95 | + |
| 96 | + bos_token = ( |
| 97 | + AddedToken(bos_token, lstrip=False, rstrip=False, special=True, normalized=False) |
| 98 | + if isinstance(bos_token, str) |
| 99 | + else bos_token |
| 100 | + ) |
| 101 | + eos_token = ( |
| 102 | + AddedToken(eos_token, lstrip=False, rstrip=False, special=True, normalized=False) |
| 103 | + if isinstance(eos_token, str) |
| 104 | + else eos_token |
| 105 | + ) |
| 106 | + unk_token = ( |
| 107 | + AddedToken(unk_token, lstrip=False, rstrip=False, special=True, normalized=False) |
| 108 | + if isinstance(unk_token, str) |
| 109 | + else unk_token |
| 110 | + ) |
| 111 | + pad_token = ( |
| 112 | + AddedToken(pad_token, lstrip=False, rstrip=False, special=True, normalized=False) |
| 113 | + if isinstance(pad_token, str) |
| 114 | + else pad_token |
| 115 | + ) |
| 116 | + |
| 117 | + super().__init__( |
| 118 | + vocab_file=vocab_file, |
| 119 | + merges_file=merges_file, |
| 120 | + tokenizer_file=tokenizer_file, |
| 121 | + unk_token=unk_token, |
| 122 | + bos_token=bos_token, |
| 123 | + eos_token=eos_token, |
| 124 | + pad_token=pad_token, |
| 125 | + **kwargs, |
| 126 | + ) |
| 127 | + |
| 128 | + # Copied from transformers.models.gpt2.tokenization_gpt2_fast.GPT2TokenizerFast.save_vocabulary |
| 129 | + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: |
| 130 | + files = self._tokenizer.model.save(save_directory, name=filename_prefix) |
| 131 | + return tuple(files) |
0 commit comments