Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Lazy_import not return None in a shared python env that contains cuda-related files on a cpu-only machine #50

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions python/xoscar/nvutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ def get_device_count() -> int | None:
return _gpu_count


def cuda_count() -> int:
return get_device_count() or 0


def _get_all_device_count() -> int | None:
_init_nvml()
if _nvml_lib is None:
Expand Down
8 changes: 8 additions & 0 deletions python/xoscar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def lazy_import(
rename: str | None = None,
placeholder: bool = False,
):
from .nvutils import cuda_count

rename = rename or name
prefix_name = name.split(".", 1)[0]
globals = globals or inspect.currentframe().f_back.f_globals # type: ignore
Expand Down Expand Up @@ -267,6 +269,12 @@ def add_load_handler(self, func: Callable):
return func

if pkgutil.find_loader(prefix_name) is not None:
# There are cuda-related files in python env (commonly happen in a shared env),
# but that is a cpu only machine without GPU cards, just return None
if (
"cupy" in name or "cudf" in name or "cuda" in name or "rmm" in name
) and cuda_count() == 0: # pragma: no cover
return None
return LazyModule()
elif placeholder:
return ModulePlaceholder(prefix_name)
Expand Down