|
| 1 | +package wasi:nn; |
| 2 | + |
| 3 | +/// `wasi-nn` is a WASI API for performing machine learning (ML) inference. The API is not (yet) |
| 4 | +/// capable of performing ML training. WebAssembly programs that want to use a host's ML |
| 5 | +/// capabilities can access these capabilities through `wasi-nn`'s core abstractions: _graphs_ and |
| 6 | +/// _tensors_. A user `load`s an ML model -- instantiated as a _graph_ -- to use in an ML _backend_. |
| 7 | +/// Then, the user passes _tensor_ inputs to the _graph_, computes the inference, and retrieves the |
| 8 | +/// _tensor_ outputs. |
| 9 | +/// |
| 10 | +/// This example world shows how to use these primitives together. |
| 11 | +world ml { |
| 12 | + import tensor; |
| 13 | + import graph; |
| 14 | + import inference; |
| 15 | + import errors; |
| 16 | +} |
| 17 | + |
| 18 | +/// All inputs and outputs to an ML inference are represented as `tensor`s. |
| 19 | +interface tensor { |
| 20 | + /// The dimensions of a tensor. |
| 21 | + /// |
| 22 | + /// The array length matches the tensor rank and each element in the array describes the size of |
| 23 | + /// each dimension |
| 24 | + type tensor-dimensions = list<u32>; |
| 25 | + |
| 26 | + /// The type of the elements in a tensor. |
| 27 | + enum tensor-type { |
| 28 | + FP16, |
| 29 | + FP32, |
| 30 | + FP64, |
| 31 | + BF16, |
| 32 | + U8, |
| 33 | + I32, |
| 34 | + I64 |
| 35 | + } |
| 36 | + |
| 37 | + /// The tensor data. |
| 38 | + /// |
| 39 | + /// Initially conceived as a sparse representation, each empty cell would be filled with zeros |
| 40 | + /// and the array length must match the product of all of the dimensions and the number of bytes |
| 41 | + /// in the type (e.g., a 2x2 tensor with 4-byte f32 elements would have a data array of length |
| 42 | + /// 16). Naturally, this representation requires some knowledge of how to lay out data in |
| 43 | + /// memory--e.g., using row-major ordering--and could perhaps be improved. |
| 44 | + type tensor-data = list<u8>; |
| 45 | + |
| 46 | + record tensor { |
| 47 | + // Describe the size of the tensor (e.g., 2x2x2x2 -> [2, 2, 2, 2]). To represent a tensor |
| 48 | + // containing a single value, use `[1]` for the tensor dimensions. |
| 49 | + dimensions: tensor-dimensions, |
| 50 | + |
| 51 | + // Describe the type of element in the tensor (e.g., `f32`). |
| 52 | + tensor-type: tensor-type, |
| 53 | + |
| 54 | + // Contains the tensor data. |
| 55 | + data: tensor-data, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// A `graph` is a loaded instance of a specific ML model (e.g., MobileNet) for a specific ML |
| 60 | +/// framework (e.g., TensorFlow): |
| 61 | +interface graph { |
| 62 | + use errors.{error}; |
| 63 | + use tensor.{tensor}; |
| 64 | + |
| 65 | + /// An execution graph for performing inference (i.e., a model). |
| 66 | + /// |
| 67 | + /// TODO: replace with `resource` (https://github.com/WebAssembly/wasi-nn/issues/47). |
| 68 | + type graph = u32; |
| 69 | + |
| 70 | + /// Describes the encoding of the graph. This allows the API to be implemented by various |
| 71 | + /// backends that encode (i.e., serialize) their graph IR with different formats. |
| 72 | + enum graph-encoding { |
| 73 | + openvino, |
| 74 | + onnx, |
| 75 | + tensorflow, |
| 76 | + pytorch, |
| 77 | + tensorflowlite, |
| 78 | + autodetect, |
| 79 | + } |
| 80 | + |
| 81 | + /// Define where the graph should be executed. |
| 82 | + enum execution-target { |
| 83 | + cpu, |
| 84 | + gpu, |
| 85 | + tpu |
| 86 | + } |
| 87 | + |
| 88 | + /// The graph initialization data. |
| 89 | + /// |
| 90 | + /// This gets bundled up into an array of buffers because implementing backends may encode their |
| 91 | + /// graph IR in parts (e.g., OpenVINO stores its IR and weights separately). |
| 92 | + type graph-builder = list<u8>; |
| 93 | + |
| 94 | + /// Load a `graph` from an opaque sequence of bytes to use for inference. |
| 95 | + load: func(builder: list<graph-builder>, encoding: graph-encoding, target: execution-target) -> result<graph, error>; |
| 96 | + |
| 97 | + /// Load a `graph` by name. |
| 98 | + /// |
| 99 | + /// How the host expects the names to be passed and how it stores the graphs for retrieval via |
| 100 | + /// this function is **implementation-specific**. This allows hosts to choose name schemes that |
| 101 | + /// range from simple to complex (e.g., URLs?) and caching mechanisms of various kinds. |
| 102 | + load-by-name: func(name: string) -> result<graph, error>; |
| 103 | +} |
| 104 | + |
| 105 | +/// An inference "session" is encapsulated by a `graph-execution-context`. This structure binds a |
| 106 | +/// `graph` to input tensors before `compute`-ing an inference: |
| 107 | +interface inference { |
| 108 | + use errors.{error}; |
| 109 | + use tensor.{tensor, tensor-data}; |
| 110 | + use graph.{graph}; |
| 111 | + |
| 112 | + /// Bind a `graph` to the input and output tensors for an inference. |
| 113 | + /// |
| 114 | + /// TODO: this is no longer necessary in WIT (https://github.com/WebAssembly/wasi-nn/issues/43) |
| 115 | + type graph-execution-context = u32; |
| 116 | + |
| 117 | + /// Create an execution instance of a loaded graph. |
| 118 | + init-execution-context: func(graph: graph) -> result<graph-execution-context, error>; |
| 119 | + |
| 120 | + /// Define the inputs to use for inference. |
| 121 | + set-input: func(ctx: graph-execution-context, index: u32, tensor: tensor) -> result<_, error>; |
| 122 | + |
| 123 | + /// Compute the inference on the given inputs. |
| 124 | + /// |
| 125 | + /// Note the expected sequence of calls: `set-input`, `compute`, `get-output`. TODO: this |
| 126 | + /// expectation could be removed as a part of https://github.com/WebAssembly/wasi-nn/issues/43. |
| 127 | + compute: func(ctx: graph-execution-context) -> result<_, error>; |
| 128 | + |
| 129 | + /// Extract the outputs after inference. |
| 130 | + get-output: func(ctx: graph-execution-context, index: u32) -> result<tensor-data, error>; |
| 131 | +} |
| 132 | + |
| 133 | +/// TODO: create function-specific errors (https://github.com/WebAssembly/wasi-nn/issues/42) |
| 134 | +interface errors { |
| 135 | + enum error { |
| 136 | + // Caller module passed an invalid argument. |
| 137 | + invalid-argument, |
| 138 | + // Invalid encoding. |
| 139 | + invalid-encoding, |
| 140 | + busy, |
| 141 | + // Runtime Error. |
| 142 | + runtime-error, |
| 143 | + // Unsupported operation. |
| 144 | + unsupported-operation, |
| 145 | + // Graph is too large. |
| 146 | + too-large, |
| 147 | + // Graph not found. |
| 148 | + not-found |
| 149 | + } |
| 150 | +} |
0 commit comments