second-state / wasmedge-wasi-helper

A rust crate to call the wasm-wasi initialization helper function. https://crates.io/crates/wasmedge-wasi-helper
0 stars 0 forks source link

fails to open file because of missing pre-opened file descriptor #1

Closed proohit closed 3 years ago

proohit commented 3 years ago

Hi, first of all thanks for your efforts on bringing wasi to ssvm. I know that this is in a very early stage, but maybe I can contribute by reporting my challenges while using ssvm-wasi-helper.

I have a very simple ML model, built and serialized with smartcore using serde and bincode. The idea is to serialize a pre-built model and load it in a WASM module for accessing and utilizing it.

Dumping and loading works fine when executing inside main with cargo run. On the other hand, when using it via wasm-bindgen and accessing it from nodejs, it panicks with following error:

Server running at http://0.0.0.0:3000/
thread '<unnamed>' panicked at 'Can not load model: Custom { kind: Other, error: "failed to find a pre-opened file descriptor through which \"iris_knn.model\" could be opened" }', src/lib.rs:30:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2021-04-27 16:14:39,896 ERROR [default] execution failed: unreachable, Code: 0x89
2021-04-27 16:14:39,896 ERROR [default]     In instruction: unreachable (0x00) , Bytecode offset: 0x00018cdf
2021-04-27 16:14:39,896 ERROR [default]     When executing function name: "load_model"
/home/direnc/workspace/nodejs-rust/smartcore-ssvm/pkg/ssvm_nodejs_starter_lib.js:28
    return vm.RunString('load_model');
              ^

Error: SSVM execution failed
    at module.exports.load_model (/home/direnc/workspace/nodejs-rust/smartcore-ssvm/pkg/ssvm_nodejs_starter_lib.js:28:15)
    at Server.<anonymous> (/home/direnc/workspace/nodejs-rust/smartcore-ssvm/node/app.js:15:13)
    at Server.emit (node:events:369:20)
    at parserOnIncoming (node:_http_server:936:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:129:17) {
  code: 'Error'
}

The ssvm binding files are in pkg, where I also put the model dump file. I tested the functionality of ssvm-wasi-helper with the example functions create_file and read_file, which are working correctly and output a hello.txt file inside pkg.

Here's the loading code:

const FILE_NAME: &str = "iris_knn.model";
use std::fs::File;
use ssvm_wasi_helper::ssvm_wasi_helper::_initialize;
use std::io::{Write, Read};
#[wasm_bindgen]
pub fn load_model() -> String {
    _initialize();
        let model: LinearRegression<f64, DenseMatrix<f64>> = {
            let mut buf: Vec<u8> = Vec::new();
            File::open(&FILE_NAME)
                .and_then(|mut f| f.read_to_end(&mut buf))
                .expect("Can not load model");
            bincode::deserialize(&buf).expect("Can not deserialize the model")
        };
        let data = DenseMatrix::from_array(1, 6, &[234.289, 235.6, 159.0, 107.608, 1947., 60.323]);

        let prediction = model.predict(&data).unwrap();
        return prediction[0].to_string();
}
├── ...
├── node
│   ├── app.js
│   └── test.js
├── pkg
│   ├── hello.txt
│   ├── iris_knn.model
│   ├── package.json
│   ├── README.md
│   ├── ssvm_nodejs_starter_bg.wasm
│   ├── ssvm_nodejs_starter.js
│   ├── ssvm_nodejs_starter_lib_bg.wasm
│   └── ssvm_nodejs_starter_lib.js
├── src
│   ├── lib.rs
│   └── main.rs

Additional info

[dependencies]
wasm-bindgen = "=0.2.61"
smartcore = {version = "0.2.0", default-features = false}
serde = "1.0.115"
bincode = "1.3.1"
ssvm-wasi-helper = "=0.1.0"

node v15.14.0 cargo 1.50.0 (f04e7fab7 2021-02-04) Ubuntu 20.04 LTS

hydai commented 3 years ago

Thanks for using our SSVM and WASI helper function in your project :-).

The Wasm execution is a sandbox, so users will need to map the path by theirselves. Hence, you can find that we map the __dirname to virtual / in our tutorial. And we always use /hello.txt in the javascript side.

Could you try to change this line from const FILE_NAME: &str = "iris_knn.model"; to const FILE_NAME: &str = "/iris_knn.model"; and check the result?

proohit commented 3 years ago

Hello and thanks for your quick response. I was pretty sure that I tested this out and that it didn't work, but I've just tried it and adding a / to the FILE_NAME constant seemed to have worked. Thanks! :)