2024-08-14 07:24:17 +08:00
|
|
|
import logging
|
2024-11-17 10:02:14 +08:00
|
|
|
from contextlib import contextmanager
|
2024-11-18 15:57:20 +08:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
2024-08-14 07:24:17 +08:00
|
|
|
|
|
|
|
|
import vllm.envs as envs
|
2024-11-03 03:08:49 +08:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2024-11-17 10:02:14 +08:00
|
|
|
from vllm.config import CompilationConfig, VllmConfig
|
2024-08-14 07:24:17 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
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')
|
2024-11-16 17:23:20 +08:00
|
|
|
if len(discovered_plugins) == 0:
|
|
|
|
|
logger.info("No plugins found.")
|
|
|
|
|
return
|
2024-11-16 13:46:27 +08:00
|
|
|
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)
|
2024-08-14 07:24:17 +08:00
|
|
|
for plugin in discovered_plugins:
|
|
|
|
|
if allowed_plugins is None or plugin.name in allowed_plugins:
|
|
|
|
|
try:
|
|
|
|
|
func = plugin.load()
|
|
|
|
|
func()
|
2024-11-16 13:46:27 +08:00
|
|
|
logger.info("plugin %s loaded.", plugin.name)
|
2024-08-14 07:24:17 +08:00
|
|
|
except Exception:
|
2024-11-16 13:46:27 +08:00
|
|
|
logger.exception("Failed to load plugin %s", plugin.name)
|
2024-09-14 00:32:42 +08:00
|
|
|
|
|
|
|
|
|
2024-11-19 07:29:37 +08:00
|
|
|
_compilation_config: Optional["CompilationConfig"] = None
|
2024-10-11 03:39:36 +08:00
|
|
|
|
|
|
|
|
|
2024-11-19 07:29:37 +08:00
|
|
|
def set_compilation_config(config: Optional["CompilationConfig"]):
|
2024-10-30 14:03:49 +08:00
|
|
|
global _compilation_config
|
|
|
|
|
_compilation_config = config
|
2024-10-11 03:39:36 +08:00
|
|
|
|
|
|
|
|
|
2024-11-19 07:29:37 +08:00
|
|
|
def get_compilation_config() -> Optional["CompilationConfig"]:
|
2024-10-30 14:03:49 +08:00
|
|
|
return _compilation_config
|
2024-11-17 10:02:14 +08:00
|
|
|
|
|
|
|
|
|
2024-11-19 07:29:37 +08:00
|
|
|
_current_vllm_config: Optional["VllmConfig"] = None
|
2024-11-17 10:02:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2024-11-19 07:29:37 +08:00
|
|
|
def set_current_vllm_config(vllm_config: "VllmConfig"):
|
2024-11-17 10:02:14 +08:00
|
|
|
"""
|
|
|
|
|
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:
|
2024-11-19 07:14:59 +08:00
|
|
|
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)
|
2024-11-17 10:02:14 +08:00
|
|
|
_current_vllm_config = old_vllm_config
|
|
|
|
|
|
|
|
|
|
|
2024-11-19 07:29:37 +08:00
|
|
|
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()
|
2024-11-17 10:02:14 +08:00
|
|
|
return _current_vllm_config
|