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

4
import contextlib
5
import copy
6
import importlib.util
7
import os
8
from functools import lru_cache
9
from pathlib import Path
10
from typing import TYPE_CHECKING, Any, TypeAlias
11

12
import huggingface_hub
13
from transformers import AutoTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast
14
from typing_extensions import assert_never
15

16
from vllm import envs
Woosuk Kwon's avatar
Woosuk Kwon committed
17
from vllm.logger import init_logger
18
from vllm.transformers_utils.config import get_sentence_transformer_tokenizer_config
19
from vllm.transformers_utils.gguf_utils import get_gguf_file_path_from_hf
20
from vllm.transformers_utils.repo_utils import list_filtered_repo_files
21
from vllm.transformers_utils.tokenizers import MistralTokenizer
22
23
24
25
26
27
from vllm.transformers_utils.utils import (
    check_gguf_file,
    is_gguf,
    is_remote_gguf,
    split_remote_gguf,
)
28

29
30
if TYPE_CHECKING:
    from vllm.config import ModelConfig
31
32
33
34
    from vllm.transformers_utils.tokenizer_base import TokenizerBase
else:
    ModelConfig = Any
    TokenizerBase = Any
35

36
37
logger = init_logger(__name__)

38
AnyTokenizer: TypeAlias = PreTrainedTokenizer | PreTrainedTokenizerFast | TokenizerBase
39

40

41
42
43
44
def decode_tokens(
    tokenizer: AnyTokenizer,
    token_ids: list[int],
    *,
45
    skip_special_tokens: bool | None = None,
46
47
48
) -> str:
    """
    Backend-agnostic equivalent of HF's
49
    `tokenizer.decode(token_ids, ...)`.
50

51
    `skip_special_tokens=None` means to use the backend's default
52
    settings.
53
    """
54
    if skip_special_tokens is not None:
55
        return tokenizer.decode(token_ids, skip_special_tokens=skip_special_tokens)
56

57
    return tokenizer.decode(token_ids)
58
59


60
61
62
63
def encode_tokens(
    tokenizer: AnyTokenizer,
    text: str,
    *,
64
65
66
    truncation: bool | None = None,
    max_length: int | None = None,
    add_special_tokens: bool | None = None,
67
68
69
) -> list[int]:
    """
    Backend-agnostic equivalent of HF's
70
    `tokenizer.encode(text, ...)`.
71

72
    `add_special_tokens=None` means to use the backend's default
73
    settings.
74
    """
75
76
77
78
79
80
81
82

    kw_args: dict[str, Any] = {}
    if max_length is not None:
        kw_args["max_length"] = max_length

    if truncation is not None:
        kw_args["truncation"] = truncation

83
    if add_special_tokens is not None:
84
        kw_args["add_special_tokens"] = add_special_tokens
85

86
    return tokenizer.encode(text, **kw_args)
87
88


89
def get_cached_tokenizer(tokenizer: AnyTokenizer) -> AnyTokenizer:
90
    """
91
    By default, transformers will recompute multiple tokenizer properties
92
93
94
95
    each time they are called, leading to a significant slowdown.
    This proxy caches these properties for faster access.
    """
    cached_tokenizer = copy.copy(tokenizer)
96

97
98
    tokenizer_all_special_ids = tokenizer.all_special_ids
    tokenizer_all_special_tokens = tokenizer.all_special_tokens
99
    tokenizer_vocab = tokenizer.get_vocab()
100
    tokenizer_len = len(tokenizer)
101

102
    max_token_id = max(tokenizer_vocab.values())
103
104
105
106
107
108
109
    # 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)
110

111
    class CachedTokenizer(tokenizer.__class__):  # type: ignore
112
        @property
113
        def all_special_ids(self) -> list[int]:
114
115
116
            return tokenizer_all_special_ids

        @property
117
        def all_special_tokens(self) -> list[str]:
118
119
120
            return tokenizer_all_special_tokens

        @property
121
        def max_token_id(self) -> int:
122
123
            return max_token_id

124
        def get_vocab(self) -> dict[str, int]:
125
126
            return tokenizer_vocab

127
        def __len__(self) -> int:
128
129
            return tokenizer_len

130
        def __reduce__(self):
131
            return get_cached_tokenizer, (tokenizer,)
132

133
134
    CachedTokenizer.__name__ = f"Cached{tokenizer.__class__.__name__}"

135
136
    cached_tokenizer.__class__ = CachedTokenizer
    return cached_tokenizer
137
138


139
def get_tokenizer(
140
    tokenizer_name: str | Path,
141
    *args,
142
    tokenizer_mode: str = "auto",
143
    trust_remote_code: bool = False,
144
145
    revision: str | None = None,
    download_dir: str | None = None,
146
    **kwargs,
147
) -> AnyTokenizer:
148
    """Gets a tokenizer for the given model name via HuggingFace or ModelScope."""
149
    if envs.VLLM_USE_MODELSCOPE:
150
151
152
153
154
        # 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

155
156
157
        # avoid circuit import
        from vllm.model_executor.model_loader.weight_utils import get_lock

158
159
        # Only set the tokenizer here, model will be downloaded on the workers.
        if not os.path.exists(tokenizer_name):
