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

Zhuohan Li's avatar
Zhuohan Li committed
4
import uuid
5
import warnings
6
from typing import Any, TypeVar
Zhuohan Li's avatar
Zhuohan Li committed
7
8

import torch
9

10
from vllm.logger import init_logger
11

12
13
14
_DEPRECATED_MAPPINGS = {
    "cprofile": "profiling",
    "cprofile_context": "profiling",
15
    # Used by lm-eval
16
17
    "get_open_port": "network_utils",
}
18
19
20


def __getattr__(name: str) -> Any:  # noqa: D401 - short deprecation docstring
21
22
23
    """Module-level getattr to handle deprecated utilities."""
    if name in _DEPRECATED_MAPPINGS:
        submodule_name = _DEPRECATED_MAPPINGS[name]
24
25
        warnings.warn(
            f"vllm.utils.{name} is deprecated and will be removed in a future version. "
26
            f"Use vllm.utils.{submodule_name}.{name} instead.",
27
28
29
            DeprecationWarning,
            stacklevel=2,
        )
30
31
        module = __import__(f"vllm.utils.{submodule_name}", fromlist=[submodule_name])
        return getattr(module, name)
32
33
34
35
36
    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def __dir__() -> list[str]:
    # expose deprecated names in dir() for better UX/tab-completion
37
    return sorted(list(globals().keys()) + list(_DEPRECATED_MAPPINGS.keys()))
38
39


40
41
logger = init_logger(__name__)

42
43
44
45
46
47
# This value is chosen to have a balance between ITL and TTFT. Note it is
# not optimized for throughput.
DEFAULT_MAX_NUM_BATCHED_TOKENS = 2048
POOLING_MODEL_MAX_NUM_BATCHED_TOKENS = 32768
MULTIMODAL_MODEL_MAX_NUM_BATCHED_TOKENS = 5120

48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Constants related to forcing the attention backend selection

# String name of register which may be set in order to
# force auto-selection of attention backend by Attention
# wrapper
STR_BACKEND_ENV_VAR: str = "VLLM_ATTENTION_BACKEND"

# Possible string values of STR_BACKEND_ENV_VAR
# register, corresponding to possible backends
STR_FLASHINFER_ATTN_VAL: str = "FLASHINFER"
STR_XFORMERS_ATTN_VAL: str = "XFORMERS"
STR_FLASH_ATTN_VAL: str = "FLASH_ATTN"
STR_INVALID_VAL: str = "INVALID"

62

63
64
T = TypeVar("T")

65

Cyrus Leung's avatar
Cyrus Leung committed
66
67
def random_uuid() -> str:
    return str(uuid.uuid4().hex)
68
69


70
def length_from_prompt_token_ids_or_embeds(
71
72
    prompt_token_ids: list[int] | None,
    prompt_embeds: torch.Tensor | None,
73
) -> int:
74
    """Calculate the request length (in number of tokens) give either
75
76
    prompt_token_ids or prompt_embeds.
    """
77
78
    prompt_token_len = None if prompt_token_ids is None else len(prompt_token_ids)
    prompt_embeds_len = None if prompt_embeds is None else len(prompt_embeds)
79
80
81

    if prompt_token_len is None:
        if prompt_embeds_len is None:
82
            raise ValueError("Neither prompt_token_ids nor prompt_embeds were defined.")
83
84
        return prompt_embeds_len
    else:
85
        if prompt_embeds_len is not None and prompt_embeds_len != prompt_token_len:
86
87
88
            raise ValueError(
                "Prompt token ids and prompt embeds had different lengths"
                f" prompt_token_ids={prompt_token_len}"
89
90
                f" prompt_embeds={prompt_embeds_len}"
            )
91
        return prompt_token_len