formatting.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. # If git diff returns a file that is in the skip list, the file may be checked anyway:
  33. # https://github.com/codespell-project/codespell/issues/1915
  34. # Avoiding the "./" prefix and using "/**" globs for directories appears to solve the problem
  35. CODESPELL_EXCLUDES=(
  36. '--skip' './tests/benchmarks/sonnet.txt,build/**'
  37. )
  38. # check spelling of specified files
  39. spell_check() {
  40. codespell "$@"
  41. }
  42. spell_check_all(){
  43. codespell --toml pyproject.toml "${CODESPELL_EXCLUDES[@]}"
  44. }
  45. # Spelling check of files that differ from main branch.
  46. spell_check_changed() {
  47. # The `if` guard ensures that the list of filenames is not empty, which
  48. # could cause ruff to receive 0 positional arguments, making it hang
  49. # waiting for STDIN.
  50. #
  51. # `diff-filter=ACM` and $MERGEBASE is to ensure we only lint files that
  52. # exist on both branches.
  53. MERGEBASE="$(git merge-base origin/main HEAD)"
  54. if ! git diff --diff-filter=ACM --quiet --exit-code "$MERGEBASE" -- '*.py' '*.pyi' &>/dev/null; then
  55. git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi' | xargs \
  56. codespell "${CODESPELL_EXCLUDES[@]}"
  57. fi
  58. }
  59. # Run Codespell
  60. ## This flag runs spell check of individual files. --files *must* be the first command line
  61. ## arg to use this option.
  62. if [[ "$1" == '--files' ]]; then
  63. spell_check "${@:2}"
  64. # If `--all` is passed, then any further arguments are ignored and the
  65. # entire python directory is linted.
  66. elif [[ "$1" == '--all' ]]; then
  67. spell_check_all
  68. else
  69. # Check spelling only of the files that changed in last commit.
  70. spell_check_changed
  71. fi
  72. echo 'Aphrodite codespell: Done'
  73. # Lint specified files
  74. lint() {
  75. ruff "$@"
  76. }
  77. # Lint files that differ from main branch. Ignores dirs that are not slated
  78. # for autolint yet.
  79. lint_changed() {
  80. # The `if` guard ensures that the list of filenames is not empty, which
  81. # could cause ruff to receive 0 positional arguments, making it hang
  82. # waiting for STDIN.
  83. #
  84. # `diff-filter=ACM` and $MERGEBASE is to ensure we only lint files that
  85. # exist on both branches.
  86. MERGEBASE="$(git merge-base origin/main HEAD)"
  87. if ! git diff --diff-filter=ACM --quiet --exit-code "$MERGEBASE" -- '*.py' '*.pyi' &>/dev/null; then
  88. git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi' | xargs \
  89. ruff
  90. fi
  91. }
  92. # Run Ruff
  93. ### This flag lints individual files. --files *must* be the first command line
  94. ### arg to use this option.
  95. if [[ "$1" == '--files' ]]; then
  96. lint "${@:2}"
  97. # If `--all` is passed, then any further arguments are ignored and the
  98. # entire python directory is linted.
  99. elif [[ "$1" == '--all' ]]; then
  100. lint aphrodite tests
  101. else
  102. # Format only the files that changed in last commit.
  103. lint_changed
  104. fi
  105. echo 'Aphrodite ruff: Done'
  106. # check spelling of specified files
  107. isort_check() {
  108. isort "$@"
  109. }
  110. isort_check_all(){
  111. isort .
  112. }
  113. # Spelling check of files that differ from main branch.
  114. isort_check_changed() {
  115. # The `if` guard ensures that the list of filenames is not empty, which
  116. # could cause ruff to receive 0 positional arguments, making it hang
  117. # waiting for STDIN.
  118. #
  119. # `diff-filter=ACM` and $MERGEBASE is to ensure we only lint files that
  120. # exist on both branches.
  121. MERGEBASE="$(git merge-base origin/main HEAD)"
  122. if ! git diff --diff-filter=ACM --quiet --exit-code "$MERGEBASE" -- '*.py' '*.pyi' &>/dev/null; then
  123. git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.py' '*.pyi' | xargs \
  124. isort
  125. fi
  126. }
  127. # Run Isort
  128. # This flag runs spell check of individual files. --files *must* be the first command line
  129. # arg to use this option.
  130. if [[ "$1" == '--files' ]]; then
  131. isort_check "${@:2}"
  132. # If `--all` is passed, then any further arguments are ignored and the
  133. # entire python directory is linted.
  134. elif [[ "$1" == '--all' ]]; then
  135. isort_check_all
  136. else
  137. # Check spelling only of the files that changed in last commit.
  138. isort_check_changed
  139. fi
  140. echo 'Aphrodite isort: Done'
  141. # Clang-format section
  142. # Exclude some files for formatting because they are vendored
  143. # NOTE: Keep up to date with .github/workflows/clang-format.yml
  144. CLANG_FORMAT_EXCLUDES=(
  145. 'kernels/moe/softmax.cu'
  146. 'kernels/punica/bgmv/bgmv_bf16_bf16_bf16.cu'
  147. 'kernels/punica/bgmv/bgmv_config.h'
  148. 'kernels/punica/bgmv/bgmv_impl.cuh'
  149. 'kernels/punica/bgmv/vec_dtypes.cuh'
  150. 'kernels/punica/punica_ops.cu'
  151. 'kernels/punica/type_convert.h'
  152. 'kernels/quantization/gguf/ggml-common.h'
  153. 'kernels/quantization/gguf/dequantize.cuh'
  154. 'kernels/quantization/gguf/vecdotq.cuh'
  155. 'kernels/quantization/gguf/mmq.cuh'
  156. 'kernels/quantization/gguf/mmvq.cuh'
  157. )
  158. # Format specified files with clang-format
  159. clang_format() {
  160. clang-format -i "$@"
  161. }
  162. # Format files that differ from main branch with clang-format.
  163. clang_format_changed() {
  164. # The `if` guard ensures that the list of filenames is not empty, which
  165. # could cause clang-format to receive 0 positional arguments, making it hang
  166. # waiting for STDIN.
  167. #
  168. # `diff-filter=ACM` and $MERGEBASE is to ensure we only format files that
  169. # exist on both branches.
  170. MERGEBASE="$(git merge-base origin/main HEAD)"
  171. # Get the list of changed files, excluding the specified ones
  172. changed_files=$(git diff --name-only --diff-filter=ACM "$MERGEBASE" -- '*.h' '*.cpp' '*.cu' '*.cuh' | grep -vFf <(printf "%s\n" "${CLANG_FORMAT_EXCLUDES[@]}"))
  173. if [ -n "$changed_files" ]; then
  174. echo "$changed_files" | xargs -P 5 clang-format -i
  175. fi
  176. }
  177. # Format all files with clang-format
  178. clang_format_all() {
  179. find kernels/ \( -name '*.h' -o -name '*.cpp' -o -name '*.cu' -o -name '*.cuh' \) -print \
  180. | grep -vFf <(printf "%s\n" "${CLANG_FORMAT_EXCLUDES[@]}") \
  181. | xargs clang-format -i
  182. }
  183. # Run clang-format
  184. if [[ "$1" == '--files' ]]; then
  185. clang_format "${@:2}"
  186. elif [[ "$1" == '--all' ]]; then
  187. clang_format_all
  188. else
  189. clang_format_changed
  190. fi
  191. echo 'Aphrodite clang-format: Done'
  192. if ! git diff --quiet &>/dev/null; then
  193. echo 'Reformatted files. Please review and stage the changes.'
  194. echo 'Changes not staged for commit:'
  195. echo
  196. git --no-pager diff --name-only
  197. exit 1
  198. fi