已关闭
test(fx): add ShapeEnv API tests (self-written + upstream patch) for v2.7.1 #36524
小辉懂编程创建于 5月22日关闭于 7月4日
test(fx): add ShapeEnv API tests (self-written + upstream patch) for v2.7.1 #36524
已关闭
小辉懂编程创建于 5月22日关闭于 7月4日
小辉懂编程
5月22日

【关联任务】

【合入来源】

请勿携带内部流程信息。

【背景与目标】

完善 Torch-NPU 对 torch.fx.experimental.symbolic_shapes.ShapeEnv 下 5 个 API 在 NPU 环境下的兼容性验证。

API 全名 功能 覆盖方式
torch.fx.experimental.symbolic_shapes.ShapeEnv.produce_guards 根据符号约束生成 guard 表达式列表 patch 适配上游 test_mega_guard / test_guards_equal
torch.fx.experimental.symbolic_shapes.ShapeEnv.get_nontrivial_guards 过滤 trivial guards,返回有语义的 guard patch 适配上游 test_guards_equal
torch.fx.experimental.symbolic_shapes.ShapeEnv.get_pruned_guards 按给定 symbol 集合裁剪 guard 子集 自写 test_shape_env_get_pruned_guards
torch.fx.experimental.symbolic_shapes.ShapeEnv.is_unbacked_symint 判断符号是否为 unbacked SymInt 自写 test_shape_env_is_unbacked_symint
torch.fx.experimental.symbolic_shapes.ShapeEnv.ignore_fresh_unbacked_symbols 上下文管理器:块内 fresh unbacked symbol 不注册 自写 test_shape_env_ignore_fresh_unbacked_symbols

【上游社区用例分析】

cd /workspace/pytorch_v2.7.1
grep -rn "produce_guards\|get_nontrivial_guards\|get_pruned_guards\|is_unbacked_symint\|ignore_fresh_unbacked_symbols" test/ --include="*.py"
检查项 结论
produce_guards / get_nontrivial_guards 上游 test/test_proxy_tensor.py 的 test_mega_guard(行 1821)、test_guards_equal(行 1889)有覆盖,但运行在 CPU 路径
get_pruned_guards / is_unbacked_symint 上游 test/ 无直接用例
ignore_fresh_unbacked_symbols v2.7.1 无直接用例(v2.9+ 才在 test_control_flow.py 中间接使用)

结论:produce_guards / get_nontrivial_guards 通过 patch 做 NPU 路径适配;另外 3 个 API 上游缺失或验证不充分,需新增自写用例。

【修改方案】

1. 自写测试:test/fx/test_symbolic_shapes.py

与上游 test/fx/ 路径一致, 8 个测试方法(TestCase + run_tests):

用例 覆盖 API 验证要点
test_shape_env_get_pruned_guards ShapeEnv.get_pruned_guards 返回 list 类型
test_shape_env_is_unbacked_symint ShapeEnv.is_unbacked_symint backed/unbacked 符号区分
test_shape_env_ignore_fresh_unbacked_symbols ShapeEnv.ignore_fresh_unbacked_symbols 上下文管理行为
test_is_accessor_node_with_call_method is_accessor_node call_method + NPU tensor
test_is_accessor_node_with_call_function is_accessor_node 正负分支
test_is_concrete_int_with_literal_and_device_shape is_concrete_int / is_symbolic NPU tensor .size()
test_is_concrete_float_with_literal_and_symbolic_value is_concrete_float / is_symbolic 字面量 vs unbacked
test_is_concrete_bool_with_literal_and_symbolic_value is_concrete_bool / is_symbolic 字面量 vs unbacked

代码规范(六.8):

  • 文件头英文 docstring;导入顺序 torch → torch.fx → torch.testing → sympy
  • device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu",张量 .to(device_type) 切 NPU
  • 全部 self.assert*,无 print / try / except

2. 上游 patch:test_upstream/test/test_proxy_tensor.py.patch

