__init__.py 2.89 KB
Newer Older
1
2
# SPDX-License-Identifier: Apache-2.0

3
import logging
4
import os
5
from typing import Any, Callable
6

7
8
import torch

9
import vllm.envs as envs
10

11
12
logger = logging.getLogger(__name__)

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
    logger.info("Available plugins for group %s:", group)
    for plugin in discovered_plugins:
33
34
        logger.info("- %s -> %s", plugin.name, plugin.value)

35
    if allowed_plugins is None:
36
37
38
39
        logger.info("All plugins in this group will be loaded. "
                    "Set `VLLM_PLUGINS` to control which plugins to load.")

    plugins = dict[str, Callable[[], Any]]()
40
41
    for plugin in discovered_plugins:
        if allowed_plugins is None or plugin.name in allowed_plugins:
42
43
44
            if allowed_plugins is not None:
                logger.info("Loading plugin %s", plugin.name)

45
46
47
48
49
            try:
                func = plugin.load()
                plugins[plugin.name] = func
            except Exception:
                logger.exception("Failed to load plugin %s", plugin.name)
50

51
52
53
    return plugins


54
55
56
57
58
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.
    """
59
60
61
62
    global plugins_loaded
    if plugins_loaded:
        return
    plugins_loaded = True
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

    # some platform-specific configurations
    from vllm.platforms import current_platform

    if current_platform.is_xpu():
        # see https://github.com/pytorch/pytorch/blob/43c5f59/torch/_dynamo/config.py#L158
        torch._dynamo.config.disable = True
    elif current_platform.is_hpu():
        # NOTE(kzawora): PT HPU lazy backend (PT_HPU_LAZY_MODE = 1)
        # does not support torch.compile
        # Eager backend (PT_HPU_LAZY_MODE = 0) must be selected for
        # torch.compile support
        is_lazy = os.environ.get('PT_HPU_LAZY_MODE', '1') == '1'
        if is_lazy:
            torch._dynamo.config.disable = True
            # NOTE(kzawora) multi-HPU inference with HPUGraphs (lazy-only)
            # requires enabling lazy collectives
            # see https://docs.habana.ai/en/latest/PyTorch/Inference_on_PyTorch/Inference_Using_HPU_Graphs.html # noqa: E501
            os.environ['PT_HPU_ENABLE_LAZY_COLLECTIVES'] = 'true'

83
84
85
86
    plugins = load_plugins_by_group(group='vllm.general_plugins')
    # general plugins, we only need to execute the loaded functions
    for func in plugins.values():
        func()