已合并
test: 新增torch.fx.experimental.symbolic_shapes NPU 适配验证与统一运行脚本 #35552
yuhongming-2026创建于 5月13日
test: 新增torch.fx.experimental.symbolic_shapes NPU 适配验证与统一运行脚本 #35552
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 yuhongming-2026 的贡献)5月13日 创建了 pull request,commit 205855e8
5月13日 关联了issue:【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(5)
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
yuhongming-2026, thanks for your pull request. All authors of the commits have signed the CLA. 👍


5月13日 添加了label:ascend-cla/yes
此处折叠了167条消息 查看更多
5月27日 添加了label:approvedlgtm
ascend-robot
5月27日 评论:
5月27日 评论:
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月27日 合入了pull request
ascend-robot
5月27日 评论:
5月27日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9454 [ commitID:af48d746 ] 已完成


ascend-robot
5月28日 评论:
5月28日 评论:
The repo or branch is not access to PR-cooperate, please check the current repo https://gitcode.com/Ascend/pytorch.git, branch=v2.12.0


【合入来源】
【背景与目标】
完善 Torch-NPU 对 PyTorch 原生 FX symbolic shapes 相关 API 的兼容性验证,覆盖以下 5 个 API 在 NPU 环境下的可用性,提升昇腾 NPU 上 PyTorch 框架的易用性与模型适配覆盖率。
torch.fx.experimental.symbolic_shapes.compute_unbacked_bindingstorch.fx.experimental.symbolic_shapes.constrain_rangetorch.fx.experimental.symbolic_shapes.constrain_unifytorch.fx.experimental.symbolic_shapes.ConvertIntKeytorch.fx.experimental.symbolic_shapes.ConvertIntKey.get【上游社区支持情况】
torch.fx.experimental.symbolic_shapes,为纯 Python 实现,不依赖 CUDA 专用算子constrain_range/constrain_unify等已列入公开 API allowlist(如test/allowlist_for_publicAPI.json)test/dynamo/test_misc.py等文件中调用constrain_range、constrain_unify;compute_unbacked_bindings、ConvertIntKey、ConvertIntKey.get在基线test/中缺少直接用例torch.npu.device上下文、由 NPU tensor 比较派生 bool 路径结论: 5 个 API 属于上游能力范围,但测试覆盖对 NPU 场景不完整(部分 API 无直接用例、部分 API 缺少 NPU关键路径)。本 PR 新增用例用于补齐 Torch-NPU 侧可验证性。本 MR 不修改文档资料。
【修改方案】
1. API功能
torch.fx.experimental.symbolic_shapes.compute_unbacked_bindings计算 unbacked SymInt 的绑定关系。本 PR 验证该接口在 NPU tensor 输入、NPU 上下文与
.to("npu")迁移路径下可稳定调用。torch.fx.experimental.symbolic_shapes.constrain_range为符号值施加范围约束。本 PR 验证正常分支、异常分支以及 NPU 上下文调用行为。
torch.fx.experimental.symbolic_shapes.constrain_unify对两个符号值进行约束统一。本 PR 验证正常/异常分支及 NPU 上下文下的可用性。
torch.fx.experimental.symbolic_shapes.ConvertIntKey将布尔条件映射为整型 key 的转换器。本 PR 验证实例化与基础映射能力。
torch.fx.experimental.symbolic_shapes.ConvertIntKey.get根据 bool 条件返回对应 int key(True->1,False->0)。本 PR 重点验证字面量 bool 和 NPU tensor 比较派生 bool 两类输入。
2. 新增 NPU 兼容性测试
新增
test/fx/test_fx_experimental_symbolic.py,写法与同目录 FX 测试一致(TestCase+run_tests,默认 NPU 环境可用)。test_compute_unbacked_bindingscompute_unbacked_bindings+ NPU tensor 输入test_compute_unbacked_bindings_npu_contextcompute_unbacked_bindings+torch.npu.device(0)上下文test_compute_unbacked_bindings_to_npucompute_unbacked_bindings+.to("npu")路径test_constrain_rangeconstrain_range基本调用与异常分支test_constrain_range_npu_contextconstrain_range+ NPU device 上下文test_constrain_unifyconstrain_unify基本调用与异常分支test_constrain_unify_npu_contextconstrain_unify+ NPU device 上下文test_convert_int_key_singletonConvertIntKey实例化test_convert_int_key_get_boolConvertIntKey.get(True/False)test_convert_int_key_get_npu_derivedConvertIntKey.get+ 由 NPU tensor 比较得到的 booltest_convert_int_key_npu_contextConvertIntKey.get+ NPU device 上下文场景覆盖说明:
compute_unbacked_bindings:NPU 张量、device 上下文、.to("npu")迁移路径(3 类主路径)constrain_range/constrain_unify:正常调用、异常分支、NPU 上下文(与上游 dynamo 通用语义用例互补)ConvertIntKey/.get:实例化、字面量 bool、NPU 派生 bool、device 上下文(4 类主路径)2.1 新增用例必要性与完整性分析
针对
compute_unbacked_bindings:上游基线
test/缺少面向 NPU tensor 的直接用例;本 PR 覆盖device="npu"、.to("npu")、torch.npu.device(0)三类主路径,补齐验证闭环。针对
constrain_range/constrain_unify:上游
test/dynamo/test_misc.py已有调用,但主要是 Python int + fallback 语义验证;本 PR 增加 NPU 上下文与异常分支验证,属于互补增强,不与上游重复。针对
ConvertIntKey/ConvertIntKey.get:上游基线
test/缺少这组 API 的直接测试,尤其缺失“由 NPU tensor 比较结果派生 bool 后调用get()”路径;本 PR 新增专项用例为必要补齐。结论:
本 PR 的新增测试属于“对上游部分覆盖场景做 NPU 侧补齐 + 对上游缺失场景补空白”,不是重复建设。
3. 多版本分支合入(如适用)
按 Ascend PyTorch 版本线分别提 MR,每个 MR 仅 1 个 squash 提交:
v2.7.1v2.9.0v2.10.0v2.11.0v2.12.0master【资料变更】
不涉及,测试 PR 本身不直接修改资料文件;资料补齐由配套资料 PR 单独提交。
本 PR 仅负责测试补齐与验证,确保测试范围与当前版本实际能力一致。
【接口变更】
不涉及(无新增/修改对外 C++ 或 Python 接口,仅新增测试)
【功能验证】
1. 测试环境
torch.npu.is_available()跳过逻辑2. 测试命令
cd <pytorch-repo-root> python test/fx/test_fx_experimental_symbolic.py3. 测试结果
4. 与上游用例的关系
constrain_range/constrain_unify调用test/dynamo/test_misc.py等(通用语义)compute_unbacked_bindingsConvertIntKey.get(含 NPU 派生 bool)【CheckList】