Skip to content

Commit 8164b1b

Browse files
leofangclaude
andcommitted
cuda.core: consolidate get_driver_version and get_driver_version_full
Merge the two separate driver version query functions into a single get_driver_version() that returns (umd_version, kmd_version) — a pair of version tuples. UMD is a 2-tuple (major, minor) and KMD is a 3-tuple (major, minor, patch). The function now requires NVML. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 50c19d0 commit 8164b1b

5 files changed

Lines changed: 51 additions & 64 deletions

File tree

cuda_core/cuda/core/system/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
__all__ = [
1313
"CUDA_BINDINGS_NVML_IS_COMPATIBLE",
1414
"get_driver_version",
15-
"get_driver_version_full",
1615
"get_num_devices",
1716
"get_process_name",
1817
]

cuda_core/cuda/core/system/_system.pyx

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,38 @@ else:
2929
from cuda.core._utils.cuda_utils import driver, handle_return, runtime
3030

3131

32-
def get_driver_version(kernel_mode: bool = False) -> tuple[int, int]:
32+
def get_driver_version() -> tuple[tuple[int, ...], tuple[int, ...]]:
3333
"""
3434
Get the driver version.
3535

36-
Parameters
37-
----------
38-
kernel_mode: bool
39-
When `True`, return the kernel-mode driver version, e.g. 580.65.06.
40-
Otherwise, return the user-mode driver version, e.g. 13.0.1.
36+
Returns both the user-mode (UMD / CUDA) driver version and the
37+
kernel-mode (KMD / GPU) driver version.
4138

4239
Returns
4340
-------
44-
version: tuple[int, int]
45-
Tuple in the format `(MAJOR, MINOR)`.
41+
version : tuple[tuple[int, ...], tuple[int, ...]]
42+
``(umd_version, kmd_version)`` where ``umd_version`` is typically
43+
a 2-tuple ``(MAJOR, MINOR)`` and ``kmd_version`` is typically
44+
a 3-tuple ``(MAJOR, MINOR, PATCH)``.
45+
46+
Raises
47+
------
48+
RuntimeError
49+
If the NVML library is not available.
4650
"""
47-
return get_driver_version_full(kernel_mode)[:2]
51+
if not CUDA_BINDINGS_NVML_IS_COMPATIBLE:
52+
raise RuntimeError("get_driver_version requires NVML support")
53+
initialize()
4854

