wasmerio / py2wasm

Nuitka is a Python compiler written in Python. It's fully compatible with Python 2.6, 2.7, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, and 3.11. You feed it your Python app, it does a lot of clever things, and spits out an executable or extension module.
http://nuitka.net
Apache License 2.0
69 stars 3 forks source link

ModuleNotFoundError: No module named 'torch' #2

Open su3 opened 2 months ago

su3 commented 2 months ago

I wrote a simple Python program for testing. After execution, I got:

wasmer ran.wasm 

Traceback (most recent call last):
  File "./ran.py", line 1, in <module>
ModuleNotFoundError: No module named 'torch'

The code is as follows:

import torch

def main():
    n = torch.rand(3, 4)
    print("Hello", n)

if __name__ == "__main__":
    main()

Here's the compilation process:

py2wasm ran.py -o ran.wasm

py2wasm: Using wasi-sdk from /Users/user/miniconda3/lib/python3.11/site-packages/nuitka/wasi-sdk/21/sdk-Darwin
Nuitka-Options: Used command line options: ran.py --standalone --static-libpython=yes --disable-ccache --lto=yes
Nuitka-Options: --output-dir=/Users/user/miniconda3/lib/python3.11/site-packages/nuitka/__py2wasm --output-filename=output.wasm --remove-output
Nuitka: Starting Python compilation with Nuitka '2.6.3' on Python '3.11' commercial grade 'not installed'.
Nuitka: Completed Python level compilation and optimization.                             
Nuitka: Generating source code for C backend compiler.
Nuitka: Running data composer tool for optimal constant value handling.
Nuitka: Running C compilation via Scons.
Nuitka-Scons: Backend C compiler: ~/miniconda3/lib/python3.11/site-packages/nuitka/wasi-sdk/21/sdk-Darwin/bin/clang (clang 17.0.6).
Nuitka-Scons: Backend linking program with 7 files (no progress information available for this stage).
clang: warning: argument unused during compilation: '-headerpad_max_install_names' [-Wunused-command-line-argument]
Nuitka: Removing build directory '/Users/user/miniconda3/lib/python3.11/site-packages/nuitka/__py2wasm/ran.build'.
Nuitka: Created binary that runs on macOS 11.1 (arm64) or higher.
Nuitka: Successfully created '/Users/user/miniconda3/lib/python3.11/site-packages/nuitka/__py2wasm/ran.dist/output.wasm'.
py2wasm: File created successfully: ran.wasm

It works fine when compiled with Nuitka.

python -m nuitka ran.py

 ./ran.bin
Hello tensor([[8.8891e-02, 1.0744e-02, 1.4557e-01, 2.1542e-01],
        [5.2137e-01, 6.6914e-03, 8.2231e-04, 8.7954e-01],
        [1.8713e-01, 3.6060e-02, 4.5850e-01, 3.1474e-01]])

The current environment is:

python -m nuitka --version

2.2.1
Commercial: None
Python: 3.11.5 (main, Sep 11 2023, 08:31:25) [Clang 14.0.6 ]
Flavor: Anaconda Python
Executable: /Users/user/Code/env/venv/bin/python
OS: Darwin
Arch: arm64
macOSRelease: 14.4.1
Version C compiler: /usr/bin/clang (clang 15.0.0).

Not sure what went wrong?

artsiommiksiuk commented 1 month ago

I'm having the same issues, just different imported modules can't be found.

artsiommiksiuk commented 1 month ago

Okay, I think I found an issue. If you have some dependencies make sure you running compilation from the same env. In my case, I had venv in one place, but because of requirement to run py2wasm from python 11 I decided to separately call it outside venv. And that resulted that none of the dependencies were found. After I resetup whole venv with python 11 and launched compilation from this venv (with all installed dependencies) compilation now found a lot of files

Nuitka-Scons: Backend linking program with 1842 files (no progress information available for this stage)., instead of just a few.

@su3 So, make sure you running everything from the same env. Because in your log it says only 7 files found for linking, which I think is a mistake (I believe pytorch has more than 7 c level dependencies).

su3 commented 1 month ago

@su3 So, make sure you running everything from the same env. Because in your log it says only 7 files found for linking, which I think is a mistake (I believe pytorch has more than 7 c level dependencies).

I confirm that I tested and compiled in the same environment. Running the Python script directly works without any issues.

syrusakbary commented 1 month ago

Hey @su3 , running torch (and any other native python modules that rare using C) in WASI is not yet supported.

We are working on adding them as part of WASIX, and doin that sholud solve this issue