Skip to content

Commit f978e67

Browse files
committed
Implement CUDA_PYTHON_CUDA_HOME_PRIORITY first, last, with default first
1 parent 2a039d2 commit f978e67

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

cuda_bindings/cuda/bindings/_path_finder/find_nvidia_dynamic_library.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,23 @@ def _find_dll_using_nvidia_bin_dirs(libname, lib_searched_for, error_messages, a
5353
return None
5454

5555

56-
def _get_cuda_home():
56+
def _get_cuda_home(priority):
57+
supported_priorities = ("first", "last")
58+
assert priority in supported_priorities
59+
env_priority = os.environ.get("CUDA_PYTHON_CUDA_HOME_PRIORITY")
60+
if env_priority:
61+
if env_priority not in supported_priorities:
62+
raise RuntimeError(f"Invalid CUDA_PYTHON_CUDA_HOME_PRIORITY {env_priority!r} ({supported_priorities=})")
63+
if priority != env_priority:
64+
return None
5765
cuda_home = os.environ.get("CUDA_HOME")
5866
if cuda_home is None:
5967
cuda_home = os.environ.get("CUDA_PATH")
6068
return cuda_home
6169

6270

63-
def _find_lib_dir_using_cuda_home(libname):
64-
cuda_home = _get_cuda_home()
71+
def _find_lib_dir_using_cuda_home(libname, priority):
72+
cuda_home = _get_cuda_home(priority)
6573
if cuda_home is None:
6674
return None
6775
if IS_WINDOWS:
@@ -126,7 +134,7 @@ def __init__(self, libname: str):
126134
self.attachments = []
127135
self.abs_path = None
128136

129-
cuda_home_lib_dir = _find_lib_dir_using_cuda_home(libname)
137+
cuda_home_lib_dir = _find_lib_dir_using_cuda_home(libname, "first")
130138
if IS_WINDOWS:
131139
self.lib_searched_for = f"{libname}*.dll"
132140
if cuda_home_lib_dir is not None:
@@ -148,6 +156,18 @@ def __init__(self, libname: str):
148156
libname, self.lib_searched_for, self.error_messages, self.attachments
149157
)
150158

159+
def retry_with_cuda_home_priority_last(self):
160+
cuda_home_lib_dir = _find_lib_dir_using_cuda_home(self.libname, "last")
161+
if cuda_home_lib_dir is not None:
162+
if IS_WINDOWS:
163+
self.abs_path = _find_dll_using_lib_dir(
164+
cuda_home_lib_dir, self.libname, self.error_messages, self.attachments
165+
)
166+
else:
167+
self.abs_path = _find_so_using_lib_dir(
168+
cuda_home_lib_dir, self.lib_searched_for, self.error_messages, self.attachments
169+
)
170+
151171
def retry_with_anchor_abs_path(self, anchor_abs_path):
152172
assert self.libname == "nvvm"
153173
nvvm_lib_dir = _find_nvvm_lib_dir_from_anchor_abs_path(anchor_abs_path)

cuda_bindings/cuda/bindings/_path_finder/load_nvidia_dynamic_library.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ def _load_nvidia_dynamic_library_no_cache(libname: str) -> LoadedDL:
6262
loaded = load_with_system_search(libname, found.lib_searched_for)
6363
if loaded is not None:
6464
return loaded
65-
if libname == "nvvm":
65+
if libname == "nvvm" and ANCHOR_LIBNAME is not None:
6666
loaded_anchor = check_if_already_loaded_from_elsewhere(ANCHOR_LIBNAME)
6767
if loaded_anchor is not None:
6868
found.retry_with_anchor_abs_path(loaded_anchor.abs_path)
6969
else:
7070
anchor_abs_path = _load_anchor_in_subprocess(ANCHOR_LIBNAME, found.error_messages)
7171
if anchor_abs_path is not None:
7272
found.retry_with_anchor_abs_path(anchor_abs_path)
73+
if found.abs_path is None:
74+
found.retry_with_cuda_home_priority_last()
7375
found.raise_if_abs_path_is_None()
7476

7577
# Load the library from the found path

0 commit comments

Comments
 (0)