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.

cross_build_android_arm_inference.sh 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env bash
  2. set -e
  3. ARCHS=("arm64-v8a" "armeabi-v7a")
  4. BUILD_TYPE=Release
  5. MGE_ARMV8_2_FEATURE_FP16=OFF
  6. MGE_DISABLE_FLOAT16=OFF
  7. ARCH=arm64-v8a
  8. REMOVE_OLD_BUILD=false
  9. NINJA_VERBOSE=OFF
  10. NINJA_DRY_RUN=OFF
  11. SPECIFIED_TARGET="install/strip"
  12. READLINK=readlink
  13. OS=$(uname -s)
  14. if [ $OS = "Darwin" ];then
  15. READLINK=greadlink
  16. fi
  17. SRC_DIR=$($READLINK -f "`dirname $0`/../../")
  18. source $SRC_DIR/scripts/cmake-build/utils/utils.sh
  19. config_ninja_default_max_jobs
  20. echo "EXTRA_CMAKE_ARGS: ${EXTRA_CMAKE_ARGS}"
  21. function usage() {
  22. echo "$0 args1 args2 .."
  23. echo "available args detail:"
  24. echo "-d : Build with Debug mode, default Release mode"
  25. echo "-f : enable MGE_ARMV8_2_FEATURE_FP16 for ARM64, need toolchain and hardware support"
  26. echo "-k : open MGE_DISABLE_FLOAT16 for NEON "
  27. echo "-a : config build arch available: ${ARCHS[@]}"
  28. echo "-r : remove old build dir before make, default off"
  29. echo "-v : ninja with verbose and explain, default off"
  30. echo "-n : ninja with -n dry run (don't run commands but act like they succeeded)"
  31. echo "-j : run N jobs in parallel for ninja, defaut is cpu_number + 2"
  32. echo "-e : build a specified target (always for debug, NOTICE: do not do strip/install target when use -e)"
  33. echo "-l : list CMakeLists.txt all options, can be use to config EXTRA_CMAKE_ARGS"
  34. echo "-h : show usage"
  35. echo "append other cmake config by config EXTRA_CMAKE_ARGS, for example, enable MGE_WITH_TEST and build with Debug mode:"
  36. echo "EXTRA_CMAKE_ARGS=\"-DMGE_WITH_TEST=ON\" $0 -d"
  37. exit -1
  38. }
  39. while getopts "lnvrkhdfa:e:j:" arg
  40. do
  41. case $arg in
  42. j)
  43. NINJA_MAX_JOBS=$OPTARG
  44. echo "config NINJA_MAX_JOBS to ${NINJA_MAX_JOBS}"
  45. ;;
  46. l)
  47. echo "list CMakeLists.txt all options, can be used to config EXTRA_CMAKE_ARGS"
  48. show_cmakelist_options
  49. exit 0
  50. ;;
  51. d)
  52. echo "Build with Debug mode"
  53. BUILD_TYPE=Debug
  54. ;;
  55. f)
  56. echo "enable MGE_ARMV8_2_FEATURE_FP16 for ARM64"
  57. MGE_ARMV8_2_FEATURE_FP16=ON
  58. ;;
  59. k)
  60. echo "open MGE_DISABLE_FLOAT16 for NEON"
  61. MGE_DISABLE_FLOAT16=ON
  62. ;;
  63. a)
  64. tmp_arch=null
  65. for arch in ${ARCHS[@]}; do
  66. if [ "$arch" = "$OPTARG" ]; then
  67. echo "CONFIG BUILD ARCH to : $OPTARG"
  68. tmp_arch=$OPTARG
  69. ARCH=$OPTARG
  70. break
  71. fi
  72. done
  73. if [ "$tmp_arch" = "null" ]; then
  74. echo "ERR args for arch (-a)"
  75. echo "available arch list: ${ARCHS[@]}"
  76. usage
  77. fi
  78. ;;
  79. h)
  80. echo "show usage"
  81. usage
  82. ;;
  83. r)
  84. echo "config REMOVE_OLD_BUILD=true"
  85. REMOVE_OLD_BUILD=true
  86. ;;
  87. v)
  88. echo "config NINJA_VERBOSE=ON"
  89. NINJA_VERBOSE=ON
  90. ;;
  91. n)
  92. echo "config NINJA_DRY_RUN=ON"
  93. NINJA_DRY_RUN=ON
  94. ;;
  95. e)
  96. SPECIFIED_TARGET=$OPTARG
  97. ;;
  98. ?)
  99. echo "unkonw argument"
  100. usage
  101. ;;
  102. esac
  103. done
  104. echo "----------------------------------------------------"
  105. echo "build config summary:"
  106. echo "BUILD_TYPE: $BUILD_TYPE"
  107. echo "MGE_ARMV8_2_FEATURE_FP16: $MGE_ARMV8_2_FEATURE_FP16"
  108. echo "MGE_DISABLE_FLOAT16: $MGE_DISABLE_FLOAT16"
  109. echo "SPECIFIED_TARGET: ${SPECIFIED_TARGET}"
  110. echo "ARCH: $ARCH"
  111. echo "----------------------------------------------------"
  112. if [[ $OS =~ "NT" ]]; then
  113. echo "BUILD in NT ..."
  114. fi
  115. if [ -z $NDK_ROOT ];then
  116. echo "can not find NDK_ROOT env, pls export you NDK root dir to NDK_ROOT"
  117. exit -1
  118. fi
  119. function cmake_build() {
  120. if [ $1 = "armeabi-v7a with NEON" ] ;then
  121. BUILD_DIR=$SRC_DIR/build_dir/android/armeabi-v7a/$BUILD_TYPE/build
  122. else
  123. BUILD_DIR=$SRC_DIR/build_dir/android/$1/$BUILD_TYPE/build
  124. fi
  125. INSTALL_DIR=$BUILD_DIR/../install
  126. BUILD_ABI=$1
  127. BUILD_NATIVE_LEVEL=$2
  128. echo "build dir: $BUILD_DIR"
  129. echo "install dir: $INSTALL_DIR"
  130. echo "build type: $BUILD_TYPE"
  131. echo "build ABI: $BUILD_ABI"
  132. echo "build native level: $BUILD_NATIVE_LEVEL"
  133. try_remove_old_build $REMOVE_OLD_BUILD $BUILD_DIR $INSTALL_DIR
  134. echo "create build dir"
  135. mkdir -p $BUILD_DIR
  136. mkdir -p $INSTALL_DIR
  137. cd_real_build_dir $BUILD_DIR
  138. unset IFS
  139. bash -c "cmake -G Ninja \
  140. -DCMAKE_TOOLCHAIN_FILE="$NDK_ROOT/build/cmake/android.toolchain.cmake" \
  141. -DANDROID_NDK="$NDK_ROOT" \
  142. -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
  143. -DANDROID_ABI=\"$BUILD_ABI\" \
  144. -DANDROID_NATIVE_API_LEVEL=$BUILD_NATIVE_LEVEL \
  145. -DMGE_INFERENCE_ONLY=ON \
  146. -DMGE_WITH_CUDA=OFF \
  147. -DMGE_ARMV8_2_FEATURE_FP16=$MGE_ARMV8_2_FEATURE_FP16 \
  148. -DMGE_DISABLE_FLOAT16=$MGE_DISABLE_FLOAT16 \
  149. -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
  150. ${EXTRA_CMAKE_ARGS} \
  151. $SRC_DIR "
  152. config_ninja_target_cmd ${NINJA_VERBOSE} "OFF" "${SPECIFIED_TARGET}" ${NINJA_DRY_RUN} ${NINJA_MAX_JOBS}
  153. bash -c "${NINJA_CMD}"
  154. }
  155. build_flatc $SRC_DIR $REMOVE_OLD_BUILD
  156. api_level=16
  157. abi="armeabi-v7a with NEON"
  158. IFS=""
  159. if [ "$ARCH" = "arm64-v8a" ]; then
  160. api_level=21
  161. abi="arm64-v8a"
  162. elif [ "$ARCH" = "armeabi-v7a" ]; then
  163. api_level=16
  164. abi="armeabi-v7a with NEON"
  165. else
  166. echo "ERR CONFIG ABORT NOW!!"
  167. exit -1
  168. fi
  169. cmake_build $abi $api_level

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台