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 25 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. export BUILD_PATH="${BASEPATH}/build/"
  20. # print usage message
  21. usage()
  22. {
  23. echo "Usage:"
  24. echo "bash build.sh [-d] [-r] [-v] [-c on|off] [-t on|off] [-g on|off] [-h] [-b ge] [-m infer|train] \\"
  25. echo " [-a on|off] [-p on|off] [-i] [-L] [-R] [-D on|off] [-j[n]] [-e gpu|d|cpu] \\"
  26. echo " [-P on|off] [-z [on|off]] [-M on|off] [-V 9.2|10.1] [-I arm64|arm32|x86_64] [-K] \\"
  27. echo " [-B on|off] [-w on|off] [-E] [-l on|off] [-n full|lite|off] [-T 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 " -D Enable dumping of function graph ir, default on"
  49. echo " -z Compile dataset & mindrecord, default on"
  50. echo " -n Compile minddata with mindspore lite, available: off, lite, full, default is lite"
  51. echo " -M Enable MPI and NCCL for GPU training, gpu default on"
  52. echo " -V Specify the minimum required cuda version, default CUDA 10.1"
  53. echo " -I Enable compiling mindspore lite for arm64, arm32 or x86_64, default disable mindspore lite compiling"
  54. echo " -K Compile with AKG, default on"
  55. echo " -s Enable serving module, default off"
  56. echo " -w Enable acl module, default off"
  57. echo " -B Enable debugger, default on"
  58. echo " -E Enable IBVERBS for parameter server, default off"
  59. echo " -l Compile with python dependency, default on"
  60. echo " -T Enable on-device training, default off"
  61. }
  62. # check value of input is 'on' or 'off'
  63. # usage: check_on_off arg_value arg_name
  64. check_on_off()
  65. {
  66. if [[ "X$1" != "Xon" && "X$1" != "Xoff" ]]; then
  67. echo "Invalid value $1 for option -$2"
  68. usage
  69. exit 1
  70. fi
  71. }
  72. # check and set options
  73. checkopts()
  74. {
  75. # Init default values of build options
  76. THREAD_NUM=8
  77. DEBUG_MODE="off"
  78. VERBOSE=""
  79. ENABLE_COVERAGE="off"
  80. RUN_TESTCASES="off"
  81. ENABLE_BACKEND=""
  82. TRAIN_MODE="INFER"
  83. ENABLE_ASAN="off"
  84. ENABLE_PROFILE="off"
  85. INC_BUILD="off"
  86. ENABLE_LOAD_IR="off"
  87. ENABLE_TIMELINE="off"
  88. ENABLE_DUMP2PROTO="on"
  89. ENABLE_DUMP_IR="on"
  90. COMPILE_MINDDATA="on"
  91. COMPILE_MINDDATA_LITE="off"
  92. ENABLE_MPI="off"
  93. CUDA_VERSION="10.1"
  94. COMPILE_LITE="off"
  95. LITE_PLATFORM=""
  96. SUPPORT_TRAIN="off"
  97. USE_GLOG="on"
  98. ENABLE_AKG="on"
  99. ENABLE_SERVING="off"
  100. ENABLE_ACL="off"
  101. ENABLE_DEBUGGER="on"
  102. ENABLE_IBVERBS="off"
  103. ENABLE_PYTHON="on"
  104. ENABLE_GPU="off"
  105. # Process the options
  106. while getopts 'drvj:c:t:hsb:a:g:p:ie:m:l:I:LRP:D:zM:V:K:swB:En:T:' opt
  107. do
  108. OPTARG=$(echo ${OPTARG} | tr '[A-Z]' '[a-z]')
  109. case "${opt}" in
  110. d)
  111. DEBUG_MODE="on"
  112. ;;
  113. n)
  114. if [[ "X$OPTARG" == "Xoff" || "X$OPTARG" == "Xlite" || "X$OPTARG" == "Xfull" || "X$OPTARG" == "Xlite_cv" ]]; then
  115. COMPILE_MINDDATA_LITE="$OPTARG"
  116. else
  117. echo "Invalid value ${OPTARG} for option -n"
  118. usage
  119. exit 1
  120. fi
  121. ;;
  122. r)
  123. DEBUG_MODE="off"
  124. ;;
  125. v)
  126. VERBOSE="VERBOSE=1"
  127. ;;
  128. j)
  129. THREAD_NUM=$OPTARG
  130. ;;
  131. c)
  132. check_on_off $OPTARG c
  133. ENABLE_COVERAGE="$OPTARG"
  134. ;;
  135. t)
  136. check_on_off $OPTARG t
  137. RUN_TESTCASES="$OPTARG"
  138. ;;
  139. g)
  140. check_on_off $OPTARG g
  141. USE_GLOG="$OPTARG"
  142. ;;
  143. h)
  144. usage
  145. exit 0
  146. ;;
  147. b)
  148. if [[ "X$OPTARG" != "Xge" && "X$OPTARG" != "Xcpu" ]]; then
  149. echo "Invalid value ${OPTARG} for option -b"
  150. usage
  151. exit 1
  152. fi
  153. ENABLE_BACKEND=$(echo "$OPTARG" | tr '[a-z]' '[A-Z]')
  154. if [[ "X$ENABLE_BACKEND" != "XCPU" ]]; then
  155. ENABLE_CPU="on"
  156. fi
  157. ;;
  158. a)
  159. check_on_off $OPTARG a
  160. ENABLE_ASAN="$OPTARG"
  161. ;;
  162. p)
  163. check_on_off $OPTARG p
  164. ENABLE_PROFILE="$OPTARG"
  165. ;;
  166. l)
  167. check_on_off $OPTARG l
  168. ENABLE_PYTHON="$OPTARG"
  169. ;;
  170. i)
  171. INC_BUILD="on"
  172. ;;
  173. m)
  174. if [[ "X$OPTARG" != "Xinfer" && "X$OPTARG" != "Xtrain" ]]; then
  175. echo "Invalid value ${OPTARG} for option -m"
  176. usage
  177. exit 1
  178. fi
  179. TRAIN_MODE=$(echo "$OPTARG" | tr '[a-z]' '[A-Z]')
  180. ;;
  181. L)
  182. ENABLE_LOAD_IR="on"
  183. echo "build with enable load anf ir"
  184. ;;
  185. R)
  186. ENABLE_TIMELINE="on"
  187. echo "enable time_line record"
  188. ;;
  189. e)
  190. if [[ "X$OPTARG" == "Xgpu" ]]; then
  191. ENABLE_GPU="on"
  192. ENABLE_CPU="on"
  193. ENABLE_MPI="on"
  194. elif [[ "X$OPTARG" == "Xd" || "X$OPTARG" == "Xascend" ]]; then
  195. ENABLE_D="on"
  196. ENABLE_CPU="on"
  197. ENABLE_SERVING="on"
  198. elif [[ "X$OPTARG" == "Xcpu" ]]; then
  199. ENABLE_CPU="on"
  200. else
  201. echo "Invalid value ${OPTARG} for option -e"
  202. usage
  203. exit 1
  204. fi
  205. ;;
  206. M)
  207. check_on_off $OPTARG M
  208. ENABLE_MPI="$OPTARG"
  209. ;;
  210. V)
  211. if [[ "X$OPTARG" != "X9.2" && "X$OPTARG" != "X10.1" ]]; then
  212. echo "Invalid value ${OPTARG} for option -V"
  213. usage
  214. exit 1
  215. fi
  216. if [[ "X$OPTARG" == "X9.2" ]]; then
  217. echo "Unsupported CUDA version 9.2"
  218. exit 1
  219. fi
  220. CUDA_VERSION="$OPTARG"
  221. ;;
  222. P)
  223. check_on_off $OPTARG p
  224. ENABLE_DUMP2PROTO="$OPTARG"
  225. echo "enable dump anf graph to proto file"
  226. ;;
  227. D)
  228. check_on_off $OPTARG D
  229. ENABLE_DUMP_IR="$OPTARG"
  230. echo "enable dump function graph ir"
  231. ;;
  232. z)
  233. eval ARG=\$\{$OPTIND\}
  234. if [[ -n $ARG && $ARG != -* ]]; then
  235. OPTARG="$ARG"
  236. check_on_off $OPTARG z
  237. OPTIND=$((OPTIND + 1))
  238. else
  239. OPTARG=""
  240. fi
  241. if [[ "X$OPTARG" == "Xoff" ]]; then
  242. COMPILE_MINDDATA="off"
  243. fi
  244. ;;
  245. I)
  246. COMPILE_LITE="on"
  247. if [[ "$OPTARG" == "arm64" ]]; then
  248. LITE_PLATFORM="arm64"
  249. elif [[ "$OPTARG" == "arm32" ]]; then
  250. LITE_PLATFORM="arm32"
  251. elif [[ "$OPTARG" == "x86_64" ]]; then
  252. ENABLE_CONVERTER="on"
  253. LITE_PLATFORM="x86_64"
  254. else
  255. echo "-I parameter must be arm64、arm32 or x86_64"
  256. exit 1
  257. fi
  258. ;;
  259. K)
  260. ENABLE_AKG="on"
  261. echo "enable compile with akg"
  262. ;;
  263. s)
  264. ENABLE_SERVING="on"
  265. echo "enable serving"
  266. ;;
  267. w)
  268. ENABLE_SERVING="on"
  269. echo "enable serving"
  270. ENABLE_ACL="on"
  271. echo "enable acl"
  272. ;;
  273. B)
  274. check_on_off $OPTARG B
  275. ENABLE_DEBUGGER="$OPTARG"
  276. ;;
  277. E)
  278. ENABLE_IBVERBS="on"
  279. echo "enable IBVERBS for parameter server"
  280. ;;
  281. T)
  282. check_on_off $OPTARG T
  283. SUPPORT_TRAIN=$OPTARG
  284. echo "support train on device "
  285. ;;
  286. *)
  287. echo "Unknown option ${opt}!"
  288. usage
  289. exit 1
  290. esac
  291. done
  292. }
  293. checkopts "$@"
  294. echo "---------------- MindSpore: build start ----------------"
  295. mkdir -pv "${BUILD_PATH}/package/mindspore/lib"
  296. git submodule update --init graphengine
  297. if [[ "X$ENABLE_AKG" = "Xon" ]] && [[ "X$ENABLE_D" = "Xon" || "X$ENABLE_GPU" = "Xon" ]]; then
  298. git submodule update --init --recursive akg
  299. fi
  300. build_exit()
  301. {
  302. echo "$@" >&2
  303. stty echo
  304. exit 1
  305. }
  306. # Create building path
  307. build_mindspore()
  308. {
  309. echo "start build mindspore project."
  310. mkdir -pv "${BUILD_PATH}/mindspore"
  311. cd "${BUILD_PATH}/mindspore"
  312. CMAKE_ARGS="-DDEBUG_MODE=$DEBUG_MODE -DBUILD_PATH=$BUILD_PATH"
  313. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_LOAD_ANF_IR=$ENABLE_LOAD_IR"
  314. if [[ "X$ENABLE_COVERAGE" = "Xon" ]]; then
  315. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_COVERAGE=ON"
  316. fi
  317. if [[ "X$RUN_TESTCASES" = "Xon" ]]; then
  318. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TESTCASES=ON"
  319. fi
  320. if [[ -n "$ENABLE_BACKEND" ]]; then
  321. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_${ENABLE_BACKEND}=ON"
  322. fi
  323. if [[ -n "$TRAIN_MODE" ]]; then
  324. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_${TRAIN_MODE}=ON"
  325. fi
  326. if [[ "X$ENABLE_ASAN" = "Xon" ]]; then
  327. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_ASAN=ON"
  328. fi
  329. if [[ "X$ENABLE_PROFILE" = "Xon" ]]; then
  330. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_PROFILE=ON"
  331. fi
  332. if [[ "X$ENABLE_TIMELINE" = "Xon" ]]; then
  333. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TIMELINE=ON"
  334. fi
  335. if [[ "X$ENABLE_DUMP2PROTO" = "Xon" ]]; then
  336. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_DUMP_PROTO=ON"
  337. fi
  338. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_DUMP_IR=${ENABLE_DUMP_IR}"
  339. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_PYTHON=${ENABLE_PYTHON}"
  340. if [[ "X$ENABLE_MPI" = "Xon" ]]; then
  341. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_MPI=ON"
  342. fi
  343. if [[ "X$ENABLE_D" = "Xon" ]]; then
  344. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_D=ON"
  345. fi
  346. if [[ "X$ENABLE_GPU" = "Xon" ]]; then
  347. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_GPU=ON -DUSE_CUDA=ON -DCUDA_PATH=$CUDA_PATH -DMS_REQUIRE_CUDA_VERSION=${CUDA_VERSION}"
  348. fi
  349. if [[ "X$ENABLE_CPU" = "Xon" ]]; then
  350. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_CPU=ON"
  351. fi
  352. if [[ "X$COMPILE_MINDDATA" = "Xon" ]]; then
  353. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_MINDDATA=ON"
  354. fi
  355. if [[ "X$USE_GLOG" = "Xon" ]]; then
  356. CMAKE_ARGS="${CMAKE_ARGS} -DUSE_GLOG=ON"
  357. fi
  358. if [[ "X$ENABLE_AKG" = "Xon" ]] && [[ "X$ENABLE_D" = "Xon" || "X$ENABLE_GPU" = "Xon" ]]; then
  359. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_AKG=ON"
  360. fi
  361. if [[ "X$ENABLE_SERVING" = "Xon" ]]; then
  362. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_SERVING=ON"
  363. fi
  364. if [[ "X$ENABLE_ACL" = "Xon" ]]; then
  365. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_ACL=ON"
  366. fi
  367. if [[ "X$ENABLE_DEBUGGER" = "Xon" ]]; then
  368. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_DEBUGGER=ON"
  369. fi
  370. if [[ "X$ENABLE_IBVERBS" = "Xon" ]]; then
  371. CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_IBVERBS=ON"
  372. fi
  373. echo "${CMAKE_ARGS}"
  374. if [[ "X$INC_BUILD" = "Xoff" ]]; then
  375. cmake ${CMAKE_ARGS} ../..
  376. fi
  377. if [[ -n "$VERBOSE" ]]; then
  378. CMAKE_VERBOSE="--verbose"
  379. fi
  380. cmake --build . --target package ${CMAKE_VERBOSE} -j$THREAD_NUM
  381. echo "success building mindspore project!"
  382. }
  383. checkndk() {
  384. if [ "${ANDROID_NDK}" ]; then
  385. echo -e "\e[31mANDROID_NDK_PATH=$ANDROID_NDK \e[0m"
  386. else
  387. echo -e "\e[31mplease set ANDROID_NDK in environment variable for example: export ANDROID_NDK=/root/usr/android-ndk-r20b/ \e[0m"
  388. exit 1
  389. fi
  390. }
  391. gene_flatbuffer() {
  392. FLAT_DIR="${BASEPATH}/mindspore/lite/schema"
  393. cd ${FLAT_DIR} && rm -rf "${FLAT_DIR}/inner" && mkdir -p "${FLAT_DIR}/inner"
  394. find . -name "*.fbs" -print0 | xargs -0 "${FLATC}" -c -b
  395. find . -name "*.fbs" -print0 | xargs -0 "${FLATC}" -c -b --reflect-types --gen-mutable --reflect-names --gen-object-api -o "${FLAT_DIR}/inner"
  396. FLAT_DIR="${BASEPATH}/mindspore/lite/tools/converter/parser/tflite"
  397. cd ${FLAT_DIR}
  398. find . -name "*.fbs" -print0 | xargs -0 "${FLATC}" -c -b --reflect-types --gen-mutable --reflect-names --gen-object-api -o "${FLAT_DIR}/"
  399. }
  400. build_flatbuffer() {
  401. cd ${BASEPATH}
  402. FLATC="${BASEPATH}"/third_party/flatbuffers/build/flatc
  403. if [[ ! -f "${FLATC}" ]]; then
  404. if [[ ${MSLIBS_SERVER} ]]; then
  405. cd "${BASEPATH}"/third_party/
  406. rm -rf ./v1.11.0.tar.gz ./flatbuffers
  407. wget http://${MSLIBS_SERVER}:8081/libs/flatbuffers/v1.11.0.tar.gz
  408. tar -zxvf ./v1.11.0.tar.gz
  409. mv ./flatbuffers-1.11.0 ./flatbuffers
  410. else
  411. git submodule update --init --recursive third_party/flatbuffers
  412. fi
  413. cd ${BASEPATH}/third_party/flatbuffers
  414. rm -rf build && mkdir -pv build && cd build && cmake -DFLATBUFFERS_BUILD_SHAREDLIB=ON .. && make -j$THREAD_NUM
  415. gene_flatbuffer
  416. fi
  417. if [[ "${INC_BUILD}" == "off" ]]; then
  418. gene_flatbuffer
  419. fi
  420. }
  421. gene_protobuf() {
  422. PROTO_SRC_DIR="${BASEPATH}/mindspore/lite/tools/converter/parser/caffe"
  423. find ${PROTO_SRC_DIR} -name "*.proto" -print0 | xargs -0 "${PROTOC}" -I"${PROTO_SRC_DIR}" --cpp_out="${PROTO_SRC_DIR}"
  424. PROTO_SRC_DIR="${BASEPATH}/mindspore/lite/tools/converter/parser/onnx"
  425. find ${PROTO_SRC_DIR} -name "*.proto" -print0 | xargs -0 "${PROTOC}" -I"${PROTO_SRC_DIR}" --cpp_out="${PROTO_SRC_DIR}"
  426. }
  427. build_protobuf() {
  428. cd ${BASEPATH}
  429. PROTOC="${BASEPATH}"/third_party/protobuf/build/bin/protoc
  430. if [[ ! -f "${PROTOC}" ]]; then
  431. if [[ ${MSLIBS_SERVER} ]]; then
  432. cd "${BASEPATH}"/third_party/
  433. rm -rf ./v3.8.0.tar.gz ./protobuf
  434. wget http://${MSLIBS_SERVER}:8081/libs/protobuf/v3.8.0.tar.gz
  435. tar -zxvf ./v3.8.0.tar.gz
  436. mv ./protobuf-3.8.0 ./protobuf
  437. else
  438. git submodule update --init --recursive third_party/protobuf
  439. fi
  440. cd ${BASEPATH}/third_party/protobuf
  441. rm -rf build && mkdir -pv build && ./autogen.sh
  442. ./configure --prefix=${BASEPATH}/third_party/protobuf/build
  443. make clean && make -j$THREAD_NUM && make install
  444. gene_protobuf
  445. fi
  446. if [[ "${INC_BUILD}" == "off" ]]; then
  447. gene_protobuf
  448. fi
  449. }
  450. build_gtest() {
  451. cd ${BASEPATH}
  452. git submodule update --init --recursive third_party/googletest
  453. }
  454. gene_clhpp() {
  455. CL_SRC_DIR="${BASEPATH}/mindspore/lite/src/runtime/kernel/opencl/cl"
  456. if [ ! -d ${CL_SRC_DIR} ]; then
  457. return
  458. fi
  459. cd ${CL_SRC_DIR}/
  460. rm -rf *.inc
  461. echo "$(cd "$(dirname $0)"; pwd)"
  462. for file_path in "${CL_SRC_DIR}"/*
  463. do
  464. file="$(basename ${file_path})"
  465. inc_file=`echo ${CL_SRC_DIR}/${file} | sed 's/$/.inc/'`
  466. sed 's/^/\"/;s/$/ \\n\" \\/' ${CL_SRC_DIR}/${file} > ${inc_file}
  467. kernel_name=`echo ${file} | sed s'/.\{3\}$//'`
  468. sed -i "1i\static const char *${kernel_name}_source =\"\\n\" \\" ${inc_file}
  469. sed -i '$a\;' ${inc_file}
  470. done
  471. }
  472. gene_ocl_program() {
  473. CL_SRC_DIR="${BASEPATH}/mindspore/lite/src/runtime/kernel/opencl/cl"
  474. SPIRV_DIR=build/spirv
  475. rm -rf ${SPIRV_DIR}
  476. mkdir -pv ${SPIRV_DIR}
  477. if [ ! -d ${CL_SRC_DIR} ]; then
  478. return
  479. fi
  480. for file_path in "${CL_SRC_DIR}"/*
  481. do
  482. file="$(basename ${file_path})"
  483. if [ "${file##*.}" != "cl" ]; then
  484. continue
  485. fi
  486. clang -Xclang -finclude-default-header -cl-std=CL2.0 --target=spir64-unknown-unknown -emit-llvm \
  487. -c -O0 -o ${SPIRV_DIR}/${file%.*}.bc ${CL_SRC_DIR}/${file}
  488. done
  489. bcs=`ls ${SPIRV_DIR}/*.bc`
  490. llvm-link ${bcs} -o ${SPIRV_DIR}/program.bc
  491. llvm-spirv -o ${SPIRV_DIR}/program.spv ${SPIRV_DIR}/program.bc
  492. CL_PROGRAM_PATH="${BASEPATH}/mindspore/lite/src/runtime/kernel/opencl/cl/program.inc"
  493. echo "#include <vector>" > ${CL_PROGRAM_PATH}
  494. echo "std::vector<unsigned char> g_program_binary = {" >> ${CL_PROGRAM_PATH}
  495. #hexdump -v -e '16/1 "0x%02x, " "\n"' ${SPIRV_DIR}/program.spv >> ${CL_PROGRAM_PATH}
  496. hexdump -v -e '1/1 "0x%02x, "' ${SPIRV_DIR}/program.spv >> ${CL_PROGRAM_PATH}
  497. echo "};" >> ${CL_PROGRAM_PATH}
  498. echo "Compile SPIRV done"
  499. }
  500. build_opencl() {
  501. cd ${BASEPATH}
  502. git submodule update --init third_party/OpenCL-Headers
  503. git submodule update --init third_party/OpenCL-CLHPP
  504. if [[ "${OPENCL_OFFLINE_COMPILE}" == "on" ]]; then
  505. gene_ocl_program
  506. else
  507. gene_clhpp
  508. fi
  509. }
  510. build_opencv() {
  511. # check what platform we are building opencv on
  512. cd ${BASEPATH}
  513. if [[ "${LITE_PLATFORM}" == "x86_64" ]]; then
  514. OPENCV_BIN="${BASEPATH}"/third_party/opencv/build/lib/libopencv_core.so.4.2.0
  515. elif [[ "${LITE_PLATFORM}" == "arm32" ]]; then
  516. OPENCV_BIN="${BASEPATH}"/third_party/opencv/build/lib/armeabi-v7a/libopencv_core.so
  517. else
  518. OPENCV_BIN="${BASEPATH}"/third_party/opencv/build/lib/arm64-v8a/libopencv_core.so
  519. fi
  520. if [[ ! -f "${OPENCV_BIN}" ]]; then
  521. if [[ ${MSLIBS_SERVER} ]]; then
  522. cd "${BASEPATH}"/third_party/
  523. rm -rf 4.2.0.tar.gz ./opencv
  524. wget http://${MSLIBS_SERVER}:8081/libs/opencv/4.2.0.tar.gz
  525. tar -zxvf ./4.2.0.tar.gz
  526. mv ./opencv-4.2.0 ./opencv
  527. rm -rf 4.2.0.tar.gz
  528. else
  529. git submodule update --init --recursive third_party/opencv
  530. fi
  531. cd ${BASEPATH}/third_party/opencv
  532. rm -rf build && mkdir -p build && cd build && cmake ${CMAKE_MINDDATA_ARGS} -DBUILD_SHARED_LIBS=ON -DBUILD_ANDROID_PROJECTS=OFF \
  533. -DBUILD_LIST=core,imgcodecs,imgproc -DBUILD_ZLIB=ON .. && make -j$THREAD_NUM
  534. fi
  535. }
  536. build_jpeg_turbo() {
  537. cd ${BASEPATH}
  538. if [[ "${LITE_PLATFORM}" == "x86_64" ]]; then
  539. JPEG_TURBO="${BASEPATH}"/third_party/libjpeg-turbo/lib/libjpeg.so.62.3.0
  540. else
  541. JPEG_TURBO="${BASEPATH}"/third_party/libjpeg-turbo/lib/libjpeg.so
  542. fi
  543. if [[ ! -f "${JPEG_TURBO}" ]]; then
  544. if [[ ${MSLIBS_SERVER} ]]; then
  545. cd "${BASEPATH}"/third_party/
  546. rm -rf 2.0.4.tar.gz ./libjpeg-turbo
  547. wget http://${MSLIBS_SERVER}:8081/libs/jpeg_turbo/2.0.4.tar.gz
  548. tar -zxvf ./2.0.4.tar.gz
  549. mv ./libjpeg-turbo-2.0.4 ./libjpeg-turbo
  550. rm -rf ./2.0.4.tar.gz
  551. else
  552. git submodule update --init --recursive third_party/libjpeg-turbo
  553. fi
  554. cd ${BASEPATH}/third_party/libjpeg-turbo
  555. rm -rf build && mkdir -p build && cd build && cmake ${CMAKE_MINDDATA_ARGS} -DCMAKE_BUILD_TYPE=Release \
  556. -DCMAKE_INSTALL_PREFIX="${BASEPATH}/third_party/libjpeg-turbo" .. && make -j$THREAD_NUM && make install
  557. fi
  558. }
  559. build_eigen() {
  560. cd ${BASEPATH}
  561. if [[ ${MSLIBS_SERVER} ]]; then
  562. cd "${BASEPATH}"/third_party/
  563. rm -rf ./eigen-3.*.tar.gz ./eigen
  564. wget http://${MSLIBS_SERVER}:8081/libs/eigen3/eigen-3.3.7.tar.gz
  565. tar -zxvf ./eigen-3.3.7.tar.gz
  566. mv ./eigen-3.3.7 ./eigen
  567. rm -rf ./eigen-3.*.tar.gz
  568. else
  569. git submodule update --init --recursive third_party/eigen
  570. fi
  571. }
  572. build_minddata_lite_deps()
  573. {
  574. echo "start build minddata lite project"
  575. if [[ "${LITE_PLATFORM}" == "arm64" ]]; then
  576. CMAKE_MINDDATA_ARGS="-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=19 \
  577. -DANDROID_NDK=${ANDROID_NDK} -DANDROID_ABI=arm64-v8a -DANDROID_TOOLCHAIN_NAME=aarch64-linux-android-clang \
  578. -DANDROID_STL=c++_shared -DCMAKE_BUILD_TYPE=${BUILD_TYPE}"
  579. elif [[ "${LITE_PLATFORM}" == "arm32" ]]; then
  580. CMAKE_MINDDATA_ARGS="-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=19 \
  581. -DANDROID_NDK=${ANDROID_NDK} -DANDROID_ABI=armeabi-v7a -DANDROID_TOOLCHAIN_NAME=clang \
  582. -DANDROID_STL=c++_shared -DCMAKE_BUILD_TYPE=${BUILD_TYPE}"
  583. else
  584. CMAKE_MINDDATA_ARGS="-DCMAKE_BUILD_TYPE=${BUILD_TYPE}"
  585. fi
  586. build_opencv
  587. build_eigen
  588. build_jpeg_turbo
  589. }
  590. build_lite()
  591. {
  592. VERSION_MAJOR=`grep "const int ms_version_major =" mindspore/lite/include/version.h | tr -dc "[0-9]"`
  593. VERSION_MINOR=`grep "const int ms_version_minor =" mindspore/lite/include/version.h | tr -dc "[0-9]"`
  594. VERSION_REVISION=`grep "const int ms_version_revision =" mindspore/lite/include/version.h | tr -dc "[0-9]"`
  595. echo "============ Start building MindSpore Lite ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION} ============"
  596. if [ "${ENABLE_GPU}" == "on" ] && [ "${LITE_PLATFORM}" == "arm64" ]; then
  597. echo "start build opencl"
  598. build_opencl
  599. fi
  600. if [[ "${LITE_PLATFORM}" == "x86_64" ]]; then
  601. build_protobuf
  602. fi
  603. build_flatbuffer
  604. build_gtest
  605. if [ "${COMPILE_MINDDATA_LITE}" == "lite" ] || [ "${COMPILE_MINDDATA_LITE}" == "full" ]; then
  606. build_minddata_lite_deps
  607. fi
  608. cd "${BASEPATH}/mindspore/lite"
  609. if [[ "${INC_BUILD}" == "off" ]]; then
  610. rm -rf build
  611. fi
  612. mkdir -pv build
  613. cd build
  614. BUILD_TYPE="Release"
  615. if [[ "${DEBUG_MODE}" == "on" ]]; then
  616. BUILD_TYPE="Debug"
  617. fi
  618. if [[ "${LITE_PLATFORM}" == "arm64" ]]; then
  619. checkndk
  620. cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" -DANDROID_NATIVE_API_LEVEL="19" \
  621. -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="arm64-v8a" -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" \
  622. -DANDROID_STL="c++_shared" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DSUPPORT_TRAIN=${SUPPORT_TRAIN} \
  623. -DBUILD_DEVICE=on -DPLATFORM_ARM64=on -DBUILD_CONVERTER=off -DENABLE_NEON=on -DENABLE_FP16="off" \
  624. -DSUPPORT_GPU=${ENABLE_GPU} -DOFFLINE_COMPILE=${OPENCL_OFFLINE_COMPILE} -DBUILD_MINDDATA=${COMPILE_MINDDATA_LITE} \
  625. -DCMAKE_INSTALL_PREFIX=${BASEPATH}/output/tmp -DMS_VERSION_MAJOR=${VERSION_MAJOR} \
  626. -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} "${BASEPATH}/mindspore/lite"
  627. elif [[ "${LITE_PLATFORM}" == "arm32" ]]; then
  628. checkndk
  629. cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" -DANDROID_NATIVE_API_LEVEL="19" \
  630. -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="armeabi-v7a" -DANDROID_TOOLCHAIN_NAME="clang" \
  631. -DANDROID_STL="c++_shared" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
  632. -DBUILD_DEVICE=on -DPLATFORM_ARM32=on -DENABLE_NEON=on -DSUPPORT_TRAIN=${SUPPORT_TRAIN} -DBUILD_CONVERTER=off \
  633. -DSUPPORT_GPU=${ENABLE_GPU} -DOFFLINE_COMPILE=${OPENCL_OFFLINE_COMPILE} -DBUILD_MINDDATA=${COMPILE_MINDDATA_LITE} \
  634. -DCMAKE_INSTALL_PREFIX=${BASEPATH}/output/tmp -DMS_VERSION_MAJOR=${VERSION_MAJOR} \
  635. -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} "${BASEPATH}/mindspore/lite"
  636. else
  637. cmake -DBUILD_DEVICE=on -DPLATFORM_ARM64=off -DBUILD_CONVERTER=${ENABLE_CONVERTER} -DSUPPORT_TRAIN=${SUPPORT_TRAIN} \
  638. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DSUPPORT_GPU=${ENABLE_GPU} -DBUILD_MINDDATA=${COMPILE_MINDDATA_LITE} \
  639. -DOFFLINE_COMPILE=${OPENCL_OFFLINE_COMPILE} -DCMAKE_INSTALL_PREFIX=${BASEPATH}/output/tmp \
  640. -DMS_VERSION_MAJOR=${VERSION_MAJOR} -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} \
  641. "${BASEPATH}/mindspore/lite"
  642. fi
  643. VERBOSE=2 make -j$THREAD_NUM && make install && make package
  644. COMPILE_RET=$?
  645. if [[ "${COMPILE_RET}" -ne 0 ]]; then
  646. echo "---------------- mindspore lite: build failed ----------------"
  647. exit 1
  648. else
  649. mv ${BASEPATH}/output/tmp/*.tar.gz* ${BASEPATH}/output/
  650. rm -rf ${BASEPATH}/output/tmp/
  651. echo "---------------- mindspore lite: build success ----------------"
  652. exit 0
  653. fi
  654. }
  655. if [[ "X$COMPILE_LITE" = "Xon" ]]; then
  656. build_lite
  657. else
  658. build_mindspore
  659. fi
  660. cp -rf ${BUILD_PATH}/package/mindspore/lib ${BUILD_PATH}/../mindspore
  661. cp -rf ${BUILD_PATH}/package/mindspore/*.so ${BUILD_PATH}/../mindspore
  662. echo "---------------- mindspore: build end ----------------"