wasi:nn is a WASI interface for running machine-learning inference from a Wasm component without building a neural-network host from scratch. A host supplies the backend; the component loads a graph, creates an execution context, provides tensors, computes, and reads outputs. This lets ML workloads use framework and hardware optimizations while retaining Wasm sandboxing. section 5.0 section 5.2 section 5.2.2
An image-classification component follows this sequence:
- Generate bindings from the wasi:nn WIT interface, then import graph and tensor types. section 5.2.2
- Read an ONNX model, load it as a graph for a CPU execution target, and initialize an execution context. section 5.2.2
- Resize and normalize an image, arrange RGB values into a floating-point tensor, and set the graph input. section 5.2.2
- Invoke compute, decode output bytes as f32 values, apply softmax, and rank labels by probability. section 5.2.2
- Run with a host that enables a wasi:nn backend; Wasmtime support is experimental and offers OpenVINO, ONNX, and WinML backends. section 5.2.2
Classifying a cat with ONNX
A command component accepts an image path, loads the SqueezeNet ONNX graph, converts a 224×224 image into an Fp32 tensor, and prints the three highest-probability labels. The sample identifies a Persian cat at about 0.95 probability. Build it with cargo component build --release, then run it in a Wasmtime environment configured with ONNX support and access to the model fixtures. section 5.2.2
Host integration is deliberate: preload ML backends, create a WasiNnCtx and graph registry, retain them in host state, and add the wasi:nn context to the component linker. This lets a composed SmartCMS application keep its key-value capability while replacing a placeholder ML guest with ONNX-backed story generation. section 5.4 section 5.4.3
Figure 5.5 The architecture of our SmartCMS project with the addition of a real ML component
Inspect the SmartCMS architecture after its ML guest becomes a real wasi:nn-backed component; it clarifies the relationship between guest inference and host-provided backend support. section 5.4
