|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as load_mod |
| 7 | +from cuda.pathfinder._dynamic_libs import search_steps as steps_mod |
| 8 | +from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL |
| 9 | +from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( |
| 10 | + _load_lib_no_cache, |
| 11 | + _resolve_system_loaded_abs_path_in_subprocess, |
| 12 | +) |
| 13 | +from cuda.pathfinder._dynamic_libs.search_steps import EARLY_FIND_STEPS |
| 14 | +from cuda.pathfinder._utils.platform_aware import IS_WINDOWS |
| 15 | + |
| 16 | +_MODULE = "cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib" |
| 17 | +_STEPS_MODULE = "cuda.pathfinder._dynamic_libs.search_steps" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(autouse=True) |
| 21 | +def _clear_canary_subprocess_probe_cache(): |
| 22 | + _resolve_system_loaded_abs_path_in_subprocess.cache_clear() |
| 23 | + yield |
| 24 | + _resolve_system_loaded_abs_path_in_subprocess.cache_clear() |
| 25 | + |
| 26 | + |
| 27 | +def _make_loaded_dl(path, found_via): |
| 28 | + return LoadedDL(path, False, 0xDEAD, found_via) |
| 29 | + |
| 30 | + |
| 31 | +def _create_cupti_in_ctk(ctk_root): |
| 32 | + """Create a fake cupti lib in extras/CUPTI/lib64.""" |
| 33 | + if IS_WINDOWS: |
| 34 | + cupti_dir = ctk_root / "extras" / "CUPTI" / "lib64" |
| 35 | + cupti_dir.mkdir(parents=True, exist_ok=True) |
| 36 | + cupti_lib = cupti_dir / "cupti64_2025.4.1.dll" |
| 37 | + else: |
| 38 | + cupti_dir = ctk_root / "extras" / "CUPTI" / "lib64" |
| 39 | + cupti_dir.mkdir(parents=True, exist_ok=True) |
| 40 | + cupti_lib = cupti_dir / "libcupti.so.13" |
| 41 | + # Create symlink like real CTK installations |
| 42 | + cupti_symlink = cupti_dir / "libcupti.so" |
| 43 | + cupti_symlink.symlink_to("libcupti.so.13") |
| 44 | + cupti_lib.write_bytes(b"fake") |
| 45 | + return cupti_lib |
| 46 | + |
| 47 | + |
| 48 | +# --------------------------------------------------------------------------- |
| 49 | +# Conda tests |
| 50 | +# Note: Site-packages and CTK are covered by real CI tests. |
| 51 | +# Mock tests focus on Conda (not covered by real CI) and error paths. |
| 52 | +# --------------------------------------------------------------------------- |
| 53 | + |
| 54 | + |
| 55 | +def test_cupti_found_in_conda(tmp_path, mocker, monkeypatch): |
| 56 | + """Test finding cupti in conda environment.""" |
| 57 | + if IS_WINDOWS: |
| 58 | + pytest.skip("Windows support for cupti not yet implemented") |
| 59 | + |
| 60 | + # Create conda structure |
| 61 | + conda_prefix = tmp_path / "conda_env" |
| 62 | + conda_lib_dir = conda_prefix / "lib" |
| 63 | + conda_lib_dir.mkdir(parents=True) |
| 64 | + cupti_lib = conda_lib_dir / "libcupti.so.13" |
| 65 | + cupti_lib.write_bytes(b"fake") |
| 66 | + |
| 67 | + # Mock conda discovery |
| 68 | + monkeypatch.setenv("CONDA_PREFIX", str(conda_prefix)) |
| 69 | + |
| 70 | + # Disable site-packages search |
| 71 | + def _run_find_steps_without_site_packages(ctx, steps): |
| 72 | + if steps is EARLY_FIND_STEPS: |
| 73 | + # Skip site-packages, only run conda |
| 74 | + from cuda.pathfinder._dynamic_libs.search_steps import find_in_conda |
| 75 | + |
| 76 | + result = find_in_conda(ctx) |
| 77 | + return result |
| 78 | + return steps_mod.run_find_steps(ctx, steps) |
| 79 | + |
| 80 | + mocker.patch(f"{_MODULE}.run_find_steps", side_effect=_run_find_steps_without_site_packages) |
| 81 | + mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) |
| 82 | + mocker.patch(f"{_MODULE}.load_dependencies") |
| 83 | + mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) |
| 84 | + mocker.patch(f"{_STEPS_MODULE}.get_cuda_home_or_path", return_value=None) |
| 85 | + mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) |
| 86 | + mocker.patch.object( |
| 87 | + load_mod.LOADER, |
| 88 | + "load_with_abs_path", |
| 89 | + side_effect=lambda _desc, path, via: _make_loaded_dl(path, via), |
| 90 | + ) |
| 91 | + |
| 92 | + result = _load_lib_no_cache("cupti") |
| 93 | + assert result.found_via == "conda" |
| 94 | + assert result.abs_path == str(cupti_lib) |
| 95 | + |
| 96 | + |
| 97 | +# --------------------------------------------------------------------------- |
| 98 | +# Error path tests |
| 99 | +# --------------------------------------------------------------------------- |
| 100 | + |
| 101 | + |
| 102 | +def test_cupti_not_found_raises_error(mocker): |
| 103 | + """Test that DynamicLibNotFoundError is raised when cupti is not found.""" |
| 104 | + if IS_WINDOWS: |
| 105 | + pytest.skip("Windows support for cupti not yet implemented") |
| 106 | + |
| 107 | + # Mock all search paths to return None |
| 108 | + def _run_find_steps_disabled(ctx, steps): |
| 109 | + return None |
| 110 | + |
| 111 | + mocker.patch(f"{_MODULE}.run_find_steps", side_effect=_run_find_steps_disabled) |
| 112 | + mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) |
| 113 | + mocker.patch(f"{_MODULE}.load_dependencies") |
| 114 | + mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) |
| 115 | + mocker.patch(f"{_STEPS_MODULE}.get_cuda_home_or_path", return_value=None) |
| 116 | + mocker.patch( |
| 117 | + f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", |
| 118 | + return_value=None, |
| 119 | + ) |
| 120 | + |
| 121 | + with pytest.raises(DynamicLibNotFoundError): |
| 122 | + _load_lib_no_cache("cupti") |
| 123 | + |
| 124 | + |
| 125 | +# --------------------------------------------------------------------------- |
| 126 | +# Search order tests (Conda-specific, since Conda is not covered by real CI) |
| 127 | +# --------------------------------------------------------------------------- |
| 128 | + |
| 129 | + |
| 130 | +def test_cupti_search_order_conda_before_cuda_home(tmp_path, mocker, monkeypatch): |
| 131 | + """Test that conda is searched before CUDA_HOME (CTK). |
| 132 | +
|
| 133 | + This test is important because Conda is not covered by real CI tests, |
| 134 | + so we need to verify the search order between Conda and CTK. |
| 135 | + """ |
| 136 | + if IS_WINDOWS: |
| 137 | + pytest.skip("Windows support for cupti not yet implemented") |
| 138 | + |
| 139 | + # Create both conda and CUDA_HOME structures |
| 140 | + conda_prefix = tmp_path / "conda_env" |
| 141 | + conda_lib_dir = conda_prefix / "lib" |
| 142 | + conda_lib_dir.mkdir(parents=True) |
| 143 | + conda_cupti_lib = conda_lib_dir / "libcupti.so.13" |
| 144 | + conda_cupti_lib.write_bytes(b"fake") |
| 145 | + |
| 146 | + ctk_root = tmp_path / "cuda-13.1" |
| 147 | + _create_cupti_in_ctk(ctk_root) |
| 148 | + |
| 149 | + # Mock discovery - disable site-packages, enable conda |
| 150 | + def _run_find_steps_without_site_packages(ctx, steps): |
| 151 | + if steps is EARLY_FIND_STEPS: |
| 152 | + # Skip site-packages, only run conda |
| 153 | + from cuda.pathfinder._dynamic_libs.search_steps import find_in_conda |
| 154 | + |
| 155 | + result = find_in_conda(ctx) |
| 156 | + return result |
| 157 | + return steps_mod.run_find_steps(ctx, steps) |
| 158 | + |
| 159 | + mocker.patch(f"{_MODULE}.run_find_steps", side_effect=_run_find_steps_without_site_packages) |
| 160 | + monkeypatch.setenv("CONDA_PREFIX", str(conda_prefix)) |
| 161 | + mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) |
| 162 | + mocker.patch(f"{_MODULE}.load_dependencies") |
| 163 | + mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) |
| 164 | + mocker.patch(f"{_STEPS_MODULE}.get_cuda_home_or_path", return_value=str(ctk_root)) |
| 165 | + mocker.patch.object( |
| 166 | + load_mod.LOADER, |
| 167 | + "load_with_abs_path", |
| 168 | + side_effect=lambda _desc, path, via: _make_loaded_dl(path, via), |
| 169 | + ) |
| 170 | + |
| 171 | + result = _load_lib_no_cache("cupti") |
| 172 | + assert result.found_via == "conda" |
| 173 | + assert result.abs_path == str(conda_cupti_lib) |
0 commit comments