|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
4 | 4 |
|
| 5 | +import os |
| 6 | +import warnings |
| 7 | +from unittest import mock |
| 8 | + |
5 | 9 | import pytest |
6 | 10 | from cuda.bindings import driver, runtime |
7 | 11 | from cuda.core._utils import cuda_utils |
@@ -75,3 +79,108 @@ def test_check_runtime_error(): |
75 | 79 | assert enum_name in msg |
76 | 80 | # Smoke test: We don't want most to be unexpected. |
77 | 81 | assert num_unexpected < len(driver.CUresult) * 0.5 |
| 82 | + |
| 83 | + |
| 84 | +class TestVersionCompatibilityCheck: |
| 85 | + """Tests for check_cuda_version_compatibility function.""" |
| 86 | + |
| 87 | + def setup_method(self): |
| 88 | + """Reset the version compatibility check flag before each test.""" |
| 89 | + cuda_utils.reset_version_compatibility_check() |
| 90 | + |
| 91 | + def teardown_method(self): |
| 92 | + """Reset the version compatibility check flag after each test.""" |
| 93 | + cuda_utils.reset_version_compatibility_check() |
| 94 | + |
| 95 | + def test_no_warning_when_driver_newer(self): |
| 96 | + """No warning should be issued when driver version >= compile version.""" |
| 97 | + # Mock compile version 12.9 and driver version 13.0 |
| 98 | + with ( |
| 99 | + mock.patch.object(driver, "CUDA_VERSION", 12090), |
| 100 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 13000)), |
| 101 | + warnings.catch_warnings(record=True) as w, |
| 102 | + ): |
| 103 | + warnings.simplefilter("always") |
| 104 | + cuda_utils.check_cuda_version_compatibility() |
| 105 | + assert len(w) == 0 |
| 106 | + |
| 107 | + def test_no_warning_when_same_major_version(self): |
| 108 | + """No warning should be issued when major versions match.""" |
| 109 | + # Mock compile version 12.9 and driver version 12.8 |
| 110 | + with ( |
| 111 | + mock.patch.object(driver, "CUDA_VERSION", 12090), |
| 112 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 113 | + warnings.catch_warnings(record=True) as w, |
| 114 | + ): |
| 115 | + warnings.simplefilter("always") |
| 116 | + cuda_utils.check_cuda_version_compatibility() |
| 117 | + assert len(w) == 0 |
| 118 | + |
| 119 | + def test_warning_when_compile_major_newer(self): |
| 120 | + """Warning should be issued when compile major version > driver major version.""" |
| 121 | + # Mock compile version 13.0 and driver version 12.8 |
| 122 | + with ( |
| 123 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 124 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 125 | + warnings.catch_warnings(record=True) as w, |
| 126 | + ): |
| 127 | + warnings.simplefilter("always") |
| 128 | + cuda_utils.check_cuda_version_compatibility() |
| 129 | + assert len(w) == 1 |
| 130 | + assert issubclass(w[0].category, UserWarning) |
| 131 | + assert "cuda-python was built against CUDA 13.0" in str(w[0].message) |
| 132 | + assert "driver only supports CUDA 12.8" in str(w[0].message) |
| 133 | + |
| 134 | + def test_warning_only_issued_once(self): |
| 135 | + """Warning should only be issued once per process.""" |
| 136 | + with ( |
| 137 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 138 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 139 | + warnings.catch_warnings(record=True) as w, |
| 140 | + ): |
| 141 | + warnings.simplefilter("always") |
| 142 | + cuda_utils.check_cuda_version_compatibility() |
| 143 | + cuda_utils.check_cuda_version_compatibility() |
| 144 | + cuda_utils.check_cuda_version_compatibility() |
| 145 | + # Only one warning despite multiple calls |
| 146 | + assert len(w) == 1 |
| 147 | + |
| 148 | + def test_warning_suppressed_by_env_var(self): |
| 149 | + """Warning should be suppressed when CUDA_PYTHON_DISABLE_VERSION_CHECK is set.""" |
| 150 | + with ( |
| 151 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 152 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 153 | + mock.patch.dict(os.environ, {"CUDA_PYTHON_DISABLE_VERSION_CHECK": "1"}), |
| 154 | + warnings.catch_warnings(record=True) as w, |
| 155 | + ): |
| 156 | + warnings.simplefilter("always") |
| 157 | + cuda_utils.check_cuda_version_compatibility() |
| 158 | + assert len(w) == 0 |
| 159 | + |
| 160 | + def test_silent_when_driver_version_fails(self): |
| 161 | + """Should silently skip if cuDriverGetVersion fails.""" |
| 162 | + with ( |
| 163 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 164 | + mock.patch.object( |
| 165 | + driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_ERROR_NOT_INITIALIZED, 0) |
| 166 | + ), |
| 167 | + warnings.catch_warnings(record=True) as w, |
| 168 | + ): |
| 169 | + warnings.simplefilter("always") |
| 170 | + cuda_utils.check_cuda_version_compatibility() |
| 171 | + assert len(w) == 0 |
| 172 | + |
| 173 | + def test_silent_when_cuda_version_not_available(self): |
| 174 | + """Should silently skip if CUDA_VERSION attribute is not available.""" |
| 175 | + # Simulate older cuda-bindings without CUDA_VERSION |
| 176 | + with mock.patch.object(driver, "CUDA_VERSION", None): |
| 177 | + # Make accessing CUDA_VERSION raise AttributeError |
| 178 | + original = driver.CUDA_VERSION |
| 179 | + del driver.CUDA_VERSION |
| 180 | + try: |
| 181 | + with warnings.catch_warnings(record=True) as w: |
| 182 | + warnings.simplefilter("always") |
| 183 | + cuda_utils.check_cuda_version_compatibility() |
| 184 | + assert len(w) == 0 |
| 185 | + finally: |
| 186 | + driver.CUDA_VERSION = original |
0 commit comments