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

4
import logging
5
from typing import Any, Callable
6
7

import vllm.envs as envs
8

9
10
logger = logging.getLogger(__name__)

11
12
DEFAULT_PLUGINS_GROUP = 'vllm.general_plugins'

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

16

17
def load_plugins_by_group(group: str) -> dict[str, Callable[[], Any]]:
18
19
20
21
22
23
24
25
26
27
28
29
    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=group)
    if len(discovered_plugins) == 0:
        logger.debug("No plugins for group %s found.", group)
        return {}
30

31
32
33
34
35
36
    # Check if the only discovered plugin is the default one
    is_default_group = (group == DEFAULT_PLUGINS_GROUP)
    # Use INFO for non-default groups and DEBUG for the default group
    log_level = logger.debug if is_default_group else logger.info

    log_level("Available plugins for group %s:", group)
37
    for plugin in discovered_plugins:
38
        log_level("- %s -> %s", plugin.name, plugin.value)
39

40
    if allowed_plugins is None:
41
42
        log_level("All plugins in this group will be loaded. "
                  "Set `VLLM_PLUGINS` to control which plugins to load.")
43
44

    plugins = dict[str, Callable[[], Any]]()
45
46
    for plugin in discovered_plugins:
        if allowed_plugins is None or plugin.name in allowed_plugins:
47
            if allowed_plugins is not None:
48
                log_level("Loading plugin %s", plugin.name)
49

50
51
52
53
54
            try:
                func = plugin.load()
                plugins[plugin.name] = func
            except Exception:
                logger.exception("Failed to load plugin %s", plugin.name)
55

56
57
58
    return plugins


59
60
61
62
63
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.
    """
64
65
66
67
    global plugins_loaded
    if plugins_loaded:
        return
    plugins_loaded = True
68

69
    plugins = load_plugins_by_group(group=DEFAULT_PLUGINS_GROUP)
70
71
72
    # general plugins, we only need to execute the loaded functions
    for func in plugins.values():
        func()