Skip to content

Commit 4aa5d19

Browse files
committed
use wasi_ephemeral_nn_* prefix in all test files.
1 parent 7822544 commit 4aa5d19

File tree

8 files changed

+74
-109
lines changed

8 files changed

+74
-109
lines changed

core/iwasm/common/wasm_runtime_common.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,10 +1465,6 @@ wasm_runtime_check_and_update_last_used_shared_heap(
14651465
#endif
14661466

14671467
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
1468-
WASM_RUNTIME_API_EXTERN void
1469-
wasm_runtime_set_wasi_nn_registry(WASMModuleInstanceCommon *module_inst,
1470-
WASINNRegistry *wasi_ctx);
1471-
14721468
WASM_RUNTIME_API_EXTERN WASINNRegistry *
14731469
wasm_runtime_get_wasi_nn_registry(WASMModuleInstanceCommon *module_inst_comm);
14741470

core/iwasm/include/wasm_export.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,19 +797,24 @@ wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
797797
struct InstantiationArgs2 *p, const char *ns_lookup_pool[],
798798
uint32_t ns_lookup_pool_size);
799799

800-
WASM_RUNTIME_API_EXTERN struct WASINNRegistry *
801-
wasm_runtime_get_wasi_nn_registry(const wasm_module_inst_t module_inst);
802-
803-
WASM_RUNTIME_API_EXTERN void
804-
wasm_runtime_set_wasi_nn_registry(wasm_module_inst_t module_inst,
805-
struct WASINNRegistry *wasi_ctx);
806-
807800
WASM_RUNTIME_API_EXTERN int
808801
wasm_runtime_wasi_nn_registry_create(struct WASINNRegistry **registryp);
809802

810803
WASM_RUNTIME_API_EXTERN void
811804
wasm_runtime_wasi_nn_registry_destroy(struct WASINNRegistry *registry);
812805

