webui.sh 10.7 KB
Newer Older
1
#!/usr/bin/env bash
O
orionaskatu 已提交
2 3 4 5
#################################################
# Please do not make any changes to this file,  #
# change the variables in webui-user.sh instead #
#################################################
6

7 8
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

9

10 11
# If run from macOS, load defaults from webui-macos-env.sh
if [[ "$OSTYPE" == "darwin"* ]]; then
12
    if [[ -f "$SCRIPT_DIR"/webui-macos-env.sh ]]
13
        then
14
        source "$SCRIPT_DIR"/webui-macos-env.sh
15 16 17
    fi
fi

O
orionaskatu 已提交
18 19
# Read variables from webui-user.sh
# shellcheck source=/dev/null
20
if [[ -f "$SCRIPT_DIR"/webui-user.sh ]]
O
orionaskatu 已提交
21
then
22
    source "$SCRIPT_DIR"/webui-user.sh
O
orionaskatu 已提交
23
fi
A
AUTOMATIC 已提交
24

25 26 27 28 29 30
# If $venv_dir is "-", then disable venv support
use_venv=1
if [[ $venv_dir == "-" ]]; then
  use_venv=0
fi

O
orionaskatu 已提交
31 32 33 34
# Set defaults
# Install directory without trailing slash
if [[ -z "${install_dir}" ]]
then
F
Frank Tao 已提交
35
    install_dir="$SCRIPT_DIR"
O
orionaskatu 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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
O
orionaskatu 已提交
51
if [[ -z "${GIT}" ]]
O
orionaskatu 已提交
52
then
O
orionaskatu 已提交
53
    export GIT="git"
54 55
else
    export GIT_PYTHON_GIT_EXECUTABLE="${GIT}"
O
orionaskatu 已提交
56 57 58
fi

# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
K
kopyl 已提交
59
if [[ -z "${venv_dir}" ]] && [[ $use_venv -eq 1 ]]
O
orionaskatu 已提交
60
then
A
AUTOMATIC 已提交
61
    venv_dir="venv"
O
orionaskatu 已提交
62 63
fi

64
if [[ -z "${LAUNCH_SCRIPT}" ]]
A
Andrew Savchyn 已提交
65
then
A
AUTOMATIC 已提交
66
    LAUNCH_SCRIPT="launch.py"
A
Andrew Savchyn 已提交
67 68
fi

69 70 71 72
# this script cannot be run as root by default
can_run_as_root=0

# read any command line flags to the webui.sh script
B
brkirch 已提交
73
while getopts "f" flag > /dev/null 2>&1
74 75 76
do
    case ${flag} in
        f) can_run_as_root=1;;
B
brkirch 已提交
77
        *) break;;
78 79 80
    esac
done

D
DepFA 已提交
81 82 83
# Disable sentry logging
export ERROR_REPORTING=FALSE

O
orionaskatu 已提交
84 85 86 87 88 89 90 91
# 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"
92
printf "\e[1m\e[34mTested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.\e[0m"
O
orionaskatu 已提交
93 94 95
printf "\n%s\n" "${delimiter}"

# Do not run as root
96
if [[ $(id -u) -eq 0 && can_run_as_root -eq 0 ]]
O
orionaskatu 已提交
97 98 99 100 101 102 103 104 105 106 107
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

W
Weiming 已提交
108 109 110 111 112 113 114 115
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

O
orionaskatu 已提交
116 117 118 119 120 121 122 123 124
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

D
Daniel M 已提交
125
# Check prerequisites
126
gpu_info=$(lspci 2>/dev/null | grep -E "VGA|Display")
D
DaniAndTheWeb 已提交
127
case "$gpu_info" in
128 129 130
    *"Navi 1"*)
        export HSA_OVERRIDE_GFX_VERSION=10.3.0
        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 134
            # 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" ]]
135
            then
136 137 138 139 140 141 142
                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"
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 146
                exit 1
            fi
147
        fi
D
DaniAndTheWeb 已提交
148
    ;;
149 150
    *"Navi 2"*) export HSA_OVERRIDE_GFX_VERSION=10.3.0
    ;;
B
Beinsezii 已提交
151
    *"Navi 3"*) [[ -z "${TORCH_COMMAND}" ]] && \
152
         export TORCH_COMMAND="pip install torch torchvision --index-url https://download.pytorch.org/whl/nightly/rocm5.7"
B
Beinsezii 已提交
153
    ;;
154 155 156 157
    *"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}"
D
DaniAndTheWeb 已提交
158
    ;;
G
gk 已提交
159
    *)
D
DaniAndTheWeb 已提交
160 161
    ;;
esac
162
if ! echo "$gpu_info" | grep -q "NVIDIA";
D
DaniAndTheWeb 已提交
163
then
164 165
    if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]]
    then
A
AUTOMATIC1111 已提交
166
	      export TORCH_COMMAND="pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.7"
W
wangshuai09 已提交
167
    elif npu-smi info 2>/dev/null
W
wangshuai09 已提交
168
    then
W
wangshuai09 已提交
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
W
Weiming 已提交
171
fi
D
DaniAndTheWeb 已提交
172

D
Daniel M 已提交
173
for preq in "${GIT}" "${python_cmd}"
O
orionaskatu 已提交
174 175 176 177 178 179 180 181 182 183
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

K
kopyl 已提交
184
if [[ $use_venv -eq 1 ]] && ! "${python_cmd}" -c "import venv" &>/dev/null
O
orionaskatu 已提交
185 186 187 188 189 190 191
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

A
AUTOMATIC 已提交
192 193
cd "${install_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/, aborting...\e[0m" "${install_dir}"; exit 1; }
if [[ -d "${clone_dir}" ]]
O
orionaskatu 已提交
194
then
A
AUTOMATIC 已提交
195 196
    cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
else
M
Mackerel 已提交
197 198 199
    printf "\n%s\n" "${delimiter}"
    printf "Clone stable-diffusion-webui"
    printf "\n%s\n" "${delimiter}"
A
AUTOMATIC 已提交
200 201
    "${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; }
O
orionaskatu 已提交
202 203
fi

K
kopyl 已提交
204
if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
O
orionaskatu 已提交
205
then
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    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
O
orionaskatu 已提交
225 226
else
    printf "\n%s\n" "${delimiter}"
227
    printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
O
orionaskatu 已提交
228 229 230
    printf "\n%s\n" "${delimiter}"
fi

G
gk 已提交
231 232 233
# Try using TCMalloc on Linux
prepare_tcmalloc() {
    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 237 238 239
        echo "glibc version is $LIBC_VER"
        libc_vernum=$(expr $LIBC_VER)
        # Since 2.34 libpthread is integrated into libc.so
        libc_v234=2.34
F
fuchen.ljl 已提交
240 241 242 243 244
        # Define Tcmalloc Libs arrays
        TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
        # Traversal array
        for lib in "${TCMALLOC_LIBS[@]}"
        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 248 249
            TC_INFO=(${TCMALLOC//=>/})
            if [[ ! -z "${TC_INFO}" ]]; then
                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 254 255 256 257 258 259 260 261
                    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
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 264 265 266 267 268 269
                    # 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
F
fuchen.ljl 已提交
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"
G
gk 已提交
273 274 275 276
        fi
    fi
}

277
KEEP_GOING=1
278
export SD_WEBUI_RESTART=tmp/restart
279 280 281 282 283 284 285 286 287 288 289 290
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
291
        "${python_cmd}" -u "${LAUNCH_SCRIPT}" "$@"
292 293 294 295 296 297
    fi

    if [[ ! -f tmp/restart ]]; then
        KEEP_GOING=0
    fi
done