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 pip 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 python3 & pip3 via apt and set it to default 23# - install CUDA by run file and cudnn via apt. 24# - install mindspore-cpu within new installed python by pip 25# - compile and install Open MPI if OPENMPI is set to on. 26# 27# Augments: 28# - PYTHON_VERSION: python version to install. [3.7(default), 3.8, 3.9] 29# - MINDSPORE_VERSION: mindspore version to install, >=1.6.0, required 30# - CUDA_VERSION: CUDA version to install. [10.1, 11.1 11.6(default)] 31# - OPENMPI: whether to install optional package Open MPI for distributed training. [on, off(default)] 32# 33# Usage: 34# Run script like `MINDSPORE_VERSION=1.7.0 bash -i ./ubuntu-gpu-pip.sh`. 35# To set augments, run it as `MINDSPORE_VERSION=1.6.0 CUDA_VERSION=10.1 OPENMPI=on bash -i ./ubuntu-gpu-pip.sh`. 36 37set -e 38 39PYTHON_VERSION=${PYTHON_VERSION:-3.7} 40MINDSPORE_VERSION=${MINDSPORE_VERSION:EMPTY} 41CUDA_VERSION=${CUDA_VERSION:-11.6} 42OPENMPI=${OPENMPI:-off} 43release_info=$(lsb_release -a | grep Release) 44UBUNTU_VERSION=${release_info//[!0-9]/} 45 46[[ "$UBUNTU_VERSION" == "2004" && "$CUDA_VERSION" == "10.1" ]] && echo "CUDA 10.1 is not supported on Ubuntu 20.04" && exit 1 47 48version_less() { 49 test "$(echo "$@" | tr ' ' '\n' | sort -rV | head -n 1)" != "$1"; 50} 51 52if [ $MINDSPORE_VERSION == "EMPTY" ] || version_less "${MINDSPORE_VERSION}" "1.6.0"; then 53 echo "MINDSPORE_VERSION should be >=1.6.0, please check available versions at https://www.mindspore.cn/versions." 54 exit 1 55fi 56 57available_py_version=(3.7 3.8 3.9) 58if [[ " ${available_py_version[*]} " != *" $PYTHON_VERSION "* ]]; then 59 echo "PYTHON_VERSION is '$PYTHON_VERSION', but available versions are [${available_py_version[*]}]." 60 exit 1 61fi 62 63if [[ "$PYTHON_VERSION" == "3.8" && ${MINDSPORE_VERSION:0:3} == "1.6" ]]; then 64 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." 65 exit 1 66fi 67 68available_cuda_version=(10.1 11.1 11.6) 69if [[ " ${available_cuda_version[*]} " != *" $CUDA_VERSION "* ]]; then 70 echo "CUDA_VERSION is '$CUDA_VERSION', but available versions are [${available_cuda_version[*]}]." 71 exit 1 72fi 73declare -A minimum_driver_version_map=() 74minimum_driver_version_map["10.1"]="418.39" 75minimum_driver_version_map["11.1"]="450.80.02" 76minimum_driver_version_map["11.6"]="510.39.01" 77driver_version=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader --id=0) 78if [[ $driver_version < ${minimum_driver_version_map[$CUDA_VERSION]} ]]; then 79 echo "CUDA $CUDA_VERSION minimum required driver version is ${minimum_driver_version_map[$CUDA_VERSION]}, \ 80 but current nvidia driver version is $driver_version, please upgrade your driver manually." 81 exit 1 82fi 83cuda_name="cuda-$CUDA_VERSION" 84 85declare -A version_map=() 86version_map["3.7"]="${MINDSPORE_VERSION/-/}-cp37-cp37m" 87version_map["3.8"]="${MINDSPORE_VERSION/-/}-cp38-cp38" 88version_map["3.9"]="${MINDSPORE_VERSION/-/}-cp39-cp39" 89 90# add value to environment variable if value is not in it 91add_env() { 92 local name=$1 93 if [[ ":${!name}:" != *":$2:"* ]]; then 94 echo -e "export $1=$2:\$$1" >> ~/.bashrc 95 fi 96} 97 98# use huaweicloud mirror in China 99sudo sed -i "s@http://.*archive.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list 100sudo sed -i "s@http://.*security.ubuntu.com@http://repo.huaweicloud.com@g" /etc/apt/sources.list 101sudo apt-get update 102 103sudo apt-get install curl make gcc-7 libgmp-dev -y 104 105# python 106sudo add-apt-repository -y ppa:deadsnakes/ppa 107sudo apt-get install python$PYTHON_VERSION python$PYTHON_VERSION-distutils python3-pip -y 108sudo update-alternatives --install /usr/bin/python python /usr/bin/python$PYTHON_VERSION 100 109# pip 110python -m pip install -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple 111echo -e "alias pip='python -m pip'" >> ~/.bashrc 112python -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 113 114# install cuda/cudnn 115echo "installing CUDA and cuDNN" 116cd /tmp 117declare -A cuda_url_map=() 118cuda_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 119cuda_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 120cuda_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 121cuda_url=${cuda_url_map[$CUDA_VERSION]} 122wget $cuda_url 123sudo sh ${cuda_url##*/} --silent --toolkit 124cd - 125sudo apt-key adv --fetch-keys https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/7fa2af80.pub 126sudo add-apt-repository "deb https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/ /" 127sudo add-apt-repository "deb https://developer.download.nvidia.cn/compute/machine-learning/repos/ubuntu${UBUNTU_VERSION}/x86_64/ /" 128sudo apt-get update 129declare -A cudnn_name_map=() 130cudnn_name_map["10.1"]="libcudnn7=7.6.5.32-1+cuda10.1 libcudnn7-dev=7.6.5.32-1+cuda10.1" 131cudnn_name_map["11.1"]="libcudnn8=8.0.5.39-1+cuda11.1 libcudnn8-dev=8.0.5.39-1+cuda11.1" 132cudnn_name_map["11.6"]="libcudnn8=8.5.0.96-1+cuda11.6 libcudnn8-dev=8.5.0.96-1+cuda11.6" 133sudo apt-get install --no-install-recommends ${cudnn_name_map[$CUDA_VERSION]} -y 134 135# add cuda to path 136set +e && source ~/.bashrc 137set -e 138add_env PATH /usr/local/cuda/bin 139add_env LD_LIBRARY_PATH /usr/local/cuda/lib64 140add_env LD_LIBRARY_PATH /usr/lib/x86_64-linux-gnu 141set +e && source ~/.bashrc 142set -e 143 144# optional openmpi for distributed training 145if [[ X"$OPENMPI" == "Xon" ]]; then 146 echo "installing openmpi" 147 cd /tmp 148 curl -O https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.4.tar.gz 149 tar xzf openmpi-4.1.4.tar.gz 150 cd openmpi-4.1.4 151 ./configure --prefix=/usr/local/openmpi-4.1.4 152 make 153 sudo make install 154 add_env PATH /usr/local/openmpi-4.1.4/bin 155 add_env LD_LIBRARY_PATH /usr/local/openmpi-4.1.4/lib 156fi 157 158arch=`uname -m` 159python -m pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/unified/${arch}/${cuda_name}/mindspore-${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 160 161# check mindspore installation 162python -c "import mindspore;mindspore.run_check()" 163 164# check if it can be run with GPU 165cd /tmp 166cat > example.py <<END 167import numpy as np 168from mindspore import Tensor 169import mindspore.ops as ops 170import mindspore.context as context 171 172context.set_context(device_target="GPU") 173x = Tensor(np.ones([1,3,3,4]).astype(np.float32)) 174y = Tensor(np.ones([1,3,3,4]).astype(np.float32)) 175print(ops.add(x, y)) 176END 177python example.py 178cd - 179