pascalpolygon
pascalpolygon
MModular
Created by pascalpolygon on 4/25/2024 in #questions
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:
def main():
var ctypes = Python.import_module("ctypes")
var np = Python.import_module("numpy")

var mojo_tensor = Tensor[DType.int32](
TensorShape(4, 4),
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
)

var mojo_tensor_ptr = mojo_tensor.data().__int__()

var num_elements = mojo_tensor.num_elements().__int__()
var tensor_type = mojo_tensor.type


#Load the hopefully contiguous tensor into a ctypes array
var int32_array = (ctypes.c_int32 * num_elements).from_address(mojo_tensor_ptr.__int__())
var np_array = np.ctypeslib.as_array(int32_array).astype(np.int32)

print("Numpy array: ", np_array)
def main():
var ctypes = Python.import_module("ctypes")
var np = Python.import_module("numpy")

var mojo_tensor = Tensor[DType.int32](
TensorShape(4, 4),
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
)

var mojo_tensor_ptr = mojo_tensor.data().__int__()

var num_elements = mojo_tensor.num_elements().__int__()
var tensor_type = mojo_tensor.type


#Load the hopefully contiguous tensor into a ctypes array
var int32_array = (ctypes.c_int32 * num_elements).from_address(mojo_tensor_ptr.__int__())
var np_array = np.ctypeslib.as_array(int32_array).astype(np.int32)

print("Numpy array: ", np_array)
Thank you.
2 replies
MModular
Created by pascalpolygon on 4/3/2024 in #questions
Is there a simple way to implement Numpy-style ndarray slicing on Mojo Tensors ?
I have a rank 4 tensor where the first dimension is an index and the last 3 represent a Voxel Grid.
# Initialize the observation space as a 4D tensor
self.observation_space = Tensor[DType.int32](num_builds, build_volume[0], build_volume[1], build_volume[2])
# Initialize the observation space as a 4D tensor
self.observation_space = Tensor[DType.int32](num_builds, build_volume[0], build_volume[1], build_volume[2])
I want to get the voxel grid given a build_id. I wrote a function to do this but I am wondering if I there's a Mojo way to do this.
2 replies
MModular
Created by pascalpolygon on 3/13/2024 in #questions
Numpy: Import from source dir error
I am trying to import Numpy in my mojo project using
from python import Python

fn main() raises:
var np = Python.import_module("numpy")
from python import Python

fn main() raises:
var np = Python.import_module("numpy")
But I get the following Error:
Unhandled exception caught during execution: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
mojo: error: execution exited with a non-zero result: 1
Unhandled exception caught during execution: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
mojo: error: execution exited with a non-zero result: 1
5 replies
MModular
Created by pascalpolygon on 2/29/2024 in #questions
Invalid Parameter to Vectorize from example
Hello! I just got started with Mojo, been playing with in for the past 2 days. I run into an a syntax error from just copying the matmul example.
@mojo
from algorithm.functional import vectorize

fn matmul_tiled(inout C: Matrix, A: Matrix, B: Matrix):
@parameter
fn calc_row(m: Int):
@parameter
fn calc_tile[tile_x: Int, tile_y: Int](x: Int, y: Int):
for k in range(y, y + tile_y):

@parameter
fn dot[nelts: Int](n: Int):
C.store(
m,
n + x,
C.load[nelts](m, n + x)
+ A[m, k] * B.load[nelts](k, n + x),
)

vectorize[dot, nelts, size=tile_x]()

tile[calc_tile, tile_n, tile_k](C.cols, B.rows)

parallelize[calc_row](C.rows, C.rows)
@mojo
from algorithm.functional import vectorize

fn matmul_tiled(inout C: Matrix, A: Matrix, B: Matrix):
@parameter
fn calc_row(m: Int):
@parameter
fn calc_tile[tile_x: Int, tile_y: Int](x: Int, y: Int):
for k in range(y, y + tile_y):

@parameter
fn dot[nelts: Int](n: Int):
C.store(
m,
n + x,
C.load[nelts](m, n + x)
+ A[m, k] * B.load[nelts](k, n + x),
)

vectorize[dot, nelts, size=tile_x]()

tile[calc_tile, tile_n, tile_k](C.cols, B.rows)

parallelize[calc_row](C.rows, C.rows)
On this line vectorize[dot, nelts, size=tile_x]() I get the error : Cannot pass 'fn[Int](n = Int) capturing -> None' value, parameter expected 'Int'mojo
4 replies