|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import os |
| 5 | + |
| 6 | +import pytest |
| 7 | +from child_load_nvidia_dynamic_lib_helper import ( |
| 8 | + build_child_process_failed_for_libname_message, |
| 9 | + run_load_nvidia_dynamic_lib_in_subprocess, |
| 10 | +) |
| 11 | + |
| 12 | +from cuda.pathfinder import ( |
| 13 | + DynamicLibNotAvailableError, |
| 14 | + DynamicLibNotFoundError, |
| 15 | + DynamicLibUnknownError, |
| 16 | + LocatedDynamicLib, |
| 17 | + find_nvidia_dynamic_lib, |
| 18 | + locate_nvidia_dynamic_lib, |
| 19 | +) |
| 20 | +from cuda.pathfinder._dynamic_libs import find_nvidia_dynamic_lib as find_mod |
| 21 | +from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as load_mod |
| 22 | +from cuda.pathfinder._dynamic_libs import supported_nvidia_libs |
| 23 | +from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import _resolve_system_loaded_abs_path_in_subprocess |
| 24 | +from cuda.pathfinder._dynamic_libs.search_steps import EARLY_FIND_STEPS, LATE_FIND_STEPS, FindResult |
| 25 | +from cuda.pathfinder._dynamic_libs.subprocess_protocol import STATUS_NOT_FOUND, parse_dynamic_lib_subprocess_payload |
| 26 | +from cuda.pathfinder._utils.platform_aware import IS_WINDOWS, quote_for_shell |
| 27 | + |
| 28 | +_MODULE = "cuda.pathfinder._dynamic_libs.find_nvidia_dynamic_lib" |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(autouse=True) |
| 32 | +def _clear_caches(): |
| 33 | + find_nvidia_dynamic_lib.cache_clear() |
| 34 | + _resolve_system_loaded_abs_path_in_subprocess.cache_clear() |
| 35 | + yield |
| 36 | + find_nvidia_dynamic_lib.cache_clear() |
| 37 | + _resolve_system_loaded_abs_path_in_subprocess.cache_clear() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def no_in_process_loading(mocker): |
| 42 | + for name in ("check_if_already_loaded_from_elsewhere", "load_with_system_search", "load_with_abs_path"): |
| 43 | + mocker.patch.object(load_mod.LOADER, name, side_effect=AssertionError(f"LOADER.{name} was called")) |
| 44 | + |
| 45 | + |
| 46 | +def _patch_find_steps(mocker, *, early=None, late=None): |
| 47 | + def run_find_steps(_ctx, steps): |
| 48 | + if steps is EARLY_FIND_STEPS: |
| 49 | + return early |
| 50 | + assert steps is LATE_FIND_STEPS |
| 51 | + return late |
| 52 | + |
| 53 | + return mocker.patch(f"{_MODULE}.run_find_steps", side_effect=run_find_steps) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize( |
| 57 | + "libname", |
| 58 | + supported_nvidia_libs.SUPPORTED_WINDOWS_DLLS if IS_WINDOWS else supported_nvidia_libs.SUPPORTED_LINUX_SONAMES, |
| 59 | +) |
| 60 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 61 | +def test_locate_matches_load_in_fresh_process(info_summary_append, libname): |
| 62 | + timeout = 120 if IS_WINDOWS else 30 |
| 63 | + result = run_load_nvidia_dynamic_lib_in_subprocess(libname, timeout=timeout) |
| 64 | + if result.returncode != 0: |
| 65 | + raise RuntimeError(build_child_process_failed_for_libname_message(libname, result)) |
| 66 | + payload = parse_dynamic_lib_subprocess_payload( |
| 67 | + result.stdout, |
| 68 | + libname=libname, |
| 69 | + error_label="Load subprocess child process", |
| 70 | + ) |
| 71 | + |
| 72 | + if payload.status == STATUS_NOT_FOUND: |
| 73 | + with pytest.raises(DynamicLibNotFoundError): |
| 74 | + locate_nvidia_dynamic_lib(libname) |
| 75 | + info_summary_append(f"Not found: {libname=!r}") |
| 76 | + return |
| 77 | + |
| 78 | + located = locate_nvidia_dynamic_lib(libname) |
| 79 | + info_summary_append(f"abs_path={quote_for_shell(located.abs_path)} found_via={located.found_via}") |
| 80 | + assert located.name == libname |
| 81 | + assert located.filename == os.path.basename(located.abs_path) |
| 82 | + assert os.path.samefile(located.abs_path, payload.abs_path) |
| 83 | + assert find_nvidia_dynamic_lib(libname) == located.abs_path |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 87 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 88 | +def test_early_find_skips_system_search(mocker, tmp_path): |
| 89 | + lib = tmp_path / "libcudart.so.13" |
| 90 | + _patch_find_steps(mocker, early=FindResult(str(lib), "site-packages")) |
| 91 | + probe = mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess") |
| 92 | + |
| 93 | + assert locate_nvidia_dynamic_lib("cudart") == LocatedDynamicLib( |
| 94 | + name="cudart", |
| 95 | + abs_path=str(lib), |
| 96 | + filename="libcudart.so.13", |
| 97 | + found_via="site-packages", |
| 98 | + ) |
| 99 | + probe.assert_not_called() |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 103 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 104 | +def test_system_search_runs_before_late_find_steps(mocker, tmp_path): |
| 105 | + lib = tmp_path / "libcudart.so.13" |
| 106 | + run_find_steps = _patch_find_steps(mocker, late=FindResult("unused", "CUDA_PATH")) |
| 107 | + probe = mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=str(lib)) |
| 108 | + |
| 109 | + located = locate_nvidia_dynamic_lib("cudart") |
| 110 | + |
| 111 | + assert located.abs_path == str(lib) |
| 112 | + assert located.found_via == "system-search" |
| 113 | + probe.assert_called_once_with("cudart") |
| 114 | + assert [call.args[1] for call in run_find_steps.call_args_list] == [EARLY_FIND_STEPS] |
| 115 | + |
| 116 | + |
| 117 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 118 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 119 | +def test_late_find_steps_after_system_search(mocker, tmp_path): |
| 120 | + lib = tmp_path / "libcudart.so.13" |
| 121 | + _patch_find_steps(mocker, late=FindResult(str(lib), "CUDA_PATH")) |
| 122 | + mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) |
| 123 | + |
| 124 | + located = locate_nvidia_dynamic_lib("cudart") |
| 125 | + |
| 126 | + assert located.abs_path == str(lib) |
| 127 | + assert located.found_via == "CUDA_PATH" |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 131 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 132 | +def test_ctk_root_canary_is_last(mocker, tmp_path): |
| 133 | + lib = tmp_path / "libnvvm.so.4" |
| 134 | + _patch_find_steps(mocker) |
| 135 | + mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) |
| 136 | + canary = mocker.patch(f"{_MODULE}._try_ctk_root_canary", return_value=str(lib)) |
| 137 | + |
| 138 | + located = locate_nvidia_dynamic_lib("nvvm") |
| 139 | + |
| 140 | + assert located.abs_path == str(lib) |
| 141 | + assert located.found_via == "system-ctk-root" |
| 142 | + canary.assert_called_once() |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 146 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 147 | +def test_not_found_raises(mocker): |
| 148 | + _patch_find_steps(mocker) |
| 149 | + mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) |
| 150 | + mocker.patch(f"{_MODULE}._try_ctk_root_canary", return_value=None) |
| 151 | + |
| 152 | + with pytest.raises(DynamicLibNotFoundError, match="Failure finding"): |
| 153 | + locate_nvidia_dynamic_lib("nvvm") |
| 154 | + with pytest.raises(DynamicLibNotFoundError, match="Failure finding"): |
| 155 | + find_nvidia_dynamic_lib("nvvm") |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 159 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 160 | +def test_driver_lib_uses_system_search_only(mocker, tmp_path): |
| 161 | + lib = tmp_path / "libcuda.so.1" |
| 162 | + run_find_steps = mocker.patch(f"{_MODULE}.run_find_steps") |
| 163 | + mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=str(lib)) |
| 164 | + |
| 165 | + located = locate_nvidia_dynamic_lib("cuda") |
| 166 | + |
| 167 | + assert located.abs_path == str(lib) |
| 168 | + assert located.found_via == "system-search" |
| 169 | + run_find_steps.assert_not_called() |
| 170 | + |
| 171 | + |
| 172 | +@pytest.mark.usefixtures("no_in_process_loading") |
| 173 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 174 | +def test_driver_lib_not_found_raises(mocker): |
| 175 | + mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) |
| 176 | + |
| 177 | + with pytest.raises(DynamicLibNotFoundError, match="NVIDIA driver library"): |
| 178 | + locate_nvidia_dynamic_lib("cuda") |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 182 | +def test_find_is_cached(mocker, tmp_path): |
| 183 | + lib = tmp_path / "libcudart.so.13" |
| 184 | + locate = mocker.patch( |
| 185 | + f"{_MODULE}.locate_nvidia_dynamic_lib", |
| 186 | + return_value=LocatedDynamicLib("cudart", str(lib), lib.name, "site-packages"), |
| 187 | + ) |
| 188 | + |
| 189 | + assert find_mod.find_nvidia_dynamic_lib("cudart") == str(lib) |
| 190 | + assert find_mod.find_nvidia_dynamic_lib("cudart") == str(lib) |
| 191 | + locate.assert_called_once_with("cudart") |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 195 | +def test_unknown_libname_raises_dynamic_lib_unknown_error(): |
| 196 | + with pytest.raises(DynamicLibUnknownError, match=r"Unknown library name: 'not_a_real_lib'"): |
| 197 | + locate_nvidia_dynamic_lib("not_a_real_lib") |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.agent_authored(model="claude-opus-5-5") |
| 201 | +def test_known_but_platform_unavailable_libname_raises_dynamic_lib_not_available_error(monkeypatch): |
| 202 | + monkeypatch.setattr(load_mod, "_ALL_KNOWN_LIBNAMES", frozenset(("known_but_unavailable",))) |
| 203 | + monkeypatch.setattr(load_mod, "ALL_AVAILABLE_LIBNAMES", frozenset()) |
| 204 | + monkeypatch.setattr(load_mod, "_PLATFORM_NAME", "TestOS") |
| 205 | + with pytest.raises(DynamicLibNotAvailableError, match=r"known_but_unavailable.*not available on TestOS"): |
| 206 | + locate_nvidia_dynamic_lib("known_but_unavailable") |
0 commit comments