Yingqing commited on
Commit
59d4f92
·
verified ·
1 Parent(s): 94a8cf8

Upload webui.sh with huggingface_hub

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