Skip to content

Commit a1ef66d

Browse files
committed
Fix small bug in nvml.unit_get_devices()
1 parent ea0215f commit a1ef66d

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

cuda_bindings/cuda/bindings/nvml.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
44
#
5-
# This code was automatically generated across versions from 12.9.1 to 13.3.0, generator version 0.3.1.dev1602+g3c8d84404. Do not modify it directly.
5+
# This code was automatically generated across versions from 12.9.1 to 13.3.0, generator version 0.3.1.dev1779+ga8cc71818.d20260626. Do not modify it directly.
66

77
cimport cython # NOQA
88

@@ -26815,13 +26815,13 @@ cpdef object unit_get_devices(intptr_t unit):
2681526815
"""
2681626816
cdef unsigned int[1] deviceCount = [0]
2681726817
with nogil:
26818-
__status__ = nvmlUnitGetDevices(<nvmlUnit_t *>unit, <unsigned int*>deviceCount, NULL)
26818+
__status__ = nvmlUnitGetDevices(<nvmlUnit_t>unit, <unsigned int*>deviceCount, NULL)
2681926819
check_status_size(__status__)
2682026820
if deviceCount[0] == 0:
2682126821
return view.array(shape=(1,), itemsize=sizeof(intptr_t), format="P", mode="c")[:0]
2682226822
cdef view.array deviceArray = view.array(shape=(deviceCount[0],), itemsize=sizeof(intptr_t), format="P", mode="c")
2682326823
with nogil:
26824-
__status__ = nvmlUnitGetDevices(<nvmlUnit_t *>unit, <unsigned int*>deviceCount, <nvmlDevice_t *>deviceArray.data)
26824+
__status__ = nvmlUnitGetDevices(<nvmlUnit_t>unit, <unsigned int*>deviceCount, <nvmlDevice_t *>deviceArray.data)
2682526825
check_status(__status__)
2682626826
return deviceArray
2682726827

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
3+
4+
import pytest
5+
6+
from cuda.bindings import nvml
7+
8+
9+
@pytest.fixture
10+
def unit_handles(nvml_init):
11+
"""Yield all unit handles, or skip if no units are present."""
12+
count = nvml.unit_get_count()
13+
if count == 0:
14+
pytest.skip("No NVML units present on this system")
15+
return [nvml.unit_get_handle_by_index(i) for i in range(count)]
16+
17+
18+
@pytest.mark.agent_authored(model="claude-sonnet-4-6")
19+
def test_unit_get_devices_returns_array(unit_handles):
20+
"""Regression test: unit_get_devices must not crash due to double-pointer cast.
21+
22+
Previously nvmlUnitGetDevices was called with <nvmlUnit_t *>unit instead of
23+
<nvmlUnit_t>unit, causing undefined behaviour because nvmlUnit_t is itself an
24+
opaque pointer handle.
25+
"""
26+
for unit in unit_handles:
27+
devices = nvml.unit_get_devices(unit)
28+
# Result is a memoryview / cython array of intptr_t device handles.
29+
assert hasattr(devices, "__len__")
30+
# Each element must be a non-zero pointer (valid device handle).
31+
for dev in devices:
32+
assert dev != 0

0 commit comments

Comments
 (0)