已合并
[test] add fx graph api coverage for find_nodes and graph_copy #36537
[test] add fx graph api coverage for find_nodes and graph_copy #36537
已合并
AZT创建于 5月23日
AZT
AZT
5月23日

【合入来源】

如有社区issue,请关联issue链接

公开issue:https://gitcode.com/Ascend/pytorch/issues/1633

本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API:

  • torch.fx.Graph.find_nodes
  • torch.fx.Graph.graph_copy
  • torch.fx.Graph.eliminate_dead_code
  • torch.fx.Graph.erase_node
  • torch.fx.Graph.get_attr

【API 功能介绍】

  • torch.fx.Graph.find_nodes:根据 op 类型和 target 快速查询图中节点,支持 sort 参数控制返回顺序。属于图结构查询操作。
  • torch.fx.Graph.graph_copy:将源图的所有节点复制到目标图中,通过 val_map 映射已有节点,支持节点复用。属于图结构复制操作。
  • torch.fx.Graph.eliminate_dead_code:删除图中无用户的死节点,保留有副作用的节点。属于图结构优化操作。
  • torch.fx.Graph.erase_node:从图中删除指定节点。属于图结构操作。
  • torch.fx.Graph.get_attr:在图中创建一个 get_attr 节点,用于获取模块属性。属于图节点创建操作。

【社区用例分析】

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 聚焦一个用例覆盖核心功能:

  1. torch.fx.Graph.find_nodes:在一个包含 call_function、call_module、get_attr 的图中验证 op/target 查询、sort 参数和空结果场景
  2. torch.fx.Graph.graph_copy:验证基本复制与 val_map 填充、val_map 复用已有节点、return_output_node 参数
  3. torch.fx.Graph.erase_node:删除中间节点并重接依赖,验证图结构和 NPU 计算正确性
  4. torch.fx.Graph.get_attr:通过 symbolic_trace 产生多属性 get_attr 节点,验证节点正确性和 NPU 计算结果

【资料变更】

不涉及。本 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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 AZT 的贡献)
AZTAZT
5月23日 创建了 pull request,commit 0d697b21
AZTAZT
5月23日 关联了issue:【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(27)
ascend-robot
ascend-robot成员
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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

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

/lgtm

likedislike
liwei386成员
6月5日 评论:

/approve

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