Coverage for cuda / bindings / utils / _version_check.py: 95.65%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-08 01:07 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE 

3 

4import os 

5import threading 

6import warnings 

7 

8# Track whether we've already checked major version compatibility 

9_major_version_compatibility_checked = False 

10_lock = threading.Lock() 

11 

12 

13def warn_if_cuda_major_version_mismatch(): 

14 """Warn if the CUDA driver major version is older than cuda-bindings compile-time version. 

15 

16 This function compares the CUDA major version that cuda-bindings was compiled 

17 against with the CUDA major version supported by the installed driver. If the 

18 compile-time major version is greater than the driver's major version, a warning 

19 is issued. 

20 

21 The check runs only once per process. Subsequent calls are no-ops. 

22 

23 The warning can be suppressed by setting the environment variable 

24 ``CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=1``. 

25 """ 

26 global _major_version_compatibility_checked 

27 if _major_version_compatibility_checked: 1afdebgc

28 return 1b

29 with _lock: 1afdebgc

30 if _major_version_compatibility_checked: 1afdebgc

31 return 

32 _major_version_compatibility_checked = True 1afdebgc

33 

34 # Allow users to suppress the warning 

35 if os.environ.get("CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING"): 1afdebgc

36 return 1g

37 

38 # Import here to avoid circular imports and allow lazy loading 

39 from cuda.bindings import driver 1afdebc

40 

41 # Get compile-time CUDA version from cuda-bindings 

42 compile_version = driver.CUDA_VERSION # e.g., 13010 1afdebc

43 compile_major = compile_version // 1000 1afdebc

44 

45 # Get runtime driver version 

46 err, runtime_version = driver.cuDriverGetVersion() 1afdebc

47 if err != driver.CUresult.CUDA_SUCCESS: 1afdebc

48 raise RuntimeError(f"Failed to query CUDA driver version: {err}") 1f

49 

50 runtime_major = runtime_version // 1000 1adebc

51 

52 if compile_major > runtime_major: 1adebc

53 warnings.warn( 1bc

54 f"cuda-bindings was built for CUDA major version {compile_major}, but the " 

55 f"NVIDIA driver only supports up to CUDA {runtime_major}. Some cuda-bindings " 

56 f"features may not work correctly. Consider updating your NVIDIA driver, " 

57 f"or using a cuda-bindings version built for CUDA {runtime_major}. " 

58 f"(Set CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=1 to suppress this warning.)", 

59 UserWarning, 

60 stacklevel=3, 

61 )