webui.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
  7. # If run from macOS, load defaults from webui-macos-env.sh
  8. if [[ "$OSTYPE" == "darwin"* ]]; then
  9. if [[ -f "$SCRIPT_DIR"/webui-macos-env.sh ]]
  10. then
  11. source "$SCRIPT_DIR"/webui-macos-env.sh
  12. fi
  13. fi
  14. # Read variables from webui-user.sh
  15. # shellcheck source=/dev/null
  16. if [[ -f "$SCRIPT_DIR"/webui-user.sh ]]
  17. then
  18. source "$SCRIPT_DIR"/webui-user.sh
  19. fi
  20. # If $venv_dir is "-", then disable venv support
  21. use_venv=1
  22. if [[ $venv_dir == "-" ]]; then
  23. use_venv=0
  24. fi
  25. # Set defaults
  26. # Install directory without trailing slash
  27. if [[ -z "${install_dir}" ]]
  28. then
  29. install_dir="$SCRIPT_DIR"
  30. fi
  31. # Name of the subdirectory (defaults to stable-diffusion-webui)
  32. if [[ -z "${clone_dir}" ]]
  33. then
  34. clone_dir="stable-diffusion-webui"
  35. fi
  36. # python3 executable
  37. if [[ -z "${python_cmd}" ]]
  38. then
  39. python_cmd="python3"
  40. fi
  41. # git executable
  42. if [[ -z "${GIT}" ]]
  43. then
  44. export GIT="git"
  45. else
  46. export GIT_PYTHON_GIT_EXECUTABLE="${GIT}"
  47. fi
  48. # python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
  49. if [[ -z "${venv_dir}" ]] && [[ $use_venv -eq 1 ]]
  50. then
  51. venv_dir="venv"
  52. fi
  53. if [[ -z "${LAUNCH_SCRIPT}" ]]
  54. then
  55. LAUNCH_SCRIPT="launch.py"
  56. fi
  57. # this script cannot be run as root by default
  58. can_run_as_root=0
  59. # read any command line flags to the webui.sh script
  60. while getopts "f" flag > /dev/null 2>&1
  61. do
  62. case ${flag} in
  63. f) can_run_as_root=1;;
  64. *) break;;
  65. esac
  66. done
  67. # Disable sentry logging
  68. export ERROR_REPORTING=FALSE
  69. # Do not reinstall existing pip packages on Debian/Ubuntu
  70. export PIP_IGNORE_INSTALLED=0
  71. # Pretty print
  72. delimiter="################################################################"
  73. printf "\n%s\n" "${delimiter}"
  74. printf "\e[1m\e[32mInstall script for stable-diffusion + Web UI\n"
  75. printf "\e[1m\e[34mTested on Debian 11 (Bullseye)\e[0m"
  76. printf "\n%s\n" "${delimiter}"
  77. # Do not run as root
  78. if [[ $(id -u) -eq 0 && can_run_as_root -eq 0 ]]
  79. then
  80. printf "\n%s\n" "${delimiter}"
  81. printf "\e[1m\e[31mERROR: This script must not be launched as root, aborting...\e[0m"
  82. printf "\n%s\n" "${delimiter}"
  83. exit 1
  84. else
  85. printf "\n%s\n" "${delimiter}"
  86. printf "Running on \e[1m\e[32m%s\e[0m user" "$(whoami)"
  87. printf "\n%s\n" "${delimiter}"
  88. fi
  89. if [[ $(getconf LONG_BIT) = 32 ]]
  90. then
  91. printf "\n%s\n" "${delimiter}"
  92. printf "\e[1m\e[31mERROR: Unsupported Running on a 32bit OS\e[0m"
  93. printf "\n%s\n" "${delimiter}"
  94. exit 1
  95. fi
  96. if [[ -d .git ]]
  97. then
  98. printf "\n%s\n" "${delimiter}"
  99. printf "Repo already cloned, using it as install directory"
  100. printf "\n%s\n" "${delimiter}"
  101. install_dir="${PWD}/../"
  102. clone_dir="${PWD##*/}"
  103. fi
  104. # Check prerequisites
  105. gpu_info=$(lspci 2>/dev/null | grep -E "VGA|Display")
  106. case "$gpu_info" in
  107. *"Navi 1"*)
  108. export HSA_OVERRIDE_GFX_VERSION=10.3.0
  109. if [[ -z "${TORCH_COMMAND}" ]]
  110. then
  111. pyv="$(${python_cmd} -c 'import sys; print(".".join(map(str, sys.version_info[0:2])))')"
  112. if [[ $(bc <<< "$pyv <= 3.10") -eq 1 ]]
  113. then
  114. # Navi users will still use torch 1.13 because 2.0 does not seem to work.
  115. 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"
  116. else
  117. printf "\e[1m\e[31mERROR: RX 5000 series GPUs must be using at max python 3.10, aborting...\e[0m"
  118. exit 1
  119. fi
  120. fi
  121. ;;
  122. *"Navi 2"*) export HSA_OVERRIDE_GFX_VERSION=10.3.0
  123. ;;
  124. *"Navi 3"*) [[ -z "${TORCH_COMMAND}" ]] && \
  125. export TORCH_COMMAND="pip install torch torchvision --index-url https://download.pytorch.org/whl/test/rocm5.6"
  126. # Navi 3 needs at least 5.5 which is only on the torch 2.1.0 release candidates right now
  127. ;;
  128. *"Renoir"*) export HSA_OVERRIDE_GFX_VERSION=9.0.0
  129. printf "\n%s\n" "${delimiter}"
  130. 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"
  131. printf "\n%s\n" "${delimiter}"
  132. ;;
  133. *)
  134. ;;
  135. esac
  136. if ! echo "$gpu_info" | grep -q "NVIDIA";
  137. then
  138. if echo "$gpu_info" | grep -q "AMD" && [[ -z "${TORCH_COMMAND}" ]]
  139. then
  140. 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"
  141. fi
  142. fi
  143. for preq in "${GIT}" "${python_cmd}"
  144. do
  145. if ! hash "${preq}" &>/dev/null
  146. then
  147. printf "\n%s\n" "${delimiter}"
  148. printf "\e[1m\e[31mERROR: %s is not installed, aborting...\e[0m" "${preq}"
  149. printf "\n%s\n" "${delimiter}"
  150. exit 1
  151. fi
  152. done
  153. if [[ $use_venv -eq 1 ]] && ! "${python_cmd}" -c "import venv" &>/dev/null
  154. then
  155. printf "\n%s\n" "${delimiter}"
  156. printf "\e[1m\e[31mERROR: python3-venv is not installed, aborting...\e[0m"
  157. printf "\n%s\n" "${delimiter}"
  158. exit 1
  159. fi
  160. cd "${install_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/, aborting...\e[0m" "${install_dir}"; exit 1; }
  161. if [[ -d "${clone_dir}" ]]
  162. then
  163. cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
  164. else
  165. printf "\n%s\n" "${delimiter}"
  166. printf "Clone stable-diffusion-webui"
  167. printf "\n%s\n" "${delimiter}"
  168. "${GIT}" clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git "${clone_dir}"
  169. cd "${clone_dir}"/ || { printf "\e[1m\e[31mERROR: Can't cd to %s/%s/, aborting...\e[0m" "${install_dir}" "${clone_dir}"; exit 1; }
  170. fi
  171. if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
  172. then
  173. printf "\n%s\n" "${delimiter}"
  174. printf "Create and activate python venv"
  175. printf "\n%s\n" "${delimiter}"
  176. 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; }
  177. if [[ ! -d "${venv_dir}" ]]
  178. then
  179. "${python_cmd}" -m venv "${venv_dir}"
  180. first_launch=1
  181. fi
  182. # shellcheck source=/dev/null
  183. if [[ -f "${venv_dir}"/bin/activate ]]
  184. then
  185. source "${venv_dir}"/bin/activate
  186. else
  187. printf "\n%s\n" "${delimiter}"
  188. printf "\e[1m\e[31mERROR: Cannot activate python venv, aborting...\e[0m"
  189. printf "\n%s\n" "${delimiter}"
  190. exit 1
  191. fi
  192. else
  193. printf "\n%s\n" "${delimiter}"
  194. printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
  195. printf "\n%s\n" "${delimiter}"
  196. fi
  197. # Try using TCMalloc on Linux
  198. prepare_tcmalloc() {
  199. if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${NO_TCMALLOC}" ]] && [[ -z "${LD_PRELOAD}" ]]; then
  200. TCMALLOC="$(PATH=/usr/sbin:$PATH ldconfig -p | grep -Po "libtcmalloc(_minimal|)\.so\.\d" | head -n 1)"
  201. if [[ ! -z "${TCMALLOC}" ]]; then
  202. echo "Using TCMalloc: ${TCMALLOC}"
  203. export LD_PRELOAD="${TCMALLOC}"
  204. else
  205. printf "\e[1m\e[31mCannot locate TCMalloc (improves CPU memory usage)\e[0m\n"
  206. fi
  207. fi
  208. }
  209. KEEP_GOING=1
  210. export SD_WEBUI_RESTART=tmp/restart
  211. while [[ "$KEEP_GOING" -eq "1" ]]; do
  212. if [[ ! -z "${ACCELERATE}" ]] && [ ${ACCELERATE}="True" ] && [ -x "$(command -v accelerate)" ]; then
  213. printf "\n%s\n" "${delimiter}"
  214. printf "Accelerating launch.py..."
  215. printf "\n%s\n" "${delimiter}"
  216. prepare_tcmalloc
  217. accelerate launch --num_cpu_threads_per_process=6 "${LAUNCH_SCRIPT}" "$@"
  218. else
  219. printf "\n%s\n" "${delimiter}"
  220. printf "Launching launch.py..."
  221. printf "\n%s\n" "${delimiter}"
  222. prepare_tcmalloc
  223. "${python_cmd}" -u "${LAUNCH_SCRIPT}" "$@"
  224. fi
  225. if [[ ! -f tmp/restart ]]; then
  226. KEEP_GOING=0
  227. fi
  228. done