test_tokenizer_registry.py 3.2 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
5
6

from vllm.transformers_utils.tokenizer import get_tokenizer
7
from vllm.transformers_utils.tokenizer_base import TokenizerBase, TokenizerRegistry
8
9
10
11
12
13
14
15
16
17
18

if TYPE_CHECKING:
    from vllm.entrypoints.chat_utils import ChatCompletionMessageParam


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

    @property
19
    def all_special_tokens(self) -> list[str]:
20
21
22
        raise NotImplementedError()

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

54
55
56
57
    @property
    def truncation_side(self) -> str:
        raise NotImplementedError()

58
59
    def __call__(
        self,
60
61
        text: str | list[str] | list[int],
        text_pair: str | None = None,
62
63
        add_special_tokens: bool = False,
        truncation: bool = False,
64
        max_length: int | None = None,
65
66
67
    ):
        raise NotImplementedError()

68
    def get_vocab(self) -> dict[str, int]:
69
70
        raise NotImplementedError()

71
    def get_added_vocab(self) -> dict[str, int]:
72
73
74
75
76
77
        raise NotImplementedError()

    def encode_one(
        self,
        text: str,
        truncation: bool = False,
78
        max_length: int | None = None,
79
    ) -> list[int]:
80
81
        raise NotImplementedError()

82
    def encode(self, text: str, add_special_tokens: bool | None = None) -> list[int]:
83
84
        raise NotImplementedError()

85
86
87
    def apply_chat_template(
        self,
        messages: list["ChatCompletionMessageParam"],
88
        tools: list[dict[str, Any]] | None = None,
89
90
        **kwargs,
    ) -> list[int]:
91
92
        raise NotImplementedError()

93
    def convert_tokens_to_string(self, tokens: list[str]) -> str:
94
95
        raise NotImplementedError()

96
    def decode(self, ids: list[int] | int, skip_special_tokens: bool = True) -> str:
97
98
99
100
        raise NotImplementedError()

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


def test_customized_tokenizer():
108
109
110
    TokenizerRegistry.register(
        "test_tokenizer", "tests.tokenization.test_tokenizer_registry", "TestTokenizer"
    )
111
112
113
114
115
116
117
118
119
120

    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