2022-12-13 04:18:25 +08:00
|
|
|
#!/usr/bin/env bash
|
2022-09-13 21:28:04 +08:00
|
|
|
#################################################
|
|
|
|
# Please do not make any changes to this file, #
|
|
|
|
# change the variables in webui-user.sh instead #
|
|
|
|
#################################################
|
2022-10-08 03:25:01 +08:00
|
|
|
|
2023-07-03 15:15:19 +08:00
|
|
|
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
|
2023-07-19 12:59:39 +08:00
|
|
|
|
2022-12-03 15:28:53 +08:00
|
|
|
# If run from macOS, load defaults from webui-macos-env.sh
|
|
|
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
2023-07-03 15:15:19 +08:00
|
|
|
if [[ -f "$SCRIPT_DIR"/webui-macos-env.sh ]]
|
2022-12-03 15:28:53 +08:00
|
|
|
then
|
2023-07-03 15:15:19 +08:00
|
|
|
source "$SCRIPT_DIR"/webui-macos-env.sh
|
2022-12-03 15:28:53 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-09-13 21:28:04 +08:00
|
|
|
# Read variables from webui-user.sh
|
|
|
|
# shellcheck source=/dev/null
|
2023-07-03 15:15:19 +08:00
|
|
|
if [[ -f "$SCRIPT_DIR"/webui-user.sh ]]
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
2023-07-03 15:15:19 +08:00
|
|
|
source "$SCRIPT_DIR"/webui-user.sh
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
2023-03-13 01:50:02 +08:00
|
|
|
|
2023-10-02 15:43:59 +08:00
|
|
|
# If $venv_dir is "-", then disable venv support
|
|
|
|
use_venv=1
|
|
|
|
if [[ $venv_dir == "-" ]]; then
|
|
|
|
use_venv=0
|
|
|
|
fi
|
|
|
|
|
2022-09-13 21:28:04 +08:00
|
|
|
# Set defaults
|
|
|
|
# Install directory without trailing slash
|
|
|
|
if [[ -z "${install_dir}" ]]
|
|
|
|
then
|
2023-07-03 15:17:27 +08:00
|
|
|
install_dir="$SCRIPT_DIR"
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Name of the subdirectory (defaults to stable-diffusion-webui)
|
|
|
|
if [[ -z "${clone_dir}" ]]
|
|
|
|
then
|
|
|
|
clone_dir="stable-diffusion-webui"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# python3 executable
|
|
|
|
if [[ -z "${python_cmd}" ]]
|
|
|
|
then
|
2024-06-26 03:01:33 +08:00
|
|
|
python_cmd="python3.10"
|
|
|
|
fi
|
|
|
|
if [[ ! -x "$(command -v "${python_cmd}")" ]]
|
|
|
|
then
|
|
|
|
python_cmd="python3"
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# git executable
|
2022-09-13 23:28:54 +08:00
|
|
|
if [[ -z "${GIT}" ]]
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
2022-09-13 23:28:54 +08:00
|
|
|
export GIT="git"
|
2023-09-13 20:20:01 +08:00
|
|
|
else
|
|
|
|
export GIT_PYTHON_GIT_EXECUTABLE="${GIT}"
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
|
2023-07-19 06:48:21 +08:00
|
|
|
if [[ -z "${venv_dir}" ]] && [[ $use_venv -eq 1 ]]
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
2023-03-13 01:50:02 +08:00
|
|
|
venv_dir="venv"
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
|
|
|
|
2022-09-30 17:44:08 +08:00
|
|
|
if [[ -z "${LAUNCH_SCRIPT}" ]]
|
2022-09-30 16:51:05 +08:00
|
|
|
then
|
2023-03-13 01:50:02 +08:00
|
|
|
LAUNCH_SCRIPT="launch.py"
|
2022-09-30 16:51:05 +08:00
|
|
|
fi
|
|
|
|
|
2022-10-08 03:25:01 +08:00
|
|
|
# this script cannot be run as root by default
|
|
|
|
can_run_as_root=0
|
|
|
|
|
|
|
|
# read any command line flags to the webui.sh script
|
2022-12-01 17:04:14 +08:00
|
|
|
while getopts "f" flag > /dev/null 2>&1
|
2022-10-08 03:25:01 +08:00
|
|
|
do
|
|
|
|
case ${flag} in
|
|
|
|
f) can_run_as_root=1;;
|
2022-12-01 17:04:14 +08:00
|
|
|
*) break;;
|
2022-10-08 03:25:01 +08:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2022-09-26 18:55:10 +08:00
|
|
|
# Disable sentry logging
|
|
|
|
export ERROR_REPORTING=FALSE
|
|
|
|
|
2022-09-13 21:28:04 +08:00
|
|
|
# Do not reinstall existing pip packages on Debian/Ubuntu
|
|
|
|
export PIP_IGNORE_INSTALLED=0
|
|
|
|
|
|
|
|
# Pretty print
|
|
|
|
delimiter="################################################################"
|
|
|
|
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "\e[1m\e[32mInstall script for stable-diffusion + Web UI\n"
|
2023-11-11 01:15:34 +08:00
|
|
|
printf "\e[1m\e[34mTested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.\e[0m"
|
2022-09-13 21:28:04 +08:00
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
|
|
|
|
# Do not run as root
|
2022-10-08 03:25:01 +08:00
|
|
|
if [[ $(id -u) -eq 0 && can_run_as_root -eq 0 ]]
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "\e[1m\e[31mERROR: This script must not be launched as root, aborting...\e[0m"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Running on \e[1m\e[32m%s\e[0m user" "$(whoami)"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
fi
|
|
|
|
|
2023-05-18 12:35:24 +08:00
|
|
|
if [[ $(getconf LONG_BIT) = 32 ]]
|
|
|
|
then
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "\e[1m\e[31mERROR: Unsupported Running on a 32bit OS\e[0m"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-04-18 12:25:32 +08:00
|
|
|
if [[ -d "$SCRIPT_DIR/.git" ]]
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Repo already cloned, using it as install directory"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
2024-04-18 12:25:32 +08:00
|
|
|
install_dir="${SCRIPT_DIR}/../"
|
|
|
|
clone_dir="${SCRIPT_DIR##*/}"
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
|
|
|
|
2022-10-15 02:50:21 +08:00
|
|
|
# Check prerequisites
|
2023-06-03 03:01:30 +08:00
|
|
|
gpu_info=$(lspci 2>/dev/null | grep -E "VGA|Display")
|
2023-01-20 06:42:12 +08:00
|
|
|
case "$gpu_info" in
|
2023-06-06 21:43:32 +08:00
|
|
|
*"Navi 1"*)
|
|
|
|
export HSA_OVERRIDE_GFX_VERSION=10.3.0
|
|
|
|
if [[ -z "${TORCH_COMMAND}" ]]
|
2023-06-06 07:05:31 +08:00
|
|
|
then
|
2024-03-30 12:33:39 +08:00
|
|
|
pyv="$(${python_cmd} -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]:02d}")')"
|
2024-03-13 07:54:32 +08:00
|
|
|
# Using an old nightly compiled against rocm 5.2 for Navi1, see https://github.com/pytorch/pytorch/issues/106728#issuecomment-1749511711
|
|
|
|
if [[ $pyv == "3.8" ]]
|
2023-06-06 21:43:32 +08:00
|
|
|
then
|
2024-03-13 07:54:32 +08:00
|
|
|
export TORCH_COMMAND="pip install https://download.pytorch.org/whl/nightly/rocm5.2/torch-2.0.0.dev20230209%2Brocm5.2-cp38-cp38-linux_x86_64.whl https://download.pytorch.org/whl/nightly/rocm5.2/torchvision-0.15.0.dev20230209%2Brocm5.2-cp38-cp38-linux_x86_64.whl"
|
|
|
|
elif [[ $pyv == "3.9" ]]
|
|
|
|
then
|
|
|
|
export TORCH_COMMAND="pip install https://download.pytorch.org/whl/nightly/rocm5.2/torch-2.0.0.dev20230209%2Brocm5.2-cp39-cp39-linux_x86_64.whl https://download.pytorch.org/whl/nightly/rocm5.2/torchvision-0.15.0.dev20230209%2Brocm5.2-cp39-cp39-linux_x86_64.whl"
|
|
|
|
elif [[ $pyv == "3.10" ]]
|
|
|
|
then
|
|
|
|
export TORCH_COMMAND="pip install https://download.pytorch.org/whl/nightly/rocm5.2/torch-2.0.0.dev20230209%2Brocm5.2-cp310-cp310-linux_x86_64.whl https://download.pytorch.org/whl/nightly/rocm5.2/torchvision-0.15.0.dev20230209%2Brocm5.2-cp310-cp310-linux_x86_64.whl"
|
2023-06-06 21:43:32 +08:00
|
|
|
else
|
2024-03-13 07:54:32 +08:00
|
|
|
printf "\e[1m\e[31mERROR: RX 5000 series GPUs python version must be between 3.8 and 3.10, aborting...\e[0m"
|
2023-06-06 21:43:32 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2023-06-06 07:05:31 +08:00
|
|
|
fi
|
2023-01-20 06:42:12 +08:00
|
|
|
;;
|
2023-06-06 15:55:49 +08:00
|
|
|
*"Navi 2"*) export HSA_OVERRIDE_GFX_VERSION=10.3.0
|
|
|
|
;;
|
2023-06-15 04:07:22 +08:00
|
|
|
*"Navi 3"*) [[ -z "${TORCH_COMMAND}" ]] && \
|
2024-03-13 07:54:32 +08:00
|
|
|
export TORCH_COMMAND="pip install torch torchvision --index-url https://download.pytorch.org/whl/nightly/rocm5.7"
|
2023-06-15 04:07:22 +08:00
|
|
|
;;
|
2023-06-06 15:55:49 +08:00
|
|
|
*"Renoir"*) export HSA_OVERRIDE_GFX_VERSION=9.0.0
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Experimental support for Renoir: make sure to have at least 4GB of VRAM and 10GB of RAM or enable cpu mode: --use-cpu all --no-half"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
2023-01-20 06:42:12 +08:00
|
|
|
;;
|
2023-04-13 07:46:59 +08:00
|
|
|
*)
|
2023-01-20 06:42:12 +08:00
|
|
|
;;
|
|
|
|
esac
|
2023-05-26 05:45:05 +08:00
|
|
|
if ! echo "$gpu_info" | grep -q "NVIDIA";
|
2023-01-20 02:23:40 +08:00
|
|
|
then
|
2023-05-26 05:45:05 +08:00
|
|
|
if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]]
|
|
|
|
then
|
2024-03-06 20:44:55 +08:00
|
|
|
export TORCH_COMMAND="pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7"
|
2024-03-04 17:19:37 +08:00
|
|
|
elif npu-smi info 2>/dev/null
|
2024-01-27 17:21:32 +08:00
|
|
|
then
|
2024-02-21 11:49:06 +08:00
|
|
|
export TORCH_COMMAND="pip install torch==2.1.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu; pip install torch_npu==2.1.0"
|
2023-05-26 05:45:05 +08:00
|
|
|
fi
|
2023-05-18 12:35:24 +08:00
|
|
|
fi
|
2023-01-20 02:21:02 +08:00
|
|
|
|
2022-10-15 02:50:21 +08:00
|
|
|
for preq in "${GIT}" "${python_cmd}"
|
2022-09-13 21:28:04 +08:00
|
|
|
do
|
|
|
|
if ! hash "${preq}" &>/dev/null
|
|
|
|
then
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "\e[1m\e[31mERROR: %s is not installed, aborting...\e[0m" "${preq}"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2023-07-19 06:48:21 +08:00
|
|
|
if [[ $use_venv -eq 1 ]] && ! "${python_cmd}" -c "import venv" &>/dev/null
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "\e[1m\e[31mERROR: python3-venv is not installed, aborting...\e[0m"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-03-13 01:50:02 +08:00
|
|
|
cd "${install_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/, aborting...\e[0m" "${install_dir}"; exit 1; }
|
|
|
|
if [[ -d "${clone_dir}" ]]
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
2023-03-13 01:50:02 +08:00
|
|
|
cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
|
|
|
|
else
|
2022-10-24 22:13:25 +08:00
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Clone stable-diffusion-webui"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
2023-03-13 01:50:02 +08:00
|
|
|
"${GIT}" clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git "${clone_dir}"
|
|
|
|
cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
|
2022-09-13 21:28:04 +08:00
|
|
|
fi
|
|
|
|
|
2023-07-19 06:48:21 +08:00
|
|
|
if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
|
2022-09-13 21:28:04 +08:00
|
|
|
then
|
2023-03-29 17:35:37 +08:00
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Create and activate python venv"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
cd "${install_dir}"/"${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
|
|
|
|
if [[ ! -d "${venv_dir}" ]]
|
|
|
|
then
|
|
|
|
"${python_cmd}" -m venv "${venv_dir}"
|
2024-05-10 05:16:53 +08:00
|
|
|
"${venv_dir}"/bin/python -m pip install --upgrade pip
|
2023-03-29 17:35:37 +08:00
|
|
|
first_launch=1
|
|
|
|
fi
|
|
|
|
# shellcheck source=/dev/null
|
|
|
|
if [[ -f "${venv_dir}"/bin/activate ]]
|
|
|
|
then
|
|
|
|
source "${venv_dir}"/bin/activate
|
2024-06-30 17:44:06 +08:00
|
|
|
# ensure use of python from venv
|
|
|
|
python_cmd="${venv_dir}"/bin/python
|
2023-03-29 17:35:37 +08:00
|
|
|
else
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "\e[1m\e[31mERROR: Cannot activate python venv, aborting...\e[0m"
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-09-13 21:28:04 +08:00
|
|
|
else
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
2023-07-19 03:43:18 +08:00
|
|
|
printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
|
2022-09-13 21:28:04 +08:00
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
fi
|
|
|
|
|
2023-04-13 07:46:59 +08:00
|
|
|
# Try using TCMalloc on Linux
|
|
|
|
prepare_tcmalloc() {
|
|
|
|
if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${NO_TCMALLOC}" ]] && [[ -z "${LD_PRELOAD}" ]]; then
|
2024-02-10 09:09:19 +08:00
|
|
|
# check glibc version
|
2024-02-13 20:49:23 +08:00
|
|
|
LIBC_VER=$(echo $(ldd --version | awk 'NR==1 {print $NF}') | grep -oP '\d+\.\d+')
|
2024-02-10 09:09:19 +08:00
|
|
|
echo "glibc version is $LIBC_VER"
|
|
|
|
libc_vernum=$(expr $LIBC_VER)
|
|
|
|
# Since 2.34 libpthread is integrated into libc.so
|
|
|
|
libc_v234=2.34
|
2023-12-06 20:23:56 +08:00
|
|
|
# Define Tcmalloc Libs arrays
|
|
|
|
TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
|
|
|
|
# Traversal array
|
|
|
|
for lib in "${TCMALLOC_LIBS[@]}"
|
|
|
|
do
|
2024-02-10 09:09:19 +08:00
|
|
|
# Determine which type of tcmalloc library the library supports
|
2024-04-17 11:01:56 +08:00
|
|
|
TCMALLOC="$(PATH=/sbin:/usr/sbin:$PATH ldconfig -p | grep -P $lib | head -n 1)"
|
2024-02-10 09:09:19 +08:00
|
|
|
TC_INFO=(${TCMALLOC//=>/})
|
|
|
|
if [[ ! -z "${TC_INFO}" ]]; then
|
|
|
|
echo "Check TCMalloc: ${TC_INFO}"
|
2024-02-13 20:49:23 +08:00
|
|
|
# Determine if the library is linked to libpthread and resolve undefined symbol: pthread_key_create
|
2024-02-10 09:09:19 +08:00
|
|
|
if [ $(echo "$libc_vernum < $libc_v234" | bc) -eq 1 ]; then
|
2024-02-13 20:49:23 +08:00
|
|
|
# glibc < 2.34 pthread_key_create into libpthread.so. check linking libpthread.so...
|
2024-02-10 09:09:19 +08:00
|
|
|
if ldd ${TC_INFO[2]} | grep -q 'libpthread'; then
|
|
|
|
echo "$TC_INFO is linked with libpthread,execute LD_PRELOAD=${TC_INFO[2]}"
|
|
|
|
# set fullpath LD_PRELOAD (To be on the safe side)
|
|
|
|
export LD_PRELOAD="${TC_INFO[2]}"
|
|
|
|
break
|
|
|
|
else
|
|
|
|
echo "$TC_INFO is not linked with libpthread will trigger undefined symbol: pthread_Key_create error"
|
|
|
|
fi
|
|
|
|
else
|
2024-02-13 20:49:23 +08:00
|
|
|
# Version 2.34 of libc.so (glibc) includes the pthread library IN GLIBC. (USE ubuntu 22.04 and modern linux system and WSL)
|
2024-02-10 09:09:19 +08:00
|
|
|
# libc.so(glibc) is linked with a library that works in ALMOST ALL Linux userlands. SO NO CHECK!
|
|
|
|
echo "$TC_INFO is linked with libc.so,execute LD_PRELOAD=${TC_INFO[2]}"
|
|
|
|
# set fullpath LD_PRELOAD (To be on the safe side)
|
|
|
|
export LD_PRELOAD="${TC_INFO[2]}"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
2023-12-06 20:23:56 +08:00
|
|
|
done
|
2024-02-10 09:09:19 +08:00
|
|
|
if [[ -z "${LD_PRELOAD}" ]]; then
|
2024-02-13 20:49:23 +08:00
|
|
|
printf "\e[1m\e[31mCannot locate TCMalloc. Do you have tcmalloc or google-perftool installed on your system? (improves CPU memory usage)\e[0m\n"
|
2023-04-13 07:46:59 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-06-03 14:59:56 +08:00
|
|
|
KEEP_GOING=1
|
2023-06-06 01:04:28 +08:00
|
|
|
export SD_WEBUI_RESTART=tmp/restart
|
2023-06-03 14:59:56 +08:00
|
|
|
while [[ "$KEEP_GOING" -eq "1" ]]; do
|
|
|
|
if [[ ! -z "${ACCELERATE}" ]] && [ ${ACCELERATE}="True" ] && [ -x "$(command -v accelerate)" ]; then
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Accelerating launch.py..."
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
prepare_tcmalloc
|
|
|
|
accelerate launch --num_cpu_threads_per_process=6 "${LAUNCH_SCRIPT}" "$@"
|
|
|
|
else
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
printf "Launching launch.py..."
|
|
|
|
printf "\n%s\n" "${delimiter}"
|
|
|
|
prepare_tcmalloc
|
2023-08-19 08:57:11 +08:00
|
|
|
"${python_cmd}" -u "${LAUNCH_SCRIPT}" "$@"
|
2023-06-03 14:59:56 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f tmp/restart ]]; then
|
|
|
|
KEEP_GOING=0
|
|
|
|
fi
|
|
|
|
done
|