__init__.py 2.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project

import logging
from collections.abc import Callable
from typing import Any

import vllm.envs as envs

logger = logging.getLogger(__name__)

# Default plugins group will be loaded in all processes (process0, engine core
# process and worker processes).
OMNI_DEFAULT_PLUGINS_GROUP = "vllm_omni.general_plugins"
# Platform plugins group will be loaded in all processes when
# `vllm_omni.platforms.current_omni_platform` is called and the value not
# initialized.
OMNI_PLATFORM_PLUGINS_GROUP = "vllm_omni.platform_plugins"

# Make sure one process only loads plugins once.
omni_plugins_loaded = False


def load_omni_plugins_by_group(group: str) -> dict[str, Callable[[], Any]]:
    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 {}

    # Check if the only discovered plugin is the default one.
    is_default_group = group == OMNI_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)
    for plugin in discovered_plugins:
        log_level("- %s -> %s", plugin.name, plugin.value)

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

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

            try:
                func = plugin.load()
                plugins[plugin.name] = func
            except Exception:
                logger.exception("Failed to load plugin %s", plugin.name)

    return plugins


def load_omni_general_plugins() -> None:
    """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.
    """
    global omni_plugins_loaded
    if omni_plugins_loaded:
        return
    omni_plugins_loaded = True

    plugins = load_omni_plugins_by_group(group=OMNI_DEFAULT_PLUGINS_GROUP)
    # General plugins: we only need to execute the loaded functions.
    for func in plugins.values():
        func()


__all__ = [
    "load_omni_general_plugins",
    "OMNI_DEFAULT_PLUGINS_GROUP",
    "OMNI_PLATFORM_PLUGINS_GROUP",
]