已合并
test(meta_registrations): add testcase for torch._meta_registrations.register_meta #42118
木路折创建于 7月19日
test(meta_registrations): add testcase for torch._meta_registrations.register_meta #42118
已合并
共 1 个文件变更+77-0
| @@ -0,0 +1,77 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 3 | +# All rights reserved. | ||
| 4 | +# | ||
| 5 | +# Licensed under the BSD 3-Clause License (the "License"); | ||
| 6 | +# you may not use this file except in compliance with the License. | ||
| 7 | +# You may obtain a copy of the License at | ||
| 8 | +# | ||
| 9 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 10 | +# | ||
| 11 | +# Unless required by applicable law or agreed to in writing, software | ||
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | +# See the License for the specific language governing permissions and | ||
| 15 | +# limitations under the License. | ||
| 16 | +# Owner(s): ["module: library"] | ||
| 17 | + | ||
| 18 | +"""Add validation cases for torch._meta_registrations.register_meta on NPU: | ||
| 19 | +1. PyTorch community lacks sufficient and direct API validations for | ||
| 20 | + torch._meta_registrations.register_meta, so this file is added. | ||
| 21 | +2. This file validates the decorator's core registration behavior, including | ||
| 22 | + single-op registration, multi-op registration, and returning the original | ||
| 23 | + function (extendable). | ||
| 24 | +""" | ||
| 25 | + | ||
| 26 | +import torch | ||
| 27 | +from torch.testing._internal.common_utils import run_tests, TestCase | ||
| 28 | + | ||
| 29 | + | ||
| 30 | +class TestLibraryMetaRegistrations(TestCase): | ||
| 31 | + """Test torch._meta_registrations.register_meta.""" | ||
| 32 | + | ||
| 33 | + def test_register_meta_adds_to_meta_table(self): | ||
| 34 | + """register_meta adds the decorated function to the meta table.""" | ||
| 35 | + op = torch.ops.aten.add.Tensor | ||
| 36 | + meta_table = torch._meta_registrations.meta_table | ||
| 37 | + original_meta = meta_table.get(op) | ||
| 38 | + | ||
| 39 | + def meta_fn(self, other, alpha=1): | ||
| 40 | + return torch.empty_like(self) | ||
| 41 | + | ||
| 42 | + try: | ||
| 43 | + wrapped = torch._meta_registrations.register_meta(op)(meta_fn) | ||
| 44 | + self.assertIs(wrapped, meta_fn) | ||
| 45 | + self.assertIn(op, meta_table) | ||
| 46 | + self.assertIs(meta_table[op], meta_fn) | ||
| 47 | + finally: | ||
| 48 | + if original_meta is not None: | ||
| 49 | + meta_table[op] = original_meta | ||
| 50 | + elif op in meta_table: | ||
| 51 | + del meta_table[op] | ||
| 52 | + | ||
| 53 | + def test_register_meta_supports_op_list(self): | ||
| 54 | + """register_meta can register the same function for multiple ops.""" | ||
| 55 | + ops = [torch.ops.aten.sub.Tensor, torch.ops.aten.mul.Tensor] | ||
| 56 | + meta_table = torch._meta_registrations.meta_table | ||
| 57 | + original_metas = [meta_table.get(op) for op in ops] | ||
| 58 | + | ||
| 59 | + def meta_fn(self, other, alpha=1): | ||
| 60 | + return torch.empty_like(self) | ||
| 61 | + | ||
| 62 | + try: | ||
| 63 | + wrapped = torch._meta_registrations.register_meta(ops)(meta_fn) | ||
| 64 | + self.assertIs(wrapped, meta_fn) | ||
| 65 | + for op in ops: | ||
| 66 | + self.assertIn(op, meta_table) | ||
| 67 | + self.assertIs(meta_table[op], meta_fn) | ||
| 68 | + finally: | ||
| 69 | + for op, original in zip(ops, original_metas): | ||
| 70 | + if original is not None: | ||
| 71 | + meta_table[op] = original | ||
| 72 | + elif op in meta_table: | ||
| 73 | + del meta_table[op] | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +if __name__ == "__main__": | ||
| 77 | + run_tests() | ||