覆盖 test_mega_guard(行 1821)和 test_guards_equal(行 1889),最小化修改:

Hunk 位置 改动
#1 test_mega_guard torch.randn(16), torch.randn(8) → 各加 .npu()
#2 test_guards_equal 测试方法内新增 4 行同名局部 _trace shadow(inps 用 torch.randn(arg).npu()),调用点 0 改动
  • 严格用 git diff 在 PyTorch v2.7.1 官方源码上生成(六.5)
  • 无 trace_on_device、不引入新函数名

3. 多版本合入

每个分支 1 个 squash 提交:

目标分支 MR 含 patch
v2.7.1 本 MR:#36524 ✅
v2.9.0 #36519 ✅
v2.10.0 #36521 ✅
v2.11.0 #36522 ✅
v2.12.0 #36523 ✅
master #36446 ✅

【资料变更】

不涉及本 MR。文档由独立资料 PR #37392 单独合入 v2.7.1,一次性刷新 5 个版本目录的 torch-fx.md。

【接口变更】

不涉及。仅新增测试代码,不修改 torch_npu 或 PyTorch 任何接口。

【功能验证】

测试环境:Ascend 910B4 / PyTorch 2.7.1 / torch_npu / CANN 8.5.0 / Python 3.11.13

cd /tmp && python3.11 /workspace/pytorch/test/fx/test_symbolic_shapes.py

测试结果:8 tests OK

测试结果

Patch 验证:

cd /workspace/pytorch_v2.7.1
patch -p1 --dry-run < /workspace/pytorch/test_upstream/test/test_proxy_tensor.py.patch

所有 hunk 应用成功,无冲突。

Patch Apply

【CheckList】

likedislike
当前Pull Request已关闭, 关闭人@小辉懂编程
小辉懂编程小辉懂编程
5月22日 创建了 pull request,commit ac9c48d9
小辉懂编程小辉懂编程
5月22日 关联了issue:[API补齐] ShapeEnv.ignore_fresh_unbacked_symbols NPU适配无需修改说明
ascend-robot
ascend-robot成员
5月22日 评论:

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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
test ❌ (0/2)(You can also ask: ffmh, 陈豪, zichun_ye, wasd1111222, TonyYA) ❌ (0/1)(You can also ask: guoqi1024, medivh-x, adrian07110, chujinjin, zichun_ye)
test_upstream ❌ (0/2)(You can also ask: wangqiang160, wjlflyer, culechan, chengpeng25, huangjingwei) ❌ (0/1)(You can also ask: wangmin0104, wasd1111222, chenrayray, zichun_ye, sunyu-xuan)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
5月22日 添加了label:ascend-cla/yes
此处折叠了700条消息 查看更多
ascend-robotascend-robot成员
6月30日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
6月30日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月30日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
6月30日 评论:
流水线 PR-pipeline_pytorch#39991 [ commitID:47797a5a ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 ✅ >>>
Build_ARM ✅ >>>
Build_LibTorch_x86 ✅ >>>
Build_LibTorch_ARM ✅ >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test ✅ >>>
恶意代码检查 Antipoison ✅ >>>
编码安全与规范检查 CodeCheck ✅ >>>
check_error ✅ >>>
CodeCheck_lintrunner ✅ >>>
开源片段检查 SCA ✅ >>>
开发者测试 UT_X86_Part_01 ✅ >>>
UT_X86_Part_02 ✅ >>>
UT_ARM_A3_Part_01 ✅ >>>
UT_ARM_A3_Part_02 ✅ >>>
UT_ARM_A2_Part_01 ✅ >>>
UT_ARM_A2_Part_02 ✅ >>>
UT_ARM_A2_Part_03 ✅ >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 ✅ >>>
UT_ARM_A2_Select_Part_02 ✅ >>>
流水线 PR-pipeline_pytorch ✅ >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
小辉懂编程小辉懂编程
7月4日 关闭了 pull request