Skip to content

Commit 9eed668

Browse files
authored
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): https://github.com/WebAssembly/wasi-nn/blob/0f77c48ec195748990ff67928a4b3eef5f16c2de/wasi-nn.wit.md - Split logic that converts WASM structs to native structs in a separate file - Simplify addition of new frameworks
1 parent 965edff commit 9eed668

24 files changed

+911
-504
lines changed

build-scripts/runtime_lib.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ if (WAMR_BUILD_LIB_PTHREAD_SEMAPHORE EQUAL 1)
9696
endif ()
9797

9898
if (WAMR_BUILD_WASI_NN EQUAL 1)
99-
execute_process(COMMAND ${WAMR_ROOT_DIR}/core/deps/install_tensorflow.sh
100-
RESULT_VARIABLE TENSORFLOW_RESULT
101-
)
99+
if (NOT EXISTS "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
100+
execute_process(COMMAND ${WAMR_ROOT_DIR}/core/deps/install_tensorflow.sh
101+
RESULT_VARIABLE TENSORFLOW_RESULT
102+
)
103+
else ()
104+
message("Tensorflow is already downloaded.")
105+
endif()
102106
set(TENSORFLOW_SOURCE_DIR "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
103107
include_directories (${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include)
104108
include_directories (${TENSORFLOW_SOURCE_DIR})

core/iwasm/aot/aot_runtime.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,17 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, uint32 stack_size,
10831083
}
10841084
#endif
10851085

1086+
#if WASM_ENABLE_WASI_NN != 0
1087+
if (!is_sub_inst) {
1088+
if (!(((AOTModuleInstanceExtra *)module_inst->e)->wasi_nn_ctx =
1089+
wasi_nn_initialize())) {
1090+
set_error_buf(error_buf, error_buf_size,
1091+
"wasi nn initialization failed");
1092+
goto fail;
1093+
}
1094+
}
1095+
#endif
1096+
10861097
/* Initialize the thread related data */
10871098
if (stack_size == 0)
10881099
stack_size = DEFAULT_WASM_STACK_SIZE;
@@ -1194,6 +1205,15 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
11941205
wasm_runtime_free(
11951206
((AOTModuleInstanceExtra *)module_inst->e)->c_api_func_imports);
11961207

1208+
#if WASM_ENABLE_WASI_NN != 0
1209+
if (!is_sub_inst) {
1210+
WASINNContext *wasi_nn_ctx =
1211+
((AOTModuleInstanceExtra *)module_inst->e)->wasi_nn_ctx;
1212+
if (wasi_nn_ctx)
1213+
wasi_nn_destroy(wasi_nn_ctx);
1214+
}
1215+
#endif
1216+
11971217
wasm_runtime_free(module_inst);
11981218
}
11991219

core/iwasm/aot/aot_runtime.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "../interpreter/wasm_runtime.h"
1212
#include "../compilation/aot.h"
1313

14+
#if WASM_ENABLE_WASI_NN != 0
15+
#include "../libraries/wasi-nn/src/wasi_nn_private.h"
16+
#endif
17+
1418
#ifdef __cplusplus
1519
extern "C" {
1620
#endif
@@ -75,6 +79,9 @@ typedef struct AOTFunctionInstance {
7579

7680
typedef struct AOTModuleInstanceExtra {
7781
CApiFuncImport *c_api_func_imports;
82+
#if WASM_ENABLE_WASI_NN != 0
83+
WASINNContext *wasi_nn_ctx;
84+
#endif
7885
} AOTModuleInstanceExtra;
7986

8087
#if defined(OS_ENABLE_HW_BOUND_CHECK) && defined(BH_PLATFORM_WINDOWS)

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,16 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
18031803
}
18041804
#endif
18051805

1806+
#if WASM_ENABLE_WASI_NN != 0
1807+
if (!is_sub_inst) {
1808+
if (!(module_inst->e->wasi_nn_ctx = wasi_nn_initialize())) {
1809+
set_error_buf(error_buf, error_buf_size,
1810+
"wasi nn initialization failed");
1811+
goto fail;
1812+
}
1813+
}
1814+
#endif
1815+
18061816
#if WASM_ENABLE_DEBUG_INTERP != 0 \
18071817
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
18081818
&& WASM_ENABLE_LAZY_JIT != 0)
@@ -1984,6 +1994,14 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
19841994
if (module_inst->e->c_api_func_imports)
19851995
wasm_runtime_free(module_inst->e->c_api_func_imports);
19861996

1997+
#if WASM_ENABLE_WASI_NN != 0
1998+
if (!is_sub_inst) {
1999+
WASINNContext *wasi_nn_ctx = module_inst->e->wasi_nn_ctx;
2000+
if (wasi_nn_ctx)
2001+
wasi_nn_destroy(wasi_nn_ctx);
2002+
}
2003+
#endif
2004+
19872005
wasm_runtime_free(module_inst);
19882006
}
19892007

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "../common/wasm_runtime_common.h"
1212
#include "../common/wasm_exec_env.h"
1313

