tokenizer.py 9 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
import contextlib
4
import os
5
import warnings
6
from pathlib import Path
7
from types import MethodType
8
from typing import Optional, Union
9

10
import huggingface_hub
11
from transformers import (AutoTokenizer, PreTrainedTokenizer,
12
13
                          PreTrainedTokenizerFast)

14
from vllm.envs import VLLM_USE_MODELSCOPE
Woosuk Kwon's avatar
Woosuk Kwon committed
15
from vllm.logger import init_logger
16
from vllm.lora.request import LoRARequest
17
18
from vllm.transformers_utils.tokenizer_base import (TokenizerBase,
                                                    TokenizerRegistry)
19
from vllm.transformers_utils.tokenizers import MistralTokenizer
20
from vllm.transformers_utils.utils import check_gguf_file
21
from vllm.utils import make_async
22
23
24

logger = init_logger(__name__)

25
AnyTokenizer = Union[PreTrainedTokenizer, PreTrainedTokenizerFast,
26
                     TokenizerBase]
27

28

29
30
31
32
33
34
35
36
37
38
39
40
41
def decode_tokens(
    tokenizer: AnyTokenizer,
    token_ids: list[int],
    *,
    skip_special_tokens: bool = False,
) -> str:
    """
    Backend-agnostic equivalent of HF's
    :code:`tokenizer.decode(token_ids, skip_special_tokens=...)`.
    """
    return tokenizer.decode(token_ids, skip_special_tokens=skip_special_tokens)


42
43
44
45
46
47
48
49
50
51
def encode_tokens(
    tokenizer: AnyTokenizer,
    text: str,
    *,
    add_special_tokens: Optional[bool] = None,
) -> list[int]:
    """
    Backend-agnostic equivalent of HF's
    :code:`tokenizer.encode(text, add_special_tokens=...)`.
    """
52
    if add_special_tokens is not None:
53
54
55
56
        return tokenizer.encode(text, add_special_tokens=add_special_tokens)
    return tokenizer.encode(text)


57
def get_cached_tokenizer(tokenizer: AnyTokenizer) -> AnyTokenizer:
58
59
60
61
62
63
64
65
66
67
68
69
    """Get tokenizer with cached properties.

    This will patch the tokenizer object in place.

    By default, transformers will recompute multiple tokenizer properties
    each time they are called, leading to a significant slowdown. This
    function caches these properties for faster access."""

    tokenizer_all_special_ids = set(tokenizer.all_special_ids)
    tokenizer_all_special_tokens_extended = (
        tokenizer.all_special_tokens_extended)
    tokenizer_all_special_tokens = set(tokenizer.all_special_tokens)
70
    tokenizer_vocab = tokenizer.get_vocab()
71
    tokenizer_len = len(tokenizer)
72

73
    max_token_id = max(tokenizer_vocab.values())
74
75
76
77
78
79
80
    # Some tokenizers (e.g., QwenTokenizer) have special tokens that
    # are added and included in the implementation of the vocab_size
    # property, but not in get_vocab(); if there is an implementation
    # of vocab size, we should take the greater value.
    if hasattr(tokenizer, "vocab_size"):
        with contextlib.suppress(NotImplementedError):
            max_token_id = max(max_token_id, tokenizer.vocab_size)
81

82
    class CachedTokenizer(tokenizer.__class__):  # type: ignore
83
84
85
86
87
88
89
90
91
92
93
94
95

        @property
        def all_special_ids(self):
            return tokenizer_all_special_ids

        @property
        def all_special_tokens(self):
            return tokenizer_all_special_tokens

        @property
        def all_special_tokens_extended(self):
            return tokenizer_all_special_tokens_extended

96
97
98
99
        @property
        def max_token_id(self):
            return max_token_id

100
101
102
        def get_vocab(self):
            return tokenizer_vocab

103
104
105
        def __len__(self):
            return tokenizer_len

106
107
108
109
110
111
    CachedTokenizer.__name__ = f"Cached{tokenizer.__class__.__name__}"

    tokenizer.__class__ = CachedTokenizer
    return tokenizer


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def patch_padding_side(tokenizer: PreTrainedTokenizer) -> None:
    """Patch _pad method to accept `padding_side` for older tokenizers."""
    orig_pad = tokenizer._pad

    def _pad(
        self: PreTrainedTokenizer,
        *args,
        padding_side: Optional[str] = None,
        **kwargs,
    ):
        if padding_side is not None and padding_side != self.padding_side:
            msg = ("`padding_side` argument is not supported by "
                   f"{type(tokenizer).__name__} and will be ignored.")
            warnings.warn(msg, stacklevel=2)

        return orig_pad(*args, **kwargs)

    tokenizer._pad = MethodType(_pad, tokenizer)


