Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AUTOMATIC1111
GitHub Repository: AUTOMATIC1111/stable-diffusion-webui
Path: blob/master/webui.sh
2305 views
1
#!/usr/bin/env bash
2
#################################################
3
# Please do not make any changes to this file, #
4
# change the variables in webui-user.sh instead #
5
#################################################
6
7
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
8
9
10
# If run from macOS, load defaults from webui-macos-env.sh
11
if [[ "$OSTYPE" == "darwin"* ]]; then
12
if [[ -f "$SCRIPT_DIR"/webui-macos-env.sh ]]
13
then
14
source "$SCRIPT_DIR"/webui-macos-env.sh
15
fi
16
fi
17
18
# Read variables from webui-user.sh
19
# shellcheck source=/dev/null
20
if [[ -f "$SCRIPT_DIR"/webui-user.sh ]]
21
then
22
source "$SCRIPT_DIR"/webui-user.sh
23
fi
24
25
# If $venv_dir is "-", then disable venv support
26
use_venv=1
27
if [[ $venv_dir == "-" ]]; then
28
use_venv=0
29
fi
30
31
# Set defaults
32
# Install directory without trailing slash
33
if [[ -z "${install_dir}" ]]
34
then
35
install_dir="$SCRIPT_DIR"
36
fi
37
38
# Name of the subdirectory (defaults to stable-diffusion-webui)
39
if [[ -z "${clone_dir}" ]]
40
then
41
clone_dir="stable-diffusion-webui"
42
fi
43
44
# python3 executable
45
if [[ -z "${python_cmd}" ]]
46
then
47
python_cmd="python3.10"
48
fi
49
if [[ ! -x "$(command -v "${python_cmd}")" ]]
50
then
51
python_cmd="python3"
52
fi
53
54
# git executable
55
if [[ -z "${GIT}" ]]
56
then
57
export GIT="git"
58
else
59
export GIT_PYTHON_GIT_EXECUTABLE="${GIT}"
60
fi
61
62
# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
63
if [[ -z "${venv_dir}" ]] && [[ $use_venv -eq 1 ]]
64
then
65
venv_dir="venv"
66
fi
67
68
if [[ -z "${LAUNCH_SCRIPT}" ]]
69
then
70
LAUNCH_SCRIPT="launch.py"
71
fi
72
73
# this script cannot be run as root by default
74
can_run_as_root=0
75
76
# read any command line flags to the webui.sh script
77
while getopts "f" flag > /dev/null 2>&1
78
do
79
case ${flag} in
80
f) can_run_as_root=1;;
81
*) break;;
82
esac
83
done
84
85
# Disable sentry logging
86
export ERROR_REPORTING=FALSE
87
88
# Do not reinstall existing pip packages on Debian/Ubuntu
89
export PIP_IGNORE_INSTALLED=0
90
91
# Pretty print
92
delimiter="################################################################"
93
94
printf "\n%s\n" "${delimiter}"
95
printf "\e[1m\e[32mInstall script for stable-diffusion + Web UI\n"
96
printf "\e[1m\e[34mTested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.\e[0m"
97
printf "\n%s\n" "${delimiter}"
98
99
# Do not run as root
100
if [[ $(id -u) -eq 0 && can_run_as_root -eq 0 ]]
101
then
102
printf "\n%s\n" "${delimiter}"
103
printf "\e[1m\e[31mERROR: This script must not be launched as root, aborting...\e[0m"
104
printf "\n%s\n" "${delimiter}"
105
exit 1
106
else
107
printf "\n%s\n" "${delimiter}"
108
printf "Running on \e[1m\e[32m%s\e[0m user" "$(whoami)"
109
printf "\n%s\n" "${delimiter}"
110
fi
111
112
if [[ $(getconf LONG_BIT) = 32 ]]
113
then
114
printf "\n%s\n" "${delimiter}"
115
printf "\e[1m\e[31mERROR: Unsupported Running on a 32bit OS\e[0m"
116
printf "\n%s\n" "${delimiter}"
117
exit 1
118
fi
119
120
if [[ -d "$SCRIPT_DIR/.git" ]]
121
then
122
printf "\n%s\n" "${delimiter}"
123
printf "Repo already cloned, using it as install directory"
124
printf "\n%s\n" "${delimiter}"
125
install_dir="${SCRIPT_DIR}/../"
126
clone_dir="${SCRIPT_DIR##*/}"
127
fi
128
129
# Check prerequisites
130
gpu_info=$(lspci 2>/dev/null | grep -E "VGA|Display")
131
case "$gpu_info" in
132
*"Navi 1"*)
133
export HSA_OVERRIDE_GFX_VERSION=10.3.0
134
if [[ -z "${TORCH_COMMAND}" ]]
135
then
136
pyv="$(${python_cmd} -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]:02d}")')"
137
# Using an old nightly compiled against rocm 5.2 for Navi1, see https://github.com/pytorch/pytorch/issues/106728#issuecomment-1749511711
138
if [[ $pyv == "3.8" ]]
139
then
140
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"
141
elif [[ $pyv == "3.9" ]]
142
then
143
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"
144
elif [[ $pyv == "3.10" ]]
145
then
146
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"
147
else
148
printf "\e[1m\e[31mERROR: RX 5000 series GPUs python version must be between 3.8 and 3.10, aborting...\e[0m"
149
exit 1
150
fi
151
fi
152
;;
153
*"Navi 2"*) export HSA_OVERRIDE_GFX_VERSION=10.3.0
154
;;
155
*"Navi 3"*) [[ -z "${TORCH_COMMAND}" ]] && \
156
export TORCH_COMMAND="pip install torch torchvision --index-url https://download.pytorch.org/whl/nightly/rocm5.7"
157
;;
158
*"Renoir"*) export HSA_OVERRIDE_GFX_VERSION=9.0.0
159
printf "\n%s\n" "${delimiter}"
160
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"
161
printf "\n%s\n" "${delimiter}"
162
;;
163
*)
164
;;
165
esac
166
if ! echo "$gpu_info" | grep -q "NVIDIA";
167
then
168
if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]]
169
then
170
export TORCH_COMMAND="pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7"
171
elif npu-smi info 2>/dev/null
172
then
173
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"
174
fi
175
fi
176
177
for preq in "${GIT}" "${python_cmd}"
178
do
179
if ! hash "${preq}" &>/dev/null
180
then
181
printf "\n%s\n" "${delimiter}"
182
printf "\e[1m\e[31mERROR: %s is not installed, aborting...\e[0m" "${preq}"
183
printf "\n%s\n" "${delimiter}"
184
exit 1
185
fi
186
done
187
188
if [[ $use_venv -eq 1 ]] && ! "${python_cmd}" -c "import venv" &>/dev/null
189
then
190
printf "\n%s\n" "${delimiter}"
191
printf "\e[1m\e[31mERROR: python3-venv is not installed, aborting...\e[0m"
192
printf "\n%s\n" "${delimiter}"
193
exit 1
194
fi
195
196
cd "${install_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/, aborting...\e[0m" "${install_dir}"; exit 1; }
197
if [[ -d "${clone_dir}" ]]
198
then
199
cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
200
else
201
printf "\n%s\n" "${delimiter}"
202
printf "Clone stable-diffusion-webui"
203
printf "\n%s\n" "${delimiter}"
204
"${GIT}" clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git "${clone_dir}"
205
cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
206
fi
207
208
if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
209
then
210
printf "\n%s\n" "${delimiter}"
211
printf "Create and activate python venv"
212
printf "\n%s\n" "${delimiter}"
213
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; }
214
if [[ ! -d "${venv_dir}" ]]
215
then
216
"${python_cmd}" -m venv "${venv_dir}"
217
"${venv_dir}"/bin/python -m pip install --upgrade pip
218
first_launch=1
219
fi
220
# shellcheck source=/dev/null
221
if [[ -f "${venv_dir}"/bin/activate ]]
222
then
223
source "${venv_dir}"/bin/activate
224
# ensure use of python from venv
225
python_cmd="${venv_dir}"/bin/python
226
else
227
printf "\n%s\n" "${delimiter}"
228
printf "\e[1m\e[31mERROR: Cannot activate python venv, aborting...\e[0m"
229
printf "\n%s\n" "${delimiter}"
230
exit 1
231
fi
232
else
233
printf "\n%s\n" "${delimiter}"
234
printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
235
printf "\n%s\n" "${delimiter}"
236
fi
237
238
# Try using TCMalloc on Linux
239
prepare_tcmalloc() {
240
if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${NO_TCMALLOC}" ]] && [[ -z "${LD_PRELOAD}" ]]; then
241
# check glibc version
242
LIBC_VER=$(echo $(ldd --version | awk 'NR==1 {print $NF}') | grep -oP '\d+\.\d+')
243
echo "glibc version is $LIBC_VER"
244
libc_vernum=$(expr $LIBC_VER)
245
# Since 2.34 libpthread is integrated into libc.so
246
libc_v234=2.34
247
# Define Tcmalloc Libs arrays
248
TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
249
# Traversal array
250
for lib in "${TCMALLOC_LIBS[@]}"
251
do
252
# Determine which type of tcmalloc library the library supports
253
TCMALLOC="$(PATH=/sbin:/usr/sbin:$PATH ldconfig -p | grep -P $lib | head -n 1)"
254
TC_INFO=(${TCMALLOC//=>/})
255
if [[ ! -z "${TC_INFO}" ]]; then
256
echo "Check TCMalloc: ${TC_INFO}"
257
# Determine if the library is linked to libpthread and resolve undefined symbol: pthread_key_create
258
if [ $(echo "$libc_vernum < $libc_v234" | bc) -eq 1 ]; then
259
# glibc < 2.34 pthread_key_create into libpthread.so. check linking libpthread.so...
260
if ldd ${TC_INFO[2]} | grep -q 'libpthread'; then
261
echo "$TC_INFO is linked with libpthread,execute LD_PRELOAD=${TC_INFO[2]}"
262
# set fullpath LD_PRELOAD (To be on the safe side)
263
export LD_PRELOAD="${TC_INFO[2]}"
264
break
265
else
266
echo "$TC_INFO is not linked with libpthread will trigger undefined symbol: pthread_Key_create error"
267
fi
268
else
269
# Version 2.34 of libc.so (glibc) includes the pthread library IN GLIBC. (USE ubuntu 22.04 and modern linux system and WSL)
270
# libc.so(glibc) is linked with a library that works in ALMOST ALL Linux userlands. SO NO CHECK!
271
echo "$TC_INFO is linked with libc.so,execute LD_PRELOAD=${TC_INFO[2]}"
272
# set fullpath LD_PRELOAD (To be on the safe side)
273
export LD_PRELOAD="${TC_INFO[2]}"
274
break
275
fi
276
fi
277
done
278
if [[ -z "${LD_PRELOAD}" ]]; then
279
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"
280
fi
281
fi
282
}
283
284
KEEP_GOING=1
285
export SD_WEBUI_RESTART=tmp/restart
286
while [[ "$KEEP_GOING" -eq "1" ]]; do
287
if [[ ! -z "${ACCELERATE}" ]] && [ ${ACCELERATE}="True" ] && [ -x "$(command -v accelerate)" ]; then
288
printf "\n%s\n" "${delimiter}"
289
printf "Accelerating launch.py..."
290
printf "\n%s\n" "${delimiter}"
291
prepare_tcmalloc
292
accelerate launch --num_cpu_threads_per_process=6 "${LAUNCH_SCRIPT}" "$@"
293
else
294
printf "\n%s\n" "${delimiter}"
295
printf "Launching launch.py..."
296
printf "\n%s\n" "${delimiter}"
297
prepare_tcmalloc
298
"${python_cmd}" -u "${LAUNCH_SCRIPT}" "$@"
299
fi
300
301
if [[ ! -f tmp/restart ]]; then
302
KEEP_GOING=0
303
fi
304
done
305
306