|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union |
| 4 | + |
| 5 | +from vllm.transformers_utils.tokenizer import get_tokenizer |
| 6 | +from vllm.transformers_utils.tokenizer_base import (TokenizerBase, |
| 7 | + TokenizerRegistry) |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from vllm.entrypoints.chat_utils import ChatCompletionMessageParam |
| 11 | + |
| 12 | + |
| 13 | +class TestTokenizer(TokenizerBase): |
| 14 | + |
| 15 | + @classmethod |
| 16 | + def from_pretrained(cls, *args, **kwargs) -> "TestTokenizer": |
| 17 | + return TestTokenizer() |
| 18 | + |
| 19 | + @property |
| 20 | + def all_special_tokens_extended(self) -> List[str]: |
| 21 | + raise NotImplementedError() |
| 22 | + |
| 23 | + @property |
| 24 | + def all_special_tokens(self) -> List[str]: |
| 25 | + raise NotImplementedError() |
| 26 | + |
| 27 | + @property |
| 28 | + def all_special_ids(self) -> List[int]: |
| 29 | + raise NotImplementedError() |
| 30 | + |
| 31 | + @property |
| 32 | + def bos_token_id(self) -> int: |
| 33 | + return 0 |
| 34 | + |
| 35 | + @property |
| 36 | + def eos_token_id(self) -> int: |
| 37 | + return 1 |
| 38 | + |
| 39 | + @property |
| 40 | + def sep_token(self) -> str: |
| 41 | + raise NotImplementedError() |
| 42 | + |
| 43 | + @property |
| 44 | + def pad_token(self) -> str: |
| 45 | + raise NotImplementedError() |
| 46 | + |
| 47 | + @property |
| 48 | + def is_fast(self) -> bool: |
| 49 | + raise NotImplementedError() |
| 50 | + |
| 51 | + @property |
| 52 | + def vocab_size(self) -> int: |
| 53 | + raise NotImplementedError() |
| 54 | + |
| 55 | + @property |
| 56 | + def max_token_id(self) -> int: |
| 57 | + raise NotImplementedError() |
| 58 | + |
| 59 | + def __call__( |
| 60 | + self, |
| 61 | + text: Union[str, List[str], List[int]], |
| 62 | + text_pair: Optional[str] = None, |
| 63 | + add_special_tokens: bool = False, |
| 64 | + truncation: bool = False, |
| 65 | + max_length: Optional[int] = None, |
| 66 | + ): |
| 67 | + raise NotImplementedError() |
| 68 | + |
| 69 | + def get_vocab(self) -> Dict[str, int]: |
| 70 | + raise NotImplementedError() |
| 71 | + |
| 72 | + def get_added_vocab(self) -> Dict[str, int]: |
| 73 | + raise NotImplementedError() |
| 74 | + |
| 75 | + def encode_one( |
| 76 | + self, |
| 77 | + text: str, |
| 78 | + truncation: bool = False, |
| 79 | + max_length: Optional[int] = None, |
| 80 | + ) -> List[int]: |
| 81 | + raise NotImplementedError() |
| 82 | + |
| 83 | + def encode(self, |
| 84 | + text: str, |
| 85 | + add_special_tokens: Optional[bool] = None) -> List[int]: |
| 86 | + raise NotImplementedError() |
| 87 | + |
| 88 | + def apply_chat_template(self, |
| 89 | + messages: List["ChatCompletionMessageParam"], |
| 90 | + tools: Optional[List[Dict[str, Any]]] = None, |
| 91 | + **kwargs) -> List[int]: |
| 92 | + raise NotImplementedError() |
| 93 | + |
| 94 | + def convert_tokens_to_string(self, tokens: List[str]) -> str: |
| 95 | + raise NotImplementedError() |
| 96 | + |
| 97 | + def decode(self, |
| 98 | + ids: Union[List[int], int], |
| 99 | + skip_special_tokens: bool = True) -> str: |
| 100 | + raise NotImplementedError() |
| 101 | + |
| 102 | + def convert_ids_to_tokens( |
| 103 | + self, |
| 104 | + ids: List[int], |
| 105 | + skip_special_tokens: bool = True, |
| 106 | + ) -> List[str]: |
| 107 | + raise NotImplementedError() |
| 108 | + |
| 109 | + |
| 110 | +def test_customized_tokenizer(): |
| 111 | + TokenizerRegistry.register("test_tokenizer", |
| 112 | + "tests.tokenization.test_tokenizer_registry", |
| 113 | + "TestTokenizer") |
| 114 | + |
| 115 | + tokenizer = TokenizerRegistry.get_tokenizer("test_tokenizer") |
| 116 | + assert isinstance(tokenizer, TestTokenizer) |
| 117 | + assert tokenizer.bos_token_id == 0 |
| 118 | + assert tokenizer.eos_token_id == 1 |
| 119 | + |
| 120 | + tokenizer = get_tokenizer("test_tokenizer", tokenizer_mode="custom") |
| 121 | + assert isinstance(tokenizer, TestTokenizer) |
| 122 | + assert tokenizer.bos_token_id == 0 |
| 123 | + assert tokenizer.eos_token_id == 1 |
0 commit comments