132
def get_tokenizer(
133
    tokenizer_name: Union[str, Path],
134
    *args,
135
    tokenizer_mode: str = "auto",
136
    trust_remote_code: bool = False,
137
    revision: Optional[str] = None,
138
    download_dir: Optional[str] = None,
139
    **kwargs,
140
) -> AnyTokenizer:
141
142
    """Gets a tokenizer for the given model name via HuggingFace or ModelScope.
    """
143
144
145
146
147
148
149
150
151
152
153
    if VLLM_USE_MODELSCOPE:
        # download model from ModelScope hub,
        # lazy import so that modelscope is not required for normal use.
        # pylint: disable=C.
        from modelscope.hub.snapshot_download import snapshot_download

        # Only set the tokenizer here, model will be downloaded on the workers.
        if not os.path.exists(tokenizer_name):
            tokenizer_path = snapshot_download(
                model_id=tokenizer_name,
                cache_dir=download_dir,
154
                revision=revision,
155
                local_files_only=huggingface_hub.constants.HF_HUB_OFFLINE,
156
                # Ignore weights - we only need the tokenizer.
157
                ignore_file_pattern=[".*.pt", ".*.safetensors", ".*.bin"])
158
159
            tokenizer_name = tokenizer_path

160
161
162
163
164
165
    if tokenizer_mode == "slow":
        if kwargs.get("use_fast", False):
            raise ValueError(
                "Cannot use the fast tokenizer in slow tokenizer mode.")
        kwargs["use_fast"] = False

166
167
168
    if "truncation_side" not in kwargs:
        kwargs["truncation_side"] = "left"

169
    # Separate model folder from file path for GGUF models
170
    is_gguf = check_gguf_file(tokenizer_name)
171
172
173
174
    if is_gguf:
        kwargs["gguf_file"] = Path(tokenizer_name).name
        tokenizer_name = Path(tokenizer_name).parent

175
176
177
178
179
    # if tokenizer is from official mistral org
    is_from_mistral_org = str(tokenizer_name).split("/")[0] == "mistralai"
    if is_from_mistral_org and tokenizer_mode != "mistral":
        warnings.warn(
            'It is strongly recommended to run mistral models with '
180
            '`--tokenizer-mode "mistral"` to ensure correct '
181
182
183
            'encoding and decoding.',
            FutureWarning,
            stacklevel=2)
184
185

    tokenizer: AnyTokenizer
186
187
188
    if tokenizer_mode == "mistral":
        tokenizer = MistralTokenizer.from_pretrained(str(tokenizer_name),
                                                     revision=revision)
189
190
191
192
193
194
    elif tokenizer_mode == "custom":
        tokenizer = TokenizerRegistry.get_tokenizer(str(tokenizer_name),
                                                    *args,
                                                    revision=revision,
                                                    download_dir=download_dir,
                                                    **kwargs)
195
196
197
    else:
        try:
            tokenizer = AutoTokenizer.from_pretrained(
198
199
200
                tokenizer_name,
                *args,
                trust_remote_code=trust_remote_code,
201
                revision=revision,
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
                **kwargs,
            )
        except ValueError as e:
            # If the error pertains to the tokenizer class not existing or not
            # currently being imported,
            # suggest using the --trust-remote-code flag.
            if not trust_remote_code and (
                    "does not exist or is not currently imported." in str(e)
                    or "requires you to execute the tokenizer file" in str(e)):
                err_msg = ("Failed to load the tokenizer. If the tokenizer "
                           "is a custom tokenizer not yet available in the "
                           "HuggingFace transformers library, consider "
                           "setting `trust_remote_code=True` in LLM or using "
                           "the `--trust-remote-code` flag in the CLI.")
                raise RuntimeError(err_msg) from e
            else:
                raise e

220
221
222
223
        # NOTE: We can remove this after https://github.com/THUDM/ChatGLM3/issues/1324
        if type(tokenizer).__name__ in ("ChatGLMTokenizer",
                                        "ChatGLM4Tokenizer"):
            assert isinstance(tokenizer, PreTrainedTokenizer)
224
            patch_padding_side(tokenizer)
225

226
227
228
229
230
        if not isinstance(tokenizer, PreTrainedTokenizerFast):
            logger.warning(
                "Using a slow tokenizer. This might cause a significant "
                "slowdown. Consider using a fast tokenizer instead.")
        tokenizer = get_cached_tokenizer(tokenizer)
231

232
    return tokenizer
233
234


235
def get_lora_tokenizer(lora_request: LoRARequest, *args,
236
                       **kwargs) -> Optional[AnyTokenizer]:
237
238
239
    if lora_request is None:
        return None
    try:
240
        tokenizer = get_tokenizer(lora_request.lora_path, *args, **kwargs)
241
    except Exception as e:
242
243
244
        # No tokenizer was found in the LoRA folder,
        # use base model tokenizer
        logger.warning(
245
            "No tokenizer found in %s, using base model tokenizer instead. "
246
            "(Exception: %s)", lora_request.lora_path, e)
247
248
249
250
251
        tokenizer = None
    return tokenizer


get_lora_tokenizer_async = make_async(get_lora_tokenizer)