modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.37k stars 2.6k forks source link

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

Open abdullahselek opened 1 year ago

abdullahselek commented 1 year 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 1 year 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 10 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 10 months ago

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

jjvraw commented 2 months ago

FYI, this was opened almost a year ago now... But thought I'd mention, for what it's worth, the following is possible:

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

Nested lists currently are not supported, and related to this:

https://github.com/modularml/mojo/blob/73c1a40785f4a491da5a49d6045510f1fb343527/stdlib/src/python/python_object.mojo#L432-L435