build.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  4. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  5. #
  6. ####################################
  7. # build tensorflow-lite sample #
  8. ####################################
  9. if [ ! -d "${EMSDK}" ]; then
  10. echo "can not find emsdk. "
  11. echo "please refer to https://emscripten.org/docs/getting_started/downloads.html "
  12. echo "to install it, or active it by 'source <emsdk_dir>emsdk_env.sh'"
  13. exit
  14. fi
  15. set -xe
  16. EMSDK_WASM_DIR="${EMSDK}/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten"
  17. BUILD_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  18. OUT_DIR="${BUILD_SCRIPT_DIR}/out"
  19. TENSORFLOW_DIR="${BUILD_SCRIPT_DIR}/tensorflow"
  20. TF_LITE_BUILD_DIR="${TENSORFLOW_DIR}/tensorflow/lite/tools/make"
  21. WAMR_PLATFORM_DIR="${BUILD_SCRIPT_DIR}/../../../product-mini/platforms"
  22. WAMRC_DIR="${BUILD_SCRIPT_DIR}/../../../wamr-compiler"
  23. function Clear_Before_Exit
  24. {
  25. [[ -f ${TENSORFLOW_DIR}/tf_lite.patch ]] &&
  26. rm -f ${TENSORFLOW_DIR}/tf_lite.patch
  27. # resume the libc.a under EMSDK_WASM_DIR
  28. cd ${EMSDK_WASM_DIR}
  29. mv libc.a.bak libc.a
  30. }
  31. # 1.hack emcc
  32. cd ${EMSDK_WASM_DIR}
  33. # back up libc.a
  34. cp libc.a libc.a.bak
  35. # delete some objects in libc.a
  36. emar d libc.a open.o
  37. emar d libc.a mmap.o
  38. emar d libc.a munmap.o
  39. emranlib libc.a
  40. # 2. build tf-lite
  41. cd ${BUILD_SCRIPT_DIR}
  42. # 2.1 clone tf repo from Github and checkout to 2303ed commit
  43. if [ ! -d "tensorflow" ]; then
  44. git clone https://github.com/tensorflow/tensorflow.git
  45. fi
  46. cd ${TENSORFLOW_DIR}
  47. git checkout 2303ed4bdb344a1fc4545658d1df6d9ce20331dd
  48. # 2.2 copy the tf-lite.patch to tensorflow_root_dir and apply
  49. cd ${TENSORFLOW_DIR}
  50. cp ${BUILD_SCRIPT_DIR}/tf_lite.patch .
  51. git checkout tensorflow/lite/tools/make/Makefile
  52. git checkout tensorflow/lite/tools/make/targets/linux_makefile.inc
  53. if [[ $(git apply tf_lite.patch 2>&1) =~ "error" ]]; then
  54. echo "git apply patch failed, please check tf-lite related changes..."
  55. Clear_Before_Exit
  56. exit 0
  57. fi
  58. cd ${TF_LITE_BUILD_DIR}
  59. # 2.3 download dependencies
  60. if [ ! -d "${TF_LITE_BUILD_DIR}/downloads" ]; then
  61. source download_dependencies.sh
  62. fi
  63. # 2.4 build tf-lite target
  64. if [ -d "${TF_LITE_BUILD_DIR}/gen" ]; then
  65. rm -fr ${TF_LITE_BUILD_DIR}/gen
  66. fi
  67. make -j 4 -C "${TENSORFLOW_DIR}" -f ${TF_LITE_BUILD_DIR}/Makefile
  68. # remove patch file and recover emcc libc.a after building
  69. Clear_Before_Exit
  70. # 2.5 copy /make/gen target files to out/
  71. rm -rf ${OUT_DIR}
  72. mkdir ${OUT_DIR}
  73. cp -r ${TF_LITE_BUILD_DIR}/gen/linux_x86_64/bin/. ${OUT_DIR}/
  74. # 3. compile tf-model.wasm to tf-model.aot with wamrc
  75. # 3.1 build wamr-compiler
  76. cd ${WAMRC_DIR}
  77. ./build_llvm.sh
  78. rm -fr build && mkdir build
  79. cd build && cmake ..
  80. make
  81. # 3.2 compile tf-mode.wasm to tf-model.aot
  82. WAMRC_CMD="$(pwd)/wamrc"
  83. cd ${OUT_DIR}
  84. if [[ $1 == '--sgx' ]]; then
  85. ${WAMRC_CMD} -sgx -o benchmark_model.aot benchmark_model.wasm
  86. elif [[ $1 == '--threads' ]]; then
  87. ${WAMRC_CMD} --enable-multi-thread -o benchmark_model.aot benchmark_model.wasm
  88. else
  89. ${WAMRC_CMD} -o benchmark_model.aot benchmark_model.wasm
  90. fi
  91. # 4. build iwasm with pthread and libc_emcc enable
  92. # platform:
  93. # linux by default
  94. # linux-sgx if $1 equals '--sgx'
  95. if [[ $1 == '--sgx' ]]; then
  96. cd ${WAMR_PLATFORM_DIR}/linux-sgx
  97. rm -fr build && mkdir build
  98. cd build && cmake .. -DWAMR_BUILD_LIB_PTHREAD=1 -DWAMR_BUILD_LIBC_EMCC=1
  99. make
  100. cd ../enclave-sample
  101. make
  102. else
  103. cd ${WAMR_PLATFORM_DIR}/linux
  104. rm -fr build && mkdir build
  105. cd build && cmake .. -DWAMR_BUILD_LIB_PTHREAD=1 -DWAMR_BUILD_LIBC_EMCC=1
  106. make
  107. fi
  108. # 5. run tensorflow with iwasm
  109. cd ${BUILD_SCRIPT_DIR}
  110. # 5.1 download tf-lite model
  111. if [ ! -f mobilenet_quant_v1_224.tflite ]; then
  112. wget "https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_224_android_quant_2017_11_08.zip"
  113. unzip mobilenet_v1_224_android_quant_2017_11_08.zip
  114. fi
  115. # 5.2 run tf-lite model with iwasm
  116. echo "---> run tensorflow benchmark model with iwasm"
  117. if [[ $1 == '--sgx' ]]; then
  118. IWASM_CMD="${WAMR_PLATFORM_DIR}/linux-sgx/enclave-sample/iwasm"
  119. else
  120. IWASM_CMD="${WAMR_PLATFORM_DIR}/linux/build/iwasm"
  121. fi
  122. if [[ $1 == '--threads' ]]; then
  123. ${IWASM_CMD} --heap-size=10475860 \
  124. ${OUT_DIR}/benchmark_model.aot --num_threads=4 \
  125. --graph=mobilenet_quant_v1_224.tflite --max_secs=300
  126. else
  127. ${IWASM_CMD} --heap-size=10475860 \
  128. ${OUT_DIR}/benchmark_model.aot \
  129. --graph=mobilenet_quant_v1_224.tflite --max_secs=300
  130. fi