Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross-compiling to wasm32-wasi #1522

Closed
cymqqqq opened this issue Mar 26, 2021 · 7 comments
Closed

cross-compiling to wasm32-wasi #1522

cymqqqq opened this issue Mar 26, 2021 · 7 comments
Labels

Comments

@cymqqqq
Copy link

cymqqqq commented Mar 26, 2021

🐛 Bug Reports

When reporting a bug, please provide the following information. If this is not a bug report you can just discard this template.

🌍 Environment

  • Your operating system and version:
  • Your python version:
  • How did you install python (e.g. apt or pyenv)? Did you use a virtualenv?:
  • Your Rust version (rustc --version):
  • Your PyO3 version:
  • Have you tried using latest PyO3 main (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?:

💥 Reproducing

Please provide a minimal working example. This means both the Rust code and the Python.

Please also write what exact flags are required to reproduce your results.
ubuntu 20.04
py3.7
install python by apt. Did you use a virtualenv? No.
rust version 1.42
pyo3 version 0.13.2
yes, tried git = "https://github.com/PyO3/pyo3" when I build my demo code, an error comes: error: failed to run custom build command for pyo3 v0.13.2`

Caused by:
process didn't exit successfully: /home/azureuser/rustpy/target/release/build/pyo3-c4d90697e291dc3a/build-script-build (exit code: 1)
--- stderr
Error: NotPresent
warning: build failed, waiting for other jobs to finish...
error: build failed
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute cargo build: exited with exit code: 101
full command: "cargo" "build" "--all-targets" "--release" "--target" "wasm32-wasi"

@davidhewitt
Copy link
Member

Hi @cymqqqq can you please share your demo code?

@cymqqqq
Copy link
Author

cymqqqq commented Mar 26, 2021

ok, here is my demo code:
use pyo3::prelude::;
use pyo3::{prelude::
, types::{IntoPyDict, PyModule}};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
Python::with_gil(|py| {
let activators = PyModule::from_code(py, r#"
def relu(x):
"""see https://en.wikipedia.org/wiki/Rectifier_(neural_networks)"""
return max(0.0, x)

            def leaky_relu(x, slope=0.01):
                return x if x >= 0 else x * slope
                    "#, "activators.py", "activators")?;

                    let relu_result: f64 = activators.getattr("relu")?.call1((-1.0,))?.extract()?;
                        assert_eq!(relu_result, 0.0);

                            let kwargs = [("slope", 0.2)].into_py_dict(py);
                                let lrelu_result: f64 = activators
                                            .getattr("leaky_relu")?.call((-1.0,), Some(kwargs))?
                                                    .extract()?;
                                    assert_eq!(lrelu_result, -0.2);

})

@cymqqqq
Copy link
Author

cymqqqq commented Mar 26, 2021

just copy the code from https://pyo3.rs/main/python_from_rust.html

@davidhewitt
Copy link
Member

@cymqqqq so what's going on here is that you're trying to cross-compile to the wasm target. To do this successfully you must direct PyO3 to the lib directory of a wasm Python build, so that PyO3 can read its information to configure the build.

However I am unsure whether building a wasm Python extension is possible. There is some prior discussion at #1221 .

The bug Error: NotPresent seems to have been related to some broken code which was recently removed in #1521. (Thanks @ravenexp!) So if you use the current main branch you should get more helpful error messages.

@davidhewitt davidhewitt changed the title error: failed to run custom build command for pyo3 v0.13.2 cross-compiling to wasm32-wasi Mar 28, 2021
@cymqqqq
Copy link
Author

cymqqqq commented Mar 29, 2021

OK. thanks for answering:-)

@abbec
Copy link

abbec commented May 14, 2021

We also have some small fixes in #1605 that at least makes it work to cross-compile pyo3 to WASI for our case (given that you have already cross-compiled Python itself).

@davidhewitt
Copy link
Member

As we now sort of support wasm32-wasi (we check the crate has no compilation errors, though we don't actually try to test with wasm32 Python, I'm going to close this issue.

If anyone with knowledge of how to build a Python interpreter for wasm wants to contribute a full CI job, that would be cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants