|
|
@@ -13,6 +13,23 @@
|
|
|
extern "C" {
|
|
|
#endif
|
|
|
|
|
|
+/* our host logic doesn't use any prefix. neither legacy wasi_nn.h does. */
|
|
|
+
|
|
|
+#if !defined(__wasm__) || !defined(WASI_NN_NAME)
|
|
|
+#define WASI_NN_NAME(name) name
|
|
|
+#define WASI_NN_ERROR_NAME(name) name
|
|
|
+#define WASI_NN_TYPE_NAME(name) name
|
|
|
+#define WASI_NN_ENCODING_NAME(name) name
|
|
|
+#define WASI_NN_TARGET_NAME(name) name
|
|
|
+#define WASI_NN_ERROR_TYPE wasi_nn_error
|
|
|
+#else
|
|
|
+#define WASI_NN_ERROR_NAME(name) WASI_NN_NAME(error_##name)
|
|
|
+#define WASI_NN_TYPE_NAME(name) WASI_NN_NAME(type_##name)
|
|
|
+#define WASI_NN_ENCODING_NAME(name) WASI_NN_NAME(encoding_##name)
|
|
|
+#define WASI_NN_TARGET_NAME(name) WASI_NN_NAME(target_##name)
|
|
|
+#define WASI_NN_ERROR_TYPE WASI_NN_NAME(error);
|
|
|
+#endif
|
|
|
+
|
|
|
/**
|
|
|
* ERRORS
|
|
|
*
|
|
|
@@ -22,22 +39,22 @@ extern "C" {
|
|
|
// https://github.com/WebAssembly/wasi-nn/blob/71320d95b8c6d43f9af7f44e18b1839db85d89b4/wasi-nn.witx#L5-L17
|
|
|
// Error codes returned by functions in this API.
|
|
|
typedef enum {
|
|
|
- success = 0,
|
|
|
- invalid_argument,
|
|
|
- invalid_encoding,
|
|
|
- missing_memory,
|
|
|
- busy,
|
|
|
- runtime_error,
|
|
|
- unsupported_operation,
|
|
|
- too_large,
|
|
|
- not_found,
|
|
|
+ WASI_NN_ERROR_NAME(success) = 0,
|
|
|
+ WASI_NN_ERROR_NAME(invalid_argument),
|
|
|
+ WASI_NN_ERROR_NAME(invalid_encoding),
|
|
|
+ WASI_NN_ERROR_NAME(missing_memory),
|
|
|
+ WASI_NN_ERROR_NAME(busy),
|
|
|
+ WASI_NN_ERROR_NAME(runtime_error),
|
|
|
+ WASI_NN_ERROR_NAME(unsupported_operation),
|
|
|
+ WASI_NN_ERROR_NAME(too_large),
|
|
|
+ WASI_NN_ERROR_NAME(not_found),
|
|
|
|
|
|
// for WasmEdge-wasi-nn
|
|
|
- end_of_sequence = 100, // End of Sequence Found.
|
|
|
- context_full = 101, // Context Full.
|
|
|
- prompt_tool_long = 102, // Prompt Too Long.
|
|
|
- model_not_found = 103, // Model Not Found.
|
|
|
-} wasi_nn_error;
|
|
|
+ WASI_NN_ERROR_NAME(end_of_sequence) = 100, // End of Sequence Found.
|
|
|
+ WASI_NN_ERROR_NAME(context_full) = 101, // Context Full.
|
|
|
+ WASI_NN_ERROR_NAME(prompt_tool_long) = 102, // Prompt Too Long.
|
|
|
+ WASI_NN_ERROR_NAME(model_not_found) = 103, // Model Not Found.
|
|
|
+} WASI_NN_ERROR_TYPE;
|
|
|
|
|
|
/**
|
|
|
* TENSOR
|
|
|
@@ -51,15 +68,27 @@ typedef enum {
|
|
|
typedef struct {
|
|
|
uint32_t *buf;
|
|
|
uint32_t size;
|
|
|
-} tensor_dimensions;
|
|
|
+} WASI_NN_NAME(tensor_dimensions);
|
|
|
|
|
|
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
|
|
// sync up with
|
|
|
// https://github.com/WebAssembly/wasi-nn/blob/71320d95b8c6d43f9af7f44e18b1839db85d89b4/wasi-nn.witx#L19-L28
|
|
|
// The type of the elements in a tensor.
|
|
|
-typedef enum { fp16 = 0, fp32, fp64, u8, i32, i64 } tensor_type;
|
|
|
+typedef enum {
|
|
|
+ WASI_NN_TYPE_NAME(fp16) = 0,
|
|
|
+ WASI_NN_TYPE_NAME(fp32),
|
|
|
+ WASI_NN_TYPE_NAME(fp64),
|
|
|
+ WASI_NN_TYPE_NAME(u8),
|
|
|
+ WASI_NN_TYPE_NAME(i32),
|
|
|
+ WASI_NN_TYPE_NAME(i64),
|
|
|
+} WASI_NN_NAME(tensor_type);
|
|
|
#else
|
|
|
-typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
|
|
|
+typedef enum {
|
|
|
+ WASI_NN_TYPE_NAME(fp16) = 0,
|
|
|
+ WASI_NN_TYPE_NAME(fp32),
|
|
|
+ WASI_NN_TYPE_NAME(up8),
|
|
|
+ WASI_NN_TYPE_NAME(ip32),
|
|
|
+} WASI_NN_NAME(tensor_type);
|
|
|
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
|
|
|
|
|
// The tensor data.
|
|
|
@@ -70,7 +99,7 @@ typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
|
|
|
// 4-byte f32 elements would have a data array of length 16). Naturally, this
|
|
|
// representation requires some knowledge of how to lay out data in
|
|
|
// memory--e.g., using row-major ordering--and could perhaps be improved.
|
|
|
-typedef uint8_t *tensor_data;
|
|
|
+typedef uint8_t *WASI_NN_NAME(tensor_data);
|
|
|
|
|
|
// A tensor.
|
|
|
typedef struct {
|
|
|
@@ -78,16 +107,16 @@ typedef struct {
|
|
|
// represent a tensor containing a single value, use `[1]` for the tensor
|
|
|
// dimensions.
|
|
|
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0 && defined(__wasm__)
|
|
|
- tensor_dimensions dimensions;
|
|
|
+ WASI_NN_NAME(tensor_dimensions) dimensions;
|
|
|
#else
|
|
|
- tensor_dimensions *dimensions;
|
|
|
+ WASI_NN_NAME(tensor_dimensions) * dimensions;
|
|
|
#endif
|
|
|
// Describe the type of element in the tensor (e.g., f32).
|
|
|
uint8_t type;
|
|
|
uint8_t _pad[3];
|
|
|
// Contains the tensor data.
|
|
|
- tensor_data data;
|
|
|
-} tensor;
|
|
|
+ WASI_NN_NAME(tensor_data) data;
|
|
|
+} WASI_NN_NAME(tensor);
|
|
|
|
|
|
/**
|
|
|
* GRAPH
|
|
|
@@ -102,15 +131,15 @@ typedef struct {
|
|
|
typedef struct {
|
|
|
uint8_t *buf;
|
|
|
uint32_t size;
|
|
|
-} graph_builder;
|
|
|
+} WASI_NN_NAME(graph_builder);
|
|
|
|
|
|
typedef struct {
|
|
|
- graph_builder *buf;
|
|
|
+ WASI_NN_NAME(graph_builder) * buf;
|
|
|
uint32_t size;
|
|
|
-} graph_builder_array;
|
|
|
+} WASI_NN_NAME(graph_builder_array);
|
|
|
|
|
|
// An execution graph for performing inference (i.e., a model).
|
|
|
-typedef uint32_t graph;
|
|
|
+typedef uint32_t WASI_NN_NAME(graph);
|
|
|
|
|
|
// sync up with
|
|
|
// https://github.com/WebAssembly/wasi-nn/blob/main/wit/wasi-nn.wit#L75
|
|
|
@@ -118,21 +147,25 @@ typedef uint32_t graph;
|
|
|
// various backends that encode (i.e., serialize) their graph IR with different
|
|
|
// formats.
|
|
|
typedef enum {
|
|
|
- openvino = 0,
|
|
|
- onnx,
|
|
|
- tensorflow,
|
|
|
- pytorch,
|
|
|
- tensorflowlite,
|
|
|
- ggml,
|
|
|
- autodetect,
|
|
|
- unknown_backend,
|
|
|
-} graph_encoding;
|
|
|
+ WASI_NN_ENCODING_NAME(openvino) = 0,
|
|
|
+ WASI_NN_ENCODING_NAME(onnx),
|
|
|
+ WASI_NN_ENCODING_NAME(tensorflow),
|
|
|
+ WASI_NN_ENCODING_NAME(pytorch),
|
|
|
+ WASI_NN_ENCODING_NAME(tensorflowlite),
|
|
|
+ WASI_NN_ENCODING_NAME(ggml),
|
|
|
+ WASI_NN_ENCODING_NAME(autodetect),
|
|
|
+ WASI_NN_ENCODING_NAME(unknown_backend),
|
|
|
+} WASI_NN_NAME(graph_encoding);
|
|
|
|
|
|
// Define where the graph should be executed.
|
|
|
-typedef enum execution_target { cpu = 0, gpu, tpu } execution_target;
|
|
|
+typedef enum WASI_NN_NAME(execution_target) {
|
|
|
+ WASI_NN_TARGET_NAME(cpu) = 0,
|
|
|
+ WASI_NN_TARGET_NAME(gpu),
|
|
|
+ WASI_NN_TARGET_NAME(tpu),
|
|
|
+} WASI_NN_NAME(execution_target);
|
|
|
|
|
|
// Bind a `graph` to the input and output tensors for an inference.
|
|
|
-typedef uint32_t graph_execution_context;
|
|
|
+typedef uint32_t WASI_NN_NAME(graph_execution_context);
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
}
|