806+
WASM_RUNTIME_API_EXTERN void
807+
wasm_runtime_instantiation_args_set_wasi_nn_registry(
808+
struct InstantiationArgs2 *p, WASINNRegistry *registry);
809+
810+
WASM_RUNTIME_API_EXTERN bool
811+
wasm_runtime_wasi_nn_registry_set_args(WASINNRegistry *registry,
812+
const char **model_names,
813+
const uint32_t **encoding,
814+
const uint32_t **target,
815+
uint32_t n_graphs,
816+
const char **graph_paths);
817+
813818
/**
814819
* Instantiate a WASM module, with specified instantiation arguments
815820
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
#define WASI_NN_NAME(name) wasi_ephemeral_nn_##name
88

99
#include "wasi_nn.h"
10+
11+
#undef WASM_ENABLE_WASI_EPHEMERAL_NN
12+
#undef WASI_NN_NAME

core/iwasm/libraries/wasi-nn/test/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ python3 sum.py
4444
cd ${CURR_PATH}
4545
/opt/wasi-sdk/bin/clang \
4646
--target=wasm32-wasi \
47+
-DWASM_ENABLE_WASI_NN=1 \
48+
-DWASM_ENABLE_WASI_EPHEMERAL_NN=1 \
4749
-DNN_LOG_LEVEL=1 \
4850
-Wl,--allow-undefined \
4951
-I../include -I../src/utils \

core/iwasm/libraries/wasi-nn/test/utils.c

Lines changed: 42 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@
88
#include <stdio.h>
99
#include <stdlib.h>
1010

11-
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
12-
typedef wasi_ephemeral_nn_error wasi_nn_error_t;
13-
#else
14-
typedef wasi_nn_error wasi_nn_error_t;
15-
#endif
16-
17-
wasi_nn_error_t
18-
wasm_load(char *model_name, WASI_NN_NAME(graph) * g,
19-
WASI_NN_NAME(execution_target) target)
11+
wasi_ephemeral_nn_error
12+
wasm_load(char *model_name, wasi_ephemeral_nn_graph *g,
13+
wasi_ephemeral_nn_execution_target target)
2014
{
2115
FILE *pFile = fopen(model_name, "r");
2216
if (pFile == NULL)
23-
return WASI_NN_ERROR_NAME(invalid_argument);
17+
return wasi_ephemeral_nn_error_invalid_argument;
2418

2519
uint8_t *buffer;
2620
size_t result;
@@ -29,80 +23,60 @@ wasm_load(char *model_name, WASI_NN_NAME(graph) * g,
2923
buffer = (uint8_t *)malloc(sizeof(uint8_t) * MAX_MODEL_SIZE);
3024
if (buffer == NULL) {
3125
fclose(pFile);
32-
return WASI_NN_ERROR_NAME(too_large);
26+
return wasi_ephemeral_nn_error_too_large;
3327
}
3428

3529
result = fread(buffer, 1, MAX_MODEL_SIZE, pFile);
3630
if (result <= 0) {
3731
fclose(pFile);
3832
free(buffer);
39-
return WASI_NN_ERROR_NAME(too_large);
33+
return wasi_ephemeral_nn_error_too_large;
4034
}
4135

42-
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
43-
WASI_NN_NAME(graph_builder) arr;
36+
wasi_ephemeral_nn_graph_builder arr;
4437

4538
arr.buf = buffer;
4639
arr.size = result;
4740

48-
wasi_nn_error_t res = WASI_NN_NAME(load)(
49-
&arr, result, WASI_NN_ENCODING_NAME(tensorflowlite), target, g);
50-
#else
51-
WASI_NN_NAME(graph_builder_array) arr;
52-
53-
arr.size = 1;
54-
arr.buf = (WASI_NN_NAME(graph_builder) *)malloc(
55-
sizeof(WASI_NN_NAME(graph_builder)));
56-
if (arr.buf == NULL) {
57-
fclose(pFile);
58-
free(buffer);
59-
return too_large;
60-
}
61-
62-
arr.buf[0].size = result;
63-
arr.buf[0].buf = buffer;
64-
65-
wasi_nn_error_t res = WASI_NN_NAME(load)(
66-
&arr, WASI_NN_ENCODING_NAME(tensorflowlite), target, g);
67-
#endif
41+
wasi_ephemeral_nn_error res = wasi_ephemeral_nn_load(
42+
&arr, result, wasi_ephemeral_nn_encoding_tensorflowlite, target, g);
6843

6944
fclose(pFile);
7045
free(buffer);
7146
free(arr.buf);
7247
return res;
7348
}
7449

75-
wasi_nn_error_t
76-
wasm_load_by_name(const char *model_name, WASI_NN_NAME(graph) * g)
50+
wasi_ephemeral_nn_error
51+
wasm_load_by_name(const char *model_name, wasi_ephemeral_nn_graph *g)
7752
{
78-
wasi_nn_error_t res =
79-
WASI_NN_NAME(load_by_name)(model_name, strlen(model_name), g);
53+
wasi_ephemeral_nn_error res =
54+
wasi_ephemeral_nn_load_by_name(model_name, strlen(model_name), g);
8055
return res;
8156
}
8257

83-
wasi_nn_error_t
84-
wasm_init_execution_context(WASI_NN_NAME(graph) g,
85-
WASI_NN_NAME(graph_execution_context) * ctx)
58+
wasi_ephemeral_nn_error
59+
wasm_init_execution_context(wasi_ephemeral_nn_graph g,
60+
wasi_ephemeral_nn_graph_execution_context *ctx)
8661
{
87-
return WASI_NN_NAME(init_execution_context)(g, ctx);
62+
return wasi_ephemeral_nn_init_execution_context(g, ctx);
8863
}
8964

90-
wasi_nn_error_t
91-
wasm_set_input(WASI_NN_NAME(graph_execution_context) ctx, float *input_tensor,
92-
uint32_t *dim)
65+
wasi_ephemeral_nn_error
66+
wasm_set_input(wasi_ephemeral_nn_graph_execution_context ctx,
67+
float *input_tensor, uint32_t *dim)
9368
{
94-
WASI_NN_NAME(tensor_dimensions) dims;
69+
wasi_ephemeral_nn_tensor_dimensions dims;
9570
dims.size = INPUT_TENSOR_DIMS;
9671
dims.buf = (uint32_t *)malloc(dims.size * sizeof(uint32_t));
9772
if (dims.buf == NULL)
98-
return WASI_NN_ERROR_NAME(too_large);
73+
return wasi_ephemeral_nn_error_too_large;
9974

100-
WASI_NN_NAME(tensor) tensor;
101-
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
75+
wasi_ephemeral_nn_tensor tensor;
10276
tensor.dimensions = dims;
10377
for (int i = 0; i < tensor.dimensions.size; ++i)
10478
tensor.dimensions.buf[i] = dim[i];
105-
tensor.type = WASI_NN_TYPE_NAME(fp32);
79+
tensor.type = wasi_ephemeral_nn_type_fp32;
10680
tensor.data.buf = (uint8_t *)input_tensor;
10781

10882
uint32_t tmp_size = 1;
@@ -111,71 +85,60 @@ wasm_set_input(WASI_NN_NAME(graph_execution_context) ctx, float *input_tensor,
11185
tmp_size *= dim[i];
11286

11387
tensor.data.size = (tmp_size * sizeof(float));
114-
#else
115-
tensor.dimensions = &dims;
116-
for (int i = 0; i < tensor.dimensions->size; ++i)
117-
tensor.dimensions->buf[i] = dim[i];
118-
tensor.type = WASI_NN_TYPE_NAME(fp32);
119-
tensor.data = (uint8_t *)input_tensor;
120-
#endif
12188

122-
wasi_nn_error_t err = WASI_NN_NAME(set_input)(ctx, 0, &tensor);
89+
wasi_ephemeral_nn_error err = wasi_ephemeral_nn_set_input(ctx, 0, &tensor);
12390

12491
free(dims.buf);
12592
return err;
12693
}
12794

128-
wasi_nn_error_t
129-
wasm_compute(WASI_NN_NAME(graph_execution_context) ctx)
95+
wasi_ephemeral_nn_error
96+
wasm_compute(wasi_ephemeral_nn_graph_execution_context ctx)
13097
{
131-
return WASI_NN_NAME(compute)(ctx);
98+
return wasi_ephemeral_nn_compute(ctx);
13299
}
133100

134-
wasi_nn_error_t
135-
wasm_get_output(WASI_NN_NAME(graph_execution_context) ctx, uint32_t index,
101+
wasi_ephemeral_nn_error
102+
wasm_get_output(wasi_ephemeral_nn_graph_execution_context ctx, uint32_t index,
136103
float *out_tensor, uint32_t *out_size)
137104
{
138-
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
139-
return WASI_NN_NAME(get_output)(ctx, index, (uint8_t *)out_tensor,
140-
MAX_OUTPUT_TENSOR_SIZE, out_size);
141-
#else
142-
return WASI_NN_NAME(get_output)(ctx, index, (uint8_t *)out_tensor,
143-
out_size);
144-
#endif
105+
return wasi_ephemeral_nn_get_output(ctx, index, (uint8_t *)out_tensor,
106+
MAX_OUTPUT_TENSOR_SIZE, out_size);
145107
}
146108

147109
float *
148110
run_inference(float *input, uint32_t *input_size, uint32_t *output_size,
149111
char *model_name, uint32_t num_output_tensors)
150112
{
151-
WASI_NN_NAME(graph) graph;
113+
wasi_ephemeral_nn_graph graph;
152114

153-
wasi_nn_error_t res = wasm_load_by_name(model_name, &graph);
115+
wasi_ephemeral_nn_error res = wasm_load_by_name(model_name, &graph);
154116

155-
if (res == WASI_NN_ERROR_NAME(not_found)) {
117+
if (res == wasi_ephemeral_nn_error_not_found) {
156118
NN_INFO_PRINTF("Model %s is not loaded, you should pass its path "
157119
"through --wasi-nn-graph",
158120
model_name);
159121
return NULL;
160122
}
161-
else if (res != WASI_NN_ERROR_NAME(success)) {
123+
else if (res != wasi_ephemeral_nn_error_success) {
162124
NN_ERR_PRINTF("Error when loading model.");
163125
exit(1);
164126
}
165127

166-
WASI_NN_NAME(graph_execution_context) ctx;
128+
wasi_ephemeral_nn_graph_execution_context ctx;
167129
if (wasm_init_execution_context(graph, &ctx)
168-
!= WASI_NN_ERROR_NAME(success)) {
130+
!= wasi_ephemeral_nn_error_success) {
169131
NN_ERR_PRINTF("Error when initialixing execution context.");
170132
exit(1);
171133
}
172134

173-
if (wasm_set_input(ctx, input, input_size) != WASI_NN_ERROR_NAME(success)) {
135+
if (wasm_set_input(ctx, input, input_size)
136+
!= wasi_ephemeral_nn_error_success) {
174137
NN_ERR_PRINTF("Error when setting input tensor.");
175138
exit(1);
176139
}
177140

178-
if (wasm_compute(ctx) != WASI_NN_ERROR_NAME(success)) {
141+
if (wasm_compute(ctx) != wasi_ephemeral_nn_error_success) {
179142
NN_ERR_PRINTF("Error when running inference.");
180143
exit(1);
181144
}
@@ -190,7 +153,7 @@ run_inference(float *input, uint32_t *input_size, uint32_t *output_size,
190153
for (int i = 0; i < num_output_tensors; ++i) {
191154
*output_size = MAX_OUTPUT_TENSOR_SIZE - *output_size;
192155
if (wasm_get_output(ctx, i, &out_tensor[offset], output_size)
193-
!= WASI_NN_ERROR_NAME(success)) {
156+
!= wasi_ephemeral_nn_error_success) {
194157
NN_ERR_PRINTF("Error when getting index %d.", i);
195158
break;
196159
}

core/iwasm/libraries/wasi-nn/test/utils.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,25 @@ typedef struct {
2626
uint32_t elements;
2727
} input_info;
2828

29-
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
30-
typedef wasi_ephemeral_nn_error wasi_nn_error_t;
31-
#else
32-
typedef wasi_nn_error wasi_nn_error_t;
33-
#endif
34-
3529
/* wasi-nn wrappers */
3630

