formatting.ps1 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Ruff formatter.
  2. #
  3. # Usage:
  4. # # Do work and commit your work.
  5. # # Format files that differ from origin/main.
  6. # .\formatting.ps1
  7. # Stop on first error
  8. $ErrorActionPreference = "Stop"
  9. # Change to script directory and get root of git repo
  10. Push-Location $PSScriptRoot
  11. $ROOT = git rev-parse --show-toplevel
  12. Set-Location $ROOT
  13. # Get tool versions
  14. $RUFF_VERSION = (ruff --version).Split(" ")[1]
  15. $MYPY_VERSION = (mypy --version).Split(" ")[1]
  16. $CODESPELL_VERSION = (codespell --version)
  17. $ISORT_VERSION = (isort --vn)
  18. $CLANGFORMAT_VERSION = (clang-format --version).Split(" ")[2]
  19. # Version check function
  20. function Test-ToolVersion {
  21. param($toolName, $currentVersion, $requiredVersion)
  22. if ($currentVersion -ne $requiredVersion) {
  23. Write-Error "Wrong $toolName version installed: $requiredVersion is required, not $currentVersion."
  24. exit 1
  25. }
  26. }
  27. # Get required versions from requirements file
  28. $REQUIRED_RUFF = (Get-Content requirements-lint.txt | Select-String "ruff==").ToString().Split("==")[2]
  29. $REQUIRED_ISORT = (Get-Content requirements-lint.txt | Select-String "isort").ToString().Split("==")[2]
  30. $REQUIRED_CODESPELL = (Get-Content requirements-lint.txt | Select-String "codespell").ToString().Split("==")[2]
  31. $REQUIRED_CLANGFORMAT = (Get-Content requirements-lint.txt | Select-String "clang-format").ToString().Split("==")[2]
  32. # Check versions
  33. Test-ToolVersion "ruff" $RUFF_VERSION $REQUIRED_RUFF
  34. Test-ToolVersion "isort" $ISORT_VERSION $REQUIRED_ISORT
  35. Test-ToolVersion "codespell" $CODESPELL_VERSION $REQUIRED_CODESPELL
  36. Test-ToolVersion "clang-format" $CLANGFORMAT_VERSION $REQUIRED_CLANGFORMAT
  37. # Codespell excludes
  38. $CODESPELL_EXCLUDES = @('--skip', 'tests/benchmarks/sonnet.txt,build/**')
  39. function Invoke-SpellCheck {
  40. param([string[]]$files)
  41. codespell $files
  42. }
  43. function Invoke-SpellCheckAll {
  44. codespell --toml pyproject.toml $CODESPELL_EXCLUDES
  45. }
  46. function Invoke-SpellCheckChanged {
  47. $MERGEBASE = git merge-base origin/main HEAD
  48. $changedFiles = git diff --name-only --diff-filter=ACM $MERGEBASE -- "*.py" "*.pyi"
  49. if ($changedFiles) {
  50. codespell $changedFiles $CODESPELL_EXCLUDES
  51. }
  52. }
  53. # Run Codespell based on arguments
  54. if ($args[0] -eq '--files') {
  55. Invoke-SpellCheck $args[1..($args.Length-1)]
  56. }
  57. elseif ($args[0] -eq '--all') {
  58. Invoke-SpellCheckAll
  59. }
  60. else {
  61. Invoke-SpellCheckChanged
  62. }
  63. Write-Host 'Aphrodite codespell: Done'
  64. # Ruff section
  65. function Invoke-Lint {
  66. param([string[]]$files)
  67. ruff $files
  68. }
  69. function Invoke-LintChanged {
  70. $MERGEBASE = git merge-base origin/main HEAD
  71. $changedFiles = git diff --name-only --diff-filter=ACM $MERGEBASE -- "*.py" "*.pyi"
  72. if ($changedFiles) {
  73. ruff $changedFiles
  74. }
  75. }
  76. # Run Ruff based on arguments
  77. if ($args[0] -eq '--files') {
  78. Invoke-Lint $args[1..($args.Length-1)]
  79. }
  80. elseif ($args[0] -eq '--all') {
  81. Invoke-Lint @("aphrodite", "tests")
  82. }
  83. else {
  84. Invoke-LintChanged
  85. }
  86. Write-Host 'Aphrodite ruff: Done'
  87. # Isort section
  88. function Invoke-IsortCheck {
  89. param([string[]]$files)
  90. isort $files
  91. }
  92. function Invoke-IsortCheckAll {
  93. isort .
  94. }
  95. function Invoke-IsortCheckChanged {
  96. $MERGEBASE = git merge-base origin/main HEAD
  97. $changedFiles = git diff --name-only --diff-filter=ACM $MERGEBASE -- "*.py" "*.pyi"
  98. if ($changedFiles) {
  99. isort $changedFiles
  100. }
  101. }
  102. # Run Isort based on arguments
  103. if ($args[0] -eq '--files') {
  104. Invoke-IsortCheck $args[1..($args.Length-1)]
  105. }
  106. elseif ($args[0] -eq '--all') {
  107. Invoke-IsortCheckAll
  108. }
  109. else {
  110. Invoke-IsortCheckChanged
  111. }
  112. Write-Host 'Aphrodite isort: Done'
  113. # Clang-format section
  114. $CLANG_FORMAT_EXCLUDES = @(
  115. 'kernels/moe/softmax.cu',
  116. 'kernels/punica/bgmv/bgmv_bf16_bf16_bf16.cu',
  117. 'kernels/punica/bgmv/bgmv_config.h',
  118. 'kernels/punica/bgmv/bgmv_impl.cuh',
  119. 'kernels/punica/bgmv/vec_dtypes.cuh',
  120. 'kernels/punica/punica_ops.cu',
  121. 'kernels/punica/type_convert.h',
  122. 'kernels/quantization/gguf/ggml-common.h',
  123. 'kernels/quantization/gguf/dequantize.cuh',
  124. 'kernels/quantization/gguf/vecdotq.cuh',
  125. 'kernels/quantization/gguf/mmq.cuh',
  126. 'kernels/quantization/gguf/mmvq.cuh'
  127. )
  128. function Invoke-ClangFormat {
  129. param([string[]]$files)
  130. clang-format -i $files
  131. }
  132. function Invoke-ClangFormatChanged {
  133. $MERGEBASE = git merge-base origin/main HEAD
  134. $changedFiles = git diff --name-only --diff-filter=ACM $MERGEBASE -- "*.h" "*.cpp" "*.cu" "*.cuh" |
  135. Where-Object { $file = $_; -not ($CLANG_FORMAT_EXCLUDES | Where-Object { $file -like "*$_*" }) }
  136. if ($changedFiles) {
  137. $changedFiles | ForEach-Object { clang-format -i $_ }
  138. }
  139. }
  140. function Invoke-ClangFormatAll {
  141. Get-ChildItem -Recurse -Path "kernels/" -Include @("*.h", "*.cpp", "*.cu", "*.cuh") |
  142. Where-Object { $file = $_.FullName; -not ($CLANG_FORMAT_EXCLUDES | Where-Object { $file -like "*$_*" }) } |
  143. ForEach-Object { clang-format -i $_.FullName }
  144. }
  145. # Run clang-format based on arguments
  146. if ($args[0] -eq '--files') {
  147. Invoke-ClangFormat $args[1..($args.Length-1)]
  148. }
  149. elseif ($args[0] -eq '--all') {
  150. Invoke-ClangFormatAll
  151. }
  152. else {
  153. Invoke-ClangFormatChanged
  154. }
  155. Write-Host 'Aphrodite clang-format: Done'
  156. # Check for unstaged changes
  157. $hasChanges = git diff --quiet
  158. if (-not $hasChanges) {
  159. Write-Host 'Reformatted files. Please review and stage the changes.'
  160. Write-Host 'Changes not staged for commit:'
  161. Write-Host
  162. git --no-pager diff --name-only
  163. exit 1
  164. }
  165. # Restore original location
  166. Pop-Location