Skip to content

Commit

Permalink
Updating the examples to include TensorFlow
Browse files Browse the repository at this point in the history
Both the AssemblyScript and Rust examples can now
utilize the TensorFlow backend. The build.sh script
now requires you specify which backend you want to
run with. Also adding performance measures.
  • Loading branch information
brianjjones committed Jan 1, 2022
1 parent bc63931 commit 3e131e6
Show file tree
Hide file tree
Showing 17 changed files with 2,213 additions and 156 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ WebAssembly, and run it in a WebAssembly runtime that supports the [wasi-nn] pro
### Examples

This repository includes examples of using these bindings. See the [Rust example] and
[AssemblyScript example] to walk through an end-to-end image classification using an AlexNet model. Currently the example uses OpenVino as the backend. If you are running Ubuntu, you can simply run the script to install the supported version`.github/actions/install-openvino/install.sh`. Otherwise you'll need to visit the [Installation Guides] and follow the instructions for your OS. The version of OpenVino currently supported is openvino_2020.4.287.
[AssemblyScript example] to walk through an end-to-end image classification using an MobileNet model. The examples use OpenVino or TensorFlow as the backend. If you are running Ubuntu, you can simply run the script to install the supported version`.github/actions/install-openvino/install.sh`. Otherwise you'll need to visit the [Installation Guides] and follow the instructions for your OS. The version of OpenVino currently supported is openvino_2020.4.287. For TensorFlow, visit [Install TensorFlow for C](https://www.tensorflow.org/install/lang_c)

Once you have OpenVino installed, run them with:
- `./build.sh rust` runs the [Rust example]
- `./build.sh as` runs the [AssemblyScript example]
- `./build.sh rust openvino` runs the [Rust example] with OpenVino
- `./build.sh rust tensorflow` runs the [Rust example] with TensorFlow
- `./build.sh as openvino` runs the [AssemblyScript example] with OpenVino
- `./build.sh as tensorflow` runs the [AssemblyScript example] with Tensorflow

If you want to see performance numbers, you can add `perf` to the end. For example, `./build.sh rust openvino perf`. __NOTE__: this currently only works for the Rust example.

[Rust example]: rust/examples/classification-example
[AssemblyScript example]: assemblyscript/examples/object-classification.ts
Expand Down
20 changes: 17 additions & 3 deletions assemblyscript/assembly/as-wasi-nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Graph {
/**
* Create a `Graph` from one or more binary blobs.
* @param builder the binary blobs that make up the graph
* @param encoding the framework required for
* @param encoding the framework required for
* @param target the device on which to run the graph
* @returns an initialized `Graph`
*/
Expand All @@ -36,7 +36,20 @@ export class Graph {
graphBuilder.push(builder[i].length);
}


// for (var i = 0; i < pathstr.length; ++i) {
// let code: u8 = pathstr.charCodeAt(i) as u8;

// bytes = bytes.concat([code]);

// // bytesv2 = bytesv2.concat([code & 0xff, code / 256 >>> 0]);
// }

// graphBuilder.push(getArrayPtr(bytes));
// graphBuilder.push(getArrayPtr(bytes));

let graphPointer: u32 = changetype<u32>(memory.data(4));
// let resultCode = wasi_ephemeral_nn.load(getArrayPtr(graphBuilder), builder.length, encoding, target, graphPointer);
let resultCode = wasi_ephemeral_nn.load(getArrayPtr(graphBuilder), builder.length, encoding, target, graphPointer);
if (resultCode != 0) {
throw new WasiNnError("Unable to load graph", resultCode);
Expand All @@ -46,7 +59,7 @@ export class Graph {

/**
* Create an execution context for performing inference requests. This indirection separates the
* "graph loading" phase (potentially expensive) from the "graph execution" phase.
* "graph loading" phase (potentially expensive) from the "graph execution" phase.
* @returns an `ExecutionContext`
*/
initExecutionContext(): ExecutionContext {
Expand All @@ -64,6 +77,7 @@ export class Graph {
*/
export const enum GraphEncoding {
openvino = 0,
tensorflow = 1,
}

/**
Expand Down Expand Up @@ -152,7 +166,7 @@ export class Tensor {

/**
* Convert data to an `ArrayBuffer` for using data views.
* @returns an ArrayBuffer with a copy of the bytes in `this.data`
* @returns an ArrayBuffer with a copy of the bytes in `this.data`
*/
toArrayBuffer(): ArrayBuffer {
const buffer = new ArrayBuffer(this.data.length);
Expand Down
5 changes: 3 additions & 2 deletions assemblyscript/demo.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Sets up the AssemblyScript demo for wasi-nn. Note you'll need to build and copy the wasmtime CLI
# Sets up the AssemblyScript demo for wasi-nn. Note you'll need to build and copy the wasmtime CLI
# and copy it to this directory for this script to work.

set -e
Expand All @@ -14,5 +14,6 @@ wget --no-clobber --directory-prefix=$DOWNLOAD_DIR $FIXTURE/mobilenet.xml
wget --no-clobber --directory-prefix=$DOWNLOAD_DIR $FIXTURE/tensor-1x224x224x3-f32.bgr
cp -rn images $DOWNLOAD_DIR

export MAPDIR="fixture"
# Run the demo
wasmtime run build/optimized.wasm --dir build --wasi-modules=experimental-wasi-nn
wasmtime run build/optimized.wasm --dir build --mapdir fixture::$DOWNLOAD_DIR --wasi-modules=experimental-wasi-nn
99 changes: 0 additions & 99 deletions assemblyscript/examples/object-classification.ts

This file was deleted.

Loading

0 comments on commit 3e131e6

Please sign in to comment.