From a471a58d241b08025dcb4ec69c2d30e5a49a757a Mon Sep 17 00:00:00 2001 From: Angel Li Date: Sat, 8 Aug 2026 22:41:23 +0000 Subject: [PATCH] final plain assertion removal (#192517) as title, reviewed claude changes manually, also added a point to claude.md to prevent it from adding plain asserts in the future Pull Request resolved: https://github.com/pytorch/pytorch/pull/192517 Approved by: https://github.com/janeyx99 --- CLAUDE.md | 24 +++++++++++++ benchmarks/distributed/bench_all_to_all_nd.py | 6 ++-- .../dynamo/launch_compile_op_numerics.py | 15 +++++--- .../torch_openreg/torch_openreg/compiler.py | 7 ++-- .../elastic/multiprocessing/api_test.py | 6 ++-- test/distributed/tensor/test_utils.py | 3 +- test/test_scatter_gather_ops.py | 7 ++-- .../torchfuzz/operators/activations.py | 18 ++++++---- .../torchfuzz/operators/bitwise.py | 12 ++++--- .../torchfuzz/operators/comparison.py | 9 +++-- .../torchfuzz/operators/elementwise_math.py | 12 ++++--- .../torchfuzz/operators/logical.py | 6 ++-- .../torchfuzz/operators/loss_functions.py | 9 +++-- .../operators/manipulation_indexing.py | 12 ++++--- .../torchfuzz/operators/reduction.py | 21 ++++++++---- .../torchfuzz/operators/special_functions.py | 9 +++-- .../torchfuzz/operators/tensor_pointwise.py | 3 +- torch/_dynamo/symbolic_convert.py | 13 +++---- torch/_dynamo/variables/nn_module.py | 5 ++- torch/_functorch/_aot_autograd/codegen.py | 8 ++--- .../_aot_autograd/runtime_wrappers.py | 7 ++-- .../_aot_autograd/to_standalone_python.py | 3 +- .../_higher_order_ops/invoke_leaf_function.py | 3 +- torch/_higher_order_ops/wrap.py | 8 ++--- torch/_inductor/codecache.py | 3 +- .../codegen/cuda_combined_scheduling.py | 7 ++-- .../codegen/nv_universal_gemm/kernel_cache.py | 3 +- .../nv_universal_gemm_kernel.py | 3 +- .../nv_universal_gemm_scheduling.py | 31 ++++++++++------- torch/_inductor/codegen/wrapper.py | 3 +- torch/_inductor/fx_passes/pre_grad.py | 3 +- torch/_inductor/fx_passes/reinplace.py | 11 ++++-- torch/_inductor/ir.py | 20 ++++++----- torch/_inductor/scheduler.py | 12 +++++-- torch/cuda/graphs.py | 5 ++- torch/distributed/pipelining/stage.py | 24 ++++++++----- torch/distributed/tensor/_redistribute.py | 7 ++-- .../tensor/experimental/_func_map.py | 9 +++-- torch/export/exported_program.py | 3 +- .../_internal/exporter/_input_observer.py | 34 +++++++++++++------ .../distributed/_tensor/common_dtensor.py | 7 ++-- torch/utils/cpp_extension.py | 3 +- 42 files changed, 277 insertions(+), 137 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index bd3d81a017ea..9da553f79207 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -79,6 +79,30 @@ Generally, use `spin lint` as to run the lint and `spin fixlint` to apply automa When the user asks you to commit or amend, run `lintrunner -a` before creating the commit. Fix any lint errors it reports, then commit. +## Never silence S101 with a noqa + +Ruff's S101 (`Use of assert detected`) must be fixed by rewriting the assert, +never by adding `# noqa: S101`. The lint message itself suggests the noqa; ignore +that suggestion. Plain `assert` is stripped by `python -O`, so a suppressed +assert is a check that silently does nothing in an optimized run. + +```python +# Bad - silences the rule; the check disappears under `python -O` +assert isinstance(x, Foo) # noqa: S101 + +# Good +if not isinstance(x, Foo): + raise AssertionError(f"expected Foo, got {type(x)}") +``` + +Preserve the original message when there is one (`assert cond, msg` -> +`if not cond: raise AssertionError(msg)`); otherwise synthesize a short one that +names what was expected and shows the actual value. Invert the condition rather +than wrapping it in `not (...)` when there is a clean inverse (`is not None` -> +`is None`, `in` -> `not in`, `==` -> `!=`). Do not invert `<`/`>`/`<=`/`>=` on +floats: `not (a < b)` is not `a >= b` when a value is NaN, so keep those as +`if not (a < b)`. + # Git This refines the Bash tool's `# Git` guidance to "branch first" when on the diff --git a/benchmarks/distributed/bench_all_to_all_nd.py b/benchmarks/distributed/bench_all_to_all_nd.py index 7393ccea3057..1ec2028d24d7 100644 --- a/benchmarks/distributed/bench_all_to_all_nd.py +++ b/benchmarks/distributed/bench_all_to_all_nd.py @@ -110,7 +110,8 @@ class AllToAllNdBenchmark(MultiProcContinuousTest): g = dist.get_world_size() dtype = torch.float16 seq_len = _COL_SCATTER_BENCH_SEQ_LEN - assert seq_len % g == 0, f"seq_len ({seq_len}) must be divisible by G ({g})" # noqa: S101 + if seq_len % g != 0: + raise AssertionError(f"seq_len ({seq_len}) must be divisible by G ({g})") rows = seq_len // g local_cols = ( 1024 # row_bytes = local_cols * esize; must be divisible by 16 for the op @@ -190,7 +191,8 @@ class AllToAllNdBenchmark(MultiProcContinuousTest): g = dist.get_world_size() dtype = torch.float16 seq_len = _COL_SCATTER_BENCH_SEQ_LEN - assert seq_len % g == 0, f"seq_len ({seq_len}) must be divisible by G ({g})" # noqa: S101 + if seq_len % g != 0: + raise AssertionError(f"seq_len ({seq_len}) must be divisible by G ({g})") rows = seq_len // g local_cols = 1024 diff --git a/benchmarks/dynamo/launch_compile_op_numerics.py b/benchmarks/dynamo/launch_compile_op_numerics.py index f64bd4f9a600..7f09900ad231 100644 --- a/benchmarks/dynamo/launch_compile_op_numerics.py +++ b/benchmarks/dynamo/launch_compile_op_numerics.py @@ -78,9 +78,12 @@ class Mode: } kwargs["options"] = options return torch.compile(fn, **kwargs) - assert self.backend is None # noqa: S101 - assert self.mode is None # noqa: S101 - assert self.options is None # noqa: S101 + if self.backend is not None: + raise AssertionError("expected self.backend to be None") + if self.mode is not None: + raise AssertionError("expected self.mode to be None") + if self.options is not None: + raise AssertionError("expected self.options to be None") return fn @@ -488,7 +491,8 @@ def sortable_config_key(config): async def worker(args): - assert len(args.gpu) == 1 # noqa: S101 + if len(args.gpu) != 1: + raise AssertionError(f"expected exactly one gpu, got {len(args.gpu)}") # find golden task # a config is a tuple (pytorch_version, cuda_version) # where pytorch_version can either be a version string or nightly or a git hash @@ -932,7 +936,8 @@ def main(): parser.add_argument("--mode") args = parser.parse_args() if args.runner: - assert torch is not None # noqa: S101 + if torch is None: + raise AssertionError("expected torch to be not None") torch.set_default_device("cuda") (gpu,) = args.gpu logging.basicConfig( diff --git a/test/cpp_extensions/open_registration_extension/torch_openreg/torch_openreg/compiler.py b/test/cpp_extensions/open_registration_extension/torch_openreg/torch_openreg/compiler.py index 6dbd7208b64c..4630814614f9 100644 --- a/test/cpp_extensions/open_registration_extension/torch_openreg/torch_openreg/compiler.py +++ b/test/cpp_extensions/open_registration_extension/torch_openreg/torch_openreg/compiler.py @@ -20,9 +20,10 @@ def openreg(gm, example_inputs): if node.op == "placeholder" and "val" in node.meta: fake = node.meta["val"] if isinstance(fake, torch.Tensor): - assert fake.device.type in ("openreg", "cpu"), ( # noqa: S101 - f"Unexpected device {fake.device} in openreg backend" - ) + if fake.device.type not in ("openreg", "cpu"): + raise AssertionError( + f"Unexpected device {fake.device} in openreg backend" + ) gm.graph.eliminate_dead_code() gm.recompile() diff --git a/test/distributed/elastic/multiprocessing/api_test.py b/test/distributed/elastic/multiprocessing/api_test.py index 494820f92997..64d0824f911e 100644 --- a/test/distributed/elastic/multiprocessing/api_test.py +++ b/test/distributed/elastic/multiprocessing/api_test.py @@ -1076,7 +1076,8 @@ class BoundedCloseTest(TestCase): self.assertTrue(join_calls, "expected proc.join() to be called") for t in join_calls: self.assertIsNotNone(t, "proc.join() must be called with a bounded timeout") - assert t is not None # noqa: S101 # for type narrowing + if t is None: # for type narrowing + raise AssertionError("expected t to be not None") self.assertGreater(t, 0) def test_subprocess_context_close_bounded_when_unkillable(self): @@ -1117,7 +1118,8 @@ class BoundedCloseTest(TestCase): self.assertTrue(wait_calls, "expected proc.wait() to be called") for t in wait_calls: self.assertIsNotNone(t, "proc.wait() must be called with a bounded timeout") - assert t is not None # noqa: S101 # for type narrowing + if t is None: # for type narrowing + raise AssertionError("expected t to be not None") self.assertGreater(t, 0) diff --git a/test/distributed/tensor/test_utils.py b/test/distributed/tensor/test_utils.py index de1b27e43804..040bd91ca9c4 100644 --- a/test/distributed/tensor/test_utils.py +++ b/test/distributed/tensor/test_utils.py @@ -1792,7 +1792,8 @@ class TestStridedShardAlltoAll(TestStridedShardCollectiveOpUtils, LocalTensorTes target_tensor_dim: int, ) -> torch.Tensor: """Perform alltoall redistribution to a new shard dimension.""" - assert isinstance(shard_spec, _StridedShard) # noqa: S101 + if not isinstance(shard_spec, _StridedShard): + raise AssertionError(f"expected _StridedShard, got {type(shard_spec)}") return shard_spec._to_new_shard_dim( local_tensor, mesh, mesh_dim, logical_shape, target_tensor_dim ) diff --git a/test/test_scatter_gather_ops.py b/test/test_scatter_gather_ops.py index ea88ce4fd756..0a0b6d46ccb1 100644 --- a/test/test_scatter_gather_ops.py +++ b/test/test_scatter_gather_ops.py @@ -634,9 +634,10 @@ def _misaligned_view(rows, cols, dtype): # Sanity check: if the allocator ever hands us a base such that the # +1-element slice is still 16B-aligned, the test silently passes # without exercising the misalignment path. - assert t.data_ptr() % 16 != 0, ( # noqa: S101 - f"test bug: data_ptr() should be misaligned, got {t.data_ptr() % 16=}" - ) + if t.data_ptr() % 16 == 0: + raise AssertionError( + f"test bug: data_ptr() should be misaligned, got {t.data_ptr() % 16=}" + ) return t diff --git a/tools/experimental/torchfuzz/operators/activations.py b/tools/experimental/torchfuzz/operators/activations.py index 0904838440cd..2f7127eed4c5 100644 --- a/tools/experimental/torchfuzz/operators/activations.py +++ b/tools/experimental/torchfuzz/operators/activations.py @@ -132,7 +132,8 @@ class CeluOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._alpha = round(random.uniform(0.01, 5.0), 4) return [ TensorSpec( @@ -178,7 +179,8 @@ class SoftshrinkOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._lambd = round(random.uniform(0.0, 2.0), 4) return [ TensorSpec( @@ -225,7 +227,8 @@ class HardtanhOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") a = round(random.uniform(-3.0, 3.0), 4) b = round(random.uniform(-3.0, 3.0), 4) lo, hi = (a, b) if a < b else (b, a) @@ -278,7 +281,8 @@ class SoftplusOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._beta = round(random.uniform(0.1, 5.0), 4) self._threshold = round(random.uniform(5.0, 30.0), 4) return [ @@ -325,7 +329,8 @@ class ThresholdOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._threshold = round(random.uniform(-3.0, 3.0), 4) self._value = round(random.uniform(-3.0, 3.0), 4) return [ @@ -375,7 +380,8 @@ class PreluOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") input_spec = TensorSpec( size=output_spec.size, stride=output_spec.stride, diff --git a/tools/experimental/torchfuzz/operators/bitwise.py b/tools/experimental/torchfuzz/operators/bitwise.py index a42d059e466b..6157fa89ff98 100644 --- a/tools/experimental/torchfuzz/operators/bitwise.py +++ b/tools/experimental/torchfuzz/operators/bitwise.py @@ -84,7 +84,8 @@ class BitwiseBinaryOperatorBase(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") spec = TensorSpec( size=output_spec.size, stride=output_spec.stride, @@ -141,7 +142,8 @@ class BitwiseShiftOperatorBase(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") spec = TensorSpec( size=output_spec.size, stride=output_spec.stride, @@ -170,7 +172,8 @@ class BitwiseShiftOperatorBase(Operator): input_names: list[str], output_spec: Spec, ) -> str: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") if _CLAMP_SHIFT_COUNTS: bw = _BIT_WIDTHS[output_spec.dtype] shift_expr = f"torch.clamp({input_names[1]}, 0, {bw - 1})" @@ -235,7 +238,8 @@ class BitwiseNotOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") return [ TensorSpec( size=output_spec.size, diff --git a/tools/experimental/torchfuzz/operators/comparison.py b/tools/experimental/torchfuzz/operators/comparison.py index 8e5a964d07d1..07c152cdd6e7 100644 --- a/tools/experimental/torchfuzz/operators/comparison.py +++ b/tools/experimental/torchfuzz/operators/comparison.py @@ -102,7 +102,8 @@ class ComparisonOperatorBase(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype == torch.bool def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") dtype_a = random.choice(_NUMERIC_INPUT_DTYPES) dtype_b = random.choice(_NUMERIC_INPUT_DTYPES) input_dtypes = [dtype_a, dtype_b] @@ -247,7 +248,8 @@ class WhereOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._form = random.choice(("A", "B", "C")) self._scalar_value = _random_scalar_for_dtype(output_spec.dtype) cond_spec = TensorSpec( @@ -288,7 +290,8 @@ class WhereOperator(Operator): input_names: list[str], output_spec: Spec, ) -> str: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") scalar_val = self._scalar_value if self._form == "A": out = ( diff --git a/tools/experimental/torchfuzz/operators/elementwise_math.py b/tools/experimental/torchfuzz/operators/elementwise_math.py index e2d71e081430..ea338d939fe0 100644 --- a/tools/experimental/torchfuzz/operators/elementwise_math.py +++ b/tools/experimental/torchfuzz/operators/elementwise_math.py @@ -581,7 +581,8 @@ class AddcmulOperator(TernaryElementwiseOperatorBase): def _clamp_bound(output_spec: Spec) -> float | int: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") bound = round(random.uniform(-10.0, 10.0), 4) if output_spec.dtype not in FLOAT_DTYPES: bound = int(bound) @@ -604,7 +605,8 @@ class ClampMaxOperator(Operator): return output_spec.dtype != torch.bool def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") return [ TensorSpec( size=output_spec.size, @@ -639,7 +641,8 @@ class ClampMinOperator(Operator): return output_spec.dtype != torch.bool def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") return [ TensorSpec( size=output_spec.size, @@ -715,7 +718,8 @@ class LdexpOperator(Operator): return is_float_dtype(output_spec.dtype) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") return [ TensorSpec( size=output_spec.size, diff --git a/tools/experimental/torchfuzz/operators/logical.py b/tools/experimental/torchfuzz/operators/logical.py index 8fdb0a1d1ed2..9081cc871a23 100644 --- a/tools/experimental/torchfuzz/operators/logical.py +++ b/tools/experimental/torchfuzz/operators/logical.py @@ -49,7 +49,8 @@ class LogicalBinaryOperatorBase(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype == torch.bool def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") dtype = random.choice(_LOGICAL_INPUT_DTYPES) spec = TensorSpec( size=output_spec.size, @@ -118,7 +119,8 @@ class LogicalNotOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype == torch.bool def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") dtype = random.choice(_LOGICAL_INPUT_DTYPES) return [ TensorSpec( diff --git a/tools/experimental/torchfuzz/operators/loss_functions.py b/tools/experimental/torchfuzz/operators/loss_functions.py index 226b9a17065c..560dffe1c5f9 100644 --- a/tools/experimental/torchfuzz/operators/loss_functions.py +++ b/tools/experimental/torchfuzz/operators/loss_functions.py @@ -69,7 +69,8 @@ class _SameShapeFloatLossBase(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") spec = TensorSpec( size=output_spec.size, stride=output_spec.stride, @@ -286,7 +287,8 @@ class NllLossOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") n = output_spec.size[0] self._num_classes = random.randint(2, 10) c = self._num_classes @@ -345,7 +347,8 @@ class CrossEntropyOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") n = output_spec.size[0] self._num_classes = random.randint(2, 10) c = self._num_classes diff --git a/tools/experimental/torchfuzz/operators/manipulation_indexing.py b/tools/experimental/torchfuzz/operators/manipulation_indexing.py index ecec5e81cb80..74d3c975fc86 100644 --- a/tools/experimental/torchfuzz/operators/manipulation_indexing.py +++ b/tools/experimental/torchfuzz/operators/manipulation_indexing.py @@ -102,7 +102,8 @@ class ConjPhysicalOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype != torch.bool def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") return [ TensorSpec( size=output_spec.size, @@ -141,7 +142,8 @@ class FillOperator(Operator): return isinstance(output_spec, TensorSpec) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._value = _random_scalar_for_dtype(output_spec.dtype) return [ TensorSpec( @@ -194,7 +196,8 @@ class FlipOperator(Operator): return isinstance(output_spec, TensorSpec) and len(output_spec.size) >= 1 def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") ndim = len(output_spec.size) k = random.randint(1, ndim) self._dims = tuple(sorted(random.sample(range(ndim), k))) @@ -249,7 +252,8 @@ class MaskedFillOperator(Operator): return isinstance(output_spec, TensorSpec) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._value = _random_scalar_for_dtype(output_spec.dtype) input_spec = TensorSpec( size=output_spec.size, diff --git a/tools/experimental/torchfuzz/operators/reduction.py b/tools/experimental/torchfuzz/operators/reduction.py index d5cf5f1fe0d0..dd7fe064cc95 100644 --- a/tools/experimental/torchfuzz/operators/reduction.py +++ b/tools/experimental/torchfuzz/operators/reduction.py @@ -119,7 +119,8 @@ class _CumulativeReductionBase(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._dim = random.randrange(len(output_spec.size)) return [ TensorSpec( @@ -271,7 +272,8 @@ class _FullOrKeepdimReductionBase(Operator): return _random_input_shape() def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._input_dtype = self._pick_input_dtype(output_spec.dtype) if output_spec.size == (): self._mode = "scalar" @@ -311,7 +313,8 @@ class _FullOrKeepdimReductionBase(Operator): input_names: list[str], output_spec: Spec, ) -> str: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") mode = self._mode dim = self._dim keepdim = self._keepdim @@ -530,7 +533,8 @@ class _DimRequiredTupleReductionBase(Operator): return self.supports_keepdim_false def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") if 1 in output_spec.size and ( not self.supports_keepdim_false or random.random() < 0.5 ): @@ -647,7 +651,8 @@ class CountNonzeroOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._input_size = _random_input_shape() self._input_dtype = random.choice(_BOOL_OR_NUMERIC_INPUT_DTYPES) return [ @@ -690,7 +695,8 @@ class AminmaxOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._input_size = _random_input_shape() return [ TensorSpec( @@ -734,7 +740,8 @@ class VarMeanOperator(Operator): ) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._correction = random.choice([0, 1, 2]) if self._correction == 0: self._input_size = _random_input_shape() diff --git a/tools/experimental/torchfuzz/operators/special_functions.py b/tools/experimental/torchfuzz/operators/special_functions.py index b7978bd6e6f3..5d8b8b5bcb5a 100644 --- a/tools/experimental/torchfuzz/operators/special_functions.py +++ b/tools/experimental/torchfuzz/operators/special_functions.py @@ -247,7 +247,8 @@ class IgammaOperator(Operator): return isinstance(output_spec, TensorSpec) and output_spec.dtype in FLOAT_DTYPES def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") spec = TensorSpec( size=output_spec.size, stride=output_spec.stride, @@ -271,7 +272,8 @@ class IgammaOperator(Operator): input_names: list[str], output_spec: Spec, ) -> str: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") a_min = _IGAMMA_A_MIN[output_spec.dtype] return ( f"{output_name} = torch.igamma(" @@ -309,7 +311,8 @@ class ChebyshevPolynomialOperatorBase(Operator): return output_spec.dtype in (torch.float32, torch.float64) def fuzz_inputs_specs(self, output_spec: Spec) -> list[Spec]: - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") self._n = random.randint(0, 5) return [ TensorSpec( diff --git a/tools/experimental/torchfuzz/operators/tensor_pointwise.py b/tools/experimental/torchfuzz/operators/tensor_pointwise.py index 1da7d1be8e6e..8f241cb0450e 100644 --- a/tools/experimental/torchfuzz/operators/tensor_pointwise.py +++ b/tools/experimental/torchfuzz/operators/tensor_pointwise.py @@ -240,7 +240,8 @@ class ClampOperator(Operator): # For integer-dtype tensors, emit integer bounds so the fuzzer doesn't # generate float-bound + int-tensor mismatches between CPU and MTIA. - assert isinstance(output_spec, TensorSpec) # noqa: S101 + if not isinstance(output_spec, TensorSpec): + raise AssertionError(f"expected TensorSpec, got {type(output_spec)}") if output_spec.dtype in FLOAT_DTYPES: min_literal, max_literal = "-1.0", "1.0" else: diff --git a/torch/_dynamo/symbolic_convert.py b/torch/_dynamo/symbolic_convert.py index c494c168d1d3..05a544d2f8be 100644 --- a/torch/_dynamo/symbolic_convert.py +++ b/torch/_dynamo/symbolic_convert.py @@ -547,11 +547,11 @@ def get_assert_bytecode_sequence(with_msg: bool) -> list[str]: if with_msg: def fn(x: Any) -> None: - assert x, "msg" # noqa: S101 + assert x, "msg" # noqa: S101 # function is to recognize assert statements in user code else: def fn(x: Any) -> None: - assert x # noqa: S101 + assert x # noqa: S101 # function is to recognize assert statements in user code insts = [inst.opname for inst in dis.get_instructions(fn)] @@ -3360,10 +3360,11 @@ class InstructionTranslatorBase( def LOAD_ATTR(self, inst: Instruction) -> None: if sys.version_info >= (3, 12): - assert inst.arg is not None and inst.arg % 2 == 0, ( # noqa: S101 - "LOAD_ATTR method variant should have been normalized by " - "remove_load_attr_method_variant in cleaned_instructions" - ) + if inst.arg is None or inst.arg % 2 != 0: + raise AssertionError( + "LOAD_ATTR method variant should have been normalized by " + "remove_load_attr_method_variant in cleaned_instructions" + ) self._load_attr(inst.argval) @break_graph_if_unsupported( diff --git a/torch/_dynamo/variables/nn_module.py b/torch/_dynamo/variables/nn_module.py index 8f0cc56d6eef..a42c38d26a51 100644 --- a/torch/_dynamo/variables/nn_module.py +++ b/torch/_dynamo/variables/nn_module.py @@ -117,7 +117,10 @@ def initialize_lazy_module( s.node.hint if isinstance(s, torch.SymInt) else s for s in fake.shape ] - assert all(isinstance(s, int) for s in shape), shape # noqa: S101 + if not all(isinstance(s, int) for s in shape): + raise AssertionError( + f"expected all shape entries to be int, got {shape}" + ) return torch.empty( # pyrefly: ignore[no-matching-overload] shape, dtype=fake.dtype, device=fake.device ) diff --git a/torch/_functorch/_aot_autograd/codegen.py b/torch/_functorch/_aot_autograd/codegen.py index 42ede01a5ddd..d35f98523c50 100644 --- a/torch/_functorch/_aot_autograd/codegen.py +++ b/torch/_functorch/_aot_autograd/codegen.py @@ -87,15 +87,15 @@ class PySourceBuilder(IndentedBuffer): def getvalue(self) -> str: # Join without a trailing newline (unlike IndentedBuffer.getvalue). - assert all(isinstance(li, str) for li in self._lines) # noqa: S101 + if not all(isinstance(li, str) for li in self._lines): + raise AssertionError(f"expected all lines to be str, got {self._lines}") return "\n".join(self._lines) # type: ignore[arg-type] def build( self, *, wrapped_fn: Callable[..., object] | None = None ) -> Callable[..., object]: - assert self._fn_name is not None and self._artifact_name is not None, ( # noqa: S101 - "build() requires fn_name and artifact_name" - ) + if self._fn_name is None or self._artifact_name is None: + raise AssertionError("build() requires fn_name and artifact_name") return _compile_and_exec_source( self.getvalue(), self.globals, diff --git a/torch/_functorch/_aot_autograd/runtime_wrappers.py b/torch/_functorch/_aot_autograd/runtime_wrappers.py index 123258d51a60..2fcc5a964c3f 100644 --- a/torch/_functorch/_aot_autograd/runtime_wrappers.py +++ b/torch/_functorch/_aot_autograd/runtime_wrappers.py @@ -1108,9 +1108,10 @@ def _create_runtime_wrapper( f"{oi}.as_strided_({ui}.size(), {ui}.stride(), {ui}.storage_offset())" ) else: - assert meta.mutates_data, ( # noqa: S101 - f"expected mutates_data for input {inpt_idx}" - ) + if not meta.mutates_data: + raise AssertionError( + f"expected mutates_data for input {inpt_idx}" + ) if meta.is_leaf: buf.writeline( f"if {oi}.requires_grad: {oi}.detach().copy_({ui})" diff --git a/torch/_functorch/_aot_autograd/to_standalone_python.py b/torch/_functorch/_aot_autograd/to_standalone_python.py index 3f34da91db35..218897c1c1be 100644 --- a/torch/_functorch/_aot_autograd/to_standalone_python.py +++ b/torch/_functorch/_aot_autograd/to_standalone_python.py @@ -247,7 +247,8 @@ def _resolve_global( # closure as its inner. That closure is not a captured wrapper and has no import # path, so wire it to the single-arg orchestration entry adapter the composer emits. if orch_closure_id is not None and id(obj) == orch_closure_id: - assert orch_entry_name is not None # noqa: S101 + if orch_entry_name is None: + raise AssertionError("expected orch_entry_name to be not None") return orch_entry_name if id(obj) in fn_id_to_name: return fn_id_to_name[id(obj)] diff --git a/torch/_higher_order_ops/invoke_leaf_function.py b/torch/_higher_order_ops/invoke_leaf_function.py index fec1e04d0157..9a57edbbd91b 100644 --- a/torch/_higher_order_ops/invoke_leaf_function.py +++ b/torch/_higher_order_ops/invoke_leaf_function.py @@ -709,7 +709,8 @@ class InvokeLeafFunctionAutogradOp(torch.autograd.Function): hook_real = getattr(real_fn_callable, "_leaf_hook_real_fn", None) hook_fake = getattr(real_fn_callable, "_leaf_hook_fake_fn", None) if hook_real is not None: - assert hook_fake is not None # noqa: S101 + if hook_fake is None: + raise AssertionError("expected hook_fake to be not None") hook_captured_out_spec: list[pytree.TreeSpec | None] = [None] wrapped_hook_real, wrapped_hook_fake = make_leaf_function_wrappers( hook_real, hook_fake, hook_captured_out_spec diff --git a/torch/_higher_order_ops/wrap.py b/torch/_higher_order_ops/wrap.py index aa987a80b983..cf3711ffecf6 100644 --- a/torch/_higher_order_ops/wrap.py +++ b/torch/_higher_order_ops/wrap.py @@ -131,7 +131,8 @@ class InductorCodeSideTable: def get_callable(self, idx: int) -> InductorCompiledCallable: """Get the callable at the given index.""" - assert idx in self.id_to_callable, f"Invalid inductor code index: {idx}" # noqa: S101 + if idx not in self.id_to_callable: + raise AssertionError(f"Invalid inductor code index: {idx}") return self.id_to_callable[idx] def __getstate__(self): @@ -160,9 +161,8 @@ def _resolve_inductor_callable( """ if isinstance(func, int): return inductor_code_side_table.get_callable(func) - assert isinstance(func, InductorCompiledCallable), ( # noqa: S101 - f"Unexpected func type: {type(func)}" - ) + if not isinstance(func, InductorCompiledCallable): + raise AssertionError(f"Unexpected func type: {type(func)}") return func diff --git a/torch/_inductor/codecache.py b/torch/_inductor/codecache.py index d7dfbd7935df..6eaa373b5cee 100644 --- a/torch/_inductor/codecache.py +++ b/torch/_inductor/codecache.py @@ -2244,7 +2244,8 @@ class FxGraphCache(GuardedCache[CompiledFxGraph]): # Now re-evaluate with the symints to add any guards to the current env. if graph.guards_expr: check = bool(evaluate_guards(graph.guards_expr, symints)) - assert check is True # noqa: S101 + if check is not True: + raise AssertionError(f"expected check to be True, got {check}") log.debug( "fx graph cache key %s post-load guards: %s", key, shape_env.guards ) diff --git a/torch/_inductor/codegen/cuda_combined_scheduling.py b/torch/_inductor/codegen/cuda_combined_scheduling.py index fb2fda5daae1..e2636b543295 100644 --- a/torch/_inductor/codegen/cuda_combined_scheduling.py +++ b/torch/_inductor/codegen/cuda_combined_scheduling.py @@ -183,9 +183,10 @@ class CUDACombinedScheduling(BaseScheduling): elif self._nv_universal_gemm_scheduling.is_nv_universal_gemm_template( template_node ): - assert not prologue_nodes, ( # noqa: S101 - "NVIDIA Universal GEMM doesn't support prologue fusion yet" - ) + if prologue_nodes: + raise AssertionError( + "NVIDIA Universal GEMM doesn't support prologue fusion yet" + ) return self._nv_universal_gemm_scheduling.codegen_template( template_node, epilogue_nodes, prologue_nodes ) diff --git a/torch/_inductor/codegen/nv_universal_gemm/kernel_cache.py b/torch/_inductor/codegen/nv_universal_gemm/kernel_cache.py index 3d442d733bde..09fb1b38a7e2 100644 --- a/torch/_inductor/codegen/nv_universal_gemm/kernel_cache.py +++ b/torch/_inductor/codegen/nv_universal_gemm/kernel_cache.py @@ -118,7 +118,8 @@ def _get_kernel_cache() -> dict[str, Any]: """ _ensure_caches() cache = _kernel_by_name_cache - assert cache is not None # noqa: S101 + if cache is None: + raise AssertionError("expected cache to be not None") return cache diff --git a/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_kernel.py b/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_kernel.py index 86d01ee6c478..345469e60e20 100644 --- a/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_kernel.py +++ b/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_kernel.py @@ -654,7 +654,8 @@ def _update_reuse_args_tensors( wrapper._runtime_tensor = getattr(val, "runtime_tensor", val) local_reduce = getattr(args, "local_reduce_out", None) if local_reduce is not None: - assert args_kwargs is not None # noqa: S101 + if args_kwargs is None: + raise AssertionError("expected args_kwargs to be not None") local_reduce._runtime_tensor = args_kwargs["local_reduce_out"] return True diff --git a/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_scheduling.py b/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_scheduling.py index c68ea26f5108..16f2094cd8d6 100644 --- a/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_scheduling.py +++ b/torch/_inductor/codegen/nv_universal_gemm/nv_universal_gemm_scheduling.py @@ -139,7 +139,8 @@ class NVUniversalGemmScheduling(BaseScheduling): node: BaseSchedulerNode, require_epilogue_fusion: bool = False ) -> NVUniversalGemmBuffer: """Extract NVUniversalGemmBuffer from node (direct or via MultiTemplateBuffer).""" - assert isinstance(node, SchedulerNode) # noqa: S101 + if not isinstance(node, SchedulerNode): + raise AssertionError(f"expected SchedulerNode, got {type(node)}") ir_node = node.node if isinstance(ir_node, NVUniversalGemmBuffer): @@ -718,18 +719,21 @@ class NVUniversalGemmScheduling(BaseScheduling): [n.get_name() for n in epilogue_nodes] if epilogue_nodes else [], [n.get_name() for n in prologue_nodes] if prologue_nodes else [], ) - assert self.is_nv_universal_gemm_template(template_node), ( # noqa: S101 - "Template node passed to NVUniversalGemmScheduling.codegen_template must be a " - "SchedulerNode that wraps a NVUniversalGemmBuffer or MultiTemplateBuffer with NVGEMM choice" - ) - assert not prologue_nodes, ( # noqa: S101 - "NVIDIA Universal GEMM doesn't support prologue fusion yet" - ) + if not self.is_nv_universal_gemm_template(template_node): + raise AssertionError( + "Template node passed to NVUniversalGemmScheduling.codegen_template must be a " + "SchedulerNode that wraps a NVUniversalGemmBuffer or MultiTemplateBuffer with NVGEMM choice" + ) + if prologue_nodes: + raise AssertionError( + "NVIDIA Universal GEMM doesn't support prologue fusion yet" + ) template_node = cast(SchedulerNode, template_node) original_ir_node = template_node.node - assert isinstance(original_ir_node, Buffer) # noqa: S101 + if not isinstance(original_ir_node, Buffer): + raise AssertionError(f"expected Buffer, got {type(original_ir_node)}") original_buffer_name = original_ir_node.get_name() ctb: NVUniversalGemmBuffer = self.get_nv_gemm_buffer_from_node( @@ -853,7 +857,8 @@ class NVUniversalGemmScheduling(BaseScheduling): log.warning("NVGEMM epilogue codegen failed unexpectedly: %s", e) raise - assert ctb.make_kernel_render is not None # noqa: S101 # noqa: S101 + if ctb.make_kernel_render is None: + raise AssertionError("expected ctb.make_kernel_render to be not None") kernel, render = ctb.make_kernel_render( ctb, epilogue_fn_code=epilogue_fn_code, @@ -982,7 +987,8 @@ class NVUniversalGemmScheduling(BaseScheduling): output_bufs: list[str] = [] if epilogue: template_sn = cast(SchedulerNode, template) - assert isinstance(template_sn.node, Buffer) # noqa: S101 + if not isinstance(template_sn.node, Buffer): + raise AssertionError(f"expected Buffer, got {type(template_sn.node)}") original_buffer_name = template_sn.node.get_name() plan = self._epilogue_plan(template_sn.node, epilogue) evt_nodes = [] if plan.feed_main is not None else plan.evt_nodes @@ -1022,7 +1028,8 @@ class NVUniversalGemmScheduling(BaseScheduling): only_gen_src_code=True, ) - assert src_code is not None # noqa: S101 # noqa: S101 + if src_code is None: + raise AssertionError("expected src_code to be not None") src_code = src_code.replace( str(Placeholder.KERNEL_NAME), _BENCHMARK_KERNEL_PREFIX ) diff --git a/torch/_inductor/codegen/wrapper.py b/torch/_inductor/codegen/wrapper.py index 5e98df9d1ef7..2f206f966acb 100644 --- a/torch/_inductor/codegen/wrapper.py +++ b/torch/_inductor/codegen/wrapper.py @@ -3568,7 +3568,8 @@ class PythonWrapperCodegen(CodeGen): } if backend_option_candidates: get_interface_for_device(device).raise_if_triton_unavailable(device) - assert GPUTarget is not None # noqa: S101 + if GPUTarget is None: + raise AssertionError("expected GPUTarget to be not None") target = GPUTarget( device_props.type, device_props.cc, diff --git a/torch/_inductor/fx_passes/pre_grad.py b/torch/_inductor/fx_passes/pre_grad.py index 149a65749778..61305837c005 100644 --- a/torch/_inductor/fx_passes/pre_grad.py +++ b/torch/_inductor/fx_passes/pre_grad.py @@ -396,7 +396,8 @@ def pre_grad_passes( ) # we support run same pattern multiple times, the default is to run only once counter_raw = fusion_options[pass_name].get("counter", 1) - assert isinstance(counter_raw, int) # noqa: S101 + if not isinstance(counter_raw, int): + raise AssertionError(f"expected int, got {type(counter_raw)}") for _ in range(counter_raw): pattern_matcher_pass.apply(gm.graph) # type: ignore[arg-type] if not is_same_dict(counters["inductor"], inductor_before_change): diff --git a/torch/_inductor/fx_passes/reinplace.py b/torch/_inductor/fx_passes/reinplace.py index 60a84a36456d..2e22e407d7f2 100644 --- a/torch/_inductor/fx_passes/reinplace.py +++ b/torch/_inductor/fx_passes/reinplace.py @@ -160,7 +160,10 @@ def _decompose_scatter_functional( view_updated = aten.slice_scatter(view, src, 1, 10, -10) inp_updated = aten.slice_scatter(inp, view_updated, 0, 0, 10) """ - assert node.target is _generalized_scatter # noqa: S101 + if node.target is not _generalized_scatter: + raise AssertionError( + f"expected node.target to be _generalized_scatter, got {node.target}" + ) return _decompose_scatter_functional_helper(graph, *node.args) # type: ignore[arg-type] @@ -179,9 +182,11 @@ def _decompose_scatter_mutating( slice2.copy_(src) """ - assert node.target in (_generalized_scatter, _inplace_generalized_scatter) # noqa: S101 + if node.target not in (_generalized_scatter, _inplace_generalized_scatter): + raise AssertionError(f"unexpected node.target: {node.target}") inp, src, view_ops = node.args - assert not node.kwargs # noqa: S101 + if node.kwargs: + raise AssertionError(f"expected no kwargs, got {node.kwargs}") if node.target is _generalized_scatter: inp = graph_call_function(graph, aten.clone, inp) diff --git a/torch/_inductor/ir.py b/torch/_inductor/ir.py index 30a7d4d7b553..e22a795a27a2 100644 --- a/torch/_inductor/ir.py +++ b/torch/_inductor/ir.py @@ -6402,8 +6402,10 @@ class MultiTemplateBuffer(TritonTemplateBuffer): def swap_as_nvgemm_caller(self, caller: ChoiceCaller) -> Iterator[None]: from torch._inductor.codegen.nv_universal_gemm import NVUniversalGemmCaller - assert isinstance(caller, NVUniversalGemmCaller), type(caller) # noqa: S101 - assert self.layout == caller.layout # noqa: S101 + if not isinstance(caller, NVUniversalGemmCaller): + raise AssertionError(f"expected NVUniversalGemmCaller, got {type(caller)}") + if self.layout != caller.layout: + raise AssertionError("Expected self.layout == caller.layout") render = self.make_kernel_render prev_kind = self._render_kind @@ -6421,9 +6423,12 @@ class MultiTemplateBuffer(TritonTemplateBuffer): def finalize_as_nvgemm_caller(self, caller: ChoiceCaller) -> None: from torch._inductor.codegen.nv_universal_gemm import NVUniversalGemmCaller - assert isinstance(caller, NVUniversalGemmCaller), type(caller) # noqa: S101 - assert self.get_size() == caller.layout.size # noqa: S101 - assert self.get_stride() == caller.layout.stride # noqa: S101 + if not isinstance(caller, NVUniversalGemmCaller): + raise AssertionError(f"expected NVUniversalGemmCaller, got {type(caller)}") + if self.get_size() != caller.layout.size: + raise AssertionError("Expected self.get_size() == caller.layout.size") + if self.get_stride() != caller.layout.stride: + raise AssertionError("Expected self.get_stride() == caller.layout.stride") self.make_kernel_render = caller.get_make_kernel_render() self._render_kind = "nvgemm" self._render_caller = caller @@ -6608,9 +6613,8 @@ class NVUniversalGemmBuffer(TemplateBuffer): - render: function that returns source code string """ if epilogue_fn_code is not None: - assert epilogue_var_renames is not None, ( # noqa: S101 - "epilogue_fn_code requires epilogue_var_renames" - ) + if epilogue_var_renames is None: + raise AssertionError("epilogue_fn_code requires epilogue_var_renames") from torch._inductor.codegen.nv_universal_gemm.nv_universal_gemm_kernel import ( NVUniversalGemmKernel, diff --git a/torch/_inductor/scheduler.py b/torch/_inductor/scheduler.py index 1fb5f77e6df4..8978e2da5f61 100644 --- a/torch/_inductor/scheduler.py +++ b/torch/_inductor/scheduler.py @@ -5767,7 +5767,11 @@ class Scheduler: ms_fused_choice = choice multi_node._choice_timings[hint_override] = new_timings if ms_fused_choice is not None: - assert isinstance(ms_fused_choice, TritonTemplateCallerBase) # noqa: S101 + if not isinstance(ms_fused_choice, TritonTemplateCallerBase): + raise AssertionError( + f"expected TritonTemplateCallerBase, " + f"got {type(ms_fused_choice)}" + ) hint_override_best_fusion_choice[hint_override] = ( ms_fused_choice ) @@ -8150,8 +8154,10 @@ class Scheduler: write_dep = epilogue_writes[0] read_dep = epilogue_reads[0] - assert isinstance(read_dep, MemoryDep) # noqa: S101 - assert isinstance(write_dep, MemoryDep) # noqa: S101 + if not isinstance(read_dep, MemoryDep): + raise AssertionError(f"expected MemoryDep, got {type(read_dep)}") + if not isinstance(write_dep, MemoryDep): + raise AssertionError(f"expected MemoryDep, got {type(write_dep)}") if read_dep.index != write_dep.index or read_dep.size != write_dep.size: why("epilogue's read and write indices differ") return False diff --git a/torch/cuda/graphs.py b/torch/cuda/graphs.py index e8b9d7292f18..878bf38d860f 100644 --- a/torch/cuda/graphs.py +++ b/torch/cuda/graphs.py @@ -857,7 +857,10 @@ class CUDAGraph(_CUDAGraph): _require_cuda_bindings() # Narrow for the type checker (cuda bindings are present past the check). - assert _cuda_runtime is not None and _cuda_driver is not None # noqa: S101 + if _cuda_runtime is None or _cuda_driver is None: + raise AssertionError( + "expected _cuda_runtime and _cuda_driver to be not None" + ) if _is_tools_id_unavailable(): raise RuntimeError( diff --git a/torch/distributed/pipelining/stage.py b/torch/distributed/pipelining/stage.py index 17e907baf6bd..caee33c80d83 100644 --- a/torch/distributed/pipelining/stage.py +++ b/torch/distributed/pipelining/stage.py @@ -177,8 +177,10 @@ def _build_p2p_direction_groups( parent_pg=group, split_ranks=split_ranks, group_desc="pp_p2p_upstream" ) # All parent ranks are members of the single split. - assert isinstance(downstream, dist.ProcessGroup) # noqa: S101 - assert isinstance(upstream, dist.ProcessGroup) # noqa: S101 + if not isinstance(downstream, dist.ProcessGroup): + raise AssertionError(f"expected dist.ProcessGroup, got {type(downstream)}") + if not isinstance(upstream, dist.ProcessGroup): + raise AssertionError(f"expected dist.ProcessGroup, got {type(upstream)}") logger.info("Pipeline P2P: using per-direction (downstream/upstream) communicators") _PP_DIRECTION_GROUP_CACHE[parent] = (downstream, upstream) return downstream, upstream @@ -444,10 +446,12 @@ class _PipelineStageBase(ABC): continue # Skip entries with None buffer (None gradients) if info.buffer is None: - assert info.tensor_meta is None # noqa: S101 + if info.tensor_meta is not None: + raise AssertionError("expected info.tensor_meta to be None") continue # At this point, source and buffer are guaranteed non-None - assert info.source is not None # noqa: S101 + if info.source is None: + raise AssertionError("expected info.source to be not None") peer_global_rank = self._resolve_peer_global_rank(info.source) ops.append(dist.P2POp(dist.irecv, info.buffer, peer_global_rank, group)) @@ -1127,7 +1131,8 @@ class _PipelineStageBase(ABC): # Note: grads_input may contain gradients for both args and kwargs (from fwd_cache), # Kwargs are local to each stage and don't need gradient transmission. # Validate backward output (input gradients) for DTensor metadata - assert self._stage_meta.inputs is not None # noqa: S101 + if self._stage_meta.inputs is None: + raise AssertionError("expected self._stage_meta.inputs to be not None") num_fwd_args = len(self._stage_meta.inputs) if self._runtime_validate and not self.is_first: self._validate_stage_tensors( @@ -1870,7 +1875,8 @@ class PipelineStage(_PipelineStageBase): if self.is_first: acc = my_vote_t elif self._is_same_rank(self.stage_index - 1): - assert received_acc is not None # noqa: S101 + if received_acc is None: + raise AssertionError("expected received_acc to be not None") acc = received_acc * my_vote_t else: peer_global = self._resolve_peer_global_rank(self.stage_index - 1) @@ -1901,7 +1907,8 @@ class PipelineStage(_PipelineStageBase): The global vote result tensor for this stage. """ if self.is_last or self._is_same_rank(self.stage_index + 1): - assert received_result is not None # noqa: S101 + if received_result is None: + raise AssertionError("expected received_result to be not None") result = received_result else: peer_global = self._resolve_peer_global_rank(self.stage_index + 1) @@ -2024,7 +2031,8 @@ class PipelineStage(_PipelineStageBase): f"got {type(args).__name__}." ) tensor_args = validate_and_normalize_to_tuple(args) - assert tensor_args is not None # noqa: S101 + if tensor_args is None: + raise AssertionError("expected tensor_args to be not None") self._stage_meta.inputs = extract_tensor_metas(tensor_args) inference_args = tuple(self._to_tensor(a) for a in tensor_args) elif self._is_same_rank(self.stage_index - 1): diff --git a/torch/distributed/tensor/_redistribute.py b/torch/distributed/tensor/_redistribute.py index 7a5bdbd801f1..01dd26389e36 100644 --- a/torch/distributed/tensor/_redistribute.py +++ b/torch/distributed/tensor/_redistribute.py @@ -1620,9 +1620,10 @@ def redistribute_local_tensor( # This is safe because _StridedShard.is_shard() returns False, so # _comm_type_key() returns None and flattening is never attempted. if isinstance(current, _StridedShard) or isinstance(target, _StridedShard): - assert mesh_to_use is device_mesh, ( # noqa: S101 - "_StridedShard redistribute assumes no flattened transforms" - ) + if mesh_to_use is not device_mesh: + raise AssertionError( + "_StridedShard redistribute assumes no flattened transforms" + ) num_chunks = mesh_to_use.size(mesh_dim=i) diff --git a/torch/distributed/tensor/experimental/_func_map.py b/torch/distributed/tensor/experimental/_func_map.py index 287b51d0c355..663e3bc4369c 100644 --- a/torch/distributed/tensor/experimental/_func_map.py +++ b/torch/distributed/tensor/experimental/_func_map.py @@ -207,7 +207,8 @@ def _out_spmd_types_to_grad_placements( grad_placement = _dtensor_placement_if_compatible( actual_type[axis].backward_type(), placement ) - assert grad_placement is not None # noqa: S101 + if grad_placement is None: + raise AssertionError("expected grad_placement to be not None") grad_spec.append(grad_placement) grad_out_placements.append(tuple(grad_spec)) @@ -458,7 +459,8 @@ def _local_map_wrapped( raise RuntimeError( "spmd_types=True requires the spmd_types package to be installed" ) - assert device_mesh is not None # noqa: S101 + if device_mesh is None: + raise AssertionError("expected device_mesh to be not None") _annotate_spmd_types( flat_local_args, in_placements, in_grad_placements, device_mesh ) @@ -489,7 +491,8 @@ def _local_map_wrapped( grad_out_placements = None if enable_spmd_types: - assert device_mesh is not None # noqa: S101 + if device_mesh is None: + raise AssertionError("expected device_mesh to be not None") grad_out_placements = _out_spmd_types_to_grad_placements( flat_out, out_placements_tuple, # pyrefly: ignore [bad-argument-type] diff --git a/torch/export/exported_program.py b/torch/export/exported_program.py index 7849bebf03a1..48a3fe9ec748 100644 --- a/torch/export/exported_program.py +++ b/torch/export/exported_program.py @@ -612,7 +612,8 @@ def _decompose_and_get_gm_with_new_signature_constants( joint_loss_index if joint_loss_index is not None else None ), ) - assert isinstance(gm, torch.fx.GraphModule) # noqa: S101 + if not isinstance(gm, torch.fx.GraphModule): + raise AssertionError(f"expected torch.fx.GraphModule, got {type(gm)}") gm.graph.eliminate_dead_code() # Update the signatures with the new placeholder names in case they diff --git a/torch/onnx/_internal/exporter/_input_observer.py b/torch/onnx/_internal/exporter/_input_observer.py index 2d8e5b67b6a4..017ab7b3d54f 100644 --- a/torch/onnx/_internal/exporter/_input_observer.py +++ b/torch/onnx/_internal/exporter/_input_observer.py @@ -465,9 +465,16 @@ class InputObserverInfo: nested structured. This option is used internally to infer arguments. """ self.align_inputs_none_values() - assert self._best_candidate is not None # noqa: S101 - assert self._best_candidate.flat_list is not None # noqa: S101 - assert self._best_candidate.aligned_flat_list is not None # noqa: S101 + if self._best_candidate is None: + raise AssertionError("expected self._best_candidate to be not None") + if self._best_candidate.flat_list is None: + raise AssertionError( + "expected self._best_candidate.flat_list to be not None" + ) + if self._best_candidate.aligned_flat_list is None: + raise AssertionError( + "expected self._best_candidate.aligned_flat_list to be not None" + ) def _set_batch_dimension(name_or_position) -> bool: if not set_batch_dimension_for: @@ -679,7 +686,8 @@ class InputObserverInfo: # This is already checked by _build_inputs_completed_with_none_values # but this is not always well captured by tools checking types. self.align_inputs_none_values() - assert self._best_candidate is not None # noqa: S101 + if self._best_candidate is None: + raise AssertionError("expected self._best_candidate to be not None") candidate = None if index_or_candidate is None: for cand in self.inputs: @@ -701,7 +709,8 @@ class InputObserverInfo: else: candidate = index_or_candidate - assert candidate is not None # noqa: S101 + if candidate is None: + raise AssertionError("expected candidate to be not None") if candidate.aligned_flat_list is None: raise RuntimeError( f"Candidate {candidate} has no aligned flat list of tensors, " @@ -710,10 +719,12 @@ class InputObserverInfo: ) aligned_flat_list = candidate.aligned_flat_list - assert aligned_flat_list is not None # noqa: S101 + if aligned_flat_list is None: + raise AssertionError("expected aligned_flat_list to be not None") if any(t is None for t in aligned_flat_list): dynamic_shapes = self.infer_dynamic_shapes(return_flat=True) - assert isinstance(dynamic_shapes, tuple) # noqa: S101 + if not isinstance(dynamic_shapes, tuple): + raise AssertionError(f"expected tuple, got {type(dynamic_shapes)}") aligned_flat_list = list(aligned_flat_list) for index in range(len(aligned_flat_list)): if aligned_flat_list[index] is not None: @@ -1056,7 +1067,8 @@ class InputObserver: Inferred arguments, every optional tensor is replaced by an empty tensor. """ self._check_captured() - assert self.info is not None # noqa: S101 + if self.info is None: + raise AssertionError("expected self.info to be not None") index_or_candidate: int | InputCandidate | None = None if index_or_args_or_kwargs is None or isinstance(index_or_args_or_kwargs, int): index_or_candidate = index_or_args_or_kwargs @@ -1132,7 +1144,8 @@ class InputObserver: # For big models, we should consider taking a filename to avoid the users # creating the model proto twice. self._check_captured() - assert self.info is not None # noqa: S101 + if self.info is None: + raise AssertionError("expected self.info to be not None") onnx_program.initialize_inference_session(initializer) @@ -1148,7 +1161,8 @@ class InputObserver: loop = io_sets data: list[dict[str, Any]] = [] for inputs, outputs, latency in loop: - assert inputs.aligned_flat_list is not None # noqa: S101 + if inputs.aligned_flat_list is None: + raise AssertionError("expected inputs.aligned_flat_list to be not None") if len(input_names) != len(inputs.aligned_flat_list): raise RuntimeError( f"There are ({len(inputs.aligned_flat_list)}) " diff --git a/torch/testing/_internal/distributed/_tensor/common_dtensor.py b/torch/testing/_internal/distributed/_tensor/common_dtensor.py index ce39240d171a..013a064a0cb5 100644 --- a/torch/testing/_internal/distributed/_tensor/common_dtensor.py +++ b/torch/testing/_internal/distributed/_tensor/common_dtensor.py @@ -1530,9 +1530,10 @@ def validate_sharding_rule_sample_backward( return None # DTensor backward - assert len(input_placements) == len(full_tensors), ( # noqa: S101 - f"placement/tensor count mismatch: {len(input_placements)} vs {len(full_tensors)}" - ) + if len(input_placements) != len(full_tensors): + raise AssertionError( + f"placement/tensor count mismatch: {len(input_placements)} vs {len(full_tensors)}" + ) dt_tensors = [ distribute_tensor(c, device_mesh, (p,)) for c, p in zip(_clone_with_grad(full_tensors), input_placements, strict=True) diff --git a/torch/utils/cpp_extension.py b/torch/utils/cpp_extension.py index 91964cf3b460..98eeda729021 100644 --- a/torch/utils/cpp_extension.py +++ b/torch/utils/cpp_extension.py @@ -2371,7 +2371,8 @@ def _jit_compile(name, clean_ctx_mgr = contextlib.nullcontext() with clean_ctx_mgr as clean_ctx: if IS_HIP_EXTENSION and (with_cuda or with_cudnn): - assert hipify_python is not None # noqa: S101 + if hipify_python is None: + raise AssertionError("expected hipify_python to be not None") hipify_result = hipify_python.hipify( project_directory=build_directory, output_directory=build_directory,