build.sh 4.4 KB

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