diff --git a/.flake8 b/.flake8 index 2d854aa6b25..ea8524f372f 100644 --- a/.flake8 +++ b/.flake8 @@ -12,7 +12,7 @@ ignore = # to line this up with executable bit EXE001, # these ignores are from flake8-bugbear; please fix! - B007,B008,B017,B019,B023,B028,B903,B904,B905,B906,B907 + B007,B008,B017,B019,B023,B028,B903,B904,B905,B906,B907,B908,B910 # these ignores are from flake8-comprehensions; please fix! C407, # these ignores are from flake8-logging-format; please fix! diff --git a/benchmarks/dynamo/genai_layers/utils.py b/benchmarks/dynamo/genai_layers/utils.py index e11995ee0b5..749b9cea203 100644 --- a/benchmarks/dynamo/genai_layers/utils.py +++ b/benchmarks/dynamo/genai_layers/utils.py @@ -134,7 +134,7 @@ class BenchmarkKernel: print( f"Failed to run {backend} backend on {self.name} kernel for {setting} due to {e}" ) - self.available_backends.remove(backend) + self.available_backends.remove(backend) # noqa: B909 continue mem_bytes = self.get_memory_bytes(args_ref, kwargs_ref) perf = Performance(setting, avg_time, mem_bytes) diff --git a/docs/source/conf.py b/docs/source/conf.py index 8b2112c165e..f49968b932d 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -181,7 +181,6 @@ html_theme_options = { theme_variables = pytorch_sphinx_theme2.get_theme_variables() html_context = { - "theme_variables": theme_variables, "github_url": "https://github.com", "github_user": "pytorch", "github_repo": "pytorch", @@ -189,7 +188,7 @@ html_context = { "github_version": "main", "pytorch_project": "docs", "doc_path": "docs/source", - "theme_variables": theme_variables, # noqa: F601 + "theme_variables": theme_variables, # library links are defined in # pytorch_sphinx_theme2/pytorch_sphinx_theme2/links.json "library_links": theme_variables.get("library_links", []), diff --git a/test/distributed/optim/test_zero_redundancy_optimizer.py b/test/distributed/optim/test_zero_redundancy_optimizer.py index 603f671546a..5c90ad8be14 100644 --- a/test/distributed/optim/test_zero_redundancy_optimizer.py +++ b/test/distributed/optim/test_zero_redundancy_optimizer.py @@ -1165,27 +1165,23 @@ class TestZeroRedundancyOptimizerDistributed(TestZeroRedundancyOptimizer): # Increased tolerances are needed to pass when using TF32 # See: https://github.com/pytorch/pytorch/issues/67764 - ( - torch.testing.assert_close( - local_loss.cpu(), - ddp_loss.cpu(), - rtol=1e-03, - atol=1e-08, - ), - "Losses differ between local optimizer and ZeRO", + torch.testing.assert_close( + local_loss.cpu(), + ddp_loss.cpu(), + rtol=1e-03, + atol=1e-08, + msg="Losses differ between local optimizer and ZeRO", ) for local_p, ddp_p in zip( local_model.parameters(), ddp_model.parameters() ): - ( - torch.testing.assert_close( - local_p.cpu(), - ddp_p.cpu(), - rtol=1e-03, - atol=1e-04, - ), - "Models differ after a step", + torch.testing.assert_close( + local_p.cpu(), + ddp_p.cpu(), + rtol=1e-03, + atol=1e-04, + msg="Models differ after a step", ) @skipIfHpu diff --git a/test/distributions/test_distributions.py b/test/distributions/test_distributions.py index 5fd8f71cecd..7cb8cc67813 100644 --- a/test/distributions/test_distributions.py +++ b/test/distributions/test_distributions.py @@ -6369,7 +6369,7 @@ class TestLazyLogitsInitialization(DistributionsTestCase): except NotImplementedError: pass self.assertNotIn("probs", dist.__dict__, msg=message) - dist.batch_shape, dist.event_shape + _ = (dist.batch_shape, dist.event_shape) self.assertNotIn("probs", dist.__dict__, msg=message) def test_lazy_probs_initialization(self): @@ -6386,7 +6386,7 @@ class TestLazyLogitsInitialization(DistributionsTestCase): except NotImplementedError: pass self.assertNotIn("logits", dist.__dict__, msg=message) - dist.batch_shape, dist.event_shape + _ = (dist.batch_shape, dist.event_shape) self.assertNotIn("logits", dist.__dict__, msg=message) diff --git a/test/inductor/test_ordered_set.py b/test/inductor/test_ordered_set.py index cbbb9bda56c..216b8ab0f02 100644 --- a/test/inductor/test_ordered_set.py +++ b/test/inductor/test_ordered_set.py @@ -991,7 +991,7 @@ class TestExceptionPropagation(TestCase): s = OrderedSet([1, 2, 3]) try: for i in s: - s.update([4]) + s.update([4]) # noqa: B909 except RuntimeError: pass else: diff --git a/test/inductor/test_torchinductor.py b/test/inductor/test_torchinductor.py index ffffc68939d..329785792f3 100644 --- a/test/inductor/test_torchinductor.py +++ b/test/inductor/test_torchinductor.py @@ -8863,7 +8863,7 @@ def forward(self, arg0_1: "Sym(s77)", arg1_1: "Sym(s27)", arg2_1: "Sym(s53)", ar with torch.library._scoped_library("mylib", "FRAGMENT") as m: def impl(a, b, c, d, e=2): - (a.add_(b[0] * c * e),) + a.add_(b[0] * c * e) if d is not None: d.add_(b[1]) @@ -8936,7 +8936,7 @@ def forward(self, arg0_1: "Sym(s77)", arg1_1: "Sym(s27)", arg2_1: "Sym(s53)", ar with torch.library._scoped_library("mylib", "FRAGMENT") as m: def impl(a, b, c, d, e=2): - (a.add_(b[0] * c * e),) + a.add_(b[0] * c * e) if d is not None: d.add_(b[1]) return b[0] + b[1] diff --git a/test/onnx/torchlib/ops_test_common.py b/test/onnx/torchlib/ops_test_common.py index 884b66d4e02..54ecbdc1951 100644 --- a/test/onnx/torchlib/ops_test_common.py +++ b/test/onnx/torchlib/ops_test_common.py @@ -246,7 +246,7 @@ def duplicate_opinfo_for_prims( new_opinfo = copy.deepcopy(opinfo) new_opinfo.name = new_name new_opinfo.op = getattr(torch.ops.prims, prims_name) - opinfos.append(new_opinfo) + opinfos.append(new_opinfo) # noqa: B909 return raise RuntimeError(f"OpInfo '{name}' not found in the database.") diff --git a/test/profiler/test_execution_trace.py b/test/profiler/test_execution_trace.py index 267e36ef8f2..74ab3cf6bc3 100644 --- a/test/profiler/test_execution_trace.py +++ b/test/profiler/test_execution_trace.py @@ -1,18 +1,5 @@ # Owner(s): ["oncall: profiler"] -# if tqdm is not shutdown properly, it will leave the monitor thread alive. -# This causes an issue in the multithreading test because we check all events -# in that test with their tids. The events that correspond to these lingering -# threads all have TID of (uint64_t)(-1) which is invalid. -# The work around is turnning off monitoring thread when tqdm is loaded. -# Since these are unit tests, it is safe to turn off monitor thread. -try: - import tqdm - - tqdm.tqdm.monitor_interval = 0 -except ImportError: - pass - import json import os import tempfile @@ -52,6 +39,19 @@ from torch.testing._internal.common_utils import ( from torch.utils._triton import has_triton +# if tqdm is not shutdown properly, it will leave the monitor thread alive. +# This causes an issue in the multithreading test because we check all events +# in that test with their tids. The events that correspond to these lingering +# threads all have TID of (uint64_t)(-1) which is invalid. +# The work around is turnning off monitoring thread when tqdm is loaded. +# Since these are unit tests, it is safe to turn off monitor thread. +try: + import tqdm + + tqdm.tqdm.monitor_interval = 0 +except ImportError: + pass + Json = dict[str, Any] diff --git a/test/profiler/test_record_function.py b/test/profiler/test_record_function.py index 4bc4ad16acb..9ab80c9a07a 100644 --- a/test/profiler/test_record_function.py +++ b/test/profiler/test_record_function.py @@ -1,19 +1,6 @@ # Owner(s): ["oncall: profiler"] # ruff: noqa: F841 -# if tqdm is not shutdown properly, it will leave the monitor thread alive. -# This causes an issue in the multithreading test because we check all events -# in that test with their tids. The events that correspond to these lingering -# threads all have TID of (uint64_t)(-1) which is invalid. -# The work around is turnning off monitoring thread when tqdm is loaded. -# Since these are unit tests, it is safe to turn off monitor thread. -try: - import tqdm - - tqdm.tqdm.monitor_interval = 0 -except ImportError: - None - from typing import Any import torch @@ -29,6 +16,19 @@ from torch.profiler import kineto_available, record_function from torch.testing._internal.common_utils import run_tests, TestCase +# if tqdm is not shutdown properly, it will leave the monitor thread alive. +# This causes an issue in the multithreading test because we check all events +# in that test with their tids. The events that correspond to these lingering +# threads all have TID of (uint64_t)(-1) which is invalid. +# The work around is turnning off monitoring thread when tqdm is loaded. +# Since these are unit tests, it is safe to turn off monitor thread. +try: + import tqdm + + tqdm.tqdm.monitor_interval = 0 +except ImportError: + pass + Json = dict[str, Any] diff --git a/test/profiler/test_torch_tidy.py b/test/profiler/test_torch_tidy.py index e10fe8d0d71..efbd4b8189d 100644 --- a/test/profiler/test_torch_tidy.py +++ b/test/profiler/test_torch_tidy.py @@ -1,18 +1,5 @@ # Owner(s): ["oncall: profiler"] -# if tqdm is not shutdown properly, it will leave the monitor thread alive. -# This causes an issue in the multithreading test because we check all events -# in that test with their tids. The events that correspond to these lingering -# threads all have TID of (uint64_t)(-1) which is invalid. -# The work around is turnning off monitoring thread when tqdm is loaded. -# Since these are unit tests, it is safe to turn off monitor thread. -try: - import tqdm - - tqdm.tqdm.monitor_interval = 0 -except ImportError: - None - import gc import re import textwrap @@ -24,14 +11,25 @@ import torch import torch.nn as nn import torch.optim import torch.utils.data -from torch._C._profiler import _TensorMetadata +from torch._C._profiler import _ExtraFields_PyCall, _TensorMetadata from torch.profiler import _utils, profile from torch.testing._internal.common_utils import run_tests, TestCase -Json = dict[str, Any] +# if tqdm is not shutdown properly, it will leave the monitor thread alive. +# This causes an issue in the multithreading test because we check all events +# in that test with their tids. The events that correspond to these lingering +# threads all have TID of (uint64_t)(-1) which is invalid. +# The work around is turnning off monitoring thread when tqdm is loaded. +# Since these are unit tests, it is safe to turn off monitor thread. +try: + import tqdm -from torch._C._profiler import _ExtraFields_PyCall + tqdm.tqdm.monitor_interval = 0 +except ImportError: + pass + +Json = dict[str, Any] def find_node_with_name(nodes, name): diff --git a/test/quantization/fx/test_quantize_fx.py b/test/quantization/fx/test_quantize_fx.py index 359767f3997..e38c56da2a7 100644 --- a/test/quantization/fx/test_quantize_fx.py +++ b/test/quantization/fx/test_quantize_fx.py @@ -827,7 +827,7 @@ class TestFuseFx(QuantizationTestCase): named_modules = dict(m.named_modules()) for node in m.graph.nodes: if node.op == "call_module" and type(named_modules[node.target]) == torch.nn.Conv2d: - self.assertTrue(len(node.args) == 2), "Expecting the fused op to have two arguments" + self.assertTrue(len(node.args) == 2, msg="Expecting the fused op to have two arguments") def test_fusion_pattern_with_matchallnode(self): """This test tests that the node matched by MatchAllNode will be regared as an input diff --git a/test/test_linalg.py b/test/test_linalg.py index f49db43b4ff..cd58ee57754 100644 --- a/test/test_linalg.py +++ b/test/test_linalg.py @@ -8097,7 +8097,7 @@ scipy_lobpcg | {eq_err_scipy:10.2e} | {eq_err_general_scipy:10.2e} | {iters2: with self.assertWarnsOnceRegex( UserWarning, f"This overload of {func}_ is deprecated"): getattr(out_tensor, func + "_")(1, b1, b2) - self.assertEqual(out_tensor, ref * 2), + self.assertEqual(out_tensor, ref * 2) getattr(res3, func + "_")(b1, b2, beta=1) self.assertEqual(out_tensor, res3) @@ -8113,7 +8113,7 @@ scipy_lobpcg | {eq_err_scipy:10.2e} | {eq_err_general_scipy:10.2e} | {iters2: self.assertEqual(out_tensor, getattr(torch, func)(1, out_tensor, 0, b1, b2)) res4 = getattr(torch, func)(out_tensor, b1, b2, beta=1, alpha=.5) - self.assertEqual(res4, ref * 3), + self.assertEqual(res4, ref * 3) nan = torch.full_like(out_tensor, math.nan) res5 = getattr(torch, func)(nan, b1, b2, beta=0, alpha=1) diff --git a/test/test_multiprocessing_spawn.py b/test/test_multiprocessing_spawn.py index 47c429ba3f9..25eea49b755 100644 --- a/test/test_multiprocessing_spawn.py +++ b/test/test_multiprocessing_spawn.py @@ -201,7 +201,7 @@ class _TestMultiProcessing: try: os.kill(pid, 0) except ProcessLookupError: - pids.remove(pid) + pids.remove(pid) # noqa: B909 break # This assert fails if any nested child process is still diff --git a/test/test_sparse_semi_structured.py b/test/test_sparse_semi_structured.py index 5078649bb00..51fb4aa48c2 100644 --- a/test/test_sparse_semi_structured.py +++ b/test/test_sparse_semi_structured.py @@ -714,16 +714,18 @@ class TestSparseSemiStructuredTraining(TestCase): max_diff = (ref_gemm - pack_gemm).abs().argmax() torch.testing.assert_close( ref_gemm, pack_gemm, - **atol_rtol_kw[dtype] - ), f"packed is wrong at pos: ({max_diff // N}, {max_diff % N})" + **atol_rtol_kw[dtype], + msg=f"packed is wrong at pos: ({max_diff // N}, {max_diff % N})", + ) # Test A.t@B pack_gemm = torch._sparse_semi_structured_linear(b.t(), packed_t, meta_t) max_diff = (ref_gemm - pack_gemm).abs().argmax() torch.testing.assert_close( ref_gemm, pack_gemm, - **atol_rtol_kw[dtype] - ), f"packed_t is wrong at pos: ({max_diff // N}, {max_diff % N})" + **atol_rtol_kw[dtype], + msg=f"packed_t is wrong at pos: ({max_diff // N}, {max_diff % N})", + ) @training_dtypes @unittest.skipIf(TEST_WITH_ROCM, "Not supported on ROCm") diff --git a/torch/_dynamo/variables/ctx_manager.py b/torch/_dynamo/variables/ctx_manager.py index ec5c881c415..09c9f4b7b72 100644 --- a/torch/_dynamo/variables/ctx_manager.py +++ b/torch/_dynamo/variables/ctx_manager.py @@ -807,10 +807,8 @@ class DeterministicAlgorithmsVariable(ContextWrappingVariable): def _call_func(self, tx: "InstructionTranslator", values): assert len(values) == 1 value = values[0] - ( - tx.output.create_node( - "call_function", torch._C._set_deterministic_algorithms, (value,), {} - ), + tx.output.create_node( + "call_function", torch._C._set_deterministic_algorithms, (value,), {} ) torch._C._set_deterministic_algorithms(value) diff --git a/torch/_inductor/pattern_matcher.py b/torch/_inductor/pattern_matcher.py index fd10e643c18..67e6773cf33 100644 --- a/torch/_inductor/pattern_matcher.py +++ b/torch/_inductor/pattern_matcher.py @@ -545,7 +545,7 @@ class _TargetExpr(PatternExpr): fns = [fns] if callable(fns) or isinstance(fns, str) else list(fns) for fn in fns: if isinstance(fn, torch._ops.OpOverloadPacket): - fns.extend(getattr(fn, overload) for overload in fn.overloads()) + fns.extend(getattr(fn, overload) for overload in fn.overloads()) # noqa: B909 self.fns = fns self.fns_set = OrderedSet(fns) diff --git a/torch/_inductor/runtime/triton_heuristics.py b/torch/_inductor/runtime/triton_heuristics.py index 5195770dc24..196dc329e80 100644 --- a/torch/_inductor/runtime/triton_heuristics.py +++ b/torch/_inductor/runtime/triton_heuristics.py @@ -574,7 +574,7 @@ class CachingAutotuner(KernelInterface): assert hasattr(self, "_reload_kernel") assert callable(self._reload_kernel) self.fn = self._reload_kernel().fn - self.compile_results.append(self._precompile_config(new_config)) + self.compile_results.append(self._precompile_config(new_config)) # noqa: B909 self._make_launchers() diff --git a/torch/_inductor/scheduler.py b/torch/_inductor/scheduler.py index 5d4ce3e4f58..92a404e7020 100644 --- a/torch/_inductor/scheduler.py +++ b/torch/_inductor/scheduler.py @@ -3937,7 +3937,7 @@ class Scheduler: if remaining: for rd in remaining: if self.fusable_read_and_write(rd, cd): - remaining.remove(rd) + remaining.remove(rd) # noqa: B909 remaining_deps = OrderedSet( dep.name diff --git a/torch/distributed/checkpoint/default_planner.py b/torch/distributed/checkpoint/default_planner.py index baae0b2bd49..66cdff8a6b7 100644 --- a/torch/distributed/checkpoint/default_planner.py +++ b/torch/distributed/checkpoint/default_planner.py @@ -408,7 +408,7 @@ class _EmptyStateDictLoadPlanner(DefaultLoadPlanner): return True if key in self.keys: - True + return True unflattened_keys: list[str] = [] planner_data = metadata.planner_data.get(key) diff --git a/torch/fx/experimental/shape_inference/infer_symbol_values.py b/torch/fx/experimental/shape_inference/infer_symbol_values.py index d7ff154c16a..1ee6d2a939a 100644 --- a/torch/fx/experimental/shape_inference/infer_symbol_values.py +++ b/torch/fx/experimental/shape_inference/infer_symbol_values.py @@ -64,7 +64,7 @@ def infer_symbol_values( for right_var in right_vars: if sp.sympify(right_var) == sp.sympify("s0"): right_equation = sp.cancel(right_equation / right_var) - right_vars.remove(right_var) + right_vars.remove(right_var) # noqa: B909 var = right_vars[0] idx = symbol_idx_dict[str(var)] diff --git a/torch/fx/passes/split_module.py b/torch/fx/passes/split_module.py index 413584070d1..56ab34d3a87 100644 --- a/torch/fx/passes/split_module.py +++ b/torch/fx/passes/split_module.py @@ -394,7 +394,7 @@ def split_module( root_partition = root_partitions.pop() sorted_partitions.append(root_partition) for dependent in partitions[root_partition].dependents: - partitions[dependent].dependencies.pop(root_partition) + partitions[dependent].dependencies.pop(root_partition) # noqa: B909 if not partitions[dependent].dependencies: root_partitions.append(dependent) if len(sorted_partitions) != len(partitions): diff --git a/torch/testing/_internal/common_methods_invocations.py b/torch/testing/_internal/common_methods_invocations.py index f80da003227..41bb2b96bd9 100644 --- a/torch/testing/_internal/common_methods_invocations.py +++ b/torch/testing/_internal/common_methods_invocations.py @@ -11847,8 +11847,11 @@ op_db: list[OpInfo] = [ safe_val=2)), BinaryUfuncInfo('add', # NumPy has no builtin reference for the alpha kwarg, but it is easy enough to emulate - ref=lambda input, other, *, alpha=1: np.add(input, other) if alpha == 1 \ - else np.add(input, np.multiply(alpha, other)), + ref=lambda input, other, *, alpha=1: ( + np.add(input, other) + if alpha == 1 + else np.add(input, np.multiply(alpha, other)) + ), dtypes=all_types_and_complex_and(torch.bool, torch.bfloat16, torch.float16, torch.chalf), dtypesIfHpu=custom_types(torch.float32, torch.bfloat16, torch.int32), @@ -20498,8 +20501,11 @@ op_db: list[OpInfo] = [ 'jiterator_binary', op=torch.cuda.jiterator._create_jit_fn( "template T binary(T x, T y, T alpha) { return x + alpha * y; }", alpha=1), - ref=lambda input, other, *, alpha=1: np.add(input, other) if alpha == 1 \ - else np.add(input, np.multiply(alpha, other)), + ref=lambda input, other, *, alpha=1: ( + np.add(input, other) + if alpha == 1 + else np.add(input, np.multiply(alpha, other)) + ), dtypes=all_types_and_complex_and(torch.bfloat16, torch.float16, torch.bool), sample_inputs_func=partial(sample_inputs_jiterator, num_inputs=2, alpha=-3.14), supports_out=False, diff --git a/torch/utils/viz/_cycles.py b/torch/utils/viz/_cycles.py index a5aebde06a3..79d8e8b8b17 100644 --- a/torch/utils/viz/_cycles.py +++ b/torch/utils/viz/_cycles.py @@ -336,7 +336,7 @@ def cuda_allocation_context(): def to_dot(nodes): lines = ["digraph GraphName {", "node [shape=rect];", 'rankdir=LR;'] for i, n in enumerate(nodes): - lines.append(f'{i} [label={escape(n.label)}, color={ "red" if n.root else "black"}];') + lines.append(f'{i} [label={escape(n.label)}, color={"red" if n.root else "black"}];') for i, f in enumerate(nodes): for label, j in f.referrents: