Add an initial wasi-nn implementation for Wasmtime (#2208)
* Add an initial wasi-nn implementation for Wasmtime This change adds a crate, `wasmtime-wasi-nn`, that uses `wiggle` to expose the current state of the wasi-nn API and `openvino` to implement the exposed functions. It includes an end-to-end test demonstrating how to do classification using wasi-nn: - `crates/wasi-nn/tests/classification-example` contains Rust code that is compiled to the `wasm32-wasi` target and run with a Wasmtime embedding that exposes the wasi-nn calls - the example uses Rust bindings for wasi-nn contained in `crates/wasi-nn/tests/wasi-nn-rust-bindings`; this crate contains code generated by `witx-bindgen` and eventually should be its own standalone crate * Test wasi-nn as a CI step This change adds: - a GitHub action for installing OpenVINO - a script, `ci/run-wasi-nn-example.sh`, to run the classification example
This commit is contained in:
65
crates/wasi-nn/examples/wasi-nn-rust-bindings/README.md
Normal file
65
crates/wasi-nn/examples/wasi-nn-rust-bindings/README.md
Normal file
@@ -0,0 +1,65 @@
|
||||
wasi-nn Rust Bindings
|
||||
=====================
|
||||
|
||||
This crate contains API bindings for [wasi-nn] system calls in Rust. It is similar in purpose to the [wasi bindings] but
|
||||
this crate provides access to the optional neural network functionality from WebAssembly.
|
||||
|
||||
[wasi-nn]: https://github.com/WebAssembly/wasi-nn
|
||||
[wasi bindings]: https://github.com/bytecodealliance/wasi
|
||||
|
||||
> __NOTE__: These bindings are experimental (use at your own risk) and subject to upstream changes in the wasi-nn
|
||||
> specification.
|
||||
|
||||
> __NOTE__: In the future this crate may be (should be) moved to its own repository, like the [wasi bindings].
|
||||
|
||||
### Use
|
||||
|
||||
Depend on this crate in your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
wasi-nn = "0.1.0"
|
||||
```
|
||||
|
||||
Use the wasi-nn APIs in your application:
|
||||
|
||||
```rust
|
||||
use wasi_nn;
|
||||
|
||||
unsafe {
|
||||
wasi_nn::load(
|
||||
&[&xml.into_bytes(), &weights],
|
||||
wasi_nn::GRAPH_ENCODING_OPENVINO,
|
||||
wasi_nn::EXECUTION_TARGET_CPU,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
```
|
||||
|
||||
Compile the application to WebAssembly:
|
||||
|
||||
```shell script
|
||||
cargo build --target=wasm32-wasi
|
||||
```
|
||||
|
||||
Run the generated Wasm in a runtime supporting wasi-nn. Currently Wasmtime has experimental support using the Wasmtime
|
||||
APIs; see [main.rs](../main.rs) for an example of how this is accomplished.
|
||||
|
||||
### Generation
|
||||
|
||||
This crate contains code ([`src/generated.rs`](src/generated.rs)) generated by
|
||||
[`witx-bindgen`](https://github.com/bytecodealliance/wasi/tree/main/crates/witx-bindgen).
|
||||
To regenerate this code, run `witx-bindgen` against the [`wasi-nn` WITX file](https://github.com/WebAssembly/wasi-nn/blob/master/phases/ephemeral/witx/wasi_ephemeral_nn.witx):
|
||||
|
||||
```shell script
|
||||
.../crates/witx-bindgen$ cargo run .../wasi-nn/phases/ephemeral/witx/wasi_ephemeral_nn.witx
|
||||
```
|
||||
|
||||
### License
|
||||
|
||||
This project is licensed under the Apache 2.0 license. See [LICENSE](LICENSE) for more details.
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you,
|
||||
as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.
|
||||
Reference in New Issue
Block a user