wasi_nn behaviour (wasm v0.16.0)
View SourceThe wasi:nn host: a component asks the host to run a model, and a backend
you name runs it natively.
imports/1 builds the wasi:nn/tensor, wasi:nn/graph, wasi:nn/inference
and wasi:nn/errors imports of wasi:nn@0.2.0-rc-2024-10-28. The runtime
holds no inference engine: a backend is a module that implements this
module's callbacks, and it lives in another application.
Read it when you write a backend, or when you change what a guest may load.
The guide is docs/wasi-nn.md.
The interface is a release candidate upstream, and so is this module: its callbacks and the grant may change when the interface does.
What the module itself decides:
- A guest loads a model by name, and only a name in the grant's
models. Any other name answersnot-found. - A guest brings a model as bytes, with
load, only in a format the grant'sencodingslists and only when the backend has the optionalloadcallback. Otherwiseloadanswersunsupported-operation. - A tensor is held by the host. The bytes of every live tensor of one
process are counted, and the grant's
max_tensor_bytesis the bound, 16 MiB when left out. Over it, the tensor constructor traps withnn_tensor_limit_reachedandcomputeanswerstoo-large. computetakes ownership of its input tensors, as the interface says, and frees them whatever the backend answers.- A backend that raises, or answers a term of another shape, gives the guest
a
runtime-error. Nothing a guest or a backend supplies makes an import raise, except the trap above.
It never calls wasi_preview2, which calls it.
Summary
Types
The format of a model a guest brings as bytes.
What a backend answers on failure: a code and a message in UTF-8.
An error code of wasi:nn/errors.
The nn grant: the backend module, the model names this guest may load, the
model formats it may bring as bytes, the bound on the tensor bytes the host
holds for it, and a term handed to the backend unchanged.
Where the guest asks for the model to run.
A tensor: its dimensions, its element type and its raw bytes. The runtime does not check the dimensions against the length of the data; a backend checks what it needs.
The element type of a tensor.
Callbacks
The guest dropped a graph or a context, or its instance was destroyed. Called once for each.
Run the model on named input tensors and answer named output tensors. The call may block; the instance waits for it.
Start one inference session on a graph.
Load a model the guest brings as bytes: one binary for each part of the
model, in the guest's order. Optional. It is called only for an encoding in
the grant's encodings.
Load the model the guest calls Name. Name is in the grant.
Functions
Check a tensor against the rule of the interface: the length of the data is
the product of the dimensions times the size of one element. A backend for a
model with fixed shapes calls it on each input; the host does not, because
guests that send text commonly send it with dimensions [1].
Free what is behind one host resource of this module: the bytes of a tensor, or a backend's graph or context. Any other resource is left alone, so the fun can be chained with another closer.
The wasi:nn imports for one grant.
Types
-type encoding() :: openvino | onnx | tensorflow | pytorch | tensorflowlite | ggml | autodetect.
The format of a model a guest brings as bytes.
-type error() :: {error_code(), Message :: binary()}.
What a backend answers on failure: a code and a message in UTF-8.
-type error_code() ::
invalid_argument | invalid_encoding | timeout | runtime_error | unsupported_operation |
too_large | not_found | security | unknown.
An error code of wasi:nn/errors.
-type grant() :: #{backend := module(), models := [binary()], encodings => [encoding()], max_tensor_bytes => pos_integer(), backend_opts => term()}.
The nn grant: the backend module, the model names this guest may load, the
model formats it may bring as bytes, the bound on the tensor bytes the host
holds for it, and a term handed to the backend unchanged.
-type target() :: cpu | gpu | tpu.
Where the guest asks for the model to run.
-type tensor() :: #{dimensions := [non_neg_integer()], type := tensor_type(), data := binary()}.
A tensor: its dimensions, its element type and its raw bytes. The runtime does not check the dimensions against the length of the data; a backend checks what it needs.
-type tensor_type() :: fp16 | fp32 | fp64 | bf16 | u8 | i32 | i64.
The element type of a tensor.
Callbacks
-callback close({graph | context, term()}) -> ok.
The guest dropped a graph or a context, or its instance was destroyed. Called once for each.
-callback compute(Context :: term(), [{Name :: binary(), tensor()}]) -> {ok, [{Name :: binary(), tensor()}]} | {error, error()}.
Run the model on named input tensors and answer named output tensors. The call may block; the instance waits for it.
Start one inference session on a graph.
-callback load(Builders :: [binary()], encoding(), target(), BackendOpts :: term()) -> {ok, Graph :: term()} | {error, error()}.
Load a model the guest brings as bytes: one binary for each part of the
model, in the guest's order. Optional. It is called only for an encoding in
the grant's encodings.
-callback load_by_name(Name :: binary(), BackendOpts :: term()) -> {ok, Graph :: term()} | {error, error()}.
Load the model the guest calls Name. Name is in the grant.
Functions
Check a tensor against the rule of the interface: the length of the data is
the product of the dimensions times the size of one element. A backend for a
model with fixed shapes calls it on each input; the host does not, because
guests that send text commonly send it with dimensions [1].
Free what is behind one host resource of this module: the bytes of a tensor, or a backend's graph or context. Any other resource is left alone, so the fun can be chained with another closer.
The wasi:nn imports for one grant.
Merge the map into the imports you instantiate a component with, and pass
resource_closer => fun wasi_nn:close_resource/1 in the options of
wasm_component:instantiate/3, and the same fun to
wasm_component:destroy/2. wasi_preview2:command/1 does both when its
options hold an nn key.