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

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

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