mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG] Unable to add numpy arrays

Open hylkedonker opened this issue 9 months ago • 1 comments

Bug description

The following code snippet, that adds two matrices, doesn't run:

from python import Python
 
def main():
    var np = Python.import_module("numpy")
    x = np.array([[1.0, 2.0]])
    y = np.array([[3.0, 4.0]])
    z = x + y

Running the file gives the error message:

/__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:136:4: error: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:136:4: note: call expansion failed - no concrete specializations /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:119:4: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:131:57: note: call expansion failed - no concrete specializations /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:91:4: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:107:18: note: call expansion failed - no concrete specializations /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:128:8: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/builtin/_startup.mojo:129:22: note: call expansion failed - no concrete specializations /home/user/workspace/mojo-tutorial/myfile.mojo:4:5: note: function instantiation failed def main(): ^ /home/user/workspace/mojo-tutorial/myfile.mojo:7:18: note: call expansion failed - no concrete specializations y = np.array([[3.0, 4.0]]) ^ /__w/modular/modular/Kernels/mojo/stdlib/python/object.mojo:212:8: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/python/object.mojo:251:44: note: call expansion failed - no concrete specializations /__w/modular/modular/Kernels/mojo/stdlib/utils/loop.mojo:30:4: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/utils/loop.mojo:41:33: note: call expansion failed - no concrete specializations /__w/modular/modular/Kernels/mojo/stdlib/utils/loop.mojo:60:4: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/utils/loop.mojo:67:18: note: call expansion failed - no concrete specializations /__w/modular/modular/Kernels/mojo/stdlib/python/object.mojo:225:12: note: function instantiation failed /__w/modular/modular/Kernels/mojo/stdlib/builtin/constrained.mojo:34:6: note: constraint failed: cannot convert nested list element to object /home/user/.modular/pkg/packages.modular.com_mojo/bin/mojo: error: failed to run the pass manager

Observe that vectors instead of matrices do work:

x = np.array([1.0, 2.0])
y = np.array([3.0, 4.0])
z = x + y

Steps to reproduce

To reproduce, save the snippet as myfile.mojo and run mojo myfile.mojo.

System information

- OS: Ubuntu 23.10
- `mojo -v`: mojo 24.3.0 (9882e19d)
- `modular -v`: modular 0.7.4 (df7a9e8b)

hylkedonker avatar May 11 '24 11:05 hylkedonker

I am having similar issues with concatenate and hstack

# Reshape xx and yy
xx_reshaped = xx.reshape(-1, 1)
yy_reshaped = yy.reshape(-1, 1)

# Concatenate xx and yy
#var reshaped_array = np.concatenate((xx_reshaped, yy_reshaped), axis=1)
var reshaped_array = np.hstack((xx_reshaped,yy_reshaped))

Summary of error


Expression [34] wrapper:73:26:       call expansion failed - no concrete specializations
  __mojo_repl_expr_body__()
                         ^

Expression [34] wrapper:36:7:         function instantiation failed
  def __mojo_repl_expr_body__() -> None:
      ^

Expression [34]:28:44:           call expansion failed - no concrete specializations
var reshaped_array = np.hstack((xx_reshaped,yy_reshaped))
                                           ^

expression failed to parse (no further compiler diagnostics)

Ultra-Code avatar May 14 '24 12:05 Ultra-Code

Hi @hylkedonker this isn't due to the addition, but the nested lists. This works:

from python import Python
def main():
    var np = Python.import_module("numpy")
    x = np.array([1.0, 2.0])
    y = np.array([3.0, 4.0])
    z = x + y

Closing in favor of: https://github.com/modularml/mojo/issues/1056

jackos avatar Jun 06 '24 22:06 jackos