Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,13 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
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")
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")
message (" WASI-NN: backend openvino enabled")
add_definitions (-DWASM_ENABLE_WASI_NN_OPENVINO)
endif ()
# Variant devices
Expand All @@ -459,7 +460,7 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
add_definitions (-DWASM_WASI_NN_EXTERNAL_DELEGATE_PATH="${WAMR_BUILD_WASI_NN_EXTERNAL_DELEGATE_PATH}")
endif ()
if (WAMR_BUILD_WASI_EPHEMERAL_NN EQUAL 1)
message (" WASI-NN: WASI-Ephemeral-NN enabled")
message (" WASI-NN: use 'wasi_ephemeral_nn' instead of 'wasi-nn'")
add_definitions (-DWASM_ENABLE_WASI_EPHEMERAL_NN=1)
endif()
endif ()
Expand Down
57 changes: 42 additions & 15 deletions core/iwasm/common/wasm_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#if WASM_ENABLE_THREAD_MGR != 0
#include "../libraries/thread-mgr/thread_manager.h"
#endif
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
#include "wasi_nn_host.h"
#endif

static NativeSymbolsList g_native_symbols_list = NULL;

Expand Down Expand Up @@ -472,11 +475,12 @@ quick_aot_entry_init();
bool
wasm_native_init()
{
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
NativeSymbol *native_symbols;
uint32 n_native_symbols;
#endif
Expand Down Expand Up @@ -562,13 +566,30 @@ wasm_native_init()
goto fail;
#endif /* WASM_ENABLE_LIB_RATS */

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
if (!wasi_nn_initialize())
goto fail;

n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
if (n_native_symbols > 0
&& !wasm_native_register_natives(
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
"wasi_ephemeral_nn",
#else
"wasi_nn",
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
native_symbols, n_native_symbols))
goto fail;
#endif /* WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */

#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
if (!quick_aot_entry_init()) {
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
goto fail;
#else
return false;
Expand All @@ -577,11 +598,12 @@ wasm_native_init()
#endif

return true;
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
fail:
wasm_native_destroy();
return false;
Expand All @@ -599,6 +621,7 @@ wasm_native_destroy()
g_wasi_context_key = NULL;
}
#endif

#if WASM_ENABLE_LIB_PTHREAD != 0
lib_pthread_destroy();
#endif
Expand All @@ -607,6 +630,10 @@ wasm_native_destroy()
lib_wasi_threads_destroy();
#endif

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
wasi_nn_destroy();
#endif

node = g_native_symbols_list;
while (node) {
node_next = node->next;
Expand Down
25 changes: 15 additions & 10 deletions core/iwasm/libraries/wasi-nn/cmake/Findtensorflow_lite.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception


find_library(TENSORFLOW_LITE
NAMES tensorflow-lite
find_library(TENSORFLOW_LITE
NAMES tensorflow-lite
HINTS ${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite
NO_DEFAULT_PATHS
)

if(NOT EXISTS ${TENSORFLOW_LITE})
if(NOT TENSORFLOW_LITE)
if(NOT EXISTS "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
execute_process(
COMMAND "${WAMR_ROOT_DIR}/core/deps/install_tensorflow.sh"
Expand All @@ -32,11 +33,15 @@ if(NOT EXISTS ${TENSORFLOW_LITE})
"${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
EXCLUDE_FROM_ALL
)
)
else ()
message(STATUS "TensorFlow Lite library found: ${TENSORFLOW_LITE}")
set(TENSORFLOW_SOURCE_DIR "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
endif()

set(TENSORFLOW_LITE_INCLUDE_DIR "${TENSORFLOW_SOURCE_DIR}")
set(FLATBUFFER_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include")
set(TENSORFLOW_LITE_INCLUDE_DIR "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite")
set(FLATBUFFER_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include")

include_directories(${TENSORFLOW_LITE_INCLUDE_DIR})
include_directories(${FLATBUFFER_INCLUDE_DIR})
endif()
include_directories(${TENSORFLOW_SOURCE_DIR})
include_directories(${FLATBUFFER_INCLUDE_DIR})
link_directories(${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite)
45 changes: 16 additions & 29 deletions core/iwasm/libraries/wasi-nn/cmake/wasi_nn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,61 +27,48 @@ endif()
#
# wasi-nn general
set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
add_library(
wasi-nn-general
SHARED
${WASI_NN_ROOT}/src/wasi_nn.c
${WASI_NN_ROOT}/src/utils/wasi_nn_app_native.c
set(WASI_NN_SOURCES
${WASI_NN_ROOT}/src/wasi_nn.c
${WASI_NN_ROOT}/src/utils/wasi_nn_app_native.c
)
target_include_directories(
wasi-nn-general
PUBLIC
${WASI_NN_ROOT}/include
${WASI_NN_ROOT}/src
${WASI_NN_ROOT}/src/utils
)
target_link_libraries(
wasi-nn-general
PUBLIC
libiwasm
)
target_compile_definitions(
wasi-nn-general
PUBLIC
$<$<CONFIG:Debug>:NN_LOG_LEVEL=0>
$<$<CONFIG:Release>:NN_LOG_LEVEL=2>
include_directories(${WASI_NN_ROOT}/include)
add_compile_definitions(
$<$<CONFIG:Debug>:NN_LOG_LEVEL=0>
$<$<CONFIG:Release>:NN_LOG_LEVEL=2>
)

#
# wasi-nn backends

#
# - tflite
if(WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
add_library(
wasi-nn-tflite
wasi_nn_tflite
SHARED
${WASI_NN_ROOT}/src/wasi_nn_tensorflowlite.cpp
)

target_link_libraries(
wasi-nn-tflite
wasi_nn_tflite
PUBLIC
libiwasm
tensorflow-lite
wasi-nn-general
)
endif()

# - openvino
if(WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
add_library(
wasi-nn-openvino
wasi_nn_openvino
SHARED
${WASI_NN_ROOT}/src/wasi_nn_openvino.c
)

target_link_libraries(
wasi-nn-openvino
wasi_nn_openvino
PUBLIC
libiwasm
openvino::runtime
openvino::runtime::c
wasi-nn-general
)
endif()
20 changes: 20 additions & 0 deletions core/iwasm/libraries/wasi-nn/include/wasi_nn_host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#ifndef WASI_NN_HOST_H
#define WASI_NN_HOST_H

#include "lib_export.h"

uint32_t
get_wasi_nn_export_apis(NativeSymbol **p_native_symbols);

bool
wasi_nn_initialize();

void
wasi_nn_destroy();

#endif /* WASI_NN_HOST_H */
4 changes: 1 addition & 3 deletions core/iwasm/libraries/wasi-nn/include/wasi_nn_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ typedef enum {
tensorflowlite,
ggml,
autodetect,
unknown_backend,
} graph_encoding;

// Define where the graph should be executed.
Expand Down Expand Up @@ -161,9 +162,6 @@ typedef struct {
BACKEND_DEINITIALIZE deinit;
} api_function;

bool
wasi_nn_register_backend(api_function apis);

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