已合并
test: add NPU validation for enable_python_dispatcher (#2678) #45963
sodachan创建于 9月7日
test: add NPU validation for enable_python_dispatcher (#2678) #45963
已合并
共 1 个文件变更+87-0
| @@ -0,0 +1,87 @@ | |||
| 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 | +"""Validate Python dispatcher context behavior with NPU tensors. | ||
| 17 | + | ||
| 18 | +The cases cover dispatcher activation, nesting, cleanup after normal and | ||
| 19 | +exceptional exits, argument validation, and a real NPU dispatcher path. | ||
| 20 | +""" | ||
| 21 | + | ||
| 22 | +import torch | ||
| 23 | +from torch._dispatch.python import enable_python_dispatcher | ||
| 24 | +from torch.testing._internal.common_utils import TestCase as TorchTestCase | ||
| 25 | +from torch.testing._internal.common_utils import run_tests | ||
| 26 | + | ||
| 27 | +from torch_npu.testing.testcase import TestCase | ||
| 28 | + | ||
| 29 | + | ||
| 30 | +device_type = ( | ||
| 31 | + acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" | ||
| 32 | +) | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +def is_python_dispatcher_enabled(): | ||
| 36 | + return torch._C._dispatch_tls_is_dispatch_key_included( | ||
| 37 | + torch._C.DispatchKey.PythonDispatcher | ||
| 38 | + ) | ||
| 39 | + | ||
| 40 | + | ||
| 41 | +class TestEnablePythonDispatcher(TorchTestCase, TestCase): | ||
| 42 | + def test_context_enables_and_restores_dispatcher(self): | ||
| 43 | + before = is_python_dispatcher_enabled() | ||
| 44 | + self.assertFalse(before) | ||
| 45 | + input_tensor = torch.tensor([1.0, 2.0, 3.0]).to(device_type) | ||
| 46 | + self.assertEqual(input_tensor.device.type, "npu") | ||
| 47 | + | ||
| 48 | + with enable_python_dispatcher(): | ||
| 49 | + self.assertTrue(is_python_dispatcher_enabled()) | ||
| 50 | + output = input_tensor.square() + 1 | ||
| 51 | + self.assertEqual(output.device.type, "npu") | ||
| 52 | + self.assertEqual( | ||
| 53 | + output, torch.tensor([2.0, 5.0, 10.0]).to(device_type) | ||
| 54 | + ) | ||
| 55 | + | ||
| 56 | + self.assertEqual(is_python_dispatcher_enabled(), before) | ||
| 57 | + | ||
| 58 | + def test_nested_context_restores_outer_state(self): | ||
| 59 | + before = is_python_dispatcher_enabled() | ||
| 60 | + self.assertFalse(before) | ||
| 61 | + | ||
| 62 | + with enable_python_dispatcher(): | ||
| 63 | + self.assertTrue(is_python_dispatcher_enabled()) | ||
| 64 | + with enable_python_dispatcher(): | ||
| 65 | + self.assertTrue(is_python_dispatcher_enabled()) | ||
| 66 | + self.assertTrue(is_python_dispatcher_enabled()) | ||
| 67 | + | ||
| 68 | + self.assertEqual(is_python_dispatcher_enabled(), before) | ||
| 69 | + | ||
| 70 | + def test_context_restores_state_after_exception(self): | ||
| 71 | + before = is_python_dispatcher_enabled() | ||
| 72 | + self.assertFalse(before) | ||
| 73 | + | ||
| 74 | + with self.assertRaisesRegex(RuntimeError, "sentinel"): | ||
| 75 | + with enable_python_dispatcher(): | ||
| 76 | + self.assertTrue(is_python_dispatcher_enabled()) | ||
| 77 | + raise RuntimeError("sentinel") | ||
| 78 | + | ||
| 79 | + self.assertEqual(is_python_dispatcher_enabled(), before) | ||
| 80 | + | ||
| 81 | + def test_invalid_argument(self): | ||
| 82 | + with self.assertRaises(TypeError): | ||
| 83 | + enable_python_dispatcher("invalid") | ||
| 84 | + | ||
| 85 | + | ||
| 86 | +if __name__ == "__main__": | ||
| 87 | + run_tests() | ||