Compare commits

..

No commits in common. "54b87cb0973040d7a981abe67efa653ea60f9600" and "cfffb62ad2cbe217dd7ec122a3b7a16aac236997" have entirely different histories.

7 changed files with 38 additions and 222 deletions

View File

@ -1,16 +0,0 @@
# for now, it is used to speed up wasi-nn tests only.
# you shall adapt below rules to incoming requirements
build
*/build
*/*/build
*/*/*/build
*/*/*/*/build
*/*/*/*/*/build
*/*/*/*/*/*/build
.*
core/deps
!core/deps/tensorflow-src
samples
tests

View File

@ -2883,24 +2883,10 @@ aot_invoke_native(WASMExecEnv *exec_env, uint32 func_idx, uint32 argc,
"create singleton exec_env failed"); "create singleton exec_env failed");
goto fail; goto fail;
} }
#if WASM_ENABLE_AOT_STACK_FRAME != 0
struct WASMInterpFrame *prev_frame = exec_env->cur_frame;
if (!aot_alloc_frame(exec_env, func_idx)) {
goto fail;
}
#endif #endif
#endif /* WASM_ENABLE_MULTI_MODULE != 0 */
ret = ret =
wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature, wasm_runtime_invoke_native(exec_env, func_ptr, func_type, signature,
attachment, argv, argc, argv); attachment, argv, argc, argv);
#if WASM_ENABLE_MULTI_MODULE != 0 && WASM_ENABLE_AOT_STACK_FRAME != 0
/* Free all frames allocated, note that some frames
may be allocated in AOT code and haven't been
freed if exception occurred */
while (exec_env->cur_frame != prev_frame)
aot_free_frame(exec_env);
#endif
} }
else { else {
signature = import_func->signature; signature = import_func->signature;

View File

@ -27,7 +27,7 @@ For some historical reasons, there are two sets of functions in the header file.
There is a big difference between the two sets of functions, `tensor_type`. There is a big difference between the two sets of functions, `tensor_type`.
```c ``` c
#if WASM_ENABLE_WASI_EPHEMERAL_NN != 0 #if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
typedef enum { fp16 = 0, fp32, fp64, bf16, u8, i32, i64 } tensor_type; typedef enum { fp16 = 0, fp32, fp64, bf16, u8, i32, i64 } tensor_type;
#else #else
@ -147,35 +147,39 @@ Supported:
## Smoke test ## Smoke test
### Testing with WasmEdge-WASINN Examples Use [classification-example](https://github.com/bytecodealliance/wasi-nn/tree/main/rust/examples/classification-example) as a smoke test case to make sure the wasi-nn support in WAMR is working properly.
To ensure everything is set up correctly, use the examples from [WasmEdge-WASINN-examples](https://github.com/second-state/WasmEdge-WASINN-examples/tree/master). These examples help verify that WASI-NN support in WAMR is functioning as expected. > [!Important]
> It requires openvino.
> Note: The repository contains two types of examples. Some use the [standard wasi-nn](https://github.com/WebAssembly/wasi-nn), while others use [WasmEdge's version of wasi-nn](https://github.com/second-state/wasmedge-wasi-nn), which is enhanced to meet specific customer needs. ### Prepare the model and the wasm
The examples test the following machine learning backends:
- OpenVINO
- PyTorch
- TensorFlow Lite
Due to the different requirements of each backend, we'll use a Docker container for a hassle-free testing environment.
#### Prepare the execution environment
```bash ```bash
$ pwd $ pwd
/workspaces/wasm-micro-runtime/ /workspaces/wasm-micro-runtime/core/iwasm/libraries/wasi-nn/test
$ docker build -t wasi-nn-smoke:v1.0 -f Dockerfile.wasi-nn-smoke . $ docker build -t wasi-nn-example:v1.0 -f Dockerfile.wasi-nn-example .
``` ```
#### Execute There are model files(\*mobilenet\**) and wasm files(*wasi-nn-example.wasm*) in the directory */workspaces/wasi-nn/rust/examples/classification-example/build\* in the image of wasi-nn-example:v1.0.
### build iwasm and test
_TODO: May need alternative steps to build the iwasm and test in the container of wasi-nn-example:v1.0_
```bash ```bash
$ docker run --rm wasi-nn-smoke:v1.0 $ pwd
/workspaces/wasm-micro-runtime
$ docker run --rm -it -v $(pwd):/workspaces/wasm-micro-runtime wasi-nn-example:v1.0 /bin/bash
``` ```
### Testing with bytecodealliance wasi-nn > [!Caution]
> The following steps are executed in the container of wasi-nn-example:v1.0.
For another example, check out [classification-example](https://github.com/bytecodealliance/wasi-nn/tree/main/rust/examples/classification-example), which focuses on OpenVINO. You can run it using the same Docker container mentioned above. ```bash
$ cd /workspaces/wasm-micro-runtime/product-mini/platforms/linux
$ cmake -S . -B build -DWAMR_BUILD_WASI_NN=1 -DWAMR_BUILD_WASI_EPHEMERAL_NN=1
$ cmake --build build
$ ./build/iwasm -v=5 --map-dir=/workspaces/wasi-nn/rust/examples/classification-example/build/::fixture /workspaces/wasi-nn/rust/examples/classification-example/build/wasi-nn-example.wasm
```

View File

@ -1,7 +1,7 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved. # Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
FROM mcr.microsoft.com/devcontainers/rust:1-1-bullseye@sha256:ddc1ee022d327f024c07484c9333db3fbbfd504bc096cdb66635653a2bebb33e FROM mcr.microsoft.com/devcontainers/rust:1-1-bullseye
ARG DEBIAN_FRONTEND=noninteractive ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asian/Shanghai ENV TZ=Asian/Shanghai
@ -10,6 +10,10 @@ ENV TZ=Asian/Shanghai
RUN apt-get update \ RUN apt-get update \
&& apt-get upgrade -y && apt-get upgrade -y
#
# Rust targets
RUN rustup target add wasm32-wasi wasm32-unknown-unknown
# #
# Openvino # Openvino
# Refer to # Refer to
@ -38,43 +42,16 @@ RUN tar -xf wasmtime-v21.0.0-x86_64-linux.tar.xz \
# #
# wasi-nn # wasi-nn
# compilation requirements
RUN rustup target add wasm32-wasi wasm32-unknown-unknown
WORKDIR /workspaces/wasi-nn WORKDIR /workspaces/wasi-nn
RUN git clone --depth 1 https://github.com/bytecodealliance/wasi-nn.git . RUN git clone --depth 1 https://github.com/bytecodealliance/wasi-nn.git .
# hadolint ignore=DL3059 # hadolint ignore=DL3059
#RUN ./build.sh rust RUN ./build.sh rust
# There are model files(mobilenet*) and wasm files(wasi-nn-example.wasm) in the directory, # There are model files(mobilenet*) and wasm files(wasi-nn-example.wasm) in the directory,
# /workspaces/wasi-nn/rust/examples/classification-example/build # /workspaces/wasi-nn/rust/examples/classification-example/build
# RUN apt-get autoremove -y \
# wasmedge && apt-get clean -y \
WORKDIR /tmp && rm -rf /tmp/*
RUN wget -q https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh \
&& chmod a+x ./install.sh
RUN ./install.sh -p /opt/wasmedge --plugins wasi_nn-tensorflowlite
ENV PATH=/opt/wasmedge/bin:${PATH}
ENV WASMEDGE_LIB_DIR=/opt/wasmedge/lib
# WORKDIR /workspaces
# wasmedge-wasinn-examples
WORKDIR /workspaces/wasmedge-wasinn-examples
RUN git clone --depth 1 https://github.com/second-state/WasmEdge-WASINN-examples.git .
#
# iwasm. build from source
WORKDIR /workspaces/wamr
COPY . .
WORKDIR /workspaces/wamr/product-mini/platforms/linux
RUN cmake -S . -B build -DWAMR_BUILD_WASI_NN=1 -DWAMR_BUILD_WASI_EPHEMERAL_NN=1 \
&& cmake --build build
RUN ln -sf "$(realpath ./build/iwasm)" /usr/local/bin/iwasm
#
# add smoke test script
COPY core/iwasm/libraries/wasi-nn/test/run_smoke_test.py /
#
WORKDIR /workspaces/wasmedge-wasinn-examples
CMD ["python3", "/run_smoke_test.py"]

View File

@ -1,120 +0,0 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
from pathlib import Path
from pprint import pprint
import re
import shlex
import shutil
import subprocess
from typing import List
def execute_tflite_birds_v1_image_once(
runtime_bin: str, runtime_args: List[str], cwd: Path
) -> str:
"""
execute tflite_birds_v1_image example with
```
iwasm --native-lib=somewhere/libwasi-nn-tflite.so --map-dir=.:. \
./wasmedge-wasinn-example-tflite-bird-image.wasm \
lite-model_aiy_vision_classifier_birds_V1_3.tflite \
bird.jpg
```
or
```
wasmedge --dir=.:. \
./wasmedge-wasinn-example-tflite-bird-image.wasm \
lite-model_aiy_vision_classifier_birds_V1_3.tflite \
bird.jpg
```
assumption:
- under the right directory, tflite-birds_v1-image
- every materials are ready
"""
wasm_file = "./wasmedge-wasinn-example-tflite-bird-image.wasm"
wasm_args = ["lite-model_aiy_vision_classifier_birds_V1_3.tflite", "bird.jpg"]
cmd = [runtime_bin]
cmd.extend(runtime_args)
cmd.append(wasm_file)
cmd.extend(wasm_args)
try:
p = subprocess.run(
cmd,
cwd=cwd,
capture_output=True,
check=True,
text=True,
universal_newlines=True,
)
return p.stdout
except subprocess.CalledProcessError as e:
print(e.stderr)
print()
print(e.stdout)
def filter_output_tflite_birds_v1_image(output: str) -> List[str]:
"""
not all output is needed for comparision
pick lines like: " 1.) [526](136)Cathartes burrovianus"
"""
filtered = []
PATTERN = re.compile(r"^\s+\d\.\)\s+\[\d+\]\(\d+\)\w+")
for line in output.split("\n"):
if PATTERN.search(line):
filtered.append(line.strip())
return filtered
def execute_tflite_birds_v1_image(iwasm_bin: str, wasmedge_bin: str, cwd: Path):
iwasm_output = execute_tflite_birds_v1_image_once(
iwasm_bin,
[
"--native-lib=/workspaces/wamr/product-mini/platforms/linux/build/libwasi-nn-tflite.so",
"--map-dir=.:.",
],
cwd,
)
iwasm_output = filter_output_tflite_birds_v1_image(iwasm_output)
wasmedge_output = execute_tflite_birds_v1_image_once(
wasmedge_bin, ["--dir=.:."], cwd
)
wasmedge_output = filter_output_tflite_birds_v1_image(wasmedge_output)
if iwasm_output == wasmedge_output:
print("- tflite_birds_v1_image. PASS")
return
print("- tflite_birds_v1_image. FAILED")
print("------------------------------------------------------------")
pprint(iwasm_output)
print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
pprint(wasmedge_output)
print("------------------------------------------------------------")
def execute_wasmedge_wasinn_exmaples(iwasm_bin: str, wasmedge_bin: str):
assert Path.cwd().name == "wasmedge-wasinn-examples"
assert shutil.which(iwasm_bin)
assert shutil.which(wasmedge_bin)
tflite_birds_v1_image_dir = Path.cwd().joinpath("./tflite-birds_v1-image")
execute_tflite_birds_v1_image(iwasm_bin, wasmedge_bin, tflite_birds_v1_image_dir)
if __name__ == "__main__":
execute_wasmedge_wasinn_exmaples("iwasm", "wasmedge")

View File

@ -49,12 +49,6 @@ endif ()
if (NOT DEFINED WAMR_BUILD_JIT) if (NOT DEFINED WAMR_BUILD_JIT)
set(WAMR_BUILD_JIT 0) set(WAMR_BUILD_JIT 0)
endif () endif ()
if (NOT DEFINED WAMR_BUILD_DUMP_CALL_STACK)
set(WAMR_BUILD_DUMP_CALL_STACK 0)
endif ()
if (NOT DEFINED WAMR_BUILD_GC)
set(WAMR_BUILD_GC 0)
endif ()
set(WAMR_BUILD_SIMD 1) set(WAMR_BUILD_SIMD 1)
set(WAMR_BUILD_REF_TYPES 1) set(WAMR_BUILD_REF_TYPES 1)
set(WAMR_BUILD_LIBC_BUILTIN 1) set(WAMR_BUILD_LIBC_BUILTIN 1)
@ -147,7 +141,6 @@ ExternalProject_Add(WASM_MODULE
-DWASI_SDK_PREFIX=${WASI_SDK_DIR} -DWASI_SDK_PREFIX=${WASI_SDK_DIR}
-DCMAKE_TOOLCHAIN_FILE=${WASI_TOOLCHAIN_FILE} -DCMAKE_TOOLCHAIN_FILE=${WASI_TOOLCHAIN_FILE}
-DCMAKE_SYSROOT=${WASI_SYS_ROOT} -DCMAKE_SYSROOT=${WASI_SYS_ROOT}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps -S ${CMAKE_CURRENT_SOURCE_DIR}/wasm-apps
BUILD_COMMAND ${CMAKE_COMMAND} --build . BUILD_COMMAND ${CMAKE_COMMAND} --build .
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy INSTALL_COMMAND ${CMAKE_COMMAND} -E copy
@ -179,24 +172,17 @@ if (WAMR_BUILD_AOT EQUAL 1)
message(STATUS "WAMR_COMPILER is ${WAMR_COMPILER}") message(STATUS "WAMR_COMPILER is ${WAMR_COMPILER}")
endif() endif()
if (WAMR_BUILD_DUMP_CALL_STACK EQUAL 1)
list(APPEND WAMR_AOT_COMPILE_OPTIONS "--enable-dump-call-stack")
endif ()
if (WAMR_BUILD_GC EQUAL 1)
list(APPEND WAMR_AOT_COMPILE_OPTIONS "--enable-gc")
endif ()
add_custom_target( add_custom_target(
wasm_to_aot wasm_to_aot
ALL ALL
DEPENDS DEPENDS
WASM_MODULE ${WAMR_COMPILER} WASM_MODULE ${WAMR_COMPILER}
COMMAND COMMAND
${WAMR_COMPILER} ${WAMR_AOT_COMPILE_OPTIONS} -o mA.aot ./mA.wasm ${WAMR_COMPILER} -o mA.aot ./mA.wasm
COMMAND COMMAND
${WAMR_COMPILER} ${WAMR_AOT_COMPILE_OPTIONS} -o mB.aot ./mB.wasm ${WAMR_COMPILER} -o mB.aot ./mB.wasm
COMMAND COMMAND
${WAMR_COMPILER} ${WAMR_AOT_COMPILE_OPTIONS} -o mC.aot ./mC.wasm ${WAMR_COMPILER} -o mC.aot ./mC.wasm
WORKING_DIRECTORY WORKING_DIRECTORY
${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}
) )

View File

@ -57,7 +57,6 @@ function(COMPILE_WITH_CLANG SOURCE_FILE COMMAND)
add_executable(${MAIN_TARGET_NAME} ${SOURCE_FILE}) add_executable(${MAIN_TARGET_NAME} ${SOURCE_FILE})
set_target_properties(${MAIN_TARGET_NAME} PROPERTIES OUTPUT_NAME ${WASM_MODULE}) set_target_properties(${MAIN_TARGET_NAME} PROPERTIES OUTPUT_NAME ${WASM_MODULE})
target_compile_options (${MAIN_TARGET_NAME} PRIVATE -fno-exceptions)
if(${COMMAND}) if(${COMMAND})
message(STATUS "Generating ${WASM_MODULE} as COMMAND...") message(STATUS "Generating ${WASM_MODULE} as COMMAND...")