formatting.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env bash
  2. # Ruff formatter.
  3. #
  4. # Usage:
  5. # # Do work and commit your work.
  6. # # Format files that differ from origin/main.
  7. # bash formatting.sh
  8. #
  9. #
  10. # Cause the script to exit if a single command fails
  11. set -eo pipefail
  12. # this stops git rev-parse from failing if we run this from the .git directory
  13. builtin cd "$(dirname "${BASH_SOURCE:-$0}")"
  14. ROOT="$(git rev-parse --show-toplevel)"
  15. builtin cd "$ROOT" || exit 1
  16. RUFF_VERSION=$(ruff --version | awk '{print $2}')
  17. MYPY_VERSION=$(mypy --version | awk '{print $2}')
  18. CODESPELL_VERSION=$(codespell --version)
  19. ISORT_VERSION=$(isort --vn)
  20. CLANGFORMAT_VERSION=$(clang-format --version | awk '{print $3}')
  21. # # params: tool name, tool version, required version
  22. tool_version_check() {
  23. if [[ $2 != $3 ]]; then
  24. echo "Wrong $1 version installed: $3 is required, not $2."
  25. exit 1
  26. fi
  27. }
  28. tool_version_check "ruff" $RUFF_VERSION "$(grep "ruff==" requirements-lint.txt | cut -d'=' -f3)"
  29. tool_version_check "isort" "$ISORT_VERSION" "$(grep isort requirements-lint.txt | cut -d'=' -f3)"
  30. tool_version_check "codespell" "$CODESPELL_VERSION" "$(grep codespell requirements-lint.txt | cut -d'=' -f3)"
  31. tool_version_check "clang-format" "$CLANGFORMAT_VERSION" "$(grep clang-format requirements-lint.txt | cut -d'=' -f3)"
  32. tool_version_check "mypy" "$MYPY_VERSION" "$(grep mypy requirements-lint.txt | cut -d'=' -f3)"
  33. # Run mypy
  34. echo 'Aphrodite mypy:'
  35. mypy --follow-imports skip
  36. mypy tests --follow-imports skip
  37. mypy aphrodite/attention --follow-imports skip
  38. mypy aphrodite/processing --follow-imports skip
  39. mypy aphrodite/distributed --follow-imports skip
  40. mypy aphrodite/engine --follow-imports skip
  41. mypy aphrodite/executor --follow-imports skip
  42. mypy aphrodite/lora --follow-imports skip
  43. mypy aphrodite/modeling --follow-imports skip
  44. mypy aphrodite/quantization --follow-imports skip
  45. mypy aphrodite/prompt_adapter --follow-imports skip
  46. mypy aphrodite/spec_decode --follow-imports skip
  47. mypy aphrodite/task_handler --follow-imports skip
  48. echo 'Aphrodite mypy: Done'
  49. # If git diff returns a file that is in the skip list, the file may be checked anyway:
  50. # https://github.com/codespell-project/codespell/issues/1915
  51. # Avoiding the "./" prefix and using "/**" globs for directories appears to solve the problem
  52. CODESPELL_EXCLUDES=(
  53. '--skip' './tests/benchmarks/sonnet.txt,build/**'
  54. )
  55. # check spelling of specified files
  56. spell_check() {
  57. codespell "$@"
  58. }
  59. spell_check_all(){
  60. codespell --toml pyproject.toml "${CODESPELL_EXCLUDES[@]}"
  61. }
  62. # Spelling check of files that differ from main branch.
  63. spell_check_changed() {
  64. # The `if` guard ensures that the list of filenames is not empty, which
  65. # could cause ruff to receive 0 positional arguments, making it hang
  66. # waiting for STDIN.
  67. #
  68. # `diff-filter=ACM` and $MERGEBASE is to ensure we only lint files that
  69. # exist on both branches.
  70. MERGEBASE="$(git merge-base origin/main HEAD)"
  71. if ! git diff --diff-filter=ACM --quiet --exit-code "$MERGEBASE" -- '*.py' '*.pyi' &>/dev/null; then
  72. git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi' | xargs \
  73. codespell "${CODESPELL_EXCLUDES[@]}"
  74. fi
  75. }
  76. # Run Codespell
  77. ## This flag runs spell check of individual files. --files *must* be the first command line
  78. ## arg to use this option.
  79. if [[ "$1" == '--files' ]]; then
  80. spell_check "${@:2}"
  81. # If `--all` is passed, then any further arguments are ignored and the
  82. # entire python directory is linted.
  83. elif [[ "$1" == '--all' ]]; then
  84. spell_check_all
  85. else
  86. # Check spelling only of the files that changed in last commit.
  87. spell_check_changed
  88. fi
  89. echo 'Aphrodite codespell: Done'
  90. # Lint specified files
  91. lint() {
  92. ruff "$@"
  93. }
  94. # Lint files that differ from main branch. Ignores dirs that are not slated
  95. # for autolint yet.
  96. lint_changed() {
  97. # The `if` guard ensures that the list of filenames is not empty, which
  98. # could cause ruff to receive 0 positional arguments, making it hang
  99. # waiting for STDIN.
  100. #
  101. # `diff-filter=ACM` and $MERGEBASE is to ensure we only lint files that
  102. # exist on both branches.
  103. MERGEBASE="$(git merge-base origin/main HEAD)"
  104. if ! git diff --diff-filter=ACM --quiet --exit-code "$MERGEBASE" -- '*.py' '*.pyi' &>/dev/null; then
  105. git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi' | xargs \
  106. ruff
  107. fi
  108. }
  109. # Run Ruff
  110. ### This flag lints individual files. --files *must* be the first command line
  111. ### arg to use this option.
  112. if [[ "$1" == '--files' ]]; then
  113. lint "${@:2}"
  114. # If `--all` is passed, then any further arguments are ignored and the
  115. # entire python directory is linted.
  116. elif [[ "$1" == '--all' ]]; then
  117. lint aphrodite tests
  118. else
  119. # Format only the files that changed in last commit.
  120. lint_changed
  121. fi
  122. echo 'Aphrodite ruff: Done'
  123. # check spelling of specified files
  124. isort_check() {
  125. isort "$@"
  126. }
  127. isort_check_all(){
  128. isort .
  129. }
  130. # Spelling check of files that differ from main branch.
  131. isort_check_changed() {
  132. # The `if` guard ensures that the list of filenames is not empty, which
  133. # could cause ruff to receive 0 positional arguments, making it hang
  134. # waiting for STDIN.
  135. #
  136. # `diff-filter=ACM` and $MERGEBASE is to ensure we only lint files that
  137. # exist on both branches.
  138. MERGEBASE="$(git merge-base origin/main HEAD)"
  139. if ! git diff --diff-filter=ACM --quiet --exit-code "$MERGEBASE" -- '*.py' '*.pyi' &>/dev/null; then
  140. git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi' | xargs \
  141. isort
  142. fi
  143. }
  144. # Run Isort
  145. # This flag runs spell check of individual files. --files *must* be the first command line
  146. # arg to use this option.
  147. if [[ "$1" == '--files' ]]; then
  148. isort_check "${@:2}"
  149. # If `--all` is passed, then any further arguments are ignored and the
  150. # entire python directory is linted.
  151. elif [[ "$1" == '--all' ]]; then
  152. isort_check_all
  153. else
  154. # Check spelling only of the files that changed in last commit.
  155. isort_check_changed
  156. fi
  157. echo 'Aphrodite isort: Done'
  158. # Clang-format section
  159. # Exclude some files for formatting because they are vendored
  160. # NOTE: Keep up to date with .github/workflows/clang-format.yml
  161. CLANG_FORMAT_EXCLUDES=(
  162. 'kernels/moe/softmax.cu'
  163. 'kernels/punica/bgmv/bgmv_bf16_bf16_bf16.cu'
  164. 'kernels/punica/bgmv/bgmv_config.h'
  165. 'kernels/punica/bgmv/bgmv_impl.cuh'
  166. 'kernels/punica/bgmv/vec_dtypes.cuh'
  167. 'kernels/punica/punica_ops.cu'
  168. 'kernels/punica/type_convert.h'
  169. 'kernels/quantization/gguf/ggml-common.h'
  170. 'kernels/quantization/gguf/dequantize.cuh'
  171. 'kernels/quantization/gguf/vecdotq.cuh'
  172. 'kernels/quantization/gguf/mmq.cuh'
  173. 'kernels/quantization/gguf/mmvq.cuh'
  174. )
  175. # Format specified files with clang-format
  176. clang_format() {
  177. clang-format -i "$@"
  178. }
  179. # Format files that differ from main branch with clang-format.
  180. clang_format_changed() {
  181. # The `if` guard ensures that the list of filenames is not empty, which
  182. # could cause clang-format to receive 0 positional arguments, making it hang
  183. # waiting for STDIN.
  184. #
  185. # `diff-filter=ACM` and $MERGEBASE is to ensure we only format files that
  186. # exist on both branches.
  187. MERGEBASE="$(git merge-base origin/main HEAD)"
  188. # Get the list of changed files, excluding the specified ones
  189. changed_files=$(git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.h' '*.cpp' '*.cu' '*.cuh' | grep -vFf <(printf "%s\n" "${CLANG_FORMAT_EXCLUDES[@]}"))
  190. if [ -n "$changed_files" ]; then
  191. echo "$changed_files" | xargs -P 5 clang-format -i
  192. fi
  193. }
  194. # Format all files with clang-format
  195. clang_format_all() {
  196. find kernels/ \( -name '*.h' -o -name '*.cpp' -o -name '*.cu' -o -name '*.cuh' \) -print \
  197. | grep -vFf <(printf "%s\n" "${CLANG_FORMAT_EXCLUDES[@]}") \
  198. | xargs clang-format -i
  199. }
  200. # Run clang-format
  201. if [[ "$1" == '--files' ]]; then
  202. clang_format "${@:2}"
  203. elif [[ "$1" == '--all' ]]; then
  204. clang_format_all
  205. else
  206. clang_format_changed
  207. fi
  208. echo 'Aphrodite clang-format: Done'
  209. if ! git diff --quiet &>/dev/null; then
  210. echo 'Reformatted files. Please review and stage the changes.'
  211. echo 'Changes not staged for commit:'
  212. echo
  213. git --no-pager diff --name-only
  214. exit 1
  215. fi