已开启
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43587
木路折创建于 8月2日
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43587
已开启
合并受阻
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。该测试通过 torch.onnx.export(dynamo=True, optimize=False) 创建未优化的 ONNXProgram,随后直接调用 ONNXProgram.optimize() 验证其正确性,同时支持在 NPU 设备(torch.accelerator.current_accelerator() 获取的设备类型)上运行。测试类 ONNXProgramOptimizeTest 覆盖了 optimize() 的返回值、模型有效性、幂等性以及输入输出数量保持四个方面。
主要改动
- 新增测试类
ONNXProgramOptimizeTest:在新增文件test/onnx/exporter/test_onnx_program.py中定义,继承common_utils.TestCase,并封装了_create_onnx_program()辅助方法,通过torch.onnx.export(..., dynamo=True, optimize=False)构造未优化的ONNXProgram供各用例复用。 - 新增
test_optimize_returns_none用例:断言onnx_program.optimize()的返回值为None,验证该 API 优化原地生效且无返回值。 - 新增
test_optimize_model_valid用例:调用optimize()后断言onnx_program.model及其graph均非空,确认优化后的模型仍然有效。 - 新增
test_optimize_idempotent用例:连续两次调用onnx_program.optimize()不抛异常,验证该方法可重复调用(幂等性)。 - 新增
test_optimize_preserves_io_count用例:对比优化前后onnx_program.model.graph.inputs与outputs的长度,验证优化过程保持模型的输入输出数量不变。


ascend-robot
8月2日 评论:
8月2日 评论:
atomgit-bot
8月2日 评论:
8月2日 评论:
此处折叠了193条消息 查看更多
2 小时前 删除了label:docs-ci-pipeline-running
2 小时前 添加了label:docs-ci-pipeline-success
1 小时前 删除了label:ci-pipeline-running
1 小时前 添加了label:ci-pipeline-passed
AtlasAccount
1 小时前 评论:
1 小时前 评论:
流水线 PR-pipeline_pytorch#66269 [ commitID:e4f9904f ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ COMPLETED | >>> |
| Build_ARM | ✅ COMPLETED | >>> | |
| Build_X86_torchair | ⚪ IGNORED | >>> | |
| Build_ARM_torchair | ⚪ IGNORED | >>> | |
| patch_test | ⚪ IGNORED | >>> | |
| Build_X86_213 | ⚪ IGNORED | >>> | |
| Build_ARM_213 | ⚪ IGNORED | >>> | |
| 恶意代码检查 | Antipoison | ✅ COMPLETED | >>> |
| 编码安全与规范检查 | codecheck_pre-commit | ✅ COMPLETED | >>> |
| check_error | ✅ COMPLETED | >>> | |
| lintrunner | ✅ COMPLETED | >>> | |
| 开源片段检查 | SCA | ✅ COMPLETED | >>> |
| 开发者测试 | UT_ARM_A3_Part_01 | ⚪ IGNORED | >>> |
| UT_ARM_A3_Part_02 | ⚪ IGNORED | >>> | |
| UT_ARM_A2_Part_01 | ✅ COMPLETED | >>> | |
| UT_ARM_A2_Part_02 | ✅ COMPLETED | >>> | |
| UT_ARM_A2_Part_03 | ✅ COMPLETED | >>> | |
| UT_inductor_Part_01 | ⚪ IGNORED | >>> | |
| UT_inductor_Part_02 | ⚪ IGNORED | >>> | |
| UT_inductor_Part_03 | ⚪ IGNORED | >>> | |
| UT_inductor_Part_04 | ⚪ IGNORED | >>> | |
| UT_DIST_ARM_Part_01 | ⚪ IGNORED | >>> | |
| UT_DIST_ARM_Part_02 | ⚪ IGNORED | >>> | |
| UT_DIST_ARM_Part_03 | ⚪ IGNORED | >>> | |
| UT_DIST_ARM_Part_04 | ⚪ IGNORED | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ COMPLETED | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ COMPLETED | >>> | |
| UT_inductor_Part_213 | ⚪ IGNORED | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ COMPLETED | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


【合入来源】
关联 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】