Skip to content

Commit 768c77e

Browse files
committed
wasi-nn: use resources
Recent discussion in the wasi-nn proposal (see [wasi-nn#59], e.g.) has concluded that the right approach for representing wasi-nn "things" (tensors, graph, etc.) is with a component model _resource_. This sweeping change brings Wasmtime's implementation in line with that decision. Initially I had structured this PR to remove all of the WITX-based implementation (#8530). But, after consulting in a Zulip [thread] on what other WASI proposals aim to do, this PR pivoted to support _both_` the WITX-based and WIT-based ABIs (e.g., preview1 era versus preview2, component model era). What is clear is that the WITX-based specification will remain "frozen in time" while the WIT-based implementation moves forward. What that means for this PR is a "split world" paradigm. In many places, we have to distinguish between the `wit` and `witx` versions of the same thing. This change isn't the end state yet: it's a big step forward towards bringing Wasmtime back in line with the WIT spec but, despite my best efforts, doesn't fully fix all the TODOs left behind over several years of development. I have, however, taken the liberty to refactor and fix various parts as I came across them (e.g., the ONNX backend). I plan to continue working on this in future PRs to figure out a good error paradigm (the current one is too wordy) and device residence. [wasi-nn#59]: WebAssembly/wasi-nn#59 [thread]: https://bytecodealliance.zulipchat.com/#narrow/stream/219900-wasi/topic/wasi-nn's.20preview1.20vs.20preview2.20timeline prtest:full
1 parent f4b49b8 commit 768c77e

34 files changed

+1547
-740
lines changed

Cargo.lock

Lines changed: 20 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bench-api/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ struct BenchState {
418418
struct HostState {
419419
wasi: WasiCtx,
420420
#[cfg(feature = "wasi-nn")]
421-
wasi_nn: wasmtime_wasi_nn::WasiNnCtx,
421+
wasi_nn: wasmtime_wasi_nn::witx::WasiNnCtx,
422422
}
423423

424424
impl BenchState {
@@ -509,7 +509,7 @@ impl BenchState {
509509
#[cfg(feature = "wasi-nn")]
510510
wasi_nn: {
511511
let (backends, registry) = wasmtime_wasi_nn::preload(&[])?;
512-
wasmtime_wasi_nn::WasiNnCtx::new(backends, registry)
512+
wasmtime_wasi_nn::witx::WasiNnCtx::new(backends, registry)
513513
},
514514
};
515515

crates/test-programs/artifacts/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ fn build_and_generate_tests() {
9090
}
9191

9292
// Generate a component from each test.
93-
if kind == "nn" || target == "dwarf_imported_memory" || target == "dwarf_shared_memory" {
93+
if target == "dwarf_imported_memory"
94+
|| target == "dwarf_shared_memory"
95+
|| target.starts_with("nn_witx")
96+
{
9497
continue;
9598
}
9699
let adapter = match target.as_str() {

crates/test-programs/src/bin/nn_image_classification_winml.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/test-programs/src/bin/nn_image_classification_onnx.rs renamed to crates/test-programs/src/bin/nn_wit_image_classification_onnx.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use anyhow::{Context, Result};
22
use std::fs;
3-
use test_programs::nn::{classify, sort_results};
4-
use wasi_nn::{ExecutionTarget, GraphBuilder, GraphEncoding};
3+
use test_programs::nn::{sort_results, wit};
54

65
pub fn main() -> Result<()> {
76
let model = fs::read("fixture/model.onnx")
87
.context("the model file to be mapped to the fixture directory")?;
9-
let graph =
10-
GraphBuilder::new(GraphEncoding::Onnx, ExecutionTarget::CPU).build_from_bytes([&model])?;
8+
let graph = wit::load(
9+
&[model],
10+
wit::GraphEncoding::Onnx,
11+
wit::ExecutionTarget::Cpu,
12+
)?;
1113
let tensor = fs::read("fixture/000000062808.rgb")
1214
.context("the tensor file to be mapped to the fixture directory")?;
13-
let results = classify(graph, tensor)?;
15+
let results = wit::classify(graph, ("input", tensor), "output")?;
1416
let top_five = &sort_results(&results)[..5];
15-
// 963 is meat loaf, meatloaf.
17+
// 963 is "meat loaf, meatloaf."
1618
// https://github.com/onnx/models/blob/bec48b6a70e5e9042c0badbaafefe4454e072d08/validated/vision/classification/synset.txt#L963
1719
assert_eq!(top_five[0].class_id(), 963);
1820
println!("found results, sorted top 5: {:?}", top_five);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use anyhow::{Context, Result};
2+
use std::fs;
3+
use test_programs::nn::{sort_results, wit};
4+
5+
pub fn main() -> Result<()> {
6+
let xml = fs::read("fixture/model.xml")
7+
.context("the model file to be mapped to the fixture directory")?;
8+
let weights = fs::read("fixture/model.bin")
9+
.context("the weights file to be mapped to the fixture directory")?;
10+
let graph = wit::load(
11+
&[xml, weights],
12+
wit::GraphEncoding::Openvino,
13+
wit::ExecutionTarget::Cpu,
14+
)?;
15+
let tensor = fs::read("fixture/tensor.bgr")
16+
.context("the tensor file to be mapped to the fixture directory")?;
17+
let results = wit::classify(
18+
graph,
19+
("input", tensor),
20+
"MobilenetV2/Predictions/Reshape_1",
21+
)?;
22+
let top_five = &sort_results(&results)[..5];
23+
println!("found results, sorted top 5: {:?}", top_five);
24+
Ok(())
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use anyhow::{Context, Result};
2+
use std::fs;
3+
use test_programs::nn::{sort_results, wit};
4+
5+
pub fn main() -> Result<()> {
6+
let graph = wit::load_by_name("fixtures")?;
7+
let tensor: Vec<u8> = fs::read("fixture/tensor.bgr")
8+
.context("the tensor file to be mapped to the fixture directory")?;
9+
let results = wit::classify(
10+
graph,
11+
("input", tensor),
12+
"MobilenetV2/Predictions/Reshape_1",
13+
)?;
14+
let top_five = &sort_results(&results)[..5];
15+
println!("found results, sorted top 5: {:?}", top_five);
16+
Ok(())
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use anyhow::{Context, Result};
22
use std::fs;
3-
use test_programs::nn::{classify, sort_results};
4-
use wasi_nn::{ExecutionTarget, GraphBuilder, GraphEncoding};
3+
use test_programs::nn::{sort_results, wit};
54

65
pub fn main() -> Result<()> {
7-
let graph = GraphBuilder::new(GraphEncoding::Openvino, ExecutionTarget::CPU)
8-
.build_from_cache("fixtures")?;
6+
let graph = wit::load_by_name("mobilenet")?;
97
let tensor = fs::read("fixture/tensor.bgr")
108
.context("the tensor file to be mapped to the fixture directory")?;
11-
let results = classify(graph, tensor)?;
9+
let results = wit::classify(graph, ("input", tensor), "output")?;
1210
let top_five = &sort_results(&results)[..5];
1311
println!("found results, sorted top 5: {:?}", top_five);
12+
assert_eq!(top_five[0].class_id(), 284);
1413
Ok(())
1514
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use anyhow::{Context, Result};
2+
use std::fs;
3+
use test_programs::nn::{sort_results, witx};
4+
5+
pub fn main() -> Result<()> {
6+
let model = fs::read("fixture/model.onnx")
7+
.context("the model file to be mapped to the fixture directory")?;
8+
let graph = witx::load(
9+
&[&model],
10+
witx::GraphEncoding::Onnx,
11+
witx::ExecutionTarget::CPU,
12+
)?;
13+
let tensor = fs::read("fixture/000000062808.rgb")
14+
.context("the tensor file to be mapped to the fixture directory")?;
15+
let results = witx::classify(graph, tensor)?;
16+
let top_five = &sort_results(&results)[..5];
17+
// 963 is "meat loaf, meatloaf."
18+
// https://github.com/onnx/models/blob/bec48b6a70e5e9042c0badbaafefe4454e072d08/validated/vision/classification/synset.txt#L963
19+
assert_eq!(top_five[0].class_id(), 963);
20+
println!("found results, sorted top 5: {:?}", top_five);
21+
Ok(())
22+
}

crates/test-programs/src/bin/nn_image_classification.rs renamed to crates/test-programs/src/bin/nn_witx_image_classification_openvino.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use anyhow::{Context, Result};
22
use std::fs;
3-
use test_programs::nn::{classify, sort_results};
4-
use wasi_nn::{ExecutionTarget, GraphBuilder, GraphEncoding};
3+
use test_programs::nn::{sort_results, witx};
54

65
pub fn main() -> Result<()> {
76
let xml = fs::read("fixture/model.xml")
87
.context("the model file to be mapped to the fixture directory")?;
98
let weights = fs::read("fixture/model.bin")
109
.context("the weights file to be mapped to the fixture directory")?;
11-
let graph = GraphBuilder::new(GraphEncoding::Openvino, ExecutionTarget::CPU)
12-
.build_from_bytes([&xml, &weights])?;
10+
let graph = witx::load(
11+
&[&xml, &weights],
12+
witx::GraphEncoding::Openvino,
13+
witx::ExecutionTarget::CPU,
14+
)?;
1315
let tensor = fs::read("fixture/tensor.bgr")
1416
.context("the tensor file to be mapped to the fixture directory")?;
15-
let results = classify(graph, tensor)?;
17+
let results = witx::classify(graph, tensor)?;
1618
let top_five = &sort_results(&results)[..5];
1719
println!("found results, sorted top 5: {:?}", top_five);
1820
Ok(())

0 commit comments

Comments
 (0)