Skip to content

Commit db025e4

Browse files
authored
sync up with latest wasi-nn spec (#3530)
1 parent d3e8989 commit db025e4

9 files changed

Lines changed: 207 additions & 99 deletions

File tree

core/iwasm/libraries/wasi-nn/README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@ $ cmake -DWAMR_BUILD_WASI_NN=1 <other options> ...
2121
2222
### Wasm
2323

24-
The definition of functions provided by WASI-NN (Wasm imports) is in the header file _core/iwasm/libraries/wasi-nn/wasi_nn.h_.
24+
The definition of functions provided by WASI-NN (Wasm imports) is in the header file [wasi_nn.h](_core/iwasm/libraries/wasi-nn/wasi_nn.h_). By only including this file in a WASM application you will bind WASI-NN into your module.
2525

26-
By only including this file in a WASM application you will bind WASI-NN into your module.
26+
For some historical reasons, there are two sets of functions in the header file. The first set is the original one, and the second set is the new one. The new set is recommended to use. In code, `WASM_ENABLE_WASI_EPHEMERAL_NN` is used to control which set of functions to use. If `WASM_ENABLE_WASI_EPHEMERAL_NN` is defined, the new set of functions will be used. Otherwise, the original set of functions will be used.
27+
28+
There is a big difference between the two sets of functions, `tensor_type`.
29+
30+
``` c
31+
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
32+
typedef enum { fp16 = 0, fp32, fp64, bf16, u8, i32, i64 } tensor_type;
33+
#else
34+
typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
35+
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
36+
```
37+
38+
It is required to recompile the Wasm application if you want to switch between the two sets of functions.
2739

2840
## Tests
2941

@@ -41,7 +53,10 @@ Build the runtime image for your execution target type.
4153
- `tpu`
4254

4355
```bash
44-
EXECUTION_TYPE=cpu docker build -t wasi-nn-${EXECUTION_TYPE} -f core/iwasm/libraries/wasi-nn/test/Dockerfile.${EXECUTION_TYPE} .
56+
$ pwd
57+
<somewhere>/wasm-micro-runtime
58+
59+
$ EXECUTION_TYPE=cpu docker build -t wasi-nn-${EXECUTION_TYPE} -f core/iwasm/libraries/wasi-nn/test/Dockerfile.${EXECUTION_TYPE} .
4560
```
4661

4762
### Build wasm app

core/iwasm/libraries/wasi-nn/include/wasi_nn.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ load(graph_builder_array *builder, graph_encoding encoding,
2929
execution_target target, graph *g)
3030
__attribute__((import_module("wasi_nn")));
3131

32+
wasi_nn_error
33+
load_by_name(const char *name, graph *g)
34+
__attribute__((import_module("wasi_nn")));
35+
3236
/**
3337
* INFERENCE
3438
*

core/iwasm/libraries/wasi-nn/include/wasi_nn_types.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,31 @@ extern "C" {
1818
*
1919
*/
2020

21-
// Error codes returned by functions in this API.
21+
// sync up with
22+
// https://github.com/WebAssembly/wasi-nn/blob/main/wit/wasi-nn.wit#L136 Error
23+
// codes returned by functions in this API.
2224
typedef enum {
2325
// No error occurred.
2426
success = 0,
2527
// Caller module passed an invalid argument.
2628
invalid_argument,
2729
// Invalid encoding.
2830
invalid_encoding,
29-
// Caller module is missing a memory export.
30-
missing_memory,
31-
// Device or resource busy.
32-
busy,
31+
// The operation timed out.
32+
timeout,
3333
// Runtime Error.
3434
runtime_error,
35+
// Unsupported operation.
36+
unsupported_operation,
37+
// Graph is too large.
38+
too_large,
39+
// Graph not found.
40+
not_found,
41+
// The operation is insecure or has insufficient privilege to be performed.
42+
// e.g., cannot access a hardware feature requested
43+
security,
44+
// The operation failed for an unspecified reason.
45+
unknown,
3546
} wasi_nn_error;
3647

3748
/**
@@ -48,8 +59,14 @@ typedef struct {
4859
uint32_t size;
4960
} tensor_dimensions;
5061

62+
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
63+
// sync up with
64+
// https://github.com/WebAssembly/wasi-nn/blob/main/wit/wasi-nn.wit#L27
5165
// The type of the elements in a tensor.
66+
typedef enum { fp16 = 0, fp32, fp64, bf16, u8, i32, i64 } tensor_type;
67+
#else
5268
typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
69+
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
5370

5471
// The tensor data.
5572
//
@@ -96,6 +113,8 @@ typedef struct {
96113
// An execution graph for performing inference (i.e., a model).
97114
typedef uint32_t graph;
98115

116+
// sync up with
117+
// https://github.com/WebAssembly/wasi-nn/blob/main/wit/wasi-nn.wit#L75
99118
// Describes the encoding of the graph. This allows the API to be implemented by
100119
// various backends that encode (i.e., serialize) their graph IR with different
101120
// formats.
@@ -105,7 +124,8 @@ typedef enum {
105124
tensorflow,
106125
pytorch,
107126
tensorflowlite,
108-
backend_amount
127+
ggml,
128+
autodetect,
109129
} graph_encoding;
110130

111131
// Define where the graph should be executed.
@@ -118,6 +138,7 @@ typedef uint32_t graph_execution_context;
118138

119139
typedef wasi_nn_error (*LOAD)(void *, graph_builder_array *, graph_encoding,
120140
execution_target, graph *);
141+
typedef wasi_nn_error (*LOAD_BY_NAME)(void *, const char *, uint32_t, graph *);
121142
typedef wasi_nn_error (*INIT_EXECUTION_CONTEXT)(void *, graph,
122143
graph_execution_context *);
123144
typedef wasi_nn_error (*SET_INPUT)(void *, graph_execution_context, uint32_t,
@@ -126,11 +147,12 @@ typedef wasi_nn_error (*COMPUTE)(void *, graph_execution_context);
126147
typedef wasi_nn_error (*GET_OUTPUT)(void *, graph_execution_context, uint32_t,
127148
tensor_data, uint32_t *);
128149
/* wasi-nn general APIs */
129-
typedef void (*BACKEND_INITIALIZE)(void **);
130-
typedef void (*BACKEND_DEINITIALIZE)(void *);
150+
typedef wasi_nn_error (*BACKEND_INITIALIZE)(void **);
151+
typedef wasi_nn_error (*BACKEND_DEINITIALIZE)(void *);
131152

132153
typedef struct {
133154
LOAD load;
155+
LOAD_BY_NAME load_by_name;
134156
INIT_EXECUTION_CONTEXT init_execution_context;
135157
SET_INPUT set_input;
136158
COMPUTE compute;
@@ -140,7 +162,7 @@ typedef struct {
140162
} api_function;
141163

142164
bool
143-
wasi_nn_register_backend(graph_encoding backend_code, api_function apis);
165+
wasi_nn_register_backend(api_function apis);
144166

145167
#ifdef __cplusplus
146168
}

core/iwasm/libraries/wasi-nn/src/utils/wasi_nn_app_native.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ graph_builder_array_app_native(wasm_module_inst_t instance,
7676
graph_builder *builder = (graph_builder *)wasm_runtime_malloc(
7777
array_size * sizeof(graph_builder));
7878
if (builder == NULL)
79-
return missing_memory;
79+
return too_large;
8080

8181
for (uint32_t i = 0; i < array_size; ++i) {
8282
wasi_nn_error res;
@@ -149,7 +149,7 @@ tensor_dimensions_app_native(wasm_module_inst_t instance,
149149
*dimensions =
150150
(tensor_dimensions *)wasm_runtime_malloc(sizeof(tensor_dimensions));
151151
if (dimensions == NULL)
152-
return missing_memory;
152+
return too_large;
153153

154154
(*dimensions)->size = dimensions_wasm->size;
155155
(*dimensions)->buf = (uint32_t *)wasm_runtime_addr_app_to_native(

0 commit comments

Comments
 (0)