run_GLM6B.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env bash
  2. PYTHON=""
  3. # python程序位置,可搭配一键包或是省去每次切换环境
  4. SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
  5. # If $venv_dir is "-", then disable venv support
  6. use_venv=1
  7. if [[ $venv_dir == "-" ]]; then
  8. use_venv=0
  9. fi
  10. # Set defaults
  11. # Install directory without trailing slash
  12. if [[ -z "${install_dir}" ]]
  13. then
  14. install_dir="$SCRIPT_DIR"
  15. fi
  16. # python3 executable
  17. if [[ -z "${python_cmd}" ]]
  18. then
  19. python_cmd="python3"
  20. fi
  21. # python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
  22. if [[ -z "${venv_dir}" ]] && [[ $use_venv -eq 1 ]]
  23. then
  24. venv_dir="venv"
  25. fi
  26. if [[ -z "${LAUNCH_SCRIPT}" ]]
  27. then
  28. LAUNCH_SCRIPT="wenda.py"
  29. fi
  30. # Pretty print
  31. delimiter="################################################################"
  32. printf "\n%s\n" "${delimiter}"
  33. printf "\e[1m\e[32mInstall script for LS Wenda\n"
  34. printf "\e[1m\e[34mTested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.\e[0m"
  35. printf "\n%s\n" "${delimiter}"
  36. # Check if python is installed
  37. if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
  38. then
  39. printf "\n%s\n" "${delimiter}"
  40. printf "Create and activate python venv"
  41. printf "\n%s\n" "${delimiter}"
  42. 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; }
  43. if [[ ! -d "${venv_dir}" ]]
  44. then
  45. "${python_cmd}" -m venv "${venv_dir}"
  46. first_launch=1
  47. fi
  48. # shellcheck source=/dev/null
  49. if [[ -f "${venv_dir}"/bin/activate ]]
  50. then
  51. source "${venv_dir}"/bin/activate
  52. else
  53. printf "\n%s\n" "${delimiter}"
  54. printf "\e[1m\e[31mERROR: Cannot activate python venv, aborting...\e[0m"
  55. printf "\n%s\n" "${delimiter}"
  56. exit 1
  57. fi
  58. else
  59. printf "\n%s\n" "${delimiter}"
  60. printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
  61. printf "\n%s\n" "${delimiter}"
  62. fi
  63. # Install requirements
  64. if [[ $use_venv -eq 1 ]]
  65. then
  66. if [[ $first_launch -eq 1 ]]
  67. then
  68. printf "\n%s\n" "${delimiter}"
  69. printf "Install requirements"
  70. printf "\n%s\n" "${delimiter}"
  71. "${python_cmd}" -m pip install --upgrade pip
  72. "${python_cmd}" -m pip install -r requirements/requirements.txt
  73. "${python_cmd}" -m pip install -r requirements/requirements-glm6b-lora.txt
  74. else
  75. printf "\n%s\n" "${delimiter}"
  76. printf "Requirements already installed"
  77. printf "\n%s\n" "${delimiter}"
  78. fi
  79. else
  80. printf "\n%s\n" "${delimiter}"
  81. printf "Run without venv"
  82. printf "\n%s\n" "${delimiter}"
  83. "${python_cmd}" -m pip install --upgrade pip
  84. "${python_cmd}" -m pip install -r requirements/requirements.txt
  85. "${python_cmd}" -m pip install -r requirements/requirements-glm6b-lora.txt
  86. fi
  87. # Try using TCMalloc on Linux
  88. prepare_tcmalloc() {
  89. if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${NO_TCMALLOC}" ]] && [[ -z "${LD_PRELOAD}" ]]; then
  90. # check glibc version
  91. LIBC_VER=$(echo $(ldd --version | awk 'NR==1 {print $NF}') | grep -oP '\d+\.\d+')
  92. echo "glibc version is $LIBC_VER"
  93. libc_vernum=$(expr $LIBC_VER)
  94. # Since 2.34 libpthread is integrated into libc.so
  95. libc_v234=2.34
  96. # Define Tcmalloc Libs arrays
  97. TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
  98. # Traversal array
  99. for lib in "${TCMALLOC_LIBS[@]}"
  100. do
  101. # Determine which type of tcmalloc library the library supports
  102. TCMALLOC="$(PATH=/sbin:/usr/sbin:$PATH ldconfig -p | grep -P $lib | head -n 1)"
  103. TC_INFO=(${TCMALLOC//=>/})
  104. if [[ ! -z "${TC_INFO}" ]]; then
  105. echo "Check TCMalloc: ${TC_INFO}"
  106. # Determine if the library is linked to libpthread and resolve undefined symbol: pthread_key_create
  107. if [ $(echo "$libc_vernum < $libc_v234" | bc) -eq 1 ]; then
  108. # glibc < 2.34 pthread_key_create into libpthread.so. check linking libpthread.so...
  109. if ldd ${TC_INFO[2]} | grep -q 'libpthread'; then
  110. echo "$TC_INFO is linked with libpthread,execute LD_PRELOAD=${TC_INFO[2]}"
  111. # set fullpath LD_PRELOAD (To be on the safe side)
  112. export LD_PRELOAD="${TC_INFO[2]}"
  113. break
  114. else
  115. echo "$TC_INFO is not linked with libpthread will trigger undefined symbol: pthread_Key_create error"
  116. fi
  117. else
  118. # Version 2.34 of libc.so (glibc) includes the pthread library IN GLIBC. (USE ubuntu 22.04 and modern linux system and WSL)
  119. # libc.so(glibc) is linked with a library that works in ALMOST ALL Linux userlands. SO NO CHECK!
  120. echo "$TC_INFO is linked with libc.so,execute LD_PRELOAD=${TC_INFO[2]}"
  121. # set fullpath LD_PRELOAD (To be on the safe side)
  122. export LD_PRELOAD="${TC_INFO[2]}"
  123. break
  124. fi
  125. fi
  126. done
  127. if [[ -z "${LD_PRELOAD}" ]]; then
  128. 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"
  129. fi
  130. fi
  131. }
  132. KEEP_GOING=1
  133. export PYTHON_BIN_RESTART=tmp/restart
  134. while [[ "$KEEP_GOING" -eq "1" ]]; do
  135. if [[ ! -z "${ACCELERATE}" ]] && [ ${ACCELERATE}="True" ] && [ -x "$(command -v accelerate)" ]; then
  136. printf "\n%s\n" "${delimiter}"
  137. printf "Accelerating wenda.py..."
  138. printf "\n%s\n" "${delimiter}"
  139. prepare_tcmalloc
  140. accelerate launch --num_cpu_threads_per_process=6 "${LAUNCH_SCRIPT}" -t glm6b "$@"
  141. else
  142. printf "\n%s\n" "${delimiter}"
  143. printf "Launching wenda.py..."
  144. printf "\n%s\n" "${delimiter}"
  145. prepare_tcmalloc
  146. "${python_cmd}" -u "${LAUNCH_SCRIPT}" -t glm6b "$@"
  147. fi
  148. if [[ ! -f tmp/restart ]]; then
  149. KEEP_GOING=0
  150. fi
  151. done
粤ICP备19079148号