Skip to content

Commit dbc22cf

Browse files
Andy-Jostclaude
andcommitted
cuda.core: let Cython propagate DLPack exchange API errors
Declare the exchange API callback typedefs and their implementations `except -1`, which is the contract dlpack.h documents. The entry points raise instead of setting the error indicator by hand, and any exception, including KeyboardInterrupt, reaches the consumer as -1 with the exception set. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 4b6d833 commit dbc22cf

2 files changed

Lines changed: 30 additions & 54 deletions

File tree

‎cuda_core/cuda/core/_dlpack.pxd‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

@@ -90,26 +90,28 @@ cdef extern from "_include/dlpack.h":
9090
void (*SetError)(void* error_ctx, const char* kind, const char* message) noexcept
9191
)
9292

93+
# dlpack.h: these entry points return -1 on failure with a Python
94+
# exception set. `except -1` tells Cython so; it does not change the C type.
9395
ctypedef int (*DLPackManagedTensorFromPyObjectNoSync)(
9496
void* py_object,
9597
DLManagedTensorVersioned** out
96-
)
98+
) except -1
9799

98100
ctypedef int (*DLPackManagedTensorToPyObjectNoSync)(
99101
DLManagedTensorVersioned* tensor,
100102
void** out_py_object
101-
)
103+
) except -1
102104

103105
ctypedef int (*DLPackDLTensorFromPyObjectNoSync)(
104106
void* py_object,
105107
DLTensor* out
106-
)
108+
) except -1
107109

108110
ctypedef int (*DLPackCurrentWorkStream)(
109111
_DLDeviceType device_type,
110112
int32_t device_id,
111113
void** out_current_stream
112-
)
114+
) except -1
113115

114116
ctypedef struct DLPackExchangeAPIHeader:
115117
DLPackVersion version

‎cuda_core/cuda/core/_memoryview.pyx‎

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -899,91 +899,65 @@ cdef int _smv_managed_tensor_allocator(
899899
cdef int _smv_managed_tensor_from_py_object_no_sync(
900900
void* py_object,
901901
DLManagedTensorVersioned** out,
902-
) noexcept with gil:
902+
) except -1 with gil:
903903
cdef DLManagedTensorVersioned* dlm_tensor_ver = NULL
904-
cdef object caught = None
905904
if out == NULL:
906-
cpython.PyErr_SetString(RuntimeError, b"out cannot be NULL")
907-
return -1
905+
raise RuntimeError("out cannot be NULL")
908906
out[0] = NULL
909907
cdef object obj = <object>py_object
910908
if not isinstance(obj, StridedMemoryView):
911-
cpython.PyErr_SetString(TypeError, b"py_object must be a StridedMemoryView")
912-
return -1
909+
raise TypeError("py_object must be a StridedMemoryView")
913910
try:
914911
dlm_tensor_ver = _smv_allocate_dlm_tensor_versioned()
915912
_smv_fill_managed_tensor_versioned(dlm_tensor_ver, <StridedMemoryView>obj)
916-
except Exception as exc:
913+
except BaseException:
914+
# Rollback must also run for KeyboardInterrupt.
917915
_smv_versioned_deleter(dlm_tensor_ver)
918-
caught = exc
919-
if caught is not None:
920-
cpython.PyErr_SetObject(type(caught), caught)
921-
return -1
916+
raise
922917
out[0] = dlm_tensor_ver
923918
return 0
924919
925920
926921
cdef int _smv_managed_tensor_to_py_object_no_sync(
927922
DLManagedTensorVersioned* tensor,
928923
void** out_py_object,
929-
) noexcept with gil:
930-
cdef object capsule
931-
cdef object py_view
932-
cdef object caught = None
924+
) except -1 with gil:
933925
if out_py_object == NULL:
934-
cpython.PyErr_SetString(RuntimeError, b"out_py_object cannot be NULL")
935-
return -1
926+
raise RuntimeError("out_py_object cannot be NULL")
936927
out_py_object[0] = NULL
937928
if tensor == NULL:
938-
cpython.PyErr_SetString(RuntimeError, b"tensor cannot be NULL")
939-
return -1
940-
try:
941-
capsule = cpython.PyCapsule_New(
942-
<void*>tensor,
943-
DLPACK_VERSIONED_TENSOR_UNUSED_NAME,
944-
_smv_pycapsule_deleter,
945-
)
946-
py_view = _smv_from_dlpack_capsule(capsule, capsule)
947-
cpython.Py_INCREF(py_view)
948-
out_py_object[0] = <void*>py_view
949-
except Exception as exc:
950-
caught = exc
951-
if caught is not None:
952-
cpython.PyErr_SetObject(type(caught), caught)
953-
return -1
929+
raise RuntimeError("tensor cannot be NULL")
930+
cdef object capsule = cpython.PyCapsule_New(
931+
<void*>tensor,
932+
DLPACK_VERSIONED_TENSOR_UNUSED_NAME,
933+
_smv_pycapsule_deleter,
934+
)
935+
cdef object py_view = _smv_from_dlpack_capsule(capsule, capsule)
936+
cpython.Py_INCREF(py_view)
937+
out_py_object[0] = <void*>py_view
954938
return 0
955939
956940
957941
cdef int _smv_dltensor_from_py_object_no_sync(
958942
void* py_object,
959943
DLTensor* out,
960-
) noexcept with gil:
961-
cdef object caught = None
944+
) except -1 with gil:
962945
if out == NULL:
963-
cpython.PyErr_SetString(RuntimeError, b"out cannot be NULL")
964-
return -1
946+
raise RuntimeError("out cannot be NULL")
965947
cdef object obj = <object>py_object
966948
if not isinstance(obj, StridedMemoryView):
967-
cpython.PyErr_SetString(TypeError, b"py_object must be a StridedMemoryView")
968-
return -1
969-
try:
970-
_smv_setup_dltensor_borrowed(out, <StridedMemoryView>obj)
971-
except Exception as exc:
972-
caught = exc
973-
if caught is not None:
974-
cpython.PyErr_SetObject(type(caught), caught)
975-
return -1
949+
raise TypeError("py_object must be a StridedMemoryView")
950+
_smv_setup_dltensor_borrowed(out, <StridedMemoryView>obj)
976951
return 0
977952
978953
979954
cdef int _smv_current_work_stream(
980955
_DLDeviceType device_type,
981956
int32_t device_id,
982957
void** out_current_stream,
983-
) noexcept with gil:
958+
) except -1 with gil:
984959
if out_current_stream == NULL:
985-
cpython.PyErr_SetString(RuntimeError, b"out_current_stream cannot be NULL")
986-
return -1
960+
raise RuntimeError("out_current_stream cannot be NULL")
987961
# cuda.core has no global/current stream state today.
988962
out_current_stream[0] = NULL
989963
return 0

0 commit comments

Comments
 (0)