1313extern "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.
2441typedef 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 {
5168typedef 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.
76105typedef 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 {
102131typedef struct {
103132 uint8_t * buf ;
104133 uint32_t size ;
105- } graph_builder ;
134+ } WASI_NN_NAME ( graph_builder ) ;
106135
107136typedef 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.
120149typedef 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}
0 commit comments