|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 3 | + |
| 4 | +import os |
| 5 | +import warnings |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +import pytest |
| 9 | +from cuda.bindings import driver |
| 10 | +from cuda.bindings.utils import _version_check, warn_if_cuda_major_version_mismatch |
| 11 | + |
| 12 | + |
| 13 | +class TestVersionCompatibilityCheck: |
| 14 | + """Tests for CUDA major version mismatch warning function.""" |
| 15 | + |
| 16 | + @pytest.fixture(autouse=True) |
| 17 | + def reset_version_check(self, monkeypatch): |
| 18 | + """Reset the version compatibility check flag for each test, restoring after.""" |
| 19 | + monkeypatch.setattr(_version_check, "_major_version_compatibility_checked", False) |
| 20 | + |
| 21 | + def test_no_warning_when_driver_newer(self): |
| 22 | + """No warning should be issued when driver version >= compile version.""" |
| 23 | + # Mock compile version 12.9 and driver version 13.0 |
| 24 | + with ( |
| 25 | + mock.patch.object(driver, "CUDA_VERSION", 12090), |
| 26 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 13000)), |
| 27 | + warnings.catch_warnings(record=True) as w, |
| 28 | + ): |
| 29 | + warnings.simplefilter("always") |
| 30 | + warn_if_cuda_major_version_mismatch() |
| 31 | + assert len(w) == 0 |
| 32 | + |
| 33 | + def test_no_warning_when_same_major_version(self): |
| 34 | + """No warning should be issued when major versions match.""" |
| 35 | + # Mock compile version 12.9 and driver version 12.8 |
| 36 | + with ( |
| 37 | + mock.patch.object(driver, "CUDA_VERSION", 12090), |
| 38 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 39 | + warnings.catch_warnings(record=True) as w, |
| 40 | + ): |
| 41 | + warnings.simplefilter("always") |
| 42 | + warn_if_cuda_major_version_mismatch() |
| 43 | + assert len(w) == 0 |
| 44 | + |
| 45 | + def test_warning_when_compile_major_newer(self): |
| 46 | + """Warning should be issued when compile major version > driver major version.""" |
| 47 | + # Mock compile version 13.0 and driver version 12.8 |
| 48 | + with ( |
| 49 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 50 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 51 | + warnings.catch_warnings(record=True) as w, |
| 52 | + ): |
| 53 | + warnings.simplefilter("always") |
| 54 | + warn_if_cuda_major_version_mismatch() |
| 55 | + assert len(w) == 1 |
| 56 | + assert issubclass(w[0].category, UserWarning) |
| 57 | + assert "cuda-bindings was built for CUDA major version 13" in str(w[0].message) |
| 58 | + assert "only supports up to CUDA 12" in str(w[0].message) |
| 59 | + |
| 60 | + def test_warning_only_issued_once(self): |
| 61 | + """Warning should only be issued once per process.""" |
| 62 | + with ( |
| 63 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 64 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 65 | + warnings.catch_warnings(record=True) as w, |
| 66 | + ): |
| 67 | + warnings.simplefilter("always") |
| 68 | + warn_if_cuda_major_version_mismatch() |
| 69 | + warn_if_cuda_major_version_mismatch() |
| 70 | + warn_if_cuda_major_version_mismatch() |
| 71 | + # Only one warning despite multiple calls |
| 72 | + assert len(w) == 1 |
| 73 | + |
| 74 | + def test_warning_suppressed_by_env_var(self): |
| 75 | + """Warning should be suppressed when CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING is set.""" |
| 76 | + with ( |
| 77 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 78 | + mock.patch.object(driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_SUCCESS, 12080)), |
| 79 | + mock.patch.dict(os.environ, {"CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING": "1"}), |
| 80 | + warnings.catch_warnings(record=True) as w, |
| 81 | + ): |
| 82 | + warnings.simplefilter("always") |
| 83 | + warn_if_cuda_major_version_mismatch() |
| 84 | + assert len(w) == 0 |
| 85 | + |
| 86 | + def test_error_when_driver_version_fails(self): |
| 87 | + """Should raise RuntimeError if cuDriverGetVersion fails.""" |
| 88 | + with ( |
| 89 | + mock.patch.object(driver, "CUDA_VERSION", 13000), |
| 90 | + mock.patch.object( |
| 91 | + driver, "cuDriverGetVersion", return_value=(driver.CUresult.CUDA_ERROR_NOT_INITIALIZED, 0) |
| 92 | + ), |
| 93 | + pytest.raises(RuntimeError, match="Failed to query CUDA driver version"), |
| 94 | + ): |
| 95 | + warn_if_cuda_major_version_mismatch() |
0 commit comments