已开启
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43585
木路折创建于 8月2日
test: Add validation cases for torch.onnx.ONNXProgram.optimize on NPU #43585
已开启
合并受阻
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 中新增测试类 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.inputs与model.graph.outputs的数量,验证优化不改变模型输入输出个数。


ascend-robot
8月2日 评论:
8月2日 评论:
atomgit-bot
8月2日 评论:
8月2日 评论:
此处折叠了204条消息 查看更多
7 天前 删除了label:docs-ci-pipeline-running
7 天前 添加了label:docs-ci-pipeline-success
7 天前 删除了label:ci-pipeline-running
7 天前 添加了label:ci-pipeline-passed
AtlasAccount
7 天前 评论:
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 | ✅ | >>> |
- 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】