已开启
test(dynamo): cover assume_constant_result on NPU #42032
2501_93637465创建于 7月18日
test(dynamo): cover assume_constant_result on NPU #42032
已开启
合并受阻
7月18日 创建了 pull request,commit 40673717
7月18日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(93),[API一致性] torch.compiler.assume_constant_result NPU兼容性测试补齐
7月18日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(93),[API一致性] torch.compiler.assume_constant_result NPU兼容性测试补齐
ascend-robot
7月18日 评论:
7月18日 评论:
atomgit-bot
7月18日 评论:
7月18日 评论:
代码审查
经过对测试文件的全面审查,该测试文件逻辑正确、结构清晰,我未发现任何需要报告的问题。
以下是我逐项验证的审查结论:
逻辑正确性
torch.compiler.assume_constant_resultAPI 正确使用,assertIs验证了返回原函数对象的语义。call_count闭包变量配合nonlocal正确追踪constant_scale的调用次数,验证常量函数仅计算一次。torch.compile(fn, backend="eager", fullgraph=True)与assume_constant_result的交互语义正确——首次调用触发 tracing 时调用一次constant_scale,后续调用复用已编译图,不再调用。- NPU 张量计算与预期结果比较逻辑正确:
actual.cpu()与(x * 2.0).cpu()均在 NPU 上计算,浮点一致性有保障。
安全与可靠性
- 无敏感信息硬编码,无命令/SQL 注入风险,无路径遍历。
torch_npu.npu.synchronize()在断言前显式同步 NPU,确保异步报错被及时捕获,符合 NPU 测试最佳实践。- 无资源泄漏:无文件/连接需要管理,张量由 Python GC 自动回收。
边界值
torch.arange(4)产生固定形状[4],两次调用输入形状一致(x与x+1均为[4]),不会触发 Dynamo 重编译。device_type通过torch.accelerator.current_accelerator()动态获取,None时回退到"cpu",walrus operator 使用正确。
与现有代码一致性
- 该目录下其他测试文件(如
test_npu_backend.py、test_compile.py)同样无条件import torch_npu并使用torch._dynamo.reset(),本文件遵循相同惯例。
关闭总结
- 审查文件数:1
test/dynamo/test_assume_constant_result.py:无问题
- 发现问题:0(P0: 0, P1: 0, P2: 0, P3: 0)
- 整体风险评估:低风险。该变更为新增的针对性 NPU 兼容性测试,逻辑简洁、覆盖明确,与已有测试基础设施一致,可安全合入。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


7月18日 添加了label:ascend-cla/yes
此处折叠了140条消息 查看更多
16 天前 删除了label:ci-pipeline-running
16 天前 添加了label:ci-pipeline-passed
ascend-robot
16 天前 评论:
16 天前 评论:
流水线 PR-pipeline_pytorch#62168 [ commitID:e8a0a0cd ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 : 停止流水线


ascend-robot
16 天前 评论:
16 天前 评论:
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.


ascend-robot
16 天前 评论:
16 天前 评论:
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.11.0【修改方案】
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】