Skip to content

Commit ae51153

Browse files
Enable ruff-check in pre-commit (#2192)
Signed-off-by: Masaki Kozuki <mkozuki@nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2927f48 commit ae51153

96 files changed

Lines changed: 250 additions & 416 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.pre-commit-config.yaml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ repos:
3838
additional_dependencies: [tomli]
3939
#args: ["--write-changes"] # uncomment if you want to get automatic fixing
4040

41+
- repo: https://github.com/astral-sh/ruff-pre-commit
42+
rev: v0.11.11
43+
hooks:
44+
- id: ruff-check
45+
args: ["--fix"]
46+
4147
- repo: https://github.com/psf/black
4248
rev: 25.1.0
4349
hooks:
@@ -65,12 +71,6 @@ repos:
6571
hooks:
6672
- id: yesqa
6773

68-
# - repo: https://github.com/charliermarsh/ruff-pre-commit
69-
# rev: v0.0.270
70-
# hooks:
71-
# - id: ruff
72-
# args: ["--fix"]
73-
7474
- repo: https://github.com/pre-commit/mirrors-prettier
7575
rev: v3.1.0
7676
hooks:

‎pyproject.toml‎

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,12 @@ line_length = 120
121121
line-length = 120
122122
# Always generate Python 3.10-compatible code.
123123
target-version = "py310"
124-
# Enable Pyflakes `E` and `F` codes by default.
125-
select = [
126-
"E", "W", # see: https://pypi.org/project/pycodestyle
127-
"F", # see: https://pypi.org/project/pyflakes
128-
# "D", # see: https://pypi.org/project/pydocstyle
129-
# "N", # see: https://pypi.org/project/pep8-naming
130-
]
131124
#extend-select = [
132125
# "C4", # see: https://pypi.org/project/flake8-comprehensions
133126
# "PT", # see: https://pypi.org/project/flake8-pytest-style
134127
# "RET", # see: https://pypi.org/project/flake8-return
135128
# "SIM", # see: https://pypi.org/project/flake8-simplify
136129
#]
137-
ignore = [
138-
"E731", # Do not assign a lambda expression, use a def
139-
"E501", # todo: Line too long (235 > 120 characters)
140-
]
141130
# Exclude a variety of commonly ignored directories.
142131
exclude = [
143132
".eggs",
@@ -148,23 +137,60 @@ exclude = [
148137
"_build",
149138
"build",
150139
"dist",
151-
"docs"
140+
"docs",
141+
"examples",
142+
"notebooks",
143+
]
144+
145+
[tool.ruff.lint]
146+
# Enable Pyflakes `E` and `F` codes by default.
147+
select = [
148+
"E", "W", # see: https://pypi.org/project/pycodestyle
149+
"F", # see: https://pypi.org/project/pyflakes
150+
# "D", # see: https://pypi.org/project/pydocstyle
151+
# "N", # see: https://pypi.org/project/pep8-naming
152+
]
153+
ignore = [
154+
"E731", # Do not assign a lambda expression, use a def
155+
"E501", # todo: Line too long (235 > 120 characters)
156+
# TODO(crcrpar): Resolves the following ignores as these are added while enabling ruff check in pre-commit
157+
"F841", # https://docs.astral.sh/ruff/rules/unused-variable/
158+
"F821", # https://docs.astral.sh/ruff/rules/undefined-name/
159+
"E402", # https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file/
160+
"F401", # https://docs.astral.sh/ruff/rules/unused-import/
161+
"F405", # https://docs.astral.sh/ruff/rules/undefined-local-with-import-star-usage/
162+
"E712", # https://docs.astral.sh/ruff/rules/true-false-comparison/
163+
"E711", # https://docs.astral.sh/ruff/rules/none-comparison/
164+
"E721", # https://docs.astral.sh/ruff/rules/type-comparison/
165+
"E722", # https://docs.astral.sh/ruff/rules/bare-except/
166+
"F403", # https://docs.astral.sh/ruff/rules/undefined-local-with-import-star/
167+
"F601", # https://docs.astral.sh/ruff/rules/multi-value-repeated-key-literal/
168+
"F822", # https://docs.astral.sh/ruff/rules/undefined-export/
169+
"F842", # https://docs.astral.sh/ruff/rules/unused-annotation/
170+
"F722", # https://docs.astral.sh/ruff/rules/forward-annotation-syntax-error/
171+
"E702", # https://docs.astral.sh/ruff/rules/multiple-statements-on-one-line-semicolon/
172+
"E741", # https://docs.astral.sh/ruff/rules/ambiguous-variable-name/
173+
"W293", # https://docs.astral.sh/ruff/rules/blank-line-with-whitespace/
174+
"F811", # https://docs.astral.sh/ruff/rules/redefined-while-unused/
175+
"W291", # https://docs.astral.sh/ruff/rules/trailing-whitespace/
152176
]
153-
ignore-init-module-imports = true
154177

155-
[tool.ruff.per-file-ignores]
178+
[tool.ruff.lint.per-file-ignores]
156179
"setup.py" = ["D100", "SIM115"]
157180
"__about__.py" = ["D100"]
158181
"__init__.py" = ["D100"]
182+
# The default is `true` and this option is deprecated as per
183+
# https://docs.astral.sh/ruff/settings/#lint_ignore-init-module-imports
184+
# "ignore-init-module-imports" = true
159185

160-
[tool.ruff.pydocstyle]
186+
[tool.ruff.lint.pydocstyle]
161187
# Use Google-style docstrings.
162188
convention = "google"
163189

164190
#[tool.ruff.pycodestyle]
165191
#ignore-overlong-task-comments = true
166192

167-
[tool.ruff.mccabe]
193+
[tool.ruff.lint.mccabe]
168194
# Unlike Flake8, default to a complexity level of 10.
169195
max-complexity = 10
170196

‎scripts/bisect_nvfuser.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ def run(self, fuel: int) -> int:
6363
high = mid
6464
assert low == high
6565

66-
print(f"Bisecting succeeded. Run the following command as a minimal reproducer:")
66+
print("Bisecting succeeded. Run the following command as a minimal reproducer:")
6767
print(f" {runner.env_var_for_fuel()}={low} {' '.join(args.command_and_args)}")
68-
print(f"The last nvFusion likely triggered the failure.")
68+
print("The last nvFusion likely triggered the failure.")

‎thunder/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282

8383
import thunder.clang as clang
8484
from thunder.core.pytree import tree_flatten, tree_unflatten, tree_map
85+
import thunder.transforms as transforms
8586

8687
# Imports executors (to populate default executors and make them accessible)
8788
import thunder.executors.pythonex
@@ -97,6 +98,7 @@
9798

9899
# TODO RC1 Review exposed names
99100
__all__ = [
101+
"transforms",
100102
# dtype aliases
101103
"bool8",
102104
"uint8",

‎thunder/benchmarks/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,19 +619,19 @@ def run_multiprocess_benchmark(
619619

620620
assert (
621621
torch.distributed.is_available()
622-
), f"Trying to run a distributed benchmark, but torch.distributed is not available"
622+
), "Trying to run a distributed benchmark, but torch.distributed is not available"
623623

624624
# Ensures the benchmark is running on a single CUDA device (which is overridden later)
625625
assert (
626626
len(benchmark.devices) == 1
627627
and Devices.device_from_string(benchmark.devices[0]).devicetype == Devices.DeviceType.CUDA
628-
), f"Distributed benchmarking currently only supports benchmarks that run on a single CUDA device"
628+
), "Distributed benchmarking currently only supports benchmarks that run on a single CUDA device"
629629

630630
# Ensures the benchmark returns a module (because ddp is only supported on modules)
631631
benchmark_fn = benchmark.fn()
632632
assert isinstance(
633633
benchmark_fn, torch.nn.Module
634-
), f"Distributed benchmarking currently only supports module benchmarks"
634+
), "Distributed benchmarking currently only supports module benchmarks"
635635

636636
# Validates world size
637637
assert (

‎thunder/benchmarks/benchmark_litgpt.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def init_model(self):
428428
init_device = torch.device("meta") if self.distributed_mode in FSDP_MODES else self.device
429429
if self.use_hf:
430430
warnings.warn(
431-
f"HuggingFace transformers mode is experimental, many options do not apply. Preliminary testing with transformers==4.50.3."
431+
"HuggingFace transformers mode is experimental, many options do not apply. Preliminary testing with transformers==4.50.3."
432432
)
433433

434434
# for the materialization, we need reset_parameters
@@ -478,7 +478,7 @@ def setup_distributed(self, model):
478478

479479
# Distributed Setup
480480
# TODO: Change compiler call names
481-
if "thunder" in self.compile and not "dynamo" in self.compile:
481+
if "thunder" in self.compile and "dynamo" not in self.compile:
482482
if self.distributed_mode == "ddp":
483483
from thunder.distributed import ddp
484484

@@ -506,7 +506,7 @@ def setup_distributed(self, model):
506506
else:
507507
if self.distributed_mode == "fsdp2":
508508
raise ValueError(
509-
f"To use `fsdp2`, use thunder as torch.compile backend by including dynamo in `--compile` option or set `--compile` to either eager or inductor"
509+
"To use `fsdp2`, use thunder as torch.compile backend by including dynamo in `--compile` option or set `--compile` to either eager or inductor"
510510
)
511511
else:
512512
if self.distributed_mode == "ddp":

‎thunder/benchmarks/einsum.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from collections.abc import Callable
2-
from functools import partial, wraps
32
from collections.abc import Sequence
43

54
import pytest
6-
import torch
75
import thunder
86

97
from thunder.benchmarks import EinsumBenchmark

‎thunder/benchmarks/targets.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
from thunder.core.interpreter import interpret
4949

5050
from thunder.tests.litgpt_model import Config as LitGPTConfig
51-
from thunder.tests.make_tensor import make_tensor
5251
from thunder.benchmarks.utils import backward_only
5352

5453
LIGER_FUSED_SWIGLU_AVAILABLE: bool = package_available("liger_kernel.ops.swiglu")

‎thunder/clang/__init__.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ def _to_tensorproxies(x: list, device: devices.DeviceType):
794794
lambda: f"Advanced indexing currently only supports zero or one-dimensional integer tensors, but found a tensor with dtype {x.dtype} and {x.ndim} dimensions",
795795
)
796796

797-
utils.check(num_ellipses <= 1, lambda: f"Found two or more ellipses in an advanced indexing key")
797+
utils.check(num_ellipses <= 1, lambda: "Found two or more ellipses in an advanced indexing key")
798798

799799
# NOTE When the key has an ellipsis it can be longer than the number of dimensions in a
800800
# (in this case the ellipsis matches no dimensions)
@@ -808,7 +808,7 @@ def _to_tensorproxies(x: list, device: devices.DeviceType):
808808
has_ellipsis: bool = num_ellipses > 0
809809
utils.check(
810810
not has_ellipsis or key[0] is Ellipsis,
811-
lambda: f"Advanced indexing currently only supports ellipses as the first sequence element",
811+
lambda: "Advanced indexing currently only supports ellipses as the first sequence element",
812812
)
813813

814814
# The following models two advanced indexing cases:
@@ -1020,8 +1020,8 @@ def movedim(a: TensorLike, /, source: int | Sequence[int], destination: int | Se
10201020
# Verifies that dims are uniquely specified
10211021
# NOTE This must be done after canonicalization, since canonicalization resolves different ways of specifying the same dim
10221022
src_set = set(src)
1023-
utils.check(len(src_set) == len(src), lambda: f"Found at least one source dimension specified multiple times")
1024-
utils.check(len(set(dst)) == len(dst), lambda: f"Found at least one destination dimension specified multiple times")
1023+
utils.check(len(src_set) == len(src), lambda: "Found at least one source dimension specified multiple times")
1024+
utils.check(len(set(dst)) == len(dst), lambda: "Found at least one destination dimension specified multiple times")
10251025

10261026
# Constructs a permutation that moves the dimensions as requested
10271027
# NOTE Essentially move_dim specifies a partial permutation, where dimensions not explicitly specified as moving
@@ -1318,7 +1318,7 @@ def cat(tensors: list[TensorProxy], dim: int):
13181318
def stack(tensors: list[TensorProxy], dim: int):
13191319
"""Concatenates the given sequence of tensors in a new (the given) dimension."""
13201320
shapes = tuple(t.shape for t in tensors)
1321-
utils.check(shapes, lambda: f"list of tensors cannot be empty")
1321+
utils.check(shapes, lambda: "list of tensors cannot be empty")
13221322
for i, s in enumerate(shapes[1:], start=1):
13231323
utils.check(
13241324
s == shapes[0], lambda: f"tensors must be of the same shape, tensor at {i} is {s} instead of {shapes[0]}"
@@ -1388,7 +1388,7 @@ def matrix_transpose(a: TensorProxy) -> TensorProxy:
13881388
mT_scalar_warning()
13891389
return a
13901390
elif a.ndim == 1:
1391-
raise RuntimeError(f"tensor.mT is only supported on matrices or batches of matrices. Got 1-D tensor.")
1391+
raise RuntimeError("tensor.mT is only supported on matrices or batches of matrices. Got 1-D tensor.")
13921392

13931393
dim0, dim1 = -2, -1
13941394
dim0, dim1 = utils.canonicalize_dims(a.ndim, (dim0, dim1))
@@ -1805,7 +1805,7 @@ def real(a: TensorProxy | Number):
18051805
def imag(a: TensorProxy | Number, /) -> TensorLike:
18061806
utils.check(
18071807
dtypes.is_complex_dtype(dtypes.to_dtype(a)),
1808-
lambda: f"imag is not implemented for tensors with non-complex dtypes",
1808+
lambda: "imag is not implemented for tensors with non-complex dtypes",
18091809
)
18101810

18111811
return _elementwise_unary_wrapper(
@@ -1874,7 +1874,7 @@ def bitwise_xor(a, b):
18741874
def copysign(a, b):
18751875
utils.check(
18761876
not dtypes.is_complex_dtype(dtypes.to_dtype(a)) and not dtypes.is_complex_dtype(dtypes.to_dtype(b)),
1877-
lambda: f"copysign is not defined for complex dtypes",
1877+
lambda: "copysign is not defined for complex dtypes",
18781878
)
18791879

18801880
computation_dtype, result_dtype = utils.elementwise_type_promotion(
@@ -1968,7 +1968,7 @@ def floor_divide(a: TensorProxy | Number, b: TensorProxy | Number) -> TensorProx
19681968
a, b, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
19691969
)
19701970

1971-
utils.check(not dtypes.is_complex_dtype(computation_dtype), lambda: f"Complex floor division is not supported")
1971+
utils.check(not dtypes.is_complex_dtype(computation_dtype), lambda: "Complex floor division is not supported")
19721972

19731973
if dtypes.is_float_dtype(computation_dtype):
19741974
return _floor_divide_float(a, b)

‎thunder/clang/langctx.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from typing import Any
2-
from collections.abc import Callable, Sequence
1+
from collections.abc import Callable
32

43
from thunder.core.langctxs import LanguageContext, register_langctx, Languages, resolve_language
54
from thunder.core.pytree import tree_flatten

0 commit comments

Comments
 (0)