已合并
test(fx): add Interpreter internal API test cases for NPU [v2.12.0] #43911
test(fx): add Interpreter internal API test cases for NPU [v2.12.0] #43911
已合并
zkx创建于 24 天前
zkx
24 天前

【合入来源】

https://gitcode.com/Ascend/pytorch/issues/3763
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

新增 torch.fx.Interpreter 内部方法(boxed_run/fetch_attr/map_nodes_to_values/fetch_args_kwargs_from_env)的 NPU 一致性验证自写用例。

一、API功能

1. torch.fx.Interpreter.boxed_run:以 "boxed" 调用约定执行模块,接收参数列表(会被清空),与 run() 等价但参数传递方式不同。
2. torch.fx.Interpreter.fetch_attr:按属性路径从模块中获取属性(Parameter/子模块等)。
3. torch.fx.Interpreter.map_nodes_to_values:将 Node 参数映射为具体值(张量等)。
4. torch.fx.Interpreter.fetch_args_kwargs_from_env:从执行环境(env)中取出节点的位置参数和关键字参数。

二、测试用例完备性说明

PyTorch 官方社区未对这 4 个内部方法提供专门直接测试用例(仅在 test_interpreter 族中通过 run() 间接调用),
社区对内部方法的直接验证不充分,故新增 test/test_fx_interpreter_npu.py 直接验证:

1. boxed_run:test_boxed_run_basic(执行结果正确)、test_boxed_run_clears_args(参数列表被清空)
2. fetch_attr:test_fetch_attr_parameter(获取 Parameter)、test_fetch_attr_submodule(获取子模块)
3. map_nodes_to_values:test_map_nodes_to_values_args(位置参数映射)、test_map_nodes_to_values_kwargs(关键字参数映射)
4. fetch_args_kwargs_from_env:test_fetch_args_kwargs_from_env(位置参数)、test_fetch_args_kwargs_from_env_non_empty_kwargs(非空关键字参数,验证 kwargs 正确映射)

覆盖正常场景 + 参数清空边界场景,共 8 个用例。Interpreter 主体及其余方法(run/run_node/placeholder/get_attr/call_function/call_method/call_module/output)
已由官方 test_fx.py.patch 完整覆盖,本文件仅补充官方缺失的直接用例。

三、NPU适配
3.1 API适配:无需修改。4 个方法均为纯框架层图解释逻辑,与底层硬件解耦。
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 下各版本目录 torch-fx.md 中,
torch.fx.Interpreter 及其方法已列出(boxed_run 条目已通过 PR #43908 补充),不需要本 PR 修改资料。

【接口变更】

不涉及

【功能验证】

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

$ python3 test/test_fx_interpreter_npu.py
.......
----------------------------------------------------------------------
Ran 8 tests in 1.716s
OK

验证日志说明:本机 NPU 环境实测 8 tests OK。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zkx 的贡献)
Zzkx
24 天前 创建了 pull request,commit e52d518e
Zzkx
24 天前 关联了issue:[Usage]: API一致性说明:torch.fx.Interpreter API NPU 对齐验证结论
atomgit-bot
atomgit-bot
24 天前 评论:

变更摘要

本 PR 新增测试文件 test/test_fx_interpreter_npu.py,为 torch.fx.Interpreter 的四个内部方法(boxed_runfetch_attrmap_nodes_to_valuesfetch_args_kwargs_from_env)补充 NPU 一致性验证用例。PyTorch 官方社区未对这些内部方法提供直接测试,因此新增 7 个自写用例,覆盖正常场景与边界场景(如 boxed_run 参数列表清空)。测试通过 torch.accelerator.current_accelerator() 自动检测 NPU 设备,不具备 NPU 环境时回落至 CPU。

主要改动

  • 新增 test/test_fx_interpreter_npu.py 测试文件:包含 4 个测试类、7 个测试用例,分别验证 boxed_run 执行正确性与参数清空行为、fetch_attr 获取 Parameter 和子模块、map_nodes_to_values 对位置参数和关键字参数的映射、以及 fetch_args_kwargs_from_env 从执行环境取参的正确性。
  • NPU 设备自适应:通过 device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" 在文件顶层获取设备类型,所有测试张量均通过 device=device_type 参数直接在目标设备上创建,确保在 NPU 和 CPU 环境下均可运行。
  • 测试用例依赖的图模块构建方式:利用 symbolic_trace 将简单 torch.nn.Module 子类(如 AddModuleM)转换为 GraphModule,再传入 Interpreter 以构造可测试的执行上下文。
likedislike
atomgit-bot
atomgit-bot
24 天前 评论:

代码审查

✅ 未发现问题

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

/approve

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