dlpack icon indicating copy to clipboard operation
dlpack copied to clipboard

tvm ndarray to numpy conversion doesn't give the correct answer

Open bethalianovike opened this issue 1 year ago • 0 comments

Hi, I try to convert tvm ndarray to numpy by dlpack's to_numpy function. If the tvm ndarray's device is CPU, the returned numpy is correct, but if the device is OpenCL, the returned numpy is incorrect. Is this function only support for tvm ndarray in CPU? Here is the code and error message. Thank you in advance!

>>> import tvm
>>> import numpy as np
>>> from dlpack import to_numpy

>>> tvm_array = tvm.nd.array(np.random.normal(size=[10,10]),device=tvm.cpu(0))
>>> np_array_ref = tvm_array.numpy()
>>> np_array = to_numpy(tvm_array.__dlpack__())
>>> np.testing.assert_equal(actual=np_array, desired=np_array_ref)

>>> tvm_array = tvm.nd.array(np.random.normal(size=[10,10]),device=tvm.opencl(0))
>>> np_array_ref = tvm_array.numpy()
>>> np_array = to_numpy(tvm_array.__dlpack__())
>>> np.testing.assert_equal(actual=np_array, desired=np_array_ref)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/miniconda3/envs/test/lib/python3.11/site-packages/numpy/testing/_private/utils.py", line 282, in assert_equal
    return assert_array_equal(actual, desired, err_msg, verbose)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/miniconda3/envs/test/lib/python3.11/site-packages/numpy/testing/_private/utils.py", line 920, in assert_array_equal
    assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
  File "/home/miniconda3/envs/test/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/miniconda3/envs/test/lib/python3.11/site-packages/numpy/testing/_private/utils.py", line 797, in assert_array_compare
    raise AssertionError(msg)
AssertionError:
Arrays are not equal

Mismatched elements: 100 / 100 (100%)
Max absolute difference: 1.45371627e+135
Max relative difference: 6.57123264e+135
 x: array([[5.082078e-310, 0.000000e+000, 0.000000e+000, 3.211427e-322,
        5.082075e-310, 6.833474e-310, 5.082075e-310, 1.453716e+135,
        4.144523e-316, 2.080551e-314],...
 y: array([[-0.032956, -0.489345, -0.859244,  1.352982,  1.518199,  0.485014,
         0.862967,  0.221224,  0.062524, -1.04238 ],
       [ 0.660143, -1.057973, -0.581994, -0.640922, -0.100951,  0.78747 ,...

bethalianovike avatar Sep 13 '24 07:09 bethalianovike