已合并
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43586
木路折创建于 8月2日
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43586
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 木路折 的贡献)8月2日 关联了issue:【社区任务】7月社区任务第四期-Ascend for PyTorch API 一致性开发(11)
atomgit-bot
8月2日 评论:
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: 定义一个简单的Model(forward返回(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()返回None;test_optimize_model_valid断言优化后model与model.graph非空;test_optimize_idempotent验证连续调用两次不报错;test_optimize_preserves_io_count验证优化前后图的输入输出数量保持一致。


不准确?
ascend-robot
8月2日 评论:
8月2日 评论:
atomgit-bot
8月2日 评论:
8月2日 评论:
此处折叠了99条消息 查看更多
18 天前 删除了label:ci-pipeline-running
18 天前 添加了label:ci-pipeline-passed
ascend-robot
18 天前 评论:
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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


18 天前 合入了pull request
ascend-robot
18 天前 评论:
18 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14327 [ commitID:6e2bf308 ] 已完成


【合入来源】
关联 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=False是torch.onnx.export()的入参,与本方法无关。本次新增test/onnx/exporter/test_onnx_program.py,通过torch.onnx.export(dynamo=True, optimize=False)创建未优化的 ONNXProgram,再调用optimize()直接验证:optimize()返回值为 None;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_DEVICE_BACKEND_AUTOLOAD=0 python -m pytest test/onnx/exporter/test_onnx_program.py -v======================== 4 passed, 2 warnings in 10.46s ========================【CheckList】