[MPS] Fix Triton stub sub-module imports on Python 3.12+ (#21551)

Co-authored-by: karanb192 <karan@example.com>
Co-authored-by: R0CKSTAR <yeahdongcn@gmail.com>
Co-authored-by: R0CKSTAR <xiaodong.ye@mthreads.com>
This commit is contained in:
Karan Bansal
2026-03-31 20:26:01 -07:00
committed by GitHub
co-authored by karanb192 R0CKSTAR R0CKSTAR
parent 1b45d81e91
commit e9b6cce237
+6 -8
View File
@@ -125,24 +125,22 @@ class _TritonFinder:
``triton.*`` sub-module that isn't already in ``sys.modules``. ``triton.*`` sub-module that isn't already in ``sys.modules``.
""" """
def find_module(self, fullname, path=None): def find_spec(self, fullname, path=None, target=None):
"""PEP 451 meta-path finder for ``triton.*`` sub-modules."""
if fullname == "triton" or fullname.startswith("triton."): if fullname == "triton" or fullname.startswith("triton."):
return self
return None
def load_module(self, fullname):
if fullname in sys.modules: if fullname in sys.modules:
return sys.modules[fullname] return getattr(sys.modules[fullname], "__spec__", None)
# Create and register the mock so the import machinery finds it
mod = _MockModule(fullname) mod = _MockModule(fullname)
sys.modules[fullname] = mod sys.modules[fullname] = mod
# Wire up the parent relationship
parts = fullname.rsplit(".", 1) parts = fullname.rsplit(".", 1)
if len(parts) == 2: if len(parts) == 2:
parent_name, child_name = parts parent_name, child_name = parts
parent = sys.modules.get(parent_name) parent = sys.modules.get(parent_name)
if parent is not None: if parent is not None:
setattr(parent, child_name, mod) setattr(parent, child_name, mod)
return mod return mod.__spec__
return None
def _make_mock(name: str) -> _MockModule: def _make_mock(name: str) -> _MockModule: