mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-07 12:16:24 +00:00
Update sample workload tensorflow (#2101)
Auto download emsdk under core/deps and hack it, and fix multi-thread issues.
This commit is contained in:
parent
5c201995f1
commit
24a7e5c1e6
|
@ -1,29 +1,19 @@
|
||||||
"tensorflow" sample introduction
|
"tensorflow" sample introduction
|
||||||
==============
|
==============
|
||||||
|
|
||||||
This sample demonstrates how to build [tensorflow](https://github.com/tensorflow/tensorflow) into WebAssembly with emsdk toolchain and run it with iwasm. Please first install [emsdk](https://github.com/emscripten-core/emsdk):
|
This sample demonstrates how to build [tensorflow](https://github.com/tensorflow/tensorflow) into WebAssembly with emsdk toolchain and run it with iwasm.:
|
||||||
```bash
|
|
||||||
git clone https://github.com/emscripten-core/emsdk.git
|
|
||||||
cd emsdk
|
|
||||||
./emsdk install 2.0.26
|
|
||||||
./emsdk activate 2.0.26
|
|
||||||
```
|
|
||||||
And set up ensdk environment:
|
|
||||||
```bash
|
|
||||||
source emsdk_env.sh
|
|
||||||
```
|
|
||||||
Then run
|
|
||||||
```bash
|
```bash
|
||||||
./build.sh
|
./build.sh
|
||||||
# for linux platform, or
|
# for linux platform, or
|
||||||
./build.sh --sgx
|
|
||||||
# for linux-sgx platform or
|
|
||||||
./build.sh --threads
|
./build.sh --threads
|
||||||
# for multi-thread execution (on linux platform)
|
# for multi-threading on linux platform
|
||||||
|
./build.sh --sgx
|
||||||
|
# for linux-sgx platform
|
||||||
```
|
```
|
||||||
to build tensorflow and run it with iwasm, which basically contains the following steps:
|
to build tensorflow and run it with iwasm, which basically contains the following steps:
|
||||||
|
- clone emsdk under `<wamr_dir>/core/deps`, install and activate 2.0.26
|
||||||
- hack emcc to delete some objects in libc.a
|
- hack emcc to delete some objects in libc.a
|
||||||
- build tf-lite with emcc compiler
|
- build tf-lite with emcc compiler
|
||||||
- build iwasm with pthread enable and include libiary under libc-emcc
|
- build iwasm with lib-pthread and libc-emcc enabled
|
||||||
- run benchmark model with iwasm:
|
- run benchmark model with iwasm:
|
||||||
--max-secs 300: means the max training time cost is 5 minutes, you can adjust it by yourself
|
--max-secs 300: means the max training time cost is 5 minutes, you can adjust it by yourself
|
||||||
|
|
|
@ -8,24 +8,20 @@
|
||||||
####################################
|
####################################
|
||||||
# build tensorflow-lite sample #
|
# build tensorflow-lite sample #
|
||||||
####################################
|
####################################
|
||||||
if [ ! -d "${EMSDK}" ]; then
|
|
||||||
echo "can not find emsdk. "
|
|
||||||
echo "please refer to https://emscripten.org/docs/getting_started/downloads.html "
|
|
||||||
echo "to install it, or active it by 'source <emsdk_dir>emsdk_env.sh'"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
set -xe
|
|
||||||
|
|
||||||
EMSDK_WASM_DIR="${EMSDK}/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten"
|
|
||||||
BUILD_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
BUILD_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
|
WAMR_DIR="${BUILD_SCRIPT_DIR}/../../.."
|
||||||
|
WAMR_PLATFORM_DIR="${WAMR_DIR}/product-mini/platforms"
|
||||||
|
WAMRC_DIR="${WAMR_DIR}/wamr-compiler"
|
||||||
|
CORE_DEPS_DIR="${WAMR_DIR}/core/deps"
|
||||||
|
EMSDK_DIR="${CORE_DEPS_DIR}/emsdk"
|
||||||
|
|
||||||
|
EMSDK_WASM_DIR="${EMSDK_DIR}/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten"
|
||||||
OUT_DIR="${BUILD_SCRIPT_DIR}/out"
|
OUT_DIR="${BUILD_SCRIPT_DIR}/out"
|
||||||
TENSORFLOW_DIR="${BUILD_SCRIPT_DIR}/tensorflow"
|
TENSORFLOW_DIR="${BUILD_SCRIPT_DIR}/tensorflow"
|
||||||
TF_LITE_BUILD_DIR="${TENSORFLOW_DIR}/tensorflow/lite/tools/make"
|
TF_LITE_BUILD_DIR="${TENSORFLOW_DIR}/tensorflow/lite/tools/make"
|
||||||
WAMR_PLATFORM_DIR="${BUILD_SCRIPT_DIR}/../../../product-mini/platforms"
|
|
||||||
WAMRC_DIR="${BUILD_SCRIPT_DIR}/../../../wamr-compiler"
|
|
||||||
|
|
||||||
function Clear_Before_Exit
|
function Clear_Before_Exit()
|
||||||
{
|
{
|
||||||
[[ -f ${TENSORFLOW_DIR}/tf_lite.patch ]] &&
|
[[ -f ${TENSORFLOW_DIR}/tf_lite.patch ]] &&
|
||||||
rm -f ${TENSORFLOW_DIR}/tf_lite.patch
|
rm -f ${TENSORFLOW_DIR}/tf_lite.patch
|
||||||
|
@ -34,7 +30,18 @@ function Clear_Before_Exit
|
||||||
mv libc.a.bak libc.a
|
mv libc.a.bak libc.a
|
||||||
}
|
}
|
||||||
|
|
||||||
# 1.hack emcc
|
set -xe
|
||||||
|
|
||||||
|
# 1.clone emsdk
|
||||||
|
cd ${CORE_DEPS_DIR}
|
||||||
|
rm -fr emsdk
|
||||||
|
git clone https://github.com/emscripten-core/emsdk.git
|
||||||
|
cd emsdk
|
||||||
|
./emsdk install 2.0.26
|
||||||
|
./emsdk activate 2.0.26
|
||||||
|
source emsdk_env.sh
|
||||||
|
|
||||||
|
# 2.hack emcc
|
||||||
cd ${EMSDK_WASM_DIR}
|
cd ${EMSDK_WASM_DIR}
|
||||||
# back up libc.a
|
# back up libc.a
|
||||||
cp libc.a libc.a.bak
|
cp libc.a libc.a.bak
|
||||||
|
@ -42,11 +49,13 @@ cp libc.a libc.a.bak
|
||||||
emar d libc.a open.o
|
emar d libc.a open.o
|
||||||
emar d libc.a mmap.o
|
emar d libc.a mmap.o
|
||||||
emar d libc.a munmap.o
|
emar d libc.a munmap.o
|
||||||
|
emar d libc.a library_pthread_stub.o
|
||||||
|
emar d libc.a pthread_self.o
|
||||||
emranlib libc.a
|
emranlib libc.a
|
||||||
|
|
||||||
# 2. build tf-lite
|
# 3. build tf-lite
|
||||||
cd ${BUILD_SCRIPT_DIR}
|
cd ${BUILD_SCRIPT_DIR}
|
||||||
# 2.1 clone tf repo from Github and checkout to 2303ed commit
|
# 3.1 clone tf repo from Github and checkout to 2303ed commit
|
||||||
if [ ! -d "tensorflow" ]; then
|
if [ ! -d "tensorflow" ]; then
|
||||||
git clone https://github.com/tensorflow/tensorflow.git
|
git clone https://github.com/tensorflow/tensorflow.git
|
||||||
fi
|
fi
|
||||||
|
@ -54,7 +63,7 @@ fi
|
||||||
cd ${TENSORFLOW_DIR}
|
cd ${TENSORFLOW_DIR}
|
||||||
git checkout 2303ed4bdb344a1fc4545658d1df6d9ce20331dd
|
git checkout 2303ed4bdb344a1fc4545658d1df6d9ce20331dd
|
||||||
|
|
||||||
# 2.2 copy the tf-lite.patch to tensorflow_root_dir and apply
|
# 3.2 copy the tf-lite.patch to tensorflow_root_dir and apply it
|
||||||
cd ${TENSORFLOW_DIR}
|
cd ${TENSORFLOW_DIR}
|
||||||
cp ${BUILD_SCRIPT_DIR}/tf_lite.patch .
|
cp ${BUILD_SCRIPT_DIR}/tf_lite.patch .
|
||||||
git checkout tensorflow/lite/tools/make/Makefile
|
git checkout tensorflow/lite/tools/make/Makefile
|
||||||
|
@ -67,12 +76,12 @@ if [[ $(git apply tf_lite.patch 2>&1) =~ "error" ]]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cd ${TF_LITE_BUILD_DIR}
|
cd ${TF_LITE_BUILD_DIR}
|
||||||
# 2.3 download dependencies
|
# 3.3 download dependencies
|
||||||
if [ ! -d "${TF_LITE_BUILD_DIR}/downloads" ]; then
|
if [ ! -d "${TF_LITE_BUILD_DIR}/downloads" ]; then
|
||||||
source download_dependencies.sh
|
source download_dependencies.sh
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 2.4 build tf-lite target
|
# 3.4 build tf-lite target
|
||||||
if [ -d "${TF_LITE_BUILD_DIR}/gen" ]; then
|
if [ -d "${TF_LITE_BUILD_DIR}/gen" ]; then
|
||||||
rm -fr ${TF_LITE_BUILD_DIR}/gen
|
rm -fr ${TF_LITE_BUILD_DIR}/gen
|
||||||
fi
|
fi
|
||||||
|
@ -82,19 +91,19 @@ make -j 4 -C "${TENSORFLOW_DIR}" -f ${TF_LITE_BUILD_DIR}/Makefile
|
||||||
# remove patch file and recover emcc libc.a after building
|
# remove patch file and recover emcc libc.a after building
|
||||||
Clear_Before_Exit
|
Clear_Before_Exit
|
||||||
|
|
||||||
# 2.5 copy /make/gen target files to out/
|
# 3.5 copy /make/gen target files to out/
|
||||||
rm -rf ${OUT_DIR}
|
rm -rf ${OUT_DIR}
|
||||||
mkdir ${OUT_DIR}
|
mkdir ${OUT_DIR}
|
||||||
cp -r ${TF_LITE_BUILD_DIR}/gen/linux_x86_64/bin/. ${OUT_DIR}/
|
cp -r ${TF_LITE_BUILD_DIR}/gen/linux_x86_64/bin/. ${OUT_DIR}/
|
||||||
|
|
||||||
# 3. compile tf-model.wasm to tf-model.aot with wamrc
|
# 4. compile tf-model.wasm to tf-model.aot with wamrc
|
||||||
# 3.1 build wamr-compiler
|
# 4.1 build wamr-compiler
|
||||||
cd ${WAMRC_DIR}
|
cd ${WAMRC_DIR}
|
||||||
./build_llvm.sh
|
./build_llvm.sh
|
||||||
rm -fr build && mkdir build
|
rm -fr build && mkdir build
|
||||||
cd build && cmake ..
|
cd build && cmake ..
|
||||||
make
|
make
|
||||||
# 3.2 compile tf-mode.wasm to tf-model.aot
|
# 4.2 compile tf-mode.wasm to tf-model.aot
|
||||||
WAMRC_CMD="$(pwd)/wamrc"
|
WAMRC_CMD="$(pwd)/wamrc"
|
||||||
cd ${OUT_DIR}
|
cd ${OUT_DIR}
|
||||||
if [[ $1 == '--sgx' ]]; then
|
if [[ $1 == '--sgx' ]]; then
|
||||||
|
@ -105,14 +114,14 @@ else
|
||||||
${WAMRC_CMD} -o benchmark_model.aot benchmark_model.wasm
|
${WAMRC_CMD} -o benchmark_model.aot benchmark_model.wasm
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 4. build iwasm with pthread and libc_emcc enable
|
# 5. build iwasm with pthread and libc_emcc enable
|
||||||
# platform:
|
# platform:
|
||||||
# linux by default
|
# linux by default
|
||||||
# linux-sgx if $1 equals '--sgx'
|
# linux-sgx if $1 equals '--sgx'
|
||||||
if [[ $1 == '--sgx' ]]; then
|
if [[ $1 == '--sgx' ]]; then
|
||||||
cd ${WAMR_PLATFORM_DIR}/linux-sgx
|
cd ${WAMR_PLATFORM_DIR}/linux-sgx
|
||||||
rm -fr build && mkdir build
|
rm -fr build && mkdir build
|
||||||
cd build && cmake .. -DWAMR_BUILD_LIB_PTHREAD=1 -DWAMR_BUILD_LIBC_EMCC=1
|
cd build && cmake .. -DWAMR_BUILD_LIBC_EMCC=1
|
||||||
make
|
make
|
||||||
cd ../enclave-sample
|
cd ../enclave-sample
|
||||||
make
|
make
|
||||||
|
@ -123,15 +132,13 @@ else
|
||||||
make
|
make
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 5. run tensorflow with iwasm
|
# 6. run tensorflow with iwasm
|
||||||
cd ${BUILD_SCRIPT_DIR}
|
cd ${OUT_DIR}
|
||||||
# 5.1 download tf-lite model
|
# 6.1 download tf-lite model
|
||||||
if [ ! -f mobilenet_quant_v1_224.tflite ]; then
|
wget "https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip"
|
||||||
wget "https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip"
|
unzip mobilenet_v1_224_android_quant_2017_11_08.zip
|
||||||
unzip mobilenet_v1_224_android_quant_2017_11_08.zip
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 5.2 run tf-lite model with iwasm
|
# 6.2 run tf-lite model with iwasm
|
||||||
echo "---> run tensorflow benchmark model with iwasm"
|
echo "---> run tensorflow benchmark model with iwasm"
|
||||||
if [[ $1 == '--sgx' ]]; then
|
if [[ $1 == '--sgx' ]]; then
|
||||||
IWASM_CMD="${WAMR_PLATFORM_DIR}/linux-sgx/enclave-sample/iwasm"
|
IWASM_CMD="${WAMR_PLATFORM_DIR}/linux-sgx/enclave-sample/iwasm"
|
||||||
|
@ -139,13 +146,12 @@ else
|
||||||
IWASM_CMD="${WAMR_PLATFORM_DIR}/linux/build/iwasm"
|
IWASM_CMD="${WAMR_PLATFORM_DIR}/linux/build/iwasm"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $1 == '--threads' ]]; then
|
if [[ $1 == '--threads' ]]; then
|
||||||
${IWASM_CMD} --heap-size=10475860 \
|
${IWASM_CMD} --heap-size=10475860 \
|
||||||
${OUT_DIR}/benchmark_model.aot --num_threads=4 \
|
benchmark_model.aot --num_threads=4 \
|
||||||
--graph=mobilenet_quant_v1_224.tflite --max_secs=300
|
--graph=mobilenet_quant_v1_224.tflite --max_secs=300
|
||||||
else
|
else
|
||||||
${IWASM_CMD} --heap-size=10475860 \
|
${IWASM_CMD} --heap-size=10475860 \
|
||||||
${OUT_DIR}/benchmark_model.aot \
|
benchmark_model.aot \
|
||||||
--graph=mobilenet_quant_v1_224.tflite --max_secs=300
|
--graph=mobilenet_quant_v1_224.tflite --max_secs=300
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user