Skip to content

Commit

Permalink
add an initial implemenation for onnxruntime backend of wasi-nn
Browse files Browse the repository at this point in the history
Signed-off-by: David Justice <david@devigned.com>
  • Loading branch information
devigned committed Dec 15, 2023
1 parent f9f8a4d commit 7201935
Show file tree
Hide file tree
Showing 14 changed files with 1,874 additions and 18 deletions.
145 changes: 142 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions ci/run-wasi-nn-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,37 @@ else
REMOVE_TMP_DIR=0
fi

# Set up the environment for OpenVINO on OSX. This is necessary because on OSX, DYLD_LIBRARY_PATH will be ignored when
# running this file as a script. See: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_security_cs_allow-dyld-environment-variables?language=objc
#
# For example, if OpenVINO is installed in `~/`, then you would execute this test like the following:
# OSX_OPENVINO_LIB_PATH="/Users/${USER}/openvino_2022.3.1/runtime/lib/arm64/Release" ./ci/run-wasi-nn-example.sh
#
# Note: I found that installing OpenVINO with Homebrew wasn't effective as the directory structure doesn't match the
# implementation of the library searching in the openvino-rs crates (https://docs.rs/openvino-finder/latest/openvino_finder/).
# $ tree $(brew --prefix openvino) -d -L 5
# /opt/homebrew/opt/openvino
# ├── bin
# ├── ...
# └── lib
# ├── cmake
# │ ├── inferenceengine2023.2.0
# │ ├── ngraph2023.2.0
# │ └── openvino2023.2.0
# ├── openvino-2023.2.0
# ├── pkgconfig
# └── python3.11
# └── ...
export DYLD_LIBRARY_PATH="${OSX_OPENVINO_LIB_PATH}"

# One of the examples expects to be in a specifically-named directory.
mkdir -p $TMP_DIR/mobilenet
TMP_DIR=$TMP_DIR/mobilenet

# Build Wasmtime with wasi-nn enabled; we attempt this first to avoid extra work
cargo install --git https://github.com/bytecodealliance/cargo-component --locked cargo-component
# Build Wasmtime with wasi-nn and component-model enabled; we attempt this first to avoid extra work
# if the build fails.
cargo build -p wasmtime-cli --features wasi-nn
cargo build -p wasmtime-cli --features wasi-nn,component-model

# Download all necessary test fixtures to the temporary directory.
wget --no-clobber $FIXTURE/mobilenet.bin --output-document=$TMP_DIR/model.bin
Expand All @@ -50,6 +74,15 @@ popd
cargo run -- run --dir $TMP_DIR::fixture -S nn,nn-graph=openvino::$TMP_DIR \
$TMP_DIR/wasi-nn-example-named.wasm

# Build and run an ONNX component example doing classification.
cargo build -p wasi-preview1-component-adapter --target wasm32-unknown-unknown --release
pushd $WASMTIME_DIR/crates/wasi-nn/examples/classification-component-onnx
cargo component build --release
cp target/wasm32-wasi/release/classification_component_onnx.wasm $TMP_DIR
cp -rf ./fixture $TMP_DIR/
popd
cargo run --features wasi-nn,component-model -- run --wasm component-model --dir $TMP_DIR/fixture::fixture -S nn $TMP_DIR/classification_component_onnx.wasm

# Clean up the temporary directory only if it was not specified (users may want
# to keep the directory around).
if [[ $REMOVE_TMP_DIR -eq 1 ]]; then
Expand Down
6 changes: 5 additions & 1 deletion crates/wasi-nn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ wasmtime = { workspace = true, features = ["component-model"] }

# These dependencies are necessary for the wasi-nn implementation:
tracing = { workspace = true }
openvino = { version = "0.5.0", features = ["runtime-linking"] }
thiserror = { workspace = true }
openvino = { version = "0.5.0", features = ["runtime-linking"] }
ort = { version = "2.0.0-alpha.2" }
lazy_static = { version = "1.4" }
bytes = { version = "1.5" }
ndarray = { version = "0.15" }

[build-dependencies]
walkdir = { workspace = true }

0 comments on commit 7201935

Please sign in to comment.