Skip to content

Commit 292c582

Browse files
authored
Merge pull request #944 from bytecodealliance/main
Merge bytecodealliance:main into wenyongh:main
2 parents 29dc9d6 + 67fa155 commit 292c582

File tree

22 files changed

+631
-332
lines changed

22 files changed

+631
-332
lines changed

build-scripts/config_common.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,13 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
438438
if (NOT WAMR_BUILD_WASI_NN_TFLITE EQUAL 1 AND NOT WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
439439
message (FATAL_ERROR " Need to select a backend for WASI-NN")
440440
endif ()
441+
441442
if (WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
442-
message (" WASI-NN backend tflite enabled")
443+
message (" WASI-NN: backend tflite enabled")
443444
add_definitions (-DWASM_ENABLE_WASI_NN_TFLITE)
444445
endif ()
445446
if (WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
446-
message (" WASI-NN backend openvino enabled")
447+
message (" WASI-NN: backend openvino enabled")
447448
add_definitions (-DWASM_ENABLE_WASI_NN_OPENVINO)
448449
endif ()
449450
# Variant devices
@@ -459,7 +460,7 @@ if (WAMR_BUILD_WASI_NN EQUAL 1)
459460
add_definitions (-DWASM_WASI_NN_EXTERNAL_DELEGATE_PATH="${WAMR_BUILD_WASI_NN_EXTERNAL_DELEGATE_PATH}")
460461
endif ()
461462
if (WAMR_BUILD_WASI_EPHEMERAL_NN EQUAL 1)
462-
message (" WASI-NN: WASI-Ephemeral-NN enabled")
463+
message (" WASI-NN: use 'wasi_ephemeral_nn' instead of 'wasi-nn'")
463464
add_definitions (-DWASM_ENABLE_WASI_EPHEMERAL_NN=1)
464465
endif()
465466
endif ()

core/iwasm/common/wasm_native.c

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#if WASM_ENABLE_THREAD_MGR != 0
1616
#include "../libraries/thread-mgr/thread_manager.h"
1717
#endif
18+
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
19+
#include "wasi_nn_host.h"
20+
#endif
1821

1922
static NativeSymbolsList g_native_symbols_list = NULL;
2023

@@ -472,11 +475,12 @@ quick_aot_entry_init();
472475
bool
473476
wasm_native_init()
474477
{
475-
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
476-
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
477-
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
478-
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
479-
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
478+
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
479+
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
480+
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
481+
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
482+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
483+
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
480484
NativeSymbol *native_symbols;
481485
uint32 n_native_symbols;
482486
#endif
@@ -562,13 +566,30 @@ wasm_native_init()
562566
goto fail;
563567
#endif /* WASM_ENABLE_LIB_RATS */
564568

569+
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
570+
if (!wasi_nn_initialize())
571+
goto fail;
572+
573+
n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
574+
if (n_native_symbols > 0
575+
&& !wasm_native_register_natives(
576+
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
577+
"wasi_ephemeral_nn",
578+
#else
579+
"wasi_nn",
580+
#endif /* WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
581+
native_symbols, n_native_symbols))
582+
goto fail;
583+
#endif /* WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 */
584+
565585
#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
566586
if (!quick_aot_entry_init()) {
567-
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
568-
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
569-
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
570-
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
571-
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
587+
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
588+
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
589+
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
590+
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
591+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
592+
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
572593
goto fail;
573594
#else
574595
return false;
@@ -577,11 +598,12 @@ wasm_native_init()
577598
#endif
578599

579600
return true;
580-
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
581-
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
582-
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
583-
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
584-
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
601+
#if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
602+
|| WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
603+
|| WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
604+
|| WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
605+
|| WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0 \
606+
|| WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
585607
fail:
586608
wasm_native_destroy();
587609
return false;
@@ -599,6 +621,7 @@ wasm_native_destroy()
599621
g_wasi_context_key = NULL;
600622
}
601623
#endif
624+
602625
#if WASM_ENABLE_LIB_PTHREAD != 0
603626
lib_pthread_destroy();
604627
#endif
@@ -607,6 +630,10 @@ wasm_native_destroy()
607630
lib_wasi_threads_destroy();
608631
#endif
609632

633+
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
634+
wasi_nn_destroy();
635+
#endif
636+
610637
node = g_native_symbols_list;
611638
while (node) {
612639
node_next = node->next;

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,27 @@ fd_determine_type_rights(os_file_handle fd, __wasi_filetype_t *type,
459459
__wasi_rights_t *rights_inheriting)
460460
{
461461
struct __wasi_filestat_t buf;
462-
__wasi_errno_t error = os_fstat(fd, &buf);
462+
__wasi_errno_t error;
463+
464+
if (os_is_stdin_handle(fd)) {
465+
*rights_base = RIGHTS_STDIN;
466+
*rights_inheriting = RIGHTS_STDIN;
467+
return __WASI_ESUCCESS;
468+
}
469+
470+
if (os_is_stdout_handle(fd)) {
471+
*rights_base = RIGHTS_STDOUT;
472+
*rights_inheriting = RIGHTS_STDOUT;
473+
return __WASI_ESUCCESS;
474+
}
475+
476+
if (os_is_stderr_handle(fd)) {
477+
*rights_base = RIGHTS_STDERR;
478+
*rights_inheriting = RIGHTS_STDERR;
479+
return __WASI_ESUCCESS;
480+
}
463481

482+
error = os_fstat(fd, &buf);
464483
if (error != __WASI_ESUCCESS)
465484
return error;
466485

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/rights.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@
4747
#define RIGHTS_CHARACTER_DEVICE_BASE RIGHTS_ALL
4848
#define RIGHTS_CHARACTER_DEVICE_INHERITING RIGHTS_ALL
4949

50+
#define RIGHTS_STDIN \
51+
(__WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_FILESTAT_GET | \
52+
__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_WRITE | \
53+
__WASI_RIGHT_POLL_FD_READWRITE)
54+
55+
#define RIGHTS_STDOUT \
56+
(__WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_DATASYNC | \
57+
__WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_SYNC | \
58+
__WASI_RIGHT_FD_READ | __WASI_RIGHT_FD_WRITE | \
59+
__WASI_RIGHT_POLL_FD_READWRITE)
60+
61+
#define RIGHTS_STDERR RIGHTS_STDOUT
62+
5063
// Only allow directory operations on directories. Directories can only
5164
// yield file descriptors to other directories and files.
5265
#define RIGHTS_DIRECTORY_BASE \

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
5-
find_library(TENSORFLOW_LITE
6-
NAMES tensorflow-lite
4+
find_library(TENSORFLOW_LITE
5+
NAMES tensorflow-lite
6+
HINTS ${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite
7+
NO_DEFAULT_PATHS
78
)
89

9-
if(NOT EXISTS ${TENSORFLOW_LITE})
10+
if(NOT TENSORFLOW_LITE)
1011
if(NOT EXISTS "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
1112
execute_process(
1213
COMMAND "${WAMR_ROOT_DIR}/core/deps/install_tensorflow.sh"
@@ -32,11 +33,15 @@ if(NOT EXISTS ${TENSORFLOW_LITE})
3233
"${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
3334
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
3435
EXCLUDE_FROM_ALL
35-
)
36+
)
37+
else ()
38+
message(STATUS "TensorFlow Lite library found: ${TENSORFLOW_LITE}")
39+
set(TENSORFLOW_SOURCE_DIR "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
40+
endif()
3641

37-
set(TENSORFLOW_LITE_INCLUDE_DIR "${TENSORFLOW_SOURCE_DIR}")
38-
set(FLATBUFFER_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include")
42+
set(TENSORFLOW_LITE_INCLUDE_DIR "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite")
43+
set(FLATBUFFER_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include")
3944

40-
include_directories(${TENSORFLOW_LITE_INCLUDE_DIR})
41-
include_directories(${FLATBUFFER_INCLUDE_DIR})
42-
endif()
45+
include_directories(${TENSORFLOW_SOURCE_DIR})
46+
include_directories(${FLATBUFFER_INCLUDE_DIR})
47+
link_directories(${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite)

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,61 +27,48 @@ endif()
2727
#
2828
# wasi-nn general
2929
set(WASI_NN_ROOT ${CMAKE_CURRENT_LIST_DIR}/..)
30-
add_library(
31-
wasi-nn-general
32-
SHARED
33-
${WASI_NN_ROOT}/src/wasi_nn.c
34-
${WASI_NN_ROOT}/src/utils/wasi_nn_app_native.c
30+
set(WASI_NN_SOURCES
31+
${WASI_NN_ROOT}/src/wasi_nn.c
32+
${WASI_NN_ROOT}/src/utils/wasi_nn_app_native.c
3533
)
36-
target_include_directories(
37-
wasi-nn-general
38-
PUBLIC
39-
${WASI_NN_ROOT}/include
40-
${WASI_NN_ROOT}/src
41-
${WASI_NN_ROOT}/src/utils
42-
)
43-
target_link_libraries(
44-
wasi-nn-general
45-
PUBLIC
46-
libiwasm
47-
)
48-
target_compile_definitions(
49-
wasi-nn-general
50-
PUBLIC
51-
$<$<CONFIG:Debug>:NN_LOG_LEVEL=0>
52-
$<$<CONFIG:Release>:NN_LOG_LEVEL=2>
34+
include_directories(${WASI_NN_ROOT}/include)
35+
add_compile_definitions(
36+
$<$<CONFIG:Debug>:NN_LOG_LEVEL=0>
37+
$<$<CONFIG:Release>:NN_LOG_LEVEL=2>
5338
)
5439

5540
#
5641
# wasi-nn backends
57-
42+
#
5843
# - tflite
5944
if(WAMR_BUILD_WASI_NN_TFLITE EQUAL 1)
6045
add_library(
61-
wasi-nn-tflite
46+
wasi_nn_tflite
6247
SHARED
6348
${WASI_NN_ROOT}/src/wasi_nn_tensorflowlite.cpp
6449
)
50+
6551
target_link_libraries(
66-
wasi-nn-tflite
52+
wasi_nn_tflite
6753
PUBLIC
54+
libiwasm
6855
tensorflow-lite
69-
wasi-nn-general
7056
)
7157
endif()
7258

7359
# - openvino
7460
if(WAMR_BUILD_WASI_NN_OPENVINO EQUAL 1)
7561
add_library(
76-
wasi-nn-openvino
62+
wasi_nn_openvino
7763
SHARED
7864
${WASI_NN_ROOT}/src/wasi_nn_openvino.c
7965
)
66+
8067
target_link_libraries(
81-
wasi-nn-openvino
68+
wasi_nn_openvino
8269
PUBLIC
70+
libiwasm
8371
openvino::runtime
8472
openvino::runtime::c
85-
wasi-nn-general
8673
)
8774
endif()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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_HOST_H
7+
#define WASI_NN_HOST_H
8+
9+
#include "lib_export.h"
10+
11+
uint32_t
12+
get_wasi_nn_export_apis(NativeSymbol **p_native_symbols);
13+
14+
bool
15+
wasi_nn_initialize();
16+
17+
void
18+
wasi_nn_destroy();
19+
20+
#endif /* WASI_NN_HOST_H */

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ typedef enum {
126126
tensorflowlite,
127127
ggml,
128128
autodetect,
129+
unknown_backend,
129130
} graph_encoding;
130131

131132
// Define where the graph should be executed.
@@ -161,9 +162,6 @@ typedef struct {
161162
BACKEND_DEINITIALIZE deinit;
162163
} api_function;
163164

164-
bool
165-
wasi_nn_register_backend(api_function apis);
166-
167165
void
168166
wasi_nn_dump_tensor_dimension(tensor_dimensions *dim, int32_t output_len,
169167
char *output);

0 commit comments

Comments
 (0)