wasi-nn: Portable Inference from Wasm
Run cargo-classification inference from a component with wasi-nn against an OpenVINO reference backend, and decide when it beats a model server.
Learning objectives
By the end of this chapter you can:
- Explain what
wasi-nnis (a WASI proposal for host-accelerated ML inference built ongraphandtensor) and why its package version,wasi:nn@0.2.0-rc-2024-10-28, carries a release-candidate tag instead of a stable0.2.x. - Trace the wasmtime 50 call sequence:
graph::load-by-name,graph.init-execution-context()and a singlecompute(inputs: list<named-tensor>) -> result<list<named-tensor>, error>, not the olderset-input/compute/get-outputtriad. - Explain why image decode, resize and tensor layout run inside the guest, and name the four facts about MobileNetV2’s OpenVINO IR that
image_to_nchw_bgr_tensordepends on: input shape[1,3,224,224], BGR channel order, no manual normalization and a 1001-wide output. - Run the guest on the
wasmtimeCLI built from source with--features wasi-nnagainst a real OpenVINO install, usingwasmtime run -S nn -S nn-graph=openvino::<dir>. - Choose between
wasi-nnand an external model server reached over HTTP, and name the backendswasi-nnis designed to make interchangeable under an unchanged guest.
Why this matters
wasi-nn is a different kind of import from the patterncatalyst:cargo contract in contracts/wit. It is a pre-1.0, release-candidate WASI proposal (wasi:nn@0.2.0-rc-2024-10-28) that ships inside the wasmtime repository, so a guest importing it is pinned to the wasmtime build that compiles it. The API also differs from most tutorials. The older WITX-era shape passed a tensor with set-input(index, tensor), ran compute() with no arguments and read the result with get-output(index). The WIT shape in wasmtime 50 replaces all three with one call, compute(inputs: list<named-tensor>) -> result<list<named-tensor>, error>, binding tensors by name instead of position. examples/part6-deviations/wasi-nn/ is written against that shape, checked against the WIT that wasmtime v50.0.0 vendors at crates/wasi-nn/wit/wasi-nn.wit.
The value is narrow. wasi-nn lets a guest ask its host to run inference, with the model, hardware and engine entirely the host’s business. The guest here decodes a JPEG, builds a tensor and reads back ranked labels. It never links OpenVINO or a C library and never learns whether the host runs on a CPU, a GPU or something else. The same artifact would run against any backend that implements the interface.
The component imports only wasi:nn
wit/world.wit declares the whole WIT surface:
world classify-cli {
import wasi:nn/tensor@0.2.0-rc-2024-10-28;
import wasi:nn/graph@0.2.0-rc-2024-10-28;
import wasi:nn/inference@0.2.0-rc-2024-10-28;
import wasi:nn/errors@0.2.0-rc-2024-10-28;
}
Four imports from one vendored package and nothing from patterncatalyst:cargo: the crate classifies a photo, not a booking. wit/deps/wasi-nn/wasi-nn.wit is copied verbatim from the wasmtime v50.0.0 source tree rather than written from a spec document. A different wasmtime version could vendor a different WIT shape under the same package version string, and copying from the runtime you will run against is the only way to be sure they match. The world declares no wasi:cli imports because the crate is a [[bin]] command: cargo-component’s command adapter synthesizes the wasi:cli/run export and the stdio, preopen and clock imports.
Cargo.toml pins the one dependency that does real work in the guest:
[dependencies.image]
version = "=0.25.10"
default-features = false
features = ["jpeg", "png"]
Default features are off because they pull in rayon, whose thread pool needs syscalls wasm32-wasip1 lacks, and codecs (AVIF, WebP, TIFF) the crate never uses. Both remaining codecs are pure Rust. A guest cannot link a C image library the way a native host could.
load-by-name, init-execution-context, compute
The whole contract fits in one function:
let graph = graph::load_by_name(GRAPH_NAME)
.map_err(|e| format!("load-by-name('{GRAPH_NAME}') failed: {}", describe(&e)))?;
let context = graph
.init_execution_context()
.map_err(|e| format!("init-execution-context failed: {}", describe(&e)))?;
let outputs = context
.compute(vec![(INPUT_TENSOR_NAME.to_string(), tensor)])
.map_err(|e| format!("compute failed: {}", describe(&e)))?;
let (_name, output) = outputs
.first()
.ok_or_else(|| "compute returned no output tensors".to_string())?;
Three calls, three WIT resources:
graph::load-by-name(name) -> result<graph, error>resolves a graph the host registered under that name."mobilenet"is the basename of the directory that wasmtime’s-S nn-graphflag preloads, not a name the crate invents.graph.init-execution-context()binds the graph to a fresh inference session. The WIT comment flags it as possibly redundant in a future revision, but wasmtime 50 requires it.context.compute(inputs)runs the inference. Onenamed-tensorgoes in, pairing"input"with the built tensor, and alist<named-tensor>comes out.
The "input" name must match the port name in the compiled graph (<layer id="0" name="input" type="Parameter"> in mobilenet.xml), because wasmtime’s OpenVINO backend binds each named tensor by that exact string. A wrong name fails inside compute() at run time as an error resource, not at build time and not as a type error. The helper that formats it:
fn describe(e: &errors::Error) -> String {
format!("{:?}: {}", e.code(), e.data())
}
errors::Error is a WIT resource: code() returns an error-code enum (invalid-argument, invalid-encoding, timeout, runtime-error, unsupported-operation, too-large, not-found, security, unknown) and data() a backend-specific string. Every fallible call routes through it, so load, context and compute failures share one code-plus-detail shape.
On the output side, wasmtime’s OpenVINO backend names output tensors by index (("0", tensor)), not by the IR’s output port name (MobilenetV2/Predictions/Reshape_1). A single-output classifier returns one entry, so outputs.first() takes it positionally.
Preprocessing in the guest, read from the model’s IR
Every decision in image_to_nchw_bgr_tensor comes from mobilenet.xml, the metadata OpenVINO’s Model Optimizer wrote when it converted the TensorFlow checkpoint, not from tuning toward a known answer:
fn image_to_nchw_bgr_tensor(image: &image::DynamicImage) -> Tensor {
let resized = image.resize_exact(SIDE, SIDE, FilterType::Triangle);
let rgb = resized.to_rgb8();
let plane_len = (SIDE * SIDE) as usize;
let mut data = vec![0u8; plane_len * 3 * 4]; // 3 channels, 4 bytes/f32
for (x, y, pixel) in rgb.enumerate_pixels() {
let [r, g, b] = pixel.0;
let offset = (y * SIDE + x) as usize;
for (channel, value) in [b, g, r].into_iter().enumerate() {
let start = (channel * plane_len + offset) * 4;
data[start..start + 4].copy_from_slice(&(value as f32).to_le_bytes());
}
}
Tensor::new(&[1, 3, SIDE, SIDE], TensorType::Fp32, &data)
}
- Shape.
SIDEis 224, matching<data shape="1,3,224,224"/>on the input layer. The layout is NCHW, channel-planar, asdisable_nhwc_to_nchw=Falserecords. The fixture’s published name,tensor-1x224x224x3-f32.bgr, describes the original TensorFlow NHWC shape, not what the compiled graph reads. Matching layout to a filename instead of the IR is the usual mistake. - Channel order. The loop writes
[b, g, r].<reverse_input_channels value="True">is the Model Optimizer flag for an RGB-trained model fed BGR, with the reversal folded into the graph. - Values. Raw
0to255intensities cast tof32, with no mean subtraction or rescaling.mean_values = input[127.5,127.5,127.5]andscale_values = input[127.5]run asMultiplyandAddnodes inside the graph, on the OpenVINO side of the boundary. Normalizing again in the guest would apply it twice.
f32_scores checks the output is exactly 1001 values and drops the first:
if floats.len() != 1001 {
return Err(format!(
"expected a 1001-way softmax (1000 ImageNet classes + background), got {} values",
floats.len()
));
}
Ok(floats[1..].to_vec())
The graph was exported with the TF-Slim convention of reserving index 0 for “background”, confirmed by the output layer’s <dim>1001</dim>. labels/imagenet.txt holds the 1000 class names, 0-indexed, under Apache-2.0 (from Google’s tensorflow/tfjs-models). Raw index i >= 1 maps to line i - 1. fixtures/cargo.jpg is a CC0 container-ship photograph, and ImageNet class 510 is container ship, containership, container vessel, the label the model is expected to rank first. Only running the inference confirms it.
The host: wasmtime built for wasi-nn
There is no host crate and no custom Engine/Store/Linker embedding. The host is the wasmtime CLI compiled with a feature most prebuilt releases omit:
git clone --branch v50.0.0 --depth 1 https://github.com/bytecodealliance/wasmtime
cd wasmtime
cargo build --release --features wasi-nn
install -m 0755 target/release/wasmtime ~/.local/bin/wasmtime
wasmtime --version # wasmtime-cli 50.0.0
The published binaries lack wasi-nn because the OpenVINO backend links the openvino crate (v0.11.0), a binding over OpenVINO’s shared libraries, and shipping it by default would force that dependency on every download. Install OpenVINO 2026.4.1 first and confirm ldconfig -p | grep openvino resolves the libraries.
Building the guest needs none of this, because wasi:nn imports resolve at wasmtime run time on the host, not at cargo component build time. The cost of satisfying them falls on whoever runs the component.
run.sh fetches the model, builds the component and runs inference with two -S flags and one preopened directory:
wasmtime run \
-S nn \
-S nn-graph="openvino::${MODEL_DIR}" \
--dir "${FIXTURES_DIR}::/fixtures" \
"$WASM" \
/fixtures/cargo.jpg
-S nnenables thewasi:nnimports. Without itwasmtime runrejects the component’s imports.-S nn-graph=openvino::<dir>preloads<dir>as an OpenVINO graph and registers it under the directory’s basename, heremobilenetbecause the directory ismodel/mobilenet/.fetch-model.shrenames the download tomodel.xmlandmodel.binbecause the backend hard-codes those names. A wrong directory or file name fails at run time with anot-founderror code, not at build time.--dir "${FIXTURES_DIR}::/fixtures"preopens the fixtures directory at/fixturesthrough the ordinarywasi:filesystemmechanism.
The -S nn-graph spelling has changed across wasmtime releases. Treat wasmtime run -S help on your own build as the source of truth.
load versus load-by-name
The graph interface also exposes load(builder: list<graph-builder>, encoding: graph-encoding, target: execution-target) -> result<graph, error>. The guest reads raw model bytes itself, through a preopened directory, and picks the graph-encoding (openvino, onnx, tensorflow, pytorch, tensorflowlite, ggml, autodetect) and execution-target (cpu, gpu, tpu).
load-by-name |
load |
|
|---|---|---|
| Model location | Host-side registry, named by a host convention (here a directory basename and one CLI flag) | Bytes the guest carries or fetches |
| Depends on | One host’s naming convention | Only the WIT contract |
| Portable across hosts | Only if they share the naming | Any wasi-nn host with that package version |
| Fits | One pinned model on a known host | A guest that must run on hosts with no shared convention |
This component uses load-by-name because it matches the wasmtime CLI for a single pinned model.
Where wasi-nn fits
The familiar alternative is an HTTP call to a model server: a FastAPI wrapper, a dedicated inference server or a managed endpoint. It is a separate process with its own deploy lifecycle. wasi-nn answers a different question: how does a component, with no network hop and no extra process, ask the host already running it to do inference on whichever backend that host compiled in? The graph-encoding enum is the portability story. Each value is a backend a host could implement behind the same tensor, graph and inference interfaces, so the identical .wasm can run on a different engine with no recompilation. This one runs on OpenVINO because that is the backend wasmtime ships and the one this setup can build and verify.
Decision
Use
wasi-nnwhen inference must run in-process at the component’s call boundary, with no network hop and no separate serving process, and the backend is the host’s concern. Use an HTTP model server when the model is large enough to need its own scaling and deployment, is shared by callers that should not each need awasi-nnhost, or is already a managed endpoint. They combine: a component can usewasi-nnfor a cheap, latency-sensitive classification and call out for a heavier model. The choice is per inference call.
🤖 Explore with your agent
“In examples/part6-deviations/wasi-nn, the classify-cli world imports the four wasi:nn@0.2.0-rc-2024-10-28 interfaces and src/main.rs calls graph::load-by-name, init-execution-context and compute. Review image_to_nchw_bgr_tensor against model/mobilenet/model.xml: list the shape, channel order, normalization and output width the IR declares, and say which of them would break if the guest also subtracted 127.5 from each pixel.”
Check: the reply cites
<data shape="1,3,224,224"/>,reverse_input_channels,mean_values/scale_valuesand<dim>1001</dim>, and says normalization would be applied twice;grep -n 'mean_values' model/mobilenet/model.xmlconfirms the metadata.
Build, run, observe
cd examples/part6-deviations/wasi-nn && ./run.sh
run.sh fetches the OpenVINO MobileNetV2 fixture, builds the component with build.sh, then runs wasmtime run -S nn -S nn-graph=openvino::model/mobilenet --dir fixtures::/fixtures cargo-wasi-nn.wasm /fixtures/cargo.jpg. It needs OpenVINO 2026.4.1 and a from-source wasmtime v50.0.0 with --features wasi-nn; the header comment in run.sh and the README.md beside it give the setup. The component decodes the JPEG, builds the NCHW/BGR tensor, runs the three calls and prints the top five ImageNet labels and scores, descending. build.sh alone needs neither OpenVINO nor wasmtime-from-source and only checks that the component imports wasi:* and exports wasi:cli/run.
Experiments:
- Rename the model directory:
load-by-name("mobilenet")fails at run time withnot-found. - Rename
model.xmlormodel.bin: the backend cannot load the directory. - Change the tensor name from
"input":computereturns an error from OpenVINO at run time. - Run on a stock
wasmtimebinary:run.shstops because-S nnis missing fromwasmtime run -S help.
Kubernetes is out of scope. A cluster run would need the containerd shim and a real OpenVINO install on the node, the same node-level install that restricted-v2 on OpenShift Local rules out, compounded by OpenVINO’s native libraries.
For deeper coverage
- WASI
wasi-nnproposal: thetensor,graph,inferenceanderrorsinterfaces and the tracking of the pre-1.0 WIT shape. wasmtime-wasi-nn: the crate implementing those interfaces against OpenVINO, ONNX Runtime and other backends, including theload-by-nameregistry and index-named output tensors.- OpenVINO documentation: the Model Optimizer metadata (
reverse_input_channels,mean_values,scale_values) that the preprocessing reads frommobilenet.xml. second-state/WasmEdge-WASINN-examples,openvino-mobilenet-image: an independent wasi-nn preprocessing of the samemobilenet.xmlandmobilenet.bin, with the same BGR, NCHW and no-normalization combination.- Your first component in Rust and in Go: the
cargo-componentcommand-component shape and the WIT package, world and import mechanics theclassify-cliworld builds on. - WebAssembly for other backend architectures: the same node-level-install constraint that keeps this inference path off CRC.