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

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