modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo
Other
22.3k stars 2.55k forks source link

[BUG]: Issue on creating numpy N-dimensional array #1139

Open abdullahselek opened 8 months ago

abdullahselek commented 8 months ago

Bug description

Can't create multi dimensional numpy array, creating single dimension numpy array works well.

Steps to reproduce

from python import Python

let np = Python.import_module("numpy")
let np_array = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

Same behaviour both on playground and VSCode. It throws "call expansion failed - no concrete specializations" and "expression failed to parse (no further compiler diagnostics)".

System information

- What OS did you do install Mojo on ?

macOS Sonoma 14.0

- Provide version information for Mojo by pasting the output of `mojo -v`

mojo 0.4.0 (9e33b013)

- Provide Modular CLI version by pasting the output of `modular -v`

modular 0.2.1 (5144fffe)
zachgrayio commented 8 months ago

👍 yeah I have hit many similar issues when trying a bit more realistic python interop; is this a known issue? is there any ETA or tracking task at all we might be able to follow to know when to expect to be able to call out to python and a few core libs like numpy reliably?

jackos commented 6 months ago

Hi @zachgrayio and @abdullahselek passing multidimensional arrays to Python isn't supported yet, we can use this ticket to track it.

@arthurevans @scottamain potentially add this to the roadmap sharp edges?

There is a workaround until this is resolved:

np_helper.py:

import ast

import numpy as np

def array(arrays: str, dtype):
    _, dtype = dtype.split(".")
    return np.array(ast.literal_eval(arrays), getattr(np, dtype))

main.mojo:

from python import Python

fn main() raises:
    Python.add_to_path(".")
    let np = Python.import_module("np_helper")
    let array = np.array("[[1, 2, 3], [4, 5, 6]]", "np.int32")
    print(array)
jackos commented 6 months ago

fyi @stumpOS I couldn't find an existing ticket for this