Co-authored-by: baoxuebin_2026<xuebin@isrc.iscas.ac.cn> # message auto-generated for no-merge-commit merge: !34556 merge codex/fx-graph-apis-master into master [test] add fx graph api coverage Created-by: baoxuebin_2026 Commit-by: baoxuebin_2026 Merged-by: ascend-robot Description: # 【合入来源】 > 如有社区issue,请关联issue链接 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 公开issue:暂无单独公开 issue 链接,本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API: - `torch.fx.graph.map_arg` - `torch.fx.Graph.node_copy` - `torch.fx.Graph.nodes` - `torch.fx.Graph.on_generate_code` - `torch.fx.Graph.output` # 【修改方案】 1. 对目标 API 进行现状核查,确认在当前 `PyTorch 2.9.0 + torch_npu 2.9.0rc1` 环境中已实际支持,不需要补齐底层实现。 2. 补充独立测试文件 `test/fx/test_graph_api.py`,覆盖以下场景: - `map_arg` 对嵌套参数结构的映射行为 - `Graph.node_copy` 的图节点复制行为 - `Graph.nodes` 的拓扑顺序遍历行为 - `Graph.on_generate_code` 对生成代码的 hook 行为 - `Graph.output` 在 NPU 输入场景下的实际执行验证 3. 采用最小化修改方案,不改动已有底层实现,仅补齐测试覆盖。 # 【资料变更】 不涉及。本文 PR 仅补测试,不包含资料修改。 # 【接口变更】 不涉及。未修改跨仓接口,也未修改用户可见接口行为。 # 【功能验证】 测试场景: 1. `torch.fx` 图相关 API 的核心行为验证 2. NPU 设备下 `Graph.output` 的实际运行验证 测试方法: ```bash cd /data/pytorch TORCH_DEVICE_BACKEND_AUTOLOAD=1 python3.11 test/fx/test_graph_api.py -v [#1635](https://gitcode.com/Ascend/pytorch/issues/1635) See merge request: Ascend/pytorch!34556
86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
# Owner(s): ["module: fx"]
|
|
|
|
import unittest
|
|
|
|
import torch
|
|
import torch_npu
|
|
from torch.fx import Graph, GraphModule
|
|
from torch.fx.graph import map_arg
|
|
from torch.testing._internal.common_utils import TestCase, run_tests
|
|
|
|
|
|
class TestGraphAPI(TestCase):
|
|
def _build_add_graph(self):
|
|
graph = Graph()
|
|
x = graph.placeholder("x")
|
|
y = graph.placeholder("y")
|
|
add = graph.call_function(torch.ops.aten.add.Tensor, args=(x, y))
|
|
output = graph.output(add)
|
|
return graph, x, y, add, output
|
|
|
|
def test_map_arg_preserves_nested_structure(self):
|
|
_, x, y, _, _ = self._build_add_graph()
|
|
|
|
mapped = map_arg(
|
|
((x,), {"rhs": [y], "tag": "graph"}),
|
|
lambda node: node.name,
|
|
)
|
|
|
|
self.assertEqual(mapped[0], ("x",))
|
|
self.assertEqual(mapped[1]["rhs"], ["y"])
|
|
self.assertEqual(mapped[1]["tag"], "graph")
|
|
|
|
def test_node_copy_rebuilds_graph(self):
|
|
graph, _, _, _, _ = self._build_add_graph()
|
|
copied = Graph()
|
|
value_map = {}
|
|
|
|
for node in graph.nodes:
|
|
if node.op == "placeholder":
|
|
value_map[node] = copied.placeholder(node.target)
|
|
else:
|
|
value_map[node] = copied.node_copy(node, lambda n: value_map[n])
|
|
|
|
self.assertEqual(
|
|
[node.op for node in copied.nodes],
|
|
["placeholder", "placeholder", "call_function", "output"],
|
|
)
|
|
|
|
def test_nodes_iterates_in_topological_order(self):
|
|
graph, _, _, add, output = self._build_add_graph()
|
|
|
|
self.assertEqual(
|
|
[node.op for node in graph.nodes],
|
|
["placeholder", "placeholder", "call_function", "output"],
|
|
)
|
|
self.assertEqual(list(graph.nodes)[-2:], [add, output])
|
|
|
|
def test_on_generate_code_updates_graph_module_code(self):
|
|
graph, _, _, _, _ = self._build_add_graph()
|
|
gm = GraphModule(torch.nn.Module(), graph)
|
|
|
|
def transform_code(code):
|
|
return ['print("fx-hook")\n', *code]
|
|
|
|
gm.graph.on_generate_code(lambda _: transform_code)
|
|
gm.recompile()
|
|
|
|
self.assertIn('print("fx-hook")', gm.code)
|
|
|
|
@unittest.skipUnless(torch.npu.is_available(), "requires npu")
|
|
def test_output_runs_with_npu_inputs(self):
|
|
graph, _, _, _, output = self._build_add_graph()
|
|
gm = GraphModule(torch.nn.Module(), graph)
|
|
x = torch.randn(2, 2).npu()
|
|
y = torch.randn(2, 2).npu()
|
|
|
|
result = gm(x, y).cpu()
|
|
|
|
self.assertEqual(output.op, "output")
|
|
self.assertEqual(result.shape, torch.Size([2, 2]))
|
|
self.assertEqual(result, (x + y).cpu())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|