Open
【API一致性任务】torch.onnx.OnnxRegistry.register_op API 一致性验证结论 #3636
zhouzirui1234Add on Aug 3
Aug 3 Modify description of issue
Aug 3 Modify description of issue
Aug 3 Modify description of issue
Aug 3 Modify description of issue
Aug 3 Modify description of issue
Aug 3 Modify description of issue
-
Aug 3 Comments:
Aug 3 Comments:
我要认领这个任务


Aug 5 Modify description of issue
6 event messages are collapsed here。 See more
Aug 11 Added label: event: api-consistencybot-triaged
TorchNPU-Bot
Aug 11 Comments:
Aug 11 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency


Aug 13 Change title to "【API一致性任务】[社区任务] torch.onnx.OnnxRegistry.register_op API 一致性验证结论", originally titled "[社区任务] torch.onnx.OnnxRegistry.register_op API 一致性验证结论".
Aug 13 Change title to "【API一致性任务】[社区任务] torch.onnx.OnnxRegistry.register_op API 一致性验证结论", originally titled "[社区任务] torch.onnx.OnnxRegistry.register_op API 一致性验证结论".
Aug 18 Change title to "【API一致性任务】torch.onnx.OnnxRegistry.register_op API 一致性验证结论", originally titled "【API一致性任务】[社区任务] torch.onnx.OnnxRegistry.register_op API 一致性验证结论".
Aug 18 Change title to "【API一致性任务】torch.onnx.OnnxRegistry.register_op API 一致性验证结论", originally titled "【API一致性任务】[社区任务] torch.onnx.OnnxRegistry.register_op API 一致性验证结论".
Aug 18 Modify description of issue
Aug 18 Added label: help-wanted
TorchNPU-Bot
Aug 18 Comments:
Aug 18 Comments:
检测到社区任务相关 issue,自动添加标签:event: api-consistency、help-wanted


Aug 18 Modify description of issue
Aug 18 Modify description of issue
Aug 18 Modify description of issue
群青世界
Aug 27 Comments:
Aug 27 Comments:
验证成功后,资料支持情况是否需要修改


Aug 27 Modify description of issue
Aug 27 Associated pull request
zhouzirui1234
Aug 27 Comments:
Aug 27 Comments:


