Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,14 @@ def _load_lib_no_cache(libname: str) -> LoadedDL:

ctx = SearchContext(desc)

# Phase 1: Try to find the library file on disk (pip wheels, conda).
find = run_find_steps(ctx, EARLY_FIND_STEPS)

# Phase 2: Cross-cutting — already-loaded check and dependency loading.
# An already-loaded library bypasses the wheel and conda search.
loaded = LOADER.check_if_already_loaded_from_elsewhere(desc)
find = None
if loaded is None:
find = run_find_steps(ctx, EARLY_FIND_STEPS)
# Another component may have loaded the library during discovery.
loaded = LOADER.check_if_already_loaded_from_elsewhere(desc)

load_dependencies(desc, load_nvidia_dynamic_lib)
if loaded is not None:
return loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | None:
# Step sequences per strategy
# ---------------------------------------------------------------------------

#: Find steps that run before the already-loaded check and system search.
#: Find steps that run after the already-loaded check and before system search.
EARLY_FIND_STEPS: tuple[FindStep, ...] = (find_in_site_packages, find_in_conda)

#: Find steps that run after system search fails.
Expand Down
93 changes: 85 additions & 8 deletions cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _run_find_steps_disabled(ctx, steps):
@pytest.mark.parametrize("preferred_source", ("site-packages", "conda", "system-search"))
@pytest.mark.agent_authored(model="gpt-6.1-sol")
def test_cuda_path_is_a_fallback_to_environment_and_native_search(tmp_path, mocker, monkeypatch, preferred_source):
desc = load_mod.LIB_DESCRIPTORS["cublasLt"]
desc = load_mod.LIB_DESCRIPTORS["cublas"]
platform = steps_mod.PLATFORM
filename = desc.windows_dlls[0] if IS_WINDOWS else desc.linux_sonames[0]

Expand Down Expand Up @@ -208,20 +208,21 @@ def find_wheel_dirs(sub_dirs):
side_effect=find_wheel_dirs,
)
mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None)
mocker.patch(f"{_MODULE}.load_dependencies")
load_dependency = mocker.patch(f"{_MODULE}.load_nvidia_dynamic_lib")
native_path = tmp_path / "native" / filename
native_search = mocker.patch.object(
load_mod.LOADER,
"load_with_system_search",
return_value=_make_loaded_dl(str(native_path), "system-search"),
)
mocker.patch.object(
load_mod.LOADER,
"load_with_abs_path",
side_effect=lambda _desc, path, via: _make_loaded_dl(path, via),
)

loaded = _load_lib_no_cache("cublasLt")
def load_from_discovered_path(_desc, path, via):
load_dependency.assert_called_once_with("cublasLt")
return _make_loaded_dl(path, via)

mocker.patch.object(load_mod.LOADER, "load_with_abs_path", side_effect=load_from_discovered_path)

loaded = _load_lib_no_cache("cublas")

expected_path = {"site-packages": wheel_path, "conda": conda_path, "system-search": native_path}[preferred_source]
assert loaded.found_via == preferred_source
Expand Down Expand Up @@ -274,3 +275,79 @@ def _run_find_steps_without_site_packages(ctx, steps):
result = _load_lib_no_cache("cupti")
assert result.found_via == "conda"
assert result.abs_path == str(conda_cupti_lib)


@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_already_loaded_library_is_returned_without_searching_the_disk(mocker):
"""An unreadable search location must not break a library that is already loaded."""
already_loaded = _make_loaded_dl("/opt/elsewhere/libcublas.so.13", "was-already-loaded-from-elsewhere")
mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=already_loaded)
mocker.patch(f"{_MODULE}.load_dependencies")
unreadable = mocker.patch(
"cuda.pathfinder._dynamic_libs.search_platform.find_sub_dirs_all_sitepackages",
side_effect=PermissionError("site-packages is not readable"),
)

assert _load_lib_no_cache("cublas") is already_loaded
unreadable.assert_not_called()


@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_already_loaded_library_still_loads_its_dependencies(mocker):
"""Skipping the search must not skip the dependencies."""
already_loaded = _make_loaded_dl("/opt/elsewhere/libcublas.so.13", "was-already-loaded-from-elsewhere")
mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=already_loaded)
load_dependencies = mocker.patch(f"{_MODULE}.load_dependencies")
run_find_steps = mocker.patch(f"{_MODULE}.run_find_steps")

assert _load_lib_no_cache("cublas") is already_loaded
load_dependencies.assert_called_once()
run_find_steps.assert_not_called()


@pytest.mark.parametrize("find_succeeds", [True, False], ids=["wheel-found", "wheel-missed"])
@pytest.mark.agent_authored(model="gpt-6")
def test_library_loaded_during_early_search_reuses_existing_handle(tmp_path, mocker, find_succeeds):
desc = load_mod.LIB_DESCRIPTORS["cublas"]
filename = desc.windows_dlls[0] if IS_WINDOWS else desc.linux_sonames[0]
already_loaded = LoadedDL(str(tmp_path / "external" / filename), True, 0xBEEF, "was-already-loaded-from-elsewhere")
probe = mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None)
load_dependencies = mocker.patch(f"{_MODULE}.load_dependencies")
calls = mocker.Mock()
calls.attach_mock(probe, "probe")
calls.attach_mock(load_dependencies, "load_dependencies")

def discover(_ctx, steps):
assert steps is EARLY_FIND_STEPS
probe.return_value = already_loaded
if find_succeeds:
return steps_mod.FindResult(str(tmp_path / "wheel" / filename), "site-packages")
return None

mocker.patch(f"{_MODULE}.run_find_steps", side_effect=discover)
load_wheel = mocker.patch.object(load_mod.LOADER, "load_with_abs_path")
load_native = mocker.patch.object(load_mod.LOADER, "load_with_system_search")

assert _load_lib_no_cache("cublas") is already_loaded
load_wheel.assert_not_called()
load_native.assert_not_called()
assert calls.mock_calls == [
mocker.call.probe(desc),
mocker.call.probe(desc),
mocker.call.load_dependencies(desc, load_mod.load_nvidia_dynamic_lib),
]


@pytest.mark.agent_authored(model="gpt-6")
def test_unloaded_library_search_failure_precedes_dependency_loading(mocker):
probe = mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None)
failure = PermissionError("site-packages is not readable")
mocker.patch(f"{_MODULE}.run_find_steps", side_effect=failure)
load_dependencies = mocker.patch(f"{_MODULE}.load_dependencies")

with pytest.raises(PermissionError) as exc_info:
_load_lib_no_cache("cublas")

assert exc_info.value is failure
probe.assert_called_once()
load_dependencies.assert_not_called()
Loading