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

24 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-03 02:41 +0000

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

2# SPDX-License-Identifier: Apache-2.0 

3 

4import threading 

5import warnings 

6 

7from ._envvar import envvar_bool 

8 

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

10_major_version_compatibility_checked = False 

11_lock = threading.Lock() 

12 

13_DISABLE_WARNING_ENV_VAR = "CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING" 

14 

15 

16def warn_if_cuda_major_version_mismatch(): 

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

18 

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

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

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

22 is issued. 

23 

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

25 

26 The warning can be suppressed by setting the environment variable 

27 ``CUDA_PYTHON_DISABLE_MAJOR_VERSION_WARNING=1``. Setting it to ``0`` (or 

28 leaving it unset or empty) keeps the warning enabled. 

29 """ 

30 global _major_version_compatibility_checked 

31 if _major_version_compatibility_checked: 1agefcbhd

32 return 1b

33 with _lock: 1agefcbhd

34 if _major_version_compatibility_checked: 1agefcbhd

35 return 

36 _major_version_compatibility_checked = True 1agefcbhd

37 

38 # Allow users to suppress the warning 

39 if envvar_bool(_DISABLE_WARNING_ENV_VAR): 1agefcbhd

40 return 1h

41 

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

43 from cuda.bindings import driver 1agefcbd

44 

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

46 compile_version = driver.CUDA_VERSION # e.g., 13010 1agefcbd

47 compile_major = compile_version // 1000 1agefcbd

48 

49 # Get runtime driver version 

50 err, runtime_version = driver.cuDriverGetVersion() 1agefcbd

51 if err != driver.CUresult.CUDA_SUCCESS: 1agefcbd

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

53 

54 runtime_major = runtime_version // 1000 1aefcbd

55 

56 if compile_major > runtime_major: 1aefcbd

57 warnings.warn( 1cbd

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

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

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

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

62 f"(Set {_DISABLE_WARNING_ENV_VAR}=1 to suppress this warning.)", 

63 UserWarning, 

64 stacklevel=3, 

65 )