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.

windows_build_whl.sh 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/bin/bash -e
  2. NT=$(echo `uname` | grep "NT")
  3. echo $NT
  4. if [ -z "$NT" ];then
  5. echo "only run at windows bash env"
  6. echo "pls consider install bash-like tools, eg MSYS or git-cmd, etc"
  7. exit -1
  8. fi
  9. function err_env() {
  10. echo "check_env failed: pls refs ${SRC_DIR}/scripts/whl/BUILD_PYTHON_WHL_README.md to init env"
  11. exit -1
  12. }
  13. function append_path_env_and_check() {
  14. echo "export vs2019 install path"
  15. export VS_PATH=/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio/2019/Enterprise
  16. # for llvm-strip
  17. export PATH=$VS_PATH/VC/Tools/Llvm/bin/:$PATH
  18. }
  19. append_path_env_and_check
  20. SRC_DIR=$(READLINK -f "`dirname $0`/../../../")
  21. source ${SRC_DIR}/scripts/whl/utils/utils.sh
  22. ALL_PYTHON=${ALL_PYTHON}
  23. FULL_PYTHON_VER="3.5.4 3.6.8 3.7.7 3.8.3"
  24. if [[ -z ${ALL_PYTHON} ]]
  25. then
  26. ALL_PYTHON=${FULL_PYTHON_VER}
  27. fi
  28. PYTHON_DIR=
  29. PYTHON_LIBRARY=
  30. PYTHON_INCLUDE_DIR=
  31. WINDOWS_WHL_HOME=${SRC_DIR}/scripts/whl/windows/windows_whl_home
  32. if [ -e "${WINDOWS_WHL_HOME}" ]; then
  33. echo "remove old windows whl file"
  34. rm -rf ${WINDOWS_WHL_HOME}
  35. fi
  36. mkdir -p ${WINDOWS_WHL_HOME}
  37. function config_python_env() {
  38. PYTHON_DIR=/c/Users/${USER}/mge_whl_python_env/$1
  39. PYTHON_BIN=${PYTHON_DIR}
  40. if [ ! -f "${PYTHON_BIN}/python3.exe" ]; then
  41. echo "ERR: can not find $PYTHON_BIN , Invalid python package"
  42. echo "now support list: ${FULL_PYTHON_VER}"
  43. err_env
  44. else
  45. echo "put ${PYTHON_BIN}/python3.exe to env..."
  46. export PATH=${PYTHON_BIN}:$PATH
  47. which python3
  48. fi
  49. echo ${ver}
  50. PYTHON_LIBRARY=${PYTHON_DIR}/libs/python3.lib
  51. PYTHON_INCLUDE_DIR=${PYTHON_DIR}/include
  52. }
  53. BUILD_WHL_CPU_ONLY=${BUILD_WHL_CPU_ONLY}
  54. if [[ -z ${BUILD_WHL_CPU_ONLY} ]]
  55. then
  56. BUILD_WHL_CPU_ONLY="OFF"
  57. fi
  58. # config NVIDIA libs
  59. TRT_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/TensorRT-6.0.1.5/lib/nvinfer.dll"
  60. CUDNN_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/cudnn-10.1-windows10-x64-v7.6.5.32/cuda/bin/cudnn64_7.dll"
  61. CUSOLVER_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cusolver64_10.dll"
  62. CUBLAS_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cublas64_10.dll"
  63. CURAND_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/curand64_10.dll"
  64. CUBLASLT_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cublasLt64_10.dll"
  65. CUDART_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cudart64_101.dll"
  66. function depend_real_copy() {
  67. REAL_DST=$1
  68. echo "real copy lib to $1"
  69. cp "${TRT_LIB}" ${REAL_DST}
  70. cp "${CUDNN_LIB}" ${REAL_DST}
  71. cp "${CUSOLVER_LIB}" ${REAL_DST}
  72. cp "${CUBLAS_LIB}" ${REAL_DST}
  73. cp "${CURAND_LIB}" ${REAL_DST}
  74. cp "${CUBLASLT_LIB}" ${REAL_DST}
  75. cp "${CUDART_LIB}" ${REAL_DST}
  76. }
  77. function copy_more_dll() {
  78. # for python whl real use
  79. echo "config BUILD_IMPERATIVE core lib dir"
  80. CP_WHL_DST_IMP=${BUILD_DIR}/staging/megengine/core/lib
  81. rm -rf ${CP_WHL_DST_IMP}
  82. mkdir ${CP_WHL_DST_IMP}
  83. # workround for cpu-only version import failed, use a
  84. # empty.file to triger setup.py to create a null empty
  85. echo "empty" > ${CP_WHL_DST_IMP}/empty.file
  86. if [ ${BUILD_WHL_CPU_ONLY} = "OFF" ]; then
  87. echo "copy nvidia lib to whl use...."
  88. depend_real_copy ${CP_WHL_DST_IMP}
  89. fi
  90. }
  91. BUILD_DIR=${SRC_DIR}/build_dir/host/build/
  92. # here we just treat cu file should not in the increment build file list
  93. INCREMENT_KEY_WORDS=".cu.obj is dirty"
  94. IS_IN_FIRST_LOOP=TRUE
  95. function do_build() {
  96. for ver in ${ALL_PYTHON}
  97. do
  98. # we want run a full clean build at the first loop
  99. if [ ${IS_IN_FIRST_LOOP} = "TRUE" ]; then
  100. # TODO: may all cmake issue can be resolved after rm CMakeCache?
  101. # if YES, remove this to use old cache and speed up CI
  102. echo "warning: remove old build_dir for the first loop"
  103. rm -rf ${BUILD_DIR}
  104. fi
  105. #config python3
  106. config_python_env ${ver}
  107. #check env
  108. if [ ! -f "$PYTHON_LIBRARY" ]; then
  109. echo "ERR: can not find $PYTHON_LIBRARY , Invalid python package"
  110. err_env
  111. fi
  112. if [ ! -d "$PYTHON_INCLUDE_DIR" ]; then
  113. echo "ERR: can not find $PYTHON_INCLUDE_DIR , Invalid python package"
  114. err_env
  115. fi
  116. echo "PYTHON_LIBRARY: ${PYTHON_LIBRARY}"
  117. echo "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}"
  118. #config build type to RelWithDebInfo to enable MGB_ENABLE_DEBUG_UTIL etc
  119. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DCMAKE_BUILD_TYPE=RelWithDebInfo "
  120. #call build and install
  121. HOST_BUILD_ARGS=" -t -s"
  122. if [ ${BUILD_WHL_CPU_ONLY} = "OFF" ]; then
  123. echo "build windows whl with cuda"
  124. HOST_BUILD_ARGS="${HOST_BUILD_ARGS} -c "
  125. else
  126. echo "build windows whl with cpu only"
  127. fi
  128. if [ -d "${BUILD_DIR}" ]; then
  129. # insure rm have args
  130. touch ${BUILD_DIR}/empty.pyd
  131. touch ${BUILD_DIR}/CMakeCache.txt
  132. /usr/bin/find ${BUILD_DIR} -name "*.pyd" | xargs rm
  133. # ninja/cmake on windows will handle error if just export
  134. # PYTHON_LIBRARY/PYTHON_INCLUDE_DIR/PYTHON_EXECUTABLE
  135. # But after put python3.exe to HEAD of PATH by config_python_env
  136. # and force remove CMakeCache.txt, ninja/cmake will auto update
  137. # PYTHON_LIBRARY/PYTHON_INCLUDE_DIR/PYTHON_EXECUTABLE
  138. /usr/bin/find ${BUILD_DIR} -name CMakeCache.txt | xargs rm
  139. fi
  140. echo "host_build.sh HOST_BUILD_ARGS: ${HOST_BUILD_ARGS}"
  141. # call ninja dry run and check increment is invalid or not
  142. if [ ${IS_IN_FIRST_LOOP} = "FALSE" ]; then
  143. ninja_dry_run_and_check_increment "${SRC_DIR}/scripts/cmake-build/host_build.sh" "${HOST_BUILD_ARGS}" "${INCREMENT_KEY_WORDS}"
  144. fi
  145. #call real build
  146. ${SRC_DIR}/scripts/cmake-build/host_build.sh ${HOST_BUILD_ARGS}
  147. # check python api call setup.py
  148. cd ${BUILD_DIR}
  149. check_build_ninja_python_api ${ver}
  150. rm -rf staging
  151. mkdir -p staging
  152. cp -a imperative/python/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  153. cd ${BUILD_DIR}/staging/megengine/core
  154. rt_file=`ls _imperative_rt.*.pyd`
  155. echo "rt file is: ${rt_file}"
  156. if [[ -z ${rt_file} ]]
  157. then
  158. echo "ERR: can not find valid rt file"
  159. exit -1
  160. fi
  161. llvm-strip -s ${rt_file}
  162. mv ${rt_file} _imperative_rt.pyd
  163. copy_more_dll
  164. cd ${BUILD_DIR}/staging
  165. echo "call setup.py now"
  166. ${PYTHON_DIR}/python3 setup.py bdist_wheel
  167. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${WINDOWS_WHL_HOME}/
  168. echo ""
  169. echo "##############################################################################################"
  170. echo "windows whl package location: ${WINDOWS_WHL_HOME}"
  171. ls ${WINDOWS_WHL_HOME}
  172. echo "##############################################################################################"
  173. IS_IN_FIRST_LOOP=FALSE
  174. done
  175. }
  176. function third_party_prepare() {
  177. echo "init third_party..."
  178. ${SRC_DIR}/third_party/prepare.sh
  179. if [[ -z ${ALREADY_INSTALL_MKL} ]]
  180. then
  181. echo "init third_party..."
  182. ${SRC_DIR}/third_party/install-mkl.sh
  183. else
  184. echo "skip init mkl internal"
  185. fi
  186. }
  187. ######################
  188. export ALREADY_CONFIG_PYTHON_VER="yes"
  189. if [ ${BUILD_WHL_CPU_ONLY} = "OFF" ]; then
  190. export SDK_NAME="cu101"
  191. else
  192. export SDK_NAME="cpu"
  193. fi
  194. third_party_prepare
  195. do_build

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