Skip to content

Commit f844b33

Browse files
authored
Make wasi-nn backends as separated shared libraries (#3509)
- All files under *core/iwasm/libraries/wasi-nn* are compiled as shared libraries - *wasi-nn.c* is shared between backends - Every backend has a separated shared library - If wasi-nn feature is enabled, iwasm will depend on shared library libiwasm.so instead of linking static library libvmlib.a
1 parent 1434c45 commit f844b33

20 files changed

Lines changed: 295 additions & 257 deletions

File tree

core/iwasm/aot/aot_runtime.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,9 +1944,6 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
19441944
#endif
19451945

19461946
if (!is_sub_inst) {
1947-
#if WASM_ENABLE_WASI_NN != 0
1948-
wasi_nn_destroy((WASMModuleInstanceCommon *)module_inst);
1949-
#endif
19501947
wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
19511948
}
19521949

core/iwasm/aot/aot_runtime.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
#include "gc_export.h"
1515
#endif
1616

17-
#if WASM_ENABLE_WASI_NN != 0
18-
#include "../libraries/wasi-nn/src/wasi_nn_private.h"
19-
#endif
20-
2117
#ifdef __cplusplus
2218
extern "C" {
2319
#endif

core/iwasm/common/wasm_native.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ get_spectest_export_apis(NativeSymbol **p_libc_builtin_apis);
3333
uint32
3434
get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis);
3535

36-
uint32_t
37-
get_wasi_nn_export_apis(NativeSymbol **p_libc_wasi_apis);
38-
3936
uint32
4037
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
4138

@@ -565,18 +562,6 @@ wasm_native_init()
565562
goto fail;
566563
#endif /* WASM_ENABLE_LIB_RATS */
567564

568-
#if WASM_ENABLE_WASI_NN != 0
569-
n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
570-
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
571-
#define wasi_nn_module_name "wasi_ephemeral_nn"
572-
#else /* WASM_ENABLE_WASI_EPHEMERAL_NN == 0 */
573-
#define wasi_nn_module_name "wasi_nn"
574-
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
575-
if (!wasm_native_register_natives(wasi_nn_module_name, native_symbols,
576-
n_native_symbols))
577-
goto fail;
578-
#endif
579-
580565
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
581566
if (!quick_aot_entry_init()) {
582567
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,9 +3181,6 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
31813181
wasm_runtime_free(module_inst->c_api_func_imports);
31823182

31833183
if (!is_sub_inst) {
3184-
#if WASM_ENABLE_WASI_NN != 0
3185-
wasi_nn_destroy((WASMModuleInstanceCommon *)module_inst);
3186-
#endif
31873184
wasm_native_call_context_dtors((WASMModuleInstanceCommon *)module_inst);
31883185
}
31893186

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#include "../common/wasm_runtime_common.h"
1414
#include "../common/wasm_exec_env.h"
1515

16-
#if WASM_ENABLE_WASI_NN != 0
17-
#include "../libraries/wasi-nn/src/wasi_nn_private.h"
18-
#endif
19-
2016
#ifdef __cplusplus
2117
extern "C" {
2218
#endif

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

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22

33
## How to use
44

5+
### Host
6+
57
Enable WASI-NN in the WAMR by spefiying it in the cmake building configuration as follows,
68

7-
```
9+
```cmake
810
set (WAMR_BUILD_WASI_NN 1)
911
```
1012

11-
The definition of the functions provided by WASI-NN is in the header file `core/iwasm/libraries/wasi-nn/wasi_nn.h`.
13+
or in command line
14+
15+
```bash
16+
$ cmake -DWAMR_BUILD_WASI_NN=1 <other options> ...
17+
```
18+
19+
> ![Caution]
20+
> If enable `WAMR_BUID_WASI_NN`, iwasm will link a shared WAMR library instead of a static one. Wasi-nn backends will be loaded dynamically at runtime. Users shall specify the path of the backend library and register it to the iwasm runtime with `--native-lib=<path of backend library>`. All shared libraries should be placed in the `LD_LIBRARY_PATH`.
21+
22+
### Wasm
23+
24+
The definition of functions provided by WASI-NN (Wasm imports) is in the header file _core/iwasm/libraries/wasi-nn/wasi_nn.h_.
1225

13-
By only including this file in your WASM application you will bind WASI-NN into your module.
26+
By only including this file in a WASM application you will bind WASI-NN into your module.
1427

1528
## Tests
1629

@@ -27,9 +40,8 @@ Build the runtime image for your execution target type.
2740
- `vx-delegate`
2841
- `tpu`
2942

30-
```
31-
EXECUTION_TYPE=cpu
32-
docker build -t wasi-nn-${EXECUTION_TYPE} -f core/iwasm/libraries/wasi-nn/test/Dockerfile.${EXECUTION_TYPE} .
43+
```bash
44+
EXECUTION_TYPE=cpu docker build -t wasi-nn-${EXECUTION_TYPE} -f core/iwasm/libraries/wasi-nn/test/Dockerfile.${EXECUTION_TYPE} .
3345
```
3446

3547
### Build wasm app
@@ -50,56 +62,63 @@ If all the tests have run properly you will the the following message in the ter
5062
Tests: passed!
5163
```
5264

65+
> [!TIP]
66+
> Use _libwasi-nn-tflite.so_ as an example. You shall use whatever you have built.
67+
5368
- CPU
5469

55-
```
70+
```bash
5671
docker run \
5772
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets \
5873
-v $PWD/core/iwasm/libraries/wasi-nn/test/models:/models \
5974
wasi-nn-cpu \
6075
--dir=/ \
6176
--env="TARGET=cpu" \
77+
--native-lib=/lib/libwasi-nn-tflite.so \
6278
/assets/test_tensorflow.wasm
6379
```
6480

6581
- (NVIDIA) GPU
6682
- Requirements:
6783
- [NVIDIA docker](https://github.com/NVIDIA/nvidia-docker).
6884

69-
```
85+
```bash
7086
docker run \
7187
--runtime=nvidia \
7288
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets \
7389
-v $PWD/core/iwasm/libraries/wasi-nn/test/models:/models \
7490
wasi-nn-nvidia-gpu \
7591
--dir=/ \
7692
--env="TARGET=gpu" \
93+
--native-lib=/lib/libwasi-nn-tflite.so \
7794
/assets/test_tensorflow.wasm
7895
```
7996

8097
- vx-delegate for NPU (x86 simulator)
8198

82-
```
99+
```bash
83100
docker run \
84101
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets \
85102
wasi-nn-vx-delegate \
86103
--dir=/ \
87104
--env="TARGET=gpu" \
105+
--native-lib=/lib/libwasi-nn-tflite.so \
88106
/assets/test_tensorflow_quantized.wasm
89107
```
90108

91109
- (Coral) TPU
92110
- Requirements:
93111
- [Coral USB](https://coral.ai/products/accelerator/).
94112

95-
```
113+
```bash
96114
docker run \
97115
--privileged \
98116
--device=/dev/bus/usb:/dev/bus/usb \
99117
-v $PWD/core/iwasm/libraries/wasi-nn/test:/assets \
100118
wasi-nn-tpu \
101119
--dir=/ \
102120
--env="TARGET=tpu" \
121+
--native-lib=/lib/libwasi-nn-tflite.so \
103122
/assets/test_tensorflow_quantized.wasm
104123
```
105124

@@ -120,20 +139,20 @@ Use [classification-example](https://github.com/bytecodealliance/wasi-nn/tree/ma
120139
121140
### Prepare the model and the wasm
122141

123-
``` bash
142+
```bash
124143
$ pwd
125144
/workspaces/wasm-micro-runtime/core/iwasm/libraries/wasi-nn/test
126145

127146
$ docker build -t wasi-nn-example:v1.0 -f Dockerfile.wasi-nn-example .
128147
```
129148

130-
There are model files(*mobilenet\**) and wasm files(*wasi-nn-example.wasm*) in the directory */workspaces/wasi-nn/rust/examples/classification-example/build* in the image of wasi-nn-example:v1.0.
149+
There are model files(\*mobilenet\**) and wasm files(*wasi-nn-example.wasm*) in the directory */workspaces/wasi-nn/rust/examples/classification-example/build\* in the image of wasi-nn-example:v1.0.
131150

132151
### build iwasm and test
133152

134-
*TODO: May need alternative steps to build the iwasm and test in the container of wasi-nn-example:v1.0*
153+
_TODO: May need alternative steps to build the iwasm and test in the container of wasi-nn-example:v1.0_
135154

136-
``` bash
155+
```bash
137156
$ pwd
138157
/workspaces/wasm-micro-runtime
139158

@@ -143,9 +162,9 @@ $ docker run --rm -it -v $(pwd):/workspaces/wasm-micro-runtime wasi-nn-example:v
143162
> [!Caution]
144163
> The following steps are executed in the container of wasi-nn-example:v1.0.
145164
146-
``` bash
165+
```bash
147166
$ cd /workspaces/wasm-micro-runtime/product-mini/platforms/linux
148167
$ cmake -S . -B build -DWAMR_BUILD_WASI_NN=1 -DWAMR_BUILD_WASI_EPHEMERAL_NN=1
149168
$ cmake --build build
150169
$ ./build/iwasm -v=5 --map-dir=/workspaces/wasi-nn/rust/examples/classification-example/build/::fixture /workspaces/wasi-nn/rust/examples/classification-example/build/wasi-nn-example.wasm
151-
```
170+
```

core/iwasm/libraries/wasi-nn/cmake/wasi_nn.cmake

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,51 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
66
# Find tensorflow-lite
77
find_package(tensorflow_lite REQUIRED)
88

9-
set(WASI_NN_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
9+
set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
1010

11-
include_directories (${WASI_NN_ROOT_DIR}/include)
12-
include_directories (${WASI_NN_ROOT_DIR}/src)
13-
include_directories (${WASI_NN_ROOT_DIR}/src/utils)
14-
15-
set (
16-
WASI_NN_SOURCES
17-
${WASI_NN_ROOT_DIR}/src/wasi_nn.c
18-
${WASI_NN_ROOT_DIR}/src/wasi_nn_tensorflowlite.cpp
19-
${WASI_NN_ROOT_DIR}/src/utils/wasi_nn_app_native.c
11+
#
12+
# wasi-nn general
13+
add_library(
14+
wasi-nn-general
15+
SHARED
16+
${WASI_NN_ROOT}/src/wasi_nn.c
17+
${WASI_NN_ROOT}/src/utils/wasi_nn_app_native.c
18+
)
19+
target_include_directories(
20+
wasi-nn-general
21+
PUBLIC
22+
${WASI_NN_ROOT}/include
23+
${WASI_NN_ROOT}/src
24+
${WASI_NN_ROOT}/src/utils
25+
)
26+
target_link_libraries(
27+
wasi-nn-general
28+
PUBLIC
29+
libiwasm
30+
)
31+
target_compile_definitions(
32+
wasi-nn-general
33+
PUBLIC
34+
$<$<CONFIG:Debug>:NN_LOG_LEVEL=0>
35+
$<$<CONFIG:Release>:NN_LOG_LEVEL=2>
2036
)
2137

22-
set (WASI_NN_LIBS tensorflow-lite)
38+
#
39+
# wasi-nn backends
40+
add_library(
41+
wasi-nn-tflite
42+
SHARED
43+
${WASI_NN_ROOT}/src/wasi_nn_tensorflowlite.cpp
44+
)
45+
#target_link_options(
46+
# wasi-nn-tflite
47+
# PRIVATE
48+
# -Wl,--whole-archive libwasi-nn-general.a
49+
# -Wl,--no-whole-archive
50+
#)
51+
target_link_libraries(
52+
wasi-nn-tflite
53+
PUBLIC
54+
tensorflow-lite
55+
wasi-nn-general
56+
)

core/iwasm/libraries/wasi-nn/external/CMakeLists.txt

Lines changed: 0 additions & 58 deletions
This file was deleted.

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)