You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

build.sh 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. #!/bin/bash
  2. # Copyright 2019 Huawei Technologies Co., Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # ============================================================================
  16. set -e
  17. BASEPATH=$(cd "$(dirname $0)"; pwd)
  18. PROJECT_PATH="${BASEPATH}"
  19. CUDA_PATH=""
  20. CUDNN_PATH=""
  21. export BUILD_PATH="${BASEPATH}/build/"
  22. # print usage message
  23. usage()
  24. {
  25. echo "Usage:"
  26. echo "bash build.sh [-d] [-r] [-v] [-c on|off] [-t on|off] [-g on|off] [-h] [-s] [-b ge|cpu] [-m infer|train] \\"
  27. echo " [-a on|off] [-g on|off] [-p on|off] [-i] [-L] [-R] [-D on|off] [-j[n]] [-e gpu|d|cpu] \\"
  28. echo " [-P on|off] [-z] [-M on|off] [-V 9.2|10.1] [-I] [-K]"
  29. echo ""
  30. echo "Options:"
  31. echo " -d Debug mode"
  32. echo " -r Release mode, default mode"
  33. echo " -v Display build command"
  34. echo " -c Enable code coverage switch, default off"
  35. echo " -t Run testcases switch, default on"
  36. echo " -g Use glog to output log, default on"
  37. echo " -h Print usage"
  38. echo " -s Install or setup"
  39. echo " -b Select other backend, available: \\"
  40. echo " ge:graph engine, cpu"
  41. echo " -m Select mode, available: infer, train, default is infer "
  42. echo " -a Enable ASAN, default off"
  43. echo " -p Enable pipeline profile, default off"
  44. echo " -i Enable increment building, default off"
  45. echo " -L Enable load ANF-IR as input of 'infer', default off"
  46. echo " -R Enable the time_line record, default off"
  47. echo " -j[n] Set the threads when building (Default: -j8)"
  48. echo " -e Use gpu, d or cpu"
  49. echo " -P Enable dump anf graph to file in ProtoBuffer format, default on"
  50. echo " -Q Enable dump end to end, default off"
  51. echo " -D Enable dumping of function graph ir, default on"
  52. echo " -z Compile dataset & mindrecord, default off"
  53. echo " -M Enable MPI and NCCL for GPU training, default off"
  54. echo " -V Specify the minimum required cuda version, default CUDA 9.2"
  55. echo " -I Compile predict, default off"
  56. echo " -K Compile with AKG, default off"
  57. }
  58. # check value of input is 'on' or 'off'
  59. # usage: check_on_off arg_value arg_name
  60. check_on_off()
  61. {
  62. if [[ "X$1" != "Xon" && "X$1" != "Xoff" ]]; then
  63. echo "Invalid value $1 for option -$2"
  64. usage
  65. exit 1
  66. fi
  67. }
  68. # check and set options
  69. checkopts()
  70. {
  71. # Init default values of build options
  72. THREAD_NUM=8
  73. DEBUG_MODE="off"
  74. VERBOSE=""
  75. ENABLE_COVERAGE="off"
  76. RUN_TESTCASES="off"
  77. EXECUTE_SETUP="off"
  78. ENABLE_BACKEND=""
  79. TRAIN_MODE="INFER"
  80. ENABLE_ASAN="off"
  81. ENABLE_PROFILE="off"
  82. INC_BUILD="off"
  83. ENABLE_LOAD_IR="off"
  84. ENABLE_TIMELINE="off"
  85. ENABLE_DUMP2PROTO="on"
  86. ENABLE_DUMPE2E="off"
  87. ENABLE_DUMP_IR="on"
  88. COMPILE_MINDDATA="off"
  89. ENABLE_MPI="off"
  90. CUDA_VERSION="9.2"
  91. COMPILE_PREDICT="off"
  92. USE_GLOG="on"
  93. PREDICT_PLATFORM=""
  94. ENABLE_AKG="off"
  95. # Process the options
  96. while getopts 'drvj:c:t:hsb:a:g:p:ie:m:I:LRP:Q:D:zM:V:K' opt
  97. do
  98. OPTARG=$(echo ${OPTARG} | tr '[A-Z]' '[a-z]')
  99. case "${opt}" in
  100. d)
  101. DEBUG_MODE="on"
  102. ;;
  103. r)
  104. DEBUG_MODE="off"
  105. ;;
  106. v)
  107. VERBOSE="VERBOSE=1"
  108. ;;
  109. j)
  110. THREAD_NUM=$OPTARG
  111. ;;
  112. c)
  113. check_on_off $OPTARG c
  114. ENABLE_COVERAGE="$OPTARG"
  115. ;;
  116. t)
  117. check_on_off $OPTARG t
  118. RUN_TESTCASES="$OPTARG"
  119. ;;
  120. g)
  121. check_on_off $OPTARG g
  122. USE_GLOG="$OPTARG"
  123. ;;
  124. h)
  125. usage
  126. exit 0
  127. ;;
  128. s)
  129. EXECUTE_SETUP="on"
  130. ;;
  131. b)
  132. if [[ "X$OPTARG" != "Xge" && "X$OPTARG" != "Xcpu" ]]; then
  133. echo "Invalid value ${OPTARG} for option -b"
  134. usage
  135. exit 1
  136. fi
  137. ENABLE_BACKEND=$(echo "$OPTARG" | tr '[a-z]' '[A-Z]')
  138. if [[ "X$ENABLE_BACKEND" == "XGE" ]]; then
  139. ENABLE_GE="on"
  140. fi
  141. if [[ "X$ENABLE_BACKEND" != "XCPU" ]]; then
  142. ENABLE_CPU="on"
  143. fi
  144. ;;
  145. a)
  146. check_on_off $OPTARG a
  147. ENABLE_ASAN="$OPTARG"
  148. ;;
  149. p)
  150. check_on_off $OPTARG p
  151. ENABLE_PROFILE="$OPTARG"
  152. ;;
  153. i)
  154. INC_BUILD="on"
  155. ;;
  156. m)
  157. if [[ "X$OPTARG" != "Xinfer" && "X$OPTARG" != "Xtrain" ]]; then
  158. echo "Invalid value ${OPTARG} for option -m"
  159. usage
  160. exit 1
  161. fi
  162. TRAIN_MODE=$(echo "$OPTARG" | tr '[a-z]' '[A-Z]')
  163. ;;
  164. L)
  165. ENABLE_LOAD_IR="on"
  166. echo "build with enable load anf ir"
  167. ;;
  168. R)
  169. ENABLE_TIMELINE="on"
  170. echo "enable time_line record"
  171. ;;
  172. e)
  173. if [[ "X$OPTARG" == "Xgpu" ]]; then
  174. ENABLE_GPU="on"
  175. ENABLE_CPU="on"
  176. elif [[ "X$OPTARG" == "Xd" ]]; then
  177. ENABLE_D="on"
  178. ENABLE_CPU="on"
  179. elif [[ "X$OPTARG" == "Xcpu" ]]; then
  180. ENABLE_CPU="on"
  181. else
  182. echo "Invalid value ${OPTARG} for option -e"
  183. usage
  184. exit 1
  185. fi
  186. ;;
  187. M)
  188. check_on_off $OPTARG M
  189. ENABLE_MPI="$OPTARG"
  190. ;;
  191. V)
  192. if [[ "X$OPTARG" != "X9.2" && "X$OPTARG" != "X10.1" ]]; then
  193. echo "Invalid value ${OPTARG} for option -V"
  194. usage
  195. exit 1
  196. fi
  197. CUDA_VERSION="$OPTARG"
  198. ;;
  199. P)
  200. check_on_off $OPTARG p
  201. ENABLE_DUMP2PROTO="$OPTARG"
  202. echo "enable dump anf graph to proto file"
  203. ;;
  204. Q)
  205. check_on_off $OPTARG Q
  206. ENABLE_DUMPE2E="$OPTARG"
  207. echo "enable dump end to end"
  208. ;;
  209. D)
  210. check_on_off $OPTARG D
  211. ENABLE_DUMP_IR="$OPTARG"
  212. echo "enable dump function graph ir"
  213. ;;
  214. z)
  215. COMPILE_MINDDATA="on"
  216. ;;
  217. I)
  218. COMPILE_PREDICT="on"
  219. if [[ "$OPTARG" == "arm64" ]]; then
  220. PREDICT_PLATFORM="arm64"
  221. elif [[ "$OPTARG" == "x86_64" ]]; then
  222. PREDICT_PLATFORM="x86_64"
  223. else
  224. echo "-I parameter must be arm64 or x86_64"
  225. exit 1
  226. fi
  227. ;;
  228. K)
  229. ENABLE_AKG="on"
  230. echo "enable compile with akg"
  231. ;;
  232. *)
  233. echo "Unknown option ${opt}!"
  234. usage
  235. exit 1
  236. esac
  237. done
  238. }
  239. checkopts "$@"
  240. echo "---------------- mindspore: build start ----------------"
  241. mkdir -pv "${BUILD_PATH}/package/mindspore/lib"
  242. git submodule update --init graphengine
  243. build_exit()
  244. {
  245. echo "$@" >&2
  246. stty echo
  247. exit 1
  248. }
  249. # Create building path
  250. build_mindspore()
  251. {
  252. echo "start build mindspore project."
  253. mkdir -pv "${BUILD_PATH}/mindspore"
  254. cd "${BUILD_PATH}/mindspore"
  255. CMAKE_ARGS="-DDEBUG_MODE=$DEBUG_MODE -DBUILD_PATH=$BUILD_PATH"
  256. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_LOAD_ANF_IR=$ENABLE_LOAD_IR"
  257. if [[ "X$ENABLE_COVERAGE" = "Xon" ]]; then
  258. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_COVERAGE=ON"
  259. fi
  260. if [[ "X$RUN_TESTCASES" = "Xon" ]]; then
  261. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TESTCASES=ON"
  262. fi
  263. if [[ -n "$ENABLE_BACKEND" ]]; then
  264. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_${ENABLE_BACKEND}=ON"
  265. fi
  266. if [[ -n "$TRAIN_MODE" ]]; then
  267. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_${TRAIN_MODE}=ON"
  268. fi
  269. if [[ "X$ENABLE_ASAN" = "Xon" ]]; then
  270. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_ASAN=ON"
  271. fi
  272. if [[ "X$ENABLE_PROFILE" = "Xon" ]]; then
  273. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_PROFILE=ON"
  274. fi
  275. if [[ "X$ENABLE_TIMELINE" = "Xon" ]]; then
  276. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TIMELINE=ON"
  277. fi
  278. if [[ "X$ENABLE_DUMP2PROTO" = "Xon" ]]; then
  279. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_DUMP_PROTO=ON"
  280. fi
  281. if [[ "X$ENABLE_DUMPE2E" = "Xon" ]]; then
  282. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_DUMP_E2E=ON"
  283. fi
  284. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_DUMP_IR=${ENABLE_DUMP_IR^^}"
  285. if [[ "X$ENABLE_MPI" = "Xon" ]]; then
  286. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_MPI=ON"
  287. fi
  288. if [[ "X$ENABLE_D" = "Xon" ]]; then
  289. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_D=ON"
  290. fi
  291. if [[ "X$ENABLE_GPU" = "Xon" ]]; then
  292. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_GPU=ON -DCUDA_PATH=$CUDA_PATH -DCUDNN_PATH=$CUDNN_PATH -DMS_REQUIRE_CUDA_VERSION=${CUDA_VERSION}"
  293. fi
  294. if [[ "X$ENABLE_CPU" = "Xon" ]]; then
  295. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_CPU=ON"
  296. fi
  297. if [[ "X$COMPILE_MINDDATA" = "Xon" ]]; then
  298. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_MINDDATA=ON"
  299. fi
  300. if [[ "X$USE_GLOG" = "Xon" ]]; then
  301. CMAKE_ARGS="${CMAKE_ARGS} -DUSE_GLOG=ON"
  302. fi
  303. if [[ "X$ENABLE_AKG" = "Xon" ]]; then
  304. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_AKG=ON"
  305. fi
  306. echo "${CMAKE_ARGS}"
  307. if [[ "X$INC_BUILD" = "Xoff" ]]; then
  308. cmake ${CMAKE_ARGS} ../..
  309. fi
  310. make ${VERBOSE} -j$THREAD_NUM
  311. if [[ "X$EXECUTE_SETUP" = "Xon" ]]; then
  312. make install
  313. fi
  314. echo "success to build mindspore project!"
  315. }
  316. build_predict()
  317. {
  318. git submodule update --init --recursive third_party/incubator-tvm
  319. echo "start build predict project"
  320. git submodule update --init --recursive third_party/flatbuffers
  321. git submodule update --init --recursive third_party/googletest
  322. git submodule update --init --recursive third_party/protobuf
  323. rm -rf "${BASEPATH}/predict/build"
  324. mkdir -pv "${BASEPATH}/predict/build"
  325. rm -rf "${BASEPATH}/predict/output"
  326. mkdir -pv "${BASEPATH}/predict/output"
  327. if [[ "$PREDICT_PLATFORM" == "arm64" ]]; then
  328. if [ "${ANDROID_NDK}" ]; then
  329. echo -e "\e[31mANDROID_NDK_PATH=$ANDROID_NDK \e[0m"
  330. else
  331. echo -e "\e[31mplease set ANDROID_NDK_PATH in environment variable for example: export ANDROID_NDK=/root/usr/android-ndk-r16b/ \e[0m"
  332. exit 1
  333. fi
  334. fi
  335. #build flatbuf
  336. cd "${BASEPATH}/third_party/flatbuffers"
  337. rm -rf build && mkdir -p build && cd build && cmake .. && make -j$THREAD_NUM
  338. FLATC="${BASEPATH}"/third_party/flatbuffers/build/flatc
  339. cd "${BASEPATH}"/predict/schema && mkdir -p "${BASEPATH}"/predict/schema/inner
  340. find . -name "*.fbs" -print0 | xargs -0 "${FLATC}" -c -b
  341. find . -name "*.fbs" -print0 | xargs -0 "${FLATC}" -c -b --reflect-types --gen-mutable --reflect-names --gen-object-api -o ${BASEPATH}/predict/schema/inner
  342. # check LLVM_PATH
  343. if [ "${LLVM_PATH}" == "" ]; then
  344. echo "Please set LLVM_PATH in env for example export LLVM_PATH=/xxxx/bin/llvm-config"
  345. exit
  346. fi
  347. #build tvm
  348. tvm_open_source="${BASEPATH}/third_party/incubator-tvm"
  349. tvm_kernel_build="${BASEPATH}/predict/module/tvm_kernel"
  350. if [ ! -f "${tvm_kernel_build}"/incubator-tvm/build/libtvm.so ]; then
  351. rm -fr "${tvm_kernel_build}"/incubator-tvm
  352. cp -fr "${tvm_open_source}" "${tvm_kernel_build}"
  353. mkdir -p "${tvm_kernel_build}"/incubator-tvm/build
  354. patch -d "${tvm_kernel_build}"/incubator-tvm -p1 < "${BASEPATH}"/third_party/patch/predict/0001-RetBugFix-CustomRuntime_v06.patch
  355. cp "${tvm_kernel_build}"/lite/src/codegen/llvm/lite_rtfunc_reset.cc "${tvm_kernel_build}"/incubator-tvm/src/codegen/llvm/
  356. cp "${tvm_open_source}"/cmake/config.cmake "${tvm_kernel_build}"/incubator-tvm
  357. if [ "${LLVM_PATH}" ]; then
  358. sed -i "s#set(USE_LLVM .*)#set(USE_LLVM \"${LLVM_PATH}\")#g" "${tvm_kernel_build}"/incubator-tvm/config.cmake
  359. else
  360. echo "need set LLVM_PATH in env for example export LLVM_PATH=/xxxx/bin/llvm-config"
  361. fi
  362. cd "${tvm_kernel_build}"/incubator-tvm/build
  363. cmake ..
  364. make -j$THREAD_NUM
  365. else
  366. cd "${tvm_kernel_build}"/incubator-tvm/build
  367. make -j$THREAD_NUM
  368. fi
  369. #gen op
  370. predict_tvm_op_lib_path="${BASEPATH}/predict/module/tvm_kernel/build/lib_x86"
  371. predict_platform="x86"
  372. if [[ "$PREDICT_PLATFORM" == "arm64" ]]; then
  373. predict_tvm_op_lib_path="${BASEPATH}/predict/module/tvm_kernel/build/lib_arm64"
  374. predict_platform="arm64"
  375. fi
  376. need_get_libs=true
  377. if [ -d "${predict_tvm_op_lib_path}" ]; then
  378. file_list=$(ls "${predict_tvm_op_lib_path}")
  379. if [ -n "${file_list}" ]; then
  380. libstime=$(stat -c %Y "${predict_tvm_op_lib_path}"/* | sort -u | tail -n1)
  381. pythontime=$(find "${BASEPATH}"/predict/module/tvm_kernel/lite/python/ -name "*.py" -exec stat -c %Y {} \; |
  382. sort -u | tail -n1)
  383. if [ "${libstime}" -ge "${pythontime}" ]; then
  384. need_get_libs=false
  385. else
  386. rm -fr "${predict_tvm_op_lib_path}"
  387. fi
  388. fi
  389. fi
  390. if $need_get_libs; then
  391. PYTHONPATH_OLD=${PYTHONPATH}
  392. export PYTHONPATH="${tvm_kernel_build}/incubator-tvm/python:${tvm_kernel_build}/incubator-tvm/topi/python:${tvm_kernel_build}/incubator-tvm/nnvm/python:${tvm_kernel_build}/lite/python:"
  393. cd "${BASEPATH}"/predict/module/tvm_kernel/lite/python/at_ops
  394. python3 at_gen_strip.py ${predict_platform}
  395. export PYTHONPATH=${PYTHONPATH_OLD}
  396. fi
  397. cd "${BASEPATH}/predict/build"
  398. if [[ "$PREDICT_PLATFORM" == "arm64" ]]; then
  399. cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
  400. -DANDROID_NATIVE_API_LEVEL=android-19 -DANDROID_NDK="${ANDROID_NDK}" \
  401. -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" -DANDROID_STL="c++_shared" \
  402. -DANDROID_ABI="arm64-v8a" -DENABLE_PREDICT_ARM64=ON -DANDROID_ALLOW_UNDEFINED_SYMBOLS=TRUE ..
  403. elif [[ "$PREDICT_PLATFORM" == "x86_64" ]]; then
  404. cmake ..
  405. fi
  406. make ${VERBOSE} -j$THREAD_NUM
  407. if [[ "$PREDICT_PLATFORM" == "x86_64" ]]; then
  408. cd "${BASEPATH}/predict/build/test" && ./run_tests.sh
  409. fi
  410. # copy securec include files
  411. mkdir -p "${BASEPATH}/predict/output/include/securec/include"
  412. cp "${BASEPATH}"/third_party/securec/include/* "${BASEPATH}"/predict/output/include/securec/include
  413. cd "${BASEPATH}/predict/output/"
  414. if [[ "$PREDICT_PLATFORM" == "x86_64" ]]; then
  415. tar -cf MSPredict-0.1.0-linux_x86_64.tar.gz include/ lib/ --warning=no-file-changed
  416. elif [[ "$PREDICT_PLATFORM" == "arm64" ]]; then
  417. tar -cf MSPredict-0.1.0-linux_aarch64.tar.gz include/ lib/ --warning=no-file-changed
  418. fi
  419. echo "success to build predict project!"
  420. }
  421. if [[ "X$COMPILE_PREDICT" = "Xon" ]]; then
  422. build_predict
  423. echo "---------------- mindspore: build end ----------------"
  424. exit
  425. else
  426. build_mindspore
  427. fi
  428. if [[ "X$INC_BUILD" = "Xoff" ]]; then
  429. if [[ "X$ENABLE_GE" = "Xon" ]]; then
  430. bash "${PROJECT_PATH}/package.sh" ge
  431. elif [[ "X$ENABLE_GPU" = "Xon" ]]; then
  432. bash "${PROJECT_PATH}/package.sh" ms gpu
  433. elif [[ "X$ENABLE_D" = "Xon" ]] || [[ "X$ENABLE_CPU" = "Xon" ]]; then
  434. bash "${PROJECT_PATH}/package.sh" ms
  435. else
  436. bash "${PROJECT_PATH}/package.sh" debug
  437. fi
  438. fi
  439. cp -rf ${BUILD_PATH}/package/mindspore/lib ${BUILD_PATH}/../mindspore
  440. cp -rf ${BUILD_PATH}/package/mindspore/*.so ${BUILD_PATH}/../mindspore
  441. if [[ -d "${BUILD_PATH}/package/build" ]]; then
  442. rm -rf "${BUILD_PATH}/package/build"
  443. fi
  444. echo "---------------- mindspore: build end ----------------"