2022-12-12 13:18:25 -07:00
#!/usr/bin/env bash
2022-09-13 07:28:04 -06:00
#################################################
# Please do not make any changes to this file, #
# change the variables in webui-user.sh instead #
#################################################
2022-10-07 13:25:01 -06:00
2023-07-18 22:59:39 -06:00
2023-07-18 16:48:21 -06:00
use_venv = 1
2023-07-18 16:47:39 -06:00
if [ [ $venv_dir = = "-" ] ] ; then
2023-07-18 16:48:21 -06:00
use_venv = 0
2023-07-18 13:43:18 -06:00
fi
2023-07-03 01:15:19 -06:00
SCRIPT_DIR = $( cd -- " $( dirname -- " ${ BASH_SOURCE [0] } " ) " & > /dev/null && pwd )
2023-07-18 22:59:39 -06:00
2022-12-03 00:28:53 -07:00
# If run from macOS, load defaults from webui-macos-env.sh
if [ [ " $OSTYPE " = = "darwin" * ] ] ; then
2023-07-03 01:15:19 -06:00
if [ [ -f " $SCRIPT_DIR " /webui-macos-env.sh ] ]
2022-12-03 00:28:53 -07:00
then
2023-07-03 01:15:19 -06:00
source " $SCRIPT_DIR " /webui-macos-env.sh
2022-12-03 00:28:53 -07:00
fi
fi
2022-09-13 07:28:04 -06:00
# Read variables from webui-user.sh
# shellcheck source=/dev/null
2023-07-03 01:15:19 -06:00
if [ [ -f " $SCRIPT_DIR " /webui-user.sh ] ]
2022-09-13 07:28:04 -06:00
then
2023-07-03 01:15:19 -06:00
source " $SCRIPT_DIR " /webui-user.sh
2022-09-13 07:28:04 -06:00
fi
2023-03-12 11:50:02 -06:00
2022-09-13 07:28:04 -06:00
# Set defaults
# Install directory without trailing slash
if [ [ -z " ${ install_dir } " ] ]
then
2023-07-03 01:17:27 -06:00
install_dir = " $SCRIPT_DIR "
2022-09-13 07:28:04 -06: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
python_cmd = "python3"
fi
# git executable
2022-09-13 09:28:54 -06:00
if [ [ -z " ${ GIT } " ] ]
2022-09-13 07:28:04 -06:00
then
2022-09-13 09:28:54 -06:00
export GIT = "git"
2022-09-13 07:28:04 -06:00
fi
# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
2023-07-18 16:48:21 -06:00
if [ [ -z " ${ venv_dir } " ] ] && [ [ $use_venv -eq 1 ] ]
2022-09-13 07:28:04 -06:00
then
2023-03-12 11:50:02 -06:00
venv_dir = "venv"
2022-09-13 07:28:04 -06:00
fi
2022-09-30 03:44:08 -06:00
if [ [ -z " ${ LAUNCH_SCRIPT } " ] ]
2022-09-30 02:51:05 -06:00
then
2023-03-12 11:50:02 -06:00
LAUNCH_SCRIPT = "launch.py"
2022-09-30 02:51:05 -06:00
fi
2022-10-07 13:25:01 -06: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 02:04:14 -07:00
while getopts "f" flag > /dev/null 2>& 1
2022-10-07 13:25:01 -06:00
do
case ${ flag } in
f) can_run_as_root = 1; ;
2022-12-01 02:04:14 -07:00
*) break; ;
2022-10-07 13:25:01 -06:00
esac
done
2022-09-26 04:55:10 -06:00
# Disable sentry logging
export ERROR_REPORTING = FALSE
2022-09-13 07:28:04 -06: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"
printf "\e[1m\e[34mTested on Debian 11 (Bullseye)\e[0m"
printf "\n%s\n" " ${ delimiter } "
# Do not run as root
2022-10-07 13:25:01 -06:00
if [ [ $( id -u) -eq 0 && can_run_as_root -eq 0 ] ]
2022-09-13 07:28:04 -06: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-17 22:35:24 -06: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
2022-09-13 07:28:04 -06:00
if [ [ -d .git ] ]
then
printf "\n%s\n" " ${ delimiter } "
printf "Repo already cloned, using it as install directory"
printf "\n%s\n" " ${ delimiter } "
install_dir = " ${ PWD } /../ "
clone_dir = " ${ PWD ##*/ } "
fi
2022-10-14 12:50:21 -06:00
# Check prerequisites
2023-06-02 13:01:30 -06:00
gpu_info = $( lspci 2>/dev/null | grep -E "VGA|Display" )
2023-01-19 15:42:12 -07:00
case " $gpu_info " in
2023-06-06 07:43:32 -06:00
*"Navi 1" *)
export HSA_OVERRIDE_GFX_VERSION = 10.3.0
if [ [ -z " ${ TORCH_COMMAND } " ] ]
2023-06-05 17:05:31 -06:00
then
2023-06-06 07:43:32 -06:00
pyv = " $( ${ python_cmd } -c 'import sys; print(".".join(map(str, sys.version_info[0:2])))' ) "
if [ [ $( bc <<< " $pyv <= 3.10 " ) -eq 1 ] ]
then
# Navi users will still use torch 1.13 because 2.0 does not seem to work.
export TORCH_COMMAND = "pip install torch==1.13.1+rocm5.2 torchvision==0.14.1+rocm5.2 --index-url https://download.pytorch.org/whl/rocm5.2"
else
printf "\e[1m\e[31mERROR: RX 5000 series GPUs must be using at max python 3.10, aborting...\e[0m"
exit 1
fi
2023-06-05 17:05:31 -06:00
fi
2023-01-19 15:42:12 -07:00
; ;
2023-06-06 01:55:49 -06:00
*"Navi 2" *) export HSA_OVERRIDE_GFX_VERSION = 10.3.0
; ;
2023-06-14 14:07:22 -06:00
*"Navi 3" *) [ [ -z " ${ TORCH_COMMAND } " ] ] && \
2023-08-20 14:59:30 -06:00
export TORCH_COMMAND = "pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/rocm5.6"
# Navi 3 needs at least 5.5 which is only on the nightly chain, previous versions are no longer online (torch==2.1.0.dev-20230614+rocm5.5 torchvision==0.16.0.dev-20230614+rocm5.5 torchaudio==2.1.0.dev-20230614+rocm5.5)
# so switch to nightly rocm5.6 without explicit versions this time
2023-06-14 14:07:22 -06:00
; ;
2023-06-06 01:55:49 -06: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-19 15:42:12 -07:00
; ;
2023-04-12 17:46:59 -06:00
*)
2023-01-19 15:42:12 -07:00
; ;
esac
2023-05-25 15:45:05 -06:00
if ! echo " $gpu_info " | grep -q "NVIDIA" ;
2023-01-19 11:23:40 -07:00
then
2023-05-25 15:45:05 -06:00
if echo " $gpu_info " | grep -q "AMD" && [ [ -z " ${ TORCH_COMMAND } " ] ]
then
2023-05-26 03:09:09 -06:00
export TORCH_COMMAND = "pip install torch==2.0.1+rocm5.4.2 torchvision==0.15.2+rocm5.4.2 --index-url https://download.pytorch.org/whl/rocm5.4.2"
2023-05-25 15:45:05 -06:00
fi
2023-05-17 22:35:24 -06:00
fi
2023-01-19 11:21:02 -07:00
2022-10-14 12:50:21 -06:00
for preq in " ${ GIT } " " ${ python_cmd } "
2022-09-13 07:28:04 -06: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-18 16:48:21 -06:00
if [ [ $use_venv -eq 1 ] ] && ! " ${ python_cmd } " -c "import venv" & >/dev/null
2022-09-13 07:28:04 -06: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-12 11:50:02 -06: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 07:28:04 -06:00
then
2023-03-12 11:50:02 -06: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 08:13:25 -06:00
printf "\n%s\n" " ${ delimiter } "
printf "Clone stable-diffusion-webui"
printf "\n%s\n" " ${ delimiter } "
2023-03-12 11:50:02 -06: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 07:28:04 -06:00
fi
2023-07-18 16:48:21 -06:00
if [ [ $use_venv -eq 1 ] ] && [ [ -z " ${ VIRTUAL_ENV } " ] ] ;
2022-09-13 07:28:04 -06:00
then
2023-03-29 03:35:37 -06: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 } "
first_launch = 1
fi
# shellcheck source=/dev/null
if [ [ -f " ${ venv_dir } " /bin/activate ] ]
then
source " ${ venv_dir } " /bin/activate
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 07:28:04 -06:00
else
printf "\n%s\n" " ${ delimiter } "
2023-07-18 13:43:18 -06:00
printf " python venv already activate or run without venv: ${ VIRTUAL_ENV } "
2022-09-13 07:28:04 -06:00
printf "\n%s\n" " ${ delimiter } "
fi
2023-04-12 17:46:59 -06:00
# Try using TCMalloc on Linux
prepare_tcmalloc( ) {
if [ [ " ${ OSTYPE } " = = "linux" * ] ] && [ [ -z " ${ NO_TCMALLOC } " ] ] && [ [ -z " ${ LD_PRELOAD } " ] ] ; then
2023-05-25 12:51:47 -06:00
TCMALLOC = " $( PATH = /usr/sbin:$PATH ldconfig -p | grep -Po "libtcmalloc(_minimal|)\.so\.\d" | head -n 1) "
2023-04-12 17:46:59 -06:00
if [ [ ! -z " ${ TCMALLOC } " ] ] ; then
echo " Using TCMalloc: ${ TCMALLOC } "
export LD_PRELOAD = " ${ TCMALLOC } "
else
printf "\e[1m\e[31mCannot locate TCMalloc (improves CPU memory usage)\e[0m\n"
fi
fi
}
2023-06-03 00:59:56 -06:00
KEEP_GOING = 1
2023-06-05 11:04:28 -06:00
export SD_WEBUI_RESTART = tmp/restart
2023-06-03 00:59:56 -06: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-18 18:57:11 -06:00
" ${ python_cmd } " -u " ${ LAUNCH_SCRIPT } " " $@ "
2023-06-03 00:59:56 -06:00
fi
if [ [ ! -f tmp/restart ] ] ; then
KEEP_GOING = 0
fi
done