已关闭
test(dynamo): cover assume_constant_result on NPU #41989
2501_93637465创建于 7月17日关闭于 27 天前
test(dynamo): cover assume_constant_result on NPU #41989
已关闭
2501_93637465创建于 7月17日关闭于 27 天前
2501_93637465
2501_93637465
7月17日

【合入来源】

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 功能验证,覆盖:

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

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

【资料变更】

不涉及。

【接口变更】

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

【功能验证】

测试项 结果
TestAssumeConstantResult.test_assume_constant_result PASS
公开/私有 API 签名 (fn) 一致性 PASS
python -m py_compile test/dynamo/test_assume_constant_result.py PASS
git diff --check origin/master...HEAD PASS

测试环境:

  • Ascend 910
  • PyTorch 2.10.0+cpu
  • torch_npu 2.10.0

【CheckList】

likedislike
当前Pull Request已关闭, 关闭人@2501_93637465
2501_936374652501_93637465
7月17日 创建了 pull request,commit ecf407d9
2501_936374652501_93637465
7月17日 关联了issue:【社区任务】7月社区任务第二期-Ascend for PyTorch API 一致性开发(93)
atomgit-bot
atomgit-bot
7月17日 评论:

变更摘要

此 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)),确认其结果被视作编译时常量。
likedislike
atomgit-bot
atomgit-bot
7月17日 评论:

代码审查

我已经完成了对 test/dynamo/test_assume_constant_result.py 的详细审查。以下是我的分析:

审查分析

逐行检查

  1. 导入 (第 1-4 行):unittesttorchtorch_npu — 标准的 NPU 测试导入模式,与该目录下其他测试文件(如 test_compile.pytest_trace_stream_event.py)一致。

  2. 类定义 (第 7 行):继承 unittest.TestCase,与 test_compile.py 中的 PublicTorchCompilerTests 模式一致。

  3. torch._dynamo.reset() (第 9 行):重置 dynamo 状态,这是该目录下广泛使用的标准模式(在 test_modules.pytest_subgraphs.pytest_repros.pytest_export.py 等文件中均有使用)。

  4. call_count + nonlocal (第 10-14 行):使用 nonlocal 在闭包中修改外部变量,Python 语法正确。

  5. torch.compiler.assume_constant_result() (第 17-19 行):调用公开 API 包装函数。PR 描述明确说明该 API 返回原函数对象,与第 20 行的 assertIs 断言一致。

  6. torch.compile() (第 25 行):使用 backend="eager"fullgraph=Truebackend="eager" 是该测试目录下常用的后端选择(参见 test_export.pytest_repros.py)。

  7. NPU 张量操作 (第 26-30 行):创建 NPU 张量,执行编译函数,npu.synchronize() 同步设备。这些操作顺序正确。

  8. 断言 (第 32-36 行):

    • torch.equal(actual.cpu(), (x * 2.0).cpu()) — 将 NPU 张量移至 CPU 后比较,避免设备不匹配。
    • self.assertEqual(call_count, 1) — 验证常量结果只计算一次。
  9. 入口 (第 39-40 行):unittest.main(),与 test_compile.pytest_trace_stream_event.py 一致。

结论

该测试文件逻辑正确、模式与目录中现有测试一致,没有发现任何正确性、安全性、可靠性或破坏性变更问题。


总结

  • 变更文件:1 个(test/dynamo/test_assume_constant_result.py
  • 发现问题:0 个
  • 风险判断:此变更为新增 NPU 功能测试,仅新增 40 行测试代码,不修改任何生产代码。测试逻辑正确,模式与现有测试一致,风险极低。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
此处折叠了136条消息 查看更多
ascend-robotascend-robot成员
7月27日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
7月27日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月27日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月27日 评论:
流水线 PR-pipeline_pytorch#49868 [ commitID:f30f587c ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
2501_936374652501_93637465
27 天前 关闭了 pull request