forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wasi-nn: refactor to allow
preview2
access (bytecodealliance#6821)
* wasi-nn: refactor to allow `preview2` access This change refactors the `wasmtime-wasi-nn` crate to allow access from both `preview1` and `preview2` ABIs. Though the `wasi-nn` specification has included a WIT description for some time, here we use some in-tree files until WebAssembly/wasi-nn#38 is landed. The `preview2` code is not exercised anywhere yet: ideally this would be wired up once component model `resource`s are fully implemented in Wasmtime. prtest:full * wasi-nn: use `preview1` linkage prtest:full * review: rename `preview*` to `wit*` This is based on @pchickey's [comments] on ABI naming. [comments]: https://bytecodealliance.zulipchat.com/#narrow/stream/266558-wasi-nn/topic/wasi-nn.20.2B.20preview2/near/383368292 * review: update README * fix: remove broken doc links * fix: replace typo `wit` with `gen` * review: use `wit` types everywhere This removes the crate-specific types in order to use the WIT-generated types throughout the crate. The main effect of this is that the crate no longer optionally includes `wasmtime` with the `component-model` feature--now that is required. * review: move `BackendKind` conversion into `witx.rs` * review: remove `<'a>` * review: use `tracing` crate instead of `eprintln!`
- Loading branch information
Showing
15 changed files
with
524 additions
and
190 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
# wasmtime-wasi-nn | ||
|
||
This crate enables support for the [wasi-nn] API in Wasmtime. Currently it contains an implementation of [wasi-nn] using | ||
OpenVINO™ but in the future it could support multiple machine learning backends. Since the [wasi-nn] API is expected | ||
to be an optional feature of WASI, this crate is currently separate from the [wasi-common] crate. This crate is | ||
experimental and its API, functionality, and location could quickly change. | ||
This crate enables support for the [wasi-nn] API in Wasmtime. Currently it | ||
contains an implementation of [wasi-nn] using OpenVINO™ but in the future it | ||
could support multiple machine learning backends. Since the [wasi-nn] API is | ||
expected to be an optional feature of WASI, this crate is currently separate | ||
from the [wasi-common] crate. This crate is experimental and its API, | ||
functionality, and location could quickly change. | ||
|
||
[examples]: examples | ||
[openvino]: https://crates.io/crates/openvino | ||
[wasi-nn]: https://github.com/WebAssembly/wasi-nn | ||
[wasi-common]: ../wasi-common | ||
[bindings]: https://crates.io/crates/wasi-nn | ||
|
||
### Use | ||
|
||
Use the Wasmtime APIs to instantiate a Wasm module and link in the `WasiNn` implementation as follows: | ||
Use the Wasmtime APIs to instantiate a Wasm module and link in the `wasi-nn` | ||
implementation as follows: | ||
|
||
``` | ||
let wasi_nn = WasiNn::new(&store, WasiNnCtx::new()?); | ||
wasi_nn.add_to_linker(&mut linker)?; | ||
```rust | ||
let wasi_nn = WasiNnCtx::new()?; | ||
wasmtime_wasi_nn::witx::add_to_linker(...); | ||
``` | ||
|
||
### Build | ||
|
||
This crate should build as usual (i.e. `cargo build`) but note that using an existing installation of OpenVINO™, rather | ||
than building from source, will drastically improve the build times. See the [openvino] crate for more information | ||
```sh | ||
$ cargo build | ||
``` | ||
|
||
To use the WIT-based ABI, compile with `--features component-model` and use `wasmtime_wasi_nn::wit::add_to_linker`. | ||
|
||
### Example | ||
|
||
An end-to-end example demonstrating ML classification is included in [examples]: | ||
- `tests/wasi-nn-rust-bindings` contains ergonomic bindings for writing Rust code against the [wasi-nn] APIs | ||
- `tests/classification-example` contains a standalone Rust project that uses the [wasi-nn] APIs and is compiled to the | ||
`wasm32-wasi` target using the `wasi-nn-rust-bindings` | ||
`examples/classification-example` contains a standalone Rust project that uses | ||
the [wasi-nn] APIs and is compiled to the `wasm32-wasi` target using the | ||
high-level `wasi-nn` [bindings]. | ||
|
||
Run the example from the Wasmtime project directory: | ||
|
||
``` | ||
ci/run-wasi-nn-example.sh | ||
```sh | ||
$ ci/run-wasi-nn-example.sh | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.