当前Pull Request已关闭, 关闭人@2501_93637465
变更摘要
此 PR 为 torch.compiler.assume_constant_result API 新增 NPU 设备的功能测试,验证该 API 在 NPU 环境下能够被 torch.compile 正确捕获、编译函数执行结果正确,以及被标记函数的结果在编译期间被视作常量仅计算一次。
主要改动
- 新增测试文件
test/dynamo/test_assume_constant_result.py:为torch.compiler.assume_constant_result在 NPU 上创建独立的兼容性测试用例,文件包含 40 行测试代码。 - 新增测试类
TestAssumeConstantResult:继承unittest.TestCase,通过torch._dynamo.reset()重置 Dynamo 状态后执行测试逻辑。 - 验证 API 返回原函数对象:使用
self.assertIs断言torch.compiler.assume_constant_result(constant_scale)返回的正是传入的constant_scale函数本身。 - 验证 NPU Tensor 在编译函数中的正确执行:在
npu设备上创建torch.arange张量,通过torch.compile配合backend="eager"和fullgraph=True编译后,连续两次执行并比对cpu()结果与预期值一致。 - 验证常量折叠语义:通过
nonlocal call_count计数器断言被assume_constant_result标记的函数在编译期间仅被调用一次(self.assertEqual(call_count, 1)),确认其结果被视作编译时常量。


代码审查
我已经完成了对 test/dynamo/test_assume_constant_result.py 的详细审查。以下是我的分析:
审查分析
逐行检查
-
导入 (第 1-4 行):
unittest、torch、torch_npu— 标准的 NPU 测试导入模式,与该目录下其他测试文件(如test_compile.py、test_trace_stream_event.py)一致。 -
类定义 (第 7 行):继承
unittest.TestCase,与test_compile.py中的PublicTorchCompilerTests模式一致。 -
torch._dynamo.reset()(第 9 行):重置 dynamo 状态,这是该目录下广泛使用的标准模式(在test_modules.py、test_subgraphs.py、test_repros.py、test_export.py等文件中均有使用)。 -
call_count+nonlocal(第 10-14 行):使用nonlocal在闭包中修改外部变量,Python 语法正确。 -
torch.compiler.assume_constant_result()(第 17-19 行):调用公开 API 包装函数。PR 描述明确说明该 API 返回原函数对象,与第 20 行的assertIs断言一致。 -
torch.compile()(第 25 行):使用backend="eager"和fullgraph=True。backend="eager"是该测试目录下常用的后端选择(参见test_export.py、test_repros.py)。 -
NPU 张量操作 (第 26-30 行):创建 NPU 张量,执行编译函数,
npu.synchronize()同步设备。这些操作顺序正确。 -
断言 (第 32-36 行):
torch.equal(actual.cpu(), (x * 2.0).cpu())— 将 NPU 张量移至 CPU 后比较,避免设备不匹配。self.assertEqual(call_count, 1)— 验证常量结果只计算一次。
-
入口 (第 39-40 行):
unittest.main(),与test_compile.py和test_trace_stream_event.py一致。
结论
该测试文件逻辑正确、模式与目录中现有测试一致,没有发现任何正确性、安全性、可靠性或破坏性变更问题。
总结
- 变更文件:1 个(
test/dynamo/test_assume_constant_result.py) - 发现问题:0 个
- 风险判断:此变更为新增 NPU 功能测试,仅新增 40 行测试代码,不修改任何生产代码。测试逻辑正确,模式与现有测试一致,风险极低。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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_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 : 停止流水线


【合入来源】
https://gitcode.com/Ascend/pytorch/issues/2990
关联 Issue:https://gitcode.com/Ascend/pytorch/issues/3036
Fixes [#3036]
API:
torch.compiler.assume_constant_result【修改方案】
新增独立测试文件
test/dynamo/test_assume_constant_result.py,为torch.compiler.assume_constant_result补充 NPU 功能验证,覆盖:torch.compile正常捕获;该 API 由 PyTorch TorchDynamo 提供,不需要新增 torch_npu 算子实现。
【资料变更】
不涉及。
【接口变更】
不涉及接口定义变更,仅新增兼容性测试。
【功能验证】
TestAssumeConstantResult.test_assume_constant_result(fn)一致性python -m py_compile test/dynamo/test_assume_constant_result.pygit diff --check origin/master...HEAD测试环境:
2.10.0+cpu2.10.0【CheckList】