37-
wasi_nn_error_t
38-
wasm_load(char *model_name, WASI_NN_NAME(graph) * g,
39-
WASI_NN_NAME(execution_target) target);
31+
wasi_ephemeral_nn_error
32+
wasm_load(char *model_name, wasi_ephemeral_nn_graph *g,
33+
wasi_ephemeral_nn_execution_target target);
4034

41-
wasi_nn_error_t wasm_init_execution_context(
42-
WASI_NN_NAME(graph) g, WASI_NN_NAME(graph_execution_context) * ctx);
35+
wasi_ephemeral_nn_error
36+
wasm_init_execution_context(wasi_ephemeral_nn_graph g,
37+
wasi_ephemeral_nn_graph_execution_context *ctx);
4338

44-
wasi_nn_error_t
45-
wasm_set_input(WASI_NN_NAME(graph_execution_context) ctx, float *input_tensor,
46-
uint32_t *dim);
39+
wasi_ephemeral_nn_error
40+
wasm_set_input(wasi_ephemeral_nn_graph_execution_context ctx,
41+
float *input_tensor, uint32_t *dim);
4742

48-
wasi_nn_error_t wasm_compute(WASI_NN_NAME(graph_execution_context) ctx);
43+
wasi_ephemeral_nn_error
44+
wasm_compute(wasi_ephemeral_nn_graph_execution_context ctx);
4945

