已合并
[test] Add fx graph internal API verify tests #35604
[test] Add fx graph internal API verify tests #35604
已合并
lihaokun-2026创建于 5月14日
lihaokun-2026
lihaokun-2026
5月14日

【合入来源】

如有社区issue,请关联issue链接
【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(25) #1631
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改背景】

本次任务聚焦 torch.fx.graph 相关内部能力的测试补齐与行为验证,主要覆盖 FX 图代码生成过程中涉及的内部辅助接口及状态维护逻辑。

结合目标分支实际源码核查后发现,torch.fx.graph.py 中相关能力的暴露形式并不完全一致:

  • torch.fx.graph._format_targettorch.fx.graph._is_from_torchtorch.fx.graph._origin_type_maptorch.fx.graph._register_custom_builtintorch.fx.graph 模块级内部对象,可直接进行 API 级测试;
  • torch.fx.graph._format_args 在当前目标分支中并非 torch.fx.graph 模块级函数,也不是 CodeGen 类方法,而是定义在 CodeGen._gen_python_code() 内部的局部 helper 函数,无法通过 torch.fx.graph._format_args 直接访问。

因此,本次 PR 对 torch.fx.graph._format_args 不进行测试验证。

本次任务范围为 test/ 目录下测试用例开发与验证,不涉及 torch_npu/ 目录实现逻辑修改。

【资料支持情况核查】

核查结论:

  1. torch.fx.graph._format_target 为模块级内部函数,可直接验证目标路径格式化行为;
  2. torch.fx.graph._is_from_torch 为模块级内部函数,可直接验证 torch 对象识别行为;
  3. torch.fx.graph._origin_type_map 为模块级内部映射表,可直接验证内置容器类型映射行为;
  4. torch.fx.graph._register_custom_builtin 为模块级内部函数,可直接验证自定义 builtin 注册及内部状态更新行为;
  5. torch.fx.graph._format_args 当前不是模块级 API,而是 CodeGen._gen_python_code() 内部局部函数,因此测试中不进行测试验证。

同时在torch官方社区中并没有针对这5个api的测试方法,同时由于torch.fx.graph._format_args 当前不是模块级 API,而是 CodeGen._gen_python_code() 内部局部函数,因此测试中不进行测试验证,因此需要在本pr中新增对4个api的测试方法,测试范围已与当前目标分支实际源码保持一致,避免将局部 helper 错误声明为模块级 API。
本次提交对应 Torch-NPU API 补齐任务,涉及以下 4 个 API:

API / 行为 作用说明 备注
torch.fx.graph._format_target 将 FX Node 的 target 路径格式化为合法的 Python 属性访问表达式。对于非法 Python 标识符,会转换为 getattr(...) 形式。 主要用于 call_methodcall_moduleget_attr 等节点的代码生成。
torch.fx.graph._is_from_torch 判断一个对象是否来自 torch 命名空间,用于区分 torch 原生对象和用户自定义对象。 FX codegen 可据此决定是否直接生成类似 torch.add(...) 的调用形式。
torch.fx.graph._origin_type_map.get 查询内置容器类型到 typing 类型的映射关系,例如 list -> typing.Listdict -> typing.Dict 主要用于 FX 代码生成过程中的类型注解处理。
torch.fx.graph._register_custom_builtin 注册 FX codegen 需要识别的自定义 builtin 对象,并更新 _custom_builtins_illegal_names 内部状态。 用于保证生成代码能够正确引用特定全局对象,同时避免变量名与 builtin 名称冲突。

【修改方案】

新增测试文件:

test/fx/test_fx_graph_internal.py

文件顶部补充用途说明,明确该文件用于承载 torch.fx.graph 相关内部 API 及 CodeGen 行为的兼容性验证。

当前测试覆盖以下 4个api :

  1. torch.fx.graph._format_target
  2. torch.fx.graph._is_from_torch
  3. torch.fx.graph._origin_type_map.get
  4. torch.fx.graph._register_custom_builtin

本次采用最小化修改方案:

  • 仅新增 test/ 下测试文件;
  • 不修改 torch_npu/ 目录下实现代码;
  • 不引入与本次任务无关的功能改动;
  • 不使用 try / except 异常捕获逻辑,测试中如出现异常直接暴露;

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

测试场景:

  1. torch.fx.Graph的4个API进行验证
  2. NPU 设备下 tensor shape 场景验证

测试方法:

export TORCH_DEVICE_BACKEND_AUTOLOAD=1 
python /test/fx/test/fx/test_fx_graph_internal.py -v

测试结果
image.png
所有api功能性测试均已通过

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

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

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

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

likedislike
openLiBingCI成员
5月14日 评论:

本PR中共发现代码检查告警抑制1处,请Committer检视合理性

本评论自动扫描PR中使用的开源代码检查工具(ruff、clang-tidy、CodeQL等)的屏蔽注释。
这些屏蔽注释会阻止开源代码检查工具对特定代码区域的检测,可能导致潜在问题被忽略。

点击下载完整报告

文件路径 行号 屏蔽类型 代码片段 工具名称
test/fx/test_fx_graph_internal.py 3 行级屏蔽 1: import inspect
3: import torch_npu # noqa: F401
5: import torch
6: import torch.fx.graph as fx_graph
7: from torch.testing._internal.common_utils import run_tests, TestCase
10: class TestFxGraphApi(TestCase):
11: def test_format_args(self):
flake8
likedislike
此处折叠了79条消息 查看更多
sunyu-xuan成员
5月21日 评论:

/lgtm

likedislike
liwei386成员
5月21日 评论:

/approve

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

Review Guide

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

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