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 17# Prepare and Install mindspore gpu by conda on Ubuntu 18.04. 18# 19# This file will: 20# - change deb source to huaweicloud mirror 21# - install mindspore dependencies via apt like gcc, libgmp 22# - install conda and set up environment for mindspore 23# - install mindspore-gpu by conda 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, >=1.6.0 29# - CUDA_VERSION: CUDA version to install. [10.1, 11.1 11.6(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-conda.sh`. 34# To set augments, run it as `PYTHON_VERSION=3.9 CUDA_VERSION=10.1 OPENMPI=on bash -i ./ubuntu-gpu-conda.sh`. 35 36set -e 37 38PYTHON_VERSION=${PYTHON_VERSION:-3.7} 39MINDSPORE_VERSION=${MINDSPORE_VERSION:-EMPTY} 40CUDA_VERSION=${CUDA_VERSION:-11.1} 41OPENMPI=${OPENMPI:-off} 42release_info=$(lsb_release -a | grep Release) 43UBUNTU_VERSION=${release_info//[!0-9]/} 44 45[[ "$UBUNTU_VERSION" == "2004" && "$CUDA_VERSION" == "10.1" ]] && echo "CUDA 10.1 is not supported on Ubuntu 20.04" && exit 1 46 47version_less() { 48 test "$(echo "$@" | tr ' ' '\n' | sort -rV | head -n 1)" != "$1"; 49} 50 51if version_less "${MINDSPORE_VERSION}" "1.6.0"; then 52 echo "MINDSPORE_VERSION should be >=1.6.0, please check available versions at https://www.mindspore.cn/versions." 53 exit 1 54fi 55 56available_py_version=(3.7 3.8 3.9) 57if [[ " ${available_py_version[*]} " != *" $PYTHON_VERSION "* ]]; then 58 echo "PYTHON_VERSION is '$PYTHON_VERSION', but available versions are [${available_py_version[*]}]." 59 exit 1 60fi 61 62if [[ "$PYTHON_VERSION" == "3.8" && ${MINDSPORE_VERSION:0:3} == "1.6" ]]; then 63 echo "PYTHON_VERSION==3.8 is not compatible with MINDSPORE_VERSION==1.6.x, please use PYTHON_VERSION==3.7 or 3.9 for MINDSPORE_VERSION==1.6.x." 64 exit 1 65fi 66 67available_cuda_version=(10.1 11.1 11.6) 68if [[ " ${available_cuda_version[*]} " != *" $CUDA_VERSION "* ]]; then 69 echo "CUDA_VERSION is '$CUDA_VERSION', but available versions are [${available_cuda_version[*]}]." 70 exit 1 71fi 72 73# add value to environment variable if value is not in it 74add_env() { 75 local name=$1 76 if [[ ":${!name}:" != *":$2:"* ]]; then 77 echo -e "export $1=$2:\$$1" >> ~/.bashrc 78 fi 79} 80 81install_conda() { 82 echo "installing Miniconda3" 83 conda_file_name="Miniconda3-py3${PYTHON_VERSION##*.}_4.10.3-Linux-$(arch).sh" 84 cd /tmp 85 wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/$conda_file_name 86 bash $conda_file_name -b 87 cd - 88 . ~/miniconda3/etc/profile.d/conda.sh 89 conda init bash 90 # setting up conda mirror with tsinghua source 91 cat >~/.condarc <<END 92channels: 93 - defaults 94show_channel_urls: true 95default_channels: 96 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main 97 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r 98 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 99custom_channels: 100 conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 101 msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 102 bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 103 menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 104 simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 105END 106} 107 108# use huaweicloud mirror in China 109sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list 110sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list 111sudo apt-get update 112 113sudo apt-get install curl gcc-7 libgmp-dev -y 114 115# optional openmpi for distributed training 116if [[ X"$OPENMPI" == "Xon" ]]; then 117 echo "installing openmpi" 118 cd /tmp 119 curl -O https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.4.tar.gz 120 tar xzf openmpi-4.1.4.tar.gz 121 cd openmpi-4.1.4 122 ./configure --prefix=/usr/local/openmpi-4.1.4 123 make 124 sudo make install 125 set +e && source ~/.bashrc 126 set -e 127 add_env PATH /usr/local/openmpi-4.1.4/bin 128 add_env LD_LIBRARY_PATH /usr/local/openmpi-4.1.4/lib 129fi 130 131# install conda 132set +e && type conda &>/dev/null 133if [[ $? -eq 0 ]]; then 134 echo "conda has been installed, skip." 135 source "$(conda info --base)"/etc/profile.d/conda.sh 136else 137 install_conda 138fi 139set -e 140 141# install cuda/cudnn 142echo "installing CUDA and cuDNN" 143cd /tmp 144declare -A cuda_url_map=() 145cuda_url_map["10.1"]=https://developer.download.nvidia.cn/compute/cuda/10.1/Prod/local_installers/cuda_10.1.243_418.87.00_linux.run 146cuda_url_map["11.1"]=https://developer.download.nvidia.cn/compute/cuda/11.1.1/local_installers/cuda_11.1.1_455.32.00_linux.run 147cuda_url_map["11.6"]=https://developer.download.nvidia.cn/compute/cuda/11.6.0/local_installers/cuda_11.6.0_510.39.01_linux.run 148cuda_url=${cuda_url_map[$CUDA_VERSION]} 149wget $cuda_url 150sudo sh ${cuda_url##*/} --silent --toolkit 151cd - 152sudo apt-key adv --fetch-keys https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/7fa2af80.pub 153sudo add-apt-repository "deb https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/ /" 154sudo add-apt-repository "deb https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu${UBUNTU_VERSION}/x86_64/ /" 155sudo apt-get update 156declare -A cudnn_name_map=() 157cudnn_name_map["10.1"]="libcudnn7=7.6.5.32-1+cuda10.1 libcudnn7-dev=7.6.5.32-1+cuda10.1" 158cudnn_name_map["11.1"]="libcudnn8=8.0.5.39-1+cuda11.1 libcudnn8-dev=8.0.5.39-1+cuda11.1" 159cudnn_name_map["11.6"]="libcudnn8=8.5.0.96-1+cuda11.6 libcudnn8-dev=8.5.0.96-1+cuda11.6" 160sudo apt-get install --no-install-recommends ${cudnn_name_map[$CUDA_VERSION]} -y 161 162# add cuda to path 163set +e && source ~/.bashrc 164set -e 165add_env PATH /usr/local/cuda/bin 166add_env LD_LIBRARY_PATH /usr/local/cuda/lib64 167add_env LD_LIBRARY_PATH /usr/lib/x86_64-linux-gnu 168set +e && source ~/.bashrc 169set -e 170 171# set up conda env and install mindspore-cpu 172env_name=mindspore_py3${PYTHON_VERSION##*.} 173conda create -n $env_name python=${PYTHON_VERSION} -c conda-forge -y 174conda activate $env_name 175install_name="mindspore" 176if [[ $MINDSPORE_VERSION != "EMPTY" ]]; then 177 install_name="${install_name}=${MINDSPORE_VERSION}" 178fi 179conda install ${install_name} -c mindspore -c conda-forge -y 180 181# check mindspore installation 182python -c "import mindspore;mindspore.run_check()" 183 184# check if it can be run with GPU 185cd /tmp 186cat > example.py <<END 187import numpy as np 188from mindspore import Tensor 189import mindspore.ops as ops 190import mindspore.context as context 191 192context.set_context(device_target="GPU") 193x = Tensor(np.ones([1,3,3,4]).astype(np.float32)) 194y = Tensor(np.ones([1,3,3,4]).astype(np.float32)) 195print(ops.add(x, y)) 196END 197python example.py 198cd - 199