2024-04-16 11:34:39 -07:00
|
|
|
import os
|
2024-04-01 13:22:06 -07:00
|
|
|
from typing import Optional, Union
|
2023-05-09 16:03:44 -07:00
|
|
|
|
2024-04-27 09:59:55 -07:00
|
|
|
import huggingface_hub
|
2023-06-28 09:46:58 -07:00
|
|
|
from transformers import (AutoTokenizer, PreTrainedTokenizer,
|
2023-05-09 16:03:44 -07:00
|
|
|
PreTrainedTokenizerFast)
|
|
|
|
|
|
2024-05-02 11:13:25 -07:00
|
|
|
from vllm.envs import VLLM_USE_MODELSCOPE
|
2023-06-17 03:07:40 -07:00
|
|
|
from vllm.logger import init_logger
|
2024-01-24 00:26:37 +01:00
|
|
|
from vllm.lora.request import LoRARequest
|
2024-04-13 06:35:50 +09:00
|
|
|
from vllm.transformers_utils.tokenizers import BaichuanTokenizer
|
2024-03-25 23:59:47 +09:00
|
|
|
from vllm.utils import make_async
|
2023-05-23 20:46:32 -07:00
|
|
|
|
|
|
|
|
logger = init_logger(__name__)
|
|
|
|
|
|
2023-05-09 16:03:44 -07:00
|
|
|
|
2024-03-15 16:37:01 -07:00
|
|
|
def get_cached_tokenizer(
|
|
|
|
|
tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast]
|
|
|
|
|
) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]:
|
|
|
|
|
"""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)
|
2024-03-29 18:46:39 -07:00
|
|
|
tokenizer_len = len(tokenizer)
|
2024-03-15 16:37:01 -07:00
|
|
|
|
2024-04-13 06:35:50 +09:00
|
|
|
class CachedTokenizer(tokenizer.__class__): # type: ignore
|
2024-03-15 16:37:01 -07:00
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
2024-03-29 18:46:39 -07:00
|
|
|
def __len__(self):
|
|
|
|
|
return tokenizer_len
|
|
|
|
|
|
2024-03-15 16:37:01 -07:00
|
|
|
CachedTokenizer.__name__ = f"Cached{tokenizer.__class__.__name__}"
|
|
|
|
|
|
|
|
|
|
tokenizer.__class__ = CachedTokenizer
|
|
|
|
|
return tokenizer
|
|
|
|
|
|
|
|
|
|
|
2023-05-09 16:03:44 -07:00
|
|
|
def get_tokenizer(
|
2023-06-28 09:46:58 -07:00
|
|
|
tokenizer_name: str,
|
2023-05-09 16:03:44 -07:00
|
|
|
*args,
|
2023-07-03 11:31:55 -07:00
|
|
|
tokenizer_mode: str = "auto",
|
2023-07-07 20:04:58 +02:00
|
|
|
trust_remote_code: bool = False,
|
2024-04-26 10:10:48 +08:00
|
|
|
revision: Optional[str] = None,
|
2024-04-16 11:34:39 -07:00
|
|
|
download_dir: Optional[str] = None,
|
2023-05-09 16:03:44 -07:00
|
|
|
**kwargs,
|
|
|
|
|
) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]:
|
2024-04-26 10:10:48 +08:00
|
|
|
"""Gets a tokenizer for the given model name via HuggingFace or ModelScope.
|
|
|
|
|
"""
|
2024-04-16 11:34:39 -07:00
|
|
|
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,
|
2024-04-26 10:10:48 +08:00
|
|
|
revision=revision,
|
2024-04-27 09:59:55 -07:00
|
|
|
local_files_only=huggingface_hub.constants.HF_HUB_OFFLINE,
|
2024-04-16 11:34:39 -07:00
|
|
|
# Ignore weights - we only need the tokenizer.
|
2024-05-01 07:38:50 +08:00
|
|
|
ignore_file_pattern=[".*.pt", ".*.safetensors", ".*.bin"])
|
2024-04-16 11:34:39 -07:00
|
|
|
tokenizer_name = tokenizer_path
|
|
|
|
|
|
2023-06-28 14:19:22 -07:00
|
|
|
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
|
|
|
|
|
|
2023-06-28 09:46:58 -07:00
|
|
|
try:
|
2023-07-07 20:04:58 +02:00
|
|
|
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
tokenizer_name,
|
|
|
|
|
*args,
|
2023-07-08 15:24:17 -07:00
|
|
|
trust_remote_code=trust_remote_code,
|
2024-04-26 10:10:48 +08:00
|
|
|
revision=revision,
|
2023-07-07 20:04:58 +02:00
|
|
|
**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.
|
2023-07-08 15:24:17 -07:00
|
|
|
if (not trust_remote_code and
|
2023-07-07 20:04:58 +02:00
|
|
|
("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 "
|
2023-07-08 15:24:17 -07:00
|
|
|
"library, consider setting `trust_remote_code=True` in LLM "
|
|
|
|
|
"or using the `--trust-remote-code` flag in the CLI.")
|
2023-07-07 20:04:58 +02:00
|
|
|
raise RuntimeError(err_msg) from e
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2023-11-30 18:35:50 -08:00
|
|
|
except AttributeError as e:
|
|
|
|
|
if "BaichuanTokenizer" in str(e):
|
|
|
|
|
# This is for the error "'BaichuanTokenizer' object has no
|
|
|
|
|
# attribute 'sp_model'".
|
|
|
|
|
tokenizer = BaichuanTokenizer.from_pretrained(
|
|
|
|
|
tokenizer_name,
|
|
|
|
|
*args,
|
|
|
|
|
trust_remote_code=trust_remote_code,
|
2024-04-26 10:10:48 +08:00
|
|
|
revision=revision,
|
2023-11-30 18:35:50 -08:00
|
|
|
**kwargs)
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2023-06-28 09:46:58 -07:00
|
|
|
|
|
|
|
|
if not isinstance(tokenizer, PreTrainedTokenizerFast):
|
|
|
|
|
logger.warning(
|
|
|
|
|
"Using a slow tokenizer. This might cause a significant "
|
|
|
|
|
"slowdown. Consider using a fast tokenizer instead.")
|
2024-03-15 16:37:01 -07:00
|
|
|
return get_cached_tokenizer(tokenizer)
|
2023-05-23 20:46:32 -07:00
|
|
|
|
|
|
|
|
|
2024-01-24 00:26:37 +01:00
|
|
|
def get_lora_tokenizer(lora_request: LoRARequest, *args,
|
|
|
|
|
**kwargs) -> Optional[PreTrainedTokenizer]:
|
|
|
|
|
if lora_request is None:
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
tokenizer = get_tokenizer(lora_request.lora_local_path, *args,
|
|
|
|
|
**kwargs)
|
|
|
|
|
except OSError as e:
|
|
|
|
|
# No tokenizer was found in the LoRA folder,
|
|
|
|
|
# use base model tokenizer
|
|
|
|
|
logger.warning(
|
2024-04-26 16:16:58 +09:00
|
|
|
"No tokenizer found in %s, using base model tokenizer instead. "
|
|
|
|
|
"(Exception: %s)", lora_request.lora_local_path, e)
|
2024-01-24 00:26:37 +01:00
|
|
|
tokenizer = None
|
|
|
|
|
return tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get_lora_tokenizer_async = make_async(get_lora_tokenizer)
|