liveBook · Manning

Wasi:nn

As explained in Server-Side WebAssembly

wasi:nn is a WebAssembly interface for loading machine-learning graphs and executing inference through a host-provided neural-network implementation.

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
A screenshot of a computer

AI-generated content may be incorrect.

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

How Server-Side WebAssembly covers it

How deep it goes
Walks through bindings, ONNX graph loading, tensor preprocessing, output ranking, and Wasmtime host linking.
Examples
Uses SqueezeNet to classify a cat image and upgrades SmartCMS from placeholder selection to ONNX-backed story generation.
What it assumes
Builds from cargo component and WIT bindings while assuming a Rust component project and a host capable of implementing wasi:nn.
What you can do after
Apply it via Wasmtime to build a component that loads an ONNX graph, submits tensors, and consumes inference outputs.
How it connects
Connects WebAssembly components with WASI, Wasmtime, ONNX, WIT, tensors, and function-as-a-service systems.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.