[CI] Fix gpu deps import in cpu test (#21950)

This commit is contained in:
Ke Bao
2026-04-03 00:06:31 +08:00
committed by GitHub
parent 083304ca44
commit b21db86e2f
5 changed files with 78 additions and 6 deletions
+38
View File
@@ -2056,6 +2056,44 @@ def _distributed_worker(rank, world_size, backend, port, func, result_queue, kwa
dist.destroy_process_group()
def maybe_stub_sgl_kernel():
"""Stub sgl_kernel if it cannot be imported (e.g. no GPU).
Must be called before any import that transitively depends on sgl_kernel.
On machines with a working sgl_kernel this is a no-op.
"""
try:
import sgl_kernel # noqa: F401
return
except (ImportError, OSError):
pass
import importlib.abc
import importlib.machinery
class _SglKernelLoader(importlib.abc.Loader):
def create_module(self, spec):
return None
def exec_module(self, module):
from unittest.mock import MagicMock
module.__getattr__ = lambda name: MagicMock()
class _SglKernelFinder(importlib.abc.MetaPathFinder):
def find_spec(self, fullname, path, target=None):
if fullname == "sgl_kernel" or fullname.startswith("sgl_kernel."):
return importlib.machinery.ModuleSpec(
fullname,
_SglKernelLoader(),
is_package=True,
)
return None
sys.meta_path.insert(0, _SglKernelFinder())
class CustomTestCase(unittest.TestCase):
def __init_subclass__(cls, **kwargs):