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 environment for mindspore ascend compilation on EulerOS 2.8. 18# 19# This file will: 20# - install mindspore dependencies via apt like gcc, cmake 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# - OPENMPI: whether to install optional package Open MPI for distributed training. [on, off(default)] 26# - LOCAL_ASCEND: Ascend AI software package installed path, default /usr/local/Ascend. 27# 28# Usage: 29# Run script like `bash -i ./euleros-ascend-source.sh`. 30# To set augments, run it as `PYTHON_VERSION=3.9 bash -i ./euleros-ascend-source.sh`. 31 32set -e 33 34PYTHON_VERSION=${PYTHON_VERSION:-3.7} 35OPENMPI=${OPENMPI:-off} 36LOCAL_ASCEND=${LOCAL_ASCEND:-/usr/local/Ascend} 37 38available_py_version=(3.7 3.8 3.9) 39if [[ " ${available_py_version[*]} " != *" $PYTHON_VERSION "* ]]; then 40 echo "PYTHON_VERSION is '$PYTHON_VERSION', but available versions are [${available_py_version[*]}]." 41 exit 1 42fi 43 44if ! (ls ${LOCAL_ASCEND}/ascend-toolkit/latest/fwkacllib/lib64/te-*-py3-none-any.whl 1> /dev/null 2>&1); then 45 echo "can not find whl packages in LOCAL_ASCEND=${LOCAL_ASCEND}, please check whether it is a valid path." 46 exit 1 47fi 48 49# add value to environment variable if value is not in it 50add_env() { 51 local name=$1 52 if [[ ":${!name}:" != *":$2:"* ]]; then 53 echo -e "export $1=$2:\$$1" >> ~/.bashrc 54 fi 55} 56 57sudo yum install gcc git gmp-devel tcl patch numactl-devel flex -y 58 59# git-lfs 60set +e && type git-lfs &>/dev/null 61if [[ $? -eq 0 ]]; then 62 echo "git-lfs has been installed, skip." 63else 64 echo "installing git-lfs" 65 cd /tmp 66 if [[ "$(arch)" == "aarch64" ]]; then 67 file_name=git-lfs-linux-arm64-v3.1.2.tar.gz 68 else 69 file_name=git-lfs-linux-amd64-v3.1.2.tar.gz 70 fi 71 curl -OL https://github.com/git-lfs/git-lfs/releases/download/v3.1.2/$file_name 72 mkdir $HOME/git-lfs 73 tar xf $file_name -C $HOME/git-lfs 74 add_env PATH $HOME/git-lfs 75 source ~/.bashrc 76 cd - 77 git lfs install 78fi 79set -e 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 curl -O 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# install conda 109set +e && type conda &>/dev/null 110if [[ $? -eq 0 ]]; then 111 echo "conda has been installed, skip." 112 source "$(conda info --base)"/etc/profile.d/conda.sh 113else 114 install_conda 115fi 116set -e 117 118# set up conda env 119env_name=mindspore_py3${PYTHON_VERSION##*.} 120# constraint openssl when py3.9+310 121openssl_constraint="" 122if [[ "$PYTHON_VERSION" == "3.9" ]]; then 123 openssl_constraint="openssl=1.1.1" 124fi 125conda create -n $env_name python=${PYTHON_VERSION} ${openssl_constraint} -c conda-forge -y 126conda activate $env_name 127 128pip install wheel 129pip install -U setuptools 130 131pip install sympy 132pip install ${LOCAL_ASCEND}/ascend-toolkit/latest/fwkacllib/lib64/te-*-py3-none-any.whl 133pip install ${LOCAL_ASCEND}/ascend-toolkit/latest/fwkacllib/lib64/hccl-*-py3-none-any.whl 134 135# cmake 136echo "installing cmake" 137cd /tmp 138cmake_file_name="cmake-3.19.8-Linux-$(arch).sh" 139curl -O "https://cmake.org/files/v3.19/${cmake_file_name}" 140mkdir $HOME/cmake-3.19.8 141bash cmake-3.19.8-Linux-*.sh --prefix=$HOME/cmake-3.19.8 --exclude-subdir 142add_env PATH $HOME/cmake-3.19.8/bin 143set +e && source ~/.bashrc 144set -e 145cd - 146 147# optional openmpi for distributed training 148if [[ X"$OPENMPI" == "Xon" ]]; then 149 echo "installing openmpi" 150 origin_wd=$PWD 151 cd /tmp 152 curl -O https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.4.tar.gz 153 tar xzf openmpi-4.1.4.tar.gz 154 cd openmpi-4.1.4 155 ./configure --prefix=$HOME/openmpi-4.1.4 156 make 157 sudo make install 158 add_env PATH $HOME/openmpi-4.1.4/bin 159 add_env LD_LIBRARY_PATH $HOME/openmpi-4.1.4/lib 160 cd $origin_wd 161fi 162 163echo "The environment is ready to clone and compile mindspore." 164