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

4
import logging
5
6
from collections.abc import Callable
from typing import Any
7
8

import vllm.envs as envs
9

10
11
logger = logging.getLogger(__name__)

12
13
# Default plugins group will be loaded in all processes(process0, engine core
# process and worker processes)
14
DEFAULT_PLUGINS_GROUP = "vllm.general_plugins"
15
16
17
18
19
# IO processor plugins group will be loaded in process0 only
IO_PROCESSOR_PLUGINS_GROUP = "vllm.io_processor_plugins"
# Platform plugins group will be loaded in all processes when
# `vllm.platforms.current_platform` is called and the value not initialized,
PLATFORM_PLUGINS_GROUP = "vllm.platform_plugins"
wangxiyuan's avatar
wangxiyuan committed
20
21
22
# Stat logger plugins group will be loaded in process0 only when serve vLLM with
# async mode.
STAT_LOGGER_PLUGINS_GROUP = "vllm.stat_logger_plugins"
23

24
25
26
# make sure one process only loads plugins once
plugins_loaded = False

27

28
def load_plugins_by_group(group: str) -> dict[str, Callable[[], Any]]:
29
    """Load plugins registered under the given entry point group."""
30
    from importlib.metadata import entry_points
31
32
33
34
35
36
37

    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 {}
38

39
    # Check if the only discovered plugin is the default one
40
    is_default_group = group == DEFAULT_PLUGINS_GROUP
41
42
43
44
    # 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)
45
    for plugin in discovered_plugins:
46
        log_level("- %s -> %s", plugin.name, plugin.value)
47

48
    if allowed_plugins is None:
49
50
51
52
        log_level(
            "All plugins in this group will be loaded. "
            "Set `VLLM_PLUGINS` to control which plugins to load."
        )
53
54

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

60
61
62
63
64
            try:
                func = plugin.load()
                plugins[plugin.name] = func
            except Exception:
                logger.exception("Failed to load plugin %s", plugin.name)
65

66
67
68
    return plugins


69
70
71
72
73
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.
    """
74
75
76
77
    global plugins_loaded
    if plugins_loaded:
        return
    plugins_loaded = True
78

79
    plugins = load_plugins_by_group(group=DEFAULT_PLUGINS_GROUP)
80
81
82
    # general plugins, we only need to execute the loaded functions
    for func in plugins.values():
        func()