Skip to content

Commit 058bc47

Browse files
authored
[wasi-nn] Add a new wasi-nn backend openvino (#3603)
1 parent 50f2849 commit 058bc47

12 files changed

Lines changed: 1036 additions & 146 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
.venv
66
/.idea
77
**/cmake-build-*/
8-
**/*build/
8+
**/*build*/
9+
!/build-scripts
910
*.obj
1011
*.a
1112
*.so

build-scripts/config_common.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,19 @@ endif ()
434434
if (WAMR_BUILD_WASI_NN EQUAL 1)
435435
message (" WASI-NN enabled")
436436
add_definitions (-DWASM_ENABLE_WASI_NN=1)
437+
# Variant backends
438+
if (NOT WAMR_BUILD_WASI_NN_TFLITE EQUAL 1 AND NOT WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
439+
message (FATAL_ERROR " Need to select a backend for WASI-NN")
440+
endif ()
441+
if (WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
442+
message (" WASI-NN backend tflite enabled")
443+
add_definitions (-DWASM_ENABLE_WASI_NN_TFLITE)
444+
endif ()
445+
if (WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
446+
message (" WASI-NN backend openvino enabled")
447+
add_definitions (-DWASM_ENABLE_WASI_NN_OPENVINO)
448+
endif ()
449+
# Variant devices
437450
if (WAMR_BUILD_WASI_NN_ENABLE_GPU EQUAL 1)
438451
message (" WASI-NN: GPU enabled")
439452
add_definitions (-DWASM_ENABLE_WASI_NN_GPU=1)

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ $ cmake -DWAMR_BUILD_WASI_NN=1 <other options> ...
1919
> ![Caution]
2020
> 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`.
2121
22+
#### Compilation options
23+
24+
- `WAMR_BUILD_WASI_NN`. enable wasi-nn support. can't work alone. need to identify a backend. Match legacy wasi-nn spec naming convention. use `wasi_nn` as import module names.
25+
- `WAMR_BUILD_WASI_EPHEMERAL_NN`. Match latest wasi-nn spec naming convention. use `wasi_ephemeral_nn` as import module names.
26+
- `WAMR_BUILD_WASI_NN_TFLITE`. identify the backend as TensorFlow Lite.
27+
- `WAMR_BUILD_WASI_NN_OPENVINO`. identify the backend as OpenVINO.
28+
2229
### Wasm
2330

2431
The definition of functions provided by WASI-NN (Wasm imports) is in the header file [wasi_nn.h](_core/iwasm/libraries/wasi-nn/wasi_nn.h_). By only including this file in a WASM application you will bind WASI-NN into your module.
@@ -37,6 +44,12 @@ typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;
3744

3845
It is required to recompile the Wasm application if you want to switch between the two sets of functions.
3946

47+
#### Openvino
48+
49+
If you're planning to use OpenVINO backends, the first step is to install OpenVINO on your computer. To do this correctly, please follow the official installation guide which you can find at this link: https://docs.openvino.ai/2024/get-started/install-openvino/install-openvino-archive-linux.html.
50+
51+
After you've installed OpenVINO, you'll need to let cmake system know where to find it. You can do this by setting an environment variable named `OpenVINO_DIR`. This variable should point to the place on your computer where OpenVINO is installed. By setting this variable, your system will be able to locate and use OpenVINO when needed. You can find installation path by running the following command if using APT `$dpkg -L openvino`. The path should be _/opt/intel/openvino/_ or _/usr/lib/openvino_.
52+
4053
## Tests
4154

4255
To run the tests we assume that the current directory is the root of the repository.
@@ -167,7 +180,7 @@ Due to the different requirements of each backend, we'll use a Docker container
167180
$ pwd
168181
/workspaces/wasm-micro-runtime/
169182

170-
$ docker build -t wasi-nn-smoke:v1.0 -f Dockerfile.wasi-nn-smoke .
183+
$ docker build -t wasi-nn-smoke:v1.0 -f ./core/iwasm/libraries/wasi-nn/test/Dockerfile.wasi-nn-smoke .
171184
```
172185

173186
#### Execute

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

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33

44
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
55

6-
# Find tensorflow-lite
7-
find_package(tensorflow_lite REQUIRED)
6+
if(WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
7+
# Find tensorflow-lite
8+
find_package(tensorflow_lite REQUIRED)
9+
endif()
810

9-
set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
11+
if(WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
12+
if(NOT DEFINED ENV{OpenVINO_DIR})
13+
message(FATAL_ERROR
14+
"OpenVINO_DIR is not defined. "
15+
"Please follow https://docs.openvino.ai/2024/get-started/install-openvino.html,"
16+
"install openvino, and set environment variable OpenVINO_DIR."
17+
"Like OpenVINO_DIR=/usr/lib/openvino-2023.2/ cmake ..."
18+
"Or OpenVINO_DIR=/opt/intel/openvino/ cmake ..."
19+
)
20+
endif()
21+
22+
list(APPEND CMAKE_MODULE_PATH $ENV{OpenVINO_DIR})
23+
# Find OpenVINO
24+
find_package(OpenVINO REQUIRED COMPONENTS Runtime)
25+
endif()
1026

1127
#
1228
# wasi-nn general
29+
set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
1330
add_library(
1431
wasi-nn-general
1532
SHARED
@@ -37,20 +54,34 @@ target_compile_definitions(
3754

3855
#
3956
# 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-
)
57+
58+
# - tflite
59+
if(WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
60+
add_library(
61+
wasi-nn-tflite
62+
SHARED
63+
${WASI_NN_ROOT}/src/wasi_nn_tensorflowlite.cpp
64+
)
65+
target_link_libraries(
66+
wasi-nn-tflite
67+
PUBLIC
68+
tensorflow-lite
69+
wasi-nn-general
70+
)
71+
endif()
72+
73+
# - openvino
74+
if(WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
75+
add_library(
76+
wasi-nn-openvino
77+
SHARED
78+
${WASI_NN_ROOT}/src/wasi_nn_openvino.c
79+
)
80+
target_link_libraries(
81+
wasi-nn-openvino
82+
PUBLIC
83+
openvino::runtime
84+
openvino::runtime::c
85+
wasi-nn-general
86+
)
87+
endif()

core/iwasm/libraries/wasi-nn/include/wasi_nn_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ typedef struct {
164164
bool
165165
wasi_nn_register_backend(api_function apis);
166166

167+
void
168+
wasi_nn_dump_tensor_dimension(tensor_dimensions *dim, int32_t output_len,
169+
char *output);
170+
167171
#ifdef __cplusplus
168172
}
169173
#endif

core/iwasm/libraries/wasi-nn/src/utils/wasi_nn_app_native.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ graph_builder_array_app_native(wasm_module_inst_t instance,
8888
}
8989

9090
NN_DBG_PRINTF("Graph builder %d contains %d elements", i,
91-
builder->size);
91+
builder[i].size);
9292
}
9393

9494
builder_array->buf = builder;

0 commit comments

Comments
 (0)