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.

ubuntu-gpu-pip.sh 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. # Copyright 2022 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. # Prepare and Install mindspore gpu by pip on Ubuntu 18.04.
  17. #
  18. # This file will:
  19. # - change deb source to huaweicloud mirror
  20. # - install mindspore dependencies via apt like gcc, libgmp
  21. # - install python3 & pip3 via apt and set it to default
  22. # - install CUDA by run file and cudnn via apt.
  23. # - install mindspore-cpu within new installed python by pip
  24. # - compile and install Open MPI if OPENMPI is set to on.
  25. #
  26. # Augments:
  27. # - PYTHON_VERSION: python version to install. [3.7(default), 3.8, 3.9]
  28. # - MINDSPORE_VERSION: mindspore version to install, default 1.6.0
  29. # - CUDA_VERSION: CUDA version to install. [10.1, 11.1(default)]
  30. # - OPENMPI: whether to install optional package Open MPI for distributed training. [on, off(default)]
  31. #
  32. # Usage:
  33. # Run script like `bash -i ./ubuntu-gpu-pip.sh`.
  34. # To set augments, run it as `PYTHON_VERSION=3.9 CUDA_VERSION=10.1 OPENMPI=on bash -i ./ubuntu-gpu-pip.sh`.
  35. set -e
  36. PYTHON_VERSION=${PYTHON_VERSION:-3.7}
  37. MINDSPORE_VERSION=${MINDSPORE_VERSION:-1.6.0}
  38. CUDA_VERSION=${CUDA_VERSION:-11.1}
  39. OPENMPI=${OPENMPI:-off}
  40. available_py_version=(3.7 3.8 3.9)
  41. if [[ " ${available_py_version[*]} " != *" $PYTHON_VERSION "* ]]; then
  42. echo "PYTHON_VERSION is '$PYTHON_VERSION', but available versions are [${available_py_version[*]}]."
  43. exit 1
  44. fi
  45. available_cuda_version=(10.1 11.1)
  46. if [[ " ${available_cuda_version[*]} " != *" $CUDA_VERSION "* ]]; then
  47. echo "CUDA_VERSION is '$CUDA_VERSION', but available versions are [${available_cuda_version[*]}]."
  48. exit 1
  49. fi
  50. declare -A minimum_driver_version_map=()
  51. minimum_driver_version_map["10.1"]="418.39"
  52. minimum_driver_version_map["11.1"]="450.80.02"
  53. driver_version=$(modinfo nvidia | grep ^version | awk '{printf $2}')
  54. if [[ $driver_version < ${minimum_driver_version_map[$CUDA_VERSION]} ]]; then
  55. echo "CUDA $CUDA_VERSION minimum required driver version is ${minimum_driver_version_map[$CUDA_VERSION]}, \
  56. but current nvidia driver version is $driver_version, please upgrade your driver manually."
  57. exit 1
  58. fi
  59. cuda_name="cuda-$CUDA_VERSION"
  60. declare -A version_map=()
  61. version_map["3.7"]="${MINDSPORE_VERSION}-cp37-cp37m"
  62. version_map["3.8"]="${MINDSPORE_VERSION}-cp38-cp38"
  63. version_map["3.9"]="${MINDSPORE_VERSION}-cp39-cp39"
  64. # add value to environment variable if value is not in it
  65. add_env() {
  66. local name=$1
  67. if [[ ":${!name}:" != *":$2:"* ]]; then
  68. echo -e "export $1=$2:\$$1" >> ~/.bashrc
  69. fi
  70. }
  71. # use huaweicloud mirror in China
  72. sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
  73. sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
  74. sudo apt-get update
  75. sudo apt-get install curl make gcc-7 libgmp-dev linux-headers-"$(uname -r)" -y
  76. # python
  77. sudo add-apt-repository -y ppa:deadsnakes/ppa
  78. sudo apt-get install python$PYTHON_VERSION python$PYTHON_VERSION-distutils python3-pip -y
  79. sudo update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 100
  80. # pip
  81. python -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple
  82. echo -e "alias pip='python -m pip'" >> ~/.bashrc
  83. source ~/.bashrc
  84. pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  85. # install cuda/cudnn
  86. cd /tmp
  87. declare -A cuda_url_map=()
  88. cuda_url_map["10.1"]=https://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run
  89. cuda_url_map["11.1"]=https://developer.download.nvidia.com/compute/cuda/11.1.1/local_installers/cuda_11.1.1_455.32.00_linux.run
  90. cuda_url=${cuda_url_map[$CUDA_VERSION]}
  91. wget $cuda_url
  92. sudo sh ${cuda_url##*/} --silent --toolkit
  93. cd -
  94. sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
  95. sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
  96. sudo add-apt-repository "deb https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64/ /"
  97. sudo apt-get update
  98. declare -A cudnn_name_map=()
  99. cudnn_name_map["10.1"]="libcudnn7=7.6.5.32-1+cuda10.1 libcudnn7-dev=7.6.5.32-1+cuda10.1"
  100. cudnn_name_map["11.1"]="libcudnn8=8.0.4.30-1+cuda11.1 libcudnn8-dev=8.0.4.30-1+cuda11.1"
  101. sudo apt-get install --no-install-recommends ${cudnn_name_map[$CUDA_VERSION]} -y
  102. # add cuda to path
  103. set +e && source ~/.bashrc
  104. set -e
  105. add_env PATH /usr/local/cuda/bin
  106. add_env LD_LIBRARY_PATH /usr/local/cuda/lib64
  107. add_env LD_LIBRARY_PATH /usr/lib/x86_64-linux-gnu
  108. set +e && source ~/.bashrc
  109. set -e
  110. # optional openmpi for distributed training
  111. if [[ X"$OPENMPI" == "Xon" ]]; then
  112. cd /tmp
  113. curl -O https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.3.tar.gz
  114. tar xzf openmpi-4.0.3.tar.gz
  115. cd openmpi-4.0.3
  116. ./configure --prefix=/usr/local/openmpi-4.0.3
  117. make
  118. sudo make install
  119. add_env PATH /usr/local/openmpi-4.0.3/bin
  120. add_env LD_LIBRARY_PATH /usr/local/openmpi-4.0.3/lib
  121. fi
  122. arch=`uname -m`
  123. pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/gpu/${arch}/${cuda_name}/mindspore_gpu-${version_map["$PYTHON_VERSION"]}-linux_${arch}.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
  124. # check mindspore installation
  125. python -c "import mindspore;mindspore.run_check()"
  126. # check if it can be run with GPU
  127. cd /tmp
  128. cat > example.py <<END
  129. import numpy as np
  130. from mindspore import Tensor
  131. import mindspore.ops as ops
  132. import mindspore.context as context
  133. context.set_context(device_target="GPU")
  134. x = Tensor(np.ones([1,3,3,4]).astype(np.float32))
  135. y = Tensor(np.ones([1,3,3,4]).astype(np.float32))
  136. print(ops.add(x, y))
  137. END
  138. python example.py
  139. cd -