Sep 4 Modify description of issue
torch.onnx.OnnxRegistry.register_op处理说明任务 issue:https://gitcode.com/Ascend/pytorch/issues/3497
一、处理结论
本任务按“官方无直接用例、纯 Python 非硬件注册接口”处理:不需要 torch-npu 实现补齐,不新增自写测试,也不新增
test_upstreampatch。七版本核查结果如下:
torch.onnx.OnnxRegistry是公开 API,register_op用于登记自定义 ONNXScript 函数;默认 overload、多次注册顺序、命名 overload 隔离、返回值以及is_custom、is_complex元数据均已实际验证通过。torch.onnx公开导出OnnxRegistry。✘,与真实功能结果不一致,不能继续得出“资料无需修改”的结论;该资料状态需要更正为✔。因此,本 Issue 可以关闭代码与测试补齐路径,但资料状态仍有明确待更正项。
二、API 功能
目标 API:
torch.onnx.OnnxRegistry.register_op( function, namespace, op_name, overload=None, is_complex=False, )接口行为:
OnnxFunction或TracedOnnxFunction;namespace、op_name和overload组成内部完整名称;overload时使用default;registration.ONNXFunction;is_custom固定为True;is_complex元数据;None;PyTorch v2.7.1 官方核心实现:
def register_op( self, function, namespace: str, op_name: str, overload: str | None = None, is_complex: bool = False, ) -> None: internal_name_instance = registration.OpName.from_name_parts( namespace=namespace, op_name=op_name, overload=overload, ) symbolic_function = registration.ONNXFunction( onnx_function=function, op_full_name=internal_name_instance.qualified_name(), is_custom=True, is_complex=is_complex, ) self._register(internal_name_instance, symbolic_function)三、PyTorch 官方用例检查
3.1 官方源码与公共导出检查
核查版本:
执行命令:
cd pytorch git checkout v2.7.1 grep -n "OnnxRegistry" torch/onnx/__init__.py grep -n -A34 -B3 "def register_op" \ torch/onnx/_internal/_exporter_legacy.py执行结果:
结果确认 v2.7.1 中目标类公开导出,方法实现只构造 Python 元数据并写入 Registry。
3.2 官方测试目录精确检索
执行命令:
cd pytorch grep -RIn \ "OnnxRegistry.register_op" \ test \ --include="*.py" grep -RInE \ "OnnxRegistry.*register_op|register_op.*OnnxRegistry" \ test \ --include="*.py"执行结果:
官方
test/onnx/test_custom_ops.py中虽有名为test_register_op的测试,但其目标是旧接口:该接口与
torch.onnx.OnnxRegistry.register_op不是同一个 API,不能作为本任务的直接官方覆盖。3.3 API 类型与 NPU 适配判断
register_op的调用链为:该过程不包含 Tensor 输入输出、设备迁移、已注册函数执行、NPU 算子、Kernel、torch-npu API、设备硬编码或分布式通信。因此应通过真实 ONNXScript 注册和回读验证 Python 行为,不应新增与注册行为无关的 NPU Tensor。
四、运行环境
五、功能验证
5.1 验证场景
v2.7.1 实际验证以下行为:
get_op_functions返回None;is_registered_op返回False;opset18.Abs注册默认 overload;register_op成功返回None;is_registered_op返回True;opset18.Neg,列表长度变为 2;is_custom元数据均为True;is_complex=True被正确保存;overload="Tensor"注册opset18.Add;后续六个版本验证当前 torch/torch-npu 版本、公共类导出状态及直接访问时的错误类型。
5.2 核心验证代码
registry = torch.onnx.OnnxRegistry() first = op.Abs second = op.Neg overloaded_function = op.Add first_result = registry.register_op( first, "review_namespace", "review_abs", ) second_result = registry.register_op( second, "review_namespace", "review_abs", is_complex=True, ) overload_result = registry.register_op( overloaded_function, "review_namespace", "review_abs", overload="Tensor", ) default_items = registry.get_op_functions( "review_namespace", "review_abs", ) named_items = registry.get_op_functions( "review_namespace", "review_abs", "Tensor", ) assert first_result is None assert second_result is None assert overload_result is None assert len(default_items) == 2 assert len(named_items) == 1 assert default_items[0].onnx_function is first assert default_items[1].onnx_function is second assert named_items[0].onnx_function is overloaded_function assert all(item.is_custom for item in (*default_items, *named_items)) assert default_items[0].is_complex is False assert default_items[1].is_complex is True5.3 v2.7.1
执行命令:
cd /root source /usr/local/Ascend/ascend-toolkit/set_env.sh >/dev/null 2>&1 PYTHONPATH=/root/api-consistency-validation/deps/onnxscript \ /root/api-consistency-validation/envs/v2.7.1/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry.py执行结果:
结论:目标 API 完成真实 ONNXScript 函数注册和回读,默认/命名 overload、注册顺序、返回值及元数据均符合官方实现。
5.4 v2.9.0
执行命令:
cd /root source /usr/local/Ascend/ascend-toolkit/set_env.sh >/dev/null 2>&1 /root/api-consistency-validation/envs/v2.9.0/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry_status.py执行结果:
结论:PyTorch v2.9.0 已不再从
torch.onnx公开导出目标类。5.5 v2.10.0
执行命令:
cd /root source /usr/local/Ascend/ascend-toolkit/set_env.sh >/dev/null 2>&1 /root/api-consistency-validation/envs/v2.10.0/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry_status.py执行结果:
结论:PyTorch v2.10.0 已不再公开导出目标类。
5.6 v2.11.0
执行命令:
cd /root source /usr/local/Ascend/ascend-toolkit/set_env.sh >/dev/null 2>&1 /root/api-consistency-validation/envs/v2.11.0/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry_status.py执行结果:
结论:PyTorch v2.11.0 已不再公开导出目标类。
5.7 v2.12.0
执行命令:
cd /root source /usr/local/Ascend/ascend-toolkit/set_env.sh >/dev/null 2>&1 /root/api-consistency-validation/envs/v2.12.0/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry_status.py执行结果:
结论:PyTorch v2.12.0 已不再公开导出目标类。
5.8 v2.13.0
执行命令:
cd /root source /usr/local/Ascend/cann-9.1.0-beta.1/set_env.sh >/dev/null 2>&1 /root/api-consistency-validation/envs/v2.13.0/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry_status.py执行结果:
结论:PyTorch v2.13.0 同样不再公开导出目标类。
5.9 master
执行命令:
cd /root source /usr/local/Ascend/ascend-toolkit/set_env.sh >/dev/null 2>&1 /root/api-consistency-validation/envs/master/bin/python \ /root/api-consistency-validation/probes/probe_onnx_registry_status.py执行结果:
结论:当前 master 同样不再公开导出目标类。
5.10 A5(Ascend 950PR)补充验证
运行环境:
分支验证环境:
PyTorch 官方用例检查
执行命令:
cd /root if rg -n 'OnnxRegistry\.opset_version|OnnxRegistry.*opset_version|opset_version.*OnnxRegistry|OnnxRegistry\.register_op|OnnxRegistry.*register_op|register_op.*OnnxRegistry' /root/onnxregistry-950pr-validation/sources/pytorch-v2.7.1/test; then echo DIRECT_OFFICIAL_MATCHES=FOUND; exit 1; else echo DIRECT_OFFICIAL_MATCHES=0; echo result=PASS; fi执行结果:
说明:PyTorch v2.7.1 官方
test/目录没有直接调用torch.onnx.OnnxRegistry.opset_version或torch.onnx.OnnxRegistry.register_op的用例;同名register_op测试属于旧接口,未作为本 PR 的目标官方用例。按照社区分类规则,不新增自写测试,使用官方实现对应的功能探针补充支持证据。v2.7.1 功能与 950PR 环境验证
环境初始化:
source /usr/local/Ascend/cann-9.1.0-beta.3/set_env.sh >/dev/null 2>&1执行命令:
cd /root PYTHONNOUSERSITE=1 /root/onnxregistry-950pr-validation/envs/v2.7.1/bin/python /root/onnxregistry-950pr-validation/tests/probe_onnx_registry_950.py执行结果:
A5 补充验证结论:
Ascend950PR_9579、NPU Tensor 创建成功,opset_version与register_op的目标语义全部通过、退出码 0。torch.onnx.OnnxRegistry,因此不在这些版本人为补回或验证已移除 API。六、资料检查
执行命令:
grep -n -A8 -B3 \ "torch.onnx.OnnxRegistry.register_op" \ docs/zh/api/native_api/pytorch_2-7-1/torch-onnx.md执行结果:
检查结论:v2.7.1 资料已收录目标 API,但三个产品支持单元格均为
✘。真实 ONNXScript 注册和回读已通过,且方法功能与设备无关,现有资料状态与证据冲突,需要更正为✔。后续版本的公共类已经移除,不应人为补回该条目。七、最终结论
is_custom、is_complex元数据OnnxRegistry不存在✘更正为✔最终处理方式:本 Issue 给出七版本源码、官方用例、功能和资料证据;不提交代码 PR 或测试 PR,不新增
test_upstreampatch。资料更正项不能写成“无需修改”,应在获准的资料流程中单独处理。