160
161
162
163
164
165
166
167
168
            # Use file lock to prevent multiple processes from
            # downloading the same file at the same time.
            with get_lock(tokenizer_name, download_dir):
                tokenizer_path = snapshot_download(
                    model_id=tokenizer_name,
                    cache_dir=download_dir,
                    revision=revision,
                    local_files_only=huggingface_hub.constants.HF_HUB_OFFLINE,
                    # Ignore weights - we only need the tokenizer.
169
170
                    ignore_file_pattern=[".*.pt", ".*.safetensors", ".*.bin"],
                )
171
                tokenizer_name = tokenizer_path
172

173
174
    if tokenizer_mode == "slow":
        if kwargs.get("use_fast", False):
175
            raise ValueError("Cannot use the fast tokenizer in slow tokenizer mode.")
176
177
        kwargs["use_fast"] = False

178
179
180
    if "truncation_side" not in kwargs:
        kwargs["truncation_side"] = "left"

181
    # Separate model folder from file path for GGUF models
182
183
184
185
186
    if is_gguf(tokenizer_name):
        if check_gguf_file(tokenizer_name):
            kwargs["gguf_file"] = Path(tokenizer_name).name
            tokenizer_name = Path(tokenizer_name).parent
        elif is_remote_gguf(tokenizer_name):
187
188
189
190
191
192
193
194
            tokenizer_name, quant_type = split_remote_gguf(tokenizer_name)
            # Get the HuggingFace Hub path for the GGUF file
            gguf_file = get_gguf_file_path_from_hf(
                tokenizer_name,
                quant_type,
                revision=revision,
            )
            kwargs["gguf_file"] = gguf_file
195

196
197
198
199
200
201
202
203
204
    # if `tokenizer_mode` == "auto", check if tokenizer can be loaded via Mistral format
    # first to use official Mistral tokenizer if possible.
    mistral_common_installed = importlib.util.find_spec("mistral_common") is not None
    if tokenizer_mode == "auto" and mistral_common_installed:
        allow_patterns = ["tekken.json", "tokenizer.model.v*"]
        files_list = list_filtered_repo_files(
            model_name_or_path=str(tokenizer_name),
            allow_patterns=allow_patterns,
            revision=revision,
205
        )
206
207
        if len(files_list) > 0:
            tokenizer_mode = "mistral"
208
209

    tokenizer: AnyTokenizer
210
    if tokenizer_mode == "mistral":
211
        logger.debug_once(f"Loading MistralTokenizer from {tokenizer_name}")
212
213
214
        tokenizer = MistralTokenizer.from_pretrained(
            str(tokenizer_name), revision=revision
        )
215
    elif tokenizer_mode == "custom":
216
        from vllm.transformers_utils.tokenizer_base import TokenizerRegistry
217

218
        logger.debug_once(f"Loading CustomTokenizer from {tokenizer_name}")
219
220
221
222
223
224
225
        tokenizer = TokenizerRegistry.get_tokenizer(
            str(tokenizer_name),
            *args,
            revision=revision,
            download_dir=download_dir,
            **kwargs,
        )
226
227
    else:
        try:
228
            logger.debug_once(f"Loading AutoTokenizer from {tokenizer_name}")
229
            tokenizer = AutoTokenizer.from_pretrained(
230
231
232
                tokenizer_name,
                *args,
                trust_remote_code=trust_remote_code,
233
                revision=revision,
234
235
236
237
238
239
240
                **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 (
241
242
243
244
245
246
247
248
249
250
                "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."
                )
251
252
253
254
                raise RuntimeError(err_msg) from e
            else:
                raise e

255
256
257
        # The special_tokens in tokenizer should also be
        # controlled by do_lower_case in encoder_config
        encoder_config = get_sentence_transformer_tokenizer_config(
258
259
            tokenizer_name, revision
        )
260
        if isinstance(encoder_config, dict) and encoder_config.get(
261
262
            "do_lower_case", False
        ):
263
            special_tokens_map = {
264
                k: v.lower() for k, v in tokenizer.special_tokens_map.items()
265
266
267
            }
            tokenizer.add_special_tokens(special_tokens_map)

268
269
270
        if not isinstance(tokenizer, PreTrainedTokenizerFast):
            logger.warning(
                "Using a slow tokenizer. This might cause a significant "
271
272
                "slowdown. Consider using a fast tokenizer instead."
            )
273
        tokenizer = get_cached_tokenizer(tokenizer)
274

275
    return tokenizer
276
277


278
279
280
281
cached_get_tokenizer = lru_cache(get_tokenizer)


def cached_tokenizer_from_config(
282
    model_config: ModelConfig,
283
284
285
286
287
    **kwargs: Any,
):
    return cached_get_tokenizer(
        model_config.tokenizer,
        tokenizer_mode=model_config.tokenizer_mode,
288
        revision=model_config.tokenizer_revision,
289
290
291
292
293
        trust_remote_code=model_config.trust_remote_code,
        **kwargs,
    )


294
295
296
297
298
299
300
301
def init_tokenizer_from_configs(model_config: ModelConfig):
    runner_type = model_config.runner_type
    if runner_type == "generate" or runner_type == "draft":
        truncation_side = "left"
    elif runner_type == "pooling":
        truncation_side = "right"
    else:
        assert_never(runner_type)
302

303
304
305
306
307
308
309
    return get_tokenizer(
        model_config.tokenizer,
        tokenizer_mode=model_config.tokenizer_mode,
        trust_remote_code=model_config.trust_remote_code,
        revision=model_config.tokenizer_revision,
        truncation_side=truncation_side,
    )