-
Notifications
You must be signed in to change notification settings - Fork 1.7k
WinML backend for wasi-nn #7807
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
200d716
Add WinML backend for wasi-nn.
jianjunz 2624feb
Log execution time.
jianjunz 31a5f6d
WinML backend supports execution target selection.
jianjunz e3d4f45
Limit WinML backend on Windows only.
jianjunz ca7ef3a
Move wasi-nn WinML example to new test infra.
jianjunz bab7508
Scale tensor data in test app.
jianjunz 1d6611a
Remove old example for wasi-nn WinML backend.
jianjunz 26119ad
Update image2tensor link.
jianjunz d6b6294
Format code.
jianjunz 8a7297b
Upgrade image2tensor to 0.3.1.
jianjunz 6a2c813
Upgrade windows to 0.52.0
abrown 2447e7a
Use tensor data as input for wasi-nn WinML backend test.
jianjunz 18166fe
Restore trailing new line for Cargo.toml.
jianjunz c4f7ce2
Remove unnecessary features for windows crate.
jianjunz 0e31cef
Check input tensor types.
jianjunz 99c33c0
Rename default model name to model.onnx.
jianjunz ca792aa
Run nn_image_classification_winml only when winml is enabled.
jianjunz 0dd6e06
vet: add trusted `windows` crate to lockfile
abrown 64ddce9
Fix wasi-nn tests when both openvino and winml are enabled.
jianjunz 2de787c
Add check for WinML availability.
jianjunz 4cd42ed
vet: reapply vet lock
abrown 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
crates/test-programs/src/bin/nn_image_classification_winml.rs
This file contains hidden or 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,58 @@ | ||
| use anyhow::Result; | ||
| use std::fs; | ||
| use std::time::Instant; | ||
| use wasi_nn::*; | ||
|
|
||
| pub fn main() -> Result<()> { | ||
| // Graph is supposed to be preloaded by `nn-graph` argument. The path ends with "mobilenet". | ||
| let graph = | ||
| wasi_nn::GraphBuilder::new(wasi_nn::GraphEncoding::Onnx, wasi_nn::ExecutionTarget::CPU) | ||
| .build_from_cache("mobilenet") | ||
| .unwrap(); | ||
|
|
||
| let mut context = graph.init_execution_context().unwrap(); | ||
| println!("Created an execution context."); | ||
|
|
||
| // Convert image to tensor data. | ||
| let tensor_data = fs::read("fixture/kitten.rgb")?; | ||
| context | ||
| .set_input(0, TensorType::F32, &[1, 3, 224, 224], &tensor_data) | ||
| .unwrap(); | ||
|
|
||
| // Execute the inference. | ||
| let before_compute = Instant::now(); | ||
| context.compute().unwrap(); | ||
| println!( | ||
| "Executed graph inference, took {} ms.", | ||
| before_compute.elapsed().as_millis() | ||
| ); | ||
|
|
||
| // Retrieve the output. | ||
| let mut output_buffer = vec![0f32; 1000]; | ||
| context.get_output(0, &mut output_buffer[..]).unwrap(); | ||
|
|
||
| let result = sort_results(&output_buffer); | ||
| println!("Found results, sorted top 5: {:?}", &result[..5]); | ||
| assert_eq!(result[0].0, 284); | ||
| Ok(()) | ||
| } | ||
|
|
||
| // 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.