-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
33 changed files
with
1,554 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# install-openvino | ||
|
||
A GitHub action to install OpenVINO from a package repository. This is only necessary for `wasi-nn` support but there | ||
are enough steps here to package the functionality separately and avoid cluttering the CI. | ||
|
||
Future improvements: | ||
- make this installer work for different OS/distributions (e.g. https://docs.openvinotoolkit.org/latest/openvino_docs_install_guides_installing_openvino_windows.html) | ||
- it would be nice to output the install directory (i.e. `/opt/intel/openvino`) |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
name: 'Install OpenVINO' | ||
description: 'Install OpenVINO binaries from a package repository; this is significantly faster than building from source' | ||
|
||
inputs: | ||
version: | ||
description: 'The release version of OpenVINO to install' | ||
required: false | ||
default: '2020.4.287' | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- run: ${{ github.action_path }}/install.sh ${{ inputs.version }} | ||
shell: bash |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
# Retrieve OpenVINO checksum. | ||
wget https://apt.repos.intel.com/openvino/2020/GPG-PUB-KEY-INTEL-OPENVINO-2020 | ||
echo '5f5cff8a2d26ba7de91942bd0540fa4d GPG-PUB-KEY-INTEL-OPENVINO-2020' > CHECKSUM | ||
md5sum --check CHECKSUM | ||
|
||
# Add OpenVINO repository (deb). | ||
sudo apt-key add GPG-PUB-KEY-INTEL-OPENVINO-2020 | ||
echo "deb https://apt.repos.intel.com/openvino/2020 all main" | sudo tee /etc/apt/sources.list.d/intel-openvino-2020.list | ||
sudo apt update | ||
|
||
# Install OpenVINO package. | ||
sudo apt install -y intel-openvino-runtime-ubuntu18-$1 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
|
||
# The following script demonstrates how to execute a machine learning inference using the wasi-nn module optionally | ||
# compiled into Wasmtime. Calling it will download the necessary model and tensor files stored separately in $FIXTURE | ||
# into $TMP_DIR (optionally pass a directory with existing files as the first argument to re-try the script). Then, | ||
# it will compile the example code in crates/wasi-nn/tests/example into a Wasm file that is subsequently | ||
# executed with the Wasmtime CLI. | ||
set -e | ||
WASMTIME_DIR=$(dirname "$0" | xargs dirname) | ||
FIXTURE=https://gist.github.com/abrown/c7847bf3701f9efbb2070da1878542c1/raw/07a9f163994b0ff8f0d7c5a5c9645ec3d8b24024 | ||
|
||
# Inform the environment of OpenVINO library locations. Then we use OPENVINO_INSTALL_DIR below to avoid building all of | ||
# OpenVINO from source (quite slow). | ||
source /opt/intel/openvino/bin/setupvars.sh | ||
|
||
# Build Wasmtime with wasi-nn enabled; we attempt this first to avoid extra work if the build fails. | ||
OPENVINO_INSTALL_DIR=/opt/intel/openvino cargo build -p wasmtime-cli --features wasi-nn | ||
|
||
# Download all necessary test fixtures to the temporary directory. | ||
TMP_DIR=${1:-$(mktemp -d -t ci-XXXXXXXXXX)} | ||
wget --no-clobber --directory-prefix=$TMP_DIR $FIXTURE/frozen_inference_graph.bin | ||
wget --no-clobber --directory-prefix=$TMP_DIR $FIXTURE/frozen_inference_graph.xml | ||
wget --no-clobber --directory-prefix=$TMP_DIR $FIXTURE/tensor-1x3x300x300-f32.bgr | ||
|
||
# Now build an example that uses the wasi-nn API. | ||
pushd $WASMTIME_DIR/crates/wasi-nn/examples/classification-example | ||
cargo build --release --target=wasm32-wasi | ||
cp target/wasm32-wasi/release/wasi-nn-example.wasm $TMP_DIR | ||
popd | ||
|
||
# Run the example in Wasmtime (note that the example uses `fixture` as the expected location of the model/tensor files). | ||
OPENVINO_INSTALL_DIR=/opt/intel/openvino cargo run --features wasi-nn -- run --mapdir fixture::$TMP_DIR $TMP_DIR/wasi-nn-example.wasm | ||
|
||
# Clean up. | ||
rm -rf $TMP_DIR |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "wasmtime-wasi-nn" | ||
version = "0.21.0" | ||
authors = ["The Wasmtime Project Developers"] | ||
description = "Wasmtime implementation of the wasi-nn API" | ||
documentation = "https://docs.rs/wasmtime-wasi-nn" | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
categories = ["wasm", "computer-vision"] | ||
keywords = ["webassembly", "wasm", "neural network"] | ||
repository = "https://github.com/bytecodealliance/wasmtime" | ||
readme = "README.md" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
# These dependencies are necessary for the witx-generation macros to work: | ||
anyhow = "1.0" | ||
log = { version = "0.4", default-features = false } | ||
wasmtime = { path = "../wasmtime", version = "0.21.0", default-features = false } | ||
wasmtime-runtime = { path = "../runtime", version = "0.21.0" } | ||
wasmtime-wiggle = { path = "../wiggle/wasmtime", version = "0.21.0" } | ||
wasmtime-wasi = { path = "../wasi", version = "0.21.0" } | ||
wiggle = { path = "../wiggle", version = "0.21.0" } | ||
|
||
# These dependencies are necessary for the wasi-nn implementation: | ||
openvino = "0.1.5" | ||
thiserror = "1.0" | ||
|
||
[badges] | ||
maintenance = { status = "experimental" } |
Oops, something went wrong.