Skip to content

Commit 36e6d42

Browse files
PotabkYikun
authored andcommitted
add pre-commit
Signed-off-by: wangli <[email protected]> Signed-off-by: Yikun Jiang <[email protected]>
1 parent ca884ef commit 36e6d42

27 files changed

+738
-584
lines changed

.github/workflows/accuracy_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ jobs:
373373
git push -f origin "${{ env.BRANCH_NAME }}"
374374
375375
- name: Create PR in upstream via API
376-
uses: actions/github-script@v6
376+
uses: actions/github-script@v7
377377
with:
378378
github-token: ${{ secrets.PAT_TOKEN }}
379379
script: |

.github/workflows/pre-commit.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: pre-commit
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
pre-commit:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout vllm-project/vllm-ascend repo
14+
uses: actions/checkout@v4
15+
- uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
16+
with:
17+
python-version: "3.10"
18+
- run: echo "::add-matcher::.github/workflows/matchers/actionlint.json"
19+
- run: echo "::add-matcher::.github/workflows/matchers/mypy.json"
20+
- name: Checkout vllm-project/vllm repo
21+
uses: actions/checkout@v4
22+
with:
23+
repository: vllm-project/vllm
24+
path: ./vllm-empty
25+
- name: Install vllm-ascend dev
26+
run: |
27+
pip install -r requirements-dev.txt --extra-index-url https://download.pytorch.org/whl/cpu
28+
- name: Install vllm
29+
working-directory: vllm-empty
30+
run: |
31+
pip install -r requirements/build.txt --extra-index-url https://download.pytorch.org/whl/cpu
32+
VLLM_TARGET_DEVICE=empty pip install .
33+
- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
34+
env:
35+
SHELLCHECK_OPTS: "--exclude=SC2046,SC2006,SC2086" # Exclude SC2046, SC2006, SC2086 for actionlint
36+
with:
37+
extra_args: --all-files --hook-stage manual

.github/workflows/vllm_ascend_test.yaml

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -67,69 +67,7 @@ concurrency:
6767

6868
jobs:
6969
lint:
70-
# Only trigger lint on pull request
71-
if: ${{ github.event_name == 'pull_request' }}
72-
runs-on: ubuntu-latest
73-
strategy:
74-
matrix:
75-
python-version: ["3.10"]
76-
steps:
77-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
78-
- name: Set up Python ${{ matrix.python-version }}
79-
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
80-
with:
81-
python-version: ${{ matrix.python-version }}
82-
- name: Install dependencies
83-
run: |
84-
python -m pip install --upgrade pip
85-
pip install -r requirements-lint.txt
86-
- name: Run codespell check
87-
run: |
88-
CODESPELL_EXCLUDES=('--skip' 'tests/prompts/**,./benchmarks/sonnet.txt,*tests/lora/data/**,build/**,./vllm_ascend.egg-info/**')
89-
CODESPELL_IGNORE_WORDS=('-L' 'CANN,cann,NNAL,nnal,ASCEND,ascend,EnQue,CopyIn')
90-
91-
codespell --toml pyproject.toml "${CODESPELL_EXCLUDES[@]}" "${CODESPELL_IGNORE_WORDS[@]}"
92-
- name: Analysing the code with ruff
93-
run: |
94-
echo "::add-matcher::.github/workflows/matchers/ruff.json"
95-
ruff check --output-format github .
96-
- name: Run isort
97-
run: |
98-
isort . --check-only
99-
- name: Running yapf
100-
run: |
101-
python -m pip install --upgrade pip
102-
pip install toml
103-
pip install yapf==0.32.0
104-
yapf --diff --recursive .
105-
106-
- name: Install dependencies
107-
run: |
108-
pip install -r requirements-dev.txt --extra-index-url https://download.pytorch.org/whl/cpu
109-
110-
- name: Checkout vllm-project/vllm repo
111-
uses: actions/checkout@v4
112-
with:
113-
repository: vllm-project/vllm
114-
path: vllm-empty
115-
116-
- name: Actionlint Check
117-
env:
118-
SHELLCHECK_OPTS: --exclude=SC2046,SC2006,SC2086
119-
run: |
120-
echo "::add-matcher::.github/workflows/matchers/actionlint.json"
121-
tools/actionlint.sh -color
122-
123-
- name: Install vllm-project/vllm from source
124-
working-directory: vllm-empty
125-
run: |
126-
pip install -r requirements/build.txt --extra-index-url https://download.pytorch.org/whl/cpu
127-
VLLM_TARGET_DEVICE=empty pip install .
128-
129-
- name: Mypy Check
130-
run: |
131-
echo "::add-matcher::.github/workflows/matchers/mypy.json"
132-
tools/mypy.sh 1 ${{ matrix.python-version }}
70+
uses: ./.github/workflows/pre-commit.yml
13371

13472
ut:
13573
needs: [lint]

