Add error checking and conda support

Committer: Anush Elangovan <anush@nod-labs.com>
This commit is contained in:
Anush Elangovan
2022-05-15 12:40:31 -07:00
parent 5dfe5ba5fc
commit 9e48a3525b

View File

@@ -1,54 +1,116 @@
#!/bin/bash
# Sets up a venv suitable for running samples.
# Recommend getting default 'python' to be python 3. For example on Debian:
# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
# Or launch with python=/some/path
# e.g:
# ./setup_venv.sh #setup a default $PYTHON3 shark.venv
# Environment Variables by the script.
# PYTHON=$PYTHON3.9 ./setup_venv.sh #pass a version of $PYTHON to use
# VENV_DIR=myshark.venv #create a venv called myshark.venv
# USE_IREE=1 #use stock IREE instead of Nod.ai's SHARK build
# if you run the script from a conda env it will install in your conda env
TD="$(cd $(dirname $0) && pwd)"
VENV_DIR="$TD/shark.venv"
if [ -z "$PYTHON" ]; then
PYTHON="$(which python3)"
PYTHON="$(which $PYTHON3)"
fi
PYTHON_VERSION_X_Y=`${PYTHON} -c 'import sys; version=sys.version_info[:2]; print("{0}.{1}".format(*version))'`
echo "Setting up venv dir: $VENV_DIR"
echo "Python: $PYTHON"
echo "Python version: $("$PYTHON" --version)"
echo "Python version: $PYTHON_VERSION_X_Y"
function die() {
echo "Error executing command: $*"
exit 1
}
$PYTHON -m venv "$VENV_DIR" || die "Could not create venv."
source "$VENV_DIR/bin/activate" || die "Could not activate venv"
# Upgrade pip and install requirements. 'python' is used here in order to
# reference to the python executable from the venv.
python -m pip install --upgrade pip || die "Could not upgrade pip"
python -m pip install --upgrade -r "$TD/requirements.txt" --extra-index-url https://download.pytorch.org/whl/nightly/cpu -f https://github.com/llvm/torch-mlir/releases
python -m pip install --find-links https://github.com/llvm/torch-mlir/releases torch-mlir
python -m pip install --find-links https://github.com/NodLabs/SHARK/releases iree-compiler iree-runtime
#python -m pip install --find-links https://github.com/google/iree/releases iree-compiler iree-runtime
if [[ -z "${CONDA_PREFIX}" ]]; then
# Not a conda env. So create a new VENV dir
echo "Using pip venv.. "
$PYTHON -m venv "$VENV_DIR" || die "Could not create venv."
source "$VENV_DIR/bin/activate" || die "Could not activate venv"
else
echo "Found conda env $CONDA_DEFAULT_ENV. Running pip install inside the conda env"
fi
Red=`tput setaf 1`
Green=`tput setaf 2`
Yellow=`tput setaf 3`
# Assume no binary torch-mlir.
# Currently available for macOS m1&intel (3.9) and Linux(3.7,3.8,3.9,3.10)
torch_mlir_bin=false
if [[ $(uname -s) = 'Darwin' ]]; then
echo "${Yellow}Apple macOS detected"
if [[ $(uname -m) == 'arm64' ]]; then
echo "${Yellow}Apple M1 Detected"
echo "${Yellow}If tokenizers install fails make sure you have the latest rustc"
echo "${Yellow}Please run:"
echo "${Yellow}curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
hash rustc 2>/dev/null
if [ $? -eq 0 ];then
echo "${Green}rustc found to compile HF tokenizers"
else
echo "${Red}Could not find rustc" >&2
echo "${Red}Please run:"
echo "${Red}curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
exit 1
fi
fi
echo "${Yellow}Run the following commands to setup your SSL certs for your Python version if you see SSL errors with tests"
echo "${Yellow}/Applications/Python\ 3.XX/Install\ Certificates.command"
if [ "$PYTHON_VERSION_X_Y" == "3.9" ]; then
torch_mlir_bin=true
fi
elif [[ $(uname -s) = 'Linux' ]]; then
echo "${Yellow}Linux detected"
if [ "$PYTHON_VERSION_X_Y" == "3.7" ] || [ "$PYTHON_VERSION_X_Y" == "3.8" ] || [ "$PYTHON_VERSION_X_Y" == "3.9" ] || [ "$PYTHON_VERSION_X_Y" == "3.10" ] ; then
torch_mlir_bin=true
fi
else
echo "${Red}OS not detected. Pray and Play"
fi
# Upgrade pip and install requirements.
$PYTHON -m pip install --upgrade pip || die "Could not upgrade pip"
$PYTHON -m pip install --upgrade -r "$TD/requirements.txt" --extra-index-url https://download.pytorch.org/whl/nightly/cpu -f https://github.com/llvm/torch-mlir/releases
if [ "$torch_mlir_bin" = true ]; then
$PYTHON -m pip install --find-links https://github.com/llvm/torch-mlir/releases torch-mlir
if [ $? -eq 0 ];then
echo "Successfully Installed torch-mlir"
else
echo "Could not install torch-mlir" >&2
fi
else
echo "${Red}No binaries found for Python $PYTHON_VERSION_X_Y on $(uname -s)"
echo "${Yello}Python 3.9 supported on macOS and 3.7,3.8,3.9 and 3.10 on Linux"
echo "${Red}Please build torch-mlir from source in your environment"
exit 1
fi
if [[ -z "${USE_IREE}" ]]; then
echo "Installing SHARK..."
$PYTHON -m pip install --find-links https://github.com/NodLabs/SHARK/releases iree-compiler iree-runtime
if [ $? -eq 0 ];then
echo "Successfully Installed SHARK Runtime"
else
echo "Could not install SHARK" >&2
exit 1
fi
else
echo "Installing IREE..."
$PYTHON -m pip install --find-links https://github.com/google/iree/releases iree-compiler iree-runtime
if [ $? -eq 0 ];then
echo "Successfully Installed IREE Runtime"
else
echo "Could not install IREE" >&2
exit 1
fi
fi
python -m pip install transformers
python -m pip install git+https://github.com/pytorch/functorch.git
$PYTHON -m pip install transformers
$PYTHON -m pip install git+https://github.com/pytorch/functorch.git
$PYTHON -m pip wheel -v -w $TD/wheelhouse $TD -f https://github.com/NodLabs/SHARK/releases -f https://github.com/llvm/torch-mlir/releases --extra-index-url https://download.pytorch.org/whl/nightly/cpu
$PYTHON -m pip install . --extra-index-url https://download.pytorch.org/whl/nightly/cpu -f https://github.com/llvm/torch-mlir/releases -f https://github.com/NodLabs/SHARK/releases
pip wheel -v -w $TD/wheelhouse $TD -f https://github.com/NodLabs/SHARK/releases -f https://github.com/llvm/torch-mlir/releases --extra-index-url https://download.pytorch.org/whl/nightly/cpu
python -m pip install . --extra-index-url https://download.pytorch.org/whl/nightly/cpu -f https://github.com/llvm/torch-mlir/releases -f https://github.com/NodLabs/SHARK/releases
echo "${Green}Before running examples activate venv with:"
echo " ${Green}source $VENV_DIR/bin/activate"
if [[ -z "${CONDA_PREFIX}" ]]; then
echo "${Green}Before running examples activate venv with:"
echo " ${Green}source $VENV_DIR/bin/activate"
fi