已关闭
test(dynamo): cover assume_constant_result on NPU #42026
2501_93637465创建于 7月18日关闭于 8月6日
test(dynamo): cover assume_constant_result on NPU #42026
已关闭
当前Pull Request已关闭, 关闭人@2501_93637465
7月18日 创建了 pull request,commit a1f2cf19
7月18日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(93)
atomgit-bot
7月18日 评论:
7月18日 评论:
变更摘要
此 PR 为 torch.compiler.assume_constant_result API 新增了 NPU 环境下的独立功能测试,验证该 API 在 Ascend 设备上可正常工作。测试覆盖了 API 返回原函数对象、与 torch.compile 的兼容性、编译后函数连续执行的正确性,以及被标记函数在编译期间仅计算一次(常量折叠)的核心语义。
主要改动
- 新增测试文件
test/dynamo/test_assume_constant_result.py:添加了完整的 NPU 兼容性测试用例,包含TestAssumeConstantResult测试类和test_assume_constant_result测试方法。 - 验证
assume_constant_result返回原函数对象:通过self.assertIs(marked_constant_scale, constant_scale)断言 API 返回的是原始函数引用本身。 - 验证 NPU Tensor 与
torch.compile兼容性:使用torch.compile(fn, backend="eager", fullgraph=True)编译包含被标记函数的计算图,并以 NPU 设备上的 Tensor 作为输入执行。 - 验证编译后连续执行结果正确:对
compiled_fn(x)和compiled_fn(x + 1)两次调用分别断言输出与预期值x * 2.0和(x + 1) * 2.0一致。 - 验证常量折叠语义:通过闭包中的
call_count计数器断言被assume_constant_result标记的函数在编译期间仅被调用一次(self.assertEqual(call_count, 1)),确认其结果被视为编译期常量。


ascend-robot
7月18日 评论:
7月18日 评论:
atomgit-bot
7月18日 评论:
7月18日 评论:
代码审查
审查总结
审查了 1 个变更文件:
- test/dynamo/test_assume_constant_result.py — 发现 2 个问题(均为 P2)
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 2 |
| P3 | 0 |
总体评估:该变更风险较低。测试逻辑本身正确,验证了 torch.compiler.assume_constant_result 在 NPU 环境下的核心行为。发现的两个问题均为工程一致性问题:
- TestCase 导入源不一致(P2):使用了
torch.testing._internal.common_utils.TestCase,而该目录下所有其他测试均使用torch._dynamo.test_case.TestCase,可能导致测试隔离不充分。 - 无条件的 NPU synchronize 调用(P2):
torch_npu.npu.synchronize()在device_type回退为 CPU 时仍被调用,语义错误且可能在无 NPU 环境下引发问题。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 2 |
💬 仅评论


此处折叠了55条消息 查看更多
7月27日 添加了label:docs-ci-pipeline-running
ascend-robot
7月27日 评论:
7月27日 评论:
✅ 跳过 docs ci 检查,没有需要检查的文档文件


7月27日 删除了label:docs-ci-pipeline-running
7月27日 添加了label:docs-ci-pipeline-success
8月6日 关闭了 pull request
【合入来源】
https://gitcode.com/Ascend/pytorch/issues/2990
关联 Issue:https://gitcode.com/Ascend/pytorch/issues/3036
Fixes #3036
API:
torch.compiler.assume_constant_result目标分支:
v2.10.0【修改方案】
为
torch.compiler.assume_constant_result补充独立的 NPU 功能测试,验证:torch.compile正常捕获;该 API 由 PyTorch TorchDynamo 提供,不需要新增 torch_npu 算子实现。
【资料变更】
不涉及。
【接口变更】
不涉及接口定义变更,仅新增兼容性测试。
【功能验证】
test/dynamo/test_assume_constant_result.pypython -m py_compilegit diff --check测试环境:
2.10.0+cpu2.10.0【CheckList】