Skip to content

Commit b08052a

Browse files
yamtlum1n0us
authored andcommitted
wasi_ephemeral_nn.h: prefix identfiers to avoid too generic names (bytecodealliance#4358)
1 parent 2c9554e commit b08052a

File tree

4 files changed

+133
-82
lines changed

4 files changed

+133
-82
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
*/
55

66
#define WASM_ENABLE_WASI_EPHEMERAL_NN 1
7+
#define WASI_NN_NAME(name) wasi_ephemeral_nn_##name
8+
79
#include "wasi_nn.h"
10+
11+
#undef WASM_ENABLE_WASI_EPHEMERAL_NN
12+
#undef WASI_NN_NAME

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

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@
3434
* @return wasi_nn_error Execution status.
3535
*/
3636
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
37-
wasi_nn_error
38-
load(graph_builder *builder, uint32_t builder_len, graph_encoding encoding,
39-
execution_target target, graph *g) WASI_NN_IMPORT("load");
37+
WASI_NN_ERROR_TYPE
38+
WASI_NN_NAME(load)
39+
(WASI_NN_NAME(graph_builder) * builder, uint32_t builder_len,
40+
WASI_NN_NAME(graph_encoding) encoding, WASI_NN_NAME(execution_target) target,
41+
WASI_NN_NAME(graph) * g) WASI_NN_IMPORT("load");
4042
#else
41-
wasi_nn_error
42-
load(graph_builder_array *builder, graph_encoding encoding,
43-
execution_target target, graph *g) WASI_NN_IMPORT("load");
43+
WASI_NN_ERROR_TYPE
44+
WASI_NN_NAME(load)
45+
(WASI_NN_NAME(graph_builder_array) * builder,
46+
WASI_NN_NAME(graph_encoding) encoding, WASI_NN_NAME(execution_target) target,
47+
WASI_NN_NAME(graph) * g) WASI_NN_IMPORT("load");
4448
#endif
4549

46-
wasi_nn_error
47-
load_by_name(const char *name, uint32_t name_len, graph *g)
50+
WASI_NN_ERROR_TYPE
51+
WASI_NN_NAME(load_by_name)
52+
(const char *name, uint32_t name_len, WASI_NN_NAME(graph) * g)
4853
WASI_NN_IMPORT("load_by_name");
4954

5055
/**
@@ -59,8 +64,9 @@ load_by_name(const char *name, uint32_t name_len, graph *g)
5964
* @param ctx Execution context.
6065
* @return wasi_nn_error Execution status.
6166
*/
62-
wasi_nn_error
63-
init_execution_context(graph g, graph_execution_context *ctx)
67+
WASI_NN_ERROR_TYPE
68+
WASI_NN_NAME(init_execution_context)
69+
(WASI_NN_NAME(graph) g, WASI_NN_NAME(graph_execution_context) * ctx)
6470
WASI_NN_IMPORT("init_execution_context");
6571

6672
/**
@@ -71,18 +77,20 @@ init_execution_context(graph g, graph_execution_context *ctx)
7177
* @param tensor Input tensor.
7278
* @return wasi_nn_error Execution status.
7379
*/
74-
wasi_nn_error
75-
set_input(graph_execution_context ctx, uint32_t index, tensor *tensor)
76-
WASI_NN_IMPORT("set_input");
80+
WASI_NN_ERROR_TYPE
81+
WASI_NN_NAME(set_input)
82+
(WASI_NN_NAME(graph_execution_context) ctx, uint32_t index,
83+
WASI_NN_NAME(tensor) * tensor) WASI_NN_IMPORT("set_input");
7784

7885
/**
7986
* @brief Compute the inference on the given inputs.
8087
*
8188
* @param ctx Execution context.
8289
* @return wasi_nn_error Execution status.
8390
*/
84-
wasi_nn_error
85-
compute(graph_execution_context ctx) WASI_NN_IMPORT("compute");
91+
WASI_NN_ERROR_TYPE
92+
WASI_NN_NAME(compute)
93+
(WASI_NN_NAME(graph_execution_context) ctx) WASI_NN_IMPORT("compute");
8694

8795
/**
8896
* @brief Extract the outputs after inference.
@@ -97,14 +105,16 @@ compute(graph_execution_context ctx) WASI_NN_IMPORT("compute");
97105
* @return wasi_nn_error Execution status.
98106
*/
99107
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
100-
wasi_nn_error
101-
get_output(graph_execution_context ctx, uint32_t index,
102-
tensor_data output_tensor, uint32_t output_tensor_max_size,
103-
uint32_t *output_tensor_size) WASI_NN_IMPORT("get_output");
108+
WASI_NN_ERROR_TYPE
109+
WASI_NN_NAME(get_output)
110+
(WASI_NN_NAME(graph_execution_context) ctx, uint32_t index,
111+
WASI_NN_NAME(tensor_data) output_tensor, uint32_t output_tensor_max_size,
112+
uint32_t *output_tensor_size) WASI_NN_IMPORT("get_output");
104113
#else
105-
wasi_nn_error
106-
get_output(graph_execution_context ctx, uint32_t index,
107-
tensor_data output_tensor, uint32_t *output_tensor_size)
114+
WASI_NN_ERROR_TYPE
115+
WASI_NN_NAME(get_output)
116+
(graph_execution_context ctx, uint32_t index,
117+
WASI_NN_NAME(tensor_data) output_tensor, uint32_t *output_tensor_size)
108118
WASI_NN_IMPORT("get_output");
109119
#endif
110120

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

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@
1313
extern "C" {
1414
#endif
1515

16+
/* our host logic doesn't use any prefix. neither legacy wasi_nn.h does. */
17+
18+
#if !defined(__wasm__) || !defined(WASI_NN_NAME)
19+
#define WASI_NN_NAME(name) name
20+
#define WASI_NN_ERROR_NAME(name) name
21+
#define WASI_NN_TYPE_NAME(name) name
22+
#define WASI_NN_ENCODING_NAME(name) name
23+
#define WASI_NN_TARGET_NAME(name) name
24+
#define WASI_NN_ERROR_TYPE wasi_nn_error
25+
#else
26+
#define WASI_NN_ERROR_NAME(name) WASI_NN_NAME(error_##name)
27+
#define WASI_NN_TYPE_NAME(name) WASI_NN_NAME(type_##name)
28+
#define WASI_NN_ENCODING_NAME(name) WASI_NN_NAME(encoding_##name)
29+
#define WASI_NN_TARGET_NAME(name) WASI_NN_NAME(target_##name)
30+
#define WASI_NN_ERROR_TYPE WASI_NN_NAME(error);
31+
#endif
32+
1633
/**
1734
* ERRORS
1835
*
@@ -22,22 +39,22 @@ extern "C" {
2239
// https://github.com/WebAssembly/wasi-nn/blob/71320d95b8c6d43f9af7f44e18b1839db85d89b4/wasi-nn.witx#L5-L17
2340
// Error codes returned by functions in this API.
2441
typedef enum {
25-
success = 0,
26-
invalid_argument,
27-
invalid_encoding,
28-
missing_memory,
29-
busy,
30-
runtime_error,
31-
unsupported_operation,
32-
too_large,
33-
not_found,
42+
WASI_NN_ERROR_NAME(success) = 0,
43+
WASI_NN_ERROR_NAME(invalid_argument),
44+
WASI_NN_ERROR_NAME(invalid_encoding),
45+
WASI_NN_ERROR_NAME(missing_memory),
46+
WASI_NN_ERROR_NAME(busy),
47+
WASI_NN_ERROR_NAME(runtime_error),
48+
WASI_NN_ERROR_NAME(unsupported_operation),
49+
WASI_NN_ERROR_NAME(too_large),
50+
WASI_NN_ERROR_NAME(not_found),
3451

3552
// for WasmEdge-wasi-nn
36-
end_of_sequence = 100, // End of Sequence Found.
37-
context_full = 101, // Context Full.
38-
prompt_tool_long = 102, // Prompt Too Long.
39-
model_not_found = 103, // Model Not Found.
40-
} wasi_nn_error;
53+
WASI_NN_ERROR_NAME(end_of_sequence) = 100, // End of Sequence Found.
54+
WASI_NN_ERROR_NAME(context_full) = 101, // Context Full.
55+
WASI_NN_ERROR_NAME(prompt_tool_long) = 102, // Prompt Too Long.
56+
WASI_NN_ERROR_NAME(model_not_found) = 103, // Model Not Found.
57+
} WASI_NN_ERROR_TYPE;
4158

4259
/**
4360
* TENSOR
@@ -51,15 +68,27 @@ typedef enum {
5168
typedef struct {
5269
uint32_t *buf;
5370
uint32_t size;
54-
} tensor_dimensions;
71+
} WASI_NN_NAME(tensor_dimensions);
5572

5673
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
5774
// sync up with
5875
// https://github.com/WebAssembly/wasi-nn/blob/71320d95b8c6d43f9af7f44e18b1839db85d89b4/wasi-nn.witx#L19-L28
5976
// The type of the elements in a tensor.
60-
typedef enum { fp16 = 0, fp32, fp64, u8, i32, i64 } tensor_type;
77+
typedef enum {
78+
WASI_NN_TYPE_NAME(fp16) = 0,
79+
WASI_NN_TYPE_NAME(fp32),
80+
WASI_NN_TYPE_NAME(fp64),
81+
WASI_NN_TYPE_NAME(u8),
82+
WASI_NN_TYPE_NAME(i32),
83+
WASI_NN_TYPE_NAME(i64),
84+
} WASI_NN_NAME(tensor_type);
6185
#else
62-
typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
86+
typedef enum {
87+
WASI_NN_TYPE_NAME(fp16) = 0,
88+
WASI_NN_TYPE_NAME(fp32),
89+
WASI_NN_TYPE_NAME(up8),
90+
WASI_NN_TYPE_NAME(ip32),
91+
} WASI_NN_NAME(tensor_type);
6392
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
6493

6594
// The tensor data.
@@ -70,24 +99,24 @@ typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
7099
// 4-byte f32 elements would have a data array of length 16). Naturally, this
71100
// representation requires some knowledge of how to lay out data in
72101
// memory--e.g., using row-major ordering--and could perhaps be improved.
73-
typedef uint8_t *tensor_data;
102+
typedef uint8_t *WASI_NN_NAME(tensor_data);
74103

75104
// A tensor.
76105
typedef struct {
77106
// Describe the size of the tensor (e.g., 2x2x2x2 -> [2, 2, 2, 2]). To
78107
// represent a tensor containing a single value, use `[1]` for the tensor
79108
// dimensions.
80109
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0 && defined(__wasm__)
81-
tensor_dimensions dimensions;
110+
WASI_NN_NAME(tensor_dimensions) dimensions;
82111
#else
83-
tensor_dimensions *dimensions;
112+
WASI_NN_NAME(tensor_dimensions) * dimensions;
84113
#endif
85114
// Describe the type of element in the tensor (e.g., f32).
86115
uint8_t type;
87116
uint8_t _pad[3];
88117
// Contains the tensor data.
89-
tensor_data data;
90-
} tensor;
118+
WASI_NN_NAME(tensor_data) data;
119+
} WASI_NN_NAME(tensor);
91120

92121
/**
93122
* GRAPH
@@ -102,37 +131,41 @@ typedef struct {
102131
typedef struct {
103132
uint8_t *buf;
104133
uint32_t size;
105-
} graph_builder;
134+
} WASI_NN_NAME(graph_builder);
106135

107136
typedef struct {
108-
graph_builder *buf;
137+
WASI_NN_NAME(graph_builder) * buf;
109138
uint32_t size;
110-
} graph_builder_array;
139+
} WASI_NN_NAME(graph_builder_array);
111140

112141
// An execution graph for performing inference (i.e., a model).
113-
typedef uint32_t graph;
142+
typedef uint32_t WASI_NN_NAME(graph);
114143

115144
// sync up with
116145
// https://github.com/WebAssembly/wasi-nn/blob/main/wit/wasi-nn.wit#L75
117146
// Describes the encoding of the graph. This allows the API to be implemented by
118147
// various backends that encode (i.e., serialize) their graph IR with different
119148
// formats.
120149
typedef enum {
121-
openvino = 0,
122-
onnx,
123-
tensorflow,
124-
pytorch,
125-
tensorflowlite,
126-
ggml,
127-
autodetect,
128-
unknown_backend,
129-
} graph_encoding;
150+
WASI_NN_ENCODING_NAME(openvino) = 0,
151+
WASI_NN_ENCODING_NAME(onnx),
152+
WASI_NN_ENCODING_NAME(tensorflow),
153+
WASI_NN_ENCODING_NAME(pytorch),
154+
WASI_NN_ENCODING_NAME(tensorflowlite),
155+
WASI_NN_ENCODING_NAME(ggml),
156+
WASI_NN_ENCODING_NAME(autodetect),
157+
WASI_NN_ENCODING_NAME(unknown_backend),
158+
} WASI_NN_NAME(graph_encoding);
130159

131160
// Define where the graph should be executed.
132-
typedef enum execution_target { cpu = 0, gpu, tpu } execution_target;
161+
typedef enum WASI_NN_NAME(execution_target) {
162+
WASI_NN_TARGET_NAME(cpu) = 0,
163+
WASI_NN_TARGET_NAME(gpu),
164+
WASI_NN_TARGET_NAME(tpu),
165+
} WASI_NN_NAME(execution_target);
133166

134167
// Bind a `graph` to the input and output tensors for an inference.
135-
typedef uint32_t graph_execution_context;
168+
typedef uint32_t WASI_NN_NAME(graph_execution_context);
136169

137170
#ifdef __cplusplus
138171
}

wamr-wasi-extensions/samples/nn/app.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ print_result(const float *result, size_t sz)
9393
int
9494
main(int argc, char **argv)
9595
{
96-
wasi_nn_error nnret;
96+
wasi_ephemeral_nn_error nnret;
9797
int ret;
9898
void *xml;
9999
size_t xmlsz;
@@ -112,25 +112,27 @@ main(int argc, char **argv)
112112
exit(1);
113113
}
114114
/* note: openvino takes two buffers, namely IR and weights */
115-
graph_builder builders[2] = { {
116-
.buf = xml,
117-
.size = xmlsz,
118-
},
119-
{
120-
.buf = weights,
121-
.size = weightssz,
122-
} };
123-
graph g;
124-
nnret = load(builders, 2, openvino, cpu, &g);
115+
wasi_ephemeral_nn_graph_builder builders[2] = { {
116+
.buf = xml,
117+
.size = xmlsz,
118+
},
119+
{
120+
.buf = weights,
121+
.size = weightssz,
122+
} };
123+
wasi_ephemeral_nn_graph g;
124+
nnret =
125+
wasi_ephemeral_nn_load(builders, 2, wasi_ephemeral_nn_encoding_openvino,
126+
wasi_ephemeral_nn_target_cpu, &g);
125127
unmap_file(xml, xmlsz);
126128
unmap_file(weights, weightssz);
127-
if (nnret != success) {
129+
if (nnret != wasi_ephemeral_nn_error_success) {
128130
fprintf(stderr, "load failed with %d\n", (int)nnret);
129131
exit(1);
130132
}
131-
graph_execution_context ctx;
132-
nnret = init_execution_context(g, &ctx);
133-
if (nnret != success) {
133+
wasi_ephemeral_nn_graph_execution_context ctx;
134+
nnret = wasi_ephemeral_nn_init_execution_context(g, &ctx);
135+
if (nnret != wasi_ephemeral_nn_error_success) {
134136
fprintf(stderr, "init_execution_context failed with %d\n", (int)nnret);
135137
exit(1);
136138
}
@@ -142,26 +144,27 @@ main(int argc, char **argv)
142144
strerror(ret));
143145
exit(1);
144146
}
145-
tensor tensor = {
147+
wasi_ephemeral_nn_tensor tensor = {
146148
.dimensions = { .buf = (uint32_t[]){1, 3, 224, 224,}, .size = 4, },
147-
.type = fp32,
149+
.type = wasi_ephemeral_nn_type_fp32,
148150
.data = tensordata,
149151
};
150-
nnret = set_input(ctx, 0, &tensor);
152+
nnret = wasi_ephemeral_nn_set_input(ctx, 0, &tensor);
151153
unmap_file(tensordata, tensordatasz);
152-
if (nnret != success) {
154+
if (nnret != wasi_ephemeral_nn_error_success) {
153155
fprintf(stderr, "set_input failed with %d\n", (int)nnret);
154156
exit(1);
155157
}
156-
nnret = compute(ctx);
157-
if (nnret != success) {
158+
nnret = wasi_ephemeral_nn_compute(ctx);
159+
if (nnret != wasi_ephemeral_nn_error_success) {
158160
fprintf(stderr, "compute failed with %d\n", (int)nnret);
159161
exit(1);
160162
}
161163
float result[1001];
162164
uint32_t resultsz;
163-
nnret = get_output(ctx, 0, (void *)result, sizeof(result), &resultsz);
164-
if (nnret != success) {
165+
nnret = wasi_ephemeral_nn_get_output(ctx, 0, (void *)result, sizeof(result),
166+
&resultsz);
167+
if (nnret != wasi_ephemeral_nn_error_success) {
165168
fprintf(stderr, "get_output failed with %d\n", (int)nnret);
166169
exit(1);
167170
}

0 commit comments

Comments
 (0)