forked from bytecodealliance/wasm-micro-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate WASI-NN into WAMR (bytecodealliance#1521)
Initial integration of WASI-NN based on bytecodealliance#1225: - Implement the library core/iwasm/libraries/wasi-nn - Support TensorFlow, CPU, F32 at the first stage - Add cmake variable `-DWAMR_BUILD_WASI_NN` - Add test case based on Docker image and update document Refer to bytecodealliance#1573
- Loading branch information
1 parent
78c38d0
commit e53ab91
Showing
25 changed files
with
1,459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.cache | ||
.vs | ||
.vscode | ||
.venv | ||
/.idea | ||
**/cmake-build-*/ | ||
**/*build/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
|
||
DEPS_ROOT=$(cd "$(dirname "$0")/" && pwd) | ||
cd ${DEPS_ROOT} | ||
|
||
echo "Downloading tensorflow in ${PWD}..." | ||
|
||
git clone https://github.com/tensorflow/tensorflow.git tensorflow-src \ | ||
--branch v2.9.2 | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# WASI-NN | ||
|
||
## How to use | ||
|
||
Enable WASI-NN in the WAMR by spefiying it in the cmake building configuration as follows, | ||
|
||
``` | ||
set (WAMR_BUILD_WASI_NN 1) | ||
``` | ||
|
||
The definition of the functions provided by WASI-NN is in the header file `core/iwasm/libraries/wasi-nn/wasi_nn.h`. | ||
|
||
By only including this file in your WASM application you will bind WASI-NN into your module. | ||
|
||
## Tests | ||
|
||
To run the tests we assume that the current directory is the root of the repository. | ||
|
||
|
||
1. Build the docker image, | ||
|
||
``` | ||
docker build -t wasi-nn -f core/iwasm/libraries/wasi-nn/test/Dockerfile . | ||
``` | ||
|
||
2. Run the container | ||
|
||
``` | ||
docker run wasi-nn | ||
``` | ||
|
||
If all the tests have run properly you will the the following message in the terminal, | ||
|
||
``` | ||
Tests: passed! | ||
``` | ||
|
||
## What is missing | ||
|
||
* Only 1 model at a time is supported. | ||
* `graph` and `graph-execution-context` are ignored. | ||
* Only `tensorflow` (lite) is supported. | ||
* Only `cpu` is supported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2019 Intel Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
*/ | ||
|
||
#ifndef WASI_NN_LOGGER_H | ||
#define WASI_NN_LOGGER_H | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define __FILENAME__ \ | ||
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | ||
|
||
/* Disable a level by removing the define */ | ||
#define ENABLE_ERR_LOG | ||
#define ENABLE_WARN_LOG | ||
#define ENABLE_DBG_LOG | ||
#define ENABLE_INFO_LOG | ||
|
||
// Definition of the levels | ||
#ifdef ENABLE_ERR_LOG | ||
#define NN_ERR_PRINTF(fmt, ...) \ | ||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \ | ||
printf("\n"); \ | ||
fflush(stdout) | ||
#else | ||
#define NN_ERR_PRINTF(fmt, ...) | ||
#endif | ||
#ifdef ENABLE_WARN_LOG | ||
#define NN_WARN_PRINTF(fmt, ...) \ | ||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \ | ||
printf("\n"); \ | ||
fflush(stdout) | ||
#else | ||
#define NN_WARN_PRINTF(fmt, ...) | ||
#endif | ||
#ifdef ENABLE_DBG_LOG | ||
#define NN_DBG_PRINTF(fmt, ...) \ | ||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \ | ||
printf("\n"); \ | ||
fflush(stdout) | ||
#else | ||
#define NN_DBG_PRINTF(fmt, ...) | ||
#endif | ||
#ifdef ENABLE_INFO_LOG | ||
#define NN_INFO_PRINTF(fmt, ...) \ | ||
printf("[%s:%d] " fmt, __FILENAME__, __LINE__, ##__VA_ARGS__); \ | ||
printf("\n"); \ | ||
fflush(stdout) | ||
#else | ||
#define NN_INFO_PRINTF(fmt, ...) | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
# Copyright (C) 2019 Intel Corporation. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
cmake_minimum_required (VERSION 2.9) | ||
|
||
project (iwasm) | ||
|
||
set (CMAKE_VERBOSE_MAKEFILE OFF) | ||
# Reset default linker flags | ||
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") | ||
set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") | ||
set (CMAKE_C_STANDARD 99) | ||
set (CMAKE_CXX_STANDARD 14) | ||
|
||
if (NOT DEFINED WAMR_BUILD_PLATFORM) | ||
set (WAMR_BUILD_PLATFORM "linux") | ||
endif () | ||
|
||
# Set WAMR_BUILD_TARGET, currently values supported: | ||
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]", | ||
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]" | ||
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) | ||
# Build as X86_64 by default in 64-bit platform | ||
set (WAMR_BUILD_TARGET "X86_64") | ||
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4) | ||
# Build as X86_32 by default in 32-bit platform | ||
set (WAMR_BUILD_TARGET "X86_32") | ||
else () | ||
message(SEND_ERROR "Unsupported build target platform!") | ||
endif () | ||
endif () | ||
|
||
if (NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Release) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_INTERP) | ||
# Enable Interpreter by default | ||
set (WAMR_BUILD_INTERP 1) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_AOT) | ||
# Enable AOT by default. | ||
set (WAMR_BUILD_AOT 1) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_JIT) | ||
# Disable JIT by default. | ||
set (WAMR_BUILD_JIT 0) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_FAST_JIT) | ||
# Disable Fast JIT by default | ||
set (WAMR_BUILD_FAST_JIT 0) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN) | ||
# Enable libc builtin support by default | ||
set (WAMR_BUILD_LIBC_BUILTIN 1) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_LIBC_WASI) | ||
# Enable libc wasi support by default | ||
set (WAMR_BUILD_LIBC_WASI 1) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_FAST_INTERP) | ||
# Enable fast interpreter | ||
set (WAMR_BUILD_FAST_INTERP 1) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_MULTI_MODULE) | ||
# Disable multiple modules by default | ||
set (WAMR_BUILD_MULTI_MODULE 0) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD) | ||
# Disable pthread library by default | ||
set (WAMR_BUILD_LIB_PTHREAD 0) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_MINI_LOADER) | ||
# Disable wasm mini loader by default | ||
set (WAMR_BUILD_MINI_LOADER 0) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_SIMD) | ||
# Enable SIMD by default | ||
set (WAMR_BUILD_SIMD 1) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_REF_TYPES) | ||
# Disable reference types by default | ||
set (WAMR_BUILD_REF_TYPES 0) | ||
endif () | ||
|
||
if (NOT DEFINED WAMR_BUILD_DEBUG_INTERP) | ||
# Disable Debug feature by default | ||
set (WAMR_BUILD_DEBUG_INTERP 0) | ||
endif () | ||
|
||
if (WAMR_BUILD_DEBUG_INTERP EQUAL 1) | ||
set (WAMR_BUILD_FAST_INTERP 0) | ||
set (WAMR_BUILD_MINI_LOADER 0) | ||
set (WAMR_BUILD_SIMD 0) | ||
endif () | ||
|
||
if (COLLECT_CODE_COVERAGE EQUAL 1) | ||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") | ||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") | ||
endif () | ||
|
||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..) | ||
|
||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake) | ||
add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE}) | ||
|
||
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -pie -fPIE") | ||
|
||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow") | ||
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion") | ||
|
||
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") | ||
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mindirect-branch-register") | ||
# UNDEFINED BEHAVIOR, refer to https://en.cppreference.com/w/cpp/language/ub | ||
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT WAMR_BUILD_JIT EQUAL 1) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined \ | ||
-fno-sanitize=bounds,bounds-strict,alignment \ | ||
-fno-sanitize-recover") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined \ | ||
-fno-sanitize=bounds,bounds-strict,alignment \ | ||
-fno-sanitize-recover") | ||
endif() | ||
else () | ||
# UNDEFINED BEHAVIOR, refer to https://en.cppreference.com/w/cpp/language/ub | ||
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT WAMR_BUILD_JIT EQUAL 1) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined \ | ||
-fno-sanitize=bounds,alignment \ | ||
-fno-sanitize-recover") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined \ | ||
-fno-sanitize=bounds,alignment \ | ||
-fno-sanitize-recover") | ||
endif() | ||
endif () | ||
endif () | ||
|
||
# The following flags are to enhance security, but it may impact performance, | ||
# we disable them by default. | ||
#if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64") | ||
# set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv -D_FORTIFY_SOURCE=2") | ||
#endif () | ||
#set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4") | ||
#set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now") | ||
|
||
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake) | ||
|
||
add_executable (iwasm ${WAMR_ROOT_DIR}/product-mini/platforms/${WAMR_BUILD_PLATFORM}/main.c ${UNCOMMON_SHARED_SOURCE}) | ||
|
||
install (TARGETS iwasm DESTINATION bin) | ||
|
||
target_link_libraries (iwasm vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} ${TENSORFLOW_LIB} -lm -ldl -lpthread) | ||
|
||
add_library (libiwasm SHARED ${WAMR_RUNTIME_LIB_SOURCE}) | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (C) 2019 Intel Corporation. All rights reserved. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
FROM ubuntu:22.04 | ||
|
||
ENV DEBIAN_FRONTEND=noninteractive | ||
|
||
RUN apt-get update && apt-get install -y \ | ||
cmake build-essential git wget python3.10 python3-pip | ||
|
||
RUN wget -q https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-14/wasi-sdk-14.0-linux.tar.gz && \ | ||
tar xf wasi-sdk-*-linux.tar.gz -C /opt && rm -f wasi-sdk-*-linux.tar.gz && \ | ||
mv /opt/wasi-sdk-14.0 /opt/wasi-sdk | ||
|
||
WORKDIR /home/wamr | ||
|
||
COPY core core | ||
COPY build-scripts build-scripts | ||
COPY product-mini product-mini | ||
|
||
RUN pip3 install -r core/iwasm/libraries/wasi-nn/test/requirements.txt | ||
|
||
WORKDIR /home/wamr/core/iwasm/libraries/wasi-nn/test/build | ||
|
||
RUN cmake -DWAMR_BUILD_WASI_NN=1 .. | ||
RUN make -j $(grep -c ^processor /proc/cpuinfo) | ||
|
||
WORKDIR /home/wamr/core/iwasm/libraries/wasi-nn/test | ||
|
||
RUN ./build.sh | ||
|
||
ENTRYPOINT [ "./build/iwasm", "--dir=.", "test_tensorflow.wasm" ] |
Oops, something went wrong.