__init__.py 2.22 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
Zhuohan Li's avatar
Zhuohan Li committed
7
8

import torch
9

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


def __getattr__(name: str) -> Any:  # noqa: D401 - short deprecation docstring
19
20
21
    """Module-level getattr to handle deprecated utilities."""
    if name in _DEPRECATED_MAPPINGS:
        submodule_name = _DEPRECATED_MAPPINGS[name]
22
23
        warnings.warn(
            f"vllm.utils.{name} is deprecated and will be removed in a future version. "
24
            f"Use vllm.utils.{submodule_name}.{name} instead.",
25
26
27
            DeprecationWarning,
            stacklevel=2,
        )
28
29
        module = __import__(f"vllm.utils.{submodule_name}", fromlist=[submodule_name])
        return getattr(module, name)
30
31
32
33
34
    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
35
    return sorted(list(globals().keys()) + list(_DEPRECATED_MAPPINGS.keys()))
36
37


38
39
MASK_64_BITS = (1 << 64) - 1

40

Cyrus Leung's avatar
Cyrus Leung committed
41
def random_uuid() -> str:
42
    return f"{uuid.uuid4().int & MASK_64_BITS:016x}"  # 16 hex chars
43
44


45
def length_from_prompt_token_ids_or_embeds(
46
47
    prompt_token_ids: list[int] | None,
    prompt_embeds: torch.Tensor | None,
48
) -> int:
49
    """Calculate the request length (in number of tokens) give either
50
51
    prompt_token_ids or prompt_embeds.
    """
52
53
    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)
54
55
56

    if prompt_token_len is None:
        if prompt_embeds_len is None:
57
            raise ValueError("Neither prompt_token_ids nor prompt_embeds were defined.")
58
59
        return prompt_embeds_len
    else:
60
        if prompt_embeds_len is not None and prompt_embeds_len != prompt_token_len:
61
62
63
            raise ValueError(
                "Prompt token ids and prompt embeds had different lengths"
                f" prompt_token_ids={prompt_token_len}"
64
65
                f" prompt_embeds={prompt_embeds_len}"
            )
66
        return prompt_token_len