Open
feat(optim): 为 npu_bert_apply_adam 增加 Ascend950 ACLNN 通路 #6194
feat(optim): 为 npu_bert_apply_adam 增加 Ascend950 ACLNN 通路 #6194
Open
陈展熹Add on 11 days ago
陈展熹
陈展熹
11 days ago

【合入来源】

为 torch_npu.npu_bert_apply_adam(..., out=(var, m, v)) 增加 Ascend950 的 ApplyAdamV2 ACLNN 通路。

配套 ops-nn PR:https://gitcode.com/cann/ops-nn/pull/14078 。部署到 950 时,需要 CANN/自定义算子包提供该 PR 自动生成的 aclnnApplyAdamV2 及 aclnnApplyAdamV2GetWorkspaceSize 符号。

【修改方案】

  1. 为 npu_bert_apply_adam 和 .out 注册 op_api: all_version,使公开 Python 接口能够进入新增的 op_api 适配层。
  2. 按设备保留兼容行为:
    • 非 IsAclnnOnly() 设备(包括 910/A2/A3)仍调用原 acl_op::npu_bert_apply_adam_out;即使新 ACLNN 符号可用,也保持原通路。
    • 950 等 IsAclnnOnly() 设备执行 aclnnApplyAdamV2。
    • 使用 DO_COMPATIBILITY 检查符号;950 缺少 ACLNN 接口时给出明确错误,旧设备允许继续使用原 ACL 通路。
  3. 将 Scalar 参数转换为与 var 相同 dtype/device 的 Tensor,适配自动生成的 ACLNN ABI;adam_mode == 0 映射为 adam,其余值映射为 mbart_adam。
  4. step_size=None 时使用 lr Tensor:Adam 模式忽略该输入,mBART 使用 lr 作为默认步长,同时兼容旧版 tiling 对 optional 输入 shape 的检查。
  5. 保留 var/m/v 的原地更新和返回别名;不带 out 的重载维持原有报错行为。
  6. 增加通过公开 Python API 的回归用例,覆盖默认参数、数值、模式映射和原地别名。

调用关系:

torch_npu.npu_bert_apply_adam(..., out=(var, m, v))
  -> op_api::npu_bert_apply_adam_out
     -> 910/A2/A3: acl_op::npu_bert_apply_adam_out
     -> 950: aclnnApplyAdamV2 -> ApplyAdamV2 kernel

【资料变更】

不涉及独立资料变更;通路和兼容设计记录于本 PR 的修改方案。

【接口变更】

公开 Python 签名、默认参数、out=(var, m, v) 调用方式及原地更新语义保持兼容。

跨仓新增对 ops-nn 自动生成的 aclnnApplyAdamV2 接口的调用,配套 PR 见上文。

【功能验证】

环境:Python 3.12、PyTorch 2.7.1、源码构建的 TorchNPU 2.7.1.post11、CANN 9.2、Ascend950PR_9579。

  • 将本 PR 的 op-plugin 源码集成到 TorchNPU,成功构建 wheel 并隔离安装;核对 wheel 与安装后的 native 库哈希一致,且实际导入隔离安装目录中的包。
  • 执行完整 test/test_base_ops/test_npu_bertapplyadam.py:
Ran 5 tests in 0.436s
OK
failures=0, errors=0, skipped=0
  • 新增 16 组 FP16/FP32 数值子用例,覆盖 Adam/mBART、非零模式映射、权重衰减、梯度裁剪、显式/缺省 step_size、var/m/v 原地更新及返回值地址一致性;同时验证无 out 重载的兼容报错。
  • mbart_adam + step_size=None 的 FP16/FP32 用例均通过。
  • 日志同时观测到 npu_bert_apply_adam_out exec aten 和 aclnnApplyAdamV2 EXEC_NPU_CMD,并确认加载了新构建的 TorchNPU 库及任务专用 ACLNN 接口库。
  • 在独立进程中去掉新增 ACLNN 接口库后,950 按预期报 Current device only support aclnn operators, but aclnnApplyAdamV2 or aclnnApplyAdamV2GetWorkspaceSize not found。
  • git diff --check 通过。

验证范围:上述测试使用本机 CANN 9.2 已安装的 kernel,未重新构建当前 ops-nn 工作树 kernel;910/A2/A3 的原通路通过代码检查保留,未作对应硬件实测。

