soinov commited on
Commit
5b76bc3
·
verified ·
1 Parent(s): bbda1ee

Upload webui.sh with huggingface_hub

Browse files
Files changed (1) hide show
  1. webui.sh +297 -0
webui.sh ADDED
@@ -0,0 +1,297 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 "$SCRIPT_DIR/.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="${SCRIPT_DIR}/../"
122
+ clone_dir="${SCRIPT_DIR##*/}"
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(f"{sys.version_info[0]}.{sys.version_info[1]:02d}")')"
133
+ # Using an old nightly compiled against rocm 5.2 for Navi1, see https://github.com/pytorch/pytorch/issues/106728#issuecomment-1749511711
134
+ if [[ $pyv == "3.8" ]]
135
+ then
136
+ 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"
137
+ elif [[ $pyv == "3.9" ]]
138
+ then
139
+ 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"
140
+ elif [[ $pyv == "3.10" ]]
141
+ then
142
+ 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"
143
+ else
144
+ printf "\e[1m\e[31mERROR: RX 5000 series GPUs python version must be between 3.8 and 3.10, aborting...\e[0m"
145
+ exit 1
146
+ fi
147
+ fi
148
+ ;;
149
+ *"Navi 2"*) export HSA_OVERRIDE_GFX_VERSION=10.3.0
150
+ ;;
151
+ *"Navi 3"*) [[ -z "${TORCH_COMMAND}" ]] && \
152
+ export TORCH_COMMAND="pip install torch torchvision --index-url https://download.pytorch.org/whl/nightly/rocm5.7"
153
+ ;;
154
+ *"Renoir"*) export HSA_OVERRIDE_GFX_VERSION=9.0.0
155
+ printf "\n%s\n" "${delimiter}"
156
+ 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"
157
+ printf "\n%s\n" "${delimiter}"
158
+ ;;
159
+ *)
160
+ ;;
161
+ esac
162
+ if ! echo "$gpu_info" | grep -q "NVIDIA";
163
+ then
164
+ if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]]
165
+ then
166
+ export TORCH_COMMAND="pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7"
167
+ elif npu-smi info 2>/dev/null
168
+ then
169
+ 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"
170
+ fi
171
+ fi
172
+
173
+ for preq in "${GIT}" "${python_cmd}"
174
+ do
175
+ if ! hash "${preq}" &>/dev/null
176
+ then
177
+ printf "\n%s\n" "${delimiter}"
178
+ printf "\e[1m\e[31mERROR: %s is not installed, aborting...\e[0m" "${preq}"
179
+ printf "\n%s\n" "${delimiter}"
180
+ exit 1
181
+ fi
182
+ done
183
+
184
+ if [[ $use_venv -eq 1 ]] && ! "${python_cmd}" -c "import venv" &>/dev/null
185
+ then
186
+ printf "\n%s\n" "${delimiter}"
187
+ printf "\e[1m\e[31mERROR: python3-venv is not installed, aborting...\e[0m"
188
+ printf "\n%s\n" "${delimiter}"
189
+ exit 1
190
+ fi
191
+
192
+ cd "${install_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/, aborting...\e[0m" "${install_dir}"; exit 1; }
193
+ if [[ -d "${clone_dir}" ]]
194
+ then
195
+ cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
196
+ else
197
+ printf "\n%s\n" "${delimiter}"
198
+ printf "Clone stable-diffusion-webui"
199
+ printf "\n%s\n" "${delimiter}"
200
+ "${GIT}" clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git "${clone_dir}"
201
+ cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
202
+ fi
203
+
204
+ if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
205
+ then
206
+ printf "\n%s\n" "${delimiter}"
207
+ printf "Create and activate python venv"
208
+ printf "\n%s\n" "${delimiter}"
209
+ 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; }
210
+ if [[ ! -d "${venv_dir}" ]]
211
+ then
212
+ "${python_cmd}" -m venv "${venv_dir}"
213
+ first_launch=1
214
+ fi
215
+ # shellcheck source=/dev/null
216
+ if [[ -f "${venv_dir}"/bin/activate ]]
217
+ then
218
+ source "${venv_dir}"/bin/activate
219
+ else
220
+ printf "\n%s\n" "${delimiter}"
221
+ printf "\e[1m\e[31mERROR: Cannot activate python venv, aborting...\e[0m"
222
+ printf "\n%s\n" "${delimiter}"
223
+ exit 1
224
+ fi
225
+ else
226
+ printf "\n%s\n" "${delimiter}"
227
+ printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
228
+ printf "\n%s\n" "${delimiter}"
229
+ fi
230
+
231
+ # Try using TCMalloc on Linux
232
+ prepare_tcmalloc() {
233
+ if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${NO_TCMALLOC}" ]] && [[ -z "${LD_PRELOAD}" ]]; then
234
+ # check glibc version
235
+ LIBC_VER=$(echo $(ldd --version | awk 'NR==1 {print $NF}') | grep -oP '\d+\.\d+')
236
+ echo "glibc version is $LIBC_VER"
237
+ libc_vernum=$(expr $LIBC_VER)
238
+ # Since 2.34 libpthread is integrated into libc.so
239
+ libc_v234=2.34
240
+ # Define Tcmalloc Libs arrays
241
+ TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
242
+ # Traversal array
243
+ for lib in "${TCMALLOC_LIBS[@]}"
244
+ do
245
+ # Determine which type of tcmalloc library the library supports
246
+ TCMALLOC="$(PATH=/sbin:/usr/sbin:$PATH ldconfig -p | grep -P $lib | head -n 1)"
247
+ TC_INFO=(${TCMALLOC//=>/})
248
+ if [[ ! -z "${TC_INFO}" ]]; then
249
+ echo "Check TCMalloc: ${TC_INFO}"
250
+ # Determine if the library is linked to libpthread and resolve undefined symbol: pthread_key_create
251
+ if [ $(echo "$libc_vernum < $libc_v234" | bc) -eq 1 ]; then
252
+ # glibc < 2.34 pthread_key_create into libpthread.so. check linking libpthread.so...
253
+ if ldd ${TC_INFO[2]} | grep -q 'libpthread'; then
254
+ echo "$TC_INFO is linked with libpthread,execute LD_PRELOAD=${TC_INFO[2]}"
255
+ # set fullpath LD_PRELOAD (To be on the safe side)
256
+ export LD_PRELOAD="${TC_INFO[2]}"
257
+ break
258
+ else
259
+ echo "$TC_INFO is not linked with libpthread will trigger undefined symbol: pthread_Key_create error"
260
+ fi
261
+ else
262
+ # Version 2.34 of libc.so (glibc) includes the pthread library IN GLIBC. (USE ubuntu 22.04 and modern linux system and WSL)
263
+ # libc.so(glibc) is linked with a library that works in ALMOST ALL Linux userlands. SO NO CHECK!
264
+ echo "$TC_INFO is linked with libc.so,execute LD_PRELOAD=${TC_INFO[2]}"
265
+ # set fullpath LD_PRELOAD (To be on the safe side)
266
+ export LD_PRELOAD="${TC_INFO[2]}"
267
+ break
268
+ fi
269
+ fi
270
+ done
271
+ if [[ -z "${LD_PRELOAD}" ]]; then
272
+ 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"
273
+ fi
274
+ fi
275
+ }
276
+
277
+ KEEP_GOING=1
278
+ export SD_WEBUI_RESTART=tmp/restart
279
+ while [[ "$KEEP_GOING" -eq "1" ]]; do
280
+ if [[ ! -z "${ACCELERATE}" ]] && [ ${ACCELERATE}="True" ] && [ -x "$(command -v accelerate)" ]; then
281
+ printf "\n%s\n" "${delimiter}"
282
+ printf "Accelerating launch.py..."
283
+ printf "\n%s\n" "${delimiter}"
284
+ prepare_tcmalloc
285
+ accelerate launch --num_cpu_threads_per_process=6 "${LAUNCH_SCRIPT}" "$@"
286
+ else
287
+ printf "\n%s\n" "${delimiter}"
288
+ printf "Launching launch.py..."
289
+ printf "\n%s\n" "${delimiter}"
290
+ prepare_tcmalloc
291
+ "${python_cmd}" -u "${LAUNCH_SCRIPT}" "$@"
292
+ fi
293
+
294
+ if [[ ! -f tmp/restart ]]; then
295
+ KEEP_GOING=0
296
+ fi
297
+ done