Skip to content

Commit 60861ec

Browse files
rwgkcursoragent
andcommitted
refactor(pathfinder): simplify CTK canary fallback scope
Centralize canary configuration in supported_nvidia_libs and gate CTK-root canary probing to discoverable libnames (currently nvvm). This avoids unnecessary canary subprocess work for other libraries and locks the behavior with a focused regression test. Co-authored-by: Cursor <[email protected]>
1 parent 5775f5d commit 60861ec

3 files changed

Lines changed: 39 additions & 20 deletions

File tree

‎cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
)
1414
from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL, load_dependencies
1515
from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import (
16+
_CTK_ROOT_CANARY_ANCHOR_LIBNAMES,
17+
_CTK_ROOT_CANARY_DISCOVERABLE_LIBNAMES,
1618
SUPPORTED_LINUX_SONAMES,
1719
SUPPORTED_WINDOWS_DLLS,
1820
)
@@ -66,12 +68,6 @@ def _load_driver_lib_no_cache(libname: str) -> LoadedDL:
6668
)
6769

6870

69-
# Libs that reside on the standard linker path in system CTK installs.
70-
# Used to discover the CTK root when a lib with a non-standard path
71-
# (e.g. nvvm under $CTK_ROOT/nvvm/lib64) can't be found directly.
72-
_CTK_ROOT_CANARY_LIBNAMES = ("cudart",)
73-
74-
7571
def _resolve_system_loaded_abs_path_in_subprocess(libname: str) -> str | None:
7672
"""Resolve a library's system-search absolute path in a child process.
7773
@@ -111,15 +107,15 @@ def _resolve_system_loaded_abs_path_in_subprocess(libname: str) -> str | None:
111107
def _try_ctk_root_canary(finder: _FindNvidiaDynamicLib) -> str | None:
112108
"""Derive the CTK root from a system-installed canary lib.
113109
114-
For libs like nvvm whose shared object doesn't reside on the standard
115-
linker path, we locate a well-known CTK lib that IS on the linker path
116-
via system search, derive the CTK installation root from its resolved
117-
path, and then look for the target lib relative to that root.
110+
For discoverable libs (currently nvvm) whose shared object doesn't reside
111+
on the standard linker path, we locate a well-known CTK lib that IS on
112+
the linker path via system search, derive the CTK installation root from
113+
its resolved path, and then look for the target lib relative to that root.
118114
119115
The canary load is performed in a subprocess to avoid introducing loader
120116
state into the current process.
121117
"""
122-
for canary_libname in _CTK_ROOT_CANARY_LIBNAMES:
118+
for canary_libname in _CTK_ROOT_CANARY_ANCHOR_LIBNAMES:
123119
canary_abs_path = _resolve_system_loaded_abs_path_in_subprocess(canary_libname)
124120
if canary_abs_path is None:
125121
continue
@@ -167,11 +163,14 @@ def _load_lib_no_cache(libname: str) -> LoadedDL:
167163
if abs_path is not None:
168164
found_via = "CUDA_HOME"
169165
else:
170-
# Canary probe: if the direct system search and CUDA_HOME both
171-
# failed (e.g. nvvm isn't on the linker path and CUDA_HOME is
172-
# unset), try to discover the CTK root by loading a well-known CTK
173-
# lib in a subprocess, then look for the target lib relative to
174-
# that root.
166+
if libname not in _CTK_ROOT_CANARY_DISCOVERABLE_LIBNAMES:
167+
finder.raise_not_found_error()
168+
169+
# Canary probe (discoverable libs only): if the direct system
170+
# search and CUDA_HOME both failed (e.g. nvvm isn't on the linker
171+
# path and CUDA_HOME is unset), try to discover the CTK root by
172+
# loading a well-known CTK lib in a subprocess, then look for the
173+
# target lib relative to that root.
175174
abs_path = _try_ctk_root_canary(finder)
176175
if abs_path is not None:
177176
found_via = "system-ctk-root"
@@ -246,10 +245,10 @@ def load_nvidia_dynamic_lib(libname: str) -> LoadedDL:
246245
247246
- If set, use ``CUDA_HOME`` or ``CUDA_PATH`` (in that order).
248247
249-
5. **CTK root canary probe**
248+
5. **CTK root canary probe (discoverable libs only)**
250249
251-
- For libraries whose shared object doesn't reside on the standard
252-
linker path (e.g. ``libnvvm.so`` lives under ``$CTK_ROOT/nvvm/lib64``),
250+
- For selected libraries whose shared object doesn't reside on the
251+
standard linker path (currently ``nvvm``),
253252
attempt to discover the CTK installation root by system-loading a
254253
well-known CTK library (``cudart``) in a subprocess, then derive
255254
the root from its resolved absolute path.

‎cuda_pathfinder/cuda/pathfinder/_dynamic_libs/supported_nvidia_libs.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@
358358

359359
LIBNAMES_REQUIRING_RTLD_DEEPBIND = ("cufftMp",)
360360

361+
# CTK root canary probe config:
362+
# - anchor libs: expected on the standard system loader path and used to derive
363+
# CTK root in an isolated child process.
364+
# - discoverable libs: libs that are allowed to use the CTK-root canary fallback.
365+
_CTK_ROOT_CANARY_ANCHOR_LIBNAMES = ("cudart",)
366+
_CTK_ROOT_CANARY_DISCOVERABLE_LIBNAMES = ("nvvm",)
367+
361368
# Based on output of toolshed/make_site_packages_libdirs_linux.py
362369
SITE_PACKAGES_LIBDIRS_LINUX_CTK = {
363370
"cublas": ("nvidia/cu13/lib", "nvidia/cublas/lib"),

‎cuda_pathfinder/tests/test_ctk_root_discovery.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
_FindNvidiaDynamicLib,
1111
derive_ctk_root,
1212
)
13-
from cuda.pathfinder._dynamic_libs.load_dl_common import LoadedDL
13+
from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL
1414
from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import (
1515
_load_lib_no_cache,
1616
_resolve_system_loaded_abs_path_in_subprocess,
@@ -328,3 +328,16 @@ def test_canary_fires_only_after_all_earlier_steps_fail(tmp_path, mocker):
328328

329329
assert result.found_via == "system-ctk-root"
330330
assert result.abs_path == str(nvvm_lib)
331+
332+
333+
@pytest.mark.usefixtures("_isolate_load_cascade")
334+
def test_non_discoverable_lib_skips_canary_probe(mocker):
335+
# Force fallback path for a lib that is not canary-discoverable.
336+
mocker.patch(f"{_MODULE}.load_with_system_search", return_value=None)
337+
mocker.patch(f"{_FIND_MODULE}.get_cuda_home_or_path", return_value=None)
338+
canary_probe = mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess")
339+
340+
with pytest.raises(DynamicLibNotFoundError):
341+
_load_lib_no_cache("cublas")
342+
343+
canary_probe.assert_not_called()

0 commit comments

Comments
 (0)