xref: /aosp_15_r20/external/pytorch/tools/shared/module_loader.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Workerfrom importlib.abc import Loader
2*da0073e9SAndroid Build Coastguard Workerfrom types import ModuleType
3*da0073e9SAndroid Build Coastguard Workerfrom typing import cast
4*da0073e9SAndroid Build Coastguard Worker
5*da0073e9SAndroid Build Coastguard Worker
6*da0073e9SAndroid Build Coastguard Workerdef import_module(name: str, path: str) -> ModuleType:
7*da0073e9SAndroid Build Coastguard Worker    import importlib.util
8*da0073e9SAndroid Build Coastguard Worker
9*da0073e9SAndroid Build Coastguard Worker    spec = importlib.util.spec_from_file_location(name, path)
10*da0073e9SAndroid Build Coastguard Worker    assert spec is not None
11*da0073e9SAndroid Build Coastguard Worker    module = importlib.util.module_from_spec(spec)
12*da0073e9SAndroid Build Coastguard Worker    cast(Loader, spec.loader).exec_module(module)
13*da0073e9SAndroid Build Coastguard Worker    return module
14