CI 修正(2026-11-11)

原流水线 8 个 x86/ARM 编译任务全部通过,失败发生在 Ascend910B4 的 Python UT:新增通用数值测试有 4 个子用例断言失败,原有两个用例和 requires_out 用例通过。

本次仅修正测试范围:

  • 通用测试保留 Adam 默认 step 的无衰减/衰减/梯度缩放,以及 mBART mode=1/2 的无衰减显式 step;FP16/FP32 合计 10 组,继续检查 var/m/v 数值、返回/输入地址、dtype 与 grad 不变。
  • Ascend950 专有测试保留 Adam 显式 step、mBART 显式 step+衰减、mBART 缺省 step+衰减;FP16/FP32 合计 6 组。
  • 为子用例标签补充 step_size,消除原 CI 日志中两个 Adam 场景同名的歧义。

依据:对照 legacy TBE kernel 源码,mBART 衰减分支的临时 buffer 会被后续 lrvar 覆盖,与 950 的 weight_decaylr*var 计算不同,不能向旧 ACL kernel 施加 950 专有数值预期。原日志未打印实际数值,且两个 Adam 子用例标签相同,不能据此断言具体是哪一个 step_size 场景失败。

验证:本机 Ascend950 上重新执行完整测试文件,Ran 5 tests in 0.436s / OK,0 失败、0 错误、0 跳过;16 组 FP16/FP32 数值子用例均通过,执行日志仍确认进入 aclnnApplyAdamV2。runtime、schema 和精度阈值保持本 PR 原实现。旧设备结果由本次重新触发的 CI验证。

【CheckList】

AI/Agent 生成声明:本 PR 由 AI 辅助编写,已完成上述本机编译与通路验证。

likedislike
Merge blocked
陈展熹陈展熹
11 days ago Created pull request, commit 6f26afe3
atomgit-bot
atomgit-bot
11 days ago Comments:

变更摘要

本 PR 为 torch_npu.npu_bert_apply_adam(..., out=(var, m, v)) 增加 Ascend950 的 ApplyAdamV2 ACLNN 通路:在算子配置中为 npu_bert_apply_adam 及其 .out 变体新增 op_api 注册,使公开 Python 接口可以进入新增的 op_api 适配层;适配层按设备分流,非 IsAclnnOnly() 设备继续走原有 acl_op::npu_bert_apply_adam_out,950 等设备调用自动生成的 aclnnApplyAdamV2,并通过 DO_COMPATIBILITY 在缺少 ACLNN 符号时给出明确报错。同时补充了通过公开 Python API 的回归用例,保持公开签名、默认参数、out 调用方式与原地更新语义不变。

主要改动

  • 算子配置注册 op_api 通路: 在 op_plugin/config/op_plugin_functions.yaml 中为 npu_bert_apply_adam 与 npu_bert_apply_adam.out 各增加 op_api: all_version,使两个接口可以分发到新增的 op_api 实现。
  • 新增 BertApplyAdamKernelNpuOpApi.cpp 适配层: 新增 op_api::npu_bert_apply_adam_out,先用 DO_COMPATIBILITY 检查 aclnnApplyAdamV2 符号是否可用,再在 !c10_npu::IsAclnnOnly() 时回退到 acl_op::npu_bert_apply_adam_out,保证 910/A2/A3 等设备通路不变。
  • Scalar 转 Tensor 及模式映射: 将 lr、beta1、beta2、epsilon、max_grad_norm、global_grad_norm、weight_decay 通过 copy_scalar_to_device 转换为与 var 相同 dtype/device 的 Tensor,并把 adam_mode == 0 映射为 "adam"、其余映射为 "mbart_adam",最后经 EXEC_NPU_CMD 调用 aclnnApplyAdamV2 并返回 var/m/v 别名。
  • step_size=None 的缺省处理: 未提供 step_size 时以 lr Tensor 作为默认输入,兼顾 Adam 模式忽略该输入与 mBART 以 lr 作为默认步长,同时满足旧版 tiling 对 optional 输入 shape 的检查。
  • 保留不带 out 的重载报错行为: 新增的 op_api::npu_bert_apply_adam 仍直接抛出 npu_bert_apply_adam is not implemented for Tensor,维持原有非 out 调用报错语义。
  • 新增公开 API 回归用例: 在 test/test_base_ops/test_npu_bertapplyadam.py 中增加覆盖默认参数、Adam/mBART 与非零模式映射、权重衰减、梯度裁剪、显式/缺省 step_size、var/m/v 原地更新与返回值地址一致性的用例,并对 mBART 缺省步长用例标注 @SupportedDevices(["Ascend950"])。
