| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #!/usr/bin/env bash
- PYTHON=""
- # python程序位置,可搭配一键包或是省去每次切换环境
- SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
- # If $venv_dir is "-", then disable venv support
- use_venv=1
- if [[ $venv_dir == "-" ]]; then
- use_venv=0
- fi
- # Set defaults
- # Install directory without trailing slash
- if [[ -z "${install_dir}" ]]
- then
- install_dir="$SCRIPT_DIR"
- fi
- # python3 executable
- if [[ -z "${python_cmd}" ]]
- then
- python_cmd="python3"
- fi
- # python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
- if [[ -z "${venv_dir}" ]] && [[ $use_venv -eq 1 ]]
- then
- venv_dir="venv"
- fi
- if [[ -z "${LAUNCH_SCRIPT}" ]]
- then
- LAUNCH_SCRIPT="wenda.py"
- fi
- # Pretty print
- delimiter="################################################################"
- printf "\n%s\n" "${delimiter}"
- printf "\e[1m\e[32mInstall script for LS Wenda\n"
- printf "\e[1m\e[34mTested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.\e[0m"
- printf "\n%s\n" "${delimiter}"
- # Check if python is installed
- if [[ $use_venv -eq 1 ]] && [[ -z "${VIRTUAL_ENV}" ]];
- then
- 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
- else
- printf "\n%s\n" "${delimiter}"
- printf "python venv already activate or run without venv: ${VIRTUAL_ENV}"
- printf "\n%s\n" "${delimiter}"
- fi
- # Install requirements
- if [[ $use_venv -eq 1 ]]
- then
- if [[ $first_launch -eq 1 ]]
- then
- printf "\n%s\n" "${delimiter}"
- printf "Install requirements"
- printf "\n%s\n" "${delimiter}"
- "${python_cmd}" -m pip install --upgrade pip
- "${python_cmd}" -m pip install -r requirements/requirements.txt
- "${python_cmd}" -m pip install -r requirements/requirements-glm6b-lora.txt
- else
- printf "\n%s\n" "${delimiter}"
- printf "Requirements already installed"
- printf "\n%s\n" "${delimiter}"
- fi
- else
- printf "\n%s\n" "${delimiter}"
- printf "Run without venv"
- printf "\n%s\n" "${delimiter}"
- "${python_cmd}" -m pip install --upgrade pip
- "${python_cmd}" -m pip install -r requirements/requirements.txt
- "${python_cmd}" -m pip install -r requirements/requirements-glm6b-lora.txt
- fi
- # Try using TCMalloc on Linux
- prepare_tcmalloc() {
- if [[ "${OSTYPE}" == "linux"* ]] && [[ -z "${NO_TCMALLOC}" ]] && [[ -z "${LD_PRELOAD}" ]]; then
- # check glibc version
- LIBC_VER=$(echo $(ldd --version | awk 'NR==1 {print $NF}') | grep -oP '\d+\.\d+')
- echo "glibc version is $LIBC_VER"
- libc_vernum=$(expr $LIBC_VER)
- # Since 2.34 libpthread is integrated into libc.so
- libc_v234=2.34
- # Define Tcmalloc Libs arrays
- TCMALLOC_LIBS=("libtcmalloc(_minimal|)\.so\.\d" "libtcmalloc\.so\.\d")
- # Traversal array
- for lib in "${TCMALLOC_LIBS[@]}"
- do
- # Determine which type of tcmalloc library the library supports
- TCMALLOC="$(PATH=/sbin:/usr/sbin:$PATH ldconfig -p | grep -P $lib | head -n 1)"
- TC_INFO=(${TCMALLOC//=>/})
- if [[ ! -z "${TC_INFO}" ]]; then
- echo "Check TCMalloc: ${TC_INFO}"
- # Determine if the library is linked to libpthread and resolve undefined symbol: pthread_key_create
- if [ $(echo "$libc_vernum < $libc_v234" | bc) -eq 1 ]; then
- # glibc < 2.34 pthread_key_create into libpthread.so. check linking libpthread.so...
- 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
- # Version 2.34 of libc.so (glibc) includes the pthread library IN GLIBC. (USE ubuntu 22.04 and modern linux system and WSL)
- # 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
- done
- if [[ -z "${LD_PRELOAD}" ]]; then
- 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"
- fi
- fi
- }
- KEEP_GOING=1
- export PYTHON_BIN_RESTART=tmp/restart
- while [[ "$KEEP_GOING" -eq "1" ]]; do
- if [[ ! -z "${ACCELERATE}" ]] && [ ${ACCELERATE}="True" ] && [ -x "$(command -v accelerate)" ]; then
- printf "\n%s\n" "${delimiter}"
- printf "Accelerating wenda.py..."
- printf "\n%s\n" "${delimiter}"
- prepare_tcmalloc
- accelerate launch --num_cpu_threads_per_process=6 "${LAUNCH_SCRIPT}" -t glm6b "$@"
- else
- printf "\n%s\n" "${delimiter}"
- printf "Launching wenda.py..."
- printf "\n%s\n" "${delimiter}"
- prepare_tcmalloc
- "${python_cmd}" -u "${LAUNCH_SCRIPT}" -t glm6b "$@"
- fi
- if [[ ! -f tmp/restart ]]; then
- KEEP_GOING=0
- fi
- done
|