Files
TorchNPU/test/test_torch_getattribute_api.py
T
zhouzirui-2026 27f3e93d0b test: add torch.__getattribute__ API validation
Co-authored-by: 2490136803<2490136803@qq.com>



# message auto-generated for no-merge-commit merge:
!41419 merge intern/test-torch-getattribute-master into master

test: add torch.__getattribute__ API validation

Created-by: zhouzirui-2026
Commit-by: 2490136803
Merged-by: ascend-robot
Description: # 【合入来源】

- [ ] 需求
- [ ] 问题单
- [x] issue/工单
- [ ] 重构优化
- [ ] 资料更新

关联 issue:

- 任务 issue:https://gitcode.com/Ascend/pytorch/issues/2734

# 【修改方案】

本 PR 新增 `test/test_torch_getattribute_api.py`,补充 `torch.__getattribute__` 在 torch-npu 环境下的 API 一致性验证用例。

该 API 是 Python module 对象的属性访问入口,用于从 `torch` module 上按名称读取属性。经检查,PyTorch 官方未提供 `torch.__getattribute__` 的独立专项测试,torch-npu 当前 `test` 和 `test_upstream` 目录中也未发现该 API 的专项测试。按照指导规范,本场景属于“PyTorch 官方社区无用例”,因此在 torch-npu `test` 目录补充自写测试。

任务 API 功能、上游社区用例情况与本 PR 处理方式如下:

| API | 功能说明 | 上游社区用例 / 覆盖情况 | 本 PR 处理 |
| --- | --- | --- | --- |
| `torch.__getattribute__` | Python module 属性访问入口,用于按名称读取 `torch` module 上的属性。 | 未检索到 PyTorch 官方直接专项测试;torch-npu 现有 `test` / `test_upstream` 目录中也未发现专项测试。 | 新增 `test/test_torch_getattribute_api.py`,验证已有属性读取、动态属性读取和异常路径。 |

主要开发思路:

1. 先确认 `torch.__getattribute__` 在当前多版本 torch-npu 环境中存在,且基础行为与 Python module 属性访问语义一致。
2. 复查 PyTorch 官方测试目录,未发现直接覆盖 `torch.__getattribute__` 的独立专项测试。
3. 复查 torch-npu 当前仓库 `test` 和 `test_upstream` 目录,未发现该 API 的现有专项测试。
4. 该 API 不涉及 Tensor 输入,不存在需要迁移到 NPU Tensor 的适配点,因此不需要 `test_upstream` patch。
5. 该 API 属于顶层 `torch` module 属性访问能力,不属于 `npu`、`fx`、`nn` 等子模块,因此测试文件放在 torch-npu 根测试目录 `test/test_torch_getattribute_api.py`。
6. 本 PR 仅补充测试,不修改 API 实现,不改变现有接口行为。

新增测试文件:

```text
test/test_torch_getattribute_api.py
```

新增测试方法:

```text
test_get_existing_attributes
test_get_dynamic_attribute
test_get_missing_attribute_raises
```

各测试核心验证点如下:

| 测试方法 | 核心验证点 |
| --- | --- |
| `test_get_existing_attributes` | 使用 `types.ModuleType.__getattribute__(torch, "__dict__")` 获取模块字典作为独立 oracle,验证 `torch.__config__`、`torch.Tensor`、`torch.nn`、`torch.empty` 等已存在属性通过 `torch.__getattribute__` 读取时返回模块字典中的同一对象。 |
| `test_get_dynamic_attribute` | 在 `torch` module 上临时设置动态属性,验证 `torch.__getattribute__` 可读取该属性,且返回对象与模块字典中记录的对象一致;测试结束后清理动态属性。 |
| `test_get_missing_attribute_raises` | 验证读取不存在属性时,保持 Python module 语义并抛出包含属性名的 `AttributeError`。 |

# 【资料变更】

不涉及资料 PR。

已检查 PyTorch 官方公开文档与 Ascend native API 文档,未发现 `torch.__getattribute__` 独立条目。该 API 属于 Python module 固有属性访问机制,不是计算类公开 API,因此不新增 `docs.md`,不提交资料 PR。

