|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for NVIDIA driver library loading ("cuda", "nvml"). |
| 5 | +
|
| 6 | +These libraries are part of the display driver, not the CUDA Toolkit. |
| 7 | +They use a simplified system-search-only path, skipping site-packages, |
| 8 | +conda, CUDA_HOME, and the canary probe. |
| 9 | +""" |
| 10 | + |
| 11 | +import json |
| 12 | +import os |
| 13 | + |
| 14 | +import pytest |
| 15 | +import spawned_process_runner |
| 16 | +from child_load_nvidia_dynamic_lib_helper import build_child_process_failed_for_libname_message, child_process_func |
| 17 | + |
| 18 | +from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL |
| 19 | +from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( |
| 20 | + _DRIVER_ONLY_LIBNAMES, |
| 21 | + _load_driver_lib_no_cache, |
| 22 | + _load_lib_no_cache, |
| 23 | +) |
| 24 | +from cuda.pathfinder._utils.platform_aware import IS_WINDOWS, quote_for_shell |
| 25 | + |
| 26 | +STRICTNESS = os.environ.get("CUDA_PATHFINDER_TEST_LOAD_NVIDIA_DYNAMIC_LIB_STRICTNESS", "see_what_works") |
| 27 | +assert STRICTNESS in ("see_what_works", "all_must_work") |
| 28 | + |
| 29 | +_MODULE = "cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib" |
| 30 | + |
| 31 | + |
| 32 | +def _make_loaded_dl(path, found_via): |
| 33 | + return LoadedDL(path, False, 0xDEAD, found_via) |
| 34 | + |
| 35 | + |
| 36 | +# --------------------------------------------------------------------------- |
| 37 | +# _load_driver_lib_no_cache |
| 38 | +# --------------------------------------------------------------------------- |
| 39 | + |
| 40 | + |
| 41 | +def test_driver_lib_returns_already_loaded(mocker): |
| 42 | + already = LoadedDL("/usr/lib/libcuda.so.1", True, 0xBEEF, "was-already-loaded-from-elsewhere") |
| 43 | + mocker.patch(f"{_MODULE}.check_if_already_loaded_from_elsewhere", return_value=already) |
| 44 | + mocker.patch(f"{_MODULE}.load_with_system_search") |
| 45 | + |
| 46 | + result = _load_driver_lib_no_cache("cuda") |
| 47 | + |
| 48 | + assert result is already |
| 49 | + # system search should not have been called |
| 50 | + from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as mod |
| 51 | + |
| 52 | + mod.load_with_system_search.assert_not_called() |
| 53 | + |
| 54 | + |
| 55 | +def test_driver_lib_falls_through_to_system_search(mocker): |
| 56 | + loaded = _make_loaded_dl("/usr/lib/libcuda.so.1", "system-search") |
| 57 | + mocker.patch(f"{_MODULE}.check_if_already_loaded_from_elsewhere", return_value=None) |
| 58 | + mocker.patch(f"{_MODULE}.load_with_system_search", return_value=loaded) |
| 59 | + |
| 60 | + result = _load_driver_lib_no_cache("cuda") |
| 61 | + |
| 62 | + assert result is loaded |
| 63 | + assert result.found_via == "system-search" |
| 64 | + |
| 65 | + |
| 66 | +def test_driver_lib_raises_when_not_found(mocker): |
| 67 | + mocker.patch(f"{_MODULE}.check_if_already_loaded_from_elsewhere", return_value=None) |
| 68 | + mocker.patch(f"{_MODULE}.load_with_system_search", return_value=None) |
| 69 | + |
| 70 | + with pytest.raises(DynamicLibNotFoundError, match="NVIDIA driver library"): |
| 71 | + _load_driver_lib_no_cache("nvml") |
| 72 | + |
| 73 | + |
| 74 | +def test_driver_lib_does_not_search_site_packages(mocker): |
| 75 | + """Driver libs must not go through the CTK search cascade.""" |
| 76 | + loaded = _make_loaded_dl("/usr/lib/libcuda.so.1", "system-search") |
| 77 | + mocker.patch(f"{_MODULE}.check_if_already_loaded_from_elsewhere", return_value=None) |
| 78 | + mocker.patch(f"{_MODULE}.load_with_system_search", return_value=loaded) |
| 79 | + |
| 80 | + from cuda.pathfinder._dynamic_libs.find_nvidia_dynamic_lib import _FindNvidiaDynamicLib |
| 81 | + |
| 82 | + spy = mocker.spy(_FindNvidiaDynamicLib, "try_site_packages") |
| 83 | + _load_driver_lib_no_cache("cuda") |
| 84 | + spy.assert_not_called() |
| 85 | + |
| 86 | + |
| 87 | +# --------------------------------------------------------------------------- |
| 88 | +# _load_lib_no_cache dispatches driver libs correctly |
| 89 | +# --------------------------------------------------------------------------- |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("libname", sorted(_DRIVER_ONLY_LIBNAMES)) |
| 93 | +def test_load_lib_no_cache_dispatches_to_driver_path(libname, mocker): |
| 94 | + loaded = _make_loaded_dl(f"/usr/lib/fake_{libname}.so", "system-search") |
| 95 | + mock_driver = mocker.patch(f"{_MODULE}._load_driver_lib_no_cache", return_value=loaded) |
| 96 | + |
| 97 | + result = _load_lib_no_cache(libname) |
| 98 | + |
| 99 | + assert result is loaded |
| 100 | + mock_driver.assert_called_once_with(libname) |
| 101 | + |
| 102 | + |
| 103 | +def test_load_lib_no_cache_does_not_dispatch_ctk_lib_to_driver_path(mocker): |
| 104 | + """Ensure regular CTK libs don't take the driver shortcut.""" |
| 105 | + mock_driver = mocker.patch(f"{_MODULE}._load_driver_lib_no_cache") |
| 106 | + # Let the normal path run far enough to prove the driver path wasn't used. |
| 107 | + # We'll make it fail quickly at check_if_already_loaded_from_elsewhere. |
| 108 | + from cuda.pathfinder._dynamic_libs.find_nvidia_dynamic_lib import _FindNvidiaDynamicLib |
| 109 | + |
| 110 | + mocker.patch.object(_FindNvidiaDynamicLib, "try_site_packages", return_value=None) |
| 111 | + mocker.patch.object(_FindNvidiaDynamicLib, "try_with_conda_prefix", return_value=None) |
| 112 | + mocker.patch(f"{_MODULE}.check_if_already_loaded_from_elsewhere", return_value=None) |
| 113 | + mocker.patch(f"{_MODULE}.load_dependencies") |
| 114 | + mocker.patch( |
| 115 | + f"{_MODULE}.load_with_system_search", |
| 116 | + return_value=_make_loaded_dl("/usr/lib/libcudart.so.13", "system-search"), |
| 117 | + ) |
| 118 | + |
| 119 | + _load_lib_no_cache("cudart") |
| 120 | + |
| 121 | + mock_driver.assert_not_called() |
| 122 | + |
| 123 | + |
| 124 | +# --------------------------------------------------------------------------- |
| 125 | +# Real loading tests (spawned child process for isolation) |
| 126 | +# --------------------------------------------------------------------------- |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize("libname", sorted(_DRIVER_ONLY_LIBNAMES)) |
| 130 | +def test_real_load_driver_lib(info_summary_append, libname): |
| 131 | + """Load a real driver library in a child process. |
| 132 | +
|
| 133 | + This complements the mock tests above: it exercises the actual OS |
| 134 | + loader path and logs results via INFO for CI/QA inspection. |
| 135 | + """ |
| 136 | + timeout = 120 if IS_WINDOWS else 30 |
| 137 | + result = spawned_process_runner.run_in_spawned_child_process(child_process_func, args=(libname,), timeout=timeout) |
| 138 | + |
| 139 | + def raise_child_process_failed(): |
| 140 | + raise RuntimeError(build_child_process_failed_for_libname_message(libname, result)) |
| 141 | + |
| 142 | + if result.returncode != 0: |
| 143 | + raise_child_process_failed() |
| 144 | + assert not result.stderr |
| 145 | + if result.stdout.startswith("CHILD_LOAD_NVIDIA_DYNAMIC_LIB_HELPER_DYNAMIC_LIB_NOT_FOUND_ERROR:"): |
| 146 | + if STRICTNESS == "all_must_work": |
| 147 | + raise_child_process_failed() |
| 148 | + info_summary_append(f"Not found: {libname=!r}") |
| 149 | + else: |
| 150 | + abs_path = json.loads(result.stdout.rstrip()) |
| 151 | + info_summary_append(f"abs_path={quote_for_shell(abs_path)}") |
| 152 | + assert os.path.isfile(abs_path) |
0 commit comments