已合并
test(fx): add tests for proxy tensor api #34871
nwww创建于 5月2日
test(fx): add tests for proxy tensor api #34871
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 nwww 的贡献)ascend-robot
5月2日 评论:
5月2日 评论:
ascend-robot
5月2日 评论:
5月2日 评论:
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
nannan-2026, thanks for your pull request. All authors of the commits have signed the CLA. 👍


5月2日 添加了label:ascend-cla/yes
此处折叠了51条消息 查看更多
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:https://gitcode.com/Ascend/pytorch/issues/1609
【修改方案】
本 PR 针对
torch.fx.experimental.proxy_tensor相关 API 新增专项验证用例,覆盖如下 API:torch.fx.experimental.proxy_tensor.get_proxy_modetorch.fx.experimental.proxy_tensor.handle_sym_dispatchtorch.fx.experimental.proxy_tensor.make_fxtorch.fx.experimental.proxy_tensor.maybe_disable_thunkifytorch.fx.experimental.proxy_tensor.maybe_enable_thunkify一、API 功能说明
torch.fx.experimental.proxy_tensor.get_proxy_mode该 API 用于获取当前活跃的
ProxyTorchDispatchMode。在普通执行环境下返回None,在make_fxtracing 过程中可获取到当前 proxy mode。torch.fx.experimental.proxy_tensor.handle_sym_dispatch该 API 用于处理 proxy tensor tracing 过程中的 symbolic dispatch 逻辑,依赖当前 proxy mode 环境。该接口属于
proxy_tensortracing 内部调度相关 helper。torch.fx.experimental.proxy_tensor.make_fx该 API 用于对 Python 函数进行 tracing,并生成对应的
torch.fx.GraphModule。生成后的GraphModule可用于表示被 trace 函数的 ATen 图结构。torch.fx.experimental.proxy_tensor.maybe_enable_thunkify该 API 用于在特定上下文中启用 thunkify 相关逻辑,可作为上下文管理器使用。
torch.fx.experimental.proxy_tensor.maybe_disable_thunkify该 API 用于在特定上下文中关闭 thunkify 相关逻辑,可作为上下文管理器使用。
二、上游社区用例分析与用例补齐说明
经检索,PyTorch 上游社区中已存在部分
make_fx相关测试用例。例如在test/export/test_export.py中存在test_stack_trace_make_fx等用例,会调用torch.fx.experimental.proxy_tensor.make_fx。但经分析,现有
make_fx用例主要验证的是 export / stack trace 等上层功能路径中make_fx的使用效果,测试重点并不是make_fxAPI 本身的基础行为,也未集中覆盖本次 issue 要求的torch.fx.experimental.proxy_tensor相关 API 组合。具体来说,现有用例未充分覆盖以下内容:make_fx返回对象是否为torch.fx.GraphModule;make_fx生成的GraphModule执行结果是否与原函数一致;get_proxy_mode在 tracing 外返回None、在make_fxtracing 中可获取到 proxy mode 的行为;handle_sym_dispatch作为 proxy tensor tracing 内部 helper 对 proxy mode 的依赖关系;maybe_enable_thunkify/maybe_disable_thunkify作为上下文管理器在make_fxtracing 中可稳定执行;同时,经检索,本次任务中的以下 API 未发现独立、直接的专项验证用例:
torch.fx.experimental.proxy_tensor.get_proxy_modetorch.fx.experimental.proxy_tensor.handle_sym_dispatchtorch.fx.experimental.proxy_tensor.maybe_disable_thunkifytorch.fx.experimental.proxy_tensor.maybe_enable_thunkify因此,现有上游社区用例对
make_fx有调用覆盖,但不足以完整覆盖本次 issue 中 5 个proxy_tensorAPI 的专项兼容性验证需求。基于此,本 PR 新增轻量级专项测试文件:该文件集中验证
torch.fx.experimental.proxy_tensor相关 API 的基础行为,且测试张量通过.to(device_type)迁移到当前 accelerator / NPU 侧运行,满足 Torch-NPU API 一致性测试要求。三、新增测试文件说明
本 PR 新增测试文件:
该测试文件用于集中验证
torch.fx.experimental.proxy_tensor相关 API 的基础兼容性。为便于后续维护和扩展,文件开头已补充英文说明,说明该文件用于补充proxy_tensorAPI 的 NPU 侧验证用例,并可继续扩展同类 API 测试。文件开头说明如下:
""" Add validation cases for torch.fx.experimental.proxy_tensor APIs on NPU: 1. PyTorch community lacks sufficient and direct API validations for some proxy_tensor APIs, so this file is added. 2. This file validates get_proxy_mode, handle_sym_dispatch, make_fx, maybe_enable_thunkify, and maybe_disable_thunkify (extendable). """新增测试覆盖以下场景:
make_fxtracing 基础场景通过
make_fx(fn)(x, y)对函数进行 tracing,验证返回结果为GraphModule,并验证生成的GraphModule执行结果与原函数一致。get_proxy_modetracing 场景验证
get_proxy_mode在 tracing 外返回None,在make_fxtracing 过程中可以获取到非空 proxy mode。handle_sym_dispatchproxy mode 依赖场景验证
handle_sym_dispatch为可调用对象,并验证其依赖当前 proxy mode 的行为。在非 proxy mode 环境下直接调用会触发断言,符合该 helper 的使用约束。maybe_enable_thunkify/maybe_disable_thunkify上下文管理器场景验证
maybe_enable_thunkify和maybe_disable_thunkify可作为上下文管理器正常使用,并可在make_fxtracing 过程中稳定执行。四、NPU 适配说明
上述 API 均属于
torch.fx.experimental.proxy_tensorPython 层 tracing / proxy tensor 相关接口,不涉及新增 NPU 算子实现,也不涉及 C++、Kernel 或底层算子逻辑修改。但测试用例中涉及张量输入,因此测试张量需要运行在 NPU / 当前 accelerator 侧。本 PR 使用如下方式获取当前 accelerator:
device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu"并通过如下方式将测试张量迁移到当前设备:
x = torch.randn(2, 3).to(device_type) y = torch.randn(2, 3).to(device_type)在 Torch-NPU 测试环境中,当前 accelerator 为 NPU,因此测试张量会通过
.to(device_type)迁移到 NPU 侧运行,满足新增用例涉及张量需在 NPU 侧执行的要求。五、涉及版本说明
本次用例补齐分别提交到以下目标分支:
v2.7.1v2.9.0v2.10.0v2.11.0v2.12.0master各版本均单独提交 PR,目标分支与源码分支一一对应。
本次根据当前任务要求提交上述分支,不涉及
v2.8.0分支。【资料变更】
不涉及。
本 PR 仅新增测试用例,不修改资料文档。
相关 API 的
docs/zh/native_apis支持状态已通过单独文档 PR 补充。本 PR 仅新增
test/fx/test_proxy_tensor_api.py测试用例,不直接修改docs/zh/native_apis资料文档。但已结合配套资料补齐任务,对本次涉及的
torch.fx.experimental.proxy_tensor相关 API 在docs/zh/native_apis中的资料支持状态进行了同步核查。资料支持情况核查结论如下:
torch.fx.experimental.proxy_tensor下的 Python 层 tracing / proxy tensor 相关接口;docs/zh/native_apis/pytorch_*/torch-fx.md中进行声明;test目录下的专项用例与行为验证,不涉及torch_npu/目录下的功能代码修改,也不涉及资料文档修改;docs/zh/native_apis支持状态已通过单独资料 PR 补充;【接口变更】
不涉及。
本 PR 不修改客户可见接口,不修改 API 行为,仅新增测试用例验证现有 API 兼容性。
【功能验证】
新增测试用例:
测试命令:
python -u test/fx/test_proxy_tensor_api.py -v测试场景:
make_fx可正常生成GraphModule;make_fx生成的GraphModule执行结果与原函数一致;get_proxy_mode在 tracing 外返回None;get_proxy_mode在make_fxtracing 中可获取到 proxy mode;handle_sym_dispatch的可调用性及其依赖 proxy mode 的行为;maybe_enable_thunkify/maybe_disable_thunkify上下文管理器可正常使用;.to(device_type)迁移到当前 accelerator / NPU 侧运行。本地验证结果示例:
已在以下版本执行该用例:
v2.7.1:通过v2.9.0:通过v2.10.0:通过v2.11.0:通过v2.12.0:通过master:通过说明:
该组 API 为
torch.fx.experimental.proxy_tensorPython 层 tracing 相关接口,不涉及 NPU 算子计算。本次测试主要验证 API 功能行为与 PyTorch 原生保持一致,同时确保测试中涉及的张量运行在当前 accelerator / NPU 侧。【CheckList】