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

import vllm.envs as envs
6
7

if TYPE_CHECKING:
8
    from vllm.config import VllmConfig
9
10
11

logger = logging.getLogger(__name__)

12
13
14
# make sure one process only loads plugins once
plugins_loaded = False

15
16
17
18
19
20

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.
    """
21
22
23
24
    global plugins_loaded
    if plugins_loaded:
        return
    plugins_loaded = True
25
26
27
28
29
30
31
32
33
    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')
34
35
36
    if len(discovered_plugins) == 0:
        logger.info("No plugins found.")
        return
37
38
39
40
41
42
43
44
45
46
    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)
47
48
49
50
51
    for plugin in discovered_plugins:
        if allowed_plugins is None or plugin.name in allowed_plugins:
            try:
                func = plugin.load()
                func()
52
                logger.info("plugin %s loaded.", plugin.name)
53
            except Exception:
54
                logger.exception("Failed to load plugin %s", plugin.name)
55
56


57
_current_vllm_config: Optional["VllmConfig"] = None
58
59
60


@contextmanager
61
def set_current_vllm_config(vllm_config: "VllmConfig"):
62
63
64
65
66
67
68
69
70
71
72
73
74
    """
    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:
75
76
77
78
        logger.debug("enabled custom ops: %s",
                     vllm_config.compilation_config.enabled_custom_ops)
        logger.debug("disabled custom ops: %s",
                     vllm_config.compilation_config.disabled_custom_ops)
79
80
81
        _current_vllm_config = old_vllm_config


82
83
84
85
86
87
88
89
def get_current_vllm_config() -> "VllmConfig":
    if _current_vllm_config is None:
        # in ci, usually when we test custom ops/modules directly,
        # we don't set the vllm config. In that case, we set a default
        # config.
        logger.warning("Current VLLM config is not set.")
        from vllm.config import VllmConfig
        return VllmConfig()
90
    return _current_vllm_config