已合并
test: add test cases for torch.onnx.ONNXProgram.model_proto #43581
test: add test cases for torch.onnx.ONNXProgram.model_proto #43581
已合并
木路折创建于 8月2日
木路折
木路折
8月2日

【合入来源】

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

  • 任务分类:用例补齐(PyTorch 官方缺少对 torch.onnx.ONNXProgram.model_proto 的直接测试)

  • 该 API 虽为纯 Python 序列化属性,但验证必须先经 torch.onnx.export(dynamo=True) 在 NPU 上完成 dynamo 导出,验证链路依赖 NPU,故在 test 目录新增用例看护该流程。

【修改方案】

一、API 功能说明

torch.onnx.ONNXProgram.model_protoONNXProgram 类的 @property,返回 ir.serde.serialize_model(self.model),即将 dynamo-based ONNX exporter 导出的 IR 模型序列化为 onnx.ModelProto protobuf 对象。该 API 为非计算类接口,不涉及 NPU 数据类型计算,仅做纯 Python 序列化操作。

二、测试用例完备性说明

PyTorch 官方 test/onnx/exporter/ 目录中没有直接针对 model_proto 的测试用例。test_onnx_program.py 仅测试 rename_axestest_api.pytest_input_observer.py 中仅将 .model_proto 作为工具间接使用,未验证其返回值结构、序列化能力等核心功能。本次新增 test/onnx/exporter/test_onnx_program.py,直接验证:

  • 返回有效的 onnx.ModelProto 对象(ir_version > 0);
  • 单输入单输出模型的 graph 结构正确;
  • producer_name 为 "pytorch";
  • protobuf 序列化结果非空;
  • 多输入多输出模型的 graph input/output 数量正确。

三、NPU 适配

测试涉及 Tensor 创建与 ONNX 导出,所有 Tensor 通过 device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" 迁移到 NPU,确保导出流程在 NPU 上执行。

【资料变更】

不涉及

资料检查结果:torch.onnx.ONNXProgram.model_proto 已收录在 docs/zh/api/native_api 各版本目录(2-7-1、2-11-0、2-12-0、2-13-0)的 torch-onnx.md 中,但支持状态标记有误,已在 https://gitcode.com/Ascend/pytorch/pull/43583 更正为 ✔(Ascend 950DT 保持 ✘)。本 PR 仅包含测试用例。

【接口变更】

不涉及

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

【功能验证】

  • 本地 NPU 运行
    • 环境:Ubuntu 22.04 / Python 3.11.15 / PyTorch 2.7.1+cpu / Torch-NPU 2.7.1.post4 / CANN 9.0 / Ascend 910B3
    • 命令:python test/onnx/exporter/test_onnx_program.py
    • 结果:Ran 5 tests in 9.949s, OK

【CheckList】

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

变更摘要

该 PR 针对 PyTorch 社区缺少对 torch.onnx.ONNXProgram.model_proto 直接测试的问题(关联 issue #3489),新增测试文件 test/onnx/exporter/test_onnx_program.py,在 NPU 环境下验证该 API 的正确性。model_protoONNXProgram 类的 @property,将 dynamo-based ONNX exporter 导出的 IR 模型序列化为 onnx.ModelProto protobuf 对象。新增测试通过 torch.accelerator.current_accelerator() 获取当前设备类型用于创建输入张量,并基于 common_utils.TestCase 编写了 5 个用例,覆盖返回值有效性、图结构、producer_name、protobuf 序列化能力及多输入多输出场景。

主要改动

  • 新增 test_onnx_program.py 测试文件: 基于 common_utils.TestCasecommon_utils.run_tests() 组织用例,并通过 torch.accelerator.current_accelerator() 动态确定设备类型(NPU 或 CPU)创建测试输入张量,为后续扩展其他 ONNXProgram API 验证预留结构。
  • test_model_proto_returns_valid_proto 用例: 对单输入模型调用 torch.onnx.export(..., dynamo=True) 后校验 model_proto 返回非空且 ir_version > 0,验证其返回有效 onnx.ModelProto 对象。
  • test_model_proto_graph_structuretest_model_proto_multiple_inputs_outputs 用例: 分别对单输入单输出与双输入双输出模型校验 proto.graph.input / proto.graph.output 的数量,验证 graph 结构与模型输入输出对应。
  • test_model_proto_producer_nametest_model_proto_serialization 用例: 断言 proto.producer_name == "pytorch",并调用 proto.SerializeToString() 校验序列化结果非空,验证 API 的 protobuf 序列化能力。
likedislike
不准确?
atomgit-bot
atomgit-bot
8月2日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
此处折叠了101条消息 查看更多
ascend-robotascend-robot成员
21 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
21 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
21 天前 评论:
流水线 PR-pipeline_pytorch#62704 [ commitID:5895efa7 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
Build_X86_213 🛑 >>>
Build_ARM_213 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 codecheck_pre-commit >>>
check_error >>>
lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
UT_ARM_A2_Part_01_213 🛑 >>>
UT_ARM_A2_Part_02_213 🛑 >>>
UT_ARM_A2_Part_03_213 🛑 >>>
UT_inductor_Part_01_213 🛑 >>>
UT_inductor_Part_02_213 🛑 >>>
UT_inductor_Part_03_213 🛑 >>>
UT_inductor_Part_04_213 🛑 >>>
UT_DIST_ARM_Part_01_213 🛑 >>>
UT_DIST_ARM_Part_02_213 🛑 >>>
UT_DIST_ARM_Part_03_213 🛑 >>>
UT_DIST_ARM_Part_04_213 🛑 >>>
UT_ARM_A2_Select_Part_01_213 🛑 >>>
UT_ARM_A2_Select_Part_02_213 🛑 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse, WSs_321]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robotascend-robot成员
21 天前 合入了pull request
ascend-robot
ascend-robot成员
21 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14329 [ commitID:5895efa7 ] 已完成
likedislike