likedislike
Incorrect?
AtlasAccount
AtlasAccountCollaborators
11 days ago Comments:
atomgit-bot
atomgit-bot
11 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
ascend-robotascend-robotCollaborators
11 days ago Added label: needs-issue
ascend-robot
ascend-robotCollaborators
11 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-Ascend/op-plugin ❌ (0/2)(You can also ask: guoqi1024, shen_weiling, renyujin, huangjingwei, liangsongwei) ❌ (0/1)(You can also ask: 褚博宁, chujinjin, li_jing_hw, wanglijun55, zyw-hw)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

yourealize, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robotCollaborators
11 days ago Added label: ascend-cla/yes
ascend-robot
ascend-robotCollaborators
11 days ago Comments:

Linking Issue Notice

@yourealize , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

likedislike
ascend-robot
ascend-robotCollaborators
11 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
11 days ago Added label: docs-ci-pipeline-running
陈展熹
陈展熹
11 days ago Comments:

/check-pr

likedislike
ascend-robot
ascend-robotCollaborators
11 days ago Comments:

The following labels are not ready.

lgtm: Please wait for reviewers to review the code.

approved: Please wait for committers to review the code.

ci-pipeline-passed: Please wait for the CI test to be completed.

The following label exists.

needs-issue: The pull request must be linked to at least one issue. If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue.

likedislike
陈展熹
陈展熹
11 days ago Comments:

compile

likedislike
陈展熹陈展熹
11 days ago Pre merge succeeded (commit_id: c6bf2dbf11b964ce89503273e561c7554ef75253)
AtlasAccount
AtlasAccountCollaborators
11 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
AtlasAccountAtlasAccountCollaborators
11 days ago Added label: ci-pipeline-running
ascend-robot
ascend-robotCollaborators
11 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robot
ascend-robotCollaborators
11 days ago Comments:

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

likedislike
ascend-robotascend-robotCollaborators
11 days ago Removed label: docs-ci-pipeline-running
9 event messages are collapsed here。 See more
陈展熹陈展熹
10 days ago Modify description of pull request
陈展熹
陈展熹
10 days ago Comments:

compile

likedislike
陈展熹陈展熹
10 days ago Pre merge succeeded (commit_id: d3d3c5492909a334f8aa09ddfc1fdeb0939ea380)
AtlasAccount
AtlasAccountCollaborators
10 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
AtlasAccountAtlasAccountCollaborators
10 days ago Removed label: ci-pipeline-failed
AtlasAccountAtlasAccountCollaborators
10 days ago Added label: ci-pipeline-running
ascend-robot
ascend-robotCollaborators
10 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
10 days ago Removed label: docs-ci-pipeline-success
ascend-robotascend-robotCollaborators
10 days ago Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
10 days ago Comments:

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

likedislike
ascend-robotascend-robotCollaborators
10 days ago Removed label: docs-ci-pipeline-running
ascend-robotascend-robotCollaborators
10 days ago Added label: docs-ci-pipeline-success
陈展熹陈展熹
10 days ago Modify description of pull request
ascend-robot
ascend-robotCollaborators
10 days ago Comments:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robotCollaborators
10 days ago Removed label: docs-ci-pipeline-success
ascend-robotascend-robotCollaborators
10 days ago Added label: docs-ci-pipeline-running
ascend-robot
ascend-robotCollaborators
10 days ago Comments:

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

likedislike
ascend-robotascend-robotCollaborators
10 days ago Removed label: docs-ci-pipeline-running
ascend-robotascend-robotCollaborators
10 days ago Added label: docs-ci-pipeline-success
AtlasAccountAtlasAccountCollaborators
10 days ago Removed label: ci-pipeline-running
AtlasAccountAtlasAccountCollaborators
10 days ago Added label: ci-pipeline-passed