Add wasi_ephemeral_nn module support (#3241)
Add `wasi_ephemeral_nn` module support with optional cmake variable, which was mentioned in #3229.
This commit is contained in:
@ -23,24 +23,47 @@ graph_builder_app_native(wasm_module_inst_t instance,
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* builder_array_wasm is consisted of {builder_wasm, size}
|
||||
*/
|
||||
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
error
|
||||
graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_wasm *builder_wasm, uint32_t size,
|
||||
graph_builder_array *builder_array)
|
||||
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
|
||||
error
|
||||
graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_array_wasm *builder_array_wasm,
|
||||
graph_builder_array *builder_array)
|
||||
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
||||
{
|
||||
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
#define array_size size
|
||||
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
|
||||
#define array_size builder_array_wasm->size
|
||||
|
||||
if (!wasm_runtime_validate_native_addr(
|
||||
instance, builder_array_wasm,
|
||||
(uint64)sizeof(graph_builder_array_wasm))) {
|
||||
NN_ERR_PRINTF("builder_array_wasm is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
||||
|
||||
NN_DBG_PRINTF("Graph builder array contains %d elements",
|
||||
builder_array_wasm->size);
|
||||
NN_DBG_PRINTF("Graph builder array contains %d elements", array_size);
|
||||
|
||||
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
if (!wasm_runtime_validate_native_addr(instance, builder_wasm,
|
||||
(uint64)array_size
|
||||
* sizeof(graph_builder_wasm))) {
|
||||
NN_ERR_PRINTF("builder_wasm is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
|
||||
if (!wasm_runtime_validate_app_addr(
|
||||
instance, (uint64)builder_array_wasm->buf_offset,
|
||||
(uint64)builder_array_wasm->size * sizeof(graph_builder_wasm))) {
|
||||
(uint64)array_size * sizeof(graph_builder_wasm))) {
|
||||
NN_ERR_PRINTF("builder_array_wasm->buf_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
@ -48,13 +71,14 @@ graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_wasm *builder_wasm =
|
||||
(graph_builder_wasm *)wasm_runtime_addr_app_to_native(
|
||||
instance, (uint64)builder_array_wasm->buf_offset);
|
||||
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
||||
|
||||
graph_builder *builder = (graph_builder *)wasm_runtime_malloc(
|
||||
builder_array_wasm->size * sizeof(graph_builder));
|
||||
array_size * sizeof(graph_builder));
|
||||
if (builder == NULL)
|
||||
return missing_memory;
|
||||
|
||||
for (uint32_t i = 0; i < builder_array_wasm->size; ++i) {
|
||||
for (uint32_t i = 0; i < array_size; ++i) {
|
||||
error res;
|
||||
if (success
|
||||
!= (res = graph_builder_app_native(instance, &builder_wasm[i],
|
||||
@ -68,23 +92,31 @@ graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
}
|
||||
|
||||
builder_array->buf = builder;
|
||||
builder_array->size = builder_array_wasm->size;
|
||||
builder_array->size = array_size;
|
||||
return success;
|
||||
#undef array_size
|
||||
}
|
||||
|
||||
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_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
#define data_size input_tensor_wasm->data_size
|
||||
#else
|
||||
#define data_size total_elements
|
||||
#endif
|
||||
|
||||
if (!wasm_runtime_validate_app_addr(instance,
|
||||
(uint64)input_tensor_wasm->data_offset,
|
||||
(uint64)total_elements)) {
|
||||
(uint64)data_size)) {
|
||||
NN_ERR_PRINTF("input_tensor_wasm->data_offset is invalid");
|
||||
return invalid_argument;
|
||||
}
|
||||
*data = (tensor_data)wasm_runtime_addr_app_to_native(
|
||||
instance, (uint64)input_tensor_wasm->data_offset);
|
||||
return success;
|
||||
#undef data_size
|
||||
}
|
||||
|
||||
static error
|
||||
@ -92,6 +124,9 @@ tensor_dimensions_app_native(wasm_module_inst_t instance,
|
||||
tensor_wasm *input_tensor_wasm,
|
||||
tensor_dimensions **dimensions)
|
||||
{
|
||||
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
tensor_dimensions_wasm *dimensions_wasm = &input_tensor_wasm->dimensions;
|
||||
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
|
||||
if (!wasm_runtime_validate_app_addr(
|
||||
instance, (uint64)input_tensor_wasm->dimensions_offset,
|
||||
(uint64)sizeof(tensor_dimensions_wasm))) {
|
||||
@ -102,6 +137,7 @@ tensor_dimensions_app_native(wasm_module_inst_t instance,
|
||||
tensor_dimensions_wasm *dimensions_wasm =
|
||||
(tensor_dimensions_wasm *)wasm_runtime_addr_app_to_native(
|
||||
instance, (uint64)input_tensor_wasm->dimensions_offset);
|
||||
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
||||
|
||||
if (!wasm_runtime_validate_app_addr(instance,
|
||||
(uint64)dimensions_wasm->buf_offset,
|
||||
|
||||
@ -34,15 +34,29 @@ typedef struct {
|
||||
} tensor_dimensions_wasm;
|
||||
|
||||
typedef struct {
|
||||
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
tensor_dimensions_wasm dimensions;
|
||||
tensor_type type;
|
||||
uint32_t data_offset;
|
||||
uint32_t data_size;
|
||||
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
|
||||
uint32_t dimensions_offset;
|
||||
tensor_type type;
|
||||
uint32_t data_offset;
|
||||
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
||||
} tensor_wasm;
|
||||
|
||||
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
error
|
||||
graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_wasm *builder_wasm, uint32_t size,
|
||||
graph_builder_array *builder_array);
|
||||
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
|
||||
error
|
||||
graph_builder_array_app_native(wasm_module_inst_t instance,
|
||||
graph_builder_array_wasm *builder,
|
||||
graph_builder_array *builder_native);
|
||||
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
|
||||
|
||||
error
|
||||
tensor_app_native(wasm_module_inst_t instance, tensor_wasm *input_tensor,
|
||||
|
||||
Reference in New Issue
Block a user