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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.8, 3.9]
  28. # - CUDA_VERSION: CUDA version to install. [10.1, 11.1(default)]
  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. # Run script like `bash -i ./ubuntu-gpu-source.sh`.
  34. # To set augments, run it as `PYTHON_VERSION=3.9 CUDA_VERSION=10.1 OPENMPI=on bash -i ./ubuntu-gpu-source.sh`.
  35. set -e
  36. PYTHON_VERSION=${PYTHON_VERSION:-3.7}
  37. CUDA_VERSION=${CUDA_VERSION:-11.1}
  38. OPENMPI=${OPENMPI:-off}
  39. LLVM=${LLVM:-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. # 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. sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
  68. sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list
  69. # base packages
  70. sudo apt-get update
  71. sudo apt-get install software-properties-common lsb-release -y
  72. sudo 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 | sudo apt-key add -
  75. sudo apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
  76. sudo 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 | sudo apt-key add -
  80. sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main"
  81. sudo apt-get update
  82. sudo apt-get install llvm-12-dev -y
  83. fi
  84. # optional openmpi for distributed training
  85. if [[ X"$OPENMPI" == "Xon" ]]; then
  86. echo "installing openmpi"
  87. origin_wd=$PWD
  88. cd /tmp
  89. curl -O https://download.open-mpi.org/release/open-mpi/v4.0/openmpi-4.0.3.tar.gz
  90. tar xzf openmpi-4.0.3.tar.gz
  91. cd openmpi-4.0.3
  92. ./configure --prefix=/usr/local/openmpi-4.0.3
  93. make
  94. sudo make install
  95. add_env PATH /usr/local/openmpi-4.0.3/bin
  96. add_env LD_LIBRARY_PATH /usr/local/openmpi-4.0.3/lib
  97. cd $origin_wd
  98. fi
  99. # python
  100. sudo add-apt-repository -y ppa:deadsnakes/ppa
  101. sudo apt-get install python$PYTHON_VERSION python$PYTHON_VERSION-dev python$PYTHON_VERSION-distutils python3-pip -y
  102. sudo update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 100
  103. # pip
  104. python -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple
  105. echo -e "alias pip='python -m pip'" >> ~/.bashrc
  106. python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  107. # install cuda/cudnn
  108. cd /tmp
  109. echo "installing CUDA and cuDNN"
  110. declare -A cuda_url_map=()
  111. 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
  112. 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
  113. cuda_url=${cuda_url_map[$CUDA_VERSION]}
  114. wget $cuda_url
  115. sudo sh ${cuda_url##*/} --silent --toolkit
  116. cd -
  117. sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
  118. sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
  119. sudo add-apt-repository "deb https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu1804/x86_64/ /"
  120. sudo apt-get update
  121. declare -A cudnn_name_map=()
  122. cudnn_name_map["10.1"]="libcudnn7=7.6.5.32-1+cuda10.1 libcudnn7-dev=7.6.5.32-1+cuda10.1"
  123. cudnn_name_map["11.1"]="libcudnn8=8.0.4.30-1+cuda11.1 libcudnn8-dev=8.0.4.30-1+cuda11.1"
  124. sudo apt-get install --no-install-recommends ${cudnn_name_map[$CUDA_VERSION]} -y
  125. # add cuda to path
  126. set +e && source ~/.bashrc
  127. set -e
  128. add_env PATH /usr/local/cuda/bin
  129. add_env LD_LIBRARY_PATH /usr/local/cuda/lib64
  130. add_env LD_LIBRARY_PATH /usr/lib/x86_64-linux-gnu
  131. set +e && source ~/.bashrc
  132. set -e
  133. # wheel
  134. python -m pip install wheel
  135. # python 3.9 needs setuptools>44.0
  136. python -m pip install -U setuptools
  137. echo "The environment is ready to clone and compile mindspore."