xref: /aosp_15_r20/external/pytorch/test/test_determination.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# Owner(s): ["module: ci"]
2
3import os
4
5import run_test
6
7from torch.testing._internal.common_utils import run_tests, TestCase
8
9
10class DummyOptions:
11    verbose = False
12
13
14class DeterminationTest(TestCase):
15    # Test determination on a subset of tests
16    TESTS = [
17        "test_nn",
18        "test_jit_profiling",
19        "test_jit",
20        "test_torch",
21        "test_cpp_extensions_aot_ninja",
22        "test_cpp_extensions_aot_no_ninja",
23        "test_utils",
24        "test_determination",
25        "test_quantization",
26    ]
27
28    @classmethod
29    def determined_tests(cls, changed_files):
30        changed_files = [os.path.normpath(path) for path in changed_files]
31        return [
32            test
33            for test in cls.TESTS
34            if run_test.should_run_test(
35                run_test.TARGET_DET_LIST, test, changed_files, DummyOptions()
36            )
37        ]
38
39    def test_target_det_list_is_sorted(self):
40        # We keep TARGET_DET_LIST sorted to minimize merge conflicts
41        # but most importantly to allow us to comment on the absence
42        # of a test. It would be very difficult to add a file right
43        # next to a comment that says to keep it out of the list.
44        self.assertListEqual(run_test.TARGET_DET_LIST, sorted(run_test.TARGET_DET_LIST))
45
46    def test_config_change_only(self):
47        """CI configs trigger all tests"""
48        self.assertEqual(self.determined_tests([".ci/pytorch/test.sh"]), self.TESTS)
49
50    def test_run_test(self):
51        """run_test.py is imported by determination tests"""
52        self.assertEqual(
53            self.determined_tests(["test/run_test.py"]), ["test_determination"]
54        )
55
56    def test_non_code_change(self):
57        """Non-code changes don't trigger any tests"""
58        self.assertEqual(
59            self.determined_tests(["CODEOWNERS", "README.md", "docs/doc.md"]), []
60        )
61
62    def test_cpp_file(self):
63        """CPP files trigger all tests"""
64        self.assertEqual(
65            self.determined_tests(["aten/src/ATen/native/cpu/Activation.cpp"]),
66            self.TESTS,
67        )
68
69    def test_test_file(self):
70        """Test files trigger themselves and dependent tests"""
71        self.assertEqual(
72            self.determined_tests(["test/test_jit.py"]),
73            ["test_jit_profiling", "test_jit"],
74        )
75        self.assertEqual(
76            self.determined_tests(["test/jit/test_custom_operators.py"]),
77            ["test_jit_profiling", "test_jit"],
78        )
79        self.assertEqual(
80            self.determined_tests(
81                ["test/quantization/eager/test_quantize_eager_ptq.py"]
82            ),
83            ["test_quantization"],
84        )
85
86    def test_test_internal_file(self):
87        """testing/_internal files trigger dependent tests"""
88        self.assertEqual(
89            self.determined_tests(["torch/testing/_internal/common_quantization.py"]),
90            [
91                "test_jit_profiling",
92                "test_jit",
93                "test_quantization",
94            ],
95        )
96
97    def test_torch_file(self):
98        """Torch files trigger dependent tests"""
99        self.assertEqual(
100            # Many files are force-imported to all tests,
101            # due to the layout of the project.
102            self.determined_tests(["torch/onnx/utils.py"]),
103            self.TESTS,
104        )
105        self.assertEqual(
106            self.determined_tests(
107                [
108                    "torch/autograd/_functions/utils.py",
109                    "torch/autograd/_functions/utils.pyi",
110                ]
111            ),
112            ["test_utils"],
113        )
114        self.assertEqual(
115            self.determined_tests(["torch/utils/cpp_extension.py"]),
116            [
117                "test_cpp_extensions_aot_ninja",
118                "test_cpp_extensions_aot_no_ninja",
119                "test_utils",
120                "test_determination",
121            ],
122        )
123
124    def test_new_folder(self):
125        """New top-level Python folder triggers all tests"""
126        self.assertEqual(self.determined_tests(["new_module/file.py"]), self.TESTS)
127
128    def test_new_test_script(self):
129        """New test script triggers nothing (since it's not in run_tests.py)"""
130        self.assertEqual(self.determined_tests(["test/test_new_test_script.py"]), [])
131
132
133if __name__ == "__main__":
134    run_tests()
135