modularml / mojo

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

[BUG] Program Crash on generating simple data. #3082

Open mytechnotalent opened 4 months ago

mytechnotalent commented 4 months ago

Bug description

main

fn main() raises:
    try:
        # generate simple dataset
        var simple_data = generate_simple_data(100)
        var X = simple_data [0]
        var y = simple_data [1]
    except e:
        print("Error during execution:", e)
        raise e

calling function

fn generate_simple_data(num_samples: Int) -> (PythonObject, PythonObject):
   try:
       var numpy = Python.import_module("numpy")
       var torch = Python.import_module("torch")

       # Generate random X and y
       var X = numpy.random.rand(num_samples, 2).astype(numpy.float32)  # Random 2D points
       var y = numpy.random.randint(0, 2, num_samples, dtype=numpy.int64)  # Random integer labels (0 or 1)

       var X_tensor = torch.tensor(X)
       var y_tensor = torch.tensor(y)

       return (X_tensor, y_tensor)

   except e:
       print("Error generating simple data:", e)
       var the_number: Int = 0
       var the_py_object = PythonObject(the_number)
       return the_py_object, the_py_object

Steps to reproduce

mojo main.mojo

Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo main.mojo
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 mojo 0x0000000100e28c24 llvm_strlcpy + 51480
1 mojo 0x0000000100e26f10 llvm_strlcpy + 44036
2 mojo 0x0000000100e292c4 llvm_strlcpy + 53176
3 libsystem_platform.dylib 0x000000018fc5f584 _sigtramp + 56
4 Python 0x000000010acafa30 PyObject_ClearWeakRefs + 428
5 libtorch_python.dylib 0x000000010ccad8ec THPVariable_subclass_dealloc(_object*) + 116
6 libtorch_python.dylib 0x0000000300078110 THPVariable_subclass_dealloc(_object*) + 8375806104
7 mojo 0x00000001011bd530 __jit_debug_register_code + 1041480
8 mojo 0x0000000100d8956c
9 mojo 0x0000000100d88f60
10 mojo 0x0000000100d71960
11 dyld 0x000000018f8a60e0 start + 2360
mojo crashed!
Please file a bug report.
[43289:1154932:20240619,181329.323794:WARNING in_range_cast.h:38] value -97304226 out of range
[43289:1154932:20240619,181329.327035:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5)
zsh: segmentation fault mojo main.mojo```

### System information

```shell
- What OS did you do install Mojo on MAC M3
- Provide version information for Mojo by pasting the output of `mojo 24.4.0 (2cb57382)`
- Provide Modular CLI version by pasting the output of `modular 0.8.0 (39a426b5)`
soraros commented 4 months ago

Smaller repro:

from python import Python, PythonObject

def f() -> Tuple[PythonObject]:
    var np = Python.import_module("numpy")
    return np.array([0])

def main():
    var t = f()
    var y = t[0]
    print(y)

I doubt if NumPy is relevant, however, I wasn't able reduce it away.