2023-03-27 19:37:00 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2023-03-28 01:58:03 +00:00
|
|
|
# This file will be the host environment setup file for all operating systems other than base Windows.
|
|
|
|
|
2023-03-29 01:07:18 +00:00
|
|
|
display_help() {
|
|
|
|
cat <<EOF
|
|
|
|
Kohya_SS Installation Script for POSIX operating systems.
|
|
|
|
|
|
|
|
The following options are useful in a runpod environment,
|
|
|
|
but will not affect a local machine install.
|
|
|
|
|
|
|
|
Usage:
|
2023-03-29 07:47:03 +00:00
|
|
|
setup.sh -b dev -d /workspace/kohya_ss -g https://mycustom.repo.tld/custom_fork.git
|
|
|
|
setup.sh --branch=dev --dir=/workspace/kohya_ss --git-repo=https://mycustom.repo.tld/custom_fork.git
|
2023-03-29 01:07:18 +00:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-b BRANCH, --branch=BRANCH Select which branch of kohya to checkout on new installs.
|
|
|
|
-d DIR, --dir=DIR The full path you want kohya_ss installed to.
|
2023-03-29 07:47:03 +00:00
|
|
|
-g, --git_repo You can optionally provide a git repo to checkout for runpod installation. Useful for custom forks.
|
|
|
|
-r, --runpod Forces a runpod installation. Useful if detection fails for any reason.
|
|
|
|
-h, --help Show this screen.
|
2023-03-29 01:07:18 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
# Variables defined before the getopts loop, so we have sane default values.
|
|
|
|
DIR="/workspace/kohya_ss"
|
2023-03-29 17:53:15 +00:00
|
|
|
BRANCH="master"
|
2023-03-29 07:47:03 +00:00
|
|
|
GIT_REPO="https://github.com/bmaltais/kohya_ss.git"
|
|
|
|
RUNPOD=false
|
2023-03-29 01:07:18 +00:00
|
|
|
|
2023-03-29 07:47:03 +00:00
|
|
|
while getopts "b:d:g:r-:" opt; do
|
2023-03-29 01:07:18 +00:00
|
|
|
# support long options: https://stackoverflow.com/a/28466267/519360
|
|
|
|
if [ "$opt" = "-" ]; then # long option: reformulate OPT and OPTARG
|
|
|
|
opt="${OPTARG%%=*}" # extract long option name
|
|
|
|
OPTARG="${OPTARG#$opt}" # extract long option argument (may be empty)
|
|
|
|
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
|
|
|
|
fi
|
|
|
|
case $opt in
|
|
|
|
b | branch) BRANCH="$OPTARG" ;;
|
|
|
|
d | dir) DIR="$OPTARG" ;;
|
2023-03-29 07:47:03 +00:00
|
|
|
g | git-repo) GIT_REPO="$OPTARG" ;;
|
|
|
|
r | runpod) RUNPOD=true ;;
|
2023-03-29 01:07:18 +00:00
|
|
|
h) display_help && exit 0 ;;
|
|
|
|
*) display_help && exit 0 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
|
|
|
# This must be set after the getopts loop to account for $DIR changes.
|
2023-03-29 17:53:15 +00:00
|
|
|
PARENT_DIR="$(dirname "${DIR}")"
|
2023-03-29 01:07:18 +00:00
|
|
|
VENV_DIR="$DIR/venv"
|
|
|
|
|
2023-03-27 19:37:00 +00:00
|
|
|
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
|
|
|
|
# Check if root or sudo
|
|
|
|
root=true
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
root=false
|
|
|
|
fi
|
|
|
|
|
2023-03-29 01:07:18 +00:00
|
|
|
env_var_exists() {
|
|
|
|
local env_var=
|
|
|
|
env_var=$(declare -p "$1")
|
|
|
|
if ! [[ -v $1 && $env_var =~ ^declare\ -x ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:52:16 +00:00
|
|
|
get_distro_name() {
|
|
|
|
local line
|
|
|
|
if [ -f /etc/os-release ]; then
|
|
|
|
# We search for the line starting with ID=
|
|
|
|
# Then we remove the ID= prefix to get the name itself
|
|
|
|
line="$(grep -Ei '^ID=' /etc/os-release)"
|
|
|
|
line=${line##*=}
|
|
|
|
echo "$line"
|
|
|
|
return 0
|
|
|
|
elif command -v python >/dev/null; then
|
|
|
|
line="$(python -mplatform)"
|
|
|
|
echo "$line"
|
|
|
|
return 0
|
|
|
|
elif command -v python3 >/dev/null; then
|
|
|
|
line="$(python3 -mplatform)"
|
|
|
|
echo "$line"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
line="None"
|
|
|
|
echo "$line"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_distro_family() {
|
|
|
|
local line
|
|
|
|
if [ -f /etc/os-release ]; then
|
|
|
|
# We search for the line starting with ID_LIKE=
|
|
|
|
# Then we remove the ID_LIKE= prefix to get the name itself
|
|
|
|
# This is the "type" of distro. For example, Ubuntu returns "debian".
|
|
|
|
if grep -Eiq '^ID_LIKE=' /etc/os-release >/dev/null; then
|
|
|
|
line="$(grep -Ei '^ID_LIKE=' /etc/os-release)"
|
|
|
|
line=${line##*=}
|
|
|
|
echo "$line"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
line="None"
|
|
|
|
echo "$line"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
line="None"
|
|
|
|
echo "$line"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:11:10 +00:00
|
|
|
if env_var_exists RUNPOD_POD_ID || env_var_exists RUNPOD_API_KEY; then
|
|
|
|
RUNPOD=true
|
|
|
|
fi
|
|
|
|
|
2023-03-29 01:07:18 +00:00
|
|
|
# This is the pre-install work for a kohya installation on a runpod
|
2023-03-29 07:47:03 +00:00
|
|
|
if [ "$RUNPOD" = true ]; then
|
2023-03-29 01:07:18 +00:00
|
|
|
if [ -d "$VENV_DIR" ]; then
|
|
|
|
echo "Pre-existing installation on a runpod detected."
|
|
|
|
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$VENV_DIR"/lib/python3.10/site-packages/tensorrt/
|
|
|
|
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$VENV_DIR"/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/
|
|
|
|
cd "$DIR" || exit 1
|
2023-03-29 07:47:03 +00:00
|
|
|
sed -i "s/interface.launch(\*\*launch_kwargs)/interface.launch(\*\*launch_kwargs,share=True)/g" ./kohya_gui.py
|
2023-03-29 01:07:18 +00:00
|
|
|
else
|
|
|
|
echo "Clean installation on a runpod detected."
|
2023-03-29 17:53:15 +00:00
|
|
|
cd "$PARENT_DIR" || exit 1
|
|
|
|
if [ ! -d "$DIR/.git" ]; then
|
|
|
|
echo "Cloning $GIT_REPO."
|
|
|
|
git clone "$GIT_REPO"
|
|
|
|
cd "$DIR" || exit 1
|
|
|
|
git checkout "$BRANCH"
|
|
|
|
else
|
|
|
|
cd "$DIR" || exit 1
|
|
|
|
echo "git repo detected. Attempting tp update repo instead."
|
|
|
|
echo "Updating: $GIT_REPO"
|
|
|
|
git pull "$GIT_REPO"
|
|
|
|
fi
|
2023-03-29 01:07:18 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-03-28 22:52:16 +00:00
|
|
|
distro=get_distro_name
|
|
|
|
family=get_distro_family
|
2023-03-29 18:42:36 +00:00
|
|
|
|
|
|
|
echo "Installing Python TK if not found on the system."
|
|
|
|
|
2023-03-28 22:52:16 +00:00
|
|
|
if "$distro" | grep -qi "Ubuntu" || "$family" | grep -qi "Ubuntu"; then
|
2023-03-27 19:37:00 +00:00
|
|
|
echo "Ubuntu detected."
|
2023-03-29 18:42:36 +00:00
|
|
|
if [ $(dpkg-query -W -f='${Status}' python3-tk 2>/dev/null | grep -c "ok installed") = 0 ]; then
|
2023-03-27 19:37:00 +00:00
|
|
|
if [ root = true ]; then
|
2023-03-29 01:07:18 +00:00
|
|
|
apt update -y && apt install -y python3-tk
|
2023-03-27 19:37:00 +00:00
|
|
|
else
|
|
|
|
echo "This script needs to be run as root or via sudo to install packages."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Python TK found! Skipping install!"
|
|
|
|
fi
|
|
|
|
elif "$distro" | grep -Eqi "Fedora|CentOS|Redhat"; then
|
2023-03-29 18:42:36 +00:00
|
|
|
echo "Redhat or Redhat base detected."
|
2023-03-27 19:37:00 +00:00
|
|
|
if ! rpm -qa | grep -qi python3-tkinter; then
|
|
|
|
if [ root = true ]; then
|
2023-03-28 23:02:20 +00:00
|
|
|
dnf install python3-tkinter -y
|
2023-03-27 19:37:00 +00:00
|
|
|
else
|
|
|
|
echo "This script needs to be run as root or via sudo to install packages."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
2023-03-28 22:52:16 +00:00
|
|
|
elif "$distro" | grep -Eqi "arch" || "$family" | grep -qi "arch"; then
|
2023-03-29 18:42:36 +00:00
|
|
|
echo "Arch Linux or Arch base detected."
|
2023-03-28 22:52:16 +00:00
|
|
|
if ! pacman -Qi tk >/dev/null; then
|
|
|
|
if [ root = true ]; then
|
2023-03-28 23:02:20 +00:00
|
|
|
pacman --noconfirm -S tk
|
2023-03-28 22:52:16 +00:00
|
|
|
else
|
|
|
|
echo "This script needs to be run as root or via sudo to install packages."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
elif "$distro" | grep -Eqi "opensuse" || "$family" | grep -qi "opensuse"; then
|
2023-03-29 18:42:36 +00:00
|
|
|
echo "OpenSUSE detected."
|
2023-03-28 22:52:16 +00:00
|
|
|
if ! rpm -qa | grep -qi python-tk; then
|
|
|
|
if [ root = true ]; then
|
2023-03-28 23:02:20 +00:00
|
|
|
zypper install -y python-tk
|
2023-03-28 22:52:16 +00:00
|
|
|
else
|
|
|
|
echo "This script needs to be run as root or via sudo to install packages."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
elif [ "$distro" = "None" ] || [ "$family" = "None" ]; then
|
|
|
|
if [ "$distro" = "None" ]; then
|
|
|
|
echo "We could not detect your distribution of Linux. Please file a bug report on github with the contents of your /etc/os-release file."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$family" = "None" ]; then
|
|
|
|
echo "We could not detect the family of your Linux distribution. Please file a bug report on github with the contents of your /etc/os-release file."
|
|
|
|
fi
|
2023-03-27 19:37:00 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
python3 -m venv venv
|
|
|
|
source venv/bin/activate
|
|
|
|
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
|
|
|
|
pip install --use-pep517 --upgrade -r requirements.txt
|
|
|
|
pip install -U -I --no-deps https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/linux/xformers-0.0.14.dev0-cp310-cp310-linux_x86_64.whl
|
2023-03-29 01:07:18 +00:00
|
|
|
|
|
|
|
# We need this extra package and setup if we are running in a runpod
|
2023-03-29 07:47:03 +00:00
|
|
|
if [ "$RUNPOD" = true ]; then
|
2023-03-29 01:07:18 +00:00
|
|
|
pip install tensorrt
|
|
|
|
ln -s "$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer_plugin.so.8" \
|
|
|
|
"$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer_plugin.so.7"
|
|
|
|
ln -s "$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer.so.8" \
|
|
|
|
"$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer.so.7"
|
|
|
|
ln -s "$VENV_DIR/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/libcudart.so.12" \
|
|
|
|
"$VENV_DIR/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/libcudart.so.11.0"
|
|
|
|
|
|
|
|
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$VENV_DIR/lib/python3.10/site-packages/tensorrt/"
|
|
|
|
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$VENV_DIR/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/"
|
|
|
|
|
|
|
|
# This is a non-interactive environment, so just directly call gui.sh after all setup steps are complete.
|
|
|
|
if command -v bash >/dev/null; then
|
|
|
|
bash "$DIR"/gui.sh
|
|
|
|
else
|
|
|
|
# This shouldn't happen, but we're going to try to help.
|
|
|
|
sh "$DIR"/gui.sh
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-03-27 19:37:00 +00:00
|
|
|
accelerate config
|
|
|
|
|
|
|
|
echo -e "Setup finished! Run \e[0;92m./gui.sh\e[0m to start."
|
|
|
|
elif [[ "$OSTYPE" == "darwin"* ]]; then
|
|
|
|
# The initial setup script to prep the environment on macOS
|
|
|
|
# xformers has been omitted as that is for Nvidia GPUs only
|
|
|
|
|
|
|
|
if ! command -v brew >/dev/null; then
|
|
|
|
echo "Please install homebrew first. This is a requirement for the remaining setup."
|
|
|
|
echo "You can find that here: https://brew.sh"
|
2023-03-28 22:52:16 +00:00
|
|
|
#shellcheck disable=SC2016
|
|
|
|
echo 'The "brew" command should be in $PATH to be detected.'
|
2023-03-27 19:37:00 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Install base python packages
|
|
|
|
echo "Installing Python 3.10 if not found."
|
|
|
|
if ! brew ls --versions python@3.10 >/dev/null; then
|
|
|
|
brew install python@3.10
|
|
|
|
else
|
|
|
|
echo "Python 3.10 found!"
|
|
|
|
fi
|
|
|
|
echo "Installing Python-TK 3.10 if not found."
|
|
|
|
if ! brew ls --versions python-tk@3.10 >/dev/null; then
|
|
|
|
brew install python-tk@3.10
|
|
|
|
else
|
|
|
|
echo "Python Tkinter 3.10 found!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if command -v python3.10 >/dev/null; then
|
|
|
|
python3.10 -m venv venv
|
|
|
|
source venv/bin/activate
|
|
|
|
|
|
|
|
# DEBUG ONLY
|
|
|
|
#pip install pydevd-pycharm~=223.8836.43
|
|
|
|
|
|
|
|
# Tensorflow installation
|
|
|
|
if wget https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl /tmp; then
|
|
|
|
python -m pip install tensorflow==0.1a3 -f https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
|
|
|
|
rm -f /tmp/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
|
|
|
|
fi
|
|
|
|
|
|
|
|
pip install torch==2.0.0 torchvision==0.15.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html
|
|
|
|
python -m pip install --use-pep517 --upgrade -r requirements.txt
|
|
|
|
accelerate config
|
|
|
|
echo -e "Setup finished! Run ./gui.sh to start."
|
|
|
|
else
|
|
|
|
echo "Python not found. Please ensure you install Python."
|
|
|
|
echo "The brew command for Python 3.10 is: brew install python@3.10"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
elif [[ "$OSTYPE" == "cygwin" ]]; then
|
|
|
|
# Cygwin is a standalone suite of Linux utilies on Windows
|
|
|
|
echo "This hasn't been validated on cygwin yet."
|
|
|
|
elif [[ "$OSTYPE" == "msys" ]]; then
|
|
|
|
# MinGW has the msys environment which is a standalone suite of Linux utilies on Windows
|
|
|
|
# "git bash" on Windows may also be detected as msys.
|
|
|
|
echo "This hasn't been validated in msys (mingw) on Windows yet."
|
|
|
|
fi
|