-
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 pytorch backend, tests, examples
- Loading branch information
1 parent
9bc918f
commit 32d61b2
Showing
16 changed files
with
1,512 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
crates/test-programs/src/bin/nn_wit_image_classification_pytorch.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,37 @@ | ||
use anyhow::{Context, Result}; | ||
use std::fs; | ||
use test_programs::nn::{sort_results, wit}; | ||
|
||
pub fn main() -> Result<()> { | ||
let model = fs::read("fixture/model.pt") | ||
.context("the model file to be mapped to the fixture directory")?; | ||
let graph = wit::load( | ||
&[model], | ||
wit::GraphEncoding::Pytorch, | ||
wit::ExecutionTarget::Cpu, | ||
)?; | ||
let tensor = fs::read("fixture/kitten.tensor") | ||
.context("the tensor file to be mapped to the fixture directory")?; | ||
let output_buffer = wit::classify(graph, ("input", tensor), "output")?; | ||
let result = softmax(output_buffer); | ||
let top_five = &sort_results(&result)[..5]; | ||
assert_eq!(top_five[0].class_id(), 281); | ||
println!("found results, sorted top 5: {top_five:?}"); | ||
Ok(()) | ||
} | ||
|
||
fn softmax(output_tensor: Vec<f32>) -> Vec<f32> { | ||
let max_val = output_tensor | ||
.iter() | ||
.cloned() | ||
.fold(f32::NEG_INFINITY, f32::max); | ||
|
||
// Compute the exponential of each element subtracted by max_val for numerical stability. | ||
let exps: Vec<f32> = output_tensor.iter().map(|&x| (x - max_val).exp()).collect(); | ||
|
||
// Compute the sum of the exponentials. | ||
let sum_exps: f32 = exps.iter().sum(); | ||
|
||
// Normalize each element to get the probabilities. | ||
exps.iter().map(|&exp| exp / sum_exps).collect() | ||
} |
37 changes: 37 additions & 0 deletions
37
crates/test-programs/src/bin/nn_witx_image_classification_pytorch.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,37 @@ | ||
use anyhow::{Context, Result}; | ||
use std::fs; | ||
use test_programs::nn::{sort_results, witx}; | ||
|
||
pub fn main() -> Result<()> { | ||
let model = fs::read("fixture/model.pt") | ||
.context("the model file to be mapped to the fixture directory")?; | ||
let graph = witx::load( | ||
&[&model], | ||
witx::GraphEncoding::Pytorch, | ||
witx::ExecutionTarget::CPU, | ||
)?; | ||
let tensor = fs::read("fixture/kitten.tensor") | ||
.context("the tensor file to be mapped to the fixture directory")?; | ||
let output_buffer = witx::classify(graph, tensor)?; | ||
let result = softmax(output_buffer); | ||
let top_five = &sort_results(&result)[..5]; | ||
assert_eq!(top_five[0].class_id(), 281); | ||
println!("found results, sorted top 5: {top_five:?}"); | ||
Ok(()) | ||
} | ||
|
||
fn softmax(output_tensor: Vec<f32>) -> Vec<f32> { | ||
let max_val = output_tensor | ||
.iter() | ||
.cloned() | ||
.fold(f32::NEG_INFINITY, f32::max); | ||
|
||
// Compute the exponential of each element subtracted by max_val for numerical stability. | ||
let exps: Vec<f32> = output_tensor.iter().map(|&x| (x - max_val).exp()).collect(); | ||
|
||
// Compute the sum of the exponentials. | ||
let sum_exps: f32 = exps.iter().sum(); | ||
|
||
// Normalize each element to get the probabilities. | ||
exps.iter().map(|&exp| exp / sum_exps).collect() | ||
} |
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
196 changes: 196 additions & 0 deletions
196
crates/wasi-nn/examples/classification-example-pytorch/Cargo.lock
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
crates/wasi-nn/examples/classification-example-pytorch/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,17 @@ | ||
[package] | ||
name = "wasi-nn-example-pytorch" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
wasi-nn = "0.6.0" | ||
anyhow = "1.0.86" | ||
image = { version = "0.25.2", default-features = false, features = ["png"] } | ||
|
||
# This crate is built with the wasm32-wasip1 target, so it's separate | ||
# from the main Wasmtime build, so use this directive to exclude it | ||
# from the parent directory's workspace. | ||
[workspace] |
15 changes: 15 additions & 0 deletions
15
crates/wasi-nn/examples/classification-example-pytorch/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,15 @@ | ||
This example project demonstrates using the `wasi-nn` API to perform PyTorch based inference. It consists of Rust code that is built using the `wasm32-wasip1` target. | ||
|
||
To run this example: | ||
1. Ensure you set appropriate Libtorch enviornment variables according to [tch-rs instructions]( https://github.com/LaurentMazare/tch-rs?tab=readme-ov-file#libtorch-manual-install). | ||
- Requires the C++ PyTorch library (libtorch) in version *v2.4.0* to be available on | ||
your system. | ||
- `export LIBTORCH=/path/to/libtorch` | ||
2. Build Wasmtime with `wasmtime-wasi-nn/pytorch` feature. | ||
3. Navigate to this example directory `crates/wasi-nn/examples/classification-example-pytorch`. | ||
4. Build this example `cargo build --target=wasm32-wasip1`. | ||
5. Run the generated wasm file with wasmtime after mapping the directory containing Resnet18 `model.pt` and sample image `kitten.png` | ||
``` | ||
${Wasmtime_root_dir}/target/debug/wasmtime -S nn --dir ${Wasmtime_root_dir}/crates/wasi-nn/examples/classification-example-pytorch::. ${Wasmtime_root_dir}/crates/wasi-nn/examples/classification-example-pytorch/target/wasm32-wasip1/debug/wasi-nn-example-pytorch.wasm | ||
``` | ||
6. Check that result `281` has highest probability, which corresponds to `tabby cat`. |
Binary file added
BIN
+55.4 KB
crates/wasi-nn/examples/classification-example-pytorch/fixture/kitten.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+44.7 MB
crates/wasi-nn/examples/classification-example-pytorch/fixture/model.pt
Binary file not shown.
Oops, something went wrong.