-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wasi-nn: add named models #6854
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
74 changes: 74 additions & 0 deletions
74
crates/wasi-nn/examples/classification-example-named/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
crates/wasi-nn/examples/classification-example-named/Cargo.toml
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,15 @@ | ||
[package] | ||
name = "wasi-nn-example-named" | ||
version = "0.0.0" | ||
authors = ["The Wasmtime Project Developers"] | ||
readme = "README.md" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
wasi-nn = "0.5.0" | ||
|
||
# This crate is built with the wasm32-wasi target, so it's separate | ||
# from the main Wasmtime build, so use this directive to exclude it | ||
# from the parent directory's workspace. | ||
[workspace] |
2 changes: 2 additions & 0 deletions
2
crates/wasi-nn/examples/classification-example-named/README.md
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,2 @@ | ||
This example project demonstrates using the `wasi-nn` API to perform ML inference. It consists of Rust code that is | ||
built using the `wasm32-wasi` target. See `ci/run-wasi-nn-example.sh` for how this is used. |
53 changes: 53 additions & 0 deletions
53
crates/wasi-nn/examples/classification-example-named/src/main.rs
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,53 @@ | ||
use std::fs; | ||
use wasi_nn::*; | ||
|
||
pub fn main() { | ||
let graph = GraphBuilder::new(GraphEncoding::Openvino, ExecutionTarget::CPU) | ||
.build_from_cache("mobilenet") | ||
.unwrap(); | ||
println!("Loaded a graph: {:?}", graph); | ||
|
||
let mut context = graph.init_execution_context().unwrap(); | ||
println!("Created an execution context: {:?}", context); | ||
|
||
// Load a tensor that precisely matches the graph input tensor (see | ||
// `fixture/frozen_inference_graph.xml`). | ||
let tensor_data = fs::read("fixture/tensor.bgr").unwrap(); | ||
println!("Read input tensor, size in bytes: {}", tensor_data.len()); | ||
context | ||
.set_input(0, TensorType::F32, &[1, 3, 224, 224], &tensor_data) | ||
.unwrap(); | ||
|
||
// Execute the inference. | ||
context.compute().unwrap(); | ||
println!("Executed graph inference"); | ||
|
||
// Retrieve the output. | ||
let mut output_buffer = vec![0f32; 1001]; | ||
context.get_output(0, &mut output_buffer[..]).unwrap(); | ||
|
||
println!( | ||
"Found results, sorted top 5: {:?}", | ||
&sort_results(&output_buffer)[..5] | ||
) | ||
} | ||
|
||
// Sort the buffer of probabilities. The graph places the match probability for each class at the | ||
// index for that class (e.g. the probability of class 42 is placed at buffer[42]). Here we convert | ||
// to a wrapping InferenceResult and sort the results. It is unclear why the MobileNet output | ||
// indices are "off by one" but the `.skip(1)` below seems necessary to get results that make sense | ||
// (e.g. 763 = "revolver" vs 762 = "restaurant") | ||
fn sort_results(buffer: &[f32]) -> Vec<InferenceResult> { | ||
let mut results: Vec<InferenceResult> = buffer | ||
.iter() | ||
.skip(1) | ||
.enumerate() | ||
.map(|(c, p)| InferenceResult(c, *p)) | ||
.collect(); | ||
results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap()); | ||
results | ||
} | ||
|
||
// A wrapper for class ID and match probabilities. | ||
#[derive(Debug, PartialEq)] | ||
struct InferenceResult(usize, f32); |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite happy with using this script for testing. It was good initially to get things working but I would prefer to be using
cargo test
instead. The problem is that there is a dependency expectation: this can only run if the system has OpenVINO installed, which will not always be the case for developers runningcargo test
.I can think of two workarounds:
#[ignore]
the OpenVINO-specific tests and add some comments telling developers to turn them on to fully test the systemFORCE_RUN_OPENVINO_TESTS
env variable is necessary?Interested in some feedback on whether one of those approaches is better than this script!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I like your second suggestion and matches what I was going to suggest as well. We do run a higher risk of not actually running the tests in CI but I think running something by default as part of
cargo test --workspace
is a good mitigating factor for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the tests are for wasi-nn, we should just have a test backend that does something basic like an affine layer.
If this is testing integration we should think about how we handle multiple backends. This same issue will come up with pytorch, onnxruntime, tf, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexcrichton, I have a whole other set of commits ready for switching to that kind of testing. Just waiting on a review of this one because the new testing also covers the
load_by_name
logic added here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, check out #6895 if you're interested in where I went with this.