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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. ALL_PYTHON=${ALL_PYTHON}
  22. FULL_PYTHON_VER="3.5.4 3.6.8 3.7.7 3.8.3"
  23. if [[ -z ${ALL_PYTHON} ]]
  24. then
  25. ALL_PYTHON=${FULL_PYTHON_VER}
  26. fi
  27. PYTHON_DIR=
  28. PYTHON_LIBRARY=
  29. PYTHON_INCLUDE_DIR=
  30. WINDOWS_WHL_HOME=${SRC_DIR}/scripts/whl/windows/windows_whl_home
  31. if [ -e "${WINDOWS_WHL_HOME}" ]; then
  32. echo "remove old windows whl file"
  33. rm -rf ${WINDOWS_WHL_HOME}
  34. fi
  35. mkdir -p ${WINDOWS_WHL_HOME}
  36. function config_python_env() {
  37. PYTHON_DIR=/c/Users/${USER}/mge_whl_python_env/$1
  38. PYTHON_BIN=${PYTHON_DIR}
  39. if [ ! -f "$PYTHON_BIN/python3.exe" ]; then
  40. echo "ERR: can not find $PYTHON_BIN , Invalid python package"
  41. echo "now support list: ${FULL_PYTHON_VER}"
  42. err_env
  43. else
  44. echo "put python3 to env..."
  45. export PATH=${PYTHON_BIN}:$PATH
  46. which python3
  47. fi
  48. echo ${ver}
  49. PYTHON_LIBRARY=${PYTHON_DIR}/libs/python3.lib
  50. PYTHON_INCLUDE_DIR=${PYTHON_DIR}/include
  51. }
  52. if [[ -z ${WINDOWS_WHL_WITH_CUDA} ]]
  53. then
  54. WINDOWS_WHL_WITH_CUDA="OFF"
  55. fi
  56. # config NVIDIA libs
  57. TRT_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/TensorRT-6.0.1.5/lib/nvinfer.dll"
  58. CUDNN_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/cudnn-10.1-windows10-x64-v7.6.5.32/cuda/bin/cudnn64_7.dll"
  59. CUSOLVER_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cusolver64_10.dll"
  60. CUBLAS_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cublas64_10.dll"
  61. CURAND_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/curand64_10.dll"
  62. CUBLASLT_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cublasLt64_10.dll"
  63. CUDART_LIB="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin/cudart64_101.dll"
  64. function depend_real_copy() {
  65. REAL_DST=$1
  66. echo "real copy lib to $1"
  67. cp "${TRT_LIB}" ${REAL_DST}
  68. cp "${CUDNN_LIB}" ${REAL_DST}
  69. cp "${CUSOLVER_LIB}" ${REAL_DST}
  70. cp "${CUBLAS_LIB}" ${REAL_DST}
  71. cp "${CURAND_LIB}" ${REAL_DST}
  72. cp "${CUBLASLT_LIB}" ${REAL_DST}
  73. cp "${CUDART_LIB}" ${REAL_DST}
  74. }
  75. function copy_more_dll() {
  76. # for python whl real use
  77. if [ ${BUILD_IMPERATIVE} = "ON" ]; then
  78. echo "config BUILD_IMPERATIVE core lib dir"
  79. CP_WHL_DST=${BUILD_DIR}/staging/megengine/core/lib
  80. else
  81. echo "config legacy python lib dir"
  82. CP_WHL_DST=${BUILD_DIR}/staging/megengine/_internal/lib
  83. fi
  84. rm -rf ${CP_WHL_DST}
  85. mkdir ${CP_WHL_DST}
  86. # workround for cpu-only version import failed, use a
  87. # empty.file to triger setup.py to create a null empty
  88. echo "empty" > ${CP_WHL_DST}/empty.file
  89. if [ ${WINDOWS_WHL_WITH_CUDA} = "ON" ]; then
  90. echo "copy nvidia lib to whl use...."
  91. depend_real_copy ${CP_WHL_DST}
  92. fi
  93. }
  94. if [[ -z ${BUILD_IMPERATIVE} ]]
  95. then
  96. BUILD_IMPERATIVE="OFF"
  97. fi
  98. function do_build() {
  99. for ver in ${ALL_PYTHON}
  100. do
  101. #config
  102. config_python_env ${ver}
  103. #check env
  104. if [ ! -f "$PYTHON_LIBRARY" ]; then
  105. echo "ERR: can not find $PYTHON_LIBRARY , Invalid python package"
  106. err_env
  107. fi
  108. if [ ! -d "$PYTHON_INCLUDE_DIR" ]; then
  109. echo "ERR: can not find $PYTHON_INCLUDE_DIR , Invalid python package"
  110. err_env
  111. fi
  112. echo "PYTHON_LIBRARY: ${PYTHON_LIBRARY}"
  113. echo "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}"
  114. #append cmake args for config python
  115. #FIXME: ninja handle err with cmake 3.17 when assgin PYTHON_LIBRARY
  116. #But after put python3.exe to HEAD of PATH by config_python_env, cmake can also handle the
  117. #right PYTHON_LIBRARY and PYTHON_INCLUDE_DIR, at the same time, clang-cl need swig target
  118. #force LINK a real PYTHON_LIBRARY file, after test we do not find the symbols conflict with python
  119. #export EXTRA_CMAKE_ARGS="-DPYTHON_LIBRARY=${PYTHON_LIBRARY} -DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR} "
  120. #config build type to RelWithDebInfo to enable MGB_ENABLE_DEBUG_UTIL etc
  121. export EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DCMAKE_BUILD_TYPE=RelWithDebInfo "
  122. #call build and install
  123. #FIXME: cmake do not triger update python config, after
  124. #change PYTHON_LIBRARY and PYTHON_INCLUDE_DIR, so add
  125. #-r to remove build cache after a new ver build, which
  126. #will be more slow build than without -r
  127. BUILD_ARGS=" -t -r"
  128. if [ ${BUILD_IMPERATIVE} = "ON" ]; then
  129. echo "build whl with IMPERATIVE python rt"
  130. BUILD_ARGS="${BUILD_ARGS} -n "
  131. else
  132. echo "build whl with legacy python rt"
  133. fi
  134. if [ ${WINDOWS_WHL_WITH_CUDA} = "ON" ]; then
  135. echo "build windows whl with cuda"
  136. BUILD_ARGS="${BUILD_ARGS} -c "
  137. else
  138. echo "build windows whl with cpu only"
  139. fi
  140. echo "host_build.sh BUILD_ARGS: ${BUILD_ARGS}"
  141. ${SRC_DIR}/scripts/cmake-build/host_build.sh ${BUILD_ARGS}
  142. #call setup.py
  143. BUILD_DIR=${SRC_DIR}/build_dir/host/build/
  144. cd ${BUILD_DIR}
  145. if [ -d "staging" ]; then
  146. echo "remove old build cache file"
  147. rm -rf staging
  148. fi
  149. mkdir -p staging
  150. if [ ${BUILD_IMPERATIVE} = "ON" ]; then
  151. echo "build whl with IMPERATIVE python rt"
  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. else
  164. echo "build whl with legacy python rt"
  165. cp -a python_module/{megengine,setup.py,requires.txt,requires-style.txt,requires-test.txt} staging/
  166. cd ${BUILD_DIR}/staging/megengine/_internal
  167. llvm-strip -s _mgb.pyd
  168. fi
  169. copy_more_dll
  170. cd ${BUILD_DIR}/staging
  171. ${PYTHON_DIR}/python3 setup.py bdist_wheel
  172. cp ${BUILD_DIR}/staging/dist/Meg*.whl ${WINDOWS_WHL_HOME}/
  173. echo ""
  174. echo "##############################################################################################"
  175. echo "windows whl package location: ${WINDOWS_WHL_HOME}"
  176. ls ${WINDOWS_WHL_HOME}
  177. echo "##############################################################################################"
  178. done
  179. }
  180. function third_party_prepare() {
  181. echo "init third_party..."
  182. ${SRC_DIR}/third_party/prepare.sh
  183. if [[ -z ${ALREADY_INSTALL_MKL} ]]
  184. then
  185. echo "init third_party..."
  186. ${SRC_DIR}/third_party/install-mkl.sh
  187. else
  188. echo "skip init mkl internal"
  189. fi
  190. }
  191. ######################
  192. export ALREADY_CONFIG_PYTHON_VER="yes"
  193. third_party_prepare
  194. do_build

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