Efficient Mojo Tensor To Numpy Array Conversion
Hello,
I am attempting to convert a Mojo Tensor to a Numpy array without looping or at least with sub-polynomial time.
My approach is to get the pointer to the Tensor and use Python's ctypes.from_address()
But it doesn't work - the output np_array is not the same as the tensor...maybe Tensor has some header bytes? Or the pointer does not point to a contiguous memory block?
I would like to understand the issue with this code and get some inputs on how to achieve fast conversions for potentially large Mojo Tensors.
Here is the code:
Thank you.
1 Reply
Hi, this code worked for me although it has some problem, for some reason, the first two elements (at least from my very little tests) of the pointer are wrong, you can also confirm that by printing the pointer values in mojo, after that i just casted the pointer to a numpy array with ctypes and numpy