50-
wasi_nn_error_t
51-
wasm_get_output(WASI_NN_NAME(graph_execution_context) ctx, uint32_t index,
46+
wasi_ephemeral_nn_error
47+
wasm_get_output(wasi_ephemeral_nn_graph_execution_context ctx, uint32_t index,
5248
float *out_tensor, uint32_t *out_size);
5349

5450
/* Utils */

product-mini/platforms/common/libc_wasi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ wasi_nn_parse(char **argv, wasi_nn_parse_context_t *ctx)
300300
}
301301

302302
static void
303-
wasi_nn_set_init_args(struct InstantiationArgs2 *args,
303+
wasi_nn_set_inst_args(struct InstantiationArgs2 *args,
304304
struct WASINNRegistry *nn_registry,
305305
wasi_nn_parse_context_t *ctx)
306306
{

product-mini/platforms/posix/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ main(int argc, char *argv[])
10191019

10201020
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
10211021
wasm_runtime_wasi_nn_registry_create(&nn_registry);
1022-
wasi_nn_set_init_args(inst_args, nn_registry, &wasi_nn_parse_ctx);
1022+
wasi_nn_set_inst_args(inst_args, nn_registry, &wasi_nn_parse_ctx);
10231023
wasm_runtime_set_wasi_nn_registry(wasm_module_inst, nn_registry);
10241024
#endif
10251025
wasm_runtime_instantiation_args_destroy(inst_args);

0 commit comments

Comments
 (0)