已合并
test(fx): add NPU tests for torch.fx graph APIs #35514
zhouzirui1234创建于 5月13日
test(fx): add NPU tests for torch.fx graph APIs #35514
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zhouzirui1234 的贡献)5月13日 创建了 pull request,commit cbd7144d
ascend-robot
5月13日 评论:
5月13日 评论:
ascend-robot
5月13日 评论:
5月13日 评论:
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
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
zhouzirui-2026, thanks for your pull request. All authors of the commits have signed the CLA. 👍


5月13日 添加了label:ascend-cla/yes
ascend-robot
5月13日 评论:
5月13日 评论:
当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.7.1 | ||
| v2.9.0 | ||
| v2.10.0 | ||
| v2.11.0 | ||
| v2.12.0 |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


此处折叠了111条消息 查看更多
sunyu-xuan
5月21日 评论:
5月21日 评论:
/lgtm


5月21日 添加了label:approvedlgtm
ascend-robot
5月21日 评论:
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.


5月21日 合入了pull request
【合入来源】
关联 issue:
【修改方案】
本 PR 新增
test/fx/test_fx_graph_api.py,补充torch.fxGraph 相关 API / 内部 codegen 链路在 NPU 环境下可直接运行的轻量验证用例。测试中涉及 Tensor 的输入均通过.to(device_type)放到当前 accelerator 上执行。任务 API 功能及上游社区用例情况如下:
torch.fx.Graph.inserting_aftertest/quantization/fx/test_model_report_fx.py::TestFxModelReportDetectDynamicStatic.test_nested_detection_case中有使用,但该用例在当前 Kunpeng/ARM 环境会因 FBGEMM 条件被 skip,无法直接进入目标主体。torch.fx.Graph.inserting_beforetest/test_fx.py::TestFX.test_insertion_point已有直接用例,但当前环境运行test/test_fx.py会受libtorchbind_test.so缺失影响。torch.fx.Graph.linttest/test_fx.py已有直接专项测试,例如TestFX.test_wrong_topo、TestFX.test_copy_no_remap。graph.lint()校验所构造图的合法性。torch.fx.graph.magic_methods.formattorch.fx.graph内部 codegen 模板链路,用于生成 magic method 对应的 Python 表达式。test/test_fx.py::TestFX.test_fx_shifts通过 shift 表达式覆盖相关 codegen 链路。symbolic_trace验证 Proxy 节点生成、节点参数、codegen 文本和 NPU 执行结果。torch.fx.graph.inplace_methods.formattorch.fx.graph内部 inplace codegen 模板链路,用于生成 inplace 操作对应的 Python 代码。test/test_fx.py::TestFX.test_imul_code_print通过operator.imul覆盖 inplace codegen。operator.imul节点触发a *= bcodegen,并验证节点参数、输出结果和输入 Tensor 原地更新。主要开发思路:
torch.fx.Graph.lint,上游已有直接专项测试,因此本 PR 不重复新增 lint 测试。torch.fx.Graph.inserting_after,上游 quantization 用例会因当前 Kunpeng/ARM 环境不支持 FBGEMM 被 skip,因此参考其插入点使用方式新增轻量测试。torch.fx.Graph.inserting_before、torch.fx.graph.magic_methods.format、torch.fx.graph.inplace_methods.format,上游相关用例集中在test/test_fx.py,该文件在当前环境存在libtorchbind_test.so依赖问题,因此新增独立小文件验证核心行为。本 PR 覆盖以下 4 个新增测试目标项:
torch.fx.Graph.inserting_aftertorch.fx.Graph.inserting_beforetorch.fx.graph.magic_methods.formatsymbolic_trace触发 Proxy 生成链路,验证operator.lshift/operator.rshift节点生成、参数传递、生成代码格式以及 NPU 执行结果。torch.fx.graph.inplace_methods.formatoperator.imulFX 节点,验证 inplace codegen 生成a *= b、节点参数传递、NPU 执行结果以及输入 Tensor 被原地更新。本 PR 不新增
torch.fx.Graph.lint专项测试,原因如下:test/test_fx.py,torch.fx.Graph.lint已有直接专项测试覆盖,例如TestFX.test_wrong_topo、TestFX.test_copy_no_remap等。graph.lint(),用于校验测试图结构合法性,但不把它作为本 PR 的新增目标 API。本 PR 不修改 API 实现,不修改
test/test_fx.py。新增独立测试文件test/fx/test_fx_graph_api.py,避免受test/test_fx.py中历史环境依赖libtorchbind_test.so缺失问题影响。新增测试文件:
新增测试方法:
各测试核心验证点如下:
test_graph_inserting_afterplaceholder -> neg -> relu -> output;使用graph.inserting_after(neg);断言relu紧跟neg;断言neg.args == (x,)、relu.args == (neg,)、relu.target == torch.relu;在 NPU Tensor 上执行GraphModule并校验结果。test_graph_inserting_beforeplaceholder -> relu -> output;使用graph.inserting_before(relu)插入neg;断言neg位于relu前且相邻;断言neg.args == (x,)、relu.args == (neg,)、neg.target == torch.neg;在 NPU Tensor 上执行GraphModule并校验结果。test_magic_methods_format_codegensymbolic_tracetracex << 3, x >> 3;断言 FX Graph 中生成operator.lshift/operator.rshift节点;断言节点参数为(x_node, 3);断言 codegen 包含x << 3、x >> 3;在 NPU LongTensor 上执行并校验结果。test_inplace_methods_format_codegenoperator.imul(a, b)节点;断言节点参数和 target;断言 codegen 包含a *= b;在 NPU Tensor 上执行并校验输出结果,同时校验输入 Tensor 被原地更新。【资料变更】
当前不涉及资料补齐 PR。三个公开
torch.fx.Graph.*API 已在docs/zh/native_apis中记录;两个torch.fx.graph.*.format项为内部 codegen 字典模板链路,不宜按普通公开 API 强行补入 native API 表。【接口变更】
不涉及。
【功能验证】
执行命令:
cd /root python /workspace/pytorch/test/fx/test_fx_graph_api.py执行结果:
最终验证结论:
inserting_after、inserting_before、magic_methods.format、inplace_methods.format。torch.fx.Graph.lint已有 PyTorch 官方直接专项测试覆盖,因此本 PR 不重复新增 lint 专项测试。【CheckList】