test_tokenizer_registry.py 3.55 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

4
from typing import TYPE_CHECKING, Any, Optional, Union
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

from vllm.transformers_utils.tokenizer import get_tokenizer
from vllm.transformers_utils.tokenizer_base import (TokenizerBase,
                                                    TokenizerRegistry)

if TYPE_CHECKING:
    from vllm.entrypoints.chat_utils import ChatCompletionMessageParam


class TestTokenizer(TokenizerBase):

    @classmethod
    def from_pretrained(cls, *args, **kwargs) -> "TestTokenizer":
        return TestTokenizer()

    @property
21
    def all_special_tokens_extended(self) -> list[str]:
22
23
24
        raise NotImplementedError()

    @property
25
    def all_special_tokens(self) -> list[str]:
26
27
28
        raise NotImplementedError()

    @property
29
    def all_special_ids(self) -> list[int]:
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
        raise NotImplementedError()

    @property
    def bos_token_id(self) -> int:
        return 0

    @property
    def eos_token_id(self) -> int:
        return 1

    @property
    def sep_token(self) -> str:
        raise NotImplementedError()

    @property
    def pad_token(self) -> str:
        raise NotImplementedError()

    @property
    def is_fast(self) -> bool:
        raise NotImplementedError()

    @property
    def vocab_size(self) -> int:
        raise NotImplementedError()

    @property
    def max_token_id(self) -> int:
        raise NotImplementedError()

60
61
62
63
    @property
    def truncation_side(self) -> str:
        raise NotImplementedError()

64
65
    def __call__(
        self,
66
        text: Union[str, list[str], list[int]],
67
68
69
70
71
72
73
        text_pair: Optional[str] = None,
        add_special_tokens: bool = False,
        truncation: bool = False,
        max_length: Optional[int] = None,
    ):
        raise NotImplementedError()

74
    def get_vocab(self) -> dict[str, int]:
75
76
        raise NotImplementedError()

77
    def get_added_vocab(self) -> dict[str, int]:
78
79
80
81
82
83
84
        raise NotImplementedError()

    def encode_one(
        self,
        text: str,
        truncation: bool = False,
        max_length: Optional[int] = None,
85
    ) -> list[int]:
86
87
88
89
        raise NotImplementedError()

    def encode(self,
               text: str,
90
               add_special_tokens: Optional[bool] = None) -> list[int]:
91
92
93
        raise NotImplementedError()

    def apply_chat_template(self,
94
95
96
                            messages: list["ChatCompletionMessageParam"],
                            tools: Optional[list[dict[str, Any]]] = None,
                            **kwargs) -> list[int]:
97
98
        raise NotImplementedError()

99
    def convert_tokens_to_string(self, tokens: list[str]) -> str:
100
101
102
        raise NotImplementedError()

    def decode(self,
103
               ids: Union[list[int], int],
104
105
106
107
108
               skip_special_tokens: bool = True) -> str:
        raise NotImplementedError()

    def convert_ids_to_tokens(
        self,
109
        ids: list[int],
110
        skip_special_tokens: bool = True,
111
    ) -> list[str]:
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        raise NotImplementedError()


def test_customized_tokenizer():
    TokenizerRegistry.register("test_tokenizer",
                               "tests.tokenization.test_tokenizer_registry",
                               "TestTokenizer")

    tokenizer = TokenizerRegistry.get_tokenizer("test_tokenizer")
    assert isinstance(tokenizer, TestTokenizer)
    assert tokenizer.bos_token_id == 0
    assert tokenizer.eos_token_id == 1

    tokenizer = get_tokenizer("test_tokenizer", tokenizer_mode="custom")
    assert isinstance(tokenizer, TestTokenizer)
    assert tokenizer.bos_token_id == 0
    assert tokenizer.eos_token_id == 1