已关闭
test(fx): add ShapeEnv API tests (self-written + upstream patch) for v2.9.0 #36519
小辉懂编程创建于 5月22日关闭于 7月4日
test(fx): add ShapeEnv API tests (self-written + upstream patch) for v2.9.0 #36519
已关闭
当前Pull Request已关闭, 关闭人@小辉懂编程
5月22日 关联了issue:[API补齐] ShapeEnv.ignore_fresh_unbacked_symbols NPU适配无需修改说明
ascend-robot
5月22日 评论:
5月22日 评论:
test_upstream/test/test_proxy_tensor.py.patch
已过期
@@ -0,0 +14,4 @@
14+ from torch._dynamo.source import LocalSource
15+ self.assertExpectedInline(
16+- str(fx_g.shape_env.produce_guards(fx_placeholder_vals(fx_g), [LocalSource("a"), LocalSource("b")], ignore_static=False)), # noqa: B950
17++ str(fx_g.shape_env.produce_guards(
18++ fx_placeholder_vals(fx_g),
【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。


ascend-robot
5月22日 评论:
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: liujunzhu, probiotics_53, 杨开昕, guoqi1024, suhaibo) | ❌ (0/1)(You can also ask: chenrayray, suhaibo, zqwenn, htchu, liangsongwei) |
| test_upstream | ❌ (0/2)(You can also ask: zhenyu10, htchu, 褚博宁, chengpeng25, wangmin0104) | ❌ (0/1)(You can also ask: li_jing_hw, wasd1111222, 褚博宁, liangsongwei, zqwenn) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies 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. 👍


此处折叠了575条消息 查看更多
6月30日 添加了label:docs-ci-pipeline-success
6月30日 删除了label:ci-pipeline-running
6月30日 添加了label:ci-pipeline-passed
ascend-robot
6月30日 评论:
6月30日 评论:
流水线 PR-pipeline_pytorch#39992 [ commitID:042178c9 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


7月4日 关闭了 pull request
【关联任务】
【合入来源】
请勿携带内部流程信息(需求链接、问题单、内部 issue 等)。
【背景与目标】
完善 Torch-NPU 对 PyTorch 原生 FX
ShapeEnv相关 API 的兼容性验证,覆盖以下 5 个 API 在 NPU 环境下的可用性,提升昇腾 NPU 上 PyTorch 框架的易用性与模型适配覆盖率。torch.fx.experimental.symbolic_shapes.ShapeEnv.produce_guardstorch.fx.experimental.symbolic_shapes.ShapeEnv.get_nontrivial_guardstorch.fx.experimental.symbolic_shapes.ShapeEnv.get_pruned_guardstorch.fx.experimental.symbolic_shapes.ShapeEnv.is_unbacked_syminttorch.fx.experimental.symbolic_shapes.ShapeEnv.ignore_fresh_unbacked_symbols【API 详细功能说明】
1.
ShapeEnv.produce_guardsproduce_guards(placeholders, sources, *, ignore_static=True, ...) -> list[str],返回 sympy 形式 guard 字符串(如"L['a'].size()[0] >= 2")。torch._dynamo编译落盘 guard、torch.export导出 ExportedProgram。2.
ShapeEnv.get_nontrivial_guardsget_nontrivial_guards() -> list[ShapeGuard]。_assert_no_guards内部调用,断言 fx tracing 后无新约束。3.
ShapeEnv.get_pruned_guardsget_pruned_guards(symints: list[sympy.Symbol]) -> list[ShapeGuard]。4.
ShapeEnv.is_unbacked_symintnonzero/unique输出尺寸)。is_unbacked_symint(symbol: sympy.Symbol) -> bool。5.
ShapeEnv.ignore_fresh_unbacked_symbolswith块内产生的 fresh unbacked symbol 不注册到全局 unbacked 列表,块结束后恢复。ignore_fresh_unbacked_symbols() -> ContextManager。cond/while等高阶算子内部,避免分支符号污染主图。【上游社区支持情况】
torch.fx.experimental.symbolic_shapes,为纯 Python 实现,不依赖 CUDA 专用算子ShapeEnv类上公开produce_guards/get_nontrivial_guards在test/test_proxy_tensor.py::TestSymbolicTracing::test_mega_guard、test_guards_equal中被覆盖;ignore_fresh_unbacked_symbols自 v2.9.0 起在test/functorch/test_control_flow.py::test_cond_gen_schema_symbool_inputs中被使用;get_pruned_guards/is_unbacked_symint在基线test/中缺少直接用例produce_guards/get_nontrivial_guards通过最小化 patch 引入 NPU 设备路径;2) 对上游未覆盖的get_pruned_guards/is_unbacked_symint新增专项自写用例;3) 对ignore_fresh_unbacked_symbols补齐独立行为验证上游用例审计命令(可复现):
cd /workspace/pytorch_v2.9.0 grep -rn "produce_guards\|get_nontrivial_guards\|get_pruned_guards\|is_unbacked_symint\|ignore_fresh_unbacked_symbols" test/ --include="*.py"v2.9.0 共命中 8 行,主要集中在
test/test_proxy_tensor.py与test/functorch/test_control_flow.py。结论:5 个 API 属于上游能力范围,但测试覆盖对 NPU 场景不完整。本 PR 通过「上游 patch + 自写测试」组合补齐 Torch-NPU 侧可验证性。
【修改方案】
1. 上游 patch:
test_upstream/test/test_proxy_tensor.py.patch针对 PyTorch v2.9.0 官方源码
test/test_proxy_tensor.py做最小化 NPU 适配,覆盖:TestSymbolicTracing.test_mega_guardShapeEnv.produce_guardsTestSymbolicTracing.test_guards_equalShapeEnv.produce_guards/ShapeEnv.get_nontrivial_guards(_assert_no_guards内部使用)适配方式(按一致性文档 六.3 原社区用例最小化原则):
git diff在 PyTorch v2.9.0 官方源码上生成(合规要求六.5).npu()——原文件无获取设备命令时的最小化做法,不引入device_type推导test_guards_equal内部以同名局部_traceshadow 模块级_trace,不引入新函数名,所有_trace调用点 0 改动patch 体量:仅 24 行 / 2 个 hunk
test_mega_guardtorch.randn(16), torch.randn(8)→ 各加.npu(),单行替换test_guards_equal_trace局部定义(inps用torch.randn(arg).npu())patch 已在
/workspace/pytorch_v2.9.0通过patch --dry-run验证,无 hunk 失败、无文件冲突。2. 自写测试:
test/fx/test_symbolic_shapes.py新增 8 个测试方法,写法与同目录 FX 测试一致(
TestCase+run_tests,默认 NPU 环境可用):test_is_accessor_node_with_call_methodis_accessor_node+ NPU tensorexample_valuetest_is_accessor_node_with_call_functionis_accessor_node正负分支test_is_concrete_int_with_literal_and_device_shapeis_concrete_int/is_symbolic+ NPU tensor.size()test_is_concrete_float_with_literal_and_symbolic_valueis_concrete_float/is_symbolictest_is_concrete_bool_with_literal_and_symbolic_valueis_concrete_bool/is_symbolictest_shape_env_get_pruned_guardsShapeEnv.get_pruned_guards(无直接上游用例,补齐空白)test_shape_env_ignore_fresh_unbacked_symbolsShapeEnv.ignore_fresh_unbacked_symbols上下文管理test_shape_env_is_unbacked_symintShapeEnv.is_unbacked_symint(无直接上游用例,补齐空白)代码规范(合规要求六.8):
unittest/torch_npu导入与@unittest.skipUnless装饰器torch.accelerator.current_accelerator()推导device_type,张量统一通过.to(device_type)切到 NPUself.assert*,无print/try/except2.1 新增用例必要性与完整性分析
produce_guards/get_nontrivial_guards:上游test_mega_guard/test_guards_equal已覆盖语义,但运行在 CPU 路径;本 PR patch 把 tensor 输入切到 NPU,验证 ShapeEnv guard 生成在 NPU 张量元数据下结果一致,属于必要的设备路径补齐。get_pruned_guards/is_unbacked_symint:上游基线test/缺少这两个方法的直接用例。本 PR 新增专项用例验证返回类型、unbacked 标识等核心语义,为必要补齐。ignore_fresh_unbacked_symbols:v2.9.0 起上游test/functorch/test_control_flow.py::test_cond_gen_schema_symbool_inputs间接使用了该 API,但作为上下文管理器的独立行为验证不充分。本 PR 通过自写用例补齐独立的 enter/exit 语义验证。结论:本 PR 的新增测试属于「对上游部分覆盖场景做 NPU 侧补齐 + 对上游缺失场景补空白」,不是重复建设。
3. 多版本分支合入
按 Ascend PyTorch 版本线分别提 MR,每个 MR 仅 1 个 squash 提交:
【资料变更】
本 PR 仅新增测试用例,不在本 PR 内修改任何资料文档。
资料补齐的统一处理方式:
所有 5 个 ShapeEnv API 的文档支持状态由独立的文档 PR 统一处理,详见:
docs: add ShapeEnv API support entries to torch.fx native_apis docs(提交至 v2.7.1 分支,按规范统一管理)pytorch_2-9-0/torch-fx.md中均标记为 "是"(支持)本 PR 已检查的文档状态:
【接口变更】
本 PR 不涉及任何对外接口的变更,具体说明:
1. 不修改 torch_npu 任何公共 / 私有 API
.py/.cpp/.h源码文件torch_npu/__init__.py等导出清单2. 仅引入测试代码
test/fx/test_symbolic_shapes.pyget_pruned_guards/is_unbacked_symint/ignore_fresh_unbacked_symbols3 个 APItest_upstream/test/test_proxy_tensor.py.patchtest/test_proxy_tensor.py的 NPU 适配 patch,覆盖produce_guards/get_nontrivial_guards2 个 API3. 不改变 PyTorch / torch_npu 现有 API 行为
4. 不引入新的 import / 依赖
sympy、torch、torch.fx、torch.testing._internal.common_utilstorch_npu显式 import(按规范六.8 ——昇腾环境下导入torch会默认加载torch_npu)5. CI 与回归影响评估
【功能验证】
1. 测试环境
torch.npu.is_available()跳过逻辑2. 自写测试命令
cd /tmp && python3.11 /workspace/pytorch/test/fx/test_symbolic_shapes.py3. 自写测试结果
4. 上游 patch 验证
cd /workspace/pytorch_v2.9.0 patch -p1 --dry-run < /workspace/pytorch/test_upstream/test/test_proxy_tensor.py.patch输出:所有 hunk 应用成功,无文件冲突。
5. 与上游用例的关系
produce_guards/get_nontrivial_guards语义test_mega_guard/test_guards_equal(CPU 路径)get_pruned_guardstest_shape_env_get_pruned_guardsis_unbacked_syminttest_shape_env_is_unbacked_symintignore_fresh_unbacked_symbolstest_control_flow.py中间接使用test_shape_env_ignore_fresh_unbacked_symbols独立验证上下文管理【CheckList】