Files
TorchNPU/test/fx/test_fx_codegen_api.py
T
nannan-2026 a236c6371f test(fx): add tests for graph codegen api
Co-authored-by: nannan-2026<1794949109@qq.com>



# message auto-generated for no-merge-commit merge:
!34465 merge test-fx-codegen-api-master into master

test(fx): add tests for graph codegen api

Created-by: nannan-2026
Commit-by: nannan-2026
Merged-by: ascend-robot
Description: # 【合入来源】

- [ ] 需求
- [ ] 问题单
- [x] issue/工单
- [ ] 重构优化
- [ ] 资料更新

关联 issue:https://gitcode.com/Ascend/pytorch/issues/1637

# 【修改方案】

本 PR 新增 torch.fx 代码生成相关 API 的单元测试,覆盖如下 API:

- torch.fx.Graph.python_code
- torch.fx.graph.PythonCode
- torch.fx.Graph.set_codegen

具体修改内容:

1. 新增 `test/test_fx_codegen_api.py` 测试文件。
2. 增加 `test_graph_python_code_returns_python_code` 用例,验证 `Graph.python_code("self")` 返回对象类型为 `PythonCode`,并校验返回对象包含 `src`、`globals` 字段。
3. 增加 `test_graph_set_codegen` 用例,验证 `Graph.set_codegen` 设置自定义 `CodeGen` 后,`GraphModule` 可重新编译并正常执行。
4. 该组 API 属于 torch.fx Python 层代码生成接口,不涉及新增 NPU 算子实现。

# 【资料变更】

不涉及。

本 PR 仅新增测试用例,不修改资料文档。如后续确认 `torch.fx.graph.PythonCode` 在 native_apis 文档中缺失,将单独提交 docs PR 补充。

# 【接口变更】

不涉及。

本 PR 不修改客户可见接口,不修改 API 行为,仅新增测试用例验证现有 API 兼容性。

# 【功能验证】

测试场景:

1. 验证 `torch.fx.Graph.python_code` 可正常生成 PythonCode。
2. 验证 `torch.fx.graph.PythonCode` 对象包含 `src` 和 `globals` 字段。
3. 验证 `torch.fx.Graph.set_codegen` 设置自定义 CodeGen 后,GraphModule 可重新编译并正常执行。

测试命令:

`TORCH_DEVICE_BACKEND_AUTOLOAD=0 python test/test_fx_codegen_api.py -v`

测试结果:

`test_graph_python_code_returns_python_code (__main__.TestFXCodegenAPI.test_graph_python_code_returns_python_code) ... ok`

`test_graph_set_codegen (__main__.TestFXCodegenAPI.test_graph_set_codegen) ... ok`

`Ran 2 tests in 0.168s`

`OK`

说明:

该 API 为 torch.fx Python 层代码生成接口,不涉及 NPU 算子计算。本次测试主要验证 API 功能行为与 PyTorch 原生保持一致。

# 【CheckList】

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常

See merge request: Ascend/pytorch!34465
2026-05-11 17:14:52 +08:00

49 lines
1.5 KiB
Python

# Owner(s): ["module: fx"]
import torch
from torch.fx import symbolic_trace
from torch.fx.graph import CodeGen, PythonCode
from torch.testing._internal.common_utils import run_tests, TestCase
class TestFXCodegenAPI(TestCase):
def test_graph_python_code_returns_python_code(self):
def fn(x, y):
return torch.relu(x + y)
gm = symbolic_trace(fn)
python_code = gm.graph.python_code("self")
self.assertIsInstance(python_code, PythonCode)
self.assertTrue(hasattr(python_code, "src"))
self.assertTrue(hasattr(python_code, "globals"))
self.assertIsInstance(python_code.src, str)
self.assertIsInstance(python_code.globals, dict)
self.assertIn("def forward", python_code.src)
def test_graph_set_codegen(self):
class ListCodeGen(CodeGen):
def gen_fn_def(self, free_vars, maybe_return_annotation):
return f"""def forward(self, args_list){maybe_return_annotation}:
{", ".join(free_vars)} = args_list"""
def process_inputs(self, *inputs):
if len(inputs) != 1:
raise RuntimeError("Expected exactly one input")
return inputs[0]
def fn(x, y):
return x + y
gm = symbolic_trace(fn)
gm.graph.set_codegen(ListCodeGen())
gm.recompile()
x = torch.randn(2, 3)
y = torch.randn(2, 3)
self.assertEqual(gm([x, y]), x + y)
if __name__ == "__main__":
run_tests()