11# SPDX-License-Identifier: Apache-2.0
2-
32import importlib
4- import pkgutil
53import sys
64import types
75from importlib .util import find_spec
6+ from typing import Optional
87
98from vllm .logger import init_logger
109
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
7385if 'triton' in sys .modules :
7486 logger .info ("Triton module has been replaced with a placeholder." )
0 commit comments