已合并
test(fx): add NPU test cases for torch.fx.GraphModule APIs [v2.9.0] #39584
冬阳创建于 6月30日
test(fx): add NPU test cases for torch.fx.GraphModule APIs [v2.9.0] #39584
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 冬阳 的贡献)ascend-robot
6月30日 评论:
6月30日 评论:
6月30日 添加了label:ascend-cla/yes
ascend-robot
6月30日 评论:
6月30日 评论:
此处折叠了89条消息 查看更多
7月2日 删除了label:ci-pipeline-running
7月2日 添加了label:ci-pipeline-passed
ascend-robot
7月2日 评论:
7月2日 评论:
流水线 PR-pipeline_pytorch#40644 [ commitID:9a9ccdce ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🛑 | >>> |
| UT_X86_Part_02 | 🛑 | >>> | |
| 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 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


7月2日 合入了pull request
ascend-robot
7月2日 评论:
7月2日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11714 [ commitID:9a9ccdce ] 已完成


【合入来源】
Fork: gcw_IDzXRVNw/pytorch_npu
分支:
test/fx-graphmodule-api-v2.9.0→ Ascend/pytorch:v2.9.0关联 Issue: https://gitcode.com/Ascend/pytorch/issues/1856
【修改方案】
本 PR 为用户提供的任务 issue #1856 的交付。社区用例情况:
pytorch/test/test_fx.py下逐一搜索GraphModule.code、GraphModule.graph等 API,确认上游不存在针对这些属性的独立测试用例(仅作为辅助断言在其他测试中引用),因此自行编写测试用例。一、API 功能说明
torch.fx.GraphModule是 PyTorch FX 框架的核心输出类,将fx.Graph转化为可执行的nn.Module,并通过 Python 代码生成实现 Python-to-Python 变换。本次验证的 10 个 API 均为纯 Python 图操作,与底层计算设备完全解耦:torch.fx.GraphModule:nn.Module 子类,持有 fx.Graph 并自动生成 Python forward 代码,是 FX Python-to-Python 变换的基础torch.fx.GraphModule.__init__:从 Module/dict 根对象和 Graph 构造 GraphModule,自动拷贝图中引用的属性和子模块torch.fx.GraphModule.add_submodule:在指定路径添加子模块,自动创建中间占位 Moduletorch.fx.GraphModule.code:只读 property,返回从 Graph 生成的 Python 源码字符串torch.fx.GraphModule.delete_all_unused_submodules:遍历 Graph 节点收集引用,删除未使用的子模块torch.fx.GraphModule.delete_submodule:删除指定路径的子模块,路径无效或为非 Module 时返回 Falsetorch.fx.GraphModule.graph:property (getter/setter),获取底层 Graph 或设置新 Graph(setter 自动触发 recompile)torch.fx.GraphModule.print_readable:生成可读的模块源码打印输出torch.fx.GraphModule.recompile:从 Graph 重新编译forward()方法,返回 PythonCode 对象torch.fx.GraphModule.to_folder:将模块导出到文件夹(含 module.py、state_dict.pt、init.py)上述 API 的实现特征:全部定义在
torch.fx.graph_module.GraphModule上,torch_npu 未做任何覆盖/重写。所有 API 操作对象为 Python 对象(Graph、Node、Module 层级、字符串),不涉及 Tensor 计算或硬件算子。二、测试文件
test_fx_graph_module_api.py用例完备性说明torch.fx.GraphModuletest_init_from_module、test_init_from_dict验证torch.fx.GraphModule.__init__test_init_from_module、test_init_from_dict、test_init_sets_class_name、test_init_raises_on_bad_typetorch.fx.GraphModule.add_submoduletest_add_submodule_root_level、test_add_submodule_nested、test_add_submodule_overwrite_fails_on_non_moduletorch.fx.GraphModule.codetest_code_returns_string、test_code_contains_forward、test_code_contains_op_names、test_code_consistent_after_recompiletorch.fx.GraphModule.delete_all_unused_submodulestest_delete_all_unused_removes_orphans、test_delete_all_unused_preserves_usedtorch.fx.GraphModule.delete_submoduletest_delete_submodule_existing、test_delete_submodule_nested、test_delete_submodule_nonexistent、test_delete_submodule_non_moduletorch.fx.GraphModule.graphtest_graph_getter_returns_graph、test_graph_getter_has_nodes、test_graph_getter_is_consistent、test_graph_setter_reassigns_graph、test_graph_setter_triggers_recompile、test_graph_setter_forward_works、test_graph_setter_raises_on_non_graph、test_graph_setter_preserves_linttorch.fx.GraphModule.print_readabletest_print_readable_returns_string、test_print_readable_contains_child_codetorch.fx.GraphModule.recompiletest_recompile_returns_python_code、test_recompile_preserves_forwardtorch.fx.GraphModule.to_foldertest_to_folder_creates_files、test_to_folder_module_file_content【结论】所有用例覆盖 API 的基础功能、异常行为、接口存在性、以及 NPU 设备场景,34 个测试用例完整覆盖昇腾 NPU 适配所需的最小功能集。
三、昇腾 NPU 适配说明
本次 GraphModule 相关 API 在昇腾 NPU 上的验证采用如下方式,符合硬件适配要求:
TestFxGraphModuleOnNpu类(3 个测试用例),显式将模块和张量迁移到 NPU 设备(npu:0),验证在真实 NPU 环境下code、graph、recompile、forward全部正常工作【结论】本测试文件的设计合理,可充分保证 GraphModule 在昇腾 NPU 环境下的功能正确性与可用性。
【资料变更】
文档变更已在 v2.7.1 分支的 PR 中合入,本 PR 仅提交测试文件。
【接口变更】
不涉及
【功能验证】
test/fx/test_fx_graph_module_api.pytorch 2.7.1+cpu+torch_npu 2.7.1.post4+NPU 910B3+CANN 8.5.0【结论】执行测试用例后,34 passed,所有测试用例在 NPU 环境下执行通过。GraphModule 核心 API 在 NPU 上验证正确。
【CheckList】