From_dlpack + ctypes

I’m planning to have a plain C library that would return struct DLManagedTensor instances. I interface with it in Python with ctypes.

How would it be possible to use from_dlpack(...) on such ctypes DLManagedTensor objects? How do I construct a “PyCapsule” from such a ctypes object? Should I use ctypes.pythonapi.PyCapsule_New? Would there be any memory leaks of the capsule object themselves?

Thanks!

Yes, it seems that I would need to create the capsule manually. I found the CuPy support PR: https://github.com/cupy/cupy/pull/1082/files that does something similar on CuPy side

Do I understand correctly that PyTorch + DLPack implicitly assume Little Endian?

I made an example of reading audio with ffmpg in C (deallocation is also done in C), calling it from Python, and reading it into a PyTorch tensor via DLPack: https://github.com/vadimkantorov/readaudio

A simpler example of just demonstating my DLPack Python / NumPy bindings: https://github.com/vadimkantorov/pydlpack

Managing memory is not very sure, as I’m not very confident about NumPy / Python array / object lifecycle. If you take a look or find problems, feel free to drop into GitHub issues!