14+
#if WASM_ENABLE_WASI_NN != 0
15+
#include "../libraries/wasi-nn/src/wasi_nn_private.h"
16+
#endif
17+
1418
#ifdef __cplusplus
1519
extern "C" {
1620
#endif
@@ -242,6 +246,10 @@ typedef struct WASMModuleInstanceExtra {
242246
&& WASM_ENABLE_LAZY_JIT != 0)
243247
WASMModuleInstance *next;
244248
#endif
249+
250+
#if WASM_ENABLE_WASI_NN != 0
251+
WASINNContext *wasi_nn_ctx;
252+
#endif
245253
} WASMModuleInstanceExtra;
246254

247255
struct AOTFuncPerfProfInfo;

core/iwasm/libraries/wasi-nn/.dockerignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

core/iwasm/libraries/wasi-nn/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ Tests: passed!
3737

3838
## What is missing
3939

40-
* Only 1 model at a time is supported.
40+
Supported:
41+
42+
* Only 1 WASM app at a time.
43+
* Only 1 model at a time.
4144
* `graph` and `graph-execution-context` are ignored.
42-
* Only `tensorflow` (lite) is supported.
43-
* Only `cpu` is supported.
45+
* Graph encoding: `tensorflowlite`.
46+
* Execution target: `cpu`.
47+
* Tensor type: `fp32`.

core/iwasm/libraries/wasi-nn/logger.h

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#ifndef WASI_NN_LOGGER_H
7+
#define WASI_NN_LOGGER_H
8+
9+
#include <stdio.h>
10+
#include <string.h>
11+
12+
#define __FILENAME__ \
13+
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
14+
15+
/* Disable a level by removing the define */
16+
#define ENABLE_ERR_LOG
17+
#define ENABLE_WARN_LOG
18+
#define ENABLE_DBG_LOG
19+
#define ENABLE_INFO_LOG
20+
21+
// Definition of the levels
22+
#ifdef ENABLE_ERR_LOG
23+
#define NN_ERR_PRINTF(fmt, ...) \
24+
do { \
25+
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
26+
printf("\n"); \
27+
fflush(stdout); \
28+
} while (0)
29+
#else
30+
#define NN_ERR_PRINTF(fmt, ...)
31+
#endif
32+
#ifdef ENABLE_WARN_LOG
33+
#define NN_WARN_PRINTF(fmt, ...) \
34+
do { \
35+
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
36+
printf("\n"); \
37+
fflush(stdout); \
38+
} while (0)
39+
#else
40+
#define NN_WARN_PRINTF(fmt, ...)
41+
#endif
42+
#ifdef ENABLE_DBG_LOG
43+
#define NN_DBG_PRINTF(fmt, ...) \
44+
do { \
45+
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
46+
printf("\n"); \
47+
fflush(stdout); \
48+
} while (0)
49+
#else
50+
#define NN_DBG_PRINTF(fmt, ...)
51+
#endif
52+
#ifdef ENABLE_INFO_LOG
53+
#define NN_INFO_PRINTF(fmt, ...) \
54+
do { \
55+
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \
56+
printf("\n"); \
57+
fflush(stdout); \
58+
} while (0)
59+
#else
60+
#define NN_INFO_PRINTF(fmt, ...)
61+
#endif
62+
63+
#endif

0 commit comments

Comments
 (0)