webui.sh 7.1 KB

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