已合并
test(meta_registrations): add testcase for torch._meta_registrations.register_meta #42118
test(meta_registrations): add testcase for torch._meta_registrations.register_meta #42118
已合并
木路折创建于 7月19日
木路折
木路折
7月19日

【合入来源】

  • 关联 issue: https://gitcode.com/Ascend/pytorch/issues/2902

  • pytorch 社区没有针对 torch._meta_registrations.register_meta 的独立测试用例,故新增该测试文件,用于验证该 API 的正确性。

【修改方案】

一、API 功能说明

torch._meta_registrations.register_meta(op_or_ops) 是 PyTorch 内部装饰器,用于将一个函数注册为一个或多个 ATen 算子的 meta 内核,并写入全局 meta_table。注册后,调用对应算子的 meta 实现时会执行被装饰的函数。该 API 为 PyTorch 私有接口。

二、测试用例完备性说明

PyTorch 官方 test/ 目录中没有直接调用 torch._meta_registrations.register_meta 的测试用例,现有测试仅通过 elementwise_meta 等内部路径间接使用,无法验证装饰器本身的注册行为。本次新增 test/test_library_meta_registrations.py,直接验证:

  • 单算子注册到 meta_table
  • 多算子注册到同一 meta 内核;
  • 装饰器返回原函数;
  • 测试结束后恢复 meta_table 原状态,避免跨测试副作用。

三、版本适用范围

torch._meta_registrations.register_meta 为 PyTorch 内部私有接口,在 PyTorch v2.7.1、v2.9.0、v2.10.0、v2.11.0、v2.12.0、master 各目标分支均存在且函数签名一致,因此本测试用例需提交到上述全部分支。

四、NPU 适配

register_metameta_table 均为纯 Python 层注册逻辑,不涉及任何硬件相关计算、算子调用或设备内存操作,与 CPU/GPU/NPU 完全解耦。昇腾 NPU 环境下无需修改 API 源码,也不需要在测试中导入 torch_npu 或创建 NPU 张量,可直接复用。

【资料变更】

不涉及

torch._meta_registrations.register_meta 为 PyTorch 私有接口,按项目规范 docs/zh/api/native_api 中无需额外刷新资料。

【接口变更】

不涉及

仅新增测试用例,未修改任何业务代码。

【功能验证】

  • 本地运行时验证:未进行
  • 原因:当前服务器环境未安装对应版本的 PyTorch/Torch-NPU
  • 已将测试分支 test-register-meta-v2.12.0 推送到 fork,由 CI 进行运行时验证

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 木路折 的贡献)
木路折木路折
7月19日 创建了 pull request,commit 20652f5b
木路折木路折
7月19日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(3)
atomgit-bot
atomgit-bot
7月19日 评论:

变更摘要

本次 PR 新增测试文件 test/test_library_meta_registrations.py,针对 torch._meta_registrations.register_meta 装饰器的核心注册行为补充测试用例,覆盖单算子注册、多算子共享注册函数以及装饰器返回值等关键场景,弥补社区测试覆盖空缺。

主要改动

  • 新增 TestLibraryMetaRegistrations 测试类:继承自 TestCase,包含两个测试方法,分别验证 register_meta 的单算子注册和多算子批量注册行为。
  • 单算子注册测试 test_register_meta_adds_to_meta_table:验证 register_meta 能将装饰函数正确注册到 meta_table,且装饰器返回原函数;测试结束后通过 finally 块恢复 meta_table 原状态。
  • 多算子注册测试 test_register_meta_supports_op_list:验证传入算子列表时,同一函数可被注册到多个算子的 meta_table 条目中,装饰器同样返回原函数;测试结束后逐条恢复各算子对应的原始 meta 注册。
likedislike
atomgit-bot
atomgit-bot
7月19日 评论:

代码审查

✅ 未发现问题

likedislike
此处折叠了124条消息 查看更多
ascend-robotascend-robot成员
22 天前 添加了label:approved
Jingwei Huang
Jingwei Huang成员
21 天前 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
21 天前 添加了label:lgtm
ascend-robotascend-robot成员
21 天前 合入了pull request
ascend-robot
ascend-robot成员
21 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13357 [ commitID:f8437070 ] 已完成
likedislike