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 ascend by pip on EulerOS 2.8. 18# 19# This file will: 20# - install mindspore dependencies via apt like gcc, libgmp 21# - install conda and set up environment for mindspore 22# 23# Augments: 24# - PYTHON_VERSION: python version to set up. [3.7(default), 3.8, 3.9] 25# - MINDSPORE_VERSION: mindspore version to install, >=1.6.0, required 26# - LOCAL_ASCEND: Ascend AI software package installed path, default /usr/local/Ascend. 27# - OPENMPI: whether to install optional package Open MPI for distributed training. [on, off(default)] 28# 29# Usage: 30# Run script like `MINDSPORE_VERSION=1.7.0 bash ./euleros-ascend-pip.sh`. 31# To set augments, run it as `PYTHON_VERSION=3.9 MINDSPORE_VERSION=1.6.0 bash ./euleros-ascend-pip.sh`. 32 33set -e 34 35PYTHON_VERSION=${PYTHON_VERSION:-3.7} 36MINDSPORE_VERSION=${MINDSPORE_VERSION:-EMPTY} 37LOCAL_ASCEND=${LOCAL_ASCEND:-/usr/local/Ascend} 38OPENMPI=${OPENMPI:-off} 39 40version_less() { 41 test "$(echo "$@" | tr ' ' '\n' | sort -rV | head -n 1)" != "$1"; 42} 43 44if [ $MINDSPORE_VERSION == "EMPTY" ] || version_less "${MINDSPORE_VERSION}" "1.6.0"; then 45 echo "MINDSPORE_VERSION should be >=1.6.0, please check available versions at https://www.mindspore.cn/versions." 46 exit 1 47fi 48 49available_py_version=(3.7 3.8 3.9) 50if [[ " ${available_py_version[*]} " != *" $PYTHON_VERSION "* ]]; then 51 echo "PYTHON_VERSION is '$PYTHON_VERSION', but available versions are [${available_py_version[*]}]." 52 exit 1 53fi 54 55if ! (ls ${LOCAL_ASCEND}/ascend-toolkit/latest/fwkacllib/lib64/te-*-py3-none-any.whl 1> /dev/null 2>&1); then 56 echo "can not find whl packages in LOCAL_ASCEND=${LOCAL_ASCEND}, please check whether it is a valid path." 57 exit 1 58fi 59 60declare -A version_map=() 61version_map["3.7"]="${MINDSPORE_VERSION/-/}-cp37-cp37m" 62version_map["3.8"]="${MINDSPORE_VERSION/-/}-cp38-cp38" 63version_map["3.9"]="${MINDSPORE_VERSION/-/}-cp39-cp39" 64 65# add value to environment variable if value is not in it 66add_env() { 67 local name=$1 68 if [[ ":${!name}:" != *":$2:"* ]]; then 69 echo -e "export $1=$2:\$$1" >> ~/.bashrc 70 fi 71} 72 73sudo yum install gcc gmp-devel -y 74 75install_conda() { 76 echo "installing Miniconda3" 77 conda_file_name="Miniconda3-py3${PYTHON_VERSION##*.}_4.10.3-Linux-$(arch).sh" 78 cd /tmp 79 curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/$conda_file_name 80 bash $conda_file_name -b 81 cd - 82 . ~/miniconda3/etc/profile.d/conda.sh 83 conda init bash 84 # setting up conda mirror with tsinghua source 85 cat >~/.condarc <<END 86channels: 87 - defaults 88show_channel_urls: true 89default_channels: 90 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main 91 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r 92 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 93custom_channels: 94 conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 95 msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 96 bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 97 menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 98 simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud 99END 100} 101 102# install conda 103set +e && type conda &>/dev/null 104if [[ $? -eq 0 ]]; then 105 echo "conda has been installed, skip." 106 source "$(conda info --base)"/etc/profile.d/conda.sh 107else 108 install_conda 109fi 110set -e 111 112# set up conda env 113env_name=mindspore_py3${PYTHON_VERSION##*.} 114conda create -n $env_name python=${PYTHON_VERSION} -c conda-forge -y 115conda activate $env_name 116 117pip install sympy 118pip install ${LOCAL_ASCEND}/ascend-toolkit/latest/fwkacllib/lib64/te-*-py3-none-any.whl 119pip install ${LOCAL_ASCEND}/ascend-toolkit/latest/fwkacllib/lib64/hccl-*-py3-none-any.whl 120 121# optional openmpi for distributed training 122if [[ X"$OPENMPI" == "Xon" ]]; then 123 echo "installing openmpi" 124 origin_wd=$PWD 125 cd /tmp 126 curl -O https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.4.tar.gz 127 tar xzf openmpi-4.1.4.tar.gz 128 cd openmpi-4.1.4 129 ./configure --prefix=$HOME/openmpi-4.1.4 130 make 131 sudo make install 132 add_env PATH $HOME/openmpi-4.1.4/bin 133 add_env LD_LIBRARY_PATH $HOME/openmpi-4.1.4/lib 134 cd $origin_wd 135fi 136 137ARCH=`uname -m` 138pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/${MINDSPORE_VERSION}/MindSpore/unified/${ARCH}/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 139