Skip to content

Commit 2dcb3f1

Browse files
authored
Fix CUDA pointer and coredump attribute types (#2929)
* Fix pointer device ordinal attribute result * Allow pointer ordinal null sentinels * Fix additional CUDA attribute result types * Format CUDA attribute regression test
1 parent f0fe2df commit 2dcb3f1

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

cuda_bindings/cuda/bindings/_lib/utils.pxd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ cimport cuda.bindings.driver as driver
55
cimport cuda.bindings.cydriver as cydriver
66
cimport cuda.bindings.cyruntime as cyruntime
77
from libcpp.vector cimport vector
8+
from libcpp cimport bool as cpp_bool
89
from cpython.buffer cimport PyBuffer_Release, Py_buffer
910

1011
cdef class _HelperKernelParams:
@@ -62,7 +63,7 @@ cdef class _HelperCUpointer_attribute:
6263
cdef unsigned int _uint
6364
cdef int _int
6465
cdef driver.CUdeviceptr _devptr
65-
cdef void** _void
66+
cdef void* _void
6667
cdef driver.CUDA_POINTER_ATTRIBUTE_P2P_TOKENS _token
6768
cdef bint _bool
6869
cdef unsigned long long _ull
@@ -165,5 +166,5 @@ cdef class _HelperCUcoredumpSettings:
165166
cdef object _references # keeps caller bytes alive so _charstar stays valid
166167

167168
# Return values
168-
cdef bint _bool
169+
cdef cpp_bool _bool
169170
cdef char* _charstar

cuda_bindings/cuda/bindings/_lib/utils.pxi

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ cdef class _HelperCUpointer_attribute:
251251
else:
252252
self._cptr = <void*><void_ptr>init_value.getPtr()
253253
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_MEMORY_TYPE,
254-
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES,
255254
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_IS_GPU_DIRECT_RDMA_CAPABLE,
256255
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_ACCESS_FLAGS,):
257256
self._uint = init_value
@@ -267,7 +266,7 @@ cdef class _HelperCUpointer_attribute:
267266
else:
268267
self._cptr = <void*><void_ptr>init_value.getPtr()
269268
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_HOST_POINTER,):
270-
self._void = <void**><void_ptr>init_value
269+
self._void = <void*><void_ptr>init_value
271270
self._cptr = <void*>&self._void
272271
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_P2P_TOKENS,):
273272
if self._is_getter:
@@ -281,7 +280,8 @@ cdef class _HelperCUpointer_attribute:
281280
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_MAPPED,):
282281
self._bool = init_value
283282
self._cptr = <void*>&self._bool
284-
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_BUFFER_ID,):
283+
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_BUFFER_ID,
284+
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES,):
285285
self._ull = init_value
286286
self._cptr = <void*>&self._ull
287287
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_RANGE_SIZE,):
@@ -308,11 +308,11 @@ cdef class _HelperCUpointer_attribute:
308308
if self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_CONTEXT,):
309309
return self._ctx
310310
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_MEMORY_TYPE,
311-
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL,
312-
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES,
313311
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_IS_GPU_DIRECT_RDMA_CAPABLE,
314312
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_ACCESS_FLAGS,):
315313
return self._uint
314+
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL,):
315+
return self._int
316316
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_DEVICE_POINTER,
317317
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_RANGE_START_ADDR,):
318318
return self._devptr
@@ -325,7 +325,8 @@ cdef class _HelperCUpointer_attribute:
325325
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE,
326326
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_MAPPED,):
327327
return self._bool
328-
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_BUFFER_ID,):
328+
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_BUFFER_ID,
329+
cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES,):
329330
return self._ull
330331
elif self._attr in (cydriver.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_RANGE_SIZE,):
331332
return self._size
@@ -680,7 +681,7 @@ cdef class _HelperCUcoredumpSettings:
680681
self._bool = init_value
681682

682683
self._cptr = <void*>&self._bool
683-
self._size = 1
684+
self._size = sizeof(cpp_bool)
684685
else:
685686
raise TypeError('Unsupported attribute: {}'.format(attr.name))
686687

cuda_bindings/tests/test_cuda.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,23 @@ def test_pointer_get_attributes_device_ordinal():
379379
cuda.CUpointer_attribute.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL,
380380
]
381381

382-
attrs = cuda.cuPointerGetAttributes(len(attributes), attributes, 0)
382+
err, attrs = cuda.cuPointerGetAttributes(len(attributes), attributes, 0)
383383

384-
# device ordinals are always small numbers. A large number would indicate
385-
# an overflow error.
384+
assert err == cuda.CUresult.CUDA_SUCCESS
385+
# Drivers use either negative sentinel for a null pointer. Both ensure the
386+
# signed device ordinal is returned rather than an unsigned value.
387+
assert attrs[0] in (cuda.CU_DEVICE_CPU, cuda.CU_DEVICE_INVALID)
388+
389+
390+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
391+
def test_pointer_allowed_handle_types_preserves_64_bits():
392+
helper = cuda._HelperCUpointer_attribute(
393+
cuda.CUpointer_attribute.CU_POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES, 0, is_getter=True
394+
)
395+
expected = 1 << 40
396+
ctypes.c_uint64.from_address(helper.cptr).value = expected
386397

387-
assert abs(attrs[1][0]) < 256
398+
assert helper.pyObj() == expected
388399

389400

390401
@pytest.mark.skipif(not supportsManagedMemory(), reason="When new attributes were introduced")
@@ -546,6 +557,16 @@ def test_cuda_coredump_attr():
546557
assert attr_list[3] is True
547558

548559

560+
@pytest.mark.agent_authored(model="gpt-5.6-sol")
561+
def test_coredump_bool_uses_single_byte_storage():
562+
helper = cuda._HelperCUcoredumpSettings(cuda.CUcoredumpSettings.CU_COREDUMP_TRIGGER_HOST, 0, is_getter=True)
563+
ctypes.c_uint32.from_address(helper.cptr).value = 0xFFFFFFFF
564+
ctypes.c_uint8.from_address(helper.cptr).value = 0
565+
566+
assert helper.size() == ctypes.sizeof(ctypes.c_bool)
567+
assert helper.pyObj() is False
568+
569+
549570
def test_get_error_name_and_string():
550571
err, device = cuda.cuDeviceGet(0)
551572
_, s = cuda.cuGetErrorString(err)

0 commit comments

Comments
 (0)