已合并
[test] add fx graph api coverage for find_nodes and graph_copy #36537
AZT创建于 5月23日
[test] add fx graph api coverage for find_nodes and graph_copy #36537
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 AZT 的贡献)5月23日 关联了issue:【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(27)
ascend-robot
5月23日 评论:
5月23日 评论:
ascend-robot
5月23日 评论:
5月23日 评论:
Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| test | ✅ sunyu-xuan, 李伟 (2/2) | ✅ 李伟 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
anning-2026, thanks for your pull request. All authors of the commits have signed the CLA. 👍


5月23日 添加了label:ascend-cla/yes
此处折叠了119条消息 查看更多
sunyu-xuan
6月4日 评论:
6月4日 评论:
/lgtm


6月5日 添加了label:approvedlgtm
6月5日 合入了pull request
ascend-robot
6月5日 评论:
6月5日 评论:
流水线 pytorch_gitcode_PR_multiVersion#10115 [ commitID:cc4b8552 ] 已完成


【合入来源】
公开issue:https://gitcode.com/Ascend/pytorch/issues/1633
本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API:
【API 功能介绍】
【社区用例分析】
1.3 类 API(上游无专门用例,自行编写并提交 PR)
torch.fx.Graph.find_nodes
上游无专门测试。虽然在 test/fx/test_fx_node_hook.py 和 test/export/test_export.py 中有间接使用,但都是作为辅助手段调用,没有针对 find_nodes 本身的参数(op、target、sort)和返回值进行验证。因此需要新增用例。
torch.fx.Graph.graph_copy
上游无专门测试。虽然在 test/test_fx.py 中有间接使用,但没有针对 graph_copy 的 val_map 填充、节点复制正确性、已有节点复用等核心行为进行独立验证。因此需要新增用例。
torch.fx.Graph.erase_node
上游在 test/fx/test_fx_node_hook.py 的 test_hooks_for_node_update 中有间接调用,但该测试核心目的是验证 Node Hook 机制,erase_node 仅作为清理手段,未对节点删除后的图结构完整性和计算正确性进行独立验证。因此需要新增用例。
torch.fx.Graph.get_attr
上游在 test/test_fx.py 的多个测试函数中有间接使用 get_attr 节点,但都是在验证其他功能时作为图的组成部分出现,没有针对 get_attr 节点的创建、多属性获取等核心行为进行独立验证。因此需要新增用例。
1.2 类 API(NPU 无需任何修改)
torch.fx.Graph.eliminate_dead_code
上游用例文件:test/fx/test_dce_pass.py,共 7 个测试函数(test_simple、test_dead_chain、test_dead_getattr、test_dead_placeholder、test_dead_placeholder_with_user、test_keep_module_with_side_effects、test_keep_torch_assert)。
源文件已自带 import torch_npu 和 import torch_npu.testing,无需 patch,直接运行全部通过。该 API 为纯图结构操作,NPU 无需任何修改。
【修改方案】
其中 eliminate_dead_code 源文件已自带 NPU 支持,无需修改。
对 find_nodes、graph_copy、erase_node、get_attr 各补充 1 个测试用例到 test/fx/test_fx_graph_api.py,在功能验证充分的前提下尽量简化,每个 API 聚焦一个用例覆盖核心功能:
【资料变更】
不涉及。本 PR 仅补测试,不包含资料修改。
【接口变更】
不涉及。未修改跨仓接口,也未修改用户可见接口行为。
【功能验证】
测试场景1:torch.fx.Graph.find_nodes、graph_copy、erase_node、get_attr 的核心行为验证。
python test/fx/test_fx_graph_api.py
测试日志:
test_erase_node ... ok
test_find_nodes ... ok
test_get_attr ... ok
test_graph_copy ... ok
test_graph_inserting_after ... ok
test_graph_inserting_before ... ok
test_inplace_methods_format_codegen ... ok
test_magic_methods_format_codegen ... ok
Ran 8 tests in 12.657s
OK
测试场景2:torch.fx.Graph.eliminate_dead_code 上游用例验证(1.2 类,源文件已自带 NPU 支持)。
python test/fx/test_dce_pass.py
测试日志:exit code 0,7/7 tests passed。
#1633