Skip to content

Commit 8eccfbb

Browse files
committed
remove torch module patch
Signed-off-by: Isotr0py <[email protected]>
1 parent 8981990 commit 8eccfbb

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

vllm/triton_utils/importing.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# SPDX-License-Identifier: Apache-2.0
2-
32
import importlib
4-
import pkgutil
53
import sys
64
import types
75
from importlib.util import find_spec
6+
from typing import Optional
87

98
from vllm.logger import init_logger
109

@@ -19,17 +18,37 @@
1918
logger.info("Triton not installed or not compatible; certain GPU-related"
2019
" functions will not be available.")
2120

22-
class TritonPlaceholder(types.ModuleType):
21+
class TritonModulePlaceholder(types.ModuleType):
22+
23+
def __init__(
24+
self,
25+
name: str,
26+
dummy_objects: Optional[list[str]] = None,
27+
):
28+
super().__init__(name)
29+
30+
if dummy_objects is not None:
31+
for obj_name in dummy_objects:
32+
setattr(self, obj_name, object)
33+
34+
class TritonLanguagePlaceholder(TritonModulePlaceholder):
35+
36+
def __init__(self):
37+
super().__init__("triton.language")
38+
self.constexpr = None
39+
self.dtype = None
40+
41+
class TritonPlaceholder(TritonModulePlaceholder):
2342

2443
def __init__(self):
25-
super().__init__("triton")
44+
super().__init__("triton", dummy_objects=["Config"])
2645
self.jit = self._dummy_decorator("jit")
2746
self.autotune = self._dummy_decorator("autotune")
2847
self.heuristics = self._dummy_decorator("heuristics")
2948
self.language = TritonLanguagePlaceholder()
3049
logger.warning_once(
3150
"Triton is not installed. Using dummy decorators. "
32-
"Install it via `pip install triton` to enable kernel"
51+
"Install it via `pip install triton` to enable kernel "
3352
"compilation.")
3453

3554
def _dummy_decorator(self, name):
@@ -41,34 +60,27 @@ def decorator(func=None, **kwargs):
4160

4261
return decorator
4362

44-
class TritonLanguagePlaceholder(types.ModuleType):
45-
46-
def __init__(self):
47-
super().__init__("triton.language")
48-
self.constexpr = None
49-
self.dtype = None
63+
# Hack `_is_triton_available` in torch to return False
64+
torch_hints = importlib.import_module("torch._inductor.runtime.hints")
65+
torch_hints._is_triton_available = lambda: False # type: ignore[attr-defined]
5066

51-
def init_torch_inductor_runtime():
52-
name = "torch._inductor.runtime"
53-
torch_runtime = importlib.import_module(name)
54-
path = torch_runtime.__path__
55-
for module_info in pkgutil.iter_modules(path, name + "."):
56-
if not module_info.ispkg:
57-
try:
58-
importlib.import_module(module_info.name)
59-
except Exception as e:
60-
logger.warning(
61-
"Ignore import error when loading " \
62-
"%s: %s", module_info.name, e)
63-
continue
64-
65-
# initialize torch inductor without triton placeholder
66-
# FIXME(Isotr0py): See if we can remove this after bumping torch version
67-
# to 2.7.0, because torch 2.7.0 has a better triton check.
68-
init_torch_inductor_runtime()
69-
# Replace the triton module in sys.modules with the placeholder
67+
# Replace the triton module and torch triton helpers in sys.modules
68+
# with the placeholder
7069
sys.modules['triton'] = TritonPlaceholder()
7170
sys.modules['triton.language'] = TritonLanguagePlaceholder()
71+
sys.modules['torch._inductor.runtime.triton_helpers'] = types.ModuleType(
72+
"triton_helpers")
73+
74+
# Replace triton submodules with dummy objects to keep compatibility with
75+
# torch triton check
76+
triton_modules_with_objects = {
77+
"triton.compiler": ["CompiledKernel"],
78+
"triton.runtime.autotuner": ["OutOfResources"],
79+
"triton.runtime.jit": ["KernelInterface"],
80+
}
81+
for module_name, dummy_objects in triton_modules_with_objects.items():
82+
sys.modules[module_name] = TritonModulePlaceholder(
83+
module_name, dummy_objects=dummy_objects)
7284

7385
if 'triton' in sys.modules:
7486
logger.info("Triton module has been replaced with a placeholder.")

0 commit comments

Comments
 (0)