.pre-commit-config.yaml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
default_install_hook_types:
2+
- pre-commit
3+
- commit-msg
4+
default_stages:
5+
- pre-commit # Run locally
6+
- manual # Run in CI
7+
exclude: 'examples/.*' # Exclude examples from all hooks by default
8+
repos:
9+
- repo: https://github.com/google/yapf
10+
rev: v0.43.0
11+
hooks:
12+
- id: yapf
13+
args: [--in-place, --verbose]
14+
# Keep the same list from yapfignore here to avoid yapf failing without any inputs
15+
exclude: '(.github|benchmarks|examples|docs)/.*'
16+
- repo: https://github.com/astral-sh/ruff-pre-commit
17+
rev: v0.11.7
18+
hooks:
19+
- id: ruff
20+
args: [--output-format, github, --fix]
21+
- id: ruff-format
22+
files: ^(benchmarks|examples)/.*
23+
- repo: https://github.com/crate-ci/typos
24+
rev: v1.32.0
25+
hooks:
26+
- id: typos
27+
- repo: https://github.com/PyCQA/isort
28+
rev: 6.0.1
29+
hooks:
30+
- id: isort
31+
# - repo: https://github.com/pre-commit/mirrors-clang-format
32+
# rev: v20.1.3
33+
# hooks:
34+
# - id: clang-format
35+
# files: ^csrc/.*\.(cpp|hpp|cc|hh|cxx|hxx)$
36+
# types_or: [c++]
37+
# args: [--style=google, --verbose]
38+
# - repo: https://github.com/jackdewinter/pymarkdown
39+
# rev: v0.9.29
40+
# hooks:
41+
# - id: pymarkdown
42+
# args: [fix]
43+
- repo: https://github.com/rhysd/actionlint
44+
rev: v1.7.7
45+
hooks:
46+
- id: actionlint
47+
- repo: local
48+
hooks:
49+
# For local development, you can run mypy using tools/mypy.sh script if needed.
50+
# - id: mypy-local
51+
# name: Run mypy for local Python installation
52+
# entry: tools/mypy.sh 0 "local"
53+
# language: system
54+
# types: [python]
55+
# stages: [pre-commit] # Don't run in CI
56+
- id: mypy-3.9 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
57+
name: Run mypy for Python 3.9
58+
entry: tools/mypy.sh 1 "3.9"
59+
# Use system python because vllm installation is required
60+
language: system
61+
types: [python]
62+
stages: [manual] # Only run in CI
63+
- id: mypy-3.10 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
64+
name: Run mypy for Python 3.10
65+
entry: tools/mypy.sh 1 "3.10"
66+
# Use system python because vllm installation is required
67+
language: system
68+
types: [python]
69+
stages: [manual] # Only run in CI
70+
- id: mypy-3.11 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
71+
name: Run mypy for Python 3.11
72+
entry: tools/mypy.sh 1 "3.11"
73+
# Use system python because vllm installation is required
74+
language: system
75+
types: [python]
76+
stages: [manual] # Only run in CI
77+
- id: mypy-3.12 # TODO: Use https://github.com/pre-commit/mirrors-mypy when mypy setup is less awkward
78+
name: Run mypy for Python 3.12
79+
entry: tools/mypy.sh 1 "3.12"
80+
# Use system python because vllm installation is required
81+
language: system
82+
types: [python]
83+
stages: [manual] # Only run in CI
84+
# FIXME: enable shellcheck
85+
# - id: shellcheck
86+
# name: Lint shell scripts
87+
# entry: tools/shellcheck.sh
88+
# language: script
89+
# types: [shell]
90+
- id: png-lint
91+
name: Lint PNG exports from excalidraw
92+
entry: tools/png-lint.sh
93+
language: script
94+
types: [png]
95+
- id: signoff-commit
96+
name: Sign-off Commit
97+
entry: bash
98+
args:
99+
- -c
100+
- |
101+
if ! grep -q "^Signed-off-by: $(git config user.name) <$(git config user.email)>" "$(git rev-parse --git-path COMMIT_EDITMSG)"; then
102+
printf "\nSigned-off-by: $(git config user.name) <$(git config user.email)>\n" >> "$(git rev-parse --git-path COMMIT_EDITMSG)"
103+
fi
104+
language: system
105+
verbose: true
106+
stages: [commit-msg]
107+
- id: check-filenames
108+
name: Check for spaces in all filenames
109+
entry: bash
110+
args:
111+
- -c
112+
- 'git ls-files | grep " " && echo "Filenames should not contain spaces!" && exit 1 || exit 0'
113+
language: system
114+
always_run: true
115+
pass_filenames: false
116+
- id: enforce-import-regex-instead-of-re
117+
name: Enforce import regex as re
118+
entry: python tools/enforce_regex_import.py
119+
language: python
120+
types: [python]
121+
pass_filenames: false
122+
additional_dependencies: [regex]
123+
# Keep `suggestion` last
124+
- id: suggestion
125+
name: Suggestion
126+
entry: bash -c 'echo "To bypass pre-commit hooks, add --no-verify to git commit."'
127+
language: system
128+
verbose: true
129+
pass_filenames: false
130+
# Insert new entries above the `suggestion` entry