55+
# UMD (user-mode / CUDA toolkit) version
56+
cdef int v
57+
v = nvml.system_get_cuda_driver_version()
58+
umd = (v // 1000, (v // 10) % 100)
4959

50-
def get_driver_version_full(kernel_mode: bool = False) -> tuple[int, int, int]:
51-
"""
52-
Get the full driver version.
60+
# KMD (kernel-mode / GPU driver) version
61+
kmd = tuple(int(x) for x in nvml.system_get_driver_version().split("."))
5362

54-
Parameters
55-
----------
56-
kernel_mode: bool
57-
When `True`, return the kernel-mode driver version, e.g. 580.65.06.
58-
Otherwise, return the user-mode driver version, e.g. 13.0.1.
59-
60-
Returns
61-
-------
62-
version: tuple[int, int, int]
63-
Tuple in the format `(MAJOR, MINOR, PATCH)`.
64-
"""
65-
cdef int v
66-
if kernel_mode:
67-
if not CUDA_BINDINGS_NVML_IS_COMPATIBLE:
68-
raise ValueError("Kernel-mode driver version requires NVML support")
69-
initialize()
70-
return tuple(int(v) for v in nvml.system_get_driver_version().split("."))
71-
else:
72-
if CUDA_BINDINGS_NVML_IS_COMPATIBLE:
73-
initialize()
74-
v = nvml.system_get_cuda_driver_version()
75-
else:
76-
v = handle_return(driver.cuDriverGetVersion())
77-
return (v // 1000, (v // 10) % 100, v % 10)
63+
return (umd, kmd)
7864

7965

8066
def get_nvml_version() -> tuple[int, ...]:
@@ -138,7 +124,6 @@ def get_process_name(pid: int) -> str:
138124
__all__ = [
139125
"get_driver_branch",
140126
"get_driver_version",
141-
"get_driver_version_full",
142127
"get_nvml_version",
143128
"get_num_devices",
144129
"get_process_name",

cuda_core/docs/source/api.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ Basic functions
255255
:toctree: generated/
256256

257257
system.get_driver_version
258-
system.get_driver_version_full
259258
system.get_driver_branch
260259
system.get_num_devices
261260
system.get_nvml_version

cuda_core/docs/source/release/1.0.0-notes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ Breaking changes
152152
:mod:`cuda.core.utils` module.
153153
(`#2028 <https://github.com/NVIDIA/cuda-python/issues/2028>`__)
154154

155+
- Consolidated ``system.get_driver_version()`` and
156+
``system.get_driver_version_full()`` into a single
157+
:func:`system.get_driver_version` that returns
158+
``(umd_version, kmd_version)`` — a 2-tuple of version tuples
159+
(UMD is ``(MAJOR, MINOR)``, KMD is ``(MAJOR, MINOR, PATCH)``).
160+
The function now requires NVML support and raises :class:`RuntimeError`
161+
if it is not available.
162+
155163
Fixes and enhancements
156164
-----------------------
157165

cuda_core/tests/system/test_system_system.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@
1919
from .conftest import skip_if_nvml_unsupported
2020

2121

22+
@skip_if_nvml_unsupported
2223
def test_driver_version():
23-
driver_version = system.get_driver_version()
24+
umd, kmd = system.get_driver_version()
25+
26+
# UMD: 2-tuple (major, minor), cross-check against cuDriverGetVersion
27+
assert isinstance(umd, tuple)
28+
assert len(umd) == 2
2429
version = handle_return(driver.cuDriverGetVersion())
25-
expected_driver_version = (version // 1000, (version % 1000) // 10)
26-
assert driver_version == expected_driver_version, "Driver version does not match expected value"
30+
expected_umd = (version // 1000, (version % 1000) // 10)
31+
assert umd == expected_umd, "UMD driver version does not match expected value"
32+
33+
# KMD: 3-tuple (major, minor, patch)
34+
assert isinstance(kmd, tuple)
35+
assert len(kmd) == 3
36+
ver_maj, ver_min, ver_patch = kmd
37+
assert 400 <= ver_maj < 1000
38+
assert ver_min >= 0
39+
assert 0 <= ver_patch <= 99
2740

2841

2942
def test_num_devices():
@@ -41,28 +54,11 @@ def test_devices():
4154
assert device.device_id == expected_device.device_id, "Device ID does not match expected value"
4255

4356

44-
def test_cuda_driver_version():
45-
cuda_driver_version = system.get_driver_version_full()
46-
assert isinstance(cuda_driver_version, tuple)
47-
assert len(cuda_driver_version) == 3
48-
49-
ver_maj, ver_min, ver_patch = cuda_driver_version
50-
assert ver_maj >= 10
51-
assert 0 <= ver_min <= 99
52-
assert 0 <= ver_patch <= 9
53-
54-
55-
@skip_if_nvml_unsupported
56-
def test_gpu_driver_version():
57-
driver_version = system.get_driver_version(kernel_mode=True)
58-
assert isinstance(driver_version, tuple)
59-
assert len(driver_version) in (2, 3)
60-
61-
(ver_maj, ver_min, *ver_patch) = driver_version
62-
assert 400 <= ver_maj < 1000
63-
assert ver_min >= 0
64-
if ver_patch:
65-
assert 0 <= ver_patch[0] <= 99
57+
def test_driver_version_requires_nvml():
58+
if system.CUDA_BINDINGS_NVML_IS_COMPATIBLE:
59+
pytest.skip("NVML is available, cannot test the error path")
60+
with pytest.raises(RuntimeError, match="requires NVML support"):
61+
system.get_driver_version()
6662

6763

6864
@skip_if_nvml_unsupported

0 commit comments

Comments
 (0)