[Misc] Fix import error in tensorizer tests and cleanup some code (#10349)

Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk>
This commit is contained in:
Cyrus Leung
2024-11-15 17:34:17 +08:00
committed by GitHub
parent 3d158cdc8d
commit b311efd0bd
7 changed files with 67 additions and 58 deletions

View File

@@ -5,6 +5,7 @@ import datetime
import enum
import gc
import getpass
import importlib.util
import inspect
import ipaddress
import os
@@ -1539,6 +1540,25 @@ def is_in_doc_build() -> bool:
return False
def import_from_path(module_name: str, file_path: Union[str, os.PathLike]):
"""
Import a Python file according to its file path.
Based on the official recipe:
https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
"""
spec = importlib.util.spec_from_file_location(module_name, file_path)
if spec is None:
raise ModuleNotFoundError(f"No module named '{module_name}'")
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
# create a library to hold the custom op
vllm_lib = Library("vllm", "FRAGMENT") # noqa