Refactor WASI-NN to simplify the support for multiple frameworks (#1834)
- Reorganize the library structure
- Use the latest version of `wasi-nn` wit (Oct 25, 2022):
0f77c48ec1/wasi-nn.wit.md
- Split logic that converts WASM structs to native structs in a separate file
- Simplify addition of new frameworks
This commit is contained in:
63
core/iwasm/libraries/wasi-nn/src/utils/logger.h
Normal file
63
core/iwasm/libraries/wasi-nn/src/utils/logger.h
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef WASI_NN_LOGGER_H
|
||||
#define WASI_NN_LOGGER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define __FILENAME__ \
|
||||
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
||||
|
||||
/* Disable a level by removing the define */
|
||||
#define ENABLE_ERR_LOG
|
||||
#define ENABLE_WARN_LOG
|
||||
#define ENABLE_DBG_LOG
|
||||
#define ENABLE_INFO_LOG
|
||||
|
||||
// Definition of the levels
|
||||
#ifdef ENABLE_ERR_LOG
|
||||
#define NN_ERR_PRINTF(fmt, ...) \
|
||||
do { \
|
||||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
#else
|
||||
#define NN_ERR_PRINTF(fmt, ...)
|
||||
#endif
|
||||
#ifdef ENABLE_WARN_LOG
|
||||
#define NN_WARN_PRINTF(fmt, ...) \
|
||||
do { \
|
||||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
#else
|
||||
#define NN_WARN_PRINTF(fmt, ...)
|
||||
#endif
|
||||
#ifdef ENABLE_DBG_LOG
|
||||
#define NN_DBG_PRINTF(fmt, ...) \
|
||||
do { \
|
||||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
#else
|
||||
#define NN_DBG_PRINTF(fmt, ...)
|
||||
#endif
|
||||
#ifdef ENABLE_INFO_LOG
|
||||
#define NN_INFO_PRINTF(fmt, ...) \
|
||||
do { \
|
||||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
#else
|
||||
#define NN_INFO_PRINTF(fmt, ...)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
163
core/iwasm/libraries/wasi-nn/src/utils/wasi_nn_app_native.c
Normal file
163
core/iwasm/libraries/wasi-nn/src/utils/wasi_nn_app_native.c
Normal file
@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "wasi_nn_app_native.h"
|
||||
|
||||
static error
|
||||
graph_builder_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_wasm *builder_wasm,
|
||||
graph_builder *builder)
|
||||
{
|
||||
if (!wasm_runtime_validate_app_addr(instance, builder_wasm->buf_offset,
|
||||
builder_wasm->size * sizeof(uint8_t))) {
|
||||
NN_ERR_PRINTF("builder_wasm->buf_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
|
||||
builder->buf = (uint8_t *)wasm_runtime_addr_app_to_native(
|
||||
instance, builder_wasm->buf_offset);
|
||||
builder->size = builder_wasm->size;
|
||||
return success;
|
||||
}
|
||||
|
||||
error
|
||||
graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_array_wasm *builder_array_wasm,
|
||||
graph_builder_array *builder_array)
|
||||
{
|
||||
if (!wasm_runtime_validate_native_addr(instance, builder_array_wasm,
|
||||
sizeof(graph_builder_array_wasm))) {
|
||||
NN_ERR_PRINTF("builder_array_wasm is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
|
||||
NN_DBG_PRINTF("Graph builder array contains %d elements",
|
||||
builder_array_wasm->size);
|
||||
|
||||
if (!wasm_runtime_validate_app_addr(
|
||||
instance, builder_array_wasm->buf_offset,
|
||||
builder_array_wasm->size * sizeof(graph_builder_wasm))) {
|
||||
NN_ERR_PRINTF("builder_array_wasm->buf_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
|
||||
graph_builder_wasm *builder_wasm =
|
||||
(graph_builder_wasm *)wasm_runtime_addr_app_to_native(
|
||||
instance, builder_array_wasm->buf_offset);
|
||||
|
||||
graph_builder *builder = (graph_builder *)wasm_runtime_malloc(
|
||||
builder_array_wasm->size * sizeof(graph_builder));
|
||||
if (builder == NULL)
|
||||
return missing_memory;
|
||||
|
||||
for (uint32_t i = 0; i < builder_array_wasm->size; ++i) {
|
||||
error res;
|
||||
if (success
|
||||
!= (res = graph_builder_app_native(instance, &builder_wasm[i],
|
||||
&builder[i]))) {
|
||||
wasm_runtime_free(builder);
|
||||
return res;
|
||||
}
|
||||
|
||||
NN_DBG_PRINTF("Graph builder %d contains %d elements", i,
|
||||
builder->size);
|
||||
}
|
||||
|
||||
builder_array->buf = builder;
|
||||
builder_array->size = builder_array_wasm->size;
|
||||
return success;
|
||||
}
|
||||
|
||||
static error
|
||||
tensor_data_app_native(wasm_module_inst_t instance, uint32_t total_elements,
|
||||
tensor_wasm *input_tensor_wasm, tensor_data *data)
|
||||
{
|
||||
if (!wasm_runtime_validate_app_addr(
|
||||
instance, input_tensor_wasm->data_offset, total_elements)) {
|
||||
NN_ERR_PRINTF("input_tensor_wasm->data_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
*data = (tensor_data)wasm_runtime_addr_app_to_native(
|
||||
instance, input_tensor_wasm->data_offset);
|
||||
return success;
|
||||
}
|
||||
|
||||
static error
|
||||
tensor_dimensions_app_native(wasm_module_inst_t instance,
|
||||
tensor_wasm *input_tensor_wasm,
|
||||
tensor_dimensions **dimensions)
|
||||
{
|
||||
if (!wasm_runtime_validate_app_addr(instance,
|
||||
input_tensor_wasm->dimensions_offset,
|
||||
sizeof(tensor_dimensions_wasm))) {
|
||||
NN_ERR_PRINTF("input_tensor_wasm->dimensions_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
|
||||
tensor_dimensions_wasm *dimensions_wasm =
|
||||
(tensor_dimensions_wasm *)wasm_runtime_addr_app_to_native(
|
||||
instance, input_tensor_wasm->dimensions_offset);
|
||||
|
||||
if (!wasm_runtime_validate_app_addr(instance, dimensions_wasm->buf_offset,
|
||||
sizeof(tensor_dimensions))) {
|
||||
NN_ERR_PRINTF("dimensions_wasm->buf_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
|
||||
*dimensions =
|
||||
(tensor_dimensions *)wasm_runtime_malloc(sizeof(tensor_dimensions));
|
||||
if (dimensions == NULL)
|
||||
return missing_memory;
|
||||
|
||||
(*dimensions)->size = dimensions_wasm->size;
|
||||
(*dimensions)->buf = (uint32_t *)wasm_runtime_addr_app_to_native(
|
||||
instance, dimensions_wasm->buf_offset);
|
||||
|
||||
NN_DBG_PRINTF("Number of dimensions: %d", (*dimensions)->size);
|
||||
return success;
|
||||
}
|
||||
|
||||
error
|
||||
tensor_app_native(wasm_module_inst_t instance, tensor_wasm *input_tensor_wasm,
|
||||
tensor *input_tensor)
|
||||
{
|
||||
NN_DBG_PRINTF("Converting tensor_wasm to tensor");
|
||||
if (!wasm_runtime_validate_native_addr(instance, input_tensor_wasm,
|
||||
sizeof(tensor_wasm))) {
|
||||
NN_ERR_PRINTF("input_tensor_wasm is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
|
||||
error res;
|
||||
|
||||
tensor_dimensions *dimensions = NULL;
|
||||
if (success
|
||||
!= (res = tensor_dimensions_app_native(instance, input_tensor_wasm,
|
||||
&dimensions))) {
|
||||
NN_ERR_PRINTF("error when parsing dimensions");
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t total_elements = 1;
|
||||
for (uint32_t i = 0; i < dimensions->size; ++i) {
|
||||
total_elements *= dimensions->buf[i];
|
||||
NN_DBG_PRINTF("Dimension %d: %d", i, dimensions->buf[i]);
|
||||
}
|
||||
NN_DBG_PRINTF("Tensor type: %d", input_tensor_wasm->type);
|
||||
NN_DBG_PRINTF("Total number of elements: %d", total_elements);
|
||||
|
||||
tensor_data data = NULL;
|
||||
if (success
|
||||
!= (res = tensor_data_app_native(instance, total_elements,
|
||||
input_tensor_wasm, &data))) {
|
||||
wasm_runtime_free(dimensions);
|
||||
return res;
|
||||
}
|
||||
|
||||
input_tensor->type = input_tensor_wasm->type;
|
||||
input_tensor->dimensions = dimensions;
|
||||
input_tensor->data = data;
|
||||
return success;
|
||||
}
|
||||
51
core/iwasm/libraries/wasi-nn/src/utils/wasi_nn_app_native.h
Normal file
51
core/iwasm/libraries/wasi-nn/src/utils/wasi_nn_app_native.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef WASI_NN_APP_NATIVE
|
||||
#define WASI_NN_APP_NATIVE
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wasi_nn.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t buf_offset;
|
||||
uint32_t size;
|
||||
} graph_builder_wasm;
|
||||
|
||||
typedef struct {
|
||||
uint32_t buf_offset;
|
||||
uint32_t size;
|
||||
} graph_builder_array_wasm;
|
||||
|
||||
typedef struct {
|
||||
uint32_t buf_offset;
|
||||
uint32_t size;
|
||||
} tensor_dimensions_wasm;
|
||||
|
||||
typedef struct {
|
||||
uint32_t dimensions_offset;
|
||||
tensor_type type;
|
||||
uint32_t data_offset;
|
||||
} tensor_wasm;
|
||||
|
||||
error
|
||||
graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_array_wasm *builder,
|
||||
graph_builder_array *builder_native);
|
||||
|
||||
error
|
||||
tensor_app_native(wasm_module_inst_t instance, tensor_wasm *input_tensor,
|
||||
tensor *input_tensor_native);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user