check-requirements.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. set -euo pipefail
  3. #
  4. # check-requirements.sh checks all requirements files for each top-level
  5. # convert*.py script.
  6. #
  7. # WARNING: This is quite IO intensive, because a fresh venv is set up for every
  8. # python script. As of 2023-12-22, this writes ~2.7GB of data. An adequately
  9. # sized tmpfs /tmp or ramdisk is recommended if running this frequently.
  10. #
  11. # usage: check-requirements.sh [<working_dir>]
  12. # check-requirements.sh nocleanup [<working_dir>]
  13. #
  14. # where:
  15. # - <working_dir> is a directory that can be used as the base for
  16. # setting up the venvs. Defaults to `/tmp`.
  17. # - 'nocleanup' as the first argument will disable automatic cleanup
  18. # of the files created by this script.
  19. #
  20. # requires:
  21. # - bash >= 3.2.57
  22. # - shellcheck
  23. #
  24. # For each script, it creates a fresh venv, `pip install`s the requirements, and
  25. # finally imports the python script to check for `ImportError`.
  26. #
  27. log() {
  28. local level=$1 msg=$2
  29. printf >&2 '%s: %s\n' "$level" "$msg"
  30. }
  31. debug() {
  32. log DEBUG "$@"
  33. }
  34. info() {
  35. log INFO "$@"
  36. }
  37. fatal() {
  38. log FATAL "$@"
  39. exit 1
  40. }
  41. cleanup() {
  42. if [[ -n ${workdir+x} && -d $workdir && -w $workdir ]]; then
  43. info "Removing $workdir"
  44. local count=0
  45. rm -rfv -- "$workdir" | while read -r; do
  46. if (( count++ > 750 )); then
  47. printf .
  48. count=0
  49. fi
  50. done
  51. printf '\n'
  52. info "Removed $workdir"
  53. fi
  54. }
  55. do_cleanup=1
  56. if [[ ${1-} == nocleanup ]]; then
  57. do_cleanup=0; shift
  58. fi
  59. if (( do_cleanup )); then
  60. trap exit INT TERM
  61. trap cleanup EXIT
  62. fi
  63. this=$(realpath -- "$0"); readonly this
  64. cd "$(dirname "$this")/.." # PWD should stay in llama.cpp project directory
  65. shellcheck "$this"
  66. readonly reqs_dir=requirements
  67. if [[ ${1+x} ]]; then
  68. tmp_dir=$(realpath -- "$1")
  69. if [[ ! ( -d $tmp_dir && -w $tmp_dir ) ]]; then
  70. fatal "$tmp_dir is not a writable directory"
  71. fi
  72. else
  73. tmp_dir=/tmp
  74. fi
  75. workdir=$(mktemp -d "$tmp_dir/check-requirements.XXXX"); readonly workdir
  76. info "Working directory: $workdir"
  77. check_requirements() {
  78. local reqs=$1
  79. info "$reqs: beginning check"
  80. pip --disable-pip-version-check install -qr "$reqs"
  81. info "$reqs: OK"
  82. }
  83. check_convert_script() {
  84. local py=$1 # e.g. ./convert-hf-to-gguf.py
  85. local pyname=${py##*/} # e.g. convert-hf-to-gguf.py
  86. pyname=${pyname%.py} # e.g. convert-hf-to-gguf
  87. info "$py: beginning check"
  88. local reqs="$reqs_dir/requirements-$pyname.txt"
  89. if [[ ! -r $reqs ]]; then
  90. fatal "$py missing requirements. Expected: $reqs"
  91. fi
  92. local venv="$workdir/$pyname-venv"
  93. python3 -m venv "$venv"
  94. (
  95. # shellcheck source=/dev/null
  96. source "$venv/bin/activate"
  97. check_requirements "$reqs"
  98. python - "$py" "$pyname" <<'EOF'
  99. import sys
  100. from importlib.machinery import SourceFileLoader
  101. py, pyname = sys.argv[1:]
  102. SourceFileLoader(pyname, py).load_module()
  103. EOF
  104. )
  105. if (( do_cleanup )); then
  106. rm -rf -- "$venv"
  107. fi
  108. info "$py: imports OK"
  109. }
  110. readonly ignore_eq_eq='check_requirements: ignore "=="'
  111. for req in "$reqs_dir"/*; do
  112. # Check that all sub-requirements are added to top-level requirements.txt
  113. if ! grep -qF "$req" requirements.txt; then
  114. fatal "$req needs to be added to requirements.txt"
  115. fi
  116. # Make sure exact release versions aren't being pinned in the requirements
  117. # Filters out the ignore string
  118. if grep -vF "$ignore_eq_eq" "$req" | grep -q '=='; then
  119. tab=$'\t'
  120. cat >&2 <<EOF
  121. FATAL: Avoid pinning exact package versions. Use '~=' instead.
  122. You can suppress this error by appending the following to the line:
  123. $tab# $ignore_eq_eq
  124. EOF
  125. exit 1
  126. fi
  127. done
  128. all_venv="$workdir/all-venv"
  129. python3 -m venv "$all_venv"
  130. (
  131. # shellcheck source=/dev/null
  132. source "$all_venv/bin/activate"
  133. check_requirements requirements.txt
  134. )
  135. if (( do_cleanup )); then
  136. rm -rf -- "$all_venv"
  137. fi
  138. check_convert_script examples/convert-legacy-llama.py
  139. for py in convert-*.py; do
  140. # skip convert-hf-to-gguf-update.py
  141. # TODO: the check is failing for some reason:
  142. # https://github.com/ggerganov/llama.cpp/actions/runs/8875330981/job/24364557177?pr=6920
  143. [[ $py == convert-hf-to-gguf-update.py ]] && continue
  144. check_convert_script "$py"
  145. done
  146. info 'Done! No issues found.'