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-source.sh 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 environment for mindspore gpu compilation on Ubuntu 18.04.
  17. #
  18. # This file will:
  19. # - change deb source to huaweicloud mirror
  20. # - install compile dependencies via apt like cmake, gcc
  21. # - install python3 & pip3 via apt and set it to default
  22. # - install CUDA by run file and cudnn via apt.
  23. # - compile and install Open MPI if OPENMPI is set to on.
  24. # - install LLVM if LLVM is set to on.
  25. #
  26. # Augments:
  27. # - PYTHON_VERSION: python version to install. [3.7(default), 3.9]
  28. # - CUDA_VERSION: CUDA version to install. [10.1(default), 11.1]
  29. # - OPENMPI: whether to install optional package Open MPI for distributed training. [on, off(default)]
  30. # - LLVM: whether to install optional dependency LLVM for graph kernel fusion. [on, off(default)]
  31. #
  32. # Usage:
  33. # Need root permission to run, like `sudo bash -i ./ubuntu-gpu-source.sh`.
  34. # To set augments, run it as `sudo PYTHON_VERSION=3.9 CUDA_VERSION=11.1 OPENMPI=on bash -i ./ubuntu-gpu-source.sh`.
  35. set -e
  36. PYTHON_VERSION=${PYTHON_VERSION:-3.7}
  37. CUDA_VERSION=${CUDA_VERSION:-10.1}
  38. OPENMPI=${OPENMPI:-off}
  39. LLVM=${LLVM:-off}
  40. available_py_version=(3.7 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. # add value to environment variable if value is not in it
  60. add_env() {
  61. local name=$1
  62. if [[ ":${!name}:" != *":$2:"* ]]; then
  63. echo -e "export $1=$2:\$$1" >> ~/.bashrc
  64. fi
  65. }
  66. # use huaweicloud mirror in China
  67. sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
  68. sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
  69. # base packages
  70. apt-get update
  71. apt-get install software-properties-common lsb-release -y
  72. apt-get install curl tcl automake autoconf libtool gcc-7 git libgmp-dev patch libnuma-dev flex -y
  73. # cmake
  74. wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | apt-key add -
  75. apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
  76. apt-get install cmake -y
  77. # optional dependency LLVM for graph-computation fusion
  78. if [[ X"$LLVM" == "Xon" ]]; then
  79. wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
  80. add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main"
  81. apt-get update
  82. apt-get install llvm-12-dev -y
  83. fi
  84. # optional openmpi for distributed training
  85. if [[ X"$OPENMPI" == "Xon" ]]; then
  86. origin_wd=$PWD
  87. cd /tmp
  88. sudo -u $SUDO_USER curl -O https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.3.tar.gz
  89. sudo -u $SUDO_USER tar xzf openmpi-4.0.3.tar.gz
  90. cd openmpi-4.0.3
  91. sudo -u $SUDO_USER ./configure --prefix=/usr/local/openmpi-4.0.3
  92. sudo -u $SUDO_USER make
  93. make install
  94. add_env PATH /usr/local/openmpi-4.0.3/bin
  95. add_env LD_LIBRARY_PATH /usr/local/openmpi-4.0.3/lib
  96. cd $origin_wd
  97. fi
  98. # python
  99. add-apt-repository -y ppa:deadsnakes/ppa
  100. apt-get install python$PYTHON_VERSION python$PYTHON_VERSION-dev python$PYTHON_VERSION-distutils python3-pip -y
  101. update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 100
  102. # pip
  103. sudo -u $SUDO_USER python -m pip install pip -i https://pypi.tuna.tsinghua.edu.cn/simple
  104. update-alternatives --install /usr/bin/pip pip ~/.local/bin/pip$PYTHON_VERSION 100
  105. update-alternatives --install /usr/local/bin/pip pip ~/.local/bin/pip$PYTHON_VERSION 100
  106. sudo -u $SUDO_USER pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  107. # install cuda/cudnn
  108. cd /tmp
  109. declare -A cuda_url_map=()
  110. 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
  111. 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
  112. cuda_url=${cuda_url_map[$CUDA_VERSION]}
  113. wget $cuda_url
  114. sh ${cuda_url##*/} --silent --toolkit
  115. cd -
  116. apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
  117. add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
  118. add-apt-repository "deb https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64/ /"
  119. apt-get update
  120. declare -A cudnn_name_map=()
  121. cudnn_name_map["10.1"]="libcudnn7=7.6.5.32-1+cuda10.1 libcudnn7-dev=7.6.5.32-1+cuda10.1"
  122. cudnn_name_map["11.1"]="libcudnn8=8.0.4.30-1+cuda11.1 libcudnn8-dev=8.0.4.30-1+cuda11.1"
  123. apt-get install --no-install-recommends ${cudnn_name_map[$CUDA_VERSION]} -y
  124. # add cuda to path
  125. set +e && source ~/.bashrc
  126. set -e
  127. add_env PATH /usr/local/cuda/bin
  128. add_env LD_LIBRARY_PATH /usr/local/cuda/lib64
  129. add_env LD_LIBRARY_PATH /usr/lib/x86_64-linux-gnu
  130. set +e && source ~/.bashrc
  131. set -e
  132. # wheel
  133. sudo -u $SUDO_USER pip install wheel
  134. # python 3.9 needs setuptools>44.0
  135. sudo -u $SUDO_USER pip install -U setuptools
  136. echo "The environment is ready to clone and compile mindspore."