"""Make TileLang unimportable, the way it is on a card where it was never installed. Two independent switches, because the two scenarios need different things: * ``DEEPINFER_SIMULATE_NO_TILELANG=1`` -- ``tilelang`` and ``tvm_ffi`` are gone. This is the target state of the torch-reference bring-up: `tile_kernels` itself is installed (it is a plain Python package), but none of its compiled kernels can be built or loaded. * ``DEEPINFER_SIMULATE_NO_TILE_KERNELS=1`` -- additionally removes `tile_kernels`. Stricter than any real deployment needs to be, useful only to prove that no interface module reaches for it at import time. Blocking by meta path rather than by uninstalling matters because the image ships its own tilelang in system site-packages, and a venv that inherits it silently gets the package back. It also must live in `sitecustomize`: the engine spawns one process per data-parallel rank, and an import in the parent would not follow them. """ import os _blocked = [] if os.environ.get("DEEPINFER_SIMULATE_NO_TILELANG"): _blocked += ["tilelang", "tvm_ffi"] if os.environ.get("DEEPINFER_SIMULATE_NO_TILE_KERNELS"): _blocked += ["tile_kernels"] if _blocked: import sys from importlib.abc import MetaPathFinder _BLOCKED = tuple(_blocked) class _Blocked(MetaPathFinder): def find_spec( self, fullname: str, path: object = None, target: object = None, ) -> None: if fullname.split(".")[0] in _BLOCKED: # Raise rather than return None: a finder returning None only says "not mine", # and the remaining finders would happily locate the installed package. Raising # is the only way a meta path entry can actually make something unimportable. # `importlib.util.find_spec` will raise too, so probes must expect that. raise ModuleNotFoundError( f"No module named {fullname!r} (blocked: simulating a host without it)", name=fullname, ) return None sys.meta_path.insert(0, _Blocked()) for _name in list(sys.modules): if _name.split(".")[0] in _BLOCKED: del sys.modules[_name]