Pull Request已成功合入, 合并人@ascend-robot
(感谢 黄桂军 的贡献)变更摘要
该 PR 为重构优化(关联 issue #4241),将 Shape Handling 相关实现从 torch_npu/utils/_shape_handling.py 迁移至 torch_npu/utils/_dynamo.py,并将 torch_npu/_inductor/shape_handling.py 收敛为兼容 shim,同时调整了 Dynamo patch 的安装时机。此改动主要聚焦于代码归属重组与加载时序优化,不涉及业务接口变更,并保留 torch_npu._inductor.shape_handling 兼容入口。
主要改动
-
Shape Handling 实现迁入
_dynamo.py: 将NPUShapeHandling类、unified_copy深拷贝函数、_patch_dynamo_context(对_TorchDynamoContext.__call__/__init__的 patch 实现)与_patch_shape_handling完整迁移至torch_npu/utils/_dynamo.py,并新增copy、warnings、TreeSpec/tree_flatten/tree_unflatten等依赖导入。 -
torch_npu/_inductor/shape_handling.py改为兼容 shim: 删除原独立实现(约 470 行),改为从torch_npu.utils._dynamo导入,仅保留NPUShapeHandling、unified_copy、patch_dynamo_context、patch_shape_handling既有入口;patch_shape_handling维持_is_patched幂等状态,并在 patch 时同步置位_impl._patch_shape_handling._is_patched。 -
Dynamo patch 安装时机提前: 在
patch_inductor_wrapper中,shape handling patch 由原先new_call内 backend scope 中的torch_npu._inductor.patch_shape_handling()调用,改为在 wrapper 初始化/backend 解析阶段(new_init与new_get_config_copy的非延迟分支)执行_patch_shape_handling(),即先于register_inductor_npu(backend scope)完成安装。 -
保持 Inductor 延迟加载时序:
_dynamo.py顶层不导入torch_npu._inductor,延迟初始化仍经_lazy_dynamo_setup/_lazy_inductor_setup完成,测试断言编译后_lazy_inductor_setup.has_run为 False 且torch_npu._inductor未进入sys.modules。 -
测试用例同步更新:
test/dynamo/test_compile_trigger.py将原“backend 选择后安装 shape handling”用例改为验证 shape handling 在 backend 选择前初始化(事件顺序变为shape_handling→scope_register),新增_NpuBackendScope入口失败后环境变量恢复的断言,并补充 shape handling 延迟加载用例。


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 |
|---|---|---|
| repo-Ascend/pytorch | ✅ li_jing_hw, rmch (2/2) | ✅ li_jing_hw (1/1) |
| test | ✅ li_jing_hw, rmch (2/2) | ✅ li_jing_hw (1/1) |
| torch_npu/_inductor | ✅ li_jing_hw, rmch (2/2) | ✅ li_jing_hw (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.12.0 | ||
| v2.11.0 | ||
| v2.10.0 | ||
| v2.9.0 | ||
| v2.7.1 | ||
| v2.12.0-26.1.0 | ||
| v2.7.1-26.1.0 | ||
| v2.10.0-26.1.0 | ||
| v2.11.0-26.1.0 | ||
| v2.9.0-26.1.0 | ||
| ci-test |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| Build_X86_213 | ✅ | >>> | |
| Build_ARM_213 | ✅ | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | codecheck_pre-commit | ✅ | >>> |
| check_error | ✅ | >>> | |
| lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | 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 | ✅ | >>> | |
| UT_ARM_A2_Part_01_213 | ✅ | >>> | |
| UT_ARM_A2_Part_02_213 | ✅ | >>> | |
| UT_ARM_A2_Part_03_213 | ✅ | >>> | |
| UT_inductor_Part_01_213 | ✅ | >>> | |
| UT_inductor_Part_02_213 | ✅ | >>> | |
| UT_inductor_Part_03_213 | ✅ | >>> | |
| UT_inductor_Part_04_213 | ✅ | >>> | |
| UT_DIST_ARM_Part_01_213 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02_213 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03_213 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04_213 | 🛑 | >>> | |
| UT_ARM_A2_Select_Part_01_213 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02_213 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/approve




【合入来源】
关联 Issue: #4094
关联 Issue:#4204
关联 Issue:#4241(Shape Handling 迁移到 Dynamo utils)
【修改方案】
NPUShapeHandling、unified_copy、Shape Handling 的 Dynamo patch 实现从torch_npu/utils/_shape_handling.py迁移至torch_npu/utils/_dynamo.py。torch_npu/_inductor/shape_handling.py为兼容 shim,保留NPUShapeHandling、unified_copy、patch_dynamo_context和patch_shape_handling的既有入口及幂等状态语义。_dynamo.py顶层导入torch_npu._inductor。【资料变更】
不涉及
【接口变更】
不涉及。保留
torch_npu._inductor.shape_handling兼容入口。【功能验证】
【CheckList】