已合并
[test] Add torch.autograd.profiler_util.StringTable.values Api Coverage #42021
lihaokun-2026创建于 7月18日
[test] Add torch.autograd.profiler_util.StringTable.values Api Coverage #42021
已合并
共 1 个文件变更+82-0
| @@ -0,0 +1,82 @@ | |||
| 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 | +Add validation cases for torch.autograd.profiler_util.StringTable.values on NPU. | ||
| 17 | + | ||
| 18 | +This file validates that StringTable.values returns a dynamic ValuesView | ||
| 19 | +containing current StringTable values in both empty and populated cases. | ||
| 20 | +""" | ||
| 21 | + | ||
| 22 | +from collections.abc import ValuesView | ||
| 23 | + | ||
| 24 | +import torch | ||
| 25 | +import torch_npu | ||
| 26 | +from torch.autograd.profiler_util import StringTable | ||
| 27 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 28 | + | ||
| 29 | + | ||
| 30 | +class TestStringTableValues(TestCase): | ||
| 31 | + """Functional tests for torch.autograd.profiler_util.StringTable.values.""" | ||
| 32 | + | ||
| 33 | + def test_values_empty_string_table(self): | ||
| 34 | + string_table = StringTable() | ||
| 35 | + | ||
| 36 | + values = string_table.values() | ||
| 37 | + | ||
| 38 | + self.assertIsInstance(values, ValuesView) | ||
| 39 | + self.assertEqual(list(values), []) | ||
| 40 | + | ||
| 41 | + def test_values_contains_explicit_items(self): | ||
| 42 | + string_table = StringTable() | ||
| 43 | + string_table["aten::add"] = "aten::add" | ||
| 44 | + string_table["aten::relu"] = "aten::relu" | ||
| 45 | + | ||
| 46 | + values = string_table.values() | ||
| 47 | + | ||
| 48 | + self.assertEqual(set(values), {"aten::add", "aten::relu"}) | ||
| 49 | + self.assertEqual(len(values), 2) | ||
| 50 | + | ||
| 51 | + def test_values_reflects_missing_key_insertion(self): | ||
| 52 | + string_table = StringTable() | ||
| 53 | + | ||
| 54 | + demangled_name = string_table["std::vector<int>"] | ||
| 55 | + values = string_table.values() | ||
| 56 | + | ||
| 57 | + self.assertIn(demangled_name, values) | ||
| 58 | + self.assertEqual(list(values), [demangled_name]) | ||
| 59 | + | ||
| 60 | + def test_values_keeps_short_key_unchanged(self): | ||
| 61 | + string_table = StringTable() | ||
| 62 | + | ||
| 63 | + short_name = string_table["t"] | ||
| 64 | + | ||
| 65 | + self.assertEqual(short_name, "t") | ||
| 66 | + self.assertEqual(list(string_table.values()), ["t"]) | ||
| 67 | + | ||
| 68 | + def test_values_view_updates_after_mutation(self): | ||
| 69 | + string_table = StringTable() | ||
| 70 | + string_table["first"] = "first" | ||
| 71 | + values = string_table.values() | ||
| 72 | + | ||
| 73 | + string_table["second"] = "second" | ||
| 74 | + | ||
| 75 | + self.assertEqual(set(values), {"first", "second"}) | ||
| 76 | + self.assertEqual(len(values), 2) | ||
| 77 | + | ||
| 78 | + | ||
| 79 | +if __name__ == "__main__": | ||
| 80 | + print(f"torch version: {torch.__version__}", flush=True) | ||
| 81 | + print(f"torch_npu version: {torch_npu.__version__}", flush=True) | ||
| 82 | + run_tests() | ||
【openlibing.ci】检测到当前PR中存在代码检查告警抑制 1 处,详情见下表,请Committer检视合理性。 / Detected 1 code check alert suppression(s) in this PR, see table below. Committers please review.
import torch_npu # noqa: F401