已合并
test(fx): add CustomBuiltin test cases for v2.7.1 #34276
test(fx): add CustomBuiltin test cases for v2.7.1 #34276
已合并
olpk创建于 4月24日
olpk
olpk
4月24日
https://gitcode.com/Ascend/pytorch/issues/1630 ## 上游社区(PyTorch 官方)测试覆盖情况分析 ### torch.fx.Graph / torch.fx.Graph.__init__ - **社区测试文件**:`test/test_fx.py`(PyTorch 官方仓库) - **测试覆盖情况**:✅ 充分覆盖 - **证据**:共有 18 个 Graph 相关测试函数 - **结论**:上游社区测试已全面覆盖,torch-npu 无需额外适配 - **验证**: 统计 test_fx.py 中 Graph 相关的测试函数数量 grep -c "def test.*[Gg]raph" /workspace/pytorch/test/test_fx.py 查看具体是哪些测试函数 grep "def test.*[Gg]raph" /workspace/pytorch/test/test_fx.py | head -20 统计 GraphModule 相关的测试 grep -c "def test.*GraphModule" /workspace/pytorch/test/test_fx.py 统计 Graph 类被使用的次数(证明被广泛测试) grep -c "torch.fx.Graph" /workspace/pytorch/test/test_fx.py ![image.png](https://raw.gitcode.com/user-images/assets/7404318/4f48a660-c3d7-41b2-a8ee-bca1d17a1733/image.png 'image.png') 上游社区测试覆盖情况汇总 序号 测试函数 测试场景 1 test_graph_module GraphModule 基本构造和功能 2 test_graph_edit_with_proxy 通过 Proxy 编辑图 3 test_graph_unique_names 节点名称唯一性 4 test_graph_unique_names_manual 手动设置节点名称 5 test_pickle_graphmodule 序列化/反序列化 6 test_deepcopy_graphmodule_with_transform 带变换的深拷贝 7 test_deepcopy_graph_with_tracer_cls 带 Tracer 类的深拷贝 8 test_pretty_print_graph 打印图结构 9 test_graph_fns Graph 辅助函数 10 test_empty_graph_codegen 空图代码生成 11 test_inline_graph 图内联 12 test_custom_traceback_raised_when_exception_source_is_graphmodule 异常追溯 13 test_graph_module_replicate_for_dp 数据并行复制 14 test_tracing_graphmodules_as_leaf_submodules 作为叶子子模块追踪 15 test_graph_module_init_buffer_param_copied_dict_init 缓冲区和参数复制(字典) 16 test_graph_module_init_buffer_param_copied_mod_init 缓冲区和参数复制(模块) 17 test_deepcopy_graphmodule 深拷贝 18 test_graph_module GraphModule 综合测试 上游社区测试已全面覆盖 `torch.fx.Graph` 的核心功能 `__init__` 通过 30 次 `torch.fx.Graph()` 实例创建被隐式覆盖 torch-npu 直接继承 PyTorch 实现,无需额外适配 ![image.png](https://raw.gitcode.com/user-images/assets/7404318/5921d449-7154-4cfb-9f74-ba71c6caf0f8/image.png 'image.png') ### _custom_builtins / _CustomBuiltin / SymbolicContext - **社区测试情况**: _custom_builtins ❌ 无结果 社区无测试 _CustomBuiltin ❌ 无结果 社区无测试 SymbolicContext ⚠️ 只有 StatelessSymbolicContext(变体) 社区无直接测试 - **结论**:需要 torch-npu 自写用例 - **验证**: ![image.png](https://raw.gitcode.com/user-images/assets/7404318/e9e18c87-692e-40dd-9cea-a362588b584f/image.png 'image.png') ## API功能 ### 原有 API(无需适配) - torch.fx.Graph:PyTorch FX图模式的核心数据结构,用于构建和表示计算图 - torch.fx.Graph.__init__:Graph类的构造函数,初始化空的图结构 ### 本次新增测试的 API - **torch.fx.graph._custom_builtins.items()**:返回PyTorch FX图模块中内置函数的字典视图。这些内置函数在图追踪时被识别为特殊操作,不会被递归展开。返回值为 dict_items 类型,包含7个预注册的内置函数,每个键为函数名(str),值为 _CustomBuiltin 实例。 - **torch.fx.graph._CustomBuiltin**:表示FX图中的一个内置函数占位符。它是一个类,实例化后代表一个被注册为内置操作的函数对象,在图追踪过程中作为原子操作处理。模块归属为 torch.fx.graph。 - **torch.fx.experimental.symbolic_shapes.SymbolicContext**:符号形状推导的上下文类。可无参实例化,配合 StatelessSymbolicContext 等变体在导出或追踪过程中传递和管理符号形状信息。模块归属为 torch.fx.experimental.symbolic_shapes。 ## 用例完备性说明 PyTorch社区在test_fx.py、test_export.py等文件中已有丰富的Graph/GraphModule测试用例,覆盖图的创建、节点操作、符号追踪等核心功能。这些用例均为框架层逻辑测试,不涉及TEST_CUDA/TEST_PRIVATEUSE1等硬件强相关分支。 **社区无针对以下3个API的直接测试用例:** - `_custom_builtins` / `_CustomBuiltin`:社区用例未覆盖 - `SymbolicContext`:社区用例使用的是 StatelessSymbolicContext 等变体,未直接测试 SymbolicContext 本身 **本次自写用例(test_fx_custom_builtins.py)覆盖5个测试方法:** | 测试方法 | 验证内容 | |---------|---------| | test_custom_builtins_items | 验证 _custom_builtins.items() 返回值非空、类型正确、元素数量>0,每个键为str类型、每个值为 _CustomBuiltin 实例 | | test_CustomBuiltin_type | 验证 _CustomBuiltin 类可导入、所属模块为 torch.fx.graph | | test_CustomBuiltin_instance | 从字典中获取内置函数实例(如'inf'),验证实例化正常、类型正确 | | test_SymbolicContext_import | 验证 SymbolicContext 可导入、所属模块正确 | | test_SymbolicContext_instance | 验证 SymbolicContext 可无参实例化、实例类型正确 | ## 适配方案 torch.fx为框架层图模式API,与底层设备算子完全解耦。在NPU环境下导入torch_npu后直接可用,无需任何代码修改。新增测试用例同样无需适配,可直接在NPU环境运行。 ## 运行结果 在NPU环境下测试通过: - torch.fx.Graph() 创建成功 - torch.fx.Graph.__init__() 正常工作 - **新增5个测试用例全部通过** ![屏幕截图 2026-05-13 213606.png](https://raw.gitcode.com/user-images/assets/7404318/c8ea9188-bcf9-4220-81f0-387f133a043f/屏幕截图_2026-05-13_213606.png '屏幕截图 2026-05-13 213606.png') ## 文档检查 已检查 docs/api/torch_npu_apis.md: - `torch.fx.Graph` 和 `torch.fx.Graph.__init__` 已在文档中登记 ✅ - `_custom_builtins`、`_CustomBuiltin`、`SymbolicContext` 为 PyTorch FX 内部/实验性 API,不在 torch-npu 公开文档范围内 因此本次 **不需要** 额外提交文档 PR。 ## 多版本兼容性 torch.fx核心架构自PyTorch 1.8引入后保持稳定,推断在2.7.1 ~ 2.12.0及master分支行为一致,均无需适配。
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 olpk 的贡献)
olpkolpk
4月24日 创建了 pull request,commit 35bd99b6
ascend-robotascend-robot成员
4月24日 添加了label:stat/needs-squash
ascend-robot
ascend-robot成员
4月24日 评论:

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

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

likedislike
ascend-robotascend-robot成员
4月24日 添加了label:ascend-cla/no
此处折叠了351条消息 查看更多
ascend-robotascend-robot成员
5月25日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月25日 评论:
流水线 PR-pipeline_pytorch#26606 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 >>>
UT_X86_Part_02 >>>
UT_ARM_A3_Part_01 >>>
UT_ARM_A3_Part_02 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robot
ascend-robot成员
5月25日 评论:

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月25日 合入了pull request
ascend-robot
ascend-robot成员
5月25日 评论:

In response to this:

/sync v2.8.0 v2.9.0 v2.10.0 v2.11.0 master

@jingkai-2026

同步操作执行结果:

Branch Status Pull Request
v2.8.0 创建同步 PR https://gitcode.com/Ascend/pytorch/merge_requests/36616
v2.9.0 创建同步 PR https://gitcode.com/Ascend/pytorch/merge_requests/36617
v2.10.0 同步失败:请手动创建 PR 进行同步,我们会继续完善分支之间同步操作,尽量避免同步失败的情况
v2.11.0 创建同步 PR https://gitcode.com/Ascend/pytorch/merge_requests/36618
master 创建同步 PR https://gitcode.com/Ascend/pytorch/merge_requests/36619
likedislike