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:
40
crates/wasi-nn/src/witx.rs
Normal file
40
crates/wasi-nn/src/witx.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
//! Contains the macro-generated implementation of wasi-nn from the its witx definition file.
|
||||
use crate::ctx::WasiNnCtx;
|
||||
use crate::ctx::WasiNnError;
|
||||
|
||||
// Generate the traits and types of wasi-nn in several Rust modules (e.g. `types`).
|
||||
wiggle::from_witx!({
|
||||
witx: ["$WASI_ROOT/phases/ephemeral/witx/wasi_ephemeral_nn.witx"],
|
||||
ctx: WasiNnCtx,
|
||||
errors: { errno => WasiNnError }
|
||||
});
|
||||
|
||||
use types::Errno;
|
||||
|
||||
/// Wiggle generates code that performs some input validation on the arguments passed in by users of
|
||||
/// wasi-nn. Here we convert the validation error into one (or more, eventually) of the error
|
||||
/// variants defined in the witx.
|
||||
impl types::GuestErrorConversion for WasiNnCtx {
|
||||
fn into_errno(&self, e: wiggle::GuestError) -> Errno {
|
||||
eprintln!("Guest error: {:?}", e);
|
||||
Errno::InvalidArgument
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> types::UserErrorConversion for WasiNnCtx {
|
||||
fn errno_from_wasi_nn_error(&self, e: WasiNnError) -> Errno {
|
||||
eprintln!("Host error: {:?}", e);
|
||||
match e {
|
||||
WasiNnError::OpenvinoError(_) => unimplemented!(),
|
||||
WasiNnError::GuestError(_) => unimplemented!(),
|
||||
WasiNnError::UsageError(_) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Additionally, we must let Wiggle know which of our error codes represents a successful operation.
|
||||
impl wiggle::GuestErrorType for Errno {
|
||||
fn success() -> Self {
|
||||
Self::Success
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user