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