From f27ace720083311f8d3c6ba10e40a46ae7374f57 Mon Sep 17 00:00:00 2001 From: Yuanyuan Chen Date: Fri, 23 Oct 2026 00:53:55 +0000 Subject: [PATCH] Fix undefined names in tests ruff reported 130 F821 in test/. Most were imports dropped by earlier trim edits and upstream merges; add them back. The rest: - Two conflict resolutions cut code in half. test_cpp_extensions_jit lost test_inline_jit_generated_bindings_keep_gil_disabled to the XPU tests, leaving its last assertion in test_mps_extension. test_fx lost the _capture_gm helper to a partial copy of test_profiler_stack_trace_augmentation. Restore both, and drop the MPS test and the duplicate. - test_foreach checked an undefined is_cuda; restore "cuda" in device. - test_libtorch_agnostic: drop the skipIfTorchVersionLessThan skips and the 2.15 version check; this torch is at least 2.15. - Remove leftovers of removed features: the Halide skips, the MTIA device interface check, the quantized resize test, JitTestCase bases, and instantiate calls for test classes that no longer exist. ruff now reports no F821 in torch/ and test/, and no new F401. Co-Authored-By: Claude Opus 5.5 --- test/cpp_extensions/test_libtorch_agnostic.py | 37 ++-------------- .../fsdp/test_fully_shard_training.py | 1 + test/distributed/checkpoint/test_fsspec.py | 1 + test/export/test_serialize.py | 1 + test/export/test_sparse.py | 1 + test/functorch/test_eager_transforms.py | 6 --- .../functorch/test_memory_efficient_fusion.py | 9 +--- test/fx/test_lazy_graph_module.py | 1 + test/inductor/test_aot_inductor.py | 1 + test/inductor/test_device_backend_hooks.py | 3 +- test/inductor/test_fused_attention.py | 1 + test/inductor/test_inductor_utils.py | 4 ++ test/inductor/test_torchinductor.py | 15 ------- test/inductor/test_torchinductor_opinfo.py | 1 + test/run_test.py | 1 + test/test_binary_ufuncs.py | 1 + test/test_cpp_extensions_jit.py | 44 ++++++++++++++----- test/test_foreach.py | 5 ++- test/test_fx.py | 28 +++--------- test/test_fx_experimental.py | 3 +- test/test_nestedtensor.py | 1 + test/test_nn.py | 3 ++ test/test_pruning_op.py | 1 + test/test_python_dispatch.py | 1 + test/test_tensor_creation_ops.py | 1 + test/test_torch.py | 10 +---- 26 files changed, 71 insertions(+), 110 deletions(-) diff --git a/test/cpp_extensions/test_libtorch_agnostic.py b/test/cpp_extensions/test_libtorch_agnostic.py index 14fe63141504b..97c277016f945 100644 --- a/test/cpp_extensions/test_libtorch_agnostic.py +++ b/test/cpp_extensions/test_libtorch_agnostic.py @@ -28,6 +28,7 @@ TestCase, xfailIf, ) +from torch.testing._internal.common_utils import xfailIfTorchDynamo @unittest.skipIf( @@ -38,13 +39,13 @@ class TestLibtorchAgnostic(TestCase): """ Tests for the libtorch_agnostic extensions, grouped by the release that introduced the APIs they exercise: libtorch_agn_2_9 through - libtorch_agn_2_14. + libtorch_agn_2_15. """ @classmethod def setUpClass(cls): base_dir = Path(__file__).parent - for version in ("2_9", "2_10", "2_11", "2_12", "2_13", "2_14"): + for version in ("2_9", "2_10", "2_11", "2_12", "2_13", "2_14", "2_15"): try: __import__(f"libtorch_agn_{version}") except Exception: @@ -52,16 +53,6 @@ def setUpClass(cls): extension_root=base_dir / f"libtorch_agn_{version}_extension" ) - if (current_major > 2) or (current_major == 2 and current_minor >= 15): - try: - import libtorch_agn_2_15 # noqa: F401 - except Exception: - install_cpp_extension( - extension_root=base_dir / "libtorch_agn_2_15_extension" - ) - else: - print(f"Skipping 2.15 extension (running on PyTorch {torch.__version__})") - @onlyCPU def test_slow_sgd(self, device): import libtorch_agn_2_9 as libtorch_agnostic @@ -299,8 +290,6 @@ def test_my_is_pinned_cuda_true(self, device): pinned = torch.randn(2, 3, device="cpu", pin_memory=True) self.assertTrue(libtorch_agnostic.ops.my_is_pinned(pinned)) - - @skipIfTorchVersionLessThan(2, 10) @parametrize("descending", [False, True]) @parametrize("dim", [0, 1, -1, -3]) def test_my_sort(self, device, dim, descending): @@ -314,8 +303,6 @@ def test_my_sort(self, device, dim, descending): values, _ = my_sort(t) self.assertEqual(values, torch.sort(t).values) - - @skipIfTorchVersionLessThan(2, 10) @parametrize("descending", [False, True]) @parametrize("dim", [0, 1, -1, -3]) def test_my_sort_stable(self, device, dim, descending): @@ -336,8 +323,6 @@ def test_my_sort_stable(self, device, dim, descending): expected = torch.sort(t_view, stable=True, dim=dim, descending=descending) self.assertEqual(values, expected.values) self.assertEqual(indices, expected.indices) - - @skipIfTorchVersionLessThan(2, 10) @skipIfTorchDynamo("Dynamo failed to run FX node with fake tensors") def test_my_masked_select(self, device): import libtorch_agn_2_10 as libtorch_agnostic @@ -358,8 +343,6 @@ def test_my_masked_select(self, device): with self.assertRaisesRegex(RuntimeError, "expected BoolTensor for mask"): libtorch_agnostic.ops.my_masked_select(t, mask.to(torch.int64)) - - @skipIfTorchVersionLessThan(2, 10) @dtypes(torch.float32, torch.float64) @parametrize( "torch_op,batch_shape", @@ -379,7 +362,6 @@ def test_my_mm_out_ops(self, device, dtype, torch_op, batch_shape): self.assertEqual(id(result), id(out)) @onlyCUDA - @skipIfTorchVersionLessThan(2, 10) @dtypes(torch.float16, torch.bfloat16) @parametrize( "torch_op,batch_shape", @@ -397,8 +379,6 @@ def test_my_mm_out_dtype_ops(self, device, dtype, torch_op, batch_shape): result = stable_op(out, a, b, torch.float32) self.assertEqual(out, torch_op(a, b, out_dtype=torch.float32)) self.assertEqual(id(result), id(out)) - - @skipIfTorchVersionLessThan(2, 10) def test_my_flip(self, device): import libtorch_agn_2_10 as libtorch_agnostic @@ -409,7 +389,6 @@ def test_my_flip(self, device): @skipIfWindows(msg="TemporaryFileName cannot unlink a memory-mapped file") @onlyCPU - @skipIfTorchVersionLessThan(2, 10) def test_my_from_file(self, device): import libtorch_agn_2_10 as libtorch_agnostic @@ -462,7 +441,6 @@ def test_pyobject_non_tensor_raises(self, device): libtorch_agnostic._interop.pyobject_roundtrip("not a tensor") @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_is_tensor(self, device): import libtorch_agn_2_15 as libtorch_agnostic @@ -474,7 +452,6 @@ def test_pyobject_is_tensor(self, device): self.assertFalse(is_tensor(torch.float32)) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_tensor_subclass(self, device): import libtorch_agn_2_14 import libtorch_agn_2_15 @@ -489,7 +466,6 @@ class TensorSubclass(torch.Tensor): self.assertEqual(y.data_ptr(), x.data_ptr()) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) @skipIfTorchDynamo("Dynamo intercepts the spoofed __class__") def test_pyobject_spoofed_tensor_class(self, device): import libtorch_agn_2_14 @@ -507,7 +483,6 @@ def __class__(self): libtorch_agn_2_14._interop.pyobject_roundtrip(obj) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) @parametrize( "dtype", [ @@ -528,7 +503,6 @@ def test_pyobject_dtype_roundtrip(self, device, dtype): self.assertIs(out, dtype) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) # "cuda" (no index) must not come back as cuda:0; constructing the # device objects needs no GPU. @parametrize("roundtrip_device", ["cpu", "meta", "cuda", "cuda:1"]) @@ -542,7 +516,6 @@ def test_pyobject_device_roundtrip(self, device, roundtrip_device): self.assertEqual(out.index, dev.index) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_tensor_dtype_and_device(self, device): import libtorch_agn_2_15 as libtorch_agnostic @@ -551,7 +524,6 @@ def test_pyobject_tensor_dtype_and_device(self, device): self.assertEqual(libtorch_agnostic._interop.tensor_device(x), x.device) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_non_dtype_raises(self, device): import libtorch_agn_2_15 as libtorch_agnostic @@ -559,7 +531,6 @@ def test_pyobject_non_dtype_raises(self, device): libtorch_agnostic._interop.dtype_roundtrip("not a dtype") @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_unsupported_dtype_raises(self, device): import libtorch_agn_2_15 as libtorch_agnostic @@ -567,7 +538,6 @@ def test_pyobject_unsupported_dtype_raises(self, device): libtorch_agnostic._interop.dtype_roundtrip(torch.qint8) @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_non_device_raises(self, device): import libtorch_agn_2_15 as libtorch_agnostic @@ -575,7 +545,6 @@ def test_pyobject_non_device_raises(self, device): libtorch_agnostic._interop.device_roundtrip("not a device") @onlyCPU - @skipIfTorchVersionLessThan(2, 15) def test_pyobject_unsupported_device_raises(self, device): import libtorch_agn_2_15 as libtorch_agnostic diff --git a/test/distributed/_composable/fsdp/test_fully_shard_training.py b/test/distributed/_composable/fsdp/test_fully_shard_training.py index 43aad61bde211..7c137f9b18905 100644 --- a/test/distributed/_composable/fsdp/test_fully_shard_training.py +++ b/test/distributed/_composable/fsdp/test_fully_shard_training.py @@ -83,6 +83,7 @@ funcol = torch.ops.c10d_functional from torch.testing._internal.common_fsdp import get_devtype +from torch.testing._internal.common_utils import instantiate_parametrized_tests, parametrize device_type = torch.device(get_devtype()) diff --git a/test/distributed/checkpoint/test_fsspec.py b/test/distributed/checkpoint/test_fsspec.py index dbce28cb3b305..713c899ba912d 100644 --- a/test/distributed/checkpoint/test_fsspec.py +++ b/test/distributed/checkpoint/test_fsspec.py @@ -14,6 +14,7 @@ import torch.distributed as dist import torch.distributed.checkpoint as dcp from torch.distributed.checkpoint._fsspec_filesystem import ( + _FileSystem, FileSystem, FsspecReader, FsspecWriter, diff --git a/test/export/test_serialize.py b/test/export/test_serialize.py index 5cc7e643c967a..3c11a90bd53ac 100644 --- a/test/export/test_serialize.py +++ b/test/export/test_serialize.py @@ -61,6 +61,7 @@ ) from torch.testing._internal.inductor_utils import HAS_TRITON from torch.testing._internal.torchbind_impls import init_torchbind_implementations +from torch._export.utils import wrap_method try: diff --git a/test/export/test_sparse.py b/test/export/test_sparse.py index a968b67ea0eb8..c0b96e4eafbb6 100644 --- a/test/export/test_sparse.py +++ b/test/export/test_sparse.py @@ -21,6 +21,7 @@ subtest, TestCase, ) +from torch._subclasses.fake_tensor import is_fake_tensor # Various data types (preserved over operations). diff --git a/test/functorch/test_eager_transforms.py b/test/functorch/test_eager_transforms.py index f4c341d4d436f..fe976d0b8cd4b 100644 --- a/test/functorch/test_eager_transforms.py +++ b/test/functorch/test_eager_transforms.py @@ -5807,12 +5807,6 @@ def f(x): only_for=only_for, allow_xpu=True, ) -instantiate_device_type_tests( - TestComposabilityDevice, - globals(), - only_for=only_for, - allow_xpu=True, -) instantiate_device_type_tests( TestExamplesCorrectnessDevice, globals(), diff --git a/test/functorch/test_memory_efficient_fusion.py b/test/functorch/test_memory_efficient_fusion.py index 5a19325cedb5f..8b7bfaad2527d 100644 --- a/test/functorch/test_memory_efficient_fusion.py +++ b/test/functorch/test_memory_efficient_fusion.py @@ -7,6 +7,7 @@ from functorch import make_fx from torch._functorch.compile_utils import fx_graph_cse from torch.testing._internal.common_utils import run_tests, TestCase +from torch.testing._internal.common_utils import HardwareClassification HAS_CUDA = torch.cuda.is_available() @@ -316,13 +317,5 @@ def f(x): check(fx_g, t, -1, graph_input=True) -instantiate_device_type_tests( - TestMemoryEfficientOpAuthoringDevice, - globals(), - only_for=("cuda", "xpu"), - allow_xpu=True, -) - - if __name__ == "__main__": run_tests() diff --git a/test/fx/test_lazy_graph_module.py b/test/fx/test_lazy_graph_module.py index 95d96f4e08c15..8370986f75316 100644 --- a/test/fx/test_lazy_graph_module.py +++ b/test/fx/test_lazy_graph_module.py @@ -17,6 +17,7 @@ ) from torch.fx.experimental.proxy_tensor import make_fx from torch.testing._internal.common_utils import TestCase +from torch.testing._internal.common_utils import HardwareClassification class TestLazyGraphModule(TestCase): diff --git a/test/inductor/test_aot_inductor.py b/test/inductor/test_aot_inductor.py index 67c1ef0fd48c8..88e2c3bce55d3 100644 --- a/test/inductor/test_aot_inductor.py +++ b/test/inductor/test_aot_inductor.py @@ -118,6 +118,7 @@ has_triton_experimental_host_tma, has_triton_tensor_descriptor_host_tma, ) +from torch.testing._internal.common_utils import TEST_WITH_ROCM requires_cuda_tma = unittest.skipIf( diff --git a/test/inductor/test_device_backend_hooks.py b/test/inductor/test_device_backend_hooks.py index 596cc9953b09e..96cba4247bdab 100644 --- a/test/inductor/test_device_backend_hooks.py +++ b/test/inductor/test_device_backend_hooks.py @@ -19,6 +19,8 @@ run_tests, TestCase, ) +import torch +from torch._dynamo.device_interface import CpuInterface, CudaInterface, get_interface_for_device class TestMultiProcessorCount(TestCase): @@ -168,7 +170,6 @@ def test_cuda_overrides_match_inline_conditions(self): def test_registered_non_cuda_inherits_base_defaults(self): self.assertIs(get_interface_for_device("cpu"), CpuInterface) self.assertIs(get_interface_for_device("xpu"), XpuInterface) - self.assertIs(get_interface_for_device("mtia"), MtiaInterface) self.assertTrue(CpuInterface.is_fp32_attention_fusion_safe(torch.float32)) self.assertTrue(CpuInterface.is_fp32_softmax_attention_fusion_safe()) self.assertFalse(CpuInterface.keep_attention_on_math_path()) diff --git a/test/inductor/test_fused_attention.py b/test/inductor/test_fused_attention.py index 68a21a9abd179..1b7882dca4438 100644 --- a/test/inductor/test_fused_attention.py +++ b/test/inductor/test_fused_attention.py @@ -31,6 +31,7 @@ HAS_CUDA_AND_TRITON, HAS_XPU_AND_TRITON, ) +from torch.testing._internal.common_utils import recover_orig_fp32_precision def checkpoint_wrapper(fn): diff --git a/test/inductor/test_inductor_utils.py b/test/inductor/test_inductor_utils.py index db0948a3105a1..5ca01dbb48439 100644 --- a/test/inductor/test_inductor_utils.py +++ b/test/inductor/test_inductor_utils.py @@ -15,6 +15,10 @@ from torch._inductor.utils import do_bench_using_profiling from torch._inductor.virtualized import NullHandler, V from torch.autograd import DeviceType +from torch.fx.experimental._constant_symnode import ConstantIntNode +from torch.fx.experimental.symbolic_shapes import DimDynamic, ShapeEnv +from torch.testing._internal.common_utils import instantiate_parametrized_tests, parametrize +from types import SimpleNamespace log = logging.getLogger(__name__) diff --git a/test/inductor/test_torchinductor.py b/test/inductor/test_torchinductor.py index 8e31bad1e8b91..28a374bee4943 100644 --- a/test/inductor/test_torchinductor.py +++ b/test/inductor/test_torchinductor.py @@ -4229,8 +4229,6 @@ def fn(a, b): self.common(fn, (a, b)) b_neg = torch.full_like(a, -divisor) self.common(fn, (a, b_neg)) - - @skip_if_halide # floordiv goes through floats, inexact for large values def test_floordiv_int_min_negative_divisor(self): # Regression test for https://github.com/pytorch/pytorch/issues/198545 def fn(a, b): @@ -6911,7 +6909,6 @@ def test_adaptive_max_pool3d_out_decomposition(self): # halide/mps take the non-logical-index path in _pool_argmax_inner_fn, so the # window offsets are still physical there; halide additionally fails to schedule # the fallback argmax for this shape. - @skip_if_halide @skip_if_mps def test_adaptive_max_pool2d_transposed_indices(self): # transposed input, indices must be logical and not physical offsets @@ -7285,7 +7282,6 @@ def fn(x): # same as test_adaptive_max_pool2d_transposed_indices: halide/mps take the # non-logical-index path in _pool_argmax_inner_fn and still return physical # window offsets. - @skip_if_halide @skip_if_mps def test_max_pool2d_transposed_indices(self): # transposed input, indices must be logical and not physical offsets @@ -11602,8 +11598,6 @@ def fn(x, w): x = torch.tensor([-2, 0, 1, 2]).repeat(16).view(8, 8).float() w = 2 * torch.eye(8) self.common(fn, (x, w)) - - @skip_if_halide # won't fuse a read of the buffer it writes in place def test_masked_fill_of_realized_buffer_kernel_count(self): # The fill writes the realized mm output in place, in one kernel. def fn(x, w, m): @@ -11691,13 +11685,6 @@ def copy_into_strided_empty(x): if case == "fsdp_copy_then_mutate" and not hasattr(torch.ops.fsdp, "copy_"): self.skipTest("needs torch.ops.fsdp.copy_") - if case in ( - "foreach_reads_sibling_input", - "foreach_reads_sibling_across_groups", - ) and is_halide_backend(self.device): - # Halide gets the inputs right but not x + 0, which misses x's - # in-place update. - self.skipTest("halide: an output misses its input's in-place update") fn = { f.__name__: f for f in ( @@ -14594,8 +14581,6 @@ def fn(x, y, w): x = torch.ones(8, dtype=torch.bfloat16) self.common(fn, (x, torch.zeros_like(x), torch.tensor(0.95))) - - @skip_if_halide def test_fp16_cast_log1p(self): self.common(lambda x: x.half().log1p(), (torch.rand(8) * 1e-3,)) diff --git a/test/inductor/test_torchinductor_opinfo.py b/test/inductor/test_torchinductor_opinfo.py index d33fc83d73f69..bbe117661359c 100644 --- a/test/inductor/test_torchinductor_opinfo.py +++ b/test/inductor/test_torchinductor_opinfo.py @@ -57,6 +57,7 @@ from torch.utils._dtype_abbrs import dtype_abbrs from torch.utils._python_dispatch import TorchDispatchMode from torch.utils._pytree import tree_leaves, tree_map +from torch.testing._internal.common_utils import parametrize try: diff --git a/test/run_test.py b/test/run_test.py index 5b95f205d8384..95d604e1191b6 100644 --- a/test/run_test.py +++ b/test/run_test.py @@ -102,6 +102,7 @@ def upload_adhoc_failure_json(*args, **kwargs): from torch.testing._internal.common_utils import HardwareClassification +import subprocess _HC_CHOICES = [e.name for e in HardwareClassification] diff --git a/test/test_binary_ufuncs.py b/test/test_binary_ufuncs.py index ff78b816a89f1..3e7ae783a5e3a 100644 --- a/test/test_binary_ufuncs.py +++ b/test/test_binary_ufuncs.py @@ -74,6 +74,7 @@ torch_to_numpy_dtype_dict, xfailIfTorchDynamo, ) +from torch.testing._internal.common_device_type import onlyAccelerator if TEST_SCIPY: diff --git a/test/test_cpp_extensions_jit.py b/test/test_cpp_extensions_jit.py index 39487af855363..677e3d230243c 100644 --- a/test/test_cpp_extensions_jit.py +++ b/test/test_cpp_extensions_jit.py @@ -295,6 +295,38 @@ def test_inline_jit_generated_bindings_gil_not_used(self): self.assertIn(expected_declaration, generated_source) self.assertNotIn(unexpected_declaration, generated_source) + @unittest.skipUnless( + sysconfig.get_config_var("Py_GIL_DISABLED") == 1, + "requires free-threaded Python", + ) + def test_inline_jit_generated_bindings_keep_gil_disabled(self): + script = """ +import sys +import tempfile + +import torch.utils.cpp_extension + +assert not sys._is_gil_enabled() +with tempfile.TemporaryDirectory() as build_directory: + module = torch.utils.cpp_extension.load_inline( + name="inline_jit_extension_gil_not_used_runtime", + cpp_sources="int add_one(int value) { return value + 1; }", + functions="add_one", + build_directory=build_directory, + gil_not_used=True, + verbose=True, + ) + assert module.add_one(1) == 2 + assert not sys._is_gil_enabled() +""" + result = subprocess.run( + [sys.executable, "-c", script], + capture_output=True, + text=True, + timeout=300, + ) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + @unittest.skipIf(not (TEST_XPU), "XPU not found") def test_jit_xpu_archlists(self): # NOTE: in this test we explicitly test few different options @@ -342,18 +374,6 @@ def test_jit_xpu_arch_floor(self): with mock.patch.dict(os.environ, {"TORCH_XPU_ARCH_LIST": "bmg"}): self.assertEqual(_get_sycl_arch_list(), "bmg") - @unittest.skipIf(not TEST_MPS, "MPS not found") - def test_mps_extension(self): - module = torch.utils.cpp_extension.load( - name="torch_test_mps_extension", - sources=[ - "cpp_extensions/mps_extension.mm", - ], - verbose=True, - keep_intermediates=False, - ) - self.assertEqual(result.returncode, 0, result.stdout + result.stderr) - def test_inline_jit_compile_extension_multiple_sources_and_no_functions(self): cpp_source1 = """ torch::Tensor sin_add(torch::Tensor x, torch::Tensor y) { diff --git a/test/test_foreach.py b/test/test_foreach.py index 58c20e6080b4e..a65f59d85fbef 100644 --- a/test/test_foreach.py +++ b/test/test_foreach.py @@ -92,8 +92,11 @@ def __call__(self, inputs, device, expect_fastpath, **kwargs): actual = None zero_size = kwargs.pop("zero_size", False) + # Only CUDA/ROCm expose a reliable fastpath profiler key + # (multi_tensor_apply_kernel, or the _foreach_mta_launch marker on + # ROCm), so the check below is CUDA-only; other accelerators skip it. if ( - is_cuda + "cuda" in device and torch.autograd.kineto_available() and torch.profiler.ProfilerActivity.CUDA in torch.profiler.supported_activities() diff --git a/test/test_fx.py b/test/test_fx.py index 7d66125087e06..ab6dbde4d06d8 100644 --- a/test/test_fx.py +++ b/test/test_fx.py @@ -4638,28 +4638,10 @@ def fn(a, b, c, d): # recorver mutable checking flag torch.fx.proxy.TracerBase.check_mutable_operations = orig_tracer_mutable_flag - @unittest.skipIf(not torch.cuda.is_available(), "CUDA not available") - @torch.fx.experimental._config.patch("enrich_profiler_metadata", True) - @blas_library_context("cublaslt") - def test_profiler_stack_trace_augmentation(self): - """ - Test that map_recorded_events_to_aten_ops_with_stack_trace correctly - augments profiler events with stack traces from FX metadata registry. - """ - - # Simple test model - class TestModel(torch.nn.Module): - def __init__(self): - super().__init__() - self.linear1 = torch.nn.Linear(10, 16) - self.relu = torch.nn.ReLU() - self.linear2 = torch.nn.Linear(16, 10) - - def forward(self, x): - x = self.linear1(x) - x = self.relu(x) - x = self.linear2(x) - return x + @staticmethod + def _capture_gm(fn, *example_inputs): + """Compile ``fn`` with dynamic shapes, return the captured GraphModule.""" + from torch._dynamo.testing import EagerAndRecordGraphs backend = EagerAndRecordGraphs() torch.compile(fn, dynamic=True, backend=backend)(*example_inputs) @@ -4935,7 +4917,7 @@ def run_getitem_target(): _wrapped_methods_to_patch.pop() -class TestFXCUDA(JitTestCase): +class TestFXCUDA(TestCase): hw_classification = HardwareClassification.CUDA def setUp(self): diff --git a/test/test_fx_experimental.py b/test/test_fx_experimental.py index 04fcb4d2fc9a1..f133984864b97 100644 --- a/test/test_fx_experimental.py +++ b/test/test_fx_experimental.py @@ -63,6 +63,7 @@ TestCase, ) import torch.utils._pytree as pytree +from torch.testing._internal.common_device_type import onlyCPU try: import torchvision.models # noqa: F401 @@ -2107,7 +2108,7 @@ def forward(self, {', '.join(param_names)}): self.assertEqual(test_out, ref_out) -class TestNormalizeOperatorsGeneric(JitTestCase): +class TestNormalizeOperatorsGeneric(TestCase): hw_classification = HardwareClassification.GENERIC def test_normalize_quantized_eb(self): diff --git a/test/test_nestedtensor.py b/test/test_nestedtensor.py index 4cf7300814ce9..cd84c309bcbe1 100644 --- a/test/test_nestedtensor.py +++ b/test/test_nestedtensor.py @@ -74,6 +74,7 @@ from torch.testing._internal.opinfo.definitions.nested import _sample_njts, njt_op_db from torch.utils._pytree import tree_flatten, tree_map_only from torch.utils.checkpoint import checkpoint +from torch._inductor.codegen.common import BackendFeature, has_backend_feature # Tests are ported from pytorch/nestedtensor. diff --git a/test/test_nn.py b/test/test_nn.py index 38a2a875084d7..0e0d058324748 100644 --- a/test/test_nn.py +++ b/test/test_nn.py @@ -60,6 +60,9 @@ from torch.testing._internal.common_cuda import tf32_on_and_off, tf32_off, tf32_on from torch.types import _TensorOrTensors from torch.testing._internal.common_mkldnn import reduced_f32_on_and_off +from torch._subclasses.fake_tensor import FakeTensorMode +from torch.testing._internal.common_device_type import largeMPSBufferTest, skipCUDAIfNotRocm +from torch.testing._internal.common_utils import HardwareClassification, skipIfRocmVersionLessThan if TEST_WITH_ROCM: os.environ["PYTORCH_MIOPEN_SUGGEST_NHWC"] = "1" diff --git a/test/test_pruning_op.py b/test/test_pruning_op.py index ab48fde82686f..76f1605fa5938 100644 --- a/test/test_pruning_op.py +++ b/test/test_pruning_op.py @@ -5,6 +5,7 @@ import numpy as np import torch from torch.testing._internal.common_utils import TestCase, run_tests, skipIfTorchDynamo +from torch.testing._internal.common_utils import HardwareClassification # Generating a tensor and running an operator on it is routinely slower than # hypothesis' default deadline. diff --git a/test/test_python_dispatch.py b/test/test_python_dispatch.py index d8a51bf3cf097..a6d4445974666 100644 --- a/test/test_python_dispatch.py +++ b/test/test_python_dispatch.py @@ -55,6 +55,7 @@ TorchDispatchMode, ) from torch.utils._pytree import tree_map, tree_map_only +import unittest # used as DataLoader collate_fn below; named here to avoid trying to pickle a lambda diff --git a/test/test_tensor_creation_ops.py b/test/test_tensor_creation_ops.py index 8ed8a9ec91f3f..d557304a92f81 100644 --- a/test/test_tensor_creation_ops.py +++ b/test/test_tensor_creation_ops.py @@ -49,6 +49,7 @@ ) from torch.utils.dlpack import to_dlpack +from torch.testing._internal.common_device_type import onlyCPU, onlyNativeDeviceTypes # TODO: replace with make_tensor def _generate_input(shape, dtype, device, with_extremal): diff --git a/test/test_torch.py b/test/test_torch.py index 386d74482e9ed..f8b3b15d18fb2 100644 --- a/test/test_torch.py +++ b/test/test_torch.py @@ -67,6 +67,7 @@ all_types_complex_float8_and, all_passthru_types_and, ) from torch.testing._internal.two_tensor import TwoTensor +from torch.testing._internal.common_utils import AlwaysWarnTypedStorageRemoval, TEST_CUDA_GRAPH, instantiate_parametrized_tests if TEST_WITH_TORCHINDUCTOR: from torch._inductor.test_case import TestCase @@ -11306,15 +11307,6 @@ class TestTorchCPU(TestCase): hw_classification = HardwareClassification.CPU exact_dtype = True - @skipIfTorchInductor("https://github.com/pytorch/pytorch/issues/113707") - @dtypes(*get_all_qint_dtypes()) - def test_nondeterministic_resize_quantized(self, device, dtype): - a = torch.tensor([-1, 0, 1, 2, 3], dtype=torch.float, device=device) - b = torch.quantize_per_tensor(a, 0.1, 10, dtype) - self.check_nondeterministic_alert( - lambda: b.resize_((10,)), - 'quantized_resize_cpu_') - # FIXME: move to test_scatter_gather_ops def test_gather_backward_one_dim(self): _test_gather_backward_one_dim(self, 'cpu', False)