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
147109float *
148110run_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 }
0 commit comments