已合并
test(fx): add tests for graph codegen api #34460
test(fx): add tests for graph codegen api #34460
已合并
nwww创建于 4月26日
nwww
4月26日

【合入来源】

关联 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/fx/test_fx_codegen_api.py 测试文件。
  2. 增加 test_graph_python_code_returns_python_code 用例,验证 Graph.python_code("self") 返回对象类型为 PythonCode,并校验返回对象包含 srcglobals 字段。
  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 对象包含 srcglobals 字段。
  3. 验证 torch.fx.Graph.set_codegen 设置自定义 CodeGen 后,GraphModule 可重新编译并正常执行。

测试命令:

TORCH_DEVICE_BACKEND_AUTOLOAD=0 python -u test/fx/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等
- [ ] PR持续集成流水线(CI)执行通过,代码检查无异常

















likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 nwww 的贡献)
Nnwww
4月26日 创建了 pull request,commit 8e3fab84
Nnwww
4月26日 关联了issue:【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(31)
ascend-robot
ascend-robot成员
4月26日 评论:

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 李伟, huangjingwei (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

nannan-2026, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
4月26日 添加了label:ascend-cla/yes
此处折叠了92条消息 查看更多
Jingwei Huang
Jingwei Huang成员
5月7日 评论:

/lgtm

likedislike
liwei386成员
5月11日 评论:

/approve

likedislike
ascend-robotascend-robot成员
5月11日 添加了label:approvedlgtm
ascend-robot
ascend-robot成员
5月11日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: 李伟.
Reviewers who wrote a comment of /lgtm are: 李伟, huangjingwei.

likedislike
ascend-robotascend-robot成员
5月11日 合入了pull request