已合并
test(fx): add graph_module internal API alignment test cases [v2.12.0] #43640
test(fx): add graph_module internal API alignment test cases [v2.12.0] #43640
已合并
zkx创建于 18 天前
zkx
18 天前

【合入来源】

https://gitcode.com/Ascend/pytorch/issues/3639

【修改方案】

为 Issue #3639 新增 torch.fx.graph_module 内部私有 API 的 NPU 一致性验证用例。PyTorch 官方社区无针对这些私有 API 的专门测试用例,因此自写用例提交到 test 目录(场景 1.3)。

一、API功能

1. torch.fx.graph_module._exec_with_source:编译并执行 Python 源码字符串,通过 exec(compile(...)) 与 _EvalCacheLoader 维护源码缓存。
2. torch.fx.graph_module._forward_from_src:从源码中提取 forward 函数,GraphModule.recompile() 依赖此接口动态生成 forward 方法。
3. torch.fx.graph_module._CodeOnlyModule:GraphModule 反序列化的轻量载体,通过 self.__dict__ = body 注入属性。
4. torch.fx.graph_module._copy_attr:反序列化时模块间属性迁移,自动区分 Tensor→register_buffer、Parameter/其他→setattr,支持嵌套路径。
5. torch.fx.graph_module._WrappedCall:__call__ 错误诊断增强包装器,异常时生成含源码行定位的错误信息。

二、测试用例完备性说明

PyTorch 官方社区未对这 5 个 _ 前缀私有 API 提供专门测试用例(在 torch 源码 test/ 目录全文检索各 API 短名称均无命中),社区验证不充分,故新增 test/test_fx_graph_module_npu.py:

1. _exec_with_source:test_exec_with_source_basic(单行赋值)、test_exec_with_source_multiple(多行多变量)
2. _forward_from_src:test_forward_from_src_basic(forward 提取与调用)、test_forward_from_src_with_imports(含 import 与 torch.relu 的头文件)
3. _CodeOnlyModule:test_code_only_module_basic(属性注入)、test_code_only_module_empty(空 body 边界)
4. _copy_attr:test_copy_attr_tensor(buffer 复制)、test_copy_attr_parameter(Parameter setattr 路径)、test_copy_attr_nested(嵌套路径递归创建)、test_copy_attr_npu_tensor(NPU 张量设备保留)
5. _WrappedCall:test_wrapped_call_basic(super().__call__ 透传)、test_wrapped_call_with_cls_call(cls_call 路径)

6. 新增异常/边界用例:_exec_with_source 非法源码与无效命名空间、_forward_from_src 缺失 forward 源码、
   _copy_attr 缺失属性与已存在父模块、_WrappedCall 异常诊断路径,验证各 API 的关键失败分支。

6. 新增 co_fields 用例:_exec_with_source_co_fields、_forward_from_src_co_fields,验证传入 co_fields(co_filename/co_firstlineno/co_name)时
   generate 代码仍正确执行,且缓存 key 保留预期的 code-object 字段标识。

覆盖正常场景 + 边界场景(空 body/异常入参)+ 设备迁移场景(NPU 张量),共 20个用例。

三、NPU适配
3.1 API适配:无需修改。5 个 API 均为纯 Python 框架层逻辑(exec/compile、字典/字符串操作、traceback 格式化、Module 属性复制),与底层硬件完全解耦,无 CUDA/NPU 特有代码路径,API 层面无需任何修改即可在 NPU 环境正常运行。
3.2 测试用例适配:新增用例在 import 后添加设备获取命令 device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu",全部张量通过 .to(device_type) 运行在 NPU 上(CPU 环境回落 cpu)。

【资料变更】

已检查文档,https://gitcode.com/Ascend/pytorch/tree/master/docs/zh/api/native_api 下各版本目录的 API 支持清单。
这 5 个 API 均为 torch.fx.graph_module 内部私有函数(_ 前缀),属于 PyTorch 私有接口,按规则私有接口不需要补充资料,docs 无需变更,不需要提交 PR 修改资料。

【接口变更】

不涉及

【功能验证】

在 v2.12.0 对应 torch 版本环境执行新增测试文件,20个用例全部通过。

$ python3 test/test_fx_graph_module_npu.py
............
----------------------------------------------------------------------
Ran 20 tests in 1.610s
OK

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zkx 的贡献)
Zzkx
18 天前 创建了 pull request,commit 6a3a9d53
Zzkx
18 天前 关联了issue:[Usage]: API一致性说明:torch.fx.graph_module 私有 API 需做 NPU 测试适配,[Usage]: API一致性说明:torch.fx.graph_module 私有 API 需做 NPU 测试适配
atomgit-bot
atomgit-bot
18 天前 评论:

变更摘要

此 PR 为 torch.fx.graph_module 的 5 个内部私有 API 新增 NPU 一致性验证用例,解决 PyTorch 社区对这些 _ 前缀 API 缺乏专门测试的问题。新增文件 test/test_fx_graph_module_npu.py 包含 5 个测试类,覆盖 _exec_with_source_forward_from_src_CodeOnlyModule_copy_attr_WrappedCall 的核心功能路径,并支持通过 torch.accelerator.current_accelerator() 自动适配 NPU 设备。

主要改动

  • 新增 TestExecWithSource 测试类:验证 _exec_with_source 对单行赋值和跨行变量引用场景的源码编译执行能力。
  • 新增 TestForwardFromSrc 测试类:验证 _forward_from_src 从源码字符串提取 forward 函数后,在 NPU 张量上正确执行基础运算和 torch.relu
  • 新增 TestCodeOnlyModule 测试类:验证 _CodeOnlyModule 通过 body 字典注入属性以及空字典初始化后仍为 nn.Module 实例。
  • 新增 TestCopyAttr 测试类:验证 _copy_attrregister_buffer(Tensor)、register_parameter(Parameter)、嵌套路径属性迁移及 NPU 设备张量的正确复制行为。
  • 新增 TestWrappedCall 测试类:验证 _WrappedCall 包装器在未指定 __call__ 和显式传入 forward 两种场景下的调用与计算正确性。
likedislike
atomgit-bot
atomgit-bot
18 天前 评论:

代码审查

✅ 未发现问题

likedislike
此处折叠了119条消息 查看更多
梁松伟
梁松伟成员
6 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
6 天前 添加了label:approvedlgtm
ascend-robotascend-robot成员
6 天前 删除了label:ci-pipeline-passed
ascend-robotascend-robot成员
6 天前 合入了pull request
ascend-robot
ascend-robot成员
6 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14063 [ commitID:ee7a5f91 ] 已完成
likedislike