已合并
test: add consistency validation cases for torch._C._jit_override_can_fuse_on_cpu (#2756) #42168
luoxiaoyan2024创建于 7月20日
test: add consistency validation cases for torch._C._jit_override_can_fuse_on_cpu (#2756) #42168
已合并
共 1 个文件变更+95-0
| @@ -0,0 +1,95 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# All rights reserved. | ||
| 3 | +# | ||
| 4 | +# Licensed under the BSD 3-Clause License (the "License"); | ||
| 5 | +# you may not use this file except in compliance with the License. | ||
| 6 | +# You may obtain a copy of the License at | ||
| 7 | +# | ||
| 8 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 9 | +# | ||
| 10 | +# Unless required by applicable law or agreed to in writing, software | ||
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | +# See the License for the specific language governing permissions and | ||
| 14 | +# limitations under the License. | ||
| 15 | + | ||
| 16 | +""" | ||
群 | |||
| 17 | +Add consistency validation cases for torch._C._jit_override_can_fuse_on_cpu. | ||
| 18 | + | ||
| 19 | +This file adds self-contained, focused validation for | ||
| 20 | +torch._C._jit_override_can_fuse_on_cpu as requested by the | ||
| 21 | +Ascend for PyTorch API consistency task (#2756). The setter overrides whether | ||
| 22 | +ops can fuse on CPU in the JIT fuser; it accepts a bool value and the | ||
| 23 | +override actually takes effect (verified via the _jit_can_fuse_on_cpu getter). | ||
| 24 | +Two cases are covered: | ||
| 25 | + | ||
| 26 | +* ``test_override_can_fuse_on_cpu`` -- the setter returns None and the global | ||
| 27 | + flag reads back the value just set (False / True / original), proving the | ||
| 28 | + override actually takes effect. | ||
| 29 | +* ``test_override_can_fuse_on_cpu_invalid_type`` -- the setter only accepts a | ||
| 30 | + bool flag; any non-bool argument must be rejected with a type error (or a | ||
| 31 | + RuntimeError surfaced from the C++ binding) instead of being silently | ||
| 32 | + coerced. | ||
| 33 | + | ||
| 34 | +Extendable: peer torch._C._jit_* flags can be appended to this file. | ||
| 35 | +""" | ||
| 36 | + | ||
| 37 | +import torch | ||
| 38 | +from torch.testing._internal.common_utils import run_tests, TestCase | ||
| 39 | + | ||
| 40 | + | ||
| 41 | +class TestJitOverrideCanFuseOnCpu(TestCase): | ||
| 42 | + | ||
| 43 | + def setUp(self): | ||
| 44 | + super().setUp() | ||
| 45 | + self._old_can_fuse_on_cpu = None | ||
| 46 | + if not hasattr(torch._C, "_jit_override_can_fuse_on_cpu"): | ||
| 47 | + self.skipTest("torch._C._jit_override_can_fuse_on_cpu unavailable") | ||
| 48 | + if not hasattr(torch._C, "_jit_can_fuse_on_cpu"): | ||
| 49 | + self.skipTest("torch._C._jit_can_fuse_on_cpu unavailable") | ||
| 50 | + # Save the original global flag; restored in tearDown so this test does | ||
| 51 | + # not leak the override into other tests. Mirrors the save/restore | ||
| 52 | + # pattern used in test/test_jit_fuser_te.py (see test_disabled). | ||
| 53 | + self._old_can_fuse_on_cpu = torch._C._jit_can_fuse_on_cpu() | ||
| 54 | + | ||
| 55 | + def tearDown(self): | ||
| 56 | + if self._old_can_fuse_on_cpu is not None: | ||
| 57 | + torch._C._jit_override_can_fuse_on_cpu(self._old_can_fuse_on_cpu) | ||
| 58 | + super().tearDown() | ||
| 59 | + | ||
| 60 | + def test_override_can_fuse_on_cpu(self): | ||
| 61 | + # The setter is a void flag setter; each call returns None. | ||
| 62 | + self.assertIsNone(torch._C._jit_override_can_fuse_on_cpu(False)) | ||
| 63 | + # After setting False, the global flag reads back as False. | ||
| 64 | + self.assertFalse(torch._C._jit_can_fuse_on_cpu()) | ||
| 65 | + | ||
| 66 | + self.assertIsNone(torch._C._jit_override_can_fuse_on_cpu(True)) | ||
| 67 | + # After setting True, the global flag reads back as True. | ||
| 68 | + self.assertTrue(torch._C._jit_can_fuse_on_cpu()) | ||
| 69 | + | ||
| 70 | + # Restoring to the original value also takes effect. | ||
| 71 | + self.assertIsNone( | ||
| 72 | + torch._C._jit_override_can_fuse_on_cpu(self._old_can_fuse_on_cpu) | ||
| 73 | + ) | ||
| 74 | + self.assertEqual( | ||
| 75 | + torch._C._jit_can_fuse_on_cpu(), self._old_can_fuse_on_cpu | ||
| 76 | + ) | ||
| 77 | + | ||
| 78 | + def test_override_can_fuse_on_cpu_invalid_type(self): | ||
| 79 | + # The setter only accepts a bool flag. A non-bool argument must be | ||
| 80 | + # rejected with a TypeError (or a RuntimeError surfaced from the C++ | ||
| 81 | + # binding) rather than silently coerced. Both the value check and the | ||
| 82 | + # no-partial-mutation invariant are asserted. | ||
| 83 | + for bad in (None, "True", 1.5, [True], {"a": 1}, (1, 2)): | ||
| 84 | + with self.assertRaises((TypeError, RuntimeError)): | ||
| 85 | + torch._C._jit_override_can_fuse_on_cpu(bad) | ||
| 86 | + # The flag must remain unchanged after the rejected calls. Restore | ||
| 87 | + # defensively in case any call happened to succeed on a given build. | ||
| 88 | + torch._C._jit_override_can_fuse_on_cpu(self._old_can_fuse_on_cpu) | ||
| 89 | + self.assertEqual( | ||
| 90 | + torch._C._jit_can_fuse_on_cpu(), self._old_can_fuse_on_cpu | ||
| 91 | + ) | ||
| 92 | + | ||
| 93 | + | ||
| 94 | +if __name__ == "__main__": | ||
| 95 | + run_tests() | ||


加上copyright