__init__.py 2.99 KB
Newer Older
1
import logging
2
from contextlib import contextmanager
3
from typing import TYPE_CHECKING, Callable, Optional, Union
4
5

import vllm.envs as envs
6
7

if TYPE_CHECKING:
8
    from vllm.config import CompilationConfig, VllmConfig
9
10
11
else:
    CompilationConfig = None
    VllmConfig = None
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

logger = logging.getLogger(__name__)


def load_general_plugins():
    """WARNING: plugins can be loaded for multiple times in different
    processes. They should be designed in a way that they can be loaded
    multiple times without causing issues.
    """
    import sys
    if sys.version_info < (3, 10):
        from importlib_metadata import entry_points
    else:
        from importlib.metadata import entry_points

    allowed_plugins = envs.VLLM_PLUGINS

    discovered_plugins = entry_points(group='vllm.general_plugins')
30
31
32
    if len(discovered_plugins) == 0:
        logger.info("No plugins found.")
        return
33
34
35
36
37
38
39
40
41
42
    logger.info("Available plugins:")
    for plugin in discovered_plugins:
        logger.info("name=%s, value=%s, group=%s", plugin.name, plugin.value,
                    plugin.group)
    if allowed_plugins is None:
        logger.info("all available plugins will be loaded.")
        logger.info("set environment variable VLLM_PLUGINS to control"
                    " which plugins to load.")
    else:
        logger.info("plugins to load: %s", allowed_plugins)
43
44
45
46
47
    for plugin in discovered_plugins:
        if allowed_plugins is None or plugin.name in allowed_plugins:
            try:
                func = plugin.load()
                func()
48
                logger.info("plugin %s loaded.", plugin.name)
49
            except Exception:
50
                logger.exception("Failed to load plugin %s", plugin.name)
51
52
53
54
55
56
57
58
59
60
61
62


_torch_compile_backend: Optional[Union[Callable, str]] = None


def set_torch_compile_backend(backend: Union[Callable, str]):
    global _torch_compile_backend
    _torch_compile_backend = backend


def get_torch_compile_backend() -> Optional[Union[Callable, str]]:
    return _torch_compile_backend
63
64


65
_compilation_config: Optional[CompilationConfig] = None
66
67


68
69
70
def set_compilation_config(config: Optional[CompilationConfig]):
    global _compilation_config
    _compilation_config = config
71
72


73
74
def get_compilation_config() -> Optional[CompilationConfig]:
    return _compilation_config
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


_current_vllm_config: Optional[VllmConfig] = None


@contextmanager
def set_current_vllm_config(vllm_config: VllmConfig):
    """
    Temporarily set the current VLLM config.
    Used during model initialization.
    We save the current VLLM config in a global variable,
    so that all modules can access it, e.g. custom ops
    can access the VLLM config to determine how to dispatch.
    """
    global _current_vllm_config
    old_vllm_config = _current_vllm_config
    try:
        _current_vllm_config = vllm_config
        yield
    finally:
        _current_vllm_config = old_vllm_config


def get_current_vllm_config() -> VllmConfig:
    assert _current_vllm_config is not None, "Current VLLM config is not set."
    return _current_vllm_config