合并受阻
变更摘要
此 PR 为 torch.compiler.assume_constant_result API 新增了独立的 NPU 功能测试文件 test/dynamo/test_assume_constant_result.py,验证该 API 在 NPU 设备上的行为正确性,包括返回原函数对象、编译捕获、连续执行结果一致性以及常量折叠只计算一次的语义。
主要改动
- 新增测试文件
test/dynamo/test_assume_constant_result.py: 引入TestAssumeConstantResult测试类,包含一个完整的test_assume_constant_result测试用例,覆盖torch.compiler.assume_constant_result在 NPU 上的核心功能验证。 - 验证 API 返回原函数对象: 通过
assertIs断言assume_constant_result的返回值与被标记的原始函数是同一对象。 - 验证编译捕获与连续执行正确性: 使用
torch.compile以eager后端编译包含被标记函数的计算图,分别对 NPU Tensor 的两次不同输入执行,并通过torch.equal校验输出结果。 - 验证常量折叠语义: 通过
nonlocal计数器call_count验证被assume_constant_result标记的函数在多次调用编译函数时仅执行一次(结果在编译期间被视为常量)。 - 同步 NPU 执行: 在断言前调用
torch_npu.npu.synchronize()确保异步错误被及时捕获,避免因异步执行导致测试结果不准确。


代码审查
最终复查
对 diff 中每行逐一复查:
- 第 5 行
device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu":walrus 运算符和回退逻辑正确,与仓库中广泛使用的模式一致。 - 第 10 行
torch._dynamo.reset():内部 API,但在test/dynamo/中大量使用(test_modules.py、test_subgraphs.py、test_export.py等),属于既定模式。 - 第 11–16 行
call_count/constant_scale:nonlocal作用域正确,闭包捕获无误。 - 第 18–21 行
assume_constant_result+assertIs:API 调用和身份断言正确。 - 第 23–26 行
fn定义 +torch.compile:闭包捕获和编译参数正确。 - 第 27–30 行 张量创建和两次调用:逻辑正确,
assume_constant_result在 tracing 期间仅执行一次。 - 第 33–37 行 断言:
torch.equal和call_count == 1语义正确。assertTrue(torch.equal(...))的错误信息不理想,但这是仓库中的常见模式,不构成缺陷。
未发现同位置的第二缺陷(值/索引/顺序/字段语义均与命名和约定一致)。
审查总结
- P0: 0
- P1: 0
- P2: 0
- P3: 1
已审查文件: test/dynamo/test_assume_constant_result.py(发现 1 个 P3 问题:缺少 NPU 可用性守卫)
总体风险评估: 低风险。测试逻辑正确,全面覆盖了 assume_constant_result 在 NPU 上的关键行为(返回原函数、编译捕获、连续执行正确性、常量只计算一次、异步同步)。唯一的不足是非 NPU 环境下缺少 skip 守卫,属于测试工程健壮性问题,不影响 NPU CI 上的测试有效性。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | ✅ | >>> | |
| 恶意代码检查 | 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 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


The MR can not be merged, because of CodeReview discussion not resolved
If you want to solve this problem, you can click here to do it in the FAQs.


The MR can not be merged, because of CodeReview discussion not resolved
If you want to solve this problem, you can click here to do it in the FAQs.


【合入来源】
任务来源:https://gitcode.com/Ascend/pytorch/issues/2990
关联 Issue:https://gitcode.com/Ascend/pytorch/issues/3036
Fixes #3036
API:
torch.compiler.assume_constant_result目标分支:
v2.7.1【修改方案】
PyTorch 官方社区已在
test/dynamo/test_decorators.py中提供torch.compiler.assume_constant_result的功能测试。按照 API 一致性指南 1.1 类要求,本 PR 不再新增独立测试文件,
而是在以下路径提交由
git diff生成的 NPU 适配 patch:test_upstream/test/dynamo/test_decorators.py.patch适配内容:
torch_npu。不涉及 API 或算子实现代码修改。
PyTorch 上游已有 assume_constant_result 测试场景,因此本 PR 不新增独立的 test/ 测试文件,而是在 test_upstream/test/dynamo/test_decorators.py.patch 中对上游测试补充 NPU Tensor 适配。
Patch 保留上游原有测试逻辑,仅将测试 Tensor 迁移到 NPU,用于验证现有上游场景在 Ascend NPU 环境下的兼容性。
【资料变更】
API 支持资料由 PR !42850 统一更新,覆盖 PyTorch 2.7.1、2.9.0、
2.10.0、2.11.0 和 2.12.0。
【接口变更】
不涉及接口定义变更,仅适配 PyTorch 官方测试在 NPU 上运行。
【功能验证】
python -m py_compilegit diff --check【CheckList】