已开启
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43585
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43585
已开启
木路折创建于 8月2日
木路折
木路折
8月2日

【合入来源】

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

  • 任务分类:1.2②(PyTorch 官方社区无用例,新增用例到 test 目录)

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

【修改方案】

一、API 功能说明

torch.onnx.ONNXProgram.optimize() 是 ONNXProgram 类的公开方法,用于优化 ONNX 模型。该方法通过调用 onnxscript_apis.optimize(self.model) 对 ONNX IR 模型执行常量折叠和冗余消除,优化原地生效,返回值为 None。该 API 为非计算类 API,操作对象是 ONNX 模型图而非 PyTorch 张量,与数据类型和设备无关。

二、测试用例完备性说明

PyTorch 官方 test/onnx/exporter/test_onnx_program.py 中仅有 ONNXProgramRenameAxesTest 测试 rename_axes 方法,没有直接调用 ONNXProgram.optimize() 的测试用例。其他测试中的 optimize=Falsetorch.onnx.export() 的入参,与本方法无关。本次新增 test/onnx/exporter/test_onnx_program.py,通过 torch.onnx.export(dynamo=True, optimize=False) 创建未优化的 ONNXProgram,再调用 optimize() 直接验证:

  • optimize() 返回值为 None;
  • 优化后模型仍然有效(model 和 graph 非空);
  • optimize() 可重复调用不报错(幂等性);
  • 优化后模型的输入输出数量保持不变。

三、NPU 适配

测试通过 torch.onnx.export() 创建 ONNXProgram,输入张量通过 device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" 迁移到 NPU。optimize() 本身为非计算类 API,操作 ONNX IR 图结构,与设备无关。该测试代码在 v2.7.1 和 v2.12.0 间无需版本差异处理。

【资料变更】

不涉及

已检查:torch.onnx.ONNXProgram.optimize 在 docs/zh/api/native_api 的 pytorch_2-7-1、pytorch_2-11-0、pytorch_2-12-0、pytorch_2-13-0 四个版本目录的 torch-onnx.md 中均未收录,资料已在 PR https://gitcode.com/Ascend/pytorch/pull/43671 补充。

【接口变更】

不涉及

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

【功能验证】

  • 本地 NPU 运行(torch 2.7.1+cpu)
    • 命令:TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest test/onnx/exporter/test_onnx_program.py -v
    • 结果:======================== 4 passed, 2 warnings in 10.46s ========================

【CheckList】

likedislike
合并受阻
木路折木路折
8月2日 创建了 pull request,commit c84fbe57
木路折木路折
8月2日 关联了issue:【社区任务】7月社区任务第四期-Ascend for PyTorch API 一致性开发(11)
atomgit-bot
atomgit-bot
8月2日 评论:

变更摘要

本 PR 针对 PyTorch 社区缺少对 torch.onnx.ONNXProgram.optimize 独立测试的问题(关联 issue #3490),在 test/onnx/exporter/test_onnx_program.py 中新增测试类 ONNXProgramOptimizeTest,用于在 NPU 上验证该 API 的正确性。测试通过 torch.onnx.export(dynamo=True, optimize=False) 创建未优化的 ONNXProgram,再调用 optimize() 方法,并借助 torch.accelerator.current_accelerator() 动态选择设备(NPU 或 CPU)。

主要改动

  • 新增测试类 ONNXProgramOptimizeTest 及辅助方法 _create_onnx_program:在新增文件 test/onnx/exporter/test_onnx_program.py 中定义测试类,辅助方法通过 torch.onnx.export(dynamo=True, optimize=False, verbose=False) 基于简单模型((x + 1) * 2)创建未优化的 ONNXProgram,并依据当前加速器类型将输入数据放到对应设备上。
  • 新增 test_optimize_returns_none 用例:断言 optimize() 的返回值为 None
  • 新增 test_optimize_model_valid 用例:调用 optimize() 后断言 onnx_program.model 及其 graph 均非空,验证优化后模型仍然有效。
  • 新增 test_optimize_idempotent 用例:连续两次调用 optimize() 不抛异常,验证该方法可重复调用(幂等性)。
  • 新增 test_optimize_preserves_io_count 用例:对比优化前后 model.graph.inputsmodel.graph.outputs 的数量,验证优化不改变模型输入输出个数。
likedislike
atomgit-bot
atomgit-bot
8月2日 评论:

代码审查

✅ 未发现问题

likedislike
此处折叠了204条消息 查看更多
ascend-robotascend-robot成员
7 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7 天前 添加了label:docs-ci-pipeline-success
AtlasAccountAtlasAccount成员
7 天前 删除了label:ci-pipeline-running
AtlasAccountAtlasAccount成员
7 天前 添加了label:ci-pipeline-passed
AtlasAccount
AtlasAccount成员
7 天前 评论:
流水线 PR-pipeline_pytorch#64880 [ commitID:7ea5de02 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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_213 🛑 >>>
UT_inductor_Part_213 🛑 >>>
UT_DIST_ARM_Part_213 🛑 >>>
UT_ARM_A2_Select_Part_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, SCh_zx]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike