Co-authored-by: lihaokun-2026<851563813@qq.com> # message auto-generated for no-merge-commit merge: !42017 merge autograd-profiler_util-StringTable-values-master into master [test] Add torch.autograd.profiler_util.StringTable.values Api Coverage Created-by: lihaokun-2026 Commit-by: lihaokun-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 [#2928](https://gitcode.com/Ascend/pytorch/issues/2928) - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 公开issue: - `torch.autograd.profiler_util.StringTable.values`: [【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(68)](https://gitcode.com/Ascend/pytorch/issues/2928) # 【修改方案】 1. API 功能说明: - `torch.autograd.profiler_util.StringTable.values`:`StringTable` 继承自 `defaultdict`,`values()` 返回当前 string table 中所有 value 的动态视图,用于获取 profiler 字符串映射表中的值集合。 2. 上游用例检查情况: 检索 PyTorch / Torch-NPU 现有测试后,未找到面向 `torch.autograd.profiler_util.StringTable.values` 的独立、直接、最小验证用例,因此按任务要求新增 NPU API 验证用例。 3. 新增用例文件: - `test_profiler_util_string_table_values.py` 4. 新增用例覆盖场景: - `StringTable.values()` 在空表场景下返回空 `ValuesView` - 显式写入 key/value 后,`values()` 返回全部 value - 访问缺省 key 触发 `StringTable.__missing__` 后,`values()` 可读取自动写入的 value - 单字符 key 保持原值,不触发 demangle 变更 - `values()` 返回动态视图,后续新增元素可被已有 view 感知 - 在 `torch_npu` 环境下验证 privateuse1 backend 为 `npu` 5. 新增文件头部已按要求补充注释,说明该文件验证的 API 功能。 # 【资料变更】 不涉及。未修改产品资料、接口说明文档或用户可见文档。 # 【接口变更】 不涉及。未修改跨代码仓接口,也未修改用户可见接口行为。 # 【功能验证】 测试环境: ```sh torch: 2.12.0+cpu torch_npu: 2.12.0 ``` 测试方法: ```sh source /usr/local/Ascend/ascend-toolkit/set_env.sh TORCH_DEVICE_BACKEND_AUTOLOAD=1 python3 test/test_profiler_util_string_table_values.py -v ``` 测试结果:  # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!42017
83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
# Copyright (c) 2026 Huawei Technologies Co., Ltd
|
|
# All rights reserved.
|
|
#
|
|
# Licensed under the BSD 3-Clause License (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""
|
|
Add validation cases for torch.autograd.profiler_util.StringTable.values on NPU.
|
|
|
|
This file validates that StringTable.values returns a dynamic ValuesView
|
|
containing current StringTable values in both empty and populated cases.
|
|
"""
|
|
|
|
from collections.abc import ValuesView
|
|
|
|
import torch
|
|
import torch_npu
|
|
from torch.autograd.profiler_util import StringTable
|
|
from torch.testing._internal.common_utils import TestCase, run_tests
|
|
|
|
|
|
class TestStringTableValues(TestCase):
|
|
"""Functional tests for torch.autograd.profiler_util.StringTable.values."""
|
|
|
|
def test_values_empty_string_table(self):
|
|
string_table = StringTable()
|
|
|
|
values = string_table.values()
|
|
|
|
self.assertIsInstance(values, ValuesView)
|
|
self.assertEqual(list(values), [])
|
|
|
|
def test_values_contains_explicit_items(self):
|
|
string_table = StringTable()
|
|
string_table["aten::add"] = "aten::add"
|
|
string_table["aten::relu"] = "aten::relu"
|
|
|
|
values = string_table.values()
|
|
|
|
self.assertEqual(set(values), {"aten::add", "aten::relu"})
|
|
self.assertEqual(len(values), 2)
|
|
|
|
def test_values_reflects_missing_key_insertion(self):
|
|
string_table = StringTable()
|
|
|
|
demangled_name = string_table["std::vector<int>"]
|
|
values = string_table.values()
|
|
|
|
self.assertIn(demangled_name, values)
|
|
self.assertEqual(list(values), [demangled_name])
|
|
|
|
def test_values_keeps_short_key_unchanged(self):
|
|
string_table = StringTable()
|
|
|
|
short_name = string_table["t"]
|
|
|
|
self.assertEqual(short_name, "t")
|
|
self.assertEqual(list(string_table.values()), ["t"])
|
|
|
|
def test_values_view_updates_after_mutation(self):
|
|
string_table = StringTable()
|
|
string_table["first"] = "first"
|
|
values = string_table.values()
|
|
|
|
string_table["second"] = "second"
|
|
|
|
self.assertEqual(set(values), {"first", "second"})
|
|
self.assertEqual(len(values), 2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(f"torch version: {torch.__version__}", flush=True)
|
|
print(f"torch_npu version: {torch_npu.__version__}", flush=True)
|
|
run_tests()
|