[Bugfix] Fix triton import with local TritonPlaceholder (#17446)

Signed-off-by: Mengqing Cao <cmq0113@163.com>
This commit is contained in:
Mengqing Cao
2025-05-06 17:53:09 +08:00
committed by GitHub
parent 05e1f96419
commit f9bc5a0693
30 changed files with 165 additions and 75 deletions

View File

@@ -1,5 +1,13 @@
# SPDX-License-Identifier: Apache-2.0
from vllm.triton_utils.importing import HAS_TRITON
from vllm.triton_utils.importing import (HAS_TRITON, TritonLanguagePlaceholder,
TritonPlaceholder)
__all__ = ["HAS_TRITON"]
if HAS_TRITON:
import triton
import triton.language as tl
else:
triton = TritonPlaceholder()
tl = TritonLanguagePlaceholder()
__all__ = ["HAS_TRITON", "triton", "tl"]

View File

@@ -16,32 +16,34 @@ 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.")
class TritonPlaceholder(types.ModuleType):
def _dummy_decorator(self, name):
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 decorator(func=None, **kwargs):
if func is None:
return lambda f: f
return func
def _dummy_decorator(self, name):
return decorator
def decorator(*args, **kwargs):
if args and callable(args[0]):
return args[0]
return lambda f: f
class TritonLanguagePlaceholder(types.ModuleType):
return decorator
def __init__(self):
super().__init__("triton.language")
self.constexpr = None
self.dtype = None
self.int64 = None
class TritonLanguagePlaceholder(types.ModuleType):
def __init__(self):
super().__init__("triton.language")
self.constexpr = None
self.dtype = None
self.int64 = None