[Bugfix][Misc] Use TritonPlaceholderModule to defensively import triton (#15099)

Signed-off-by: Mengqing Cao <cmq0113@163.com>
This commit is contained in:
Mengqing Cao
2025-04-25 13:51:02 +08:00
committed by GitHub
parent 5aa6efb9a5
commit 2f54045508
5 changed files with 53 additions and 6 deletions

View File

@@ -2,4 +2,4 @@
from vllm.triton_utils.importing import HAS_TRITON
__all__ = ["HAS_TRITON"]
__all__ = ["HAS_TRITON"]

View File

@@ -1,17 +1,53 @@
# SPDX-License-Identifier: Apache-2.0
import sys
import types
from importlib.util import find_spec
from vllm.logger import init_logger
from vllm.platforms import current_platform
logger = init_logger(__name__)
HAS_TRITON = (
find_spec("triton") is not None
and not current_platform.is_xpu() # Not compatible
or find_spec("pytorch-triton-xpu") is not None # Not compatible
)
if not HAS_TRITON:
logger.info("Triton not installed or not compatible; certain GPU-related"
" functions will not be available.")
class TritonPlaceholder(types.ModuleType):
def __init__(self):
super().__init__("triton")
self.jit = self._dummy_decorator("jit")
self.autotune = self._dummy_decorator("autotune")
self.heuristics = self._dummy_decorator("heuristics")
self.language = TritonLanguagePlaceholder()
logger.warning_once(
"Triton is not installed. Using dummy decorators. "
"Install it via `pip install triton` to enable kernel"
"compilation.")
def _dummy_decorator(self, name):
def decorator(func=None, **kwargs):
if func is None:
return lambda f: f
return func
return decorator
class TritonLanguagePlaceholder(types.ModuleType):
def __init__(self):
super().__init__("triton.language")
self.constexpr = None
self.dtype = None
sys.modules['triton'] = TritonPlaceholder()
sys.modules['triton.language'] = TritonLanguagePlaceholder()
if 'triton' in sys.modules:
logger.info("Triton module has been replaced with a placeholder.")