tqchen commented on a change in pull request #12047: [MXNET-779]Add DLPack Transformation API URL: https://github.com/apache/incubator-mxnet/pull/12047#discussion_r209416606
########## File path: include/mxnet/c_api.h ########## @@ -737,6 +741,57 @@ MXNET_DLL int MXNDArrayGetShape(NDArrayHandle handle, */ MXNET_DLL int MXNDArrayGetData(NDArrayHandle handle, void **out_pdata); +/*! +* \brief Create a reference view of NDArray that +* represents as DLManagedTensor until +* all the pending writes with respect NDArray are finished. +* \param handle the handle to the ndarray +* \param out_dlpack pointer holder to get pointer of DLManagedTensor +* \return 0 when success, -1 when failure happens +*/ +MXNET_DLL int MXNDArrayToDLPackForRead(NDArrayHandle handle, + DLManagedTensorHandle *out_dlpack); + +/*! +* \brief Create a reference view of NDArray that +* represents as DLManagedTensor until +* all the pending reads/writes with respect NDArray are finished. +* \param handle the handle to the ndarray +* \param out_dlpack pointer holder to get pointer of DLManagedTensor +* \return 0 when success, -1 when failure happens +*/ +MXNET_DLL int MXNDArrayToDLPackForWrite(NDArrayHandle handle, + DLManagedTensorHandle *out_dlpack); + +/*! +* \brief Create a NDArray backed by a dlpack tensor. +* +* This allows us to create a NDArray using the memory +* allocated by an external deep learning framework +* that is DLPack compatible. +* +* The memory is retained until the NDArray went out of scope. +* +* \param dlpack the pointer of the input DLManagedTensor +* \param out_handle pointer holder to get pointer of NDArray +* \return 0 when success, -1 when failure happens +*/ +MXNET_DLL int MXNDArrayFromDLPack(DLManagedTensorHandle dlpack, + NDArrayHandle *out_handle); +/*! + * \brief Delete a dlpack tensor + * \param dlpack the pointer of the input DLManagedTensor + * \return 0 when success, -1 when failure happens + */ +MXNET_DLL int MXNDArrayCallDLPackDeleter(DLManagedTensorHandle dlpack); + +/*! + * \brief Delete a dlpack tensor + * \param dlpack_capsule the pointer of a PyCapsule storing DLManagedTensor + * \return 0 when success, -1 when failure happens + */ +MXNET_DLL void MXNDArrayCallDLPackCapsuleDeleter(PyObjectHandle dlpack_capsule); Review comment: I see two problems in your particular gist you paste. - The destructor need to be declared in the global scope(instead of constructing when passing to the argument) - THe cstring need to outlive the capsule(construct a global string) - The function need to outlive the capsule(constructor c func and put it under global scope) ```python cfunc = ctypes.CFUNCTYPE(None, ctypes.c_void_p) def dfunc(dltensor): pycaps = ctypes.cast(dltensor, ctypes.py_object) pass c_destructor = cfunc(dfunc) c_str_dltensor = ctypes.c_char_p(b"dltensor") def test(): a = ctypes.pythonapi.PyCapsule_New(1, c_str_dltensor, c_destructor) test() ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services