Files
TorchNPU/test/fx
wangshiqi-2026 97e283151e test(fx): add NPU tests for symbolic_shapes.lru_cache and PropagateUnbackedSymInts
Co-authored-by: wangshiqi-2026<wang-shiqi@sjtu.edu.cn>



# message auto-generated for no-merge-commit merge:
!38673 merge test-fx-symbolic-shapes-unbacked-symints into master

test(fx): add NPU tests for symbolic_shapes.lru_cache and PropagateUnbackedSymInts

Created-by: wangshiqi-2026
Commit-by: wangshiqi-2026
Merged-by: ascend-robot
Description: 【合入来源】
如有社区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_cache**:`torch/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. 多版本分支合入

目标分支:`master`

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

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

【功能验证】

### 1. 测试环境

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

### 2. 测试命令

```bash
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. 测试结果(运行日志)

```text
# 环境:Ascend NPU(torch-npu 2.13.0 / torch 2.13.0)

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

# PropagateUnbackedSymInts(含 boxed_run / call_function / call_method)
.......
----------------------------------------------------------------------
Ran 7 tests in 0.747s
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]

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [ ] PR持续集成流水线(CI)执行通过,代码检查无异常



See merge request: Ascend/pytorch!38673
2026-08-06 15:26:29 +08:00
..