Skip to content

Commit 7b29de3

Browse files
authored
Deprecate and provide alternatives for things in cuda.core.system (NVIDIA#1374)
* Deprecate and provide alternatives for things in `cuda.core.system` In preparation for the new `cuda.core.system` package. * Add release notes * Remove AssertionError
1 parent 1fc792d commit 7b29de3

File tree

11 files changed

+89
-19
lines changed

11 files changed

+89
-19
lines changed

cuda_core/cuda/core/experimental/_device.pyx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,20 @@ class Device:
10241024
raise CUDAError("Internal error (current device is not equal to Device.device_id)")
10251025
return driver.CUcontext(<uintptr_t>ctx)
10261026

1027+
@classmethod
1028+
def get_all_devices(cls):
1029+
"""
1030+
Query the available device instances.
1031+
1032+
Returns
1033+
-------
1034+
tuple of Device
1035+
A tuple containing instances of available devices.
1036+
"""
1037+
from cuda.core.experimental import system
1038+
total = system.get_num_devices()
1039+
return tuple(cls(device_id) for device_id in range(total))
1040+
10271041
@property
10281042
def device_id(self) -> int:
10291043
"""Return device ordinal."""

cuda_core/cuda/core/experimental/_system.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
import warnings
6+
57
from cuda.core.experimental._device import Device
68
from cuda.core.experimental._utils.cuda_utils import driver, handle_return, runtime
79

@@ -23,8 +25,7 @@ def __init__(self):
2325
return
2426
self._initialized = True
2527

26-
@property
27-
def driver_version(self) -> tuple[int, int]:
28+
def get_driver_version(self) -> tuple[int, int]:
2829
"""
2930
Query the CUDA driver version.
3031
@@ -39,7 +40,27 @@ def driver_version(self) -> tuple[int, int]:
3940
return (major, minor)
4041

4142
@property
42-
def num_devices(self) -> int:
43+
def driver_version(self) -> tuple[int, int]:
44+
"""
45+
Query the CUDA driver version.
46+
47+
Returns
48+
-------
49+
tuple of int
50+
A 2-tuple of (major, minor) version numbers.
51+
52+
.. deprecated:: 0.5.0
53+
`cuda.core.system.driver_version` will be removed in 0.6.0.
54+
Use `cuda.core.system.get_driver_version()` instead.
55+
"""
56+
warnings.warn(
57+
"cuda.core.system.driver_version is deprecated. Use cuda.core.system.get_driver_version() instead.",
58+
DeprecationWarning,
59+
stacklevel=1,
60+
)
61+
return self.get_driver_version()
62+
63+
def get_num_devices(self) -> int:
4364
"""
4465
Query the number of available GPUs.
4566
@@ -50,6 +71,27 @@ def num_devices(self) -> int:
5071
"""
5172
return handle_return(runtime.cudaGetDeviceCount())
5273

74+
@property
75+
def num_devices(self) -> int:
76+
"""
77+
Query the number of available GPUs.
78+
79+
Returns
80+
-------
81+
int
82+
The number of available GPU devices.
83+
84+
.. deprecated:: 0.5.0
85+
`cuda.core.system.num_devices` will be removed in 0.6.0.
86+
Use `cuda.core.system.get_num_devices()` instead.
87+
"""
88+
warnings.warn(
89+
"cuda.core.system.num_devices is deprecated. Use cuda.core.system.get_num_devices() instead.",
90+
DeprecationWarning,
91+
stacklevel=1,
92+
)
93+
return self.get_num_devices()
94+
5395
@property
5496
def devices(self) -> tuple:
5597
"""
@@ -59,6 +101,14 @@ def devices(self) -> tuple:
59101
-------
60102
tuple of Device
61103
A tuple containing instances of available devices.
104+
105+
.. deprecated:: 0.5.0
106+
`cuda.core.system.devices` will be removed in 0.6.0.
107+
Use `cuda.core.Device.get_all_devices()` instead.
62108
"""
63-
total = self.num_devices
64-
return tuple(Device(device_id) for device_id in range(total))
109+
warnings.warn(
110+
"cuda.core.system.devices is deprecated. Use cuda.core.Device.get_all_devices() instead.",
111+
DeprecationWarning,
112+
stacklevel=1,
113+
)
114+
return Device.get_all_devices()

cuda_core/docs/source/api.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ CUDA compilation toolchain
6060
CUDA system information
6161
-----------------------
6262

63-
.. autoproperty:: cuda.core.experimental._system.System.driver_version
64-
.. autoproperty:: cuda.core.experimental._system.System.num_devices
65-
.. autoproperty:: cuda.core.experimental._system.System.devices
63+
.. automethod:: cuda.core.experimental._system.System.get_driver_version
64+
.. automethod:: cuda.core.experimental._system.System.get_num_devices
6665

6766

6867
.. module:: cuda.core.experimental.utils

cuda_core/docs/source/release/0.5.x-notes.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ None.
1616
Breaking Changes
1717
----------------
1818

19-
None.
19+
The following APIs have been deprecated and will be removed in 0.6.0:
20+
21+
- ``cuda.core.experimental.system.driver_version`` has been replaced with
22+
``cuda.core.experimental.system.get_driver_version()``.
23+
- ``cuda.core.experimental.system.num_devices`` has been replaced with
24+
``cuda.core.experimental.system.get_num_devices()``.
25+
- ``cuda.core.experimental.system.devices`` has been replaced with
26+
``cuda.core.experimental.Device.get_all_devices()``.
2027

2128
New features
2229
------------

cuda_core/examples/show_device_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def print_device_properties(properties):
215215

216216
# Print info about all CUDA devices in the system
217217
def show_device_properties():
218-
ndev = system.num_devices
218+
ndev = system.get_num_devices()
219219
print(f"Number of GPUs: {ndev}")
220220

221221
for device_id in range(ndev):

cuda_core/examples/simple_multi_gpu_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import cupy as cp
1515
from cuda.core.experimental import Device, LaunchConfig, Program, launch, system
1616

17-
if system.num_devices < 2:
17+
if system.get_num_devices() < 2:
1818
print("this example requires at least 2 GPUs", file=sys.stderr)
1919
sys.exit(0)
2020

cuda_core/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def mempool_device():
124124

125125

126126
def _mempool_device_impl(num):
127-
num_devices = len(cuda.core.experimental.system.devices)
127+
num_devices = len(cuda.core.experimental.Device.get_all_devices())
128128
if num_devices < num:
129129
pytest.skip(f"Test requires at least {num} GPUs")
130130

cuda_core/tests/test_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_device_alloc(deinit_cuda):
4848

4949

5050
def test_device_id(deinit_cuda):
51-
for device in cuda.core.experimental.system.devices:
51+
for device in Device.get_all_devices():
5252
device.set_current()
5353
assert device.device_id == handle_return(runtime.cudaGetDevice())
5454

cuda_core/tests/test_memory_peer_access.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def check(expected):
7272
with pytest.raises(ValueError, match=r"device_id must be \>\= 0"):
7373
dmr.peer_accessible_by = [-1] # device ID out of bounds
7474

75-
num_devices = len(cuda.core.experimental.system.devices)
75+
num_devices = len(cuda.core.experimental.Device.get_all_devices())
7676

7777
with pytest.raises(ValueError, match=r"device_id must be within \[0, \d+\)"):
7878
dmr.peer_accessible_by = [num_devices] # device ID out of bounds

cuda_core/tests/test_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import cuda.core.experimental
99
import pytest
10-
from cuda.core.experimental import Device, ObjectCode, Program, ProgramOptions, system
10+
from cuda.core.experimental import Device, ObjectCode, Program, ProgramOptions
1111
from cuda.core.experimental._utils.cuda_utils import CUDAError, driver, get_binding_version, handle_return
1212

1313
try:
@@ -135,7 +135,7 @@ def test_read_only_kernel_attributes(get_saxpy_kernel_cubin, attr, expected_type
135135
assert value is not None
136136

137137
# get the value for each device on the system, using either the device object or ordinal
138-
for device in system.devices:
138+
for device in Device.get_all_devices():
139139
value = method(device)
140140
value = method(device.device_id)
141141
assert isinstance(value, expected_type), f"Expected {attr} to be of type {expected_type}, but got {type(value)}"

0 commit comments

Comments
 (0)