已关闭
test(dynamo): cover assume_constant_result on NPU #42026
2501_93637465创建于 7月18日关闭于 8月6日
test(dynamo): cover assume_constant_result on NPU #42026
已关闭
2501_93637465创建于 7月18日关闭于 8月6日
2501_93637465
2501_93637465
7月18日

【合入来源】

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 功能测试,验证:

  • API 返回原函数对象;
  • 使用 NPU Tensor 时可被 torch.compile 正常捕获;
  • 编译函数连续执行结果正确;
  • 被标记函数的结果在编译期间被视为常量,只计算一次。

该 API 由 PyTorch TorchDynamo 提供,不需要新增 torch_npu 算子实现。

【资料变更】

不涉及。

【接口变更】

不涉及接口定义变更,仅新增兼容性测试。

【功能验证】

测试项 结果
test/dynamo/test_assume_constant_result.py PASS
python -m py_compile PASS
git diff --check PASS

测试环境:

  • Ascend 910
  • PyTorch 2.10.0+cpu
  • torch_npu 2.10.0

【CheckList】

likedislike
当前Pull Request已关闭, 关闭人@2501_93637465
2501_936374652501_93637465
7月18日 创建了 pull request,commit a1f2cf19
2501_936374652501_93637465
7月18日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(93)
atomgit-bot
atomgit-bot
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)),确认其结果被视为编译期常量。
likedislike
atomgit-bot
atomgit-bot
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 环境下的核心行为。发现的两个问题均为工程一致性问题:

  1. TestCase 导入源不一致(P2):使用了 torch.testing._internal.common_utils.TestCase,而该目录下所有其他测试均使用 torch._dynamo.test_case.TestCase,可能导致测试隔离不充分。
  2. 无条件的 NPU synchronize 调用(P2):torch_npu.npu.synchronize()device_type 回退为 CPU 时仍被调用,语义错误且可能在无 NPU 环境下引发问题。
类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

likedislike
此处折叠了55条消息 查看更多
ascend-robotascend-robot成员
7月27日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月27日 评论:

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

likedislike
ascend-robotascend-robot成员
7月27日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月27日 添加了label:docs-ci-pipeline-success
2501_936374652501_93637465
8月6日 关闭了 pull request