benchmarks/ops/ben_vocabparallelembedding.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
def benchmark_npu(fn, num_iterations=100, num_warmup_iterations=50):
1313
"""
1414
Benchmark function for NPU operations
15-
15+
1616
Args:
1717
fn: Function to benchmark
1818
num_iterations: Number of timing iterations
1919
num_warmup_iterations: Number of warmup iterations
20-
20+
2121
Returns:
2222
float: Minimum elapsed time in seconds
2323
"""
@@ -41,19 +41,26 @@ def benchmark_npu(fn, num_iterations=100, num_warmup_iterations=50):
4141

4242

4343
def get_masked_input_and_mask_ref(
44-
input_: torch.Tensor, org_vocab_start_index: int,
45-
org_vocab_end_index: int, num_org_vocab_padding: int,
46-
added_vocab_start_index: int,
47-
added_vocab_end_index: int) -> Tuple[torch.Tensor, torch.Tensor]:
44+
input_: torch.Tensor,
45+
org_vocab_start_index: int,
46+
org_vocab_end_index: int,
47+
num_org_vocab_padding: int,
48+
added_vocab_start_index: int,
49+
added_vocab_end_index: int,
50+
) -> Tuple[torch.Tensor, torch.Tensor]:
4851
"""Reference implementation for verification"""
49-
org_vocab_mask = (input_ >= org_vocab_start_index) & (input_ <
50-
org_vocab_end_index)
52+
org_vocab_mask = (input_ >= org_vocab_start_index) & (input_ < org_vocab_end_index)
5153
added_vocab_mask = (input_ >= added_vocab_start_index) & (
52-
input_ < added_vocab_end_index)
53-
added_offset = added_vocab_start_index - (
54-
org_vocab_end_index - org_vocab_start_index) - num_org_vocab_padding
55-
valid_offset = (org_vocab_start_index *
56-
org_vocab_mask) + (added_offset * added_vocab_mask)
54+
input_ < added_vocab_end_index
55+
)
56+
added_offset = (
57+
added_vocab_start_index
58+
- (org_vocab_end_index - org_vocab_start_index)
59+
- num_org_vocab_padding
60+
)
61+
valid_offset = (org_vocab_start_index * org_vocab_mask) + (
62+
added_offset * added_vocab_mask
63+
)
5764
vocab_mask = org_vocab_mask | added_vocab_mask
5865
masked_input = vocab_mask * (input_ - valid_offset)
5966
return masked_input, ~vocab_mask
@@ -94,21 +101,25 @@ def test_get_masked_input_and_mask(
94101

95102
# Define reference function
96103
def ref_fn():
97-
return get_masked_input_and_mask_ref(input_tensor,
98-
test_case["org_start"],
99-
test_case["org_end"],
100-
test_case["padding"],
101-
test_case["added_start"],
102-
test_case["added_end"])
104+
return get_masked_input_and_mask_ref(
105+
input_tensor,
106+
test_case["org_start"],
107+
test_case["org_end"],
108+
test_case["padding"],
109+
test_case["added_start"],
110+
test_case["added_end"],
111+
)
103112

104113
# Define custom function
105114
def custom_fn():
106-
return torch.ops._C.get_masked_input_and_mask(input_tensor,
107-
test_case["org_start"],
108-
test_case["org_end"],
109-
test_case["padding"],
110-
test_case["added_start"],
111-
test_case["added_end"])
115+
return torch.ops._C.get_masked_input_and_mask(
116+
input_tensor,
117+
test_case["org_start"],
118+
test_case["org_end"],
119+
test_case["padding"],
120+
test_case["added_start"],
121+
test_case["added_end"],
122+
)
112123

113124
# Get results for correctness testing
114125
ref_masked_input, ref_mask = ref_fn()
@@ -120,9 +131,9 @@ def custom_fn():
120131

121132
# Print performance results
122133
print("\nPerformance Results:")
123-
print(f"Reference implementation: {ref_time*1000:.3f} ms")
124-
print(f"Custom implementation: {custom_time*1000:.3f} ms")
125-
print(f"Speedup: {ref_time/custom_time:.2f}x")
134+
print(f"Reference implementation: {ref_time * 1000:.3f} ms")
135+
print(f"Custom implementation: {custom_time * 1000:.3f} ms")
136+
print(f"Speedup: {ref_time / custom_time:.2f}x")
126137

127138
# Compare results for correctness
128139
ref_masked_input = ref_masked_input.to(dtype)
@@ -136,9 +147,12 @@ def custom_fn():
136147
ref_masked_input,
137148
rtol=1e-5,
138149
atol=1e-5,
139-
msg=f"Masked input mismatch for case: {test_case}")
140-
torch.testing.assert_close(custom_mask,
141-
ref_mask,
142-
rtol=1e-5,
143-
atol=1e-5,
144-
msg=f"Mask mismatch for case: {test_case}")
150+
msg=f"Masked input mismatch for case: {test_case}",
151+
)
152+
torch.testing.assert_close(
153+
custom_mask,
154+
ref_mask,
155+
rtol=1e-5,
156+
atol=1e-5,
157+
msg=f"Mask mismatch for case: {test_case}",
158+
)

0 commit comments

Comments
 (0)