已合并
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43586
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43586
已合并
木路折创建于 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
Pull Request已成功合入, 合并人@ascend-robot
(感谢 木路折 的贡献)
木路折木路折
8月2日 创建了 pull request,commit b8be91be
木路折木路折
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(共新增 75 行),在 NPU 上验证 ONNXProgram.optimize() 的正确性。测试通过 torch.onnx.export(dynamo=True, optimize=False) 创建未优化的 ONNXProgram 后调用 optimize(),覆盖返回值、模型有效性、幂等性和输入输出数量保持等场景。

主要改动

  • 新增测试类 ONNXProgramOptimizeTest: 继承 common_utils.TestCase,作为 ONNXProgram.optimize 的专项测试入口,文件末尾通过 common_utils.run_tests() 运行测试。
  • 新增辅助方法 _create_onnx_program: 定义一个简单的 Modelforward 返回 (x + 1) * 2),通过 torch.onnx.export(dynamo=True, optimize=False, verbose=False) 创建未优化的 ONNXProgram,作为各测试用例的公共前置。
  • 设备自适应: 通过 torch.accelerator.current_accelerator() 获取当前加速器类型并据此创建输入张量(NPU 或 CPU),使测试可在 NPU 上运行。
  • 新增 4 个测试用例: test_optimize_returns_none 断言 optimize() 返回 Nonetest_optimize_model_valid 断言优化后 modelmodel.graph 非空;test_optimize_idempotent 验证连续调用两次不报错;test_optimize_preserves_io_count 验证优化前后图的输入输出数量保持一致。
likedislike
不准确?
atomgit-bot
atomgit-bot
8月2日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
此处折叠了99条消息 查看更多
ascend-robotascend-robot成员
18 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
18 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
18 天前 评论:
流水线 PR-pipeline_pytorch#62707 [ commitID:6e2bf308 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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成员
18 天前 合入了pull request
ascend-robot
ascend-robot成员
18 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14327 [ commitID:6e2bf308 ] 已完成
likedislike