VPI __cuda_array_interface__ Interoperability

Trying to convert between VPI, cupy, jetson_utils cudaImage, which all support cuda_array_interface, except it is easy to go into VPI but not out because VPI’s is slightly different. VPI’s ‘shape’ and ‘strides’ are a list and the others are as a tuple.

On VPI 2.2.7 with AGX Orin 32gb Jetpack 5.1.1-b56

import vpi
import cupy
from jetson_utils import cudaImage

vpi_image = vpi.Image((320,256), format=vpi.Format.RGB8)
vpi_image.cuda().__cuda_array_interface__
# Note 'shape' and 'strides' are lists
#{'shape': [256, 320, 3], 'strides': [1024, 3, 1], 'typestr': 'B', 'data': (8796327936, False), 'version': 2}

cuArr = cupy.zeros((256,320,3), dtype=cupy.uint8)
cuArr.__cuda_array_interface__
#{'shape': (256, 320, 3), 'typestr': '|u1', 'descr': [('', '|u1')], 'stream': 1, 'version': 3, 'strides': None, 'data': (8796082176, False)}

cuImg = cudaImage(320,256, "rgb8")
cuImg.__cuda_array_interface__
# {'shape': (256, 320, 3), 'typestr': '<u1', 'data': (8774062080, False), 'version': 3}

# cupy to VPI
vpi_image2 = vpi.asimage(cuArr) #OK
# cudaImage to VPI
vpi_image3 = vpi.asimage(cuImg) #OK
# cudaImage to cupy
cuArr2 = cupy.asarray(cuImg) # OK

# vpi to cupy 
cuImg = cupy.asarray(vpi_image.cuda()) #ERROR
#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
# File "/usr/local/lib/python3.8/dist-packages/cupy/_creation/from_data.py", line 75, in asarray
#    return _core.array(a, dtype, False, order)
#  File "cupy/_core/core.pyx", line 2376, in cupy._core.core.array
#  File "cupy/_core/core.pyx", line 2388, in cupy._core.core.array
#  File "cupy/_core/core.pyx", line 2431, in cupy._core.core._array_from_cuda_array_interface
#  File "cupy/_core/core.pyx", line 2741, in cupy._core.core._convert_object_with_cuda_array_interface
# TypeError: Expected tuple, got list

Is there way to resolve this discrepancy so it is easier to go out from VPI?

Hi,

Thanks for reporting this.
Would you mind testing if the same issue can also be reproduced on JetPack 5.1.2/VPI2.3?

Thanks.

I might be able to try that soon but it is also this way with VPI 3.0.10 on x86

>>> c = cupy.zeros((8,8), dtype=numpy.uint8)
>>> v = vpi.asimage(c, vpi.Format.U8).convert(vpi.Format.U8, backend=vpi.Backend.CUDA)
>>> v.cuda().__cuda_array_interface__
{'shape': [8, 8], 'strides': [64, 1], 'typestr': 'B', 'data': (140678581453312, False), 'version': 2}
>>> vpi.__version__
'3.0.10'

Hi,

Thanks for the confirm and suggestion.

We will pass this feature request to our internal team.
Will let you know if we get any response.

Thanks.

https://numba.pydata.org/numba-doc/dev/cuda/cuda_array_interface.html

The spec says they should be tuple.

Since VPI 3 Python sources are available, I was able to quickly patch a proof of concept that allowed:

with vpi_img.rlock_cuda() as data:
    cupy_out = cupy.asarray(data)

I only confirmed that I could go cupy → vpi → cupy and compared the cupy arrays with cupy.equal_array

There is no update from you for a period, assuming this is not an issue any more.
Hence we are closing this topic. If need further support, please open a new one.
Thanks

Hi,

Thanks for the update.

Would you mind sharing the change you made with us?
So we can check with our internal team to see if this can be integrated into our further release.

Thanks.