1
- package wasi : nn
1
+ package wasi : nn ;
2
2
3
3
/// `wasi-nn` is a WASI API for performing machine learning (ML) inference. The API is not (yet)
4
4
/// capable of performing ML training. WebAssembly programs that want to use a host's ML
@@ -9,10 +9,10 @@ package wasi:nn
9
9
///
10
10
/// This example world shows how to use these primitives together.
11
11
world ml {
12
- import tensor
13
- import graph
14
- import inference
15
- import errors
12
+ import tensor ;
13
+ import graph ;
14
+ import inference ;
15
+ import errors ;
16
16
}
17
17
18
18
/// All inputs and outputs to an ML inference are represented as `tensor` s.
@@ -21,7 +21,7 @@ interface tensor {
21
21
///
22
22
/// The array length matches the tensor rank and each element in the array describes the size of
23
23
/// each dimension
24
- type tensor-dimensions = list <u32 >
24
+ type tensor-dimensions = list <u32 >;
25
25
26
26
/// The type of the elements in a tensor.
27
27
enum tensor-type {
@@ -41,7 +41,7 @@ interface tensor {
41
41
/// in the type (e.g., a 2x2 tensor with 4-byte f32 elements would have a data array of length
42
42
/// 16). Naturally, this representation requires some knowledge of how to lay out data in
43
43
/// memory--e.g., using row-major ordering--and could perhaps be improved.
44
- type tensor-data = list <u8 >
44
+ type tensor-data = list <u8 >;
45
45
46
46
record tensor {
47
47
// Describe the size of the tensor (e.g., 2x2x2x2 -> [2, 2, 2, 2]). To represent a tensor
@@ -59,13 +59,13 @@ interface tensor {
59
59
/// A `graph` is a loaded instance of a specific ML model (e.g., MobileNet) for a specific ML
60
60
/// framework (e.g., TensorFlow):
61
61
interface graph {
62
- use errors . {error }
63
- use tensor . {tensor }
62
+ use errors . {error };
63
+ use tensor . {tensor };
64
64
65
65
/// An execution graph for performing inference (i.e., a model).
66
66
///
67
67
/// TODO: replace with `resource` (https://github.com/WebAssembly/wasi-nn/issues/47).
68
- type graph = u32
68
+ type graph = u32 ;
69
69
70
70
/// Describes the encoding of the graph. This allows the API to be implemented by various
71
71
/// backends that encode (i.e., serialize) their graph IR with different formats.
@@ -89,45 +89,45 @@ interface graph {
89
89
///
90
90
/// This gets bundled up into an array of buffers because implementing backends may encode their
91
91
/// graph IR in parts (e.g., OpenVINO stores its IR and weights separately).
92
- type graph-builder = list <u8 >
92
+ type graph-builder = list <u8 >;
93
93
94
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 >
95
+ load : func (builder : list <graph-builder >, encoding : graph-encoding , target : execution-target ) -> result <graph , error >;
96
96
97
97
/// Load a `graph` by name.
98
98
///
99
99
/// How the host expects the names to be passed and how it stores the graphs for retrieval via
100
100
/// this function is **implementation-specific** . This allows hosts to choose name schemes that
101
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 >
102
+ load-by-name : func (name : string ) -> result <graph , error >;
103
103
}
104
104
105
105
/// An inference "session" is encapsulated by a `graph-execution-context` . This structure binds a
106
106
/// `graph` to input tensors before `compute` -ing an inference:
107
107
interface inference {
108
- use errors . {error }
109
- use tensor . {tensor , tensor-data }
110
- use graph . {graph }
108
+ use errors . {error };
109
+ use tensor . {tensor , tensor-data };
110
+ use graph . {graph };
111
111
112
112
/// Bind a `graph` to the input and output tensors for an inference.
113
113
///
114
114
/// TODO: this is no longer necessary in WIT (https://github.com/WebAssembly/wasi-nn/issues/43)
115
- type graph-execution-context = u32
115
+ type graph-execution-context = u32 ;
116
116
117
117
/// Create an execution instance of a loaded graph.
118
- init-execution-context : func (graph : graph ) -> result <graph-execution-context , error >
118
+ init-execution-context : func (graph : graph ) -> result <graph-execution-context , error >;
119
119
120
120
/// Define the inputs to use for inference.
121
- set-input : func (ctx : graph-execution-context , index : u32 , tensor : tensor ) -> result <_ , error >
121
+ set-input : func (ctx : graph-execution-context , index : u32 , tensor : tensor ) -> result <_ , error >;
122
122
123
123
/// Compute the inference on the given inputs.
124
124
///
125
125
/// Note the expected sequence of calls: `set-input` , `compute` , `get-output` . TODO: this
126
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 >
127
+ compute : func (ctx : graph-execution-context ) -> result <_ , error >;
128
128
129
129
/// Extract the outputs after inference.
130
- get-output : func (ctx : graph-execution-context , index : u32 ) -> result <tensor-data , error >
130
+ get-output : func (ctx : graph-execution-context , index : u32 ) -> result <tensor-data , error >;
131
131
}
132
132
133
133
/// TODO: create function-specific errors (https://github.com/WebAssembly/wasi-nn/issues/42)
0 commit comments