Quellcode durchsuchen

wasi-nn: Simplify cmake and headers' location (#2308)

Major changes:
- Public headers inside `wasi-nn/include`
- Put cmake files in `cmake` folder
- Make linux iwasm link with `${WASI_NN_LIBS}` so iwasm can enable wasi-nn
tonibofarull vor 2 Jahren
Ursprung
Commit
ac9e789951

+ 2 - 2
build-scripts/runtime_lib.cmake

@@ -101,7 +101,7 @@ if (WAMR_BUILD_LIB_PTHREAD_SEMAPHORE EQUAL 1)
 endif ()
 
 if (WAMR_BUILD_WASI_NN EQUAL 1)
-    include (${IWASM_DIR}/libraries/wasi-nn/wasi_nn.cmake)
+    include (${IWASM_DIR}/libraries/wasi-nn/cmake/wasi_nn.cmake)
 endif ()
 
 if (WAMR_BUILD_LIB_PTHREAD EQUAL 1)
@@ -177,7 +177,7 @@ set (source_all
     ${UTILS_SHARED_SOURCE}
     ${LIBC_BUILTIN_SOURCE}
     ${LIBC_WASI_SOURCE}
-    ${LIBC_WASI_NN_SOURCE}
+    ${WASI_NN_SOURCES}
     ${IWASM_COMMON_SOURCE}
     ${IWASM_INTERP_SOURCE}
     ${IWASM_AOT_SOURCE}

+ 5 - 1
core/deps/install_tensorflow.sh

@@ -6,6 +6,10 @@ cd ${DEPS_ROOT}
 echo "Downloading tensorflow in ${PWD}..."
 
 git clone https://github.com/tensorflow/tensorflow.git tensorflow-src \
-    --branch v2.11.1
+    --branch v2.12.0
+
+# NOTE: fixes this https://github.com/tensorflow/tensorflow/issues/59631
+cd tensorflow-src
+git cherry-pick 5115fa96d7c5b41451674892317be43e30b7c389
 
 exit 0

+ 23 - 26
core/iwasm/libraries/wasi-nn/cmake/Findtensorflow_lite.cmake

@@ -7,35 +7,32 @@ find_library(TENSORFLOW_LITE
 )
 
 if(NOT EXISTS ${TENSORFLOW_LITE})
-    if (NOT EXISTS "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
-        execute_process(COMMAND ${WAMR_ROOT_DIR}/core/deps/install_tensorflow.sh
-                        RESULT_VARIABLE TENSORFLOW_RESULT
-        )
-    else ()
-        message("Tensorflow is already downloaded.")
-    endif()
-    set(TENSORFLOW_SOURCE_DIR "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
-
-    if (WASI_NN_ENABLE_GPU EQUAL 1)
+  if(NOT EXISTS "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
+    execute_process(
+      COMMAND "${WAMR_ROOT_DIR}/core/deps/install_tensorflow.sh"
+      RESULT_VARIABLE TENSORFLOW_RESULT
+    )
+  else()
+    message("Tensorflow is already downloaded.")
+  endif()
+
+  set(TENSORFLOW_SOURCE_DIR "${WAMR_ROOT_DIR}/core/deps/tensorflow-src")
+
+  if(WASI_NN_ENABLE_GPU EQUAL 1)
     # Tensorflow specific:
     # * https://www.tensorflow.org/lite/guide/build_cmake#available_options_to_build_tensorflow_lite
     set (TFLITE_ENABLE_GPU ON)
-    endif ()
+  endif()
 
-    include_directories (${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include)
-    include_directories (${TENSORFLOW_SOURCE_DIR})
-    add_subdirectory(
-        "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
-        "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL) 
+  add_subdirectory(
+    "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
+    "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
+    EXCLUDE_FROM_ALL
+  )  
 
-else()
-    find_path(TENSORFLOW_LITE_INCLUDE_DIR
-    NAMES tensorflow/lite/interpreter.h
-    )
-    find_path(FLATBUFFER_INCLUDE_DIR
-    NAMES flatbuffers/flatbuffers.h
-    )
-    include_directories (${TENSORFLOW_LITE_INCLUDE_DIR})
-    include_directories (${FLATBUFFER_INCLUDE_DIR})    
-endif()
+  set(TENSORFLOW_LITE_INCLUDE_DIR "${TENSORFLOW_SOURCE_DIR}")
+  set(FLATBUFFER_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers/include")
 
+  include_directories(${TENSORFLOW_LITE_INCLUDE_DIR})
+  include_directories(${FLATBUFFER_INCLUDE_DIR})
+endif()

+ 46 - 0
core/iwasm/libraries/wasi-nn/cmake/iwasm_helper.cmake

@@ -0,0 +1,46 @@
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+if (NOT DEFINED WAMR_BUILD_PLATFORM)
+  string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
+endif ()
+
+set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+set (CMAKE_C_STANDARD 99)
+
+if (NOT DEFINED WAMR_BUILD_TARGET)
+  if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
+    set (WAMR_BUILD_TARGET "AARCH64")
+  elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
+    set (WAMR_BUILD_TARGET "RISCV64")
+  elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
+    set (WAMR_BUILD_TARGET "X86_64")
+  elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
+    set (WAMR_BUILD_TARGET "X86_32")
+  else ()
+    message(SEND_ERROR "Unsupported build target platform!")
+  endif ()
+endif ()
+
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow -Wno-unused-parameter")
+
+set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused")
+
+if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
+  if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
+  endif ()
+endif ()
+
+set (WAMR_BUILD_INTERP 1)
+set (WAMR_BUILD_AOT 1)
+set (WAMR_BUILD_JIT 0)
+set (WAMR_BUILD_LIBC_WASI 1)
+set (WAMR_BUILD_FAST_INTERP 1)
+
+if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+  set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+endif ()
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")

+ 22 - 0
core/iwasm/libraries/wasi-nn/cmake/wasi_nn.cmake

@@ -0,0 +1,22 @@
+# Copyright (C) 2019 Intel Corporation.  All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
+
+# Find tensorflow-lite
+find_package(tensorflow_lite REQUIRED)
+
+set(WASI_NN_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
+
+include_directories (${WASI_NN_ROOT_DIR}/include)
+include_directories (${WASI_NN_ROOT_DIR}/src)
+include_directories (${WASI_NN_ROOT_DIR}/src/utils)
+
+set (
+  WASI_NN_SOURCES
+  ${WASI_NN_ROOT_DIR}/src/wasi_nn.c
+  ${WASI_NN_ROOT_DIR}/src/wasi_nn_tensorflowlite.cpp
+  ${WASI_NN_ROOT_DIR}/src/utils/wasi_nn_app_native.c
+)
+
+set (WASI_NN_LIBS tensorflow-lite)

+ 0 - 0
core/iwasm/libraries/wasi-nn/wasi_nn.h → core/iwasm/libraries/wasi-nn/include/wasi_nn.h


+ 3 - 0
core/iwasm/libraries/wasi-nn/wasi_nn_types.h → core/iwasm/libraries/wasi-nn/include/wasi_nn_types.h

@@ -6,6 +6,9 @@
 #ifndef WASI_NN_TYPES_H
 #define WASI_NN_TYPES_H
 
+#include <stdint.h>
+#include <stdbool.h>
+
 /**
  * ERRORS
  *

+ 1 - 1
core/iwasm/libraries/wasi-nn/test/CMakeLists.txt

@@ -162,7 +162,7 @@ add_executable (iwasm ${WAMR_ROOT_DIR}/product-mini/platforms/${WAMR_BUILD_PLATF
 
 install (TARGETS iwasm DESTINATION bin)
 
-target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} ${TENSORFLOW_LIB} -lm -ldl -lpthread)
+target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} ${WASI_NN_LIBS} -lm -ldl -lpthread)
 
 add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
 

+ 0 - 22
core/iwasm/libraries/wasi-nn/wasi_nn.cmake

@@ -1,22 +0,0 @@
-# Copyright (C) 2019 Intel Corporation.  All rights reserved.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
-
-# Find tensorflow-lite
-find_package(tensorflow_lite REQUIRED)
-
-set (WASI_NN_DIR ${CMAKE_CURRENT_LIST_DIR})
-
-include_directories (${WASI_NN_DIR})
-include_directories (${WASI_NN_DIR}/src)
-include_directories (${WASI_NN_DIR}/src/utils)
-
-set (
-    LIBC_WASI_NN_SOURCE
-    ${WASI_NN_DIR}/src/wasi_nn.c
-    ${WASI_NN_DIR}/src/wasi_nn_tensorflowlite.cpp
-    ${WASI_NN_DIR}/src/utils/wasi_nn_app_native.c
-)
-
-set (TENSORFLOW_LIB tensorflow-lite)

+ 2 - 2
product-mini/platforms/linux/CMakeLists.txt

@@ -155,7 +155,7 @@ set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
 
 install (TARGETS iwasm DESTINATION bin)
 
-target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
+target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} ${WASI_NN_LIBS} -lm -ldl -lpthread)
 
 add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE})
 
@@ -163,4 +163,4 @@ install (TARGETS libiwasm DESTINATION lib)
 
 set_target_properties (libiwasm PROPERTIES OUTPUT_NAME iwasm)
 
-target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
+target_link_libraries (libiwasm ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} ${WASI_NN_LIBS} -lm -ldl -lpthread)