已合并
test(fx): add NPU tests for symbolic_shapes.lru_cache and PropagateUnbackedSymInts #38674
test(fx): add NPU tests for symbolic_shapes.lru_cache and PropagateUnbackedSymInts #38674
已合并
Nokstella创建于 6月16日
Nokstella
Nokstella
6月16日

【合入来源】
如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

问题单
issue/工单
关联issue:https://gitcode.com/Ascend/pytorch/issues/1617

【修改方案】
完善 Torch-NPU 对 torch.fx.experimental.symbolic_shapes 下 5 个 API 在 NPU 环境下的兼容性验证,覆盖以下 API:

API 说明
torch.fx.experimental.symbolic_shapes.lru_cache symbolic_shapes 模块提供的 lru_cache 封装,支持 hits/misses、cache_clear、cumulative_cache_info
torch.fx.experimental.symbolic_shapes.PropagateUnbackedSymInts FX Interpreter 子类,用于传播 unbacked SymInt 绑定
torch.fx.experimental.symbolic_shapes.PropagateUnbackedSymInts.boxed_run 以 boxed 调用约定执行 FX 图
torch.fx.experimental.symbolic_shapes.PropagateUnbackedSymInts.call_function 执行 call_function 节点并返回结果
torch.fx.experimental.symbolic_shapes.PropagateUnbackedSymInts.call_method 执行 call_method 节点并返回结果

1. 上游社区用例情况分析

在 PyTorch 上游仓库(github.com/pytorch/pytorch)中搜索这 5 个 API:

  • lru_cachetorch/fx/experimental/symbolic_shapes.py 中有公开实现,但上游 test/ 目录下无针对 symbolic_shapes.lru_cache 的直接单测覆盖
  • PropagateUnbackedSymInts:类定义于 symbolic_shapes.py,上游仅在 test/fx/test_fx_traceback.py 中间接涉及,无独立、直接的 API 验证用例
  • PropagateUnbackedSymInts.boxed_run / call_function / call_method:继承自 torch.fx.Interpreter,上游无针对 PropagateUnbackedSymInts 这三个方法的直接单测

结论:5 个 API 均属于上游"无直接用例"场景,按 1.3 规范自写用例,提交到 torch-npu 的 test/ 目录。

2. NPU 适配方案

5 个 API 均为 FX / symbolic_shapes 框架层纯 Python 接口,不涉及 NPU 算子调用,在 NPU 上行为与 CPU 一致,无需做任何 API 代码适配修改

3. 新增测试用例

新增/扩展以下测试文件进行覆盖:

测试文件 覆盖 API
test/fx/test_symbolic_shapes_api.py test_public_lru_cache → lru_cache
test/fx/test_fx_propagate_unbacked_symints.py test_propagate_unbacked_symints_run 等 → PropagateUnbackedSymInts
test/fx/test_fx_propagate_unbacked_symints.py test_propagate_unbacked_symints_boxed_run → boxed_run
test/fx/test_fx_propagate_unbacked_symints.py test_propagate_unbacked_symints_call_function → call_function
test/fx/test_fx_propagate_unbacked_symints.py test_propagate_unbacked_symints_call_method → call_method

4. API 补齐

不需要。5 个 API 在 NPU 上运行结果与 PyTorch 社区一致。

5. 多版本分支合入

目标分支:v2.12.0

【资料变更】
不涉及(文档补齐在独立 docs PR 中完成)。

【接口变更】
不涉及(无新增/修改对外 C++ 或 Python 接口,仅新增测试用例)

【功能验证】

1. 测试环境

设备:Ascend NPU
框架:torch-npu + PyTorch
说明:5 个 API 为 FX/symbolic_shapes 的框架层纯 Python 接口,不涉及张量算子差异;用例中涉及张量时使用 .to(device_type) 运行在当前 accelerator。

2. 测试命令

cd /tmp
source /usr/local/Ascend/ascend-toolkit/set_env.sh
python /workspace/pytorch/test/fx/test_symbolic_shapes_api.py -k test_public_lru_cache
python /workspace/pytorch/test/fx/test_fx_propagate_unbacked_symints.py -k propagate_unbacked

3. 测试结果(运行日志)

# 环境:Ascend NPU(torch-npu 2.12.0rc1 / torch 2.12.0)

# lru_cache
.
----------------------------------------------------------------------
Ran 1 test in 0.021s
OK

# PropagateUnbackedSymInts(含 boxed_run / call_function / call_method)
.......
----------------------------------------------------------------------
Ran 7 tests in 0.618s
OK

4. 与上游用例的关系

验证维度 上游现有用例 本 PR 新增用例
symbolic_shapes.lru_cache 无直接用例 已覆盖(hit/miss、cumulative_cache_info、cache_clear)
PropagateUnbackedSymInts 仅 test_fx_traceback.py 间接涉及 已覆盖(run / run_node / placeholder / output)
PropagateUnbackedSymInts.boxed_run 无直接用例 已覆盖
PropagateUnbackedSymInts.call_function 无直接用例 已覆盖(NPU 张量 + shape 校验)
PropagateUnbackedSymInts.call_method 无直接用例 已覆盖(NPU 张量 + shape 校验)
NPU 侧验证闭环 不完整 本 PR 补齐

【CheckList】
PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Nokstella 的贡献)
NokstellaNokstella
6月16日 创建了 pull request,commit a4387ca6
NokstellaNokstella
6月16日 关联了issue:【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(11)
ascend-robotascend-robot成员
6月16日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月16日 评论:

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 li_jing_hw, huangjingwei (2/2) li_jing_hw (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

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

likedislike
此处折叠了215条消息 查看更多
li_jing_hw成员
20 天前 评论:

/approve

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