# 【接口变更】

不涉及。

本 PR 不修改 API 实现,不改变现有接口行为,不新增 torch-npu 对外接口。

# 【功能验证】

运行环境:

```text
操作系统:Ubuntu 22.04.5 LTS (aarch64)
内核版本:5.10.0-182.0.0.95.oe2203sp3.aarch64
NPU 型号:Ascend 910B4
Driver / npu-smi:25.2.0
CANN Toolkit:9.0.0
CANN 路径:/usr/local/Ascend/cann-9.0.0
Python:3.10.12
```

分支验证环境:

```text
分支:master
torch:2.12.0
torch-npu:2.12.0rc1
Python 环境:/workspace/a3-zzr-env/envs/venv_master_base_torch2.12.0_torchnpu2.12.0rc1_py310
```

执行命令:

```bash
cd /root
python /workspace/a3-zzr-env/repos/pytorch-master/test/test_torch_getattribute_api.py
```

执行结果:

```text
W0713 09:42:09.361000 5136 torch/_native/cutedsl_utils.py:55] CuTeDSL operators require optional Python packages `nvidia-cutlass-dsl` and `apache-tvm-ffi`; missing optional dependency `nvidia_cutlass_dsl`
...
----------------------------------------------------------------------
Ran 3 tests in 0.209s

OK
```

说明:该 warning 来自 PyTorch 导入阶段 optional dependency 提示,不影响本用例执行结果。

最终验证结论:

1. 本 PR 补充 3 个 API 一致性测试,覆盖 `torch.__getattribute__` 的正常属性读取、动态属性读取和异常路径。
2. 补充测试不仅验证 API 可调用,也使用模块字典作为独立 oracle 验证已有属性读取、运行时动态属性访问能力和缺失属性异常语义。
3. 该 API 不涉及 Tensor 输入,不需要 NPU Tensor 迁移适配。
4. 补充测试已在 master 环境验证通过。
5. 目标 API 当前行为符合预期,未发现需要 API 功能补齐的问题。
6. 本 PR 不修改 API 实现,不涉及接口变更。

# 【CheckList】

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [ ] PR持续集成流水线(CI)执行通过,代码检查无异常

See merge request: Ascend/pytorch!41419
2026-07-23 21:49:40 +08:00

50 lines
1.9 KiB
Python

"""
Add validation cases for torch.__getattribute__ API:
1. PyTorch community does not provide a direct standalone test for this API.
2. This file validates normal attribute lookup, dynamically added module
attributes, and the AttributeError path for missing attributes.
"""
import types
import torch
from torch.testing._internal.common_utils import TestCase, run_tests
class TestTorchGetattributeApi(TestCase):
def _torch_module_dict(self):
return types.ModuleType.__getattribute__(torch, "__dict__")
def test_get_existing_attributes(self):
module_dict = self._torch_module_dict()
attrs = ("__config__", "Tensor", "nn", "empty")
for name in attrs:
# Use the module dict as the oracle instead of getattr, which shares
# the same module attribute access path as torch.__getattribute__.
self.assertIn(name, module_dict)
self.assertIs(torch.__getattribute__(name), module_dict[name])
def test_get_dynamic_attribute(self):
module_dict = self._torch_module_dict()
attr_name = "_torch_npu_getattribute_test_value"
attr_value = object()
self.assertNotIn(attr_name, module_dict)
try:
setattr(torch, attr_name, attr_value)
# The dynamically added module attribute should be returned directly.
self.assertIn(attr_name, module_dict)
self.assertIs(torch.__getattribute__(attr_name), module_dict[attr_name])
finally:
module_dict.pop(attr_name, None)
def test_get_missing_attribute_raises(self):
module_dict = self._torch_module_dict()
attr_name = "_torch_npu_missing_getattribute_test_value"
self.assertNotIn(attr_name, module_dict)
with self.assertRaisesRegex(AttributeError, attr_name):
torch.__getattribute__(attr_name)
if __name__ == "__main__":
run_tests()