liveBook · Manning

Wit-bindgen

As explained in Server-Side WebAssembly

wit-bindgen is a code-generation library and tool for producing language bindings and interoperability glue from WIT interfaces.

wit-bindgen generates Rust bindings from a WIT interface so a WebAssembly component can call an imported API without hand-writing its interoperability glue. In a wasi:nn component, the generated bindings expose graph and tensor operations used to load an ONNX model, provide input, and read results. section 5.2.2

wit_bindgen::generate!({
    path: "wit/wasi-nn.wit",
    world: "ml",
});

Generate bindings from the local wasi-nn WIT file and select its ml world before importing the generated wasi::nn APIs. section 5.2.2

WASI neural-network component

Add wit-bindgen with its macros feature, place wasi-nn.wit under wit/, invoke wit_bindgen::generate!, then use the generated Graph, Tensor, and TensorType APIs to run inference. section 5.2.2

How Server-Side WebAssembly covers it

How deep it goes
Demonstrates dependency setup, WIT-file placement, macro invocation, and use of generated graph and tensor APIs.
Examples
Uses a Rust component that classifies a cat image with the SqueezeNet ONNX model.
What it assumes
Introduces bindings through a concrete Rust component setup while relying on Cargo dependencies and a supplied WIT interface.
What you can do after
Apply it via wit-bindgen macros to generate and call wasi:nn bindings in a Rust component.
How it connects
Connects WIT, WebAssembly components, wasi:nn, Wasmtime, ONNX, Graph, and Tensor APIs.

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.