Skip to content

Commit

Permalink
[wasi-nn] Add a new wasi-nn backend openvino (#3603)
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us authored Jul 22, 2024
1 parent 50f2849 commit 058bc47
Show file tree
Hide file tree
Showing 12 changed files with 1,036 additions and 146 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
.venv
/.idea
**/cmake-build-*/
**/*build/
**/*build*/
!/build-scripts
*.obj
*.a
*.so
Expand Down
13 changes: 13 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,19 @@ endif ()
if (WAMR_BUILD_WASI_NN EQUAL 1)
message (" WASI-NN enabled")
add_definitions (-DWASM_ENABLE_WASI_NN=1)
# Variant backends
if (NOT WAMR_BUILD_WASI_NN_TFLITE EQUAL 1 AND NOT WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
message (FATAL_ERROR " Need to select a backend for WASI-NN")
endif ()
if (WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
message (" WASI-NN backend tflite enabled")
add_definitions (-DWASM_ENABLE_WASI_NN_TFLITE)
endif ()
if (WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
message (" WASI-NN backend openvino enabled")
add_definitions (-DWASM_ENABLE_WASI_NN_OPENVINO)
endif ()
# Variant devices
if (WAMR_BUILD_WASI_NN_ENABLE_GPU EQUAL 1)
message (" WASI-NN: GPU enabled")
add_definitions (-DWASM_ENABLE_WASI_NN_GPU=1)
Expand Down
15 changes: 14 additions & 1 deletion core/iwasm/libraries/wasi-nn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ $ cmake -DWAMR_BUILD_WASI_NN=1 <other options> ...
> ![Caution]
> 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`.
#### Compilation options

- `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.
- `WAMR_BUILD_WASI_EPHEMERAL_NN`. Match latest wasi-nn spec naming convention. use `wasi_ephemeral_nn` as import module names.
- `WAMR_BUILD_WASI_NN_TFLITE`. identify the backend as TensorFlow Lite.
- `WAMR_BUILD_WASI_NN_OPENVINO`. identify the backend as OpenVINO.

### Wasm

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.
Expand All @@ -37,6 +44,12 @@ typedef enum { fp16 = 0, fp32, up8, ip32 } tensor_type;

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

#### Openvino

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.

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_.

## Tests

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

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

#### Execute
Expand Down
71 changes: 51 additions & 20 deletions core/iwasm/libraries/wasi-nn/cmake/wasi_nn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

# Find tensorflow-lite
find_package(tensorflow_lite REQUIRED)
if(WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
# Find tensorflow-lite
find_package(tensorflow_lite REQUIRED)
endif()

set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
if(WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
if(NOT DEFINED ENV{OpenVINO_DIR})
message(FATAL_ERROR
"OpenVINO_DIR is not defined. "
"Please follow https://docs.openvino.ai/2024/get-started/install-openvino.html,"
"install openvino, and set environment variable OpenVINO_DIR."
"Like OpenVINO_DIR=/usr/lib/openvino-2023.2/ cmake ..."
"Or OpenVINO_DIR=/opt/intel/openvino/ cmake ..."
)
endif()

list(APPEND CMAKE_MODULE_PATH $ENV{OpenVINO_DIR})
# Find OpenVINO
find_package(OpenVINO REQUIRED COMPONENTS Runtime)
endif()

#
# wasi-nn general
set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
add_library(
wasi-nn-general
SHARED
Expand Down Expand Up @@ -37,20 +54,34 @@ target_compile_definitions(

#
# wasi-nn backends
add_library(
wasi-nn-tflite
SHARED
${WASI_NN_ROOT}/src/wasi_nn_tensorflowlite.cpp
)
#target_link_options(
# wasi-nn-tflite
# PRIVATE
# -Wl,--whole-archive libwasi-nn-general.a
# -Wl,--no-whole-archive
#)
target_link_libraries(
wasi-nn-tflite
PUBLIC
tensorflow-lite
wasi-nn-general
)

# - tflite
if(WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
add_library(
wasi-nn-tflite
SHARED
${WASI_NN_ROOT}/src/wasi_nn_tensorflowlite.cpp
)
target_link_libraries(
wasi-nn-tflite
PUBLIC
tensorflow-lite
wasi-nn-general
)
endif()

# - openvino
if(WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
add_library(
wasi-nn-openvino
SHARED
${WASI_NN_ROOT}/src/wasi_nn_openvino.c
)
target_link_libraries(
wasi-nn-openvino
PUBLIC
openvino::runtime
openvino::runtime::c
wasi-nn-general
)
endif()
4 changes: 4 additions & 0 deletions core/iwasm/libraries/wasi-nn/include/wasi_nn_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ typedef struct {
bool
wasi_nn_register_backend(api_function apis);

void
wasi_nn_dump_tensor_dimension(tensor_dimensions *dim, int32_t output_len,
char *output);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ graph_builder_array_app_native(wasm_module_inst_t instance,
}

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

builder_array->buf = builder;
Expand Down
Loading

0 comments on commit 058bc47

Please sign in to comment.